Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_desc.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "usb_desc.h"
  2. // USB Descriptors are binary data which the USB host reads to
  3. // automatically detect a USB device's capabilities. The format
  4. // and meaning of every field is documented in numerous USB
  5. // standards. When working with USB descriptors, despite the
  6. // complexity of the standards and poor writing quality in many
  7. // of those documents, remember descriptors are nothing more
  8. // than constant binary data that tells the USB host what the
  9. // device can do. Computers will load drivers based on this data.
  10. // Those drivers then communicate on the endpoints specified by
  11. // the descriptors.
  12. // To configure a new combination of interfaces or make minor
  13. // changes to existing configuration (eg, change the name or ID
  14. // numbers), usually you would edit "usb_desc.h". This file
  15. // is meant to be configured by the header, so generally it is
  16. // only edited to add completely new USB interfaces or features.
  17. // **************************************************************
  18. // USB Device
  19. // **************************************************************
  20. #define LSB(n) ((n) & 255)
  21. #define MSB(n) (((n) >> 8) & 255)
  22. // USB Device Descriptor. The USB host reads this first, to learn
  23. // what type of device is connected.
  24. static uint8_t device_descriptor[] = {
  25. 18, // bLength
  26. 1, // bDescriptorType
  27. 0x00, 0x02, // bcdUSB
  28. #ifdef DEVICE_CLASS
  29. DEVICE_CLASS, // bDeviceClass
  30. #else
  31. 0,
  32. #endif
  33. #ifdef DEVICE_SUBCLASS
  34. DEVICE_SUBCLASS, // bDeviceSubClass
  35. #else
  36. 0,
  37. #endif
  38. #ifdef DEVICE_PROTOCOL
  39. DEVICE_PROTOCOL, // bDeviceProtocol
  40. #else
  41. 0,
  42. #endif
  43. EP0_SIZE, // bMaxPacketSize0
  44. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  45. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  46. 0x00, 0x01, // bcdDevice
  47. 1, // iManufacturer
  48. 2, // iProduct
  49. 3, // iSerialNumber
  50. 1 // bNumConfigurations
  51. };
  52. // These descriptors must NOT be "const", because the USB DMA
  53. // has trouble accessing flash memory with enough bandwidth
  54. // while the processor is executing from flash.
  55. // **************************************************************
  56. // HID Report Descriptors
  57. // **************************************************************
  58. // Each HID interface needs a special report descriptor that tells
  59. // the meaning and format of the data.
  60. #ifdef KEYBOARD_INTERFACE
  61. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  62. static uint8_t keyboard_report_desc[] = {
  63. 0x05, 0x01, // Usage Page (Generic Desktop),
  64. 0x09, 0x06, // Usage (Keyboard),
  65. 0xA1, 0x01, // Collection (Application),
  66. 0x75, 0x01, // Report Size (1),
  67. 0x95, 0x08, // Report Count (8),
  68. 0x05, 0x07, // Usage Page (Key Codes),
  69. 0x19, 0xE0, // Usage Minimum (224),
  70. 0x29, 0xE7, // Usage Maximum (231),
  71. 0x15, 0x00, // Logical Minimum (0),
  72. 0x25, 0x01, // Logical Maximum (1),
  73. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  74. 0x95, 0x08, // Report Count (8),
  75. 0x75, 0x01, // Report Size (1),
  76. 0x15, 0x00, // Logical Minimum (0),
  77. 0x25, 0x01, // Logical Maximum (1),
  78. 0x05, 0x0C, // Usage Page (Consumer),
  79. 0x09, 0xE9, // Usage (Volume Increment),
  80. 0x09, 0xEA, // Usage (Volume Decrement),
  81. 0x09, 0xE2, // Usage (Mute),
  82. 0x09, 0xCD, // Usage (Play/Pause),
  83. 0x09, 0xB5, // Usage (Scan Next Track),
  84. 0x09, 0xB6, // Usage (Scan Previous Track),
  85. 0x09, 0xB7, // Usage (Stop),
  86. 0x09, 0xB8, // Usage (Eject),
  87. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys
  88. 0x95, 0x05, // Report Count (5),
  89. 0x75, 0x01, // Report Size (1),
  90. 0x05, 0x08, // Usage Page (LEDs),
  91. 0x19, 0x01, // Usage Minimum (1),
  92. 0x29, 0x05, // Usage Maximum (5),
  93. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  94. 0x95, 0x01, // Report Count (1),
  95. 0x75, 0x03, // Report Size (3),
  96. 0x91, 0x03, // Output (Constant), ;LED report padding
  97. 0x95, 0x06, // Report Count (6),
  98. 0x75, 0x08, // Report Size (8),
  99. 0x15, 0x00, // Logical Minimum (0),
  100. 0x25, 0x7F, // Logical Maximum(104),
  101. 0x05, 0x07, // Usage Page (Key Codes),
  102. 0x19, 0x00, // Usage Minimum (0),
  103. 0x29, 0x7F, // Usage Maximum (104),
  104. 0x81, 0x00, // Input (Data, Array), ;Normal keys
  105. 0xc0 // End Collection
  106. };
  107. #endif
  108. #ifdef MOUSE_INTERFACE
  109. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  110. static uint8_t mouse_report_desc[] = {
  111. 0x05, 0x01, // Usage Page (Generic Desktop)
  112. 0x09, 0x02, // Usage (Mouse)
  113. 0xA1, 0x01, // Collection (Application)
  114. 0x05, 0x09, // Usage Page (Button)
  115. 0x19, 0x01, // Usage Minimum (Button #1)
  116. 0x29, 0x03, // Usage Maximum (Button #3)
  117. 0x15, 0x00, // Logical Minimum (0)
  118. 0x25, 0x01, // Logical Maximum (1)
  119. 0x95, 0x03, // Report Count (3)
  120. 0x75, 0x01, // Report Size (1)
  121. 0x81, 0x02, // Input (Data, Variable, Absolute)
  122. 0x95, 0x01, // Report Count (1)
  123. 0x75, 0x05, // Report Size (5)
  124. 0x81, 0x03, // Input (Constant)
  125. 0x05, 0x01, // Usage Page (Generic Desktop)
  126. 0x09, 0x30, // Usage (X)
  127. 0x09, 0x31, // Usage (Y)
  128. 0x15, 0x81, // Logical Minimum (-127)
  129. 0x25, 0x7F, // Logical Maximum (127)
  130. 0x75, 0x08, // Report Size (8),
  131. 0x95, 0x02, // Report Count (2),
  132. 0x81, 0x06, // Input (Data, Variable, Relative)
  133. 0x09, 0x38, // Usage (Wheel)
  134. 0x95, 0x01, // Report Count (1),
  135. 0x81, 0x06, // Input (Data, Variable, Relative)
  136. 0xC0 // End Collection
  137. };
  138. #endif
  139. // **************************************************************
  140. // USB Configuration
  141. // **************************************************************
  142. // USB Configuration Descriptor. This huge descriptor tells all
  143. // of the devices capbilities.
  144. static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
  145. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  146. 9, // bLength;
  147. 2, // bDescriptorType;
  148. LSB(CONFIG_DESC_SIZE), // wTotalLength
  149. MSB(CONFIG_DESC_SIZE),
  150. NUM_INTERFACE, // bNumInterfaces
  151. 1, // bConfigurationValue
  152. 0, // iConfiguration
  153. 0xC0, // bmAttributes
  154. 50, // bMaxPower
  155. #ifdef CDC_IAD_DESCRIPTOR
  156. // interface association descriptor, USB ECN, Table 9-Z
  157. 8, // bLength
  158. 11, // bDescriptorType
  159. CDC_STATUS_INTERFACE, // bFirstInterface
  160. 2, // bInterfaceCount
  161. 0x02, // bFunctionClass
  162. 0x02, // bFunctionSubClass
  163. 0x01, // bFunctionProtocol
  164. 4, // iFunction
  165. #endif
  166. #ifdef CDC_DATA_INTERFACE
  167. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  168. 9, // bLength
  169. 4, // bDescriptorType
  170. CDC_STATUS_INTERFACE, // bInterfaceNumber
  171. 0, // bAlternateSetting
  172. 1, // bNumEndpoints
  173. 0x02, // bInterfaceClass
  174. 0x02, // bInterfaceSubClass
  175. 0x01, // bInterfaceProtocol
  176. 0, // iInterface
  177. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  178. 5, // bFunctionLength
  179. 0x24, // bDescriptorType
  180. 0x00, // bDescriptorSubtype
  181. 0x10, 0x01, // bcdCDC
  182. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  183. 5, // bFunctionLength
  184. 0x24, // bDescriptorType
  185. 0x01, // bDescriptorSubtype
  186. 0x01, // bmCapabilities
  187. 1, // bDataInterface
  188. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  189. 4, // bFunctionLength
  190. 0x24, // bDescriptorType
  191. 0x02, // bDescriptorSubtype
  192. 0x06, // bmCapabilities
  193. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  194. 5, // bFunctionLength
  195. 0x24, // bDescriptorType
  196. 0x06, // bDescriptorSubtype
  197. CDC_STATUS_INTERFACE, // bMasterInterface
  198. CDC_DATA_INTERFACE, // bSlaveInterface0
  199. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  200. 7, // bLength
  201. 5, // bDescriptorType
  202. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  203. 0x03, // bmAttributes (0x03=intr)
  204. CDC_ACM_SIZE, 0, // wMaxPacketSize
  205. 64, // bInterval
  206. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  207. 9, // bLength
  208. 4, // bDescriptorType
  209. CDC_DATA_INTERFACE, // bInterfaceNumber
  210. 0, // bAlternateSetting
  211. 2, // bNumEndpoints
  212. 0x0A, // bInterfaceClass
  213. 0x00, // bInterfaceSubClass
  214. 0x00, // bInterfaceProtocol
  215. 0, // iInterface
  216. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  217. 7, // bLength
  218. 5, // bDescriptorType
  219. CDC_RX_ENDPOINT, // bEndpointAddress
  220. 0x02, // bmAttributes (0x02=bulk)
  221. CDC_RX_SIZE, 0, // wMaxPacketSize
  222. 0, // bInterval
  223. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  224. 7, // bLength
  225. 5, // bDescriptorType
  226. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  227. 0x02, // bmAttributes (0x02=bulk)
  228. CDC_TX_SIZE, 0, // wMaxPacketSize
  229. 0, // bInterval
  230. #endif // CDC_DATA_INTERFACE
  231. #ifdef KEYBOARD_INTERFACE
  232. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  233. 9, // bLength
  234. 4, // bDescriptorType
  235. KEYBOARD_INTERFACE, // bInterfaceNumber
  236. 0, // bAlternateSetting
  237. 1, // bNumEndpoints
  238. 0x03, // bInterfaceClass (0x03 = HID)
  239. 0x01, // bInterfaceSubClass (0x01 = Boot)
  240. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  241. 0, // iInterface
  242. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  243. 9, // bLength
  244. 0x21, // bDescriptorType
  245. 0x11, 0x01, // bcdHID
  246. 0, // bCountryCode
  247. 1, // bNumDescriptors
  248. 0x22, // bDescriptorType
  249. LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
  250. MSB(sizeof(keyboard_report_desc)),
  251. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  252. 7, // bLength
  253. 5, // bDescriptorType
  254. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  255. 0x03, // bmAttributes (0x03=intr)
  256. KEYBOARD_SIZE, 0, // wMaxPacketSize
  257. KEYBOARD_INTERVAL, // bInterval
  258. #endif // KEYBOARD_INTERFACE
  259. #ifdef MOUSE_INTERFACE
  260. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  261. 9, // bLength
  262. 4, // bDescriptorType
  263. MOUSE_INTERFACE, // bInterfaceNumber
  264. 0, // bAlternateSetting
  265. 1, // bNumEndpoints
  266. 0x03, // bInterfaceClass (0x03 = HID)
  267. 0x01, // bInterfaceSubClass (0x01 = Boot)
  268. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  269. 0, // iInterface
  270. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  271. 9, // bLength
  272. 0x21, // bDescriptorType
  273. 0x11, 0x01, // bcdHID
  274. 0, // bCountryCode
  275. 1, // bNumDescriptors
  276. 0x22, // bDescriptorType
  277. LSB(sizeof(mouse_report_desc)), // wDescriptorLength
  278. MSB(sizeof(mouse_report_desc)),
  279. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  280. 7, // bLength
  281. 5, // bDescriptorType
  282. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  283. 0x03, // bmAttributes (0x03=intr)
  284. MOUSE_SIZE, 0, // wMaxPacketSize
  285. MOUSE_INTERVAL, // bInterval
  286. #endif // MOUSE_INTERFACE
  287. };
  288. // **************************************************************
  289. // String Descriptors
  290. // **************************************************************
  291. // The descriptors above can provide human readable strings,
  292. // referenced by index numbers. These descriptors are the
  293. // actual string data
  294. struct usb_string_descriptor_struct {
  295. uint8_t bLength;
  296. uint8_t bDescriptorType;
  297. uint16_t wString[];
  298. };
  299. static struct usb_string_descriptor_struct string0 = {
  300. 4,
  301. 3,
  302. {0x0409}
  303. };
  304. static struct usb_string_descriptor_struct string1 = {
  305. sizeof(STR_MANUFACTURER),
  306. 3,
  307. STR_MANUFACTURER
  308. };
  309. static struct usb_string_descriptor_struct string2 = {
  310. sizeof(STR_PRODUCT),
  311. 3,
  312. STR_PRODUCT
  313. };
  314. static struct usb_string_descriptor_struct string3 = {
  315. sizeof(STR_SERIAL),
  316. 3,
  317. STR_SERIAL
  318. };
  319. // **************************************************************
  320. // Descriptors List
  321. // **************************************************************
  322. // This table provides access to all the descriptor data above.
  323. const usb_descriptor_list_t usb_descriptor_list[] = {
  324. //wValue, wIndex, address, length
  325. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  326. {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
  327. #ifdef KEYBOARD_INTERFACE
  328. {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
  329. {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
  330. #endif
  331. #ifdef MOUSE_INTERFACE
  332. {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
  333. {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
  334. #endif
  335. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  336. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  337. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
  338. {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)},
  339. {0, 0, NULL, 0}
  340. };
  341. // **************************************************************
  342. // Endpoint Configuration
  343. // **************************************************************
  344. #if 0
  345. // 0x00 = not used
  346. // 0x19 = Recieve only
  347. // 0x15 = Transmit only
  348. // 0x1D = Transmit & Recieve
  349. //
  350. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  351. {
  352. 0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00,
  353. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  354. };
  355. #endif
  356. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  357. {
  358. #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
  359. ENDPOINT1_CONFIG,
  360. #elif (NUM_ENDPOINTS >= 1)
  361. ENDPOINT_UNUSED,
  362. #endif
  363. #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
  364. ENDPOINT2_CONFIG,
  365. #elif (NUM_ENDPOINTS >= 2)
  366. ENDPOINT_UNUSED,
  367. #endif
  368. #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
  369. ENDPOINT3_CONFIG,
  370. #elif (NUM_ENDPOINTS >= 3)
  371. ENDPOINT_UNUSED,
  372. #endif
  373. #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
  374. ENDPOINT4_CONFIG,
  375. #elif (NUM_ENDPOINTS >= 4)
  376. ENDPOINT_UNUSED,
  377. #endif
  378. #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
  379. ENDPOINT5_CONFIG,
  380. #elif (NUM_ENDPOINTS >= 5)
  381. ENDPOINT_UNUSED,
  382. #endif
  383. #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
  384. ENDPOINT6_CONFIG,
  385. #elif (NUM_ENDPOINTS >= 6)
  386. ENDPOINT_UNUSED,
  387. #endif
  388. #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
  389. ENDPOINT7_CONFIG,
  390. #elif (NUM_ENDPOINTS >= 7)
  391. ENDPOINT_UNUSED,
  392. #endif
  393. #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
  394. ENDPOINT8_CONFIG,
  395. #elif (NUM_ENDPOINTS >= 8)
  396. ENDPOINT_UNUSED,
  397. #endif
  398. #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
  399. ENDPOINT9_CONFIG,
  400. #elif (NUM_ENDPOINTS >= 9)
  401. ENDPOINT_UNUSED,
  402. #endif
  403. #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
  404. ENDPOINT10_CONFIG,
  405. #elif (NUM_ENDPOINTS >= 10)
  406. ENDPOINT_UNUSED,
  407. #endif
  408. #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
  409. ENDPOINT11_CONFIG,
  410. #elif (NUM_ENDPOINTS >= 11)
  411. ENDPOINT_UNUSED,
  412. #endif
  413. #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
  414. ENDPOINT12_CONFIG,
  415. #elif (NUM_ENDPOINTS >= 12)
  416. ENDPOINT_UNUSED,
  417. #endif
  418. #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
  419. ENDPOINT13_CONFIG,
  420. #elif (NUM_ENDPOINTS >= 13)
  421. ENDPOINT_UNUSED,
  422. #endif
  423. #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
  424. ENDPOINT14_CONFIG,
  425. #elif (NUM_ENDPOINTS >= 14)
  426. ENDPOINT_UNUSED,
  427. #endif
  428. #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
  429. ENDPOINT15_CONFIG,
  430. #elif (NUM_ENDPOINTS >= 15)
  431. ENDPOINT_UNUSED,
  432. #endif
  433. };