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.

886 lines
44KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_desc.h"
  31. #include "usb_names.h"
  32. #include "mk20dx128.h"
  33. #include "avr_functions.h"
  34. // USB Descriptors are binary data which the USB host reads to
  35. // automatically detect a USB device's capabilities. The format
  36. // and meaning of every field is documented in numerous USB
  37. // standards. When working with USB descriptors, despite the
  38. // complexity of the standards and poor writing quality in many
  39. // of those documents, remember descriptors are nothing more
  40. // than constant binary data that tells the USB host what the
  41. // device can do. Computers will load drivers based on this data.
  42. // Those drivers then communicate on the endpoints specified by
  43. // the descriptors.
  44. // To configure a new combination of interfaces or make minor
  45. // changes to existing configuration (eg, change the name or ID
  46. // numbers), usually you would edit "usb_desc.h". This file
  47. // is meant to be configured by the header, so generally it is
  48. // only edited to add completely new USB interfaces or features.
  49. // **************************************************************
  50. // USB Device
  51. // **************************************************************
  52. #define LSB(n) ((n) & 255)
  53. #define MSB(n) (((n) >> 8) & 255)
  54. // USB Device Descriptor. The USB host reads this first, to learn
  55. // what type of device is connected.
  56. static uint8_t device_descriptor[] = {
  57. 18, // bLength
  58. 1, // bDescriptorType
  59. 0x00, 0x02, // bcdUSB
  60. #ifdef DEVICE_CLASS
  61. DEVICE_CLASS, // bDeviceClass
  62. #else
  63. 0,
  64. #endif
  65. #ifdef DEVICE_SUBCLASS
  66. DEVICE_SUBCLASS, // bDeviceSubClass
  67. #else
  68. 0,
  69. #endif
  70. #ifdef DEVICE_PROTOCOL
  71. DEVICE_PROTOCOL, // bDeviceProtocol
  72. #else
  73. 0,
  74. #endif
  75. EP0_SIZE, // bMaxPacketSize0
  76. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  77. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  78. 0x00, 0x01, // bcdDevice
  79. 1, // iManufacturer
  80. 2, // iProduct
  81. 3, // iSerialNumber
  82. 1 // bNumConfigurations
  83. };
  84. // These descriptors must NOT be "const", because the USB DMA
  85. // has trouble accessing flash memory with enough bandwidth
  86. // while the processor is executing from flash.
  87. // **************************************************************
  88. // HID Report Descriptors
  89. // **************************************************************
  90. // Each HID interface needs a special report descriptor that tells
  91. // the meaning and format of the data.
  92. #ifdef KEYBOARD_INTERFACE
  93. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  94. static uint8_t keyboard_report_desc[] = {
  95. 0x05, 0x01, // Usage Page (Generic Desktop),
  96. 0x09, 0x06, // Usage (Keyboard),
  97. 0xA1, 0x01, // Collection (Application),
  98. 0x75, 0x01, // Report Size (1),
  99. 0x95, 0x08, // Report Count (8),
  100. 0x05, 0x07, // Usage Page (Key Codes),
  101. 0x19, 0xE0, // Usage Minimum (224),
  102. 0x29, 0xE7, // Usage Maximum (231),
  103. 0x15, 0x00, // Logical Minimum (0),
  104. 0x25, 0x01, // Logical Maximum (1),
  105. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  106. 0x95, 0x08, // Report Count (8),
  107. 0x75, 0x01, // Report Size (1),
  108. 0x15, 0x00, // Logical Minimum (0),
  109. 0x25, 0x01, // Logical Maximum (1),
  110. 0x05, 0x0C, // Usage Page (Consumer),
  111. 0x09, 0xE9, // Usage (Volume Increment),
  112. 0x09, 0xEA, // Usage (Volume Decrement),
  113. 0x09, 0xE2, // Usage (Mute),
  114. 0x09, 0xCD, // Usage (Play/Pause),
  115. 0x09, 0xB5, // Usage (Scan Next Track),
  116. 0x09, 0xB6, // Usage (Scan Previous Track),
  117. 0x09, 0xB7, // Usage (Stop),
  118. 0x09, 0xB8, // Usage (Eject),
  119. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys
  120. 0x95, 0x05, // Report Count (5),
  121. 0x75, 0x01, // Report Size (1),
  122. 0x05, 0x08, // Usage Page (LEDs),
  123. 0x19, 0x01, // Usage Minimum (1),
  124. 0x29, 0x05, // Usage Maximum (5),
  125. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  126. 0x95, 0x01, // Report Count (1),
  127. 0x75, 0x03, // Report Size (3),
  128. 0x91, 0x03, // Output (Constant), ;LED report padding
  129. 0x95, 0x06, // Report Count (6),
  130. 0x75, 0x08, // Report Size (8),
  131. 0x15, 0x00, // Logical Minimum (0),
  132. 0x25, 0x7F, // Logical Maximum(104),
  133. 0x05, 0x07, // Usage Page (Key Codes),
  134. 0x19, 0x00, // Usage Minimum (0),
  135. 0x29, 0x7F, // Usage Maximum (104),
  136. 0x81, 0x00, // Input (Data, Array), ;Normal keys
  137. 0xc0 // End Collection
  138. };
  139. #endif
  140. #ifdef MOUSE_INTERFACE
  141. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  142. static uint8_t mouse_report_desc[] = {
  143. 0x05, 0x01, // Usage Page (Generic Desktop)
  144. 0x09, 0x02, // Usage (Mouse)
  145. 0xA1, 0x01, // Collection (Application)
  146. 0x05, 0x09, // Usage Page (Button)
  147. 0x19, 0x01, // Usage Minimum (Button #1)
  148. 0x29, 0x03, // Usage Maximum (Button #3)
  149. 0x15, 0x00, // Logical Minimum (0)
  150. 0x25, 0x01, // Logical Maximum (1)
  151. 0x95, 0x03, // Report Count (3)
  152. 0x75, 0x01, // Report Size (1)
  153. 0x81, 0x02, // Input (Data, Variable, Absolute)
  154. 0x95, 0x01, // Report Count (1)
  155. 0x75, 0x05, // Report Size (5)
  156. 0x81, 0x03, // Input (Constant)
  157. 0x05, 0x01, // Usage Page (Generic Desktop)
  158. 0x09, 0x30, // Usage (X)
  159. 0x09, 0x31, // Usage (Y)
  160. 0x15, 0x00, // Logical Minimum (0)
  161. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  162. 0x75, 0x10, // Report Size (16),
  163. 0x95, 0x02, // Report Count (2),
  164. 0x81, 0x02, // Input (Data, Variable, Absolute)
  165. 0x09, 0x38, // Usage (Wheel)
  166. 0x15, 0x81, // Logical Minimum (-127)
  167. 0x25, 0x7F, // Logical Maximum (127)
  168. 0x75, 0x08, // Report Size (8),
  169. 0x95, 0x01, // Report Count (1),
  170. 0x81, 0x06, // Input (Data, Variable, Relative)
  171. 0xC0 // End Collection
  172. };
  173. #endif
  174. #ifdef JOYSTICK_INTERFACE
  175. static uint8_t joystick_report_desc[] = {
  176. 0x05, 0x01, // Usage Page (Generic Desktop)
  177. 0x09, 0x04, // Usage (Joystick)
  178. 0xA1, 0x01, // Collection (Application)
  179. 0x15, 0x00, // Logical Minimum (0)
  180. 0x25, 0x01, // Logical Maximum (1)
  181. 0x75, 0x01, // Report Size (1)
  182. 0x95, 0x20, // Report Count (32)
  183. 0x05, 0x09, // Usage Page (Button)
  184. 0x19, 0x01, // Usage Minimum (Button #1)
  185. 0x29, 0x20, // Usage Maximum (Button #32)
  186. 0x81, 0x02, // Input (variable,absolute)
  187. 0x15, 0x00, // Logical Minimum (0)
  188. 0x25, 0x07, // Logical Maximum (7)
  189. 0x35, 0x00, // Physical Minimum (0)
  190. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  191. 0x75, 0x04, // Report Size (4)
  192. 0x95, 0x01, // Report Count (1)
  193. 0x65, 0x14, // Unit (20)
  194. 0x05, 0x01, // Usage Page (Generic Desktop)
  195. 0x09, 0x39, // Usage (Hat switch)
  196. 0x81, 0x42, // Input (variable,absolute,null_state)
  197. 0x05, 0x01, // Usage Page (Generic Desktop)
  198. 0x09, 0x01, // Usage (Pointer)
  199. 0xA1, 0x00, // Collection ()
  200. 0x15, 0x00, // Logical Minimum (0)
  201. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  202. 0x75, 0x0A, // Report Size (10)
  203. 0x95, 0x04, // Report Count (4)
  204. 0x09, 0x30, // Usage (X)
  205. 0x09, 0x31, // Usage (Y)
  206. 0x09, 0x32, // Usage (Z)
  207. 0x09, 0x35, // Usage (Rz)
  208. 0x81, 0x02, // Input (variable,absolute)
  209. 0xC0, // End Collection
  210. 0x15, 0x00, // Logical Minimum (0)
  211. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  212. 0x75, 0x0A, // Report Size (10)
  213. 0x95, 0x02, // Report Count (2)
  214. 0x09, 0x36, // Usage (Slider)
  215. 0x09, 0x36, // Usage (Slider)
  216. 0x81, 0x02, // Input (variable,absolute)
  217. 0xC0 // End Collection
  218. };
  219. #endif
  220. #ifdef SEREMU_INTERFACE
  221. static uint8_t seremu_report_desc[] = {
  222. 0x06, 0xC9, 0xFF, // Usage Page 0xFFC9 (vendor defined)
  223. 0x09, 0x04, // Usage 0x04
  224. 0xA1, 0x5C, // Collection 0x5C
  225. 0x75, 0x08, // report size = 8 bits (global)
  226. 0x15, 0x00, // logical minimum = 0 (global)
  227. 0x26, 0xFF, 0x00, // logical maximum = 255 (global)
  228. 0x95, SEREMU_TX_SIZE, // report count (global)
  229. 0x09, 0x75, // usage (local)
  230. 0x81, 0x02, // Input
  231. 0x95, SEREMU_RX_SIZE, // report count (global)
  232. 0x09, 0x76, // usage (local)
  233. 0x91, 0x02, // Output
  234. 0x95, 0x04, // report count (global)
  235. 0x09, 0x76, // usage (local)
  236. 0xB1, 0x02, // Feature
  237. 0xC0 // end collection
  238. };
  239. #endif
  240. #ifdef RAWHID_INTERFACE
  241. static uint8_t rawhid_report_desc[] = {
  242. 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),
  243. 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
  244. 0xA1, 0x01, // Collection 0x01
  245. 0x75, 0x08, // report size = 8 bits
  246. 0x15, 0x00, // logical minimum = 0
  247. 0x26, 0xFF, 0x00, // logical maximum = 255
  248. 0x95, RAWHID_TX_SIZE, // report count
  249. 0x09, 0x01, // usage
  250. 0x81, 0x02, // Input (array)
  251. 0x95, RAWHID_RX_SIZE, // report count
  252. 0x09, 0x02, // usage
  253. 0x91, 0x02, // Output (array)
  254. 0xC0 // end collection
  255. };
  256. #endif
  257. #ifdef FLIGHTSIM_INTERFACE
  258. static uint8_t flightsim_report_desc[] = {
  259. 0x06, 0x1C, 0xFF, // Usage page = 0xFF1C
  260. 0x0A, 0x39, 0xA7, // Usage = 0xA739
  261. 0xA1, 0x01, // Collection 0x01
  262. 0x75, 0x08, // report size = 8 bits
  263. 0x15, 0x00, // logical minimum = 0
  264. 0x26, 0xFF, 0x00, // logical maximum = 255
  265. 0x95, FLIGHTSIM_TX_SIZE, // report count
  266. 0x09, 0x01, // usage
  267. 0x81, 0x02, // Input (array)
  268. 0x95, FLIGHTSIM_RX_SIZE, // report count
  269. 0x09, 0x02, // usage
  270. 0x91, 0x02, // Output (array)
  271. 0xC0 // end collection
  272. };
  273. #endif
  274. // **************************************************************
  275. // USB Configuration
  276. // **************************************************************
  277. // USB Configuration Descriptor. This huge descriptor tells all
  278. // of the devices capbilities.
  279. static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
  280. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  281. 9, // bLength;
  282. 2, // bDescriptorType;
  283. LSB(CONFIG_DESC_SIZE), // wTotalLength
  284. MSB(CONFIG_DESC_SIZE),
  285. NUM_INTERFACE, // bNumInterfaces
  286. 1, // bConfigurationValue
  287. 0, // iConfiguration
  288. 0xC0, // bmAttributes
  289. 50, // bMaxPower
  290. #ifdef CDC_IAD_DESCRIPTOR
  291. // interface association descriptor, USB ECN, Table 9-Z
  292. 8, // bLength
  293. 11, // bDescriptorType
  294. CDC_STATUS_INTERFACE, // bFirstInterface
  295. 2, // bInterfaceCount
  296. 0x02, // bFunctionClass
  297. 0x02, // bFunctionSubClass
  298. 0x01, // bFunctionProtocol
  299. 4, // iFunction
  300. #endif
  301. #ifdef CDC_DATA_INTERFACE
  302. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  303. 9, // bLength
  304. 4, // bDescriptorType
  305. CDC_STATUS_INTERFACE, // bInterfaceNumber
  306. 0, // bAlternateSetting
  307. 1, // bNumEndpoints
  308. 0x02, // bInterfaceClass
  309. 0x02, // bInterfaceSubClass
  310. 0x01, // bInterfaceProtocol
  311. 0, // iInterface
  312. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  313. 5, // bFunctionLength
  314. 0x24, // bDescriptorType
  315. 0x00, // bDescriptorSubtype
  316. 0x10, 0x01, // bcdCDC
  317. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  318. 5, // bFunctionLength
  319. 0x24, // bDescriptorType
  320. 0x01, // bDescriptorSubtype
  321. 0x01, // bmCapabilities
  322. 1, // bDataInterface
  323. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  324. 4, // bFunctionLength
  325. 0x24, // bDescriptorType
  326. 0x02, // bDescriptorSubtype
  327. 0x06, // bmCapabilities
  328. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  329. 5, // bFunctionLength
  330. 0x24, // bDescriptorType
  331. 0x06, // bDescriptorSubtype
  332. CDC_STATUS_INTERFACE, // bMasterInterface
  333. CDC_DATA_INTERFACE, // bSlaveInterface0
  334. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  335. 7, // bLength
  336. 5, // bDescriptorType
  337. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  338. 0x03, // bmAttributes (0x03=intr)
  339. CDC_ACM_SIZE, 0, // wMaxPacketSize
  340. 64, // bInterval
  341. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  342. 9, // bLength
  343. 4, // bDescriptorType
  344. CDC_DATA_INTERFACE, // bInterfaceNumber
  345. 0, // bAlternateSetting
  346. 2, // bNumEndpoints
  347. 0x0A, // bInterfaceClass
  348. 0x00, // bInterfaceSubClass
  349. 0x00, // bInterfaceProtocol
  350. 0, // iInterface
  351. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  352. 7, // bLength
  353. 5, // bDescriptorType
  354. CDC_RX_ENDPOINT, // bEndpointAddress
  355. 0x02, // bmAttributes (0x02=bulk)
  356. CDC_RX_SIZE, 0, // wMaxPacketSize
  357. 0, // bInterval
  358. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  359. 7, // bLength
  360. 5, // bDescriptorType
  361. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  362. 0x02, // bmAttributes (0x02=bulk)
  363. CDC_TX_SIZE, 0, // wMaxPacketSize
  364. 0, // bInterval
  365. #endif // CDC_DATA_INTERFACE
  366. #ifdef MIDI_INTERFACE
  367. // Standard MS Interface Descriptor,
  368. 9, // bLength
  369. 4, // bDescriptorType
  370. MIDI_INTERFACE, // bInterfaceNumber
  371. 0, // bAlternateSetting
  372. 2, // bNumEndpoints
  373. 0x01, // bInterfaceClass (0x01 = Audio)
  374. 0x03, // bInterfaceSubClass (0x03 = MIDI)
  375. 0x00, // bInterfaceProtocol (unused for MIDI)
  376. 0, // iInterface
  377. // MIDI MS Interface Header, USB MIDI 6.1.2.1, page 21, Table 6-2
  378. 7, // bLength
  379. 0x24, // bDescriptorType = CS_INTERFACE
  380. 0x01, // bDescriptorSubtype = MS_HEADER
  381. 0x00, 0x01, // bcdMSC = revision 01.00
  382. 0x41, 0x00, // wTotalLength
  383. // MIDI IN Jack Descriptor, B.4.3, Table B-7 (embedded), page 40
  384. 6, // bLength
  385. 0x24, // bDescriptorType = CS_INTERFACE
  386. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  387. 0x01, // bJackType = EMBEDDED
  388. 1, // bJackID, ID = 1
  389. 0, // iJack
  390. // MIDI IN Jack Descriptor, B.4.3, Table B-8 (external), page 40
  391. 6, // bLength
  392. 0x24, // bDescriptorType = CS_INTERFACE
  393. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  394. 0x02, // bJackType = EXTERNAL
  395. 2, // bJackID, ID = 2
  396. 0, // iJack
  397. // MIDI OUT Jack Descriptor, B.4.4, Table B-9, page 41
  398. 9,
  399. 0x24, // bDescriptorType = CS_INTERFACE
  400. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  401. 0x01, // bJackType = EMBEDDED
  402. 3, // bJackID, ID = 3
  403. 1, // bNrInputPins = 1 pin
  404. 2, // BaSourceID(1) = 2
  405. 1, // BaSourcePin(1) = first pin
  406. 0, // iJack
  407. // MIDI OUT Jack Descriptor, B.4.4, Table B-10, page 41
  408. 9,
  409. 0x24, // bDescriptorType = CS_INTERFACE
  410. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  411. 0x02, // bJackType = EXTERNAL
  412. 4, // bJackID, ID = 4
  413. 1, // bNrInputPins = 1 pin
  414. 1, // BaSourceID(1) = 1
  415. 1, // BaSourcePin(1) = first pin
  416. 0, // iJack
  417. // Standard Bulk OUT Endpoint Descriptor, B.5.1, Table B-11, pae 42
  418. 9, // bLength
  419. 5, // bDescriptorType = ENDPOINT
  420. MIDI_RX_ENDPOINT, // bEndpointAddress
  421. 0x02, // bmAttributes (0x02=bulk)
  422. MIDI_RX_SIZE, 0, // wMaxPacketSize
  423. 0, // bInterval
  424. 0, // bRefresh
  425. 0, // bSynchAddress
  426. // Class-specific MS Bulk OUT Endpoint Descriptor, B.5.2, Table B-12, page 42
  427. 5, // bLength
  428. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  429. 0x01, // bJackType = MS_GENERAL
  430. 1, // bNumEmbMIDIJack = 1 jack
  431. 1, // BaAssocJackID(1) = jack ID #1
  432. // Standard Bulk IN Endpoint Descriptor, B.5.1, Table B-11, pae 42
  433. 9, // bLength
  434. 5, // bDescriptorType = ENDPOINT
  435. MIDI_TX_ENDPOINT | 0x80, // bEndpointAddress
  436. 0x02, // bmAttributes (0x02=bulk)
  437. MIDI_TX_SIZE, 0, // wMaxPacketSize
  438. 0, // bInterval
  439. 0, // bRefresh
  440. 0, // bSynchAddress
  441. // Class-specific MS Bulk IN Endpoint Descriptor, B.5.2, Table B-12, page 42
  442. 5, // bLength
  443. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  444. 0x01, // bJackType = MS_GENERAL
  445. 1, // bNumEmbMIDIJack = 1 jack
  446. 3, // BaAssocJackID(1) = jack ID #3
  447. #endif // MIDI_INTERFACE
  448. #ifdef KEYBOARD_INTERFACE
  449. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  450. 9, // bLength
  451. 4, // bDescriptorType
  452. KEYBOARD_INTERFACE, // bInterfaceNumber
  453. 0, // bAlternateSetting
  454. 1, // bNumEndpoints
  455. 0x03, // bInterfaceClass (0x03 = HID)
  456. 0x01, // bInterfaceSubClass (0x01 = Boot)
  457. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  458. 0, // iInterface
  459. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  460. 9, // bLength
  461. 0x21, // bDescriptorType
  462. 0x11, 0x01, // bcdHID
  463. 0, // bCountryCode
  464. 1, // bNumDescriptors
  465. 0x22, // bDescriptorType
  466. LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
  467. MSB(sizeof(keyboard_report_desc)),
  468. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  469. 7, // bLength
  470. 5, // bDescriptorType
  471. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  472. 0x03, // bmAttributes (0x03=intr)
  473. KEYBOARD_SIZE, 0, // wMaxPacketSize
  474. KEYBOARD_INTERVAL, // bInterval
  475. #endif // KEYBOARD_INTERFACE
  476. #ifdef MOUSE_INTERFACE
  477. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  478. 9, // bLength
  479. 4, // bDescriptorType
  480. MOUSE_INTERFACE, // bInterfaceNumber
  481. 0, // bAlternateSetting
  482. 1, // bNumEndpoints
  483. 0x03, // bInterfaceClass (0x03 = HID)
  484. 0x00, // bInterfaceSubClass (0x01 = Boot)
  485. 0x00, // bInterfaceProtocol (0x02 = Mouse)
  486. 0, // iInterface
  487. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  488. 9, // bLength
  489. 0x21, // bDescriptorType
  490. 0x11, 0x01, // bcdHID
  491. 0, // bCountryCode
  492. 1, // bNumDescriptors
  493. 0x22, // bDescriptorType
  494. LSB(sizeof(mouse_report_desc)), // wDescriptorLength
  495. MSB(sizeof(mouse_report_desc)),
  496. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  497. 7, // bLength
  498. 5, // bDescriptorType
  499. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  500. 0x03, // bmAttributes (0x03=intr)
  501. MOUSE_SIZE, 0, // wMaxPacketSize
  502. MOUSE_INTERVAL, // bInterval
  503. #endif // MOUSE_INTERFACE
  504. #ifdef RAWHID_INTERFACE
  505. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  506. 9, // bLength
  507. 4, // bDescriptorType
  508. RAWHID_INTERFACE, // bInterfaceNumber
  509. 0, // bAlternateSetting
  510. 2, // bNumEndpoints
  511. 0x03, // bInterfaceClass (0x03 = HID)
  512. 0x00, // bInterfaceSubClass
  513. 0x00, // bInterfaceProtocol
  514. 0, // iInterface
  515. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  516. 9, // bLength
  517. 0x21, // bDescriptorType
  518. 0x11, 0x01, // bcdHID
  519. 0, // bCountryCode
  520. 1, // bNumDescriptors
  521. 0x22, // bDescriptorType
  522. LSB(sizeof(rawhid_report_desc)), // wDescriptorLength
  523. MSB(sizeof(rawhid_report_desc)),
  524. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  525. 7, // bLength
  526. 5, // bDescriptorType
  527. RAWHID_TX_ENDPOINT | 0x80, // bEndpointAddress
  528. 0x03, // bmAttributes (0x03=intr)
  529. RAWHID_TX_SIZE, 0, // wMaxPacketSize
  530. RAWHID_TX_INTERVAL, // bInterval
  531. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  532. 7, // bLength
  533. 5, // bDescriptorType
  534. RAWHID_RX_ENDPOINT, // bEndpointAddress
  535. 0x03, // bmAttributes (0x03=intr)
  536. RAWHID_RX_SIZE, 0, // wMaxPacketSize
  537. RAWHID_RX_INTERVAL, // bInterval
  538. #endif // RAWHID_INTERFACE
  539. #ifdef FLIGHTSIM_INTERFACE
  540. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  541. 9, // bLength
  542. 4, // bDescriptorType
  543. FLIGHTSIM_INTERFACE, // bInterfaceNumber
  544. 0, // bAlternateSetting
  545. 2, // bNumEndpoints
  546. 0x03, // bInterfaceClass (0x03 = HID)
  547. 0x00, // bInterfaceSubClass
  548. 0x00, // bInterfaceProtocol
  549. 0, // iInterface
  550. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  551. 9, // bLength
  552. 0x21, // bDescriptorType
  553. 0x11, 0x01, // bcdHID
  554. 0, // bCountryCode
  555. 1, // bNumDescriptors
  556. 0x22, // bDescriptorType
  557. LSB(sizeof(flightsim_report_desc)), // wDescriptorLength
  558. MSB(sizeof(flightsim_report_desc)),
  559. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  560. 7, // bLength
  561. 5, // bDescriptorType
  562. FLIGHTSIM_TX_ENDPOINT | 0x80, // bEndpointAddress
  563. 0x03, // bmAttributes (0x03=intr)
  564. FLIGHTSIM_TX_SIZE, 0, // wMaxPacketSize
  565. FLIGHTSIM_TX_INTERVAL, // bInterval
  566. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  567. 7, // bLength
  568. 5, // bDescriptorType
  569. FLIGHTSIM_RX_ENDPOINT, // bEndpointAddress
  570. 0x03, // bmAttributes (0x03=intr)
  571. FLIGHTSIM_RX_SIZE, 0, // wMaxPacketSize
  572. FLIGHTSIM_RX_INTERVAL, // bInterval
  573. #endif // FLIGHTSIM_INTERFACE
  574. #ifdef SEREMU_INTERFACE
  575. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  576. 9, // bLength
  577. 4, // bDescriptorType
  578. SEREMU_INTERFACE, // bInterfaceNumber
  579. 0, // bAlternateSetting
  580. 2, // bNumEndpoints
  581. 0x03, // bInterfaceClass (0x03 = HID)
  582. 0x00, // bInterfaceSubClass
  583. 0x00, // bInterfaceProtocol
  584. 0, // iInterface
  585. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  586. 9, // bLength
  587. 0x21, // bDescriptorType
  588. 0x11, 0x01, // bcdHID
  589. 0, // bCountryCode
  590. 1, // bNumDescriptors
  591. 0x22, // bDescriptorType
  592. LSB(sizeof(seremu_report_desc)), // wDescriptorLength
  593. MSB(sizeof(seremu_report_desc)),
  594. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  595. 7, // bLength
  596. 5, // bDescriptorType
  597. SEREMU_TX_ENDPOINT | 0x80, // bEndpointAddress
  598. 0x03, // bmAttributes (0x03=intr)
  599. SEREMU_TX_SIZE, 0, // wMaxPacketSize
  600. SEREMU_TX_INTERVAL, // bInterval
  601. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  602. 7, // bLength
  603. 5, // bDescriptorType
  604. SEREMU_RX_ENDPOINT, // bEndpointAddress
  605. 0x03, // bmAttributes (0x03=intr)
  606. SEREMU_RX_SIZE, 0, // wMaxPacketSize
  607. SEREMU_RX_INTERVAL, // bInterval
  608. #endif // SEREMU_INTERFACE
  609. #ifdef JOYSTICK_INTERFACE
  610. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  611. 9, // bLength
  612. 4, // bDescriptorType
  613. JOYSTICK_INTERFACE, // bInterfaceNumber
  614. 0, // bAlternateSetting
  615. 1, // bNumEndpoints
  616. 0x03, // bInterfaceClass (0x03 = HID)
  617. 0x00, // bInterfaceSubClass
  618. 0x00, // bInterfaceProtocol
  619. 0, // iInterface
  620. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  621. 9, // bLength
  622. 0x21, // bDescriptorType
  623. 0x11, 0x01, // bcdHID
  624. 0, // bCountryCode
  625. 1, // bNumDescriptors
  626. 0x22, // bDescriptorType
  627. LSB(sizeof(joystick_report_desc)), // wDescriptorLength
  628. MSB(sizeof(joystick_report_desc)),
  629. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  630. 7, // bLength
  631. 5, // bDescriptorType
  632. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  633. 0x03, // bmAttributes (0x03=intr)
  634. JOYSTICK_SIZE, 0, // wMaxPacketSize
  635. JOYSTICK_INTERVAL, // bInterval
  636. #endif // JOYSTICK_INTERFACE
  637. };
  638. // **************************************************************
  639. // String Descriptors
  640. // **************************************************************
  641. // The descriptors above can provide human readable strings,
  642. // referenced by index numbers. These descriptors are the
  643. // actual string data
  644. /* defined in usb_names.h
  645. struct usb_string_descriptor_struct {
  646. uint8_t bLength;
  647. uint8_t bDescriptorType;
  648. uint16_t wString[];
  649. };
  650. */
  651. extern struct usb_string_descriptor_struct usb_string_manufacturer_name
  652. __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
  653. extern struct usb_string_descriptor_struct usb_string_product_name
  654. __attribute__ ((weak, alias("usb_string_product_name_default")));
  655. extern struct usb_string_descriptor_struct usb_string_serial_number
  656. __attribute__ ((weak, alias("usb_string_serial_number_default")));
  657. struct usb_string_descriptor_struct string0 = {
  658. 4,
  659. 3,
  660. {0x0409}
  661. };
  662. struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
  663. 2 + MANUFACTURER_NAME_LEN * 2,
  664. 3,
  665. MANUFACTURER_NAME
  666. };
  667. struct usb_string_descriptor_struct usb_string_product_name_default = {
  668. 2 + PRODUCT_NAME_LEN * 2,
  669. 3,
  670. PRODUCT_NAME
  671. };
  672. struct usb_string_descriptor_struct usb_string_serial_number_default = {
  673. 12,
  674. 3,
  675. {0,0,0,0,0,0,0,0,0,0}
  676. };
  677. void usb_init_serialnumber(void)
  678. {
  679. char buf[11];
  680. uint32_t i, num;
  681. __disable_irq();
  682. FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
  683. FTFL_FCCOB0 = 0x41;
  684. FTFL_FCCOB1 = 15;
  685. FTFL_FSTAT = FTFL_FSTAT_CCIF;
  686. while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
  687. num = *(uint32_t *)&FTFL_FCCOB7;
  688. __enable_irq();
  689. ultoa(num, buf, 10);
  690. for (i=0; i<10; i++) {
  691. char c = buf[i];
  692. if (!c) break;
  693. usb_string_serial_number_default.wString[i] = c;
  694. }
  695. usb_string_serial_number_default.bLength = i * 2 + 2;
  696. }
  697. // **************************************************************
  698. // Descriptors List
  699. // **************************************************************
  700. // This table provides access to all the descriptor data above.
  701. const usb_descriptor_list_t usb_descriptor_list[] = {
  702. //wValue, wIndex, address, length
  703. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  704. {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
  705. #ifdef SEREMU_INTERFACE
  706. {0x2200, SEREMU_INTERFACE, seremu_report_desc, sizeof(seremu_report_desc)},
  707. {0x2100, SEREMU_INTERFACE, config_descriptor+SEREMU_DESC_OFFSET, 9},
  708. #endif
  709. #ifdef KEYBOARD_INTERFACE
  710. {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
  711. {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
  712. #endif
  713. #ifdef MOUSE_INTERFACE
  714. {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
  715. {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
  716. #endif
  717. #ifdef JOYSTICK_INTERFACE
  718. {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
  719. {0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_DESC_OFFSET, 9},
  720. #endif
  721. #ifdef RAWHID_INTERFACE
  722. {0x2200, RAWHID_INTERFACE, rawhid_report_desc, sizeof(rawhid_report_desc)},
  723. {0x2100, RAWHID_INTERFACE, config_descriptor+RAWHID_DESC_OFFSET, 9},
  724. #endif
  725. #ifdef FLIGHTSIM_INTERFACE
  726. {0x2200, FLIGHTSIM_INTERFACE, flightsim_report_desc, sizeof(flightsim_report_desc)},
  727. {0x2100, FLIGHTSIM_INTERFACE, config_descriptor+FLIGHTSIM_DESC_OFFSET, 9},
  728. #endif
  729. {0x0300, 0x0000, (const uint8_t *)&string0, 0},
  730. {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
  731. {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
  732. {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
  733. //{0x0301, 0x0409, (const uint8_t *)&string1, 0},
  734. //{0x0302, 0x0409, (const uint8_t *)&string2, 0},
  735. //{0x0303, 0x0409, (const uint8_t *)&string3, 0},
  736. {0, 0, NULL, 0}
  737. };
  738. // **************************************************************
  739. // Endpoint Configuration
  740. // **************************************************************
  741. #if 0
  742. // 0x00 = not used
  743. // 0x19 = Recieve only
  744. // 0x15 = Transmit only
  745. // 0x1D = Transmit & Recieve
  746. //
  747. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  748. {
  749. 0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00,
  750. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  751. };
  752. #endif
  753. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  754. {
  755. #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
  756. ENDPOINT1_CONFIG,
  757. #elif (NUM_ENDPOINTS >= 1)
  758. ENDPOINT_UNUSED,
  759. #endif
  760. #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
  761. ENDPOINT2_CONFIG,
  762. #elif (NUM_ENDPOINTS >= 2)
  763. ENDPOINT_UNUSED,
  764. #endif
  765. #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
  766. ENDPOINT3_CONFIG,
  767. #elif (NUM_ENDPOINTS >= 3)
  768. ENDPOINT_UNUSED,
  769. #endif
  770. #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
  771. ENDPOINT4_CONFIG,
  772. #elif (NUM_ENDPOINTS >= 4)
  773. ENDPOINT_UNUSED,
  774. #endif
  775. #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
  776. ENDPOINT5_CONFIG,
  777. #elif (NUM_ENDPOINTS >= 5)
  778. ENDPOINT_UNUSED,
  779. #endif
  780. #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
  781. ENDPOINT6_CONFIG,
  782. #elif (NUM_ENDPOINTS >= 6)
  783. ENDPOINT_UNUSED,
  784. #endif
  785. #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
  786. ENDPOINT7_CONFIG,
  787. #elif (NUM_ENDPOINTS >= 7)
  788. ENDPOINT_UNUSED,
  789. #endif
  790. #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
  791. ENDPOINT8_CONFIG,
  792. #elif (NUM_ENDPOINTS >= 8)
  793. ENDPOINT_UNUSED,
  794. #endif
  795. #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
  796. ENDPOINT9_CONFIG,
  797. #elif (NUM_ENDPOINTS >= 9)
  798. ENDPOINT_UNUSED,
  799. #endif
  800. #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
  801. ENDPOINT10_CONFIG,
  802. #elif (NUM_ENDPOINTS >= 10)
  803. ENDPOINT_UNUSED,
  804. #endif
  805. #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
  806. ENDPOINT11_CONFIG,
  807. #elif (NUM_ENDPOINTS >= 11)
  808. ENDPOINT_UNUSED,
  809. #endif
  810. #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
  811. ENDPOINT12_CONFIG,
  812. #elif (NUM_ENDPOINTS >= 12)
  813. ENDPOINT_UNUSED,
  814. #endif
  815. #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
  816. ENDPOINT13_CONFIG,
  817. #elif (NUM_ENDPOINTS >= 13)
  818. ENDPOINT_UNUSED,
  819. #endif
  820. #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
  821. ENDPOINT14_CONFIG,
  822. #elif (NUM_ENDPOINTS >= 14)
  823. ENDPOINT_UNUSED,
  824. #endif
  825. #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
  826. ENDPOINT15_CONFIG,
  827. #elif (NUM_ENDPOINTS >= 15)
  828. ENDPOINT_UNUSED,
  829. #endif
  830. };