-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUsbDescriptor.c
322 lines (271 loc) · 9.65 KB
/
UsbDescriptor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "Type.h"
#include "UsbDef.h"
#include "hid.h"
#include "UsbDescriptor.h"
/* device descriptor */
static UINT8C _DevDesc[] =
{
0x12, /*bLength */
USB_DESCR_TYP_DEVICE, /*bDescriptorType*/
#if USB_DEVICE_TYPE == LOW_SPEED_DEVICE
0x10, 0x01, /*bcdUSB */
#elif USB_DEVICE_TYPE == FULL_SPEED_DEVICE
0x00, 0x02, /*bcdUSB */
#else
#error "usb device type error"
#endif
0x00, /*bDeviceClass*/
0x00, /*bDeviceSubClass*/
0x00, /*bDeviceProtocol*/
DEFAULT_ENDP0_SIZE, /*bMaxPacketSize*/
0x3b, 0x41, /*idVendor (0x413b)*/
0x07, 0x21, /*idProduct = 0x2107*/
0x00, 0x00, /*bcdDevice rel. 0.00*/
0x01, /*Index of string descriptor describing
manufacturer */
0x02, /*Index of string descriptor describing
product*/
0x03, /*Index of string descriptor describing the
device serial number */
0x01 /*bNumConfigurations*/
};
/* device descriptor */
const DescriptorData DevDesc =
{
_DevDesc,
sizeof(_DevDesc)
};
/* keyboard report descriptor */
static UINT8C _KeyRepDesc[] =
{
HID_UsagePage(HID_USAGE_PAGE_GENERIC),
HID_Usage(HID_USAGE_GENERIC_KEYBOARD),
HID_Collection(HID_Application),
HID_UsagePage(HID_USAGE_PAGE_KEYBOARD),
HID_UsageMin(0xE0),
HID_UsageMax(0xE7),
HID_LogicalMin(0),
HID_LogicalMax(1),
HID_ReportSize(1),
HID_ReportCount(8),
HID_Input(HID_Data | HID_Variable | HID_Absolute),
HID_ReportSize(8),
HID_ReportCount(1),
HID_Input(HID_Constant | HID_Variable | HID_Absolute),
HID_ReportSize(1),
HID_ReportCount(5),
HID_UsagePage(HID_USAGE_PAGE_LED),
HID_UsageMin(0x01),
HID_UsageMax(0x05),
HID_Output(HID_Data | HID_Variable | HID_Absolute),
HID_ReportSize(3),
HID_ReportCount(1),
HID_Output(HID_Constant | HID_Variable | HID_Absolute),
HID_ReportSize(8),
HID_ReportCount(6),
HID_LogicalMin(0),
HID_LogicalMax(0xFF),
HID_UsagePage(HID_USAGE_PAGE_KEYBOARD),
HID_UsageMin(0x00),
HID_UsageMax(0x91),
HID_Input(HID_Data | HID_Array | HID_Absolute),
HID_EndCollection,
};
/* keyboard report descriptor */
const DescriptorData KeyRepDesc =
{
_KeyRepDesc,
sizeof(_KeyRepDesc)
};
/* mouse report descriptor */
static UINT8C _MouseRepDesc[] =
{
HID_UsagePage(HID_USAGE_PAGE_GENERIC),
HID_Usage(HID_USAGE_GENERIC_MOUSE),
HID_Collection(HID_Application),
HID_Usage(HID_USAGE_GENERIC_POINTER),
HID_Collection(HID_Physical),
HID_UsagePage(HID_USAGE_PAGE_BUTTON),
HID_UsageMin(0x01),
HID_UsageMax(0x03),
HID_LogicalMin(0),
HID_LogicalMax(1),
HID_ReportSize(1),
HID_ReportCount(3),
HID_Input(HID_Data | HID_Variable | HID_Absolute),
HID_ReportSize(5),
HID_ReportCount(1),
HID_Input(HID_Constant | HID_Variable | HID_Absolute),
HID_UsagePage(HID_USAGE_PAGE_GENERIC),
HID_Usage(HID_USAGE_GENERIC_X),
HID_Usage(HID_USAGE_GENERIC_Y),
HID_Usage(HID_USAGE_GENERIC_WHEEL),
HID_LogicalMin(0x81),
HID_LogicalMax(0x7F),
HID_ReportSize(8),
HID_ReportCount(3),
HID_Input(HID_Data | HID_Variable | HID_Relative),
HID_EndCollection,
HID_EndCollection,
};
/* mouse report descriptor */
const DescriptorData MouseRepDesc =
{
_MouseRepDesc,
sizeof(_MouseRepDesc)
};
#define TOTAL_CONFIG_DESCR_SIZE sizeof(USB_CFG_DESCR) + \
sizeof(USB_ITF_DESCR) + \
sizeof(USB_HID_DESCR) + \
sizeof(USB_ENDP_DESCR) + \
sizeof(USB_ITF_DESCR) + \
sizeof(USB_HID_DESCR) + \
sizeof(USB_ENDP_DESCR)
#define KEYBOARD_REPORT_DESCR_SIZE sizeof(_KeyRepDesc)
#define MOUSE_REPORT_DESCR_SIZE sizeof(_MouseRepDesc)
/* configuration descriptor */
static UINT8C _CfgDesc[] =
{
0x09, /* bLength: Configuation Descriptor size */
USB_DESCR_TYP_CONFIG, /* bDescriptorType: Configuration */
TOTAL_CONFIG_DESCR_SIZE & 0xff, (TOTAL_CONFIG_DESCR_SIZE >> 8) & 0xff,/* wTotalLength: Bytes returned */
0x02, /*bNumInterfaces: 2 interface*/
0x01, /*bConfigurationValue: Configuration value*/
0x00, /*iConfiguration: Index of string descriptor describing
the configuration*/
0x80 | (1 << 6) | (1 << 5), /*bmAttributes: self powered, remote wakeup */
100 / 2, /*MaxPower 100 mA: this current is used for detecting Vbus*/
/************** Descriptor of keyboard interface ****************/
0x09, /*bLength: Interface Descriptor size*/
USB_DESCR_TYP_INTERF, /*bDescriptorType: Interface descriptor type*/
0x00, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0x00, /*iInterface: Index of string descriptor*/
/******************** Descriptor of keyboard HID ********************/
0x09, /*bLength: HID Descriptor size*/
USB_DESCR_TYP_HID, /*bDescriptorType: HID*/
0x11, 0x01, /*bcdHID: HID Class Spec release number*/
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
USB_DESCR_TYP_REPORT, /*bDescriptorType*/
KEYBOARD_REPORT_DESCR_SIZE & 0xff, (KEYBOARD_REPORT_DESCR_SIZE >> 8) & 0xff, /*wItemLength: Total length of Report descriptor*/
/******************** Descriptor of keyboard endpoint ********************/
0x07, /*bLength: Endpoint Descriptor size*/
USB_DESCR_TYP_ENDP, /*bDescriptorType:*/
0x81, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
MAX_PACKET_SIZE & 0xff, (MAX_PACKET_SIZE >> 8) & 0xff, /*wMaxPacketSize: 32 Byte max */
0x0a, /*bInterval: Polling Interval (10 ms)*/
/************** Descriptor of mousse interface ****************/
0x09, /*bLength: Interface Descriptor size*/
USB_DESCR_TYP_INTERF, /*bDescriptorType: Interface descriptor type*/
0x01, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0x00, /*iInterface: Index of string descriptor*/
/******************** Descriptor of mouse HID ********************/
0x09, /*bLength: HID Descriptor size*/
USB_DESCR_TYP_HID, /*bDescriptorType: HID*/
0x10, 0x01, /*bcdHID: HID Class Spec release number*/
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
USB_DESCR_TYP_REPORT,/*bDescriptorType*/
MOUSE_REPORT_DESCR_SIZE & 0xff, (MOUSE_REPORT_DESCR_SIZE >> 8) & 0xff, /*wItemLength: Total length of Report descriptor*/
/******************** Descriptor of mouse endpoint ********************/
0x07, /*bLength: Endpoint Descriptor size*/
USB_DESCR_TYP_ENDP, /*bDescriptorType:*/
0x82, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
MAX_PACKET_SIZE & 0xff, (MAX_PACKET_SIZE >> 8) & 0xff, /*wMaxPacketSize: 32 Byte max */
0x0a /*bInterval: Polling Interval (10 ms)*/
};
/* configure descriptor */
const DescriptorData CfgDesc =
{
_CfgDesc,
sizeof(_CfgDesc)
};
/* USB Device Qualifier */
static UINT8C _USB_DeviceQualifier[] =
{
10, /* bLength */
USB_DESCR_TYP_QUALIF, /* bDescriptorType */
0x00, 0x02, /*bcdUSB */
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
DEFAULT_ENDP0_SIZE, /* bMaxPacketSize0 */
0x00, /* bNumOtherSpeedConfigurations */
0x00 /* bReserved */
};
/* device qualifier descriptor */
const DescriptorData DeviceQualifierCfg =
{
_USB_DeviceQualifier,
sizeof(_USB_DeviceQualifier)
};
#define STRING_LANGID_SIZE 4
#define STRING_VENDOR_SIZE 12
#define STRING_PRODUCT_SIZE 22
#define STRING_SERIAL_SIZE 18
static UINT8C StringLangID[STRING_LANGID_SIZE] =
{
STRING_LANGID_SIZE,
USB_DESCR_TYP_STRING,
0x09, 0x04
};
static UINT8C StringVecdor[STRING_VENDOR_SIZE] =
{
STRING_VENDOR_SIZE,
USB_DESCR_TYP_STRING,
/* vendor */
'L', 0,
'a', 0,
'n', 0,
'b', 0,
'e', 0
};
static UINT8C StringProduct[STRING_PRODUCT_SIZE] =
{
STRING_PRODUCT_SIZE,
USB_DESCR_TYP_STRING,
/* product */
'K', 0,
'V', 0,
'M', 0,
' ', 0,
'D', 0,
'e', 0,
'v', 0,
'i', 0,
'c', 0,
'e', 0
};
UINT8X StringSerial[STRING_SERIAL_SIZE] =
{
STRING_SERIAL_SIZE,
USB_DESCR_TYP_STRING,
/* serial */
'0', 0,
'0', 0,
'0', 0,
'0', 0,
'0', 0,
'0', 0,
'0', 0,
'0', 0
};
const DescriptorData StringDescriptors[4] =
{
{ StringLangID, sizeof(StringLangID) },
{ StringVecdor, sizeof(StringVecdor) },
{ StringProduct, sizeof(StringProduct) },
{ StringSerial, sizeof(StringSerial) }
};