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.

894 satır
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. 0x85, 0x01, // REPORT_ID (1)
  147. 0x05, 0x09, // Usage Page (Button)
  148. 0x19, 0x01, // Usage Minimum (Button #1)
  149. 0x29, 0x08, // Usage Maximum (Button #8)
  150. 0x15, 0x00, // Logical Minimum (0)
  151. 0x25, 0x01, // Logical Maximum (1)
  152. 0x95, 0x08, // Report Count (8)
  153. 0x75, 0x01, // Report Size (1)
  154. 0x81, 0x02, // Input (Data, Variable, Absolute)
  155. 0x05, 0x01, // Usage Page (Generic Desktop)
  156. 0x09, 0x30, // Usage (X)
  157. 0x09, 0x31, // Usage (Y)
  158. 0x09, 0x38, // Usage (Wheel)
  159. 0x15, 0x81, // Logical Minimum (-127)
  160. 0x25, 0x7F, // Logical Maximum (127)
  161. 0x75, 0x08, // Report Size (8),
  162. 0x95, 0x03, // Report Count (3),
  163. 0x81, 0x06, // Input (Data, Variable, Relative)
  164. 0xC0, // End Collection
  165. 0x05, 0x01, // Usage Page (Generic Desktop)
  166. 0x09, 0x02, // Usage (Mouse)
  167. 0xA1, 0x01, // Collection (Application)
  168. 0x85, 0x02, // REPORT_ID (2)
  169. 0x05, 0x01, // Usage Page (Generic Desktop)
  170. 0x09, 0x30, // Usage (X)
  171. 0x09, 0x31, // Usage (Y)
  172. 0x15, 0x00, // Logical Minimum (0)
  173. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  174. 0x75, 0x10, // Report Size (16),
  175. 0x95, 0x02, // Report Count (2),
  176. 0x81, 0x02, // Input (Data, Variable, Absolute)
  177. 0xC0 // End Collection
  178. };
  179. #endif
  180. #ifdef JOYSTICK_INTERFACE
  181. static uint8_t joystick_report_desc[] = {
  182. 0x05, 0x01, // Usage Page (Generic Desktop)
  183. 0x09, 0x04, // Usage (Joystick)
  184. 0xA1, 0x01, // Collection (Application)
  185. 0x15, 0x00, // Logical Minimum (0)
  186. 0x25, 0x01, // Logical Maximum (1)
  187. 0x75, 0x01, // Report Size (1)
  188. 0x95, 0x20, // Report Count (32)
  189. 0x05, 0x09, // Usage Page (Button)
  190. 0x19, 0x01, // Usage Minimum (Button #1)
  191. 0x29, 0x20, // Usage Maximum (Button #32)
  192. 0x81, 0x02, // Input (variable,absolute)
  193. 0x15, 0x00, // Logical Minimum (0)
  194. 0x25, 0x07, // Logical Maximum (7)
  195. 0x35, 0x00, // Physical Minimum (0)
  196. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  197. 0x75, 0x04, // Report Size (4)
  198. 0x95, 0x01, // Report Count (1)
  199. 0x65, 0x14, // Unit (20)
  200. 0x05, 0x01, // Usage Page (Generic Desktop)
  201. 0x09, 0x39, // Usage (Hat switch)
  202. 0x81, 0x42, // Input (variable,absolute,null_state)
  203. 0x05, 0x01, // Usage Page (Generic Desktop)
  204. 0x09, 0x01, // Usage (Pointer)
  205. 0xA1, 0x00, // Collection ()
  206. 0x15, 0x00, // Logical Minimum (0)
  207. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  208. 0x75, 0x0A, // Report Size (10)
  209. 0x95, 0x04, // Report Count (4)
  210. 0x09, 0x30, // Usage (X)
  211. 0x09, 0x31, // Usage (Y)
  212. 0x09, 0x32, // Usage (Z)
  213. 0x09, 0x35, // Usage (Rz)
  214. 0x81, 0x02, // Input (variable,absolute)
  215. 0xC0, // End Collection
  216. 0x15, 0x00, // Logical Minimum (0)
  217. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  218. 0x75, 0x0A, // Report Size (10)
  219. 0x95, 0x02, // Report Count (2)
  220. 0x09, 0x36, // Usage (Slider)
  221. 0x09, 0x36, // Usage (Slider)
  222. 0x81, 0x02, // Input (variable,absolute)
  223. 0xC0 // End Collection
  224. };
  225. #endif
  226. #ifdef SEREMU_INTERFACE
  227. static uint8_t seremu_report_desc[] = {
  228. 0x06, 0xC9, 0xFF, // Usage Page 0xFFC9 (vendor defined)
  229. 0x09, 0x04, // Usage 0x04
  230. 0xA1, 0x5C, // Collection 0x5C
  231. 0x75, 0x08, // report size = 8 bits (global)
  232. 0x15, 0x00, // logical minimum = 0 (global)
  233. 0x26, 0xFF, 0x00, // logical maximum = 255 (global)
  234. 0x95, SEREMU_TX_SIZE, // report count (global)
  235. 0x09, 0x75, // usage (local)
  236. 0x81, 0x02, // Input
  237. 0x95, SEREMU_RX_SIZE, // report count (global)
  238. 0x09, 0x76, // usage (local)
  239. 0x91, 0x02, // Output
  240. 0x95, 0x04, // report count (global)
  241. 0x09, 0x76, // usage (local)
  242. 0xB1, 0x02, // Feature
  243. 0xC0 // end collection
  244. };
  245. #endif
  246. #ifdef RAWHID_INTERFACE
  247. static uint8_t rawhid_report_desc[] = {
  248. 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),
  249. 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
  250. 0xA1, 0x01, // Collection 0x01
  251. 0x75, 0x08, // report size = 8 bits
  252. 0x15, 0x00, // logical minimum = 0
  253. 0x26, 0xFF, 0x00, // logical maximum = 255
  254. 0x95, RAWHID_TX_SIZE, // report count
  255. 0x09, 0x01, // usage
  256. 0x81, 0x02, // Input (array)
  257. 0x95, RAWHID_RX_SIZE, // report count
  258. 0x09, 0x02, // usage
  259. 0x91, 0x02, // Output (array)
  260. 0xC0 // end collection
  261. };
  262. #endif
  263. #ifdef FLIGHTSIM_INTERFACE
  264. static uint8_t flightsim_report_desc[] = {
  265. 0x06, 0x1C, 0xFF, // Usage page = 0xFF1C
  266. 0x0A, 0x39, 0xA7, // Usage = 0xA739
  267. 0xA1, 0x01, // Collection 0x01
  268. 0x75, 0x08, // report size = 8 bits
  269. 0x15, 0x00, // logical minimum = 0
  270. 0x26, 0xFF, 0x00, // logical maximum = 255
  271. 0x95, FLIGHTSIM_TX_SIZE, // report count
  272. 0x09, 0x01, // usage
  273. 0x81, 0x02, // Input (array)
  274. 0x95, FLIGHTSIM_RX_SIZE, // report count
  275. 0x09, 0x02, // usage
  276. 0x91, 0x02, // Output (array)
  277. 0xC0 // end collection
  278. };
  279. #endif
  280. // **************************************************************
  281. // USB Configuration
  282. // **************************************************************
  283. // USB Configuration Descriptor. This huge descriptor tells all
  284. // of the devices capbilities.
  285. static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
  286. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  287. 9, // bLength;
  288. 2, // bDescriptorType;
  289. LSB(CONFIG_DESC_SIZE), // wTotalLength
  290. MSB(CONFIG_DESC_SIZE),
  291. NUM_INTERFACE, // bNumInterfaces
  292. 1, // bConfigurationValue
  293. 0, // iConfiguration
  294. 0xC0, // bmAttributes
  295. 50, // bMaxPower
  296. #ifdef CDC_IAD_DESCRIPTOR
  297. // interface association descriptor, USB ECN, Table 9-Z
  298. 8, // bLength
  299. 11, // bDescriptorType
  300. CDC_STATUS_INTERFACE, // bFirstInterface
  301. 2, // bInterfaceCount
  302. 0x02, // bFunctionClass
  303. 0x02, // bFunctionSubClass
  304. 0x01, // bFunctionProtocol
  305. 4, // iFunction
  306. #endif
  307. #ifdef CDC_DATA_INTERFACE
  308. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  309. 9, // bLength
  310. 4, // bDescriptorType
  311. CDC_STATUS_INTERFACE, // bInterfaceNumber
  312. 0, // bAlternateSetting
  313. 1, // bNumEndpoints
  314. 0x02, // bInterfaceClass
  315. 0x02, // bInterfaceSubClass
  316. 0x01, // bInterfaceProtocol
  317. 0, // iInterface
  318. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  319. 5, // bFunctionLength
  320. 0x24, // bDescriptorType
  321. 0x00, // bDescriptorSubtype
  322. 0x10, 0x01, // bcdCDC
  323. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  324. 5, // bFunctionLength
  325. 0x24, // bDescriptorType
  326. 0x01, // bDescriptorSubtype
  327. 0x01, // bmCapabilities
  328. 1, // bDataInterface
  329. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  330. 4, // bFunctionLength
  331. 0x24, // bDescriptorType
  332. 0x02, // bDescriptorSubtype
  333. 0x06, // bmCapabilities
  334. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  335. 5, // bFunctionLength
  336. 0x24, // bDescriptorType
  337. 0x06, // bDescriptorSubtype
  338. CDC_STATUS_INTERFACE, // bMasterInterface
  339. CDC_DATA_INTERFACE, // bSlaveInterface0
  340. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  341. 7, // bLength
  342. 5, // bDescriptorType
  343. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  344. 0x03, // bmAttributes (0x03=intr)
  345. CDC_ACM_SIZE, 0, // wMaxPacketSize
  346. 64, // bInterval
  347. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  348. 9, // bLength
  349. 4, // bDescriptorType
  350. CDC_DATA_INTERFACE, // bInterfaceNumber
  351. 0, // bAlternateSetting
  352. 2, // bNumEndpoints
  353. 0x0A, // bInterfaceClass
  354. 0x00, // bInterfaceSubClass
  355. 0x00, // bInterfaceProtocol
  356. 0, // iInterface
  357. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  358. 7, // bLength
  359. 5, // bDescriptorType
  360. CDC_RX_ENDPOINT, // bEndpointAddress
  361. 0x02, // bmAttributes (0x02=bulk)
  362. CDC_RX_SIZE, 0, // wMaxPacketSize
  363. 0, // bInterval
  364. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  365. 7, // bLength
  366. 5, // bDescriptorType
  367. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  368. 0x02, // bmAttributes (0x02=bulk)
  369. CDC_TX_SIZE, 0, // wMaxPacketSize
  370. 0, // bInterval
  371. #endif // CDC_DATA_INTERFACE
  372. #ifdef MIDI_INTERFACE
  373. // Standard MS Interface Descriptor,
  374. 9, // bLength
  375. 4, // bDescriptorType
  376. MIDI_INTERFACE, // bInterfaceNumber
  377. 0, // bAlternateSetting
  378. 2, // bNumEndpoints
  379. 0x01, // bInterfaceClass (0x01 = Audio)
  380. 0x03, // bInterfaceSubClass (0x03 = MIDI)
  381. 0x00, // bInterfaceProtocol (unused for MIDI)
  382. 0, // iInterface
  383. // MIDI MS Interface Header, USB MIDI 6.1.2.1, page 21, Table 6-2
  384. 7, // bLength
  385. 0x24, // bDescriptorType = CS_INTERFACE
  386. 0x01, // bDescriptorSubtype = MS_HEADER
  387. 0x00, 0x01, // bcdMSC = revision 01.00
  388. 0x41, 0x00, // wTotalLength
  389. // MIDI IN Jack Descriptor, B.4.3, Table B-7 (embedded), page 40
  390. 6, // bLength
  391. 0x24, // bDescriptorType = CS_INTERFACE
  392. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  393. 0x01, // bJackType = EMBEDDED
  394. 1, // bJackID, ID = 1
  395. 0, // iJack
  396. // MIDI IN Jack Descriptor, B.4.3, Table B-8 (external), page 40
  397. 6, // bLength
  398. 0x24, // bDescriptorType = CS_INTERFACE
  399. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  400. 0x02, // bJackType = EXTERNAL
  401. 2, // bJackID, ID = 2
  402. 0, // iJack
  403. // MIDI OUT Jack Descriptor, B.4.4, Table B-9, page 41
  404. 9,
  405. 0x24, // bDescriptorType = CS_INTERFACE
  406. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  407. 0x01, // bJackType = EMBEDDED
  408. 3, // bJackID, ID = 3
  409. 1, // bNrInputPins = 1 pin
  410. 2, // BaSourceID(1) = 2
  411. 1, // BaSourcePin(1) = first pin
  412. 0, // iJack
  413. // MIDI OUT Jack Descriptor, B.4.4, Table B-10, page 41
  414. 9,
  415. 0x24, // bDescriptorType = CS_INTERFACE
  416. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  417. 0x02, // bJackType = EXTERNAL
  418. 4, // bJackID, ID = 4
  419. 1, // bNrInputPins = 1 pin
  420. 1, // BaSourceID(1) = 1
  421. 1, // BaSourcePin(1) = first pin
  422. 0, // iJack
  423. // Standard Bulk OUT Endpoint Descriptor, B.5.1, Table B-11, pae 42
  424. 9, // bLength
  425. 5, // bDescriptorType = ENDPOINT
  426. MIDI_RX_ENDPOINT, // bEndpointAddress
  427. 0x02, // bmAttributes (0x02=bulk)
  428. MIDI_RX_SIZE, 0, // wMaxPacketSize
  429. 0, // bInterval
  430. 0, // bRefresh
  431. 0, // bSynchAddress
  432. // Class-specific MS Bulk OUT Endpoint Descriptor, B.5.2, Table B-12, page 42
  433. 5, // bLength
  434. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  435. 0x01, // bJackType = MS_GENERAL
  436. 1, // bNumEmbMIDIJack = 1 jack
  437. 1, // BaAssocJackID(1) = jack ID #1
  438. // Standard Bulk IN Endpoint Descriptor, B.5.1, Table B-11, pae 42
  439. 9, // bLength
  440. 5, // bDescriptorType = ENDPOINT
  441. MIDI_TX_ENDPOINT | 0x80, // bEndpointAddress
  442. 0x02, // bmAttributes (0x02=bulk)
  443. MIDI_TX_SIZE, 0, // wMaxPacketSize
  444. 0, // bInterval
  445. 0, // bRefresh
  446. 0, // bSynchAddress
  447. // Class-specific MS Bulk IN Endpoint Descriptor, B.5.2, Table B-12, page 42
  448. 5, // bLength
  449. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  450. 0x01, // bJackType = MS_GENERAL
  451. 1, // bNumEmbMIDIJack = 1 jack
  452. 3, // BaAssocJackID(1) = jack ID #3
  453. #endif // MIDI_INTERFACE
  454. #ifdef KEYBOARD_INTERFACE
  455. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  456. 9, // bLength
  457. 4, // bDescriptorType
  458. KEYBOARD_INTERFACE, // bInterfaceNumber
  459. 0, // bAlternateSetting
  460. 1, // bNumEndpoints
  461. 0x03, // bInterfaceClass (0x03 = HID)
  462. 0x01, // bInterfaceSubClass (0x01 = Boot)
  463. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  464. 0, // iInterface
  465. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  466. 9, // bLength
  467. 0x21, // bDescriptorType
  468. 0x11, 0x01, // bcdHID
  469. 0, // bCountryCode
  470. 1, // bNumDescriptors
  471. 0x22, // bDescriptorType
  472. LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
  473. MSB(sizeof(keyboard_report_desc)),
  474. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  475. 7, // bLength
  476. 5, // bDescriptorType
  477. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  478. 0x03, // bmAttributes (0x03=intr)
  479. KEYBOARD_SIZE, 0, // wMaxPacketSize
  480. KEYBOARD_INTERVAL, // bInterval
  481. #endif // KEYBOARD_INTERFACE
  482. #ifdef MOUSE_INTERFACE
  483. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  484. 9, // bLength
  485. 4, // bDescriptorType
  486. MOUSE_INTERFACE, // bInterfaceNumber
  487. 0, // bAlternateSetting
  488. 1, // bNumEndpoints
  489. 0x03, // bInterfaceClass (0x03 = HID)
  490. 0x00, // bInterfaceSubClass (0x01 = Boot)
  491. 0x00, // bInterfaceProtocol (0x02 = Mouse)
  492. 0, // iInterface
  493. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  494. 9, // bLength
  495. 0x21, // bDescriptorType
  496. 0x11, 0x01, // bcdHID
  497. 0, // bCountryCode
  498. 1, // bNumDescriptors
  499. 0x22, // bDescriptorType
  500. LSB(sizeof(mouse_report_desc)), // wDescriptorLength
  501. MSB(sizeof(mouse_report_desc)),
  502. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  503. 7, // bLength
  504. 5, // bDescriptorType
  505. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  506. 0x03, // bmAttributes (0x03=intr)
  507. MOUSE_SIZE, 0, // wMaxPacketSize
  508. MOUSE_INTERVAL, // bInterval
  509. #endif // MOUSE_INTERFACE
  510. #ifdef RAWHID_INTERFACE
  511. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  512. 9, // bLength
  513. 4, // bDescriptorType
  514. RAWHID_INTERFACE, // bInterfaceNumber
  515. 0, // bAlternateSetting
  516. 2, // bNumEndpoints
  517. 0x03, // bInterfaceClass (0x03 = HID)
  518. 0x00, // bInterfaceSubClass
  519. 0x00, // bInterfaceProtocol
  520. 0, // iInterface
  521. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  522. 9, // bLength
  523. 0x21, // bDescriptorType
  524. 0x11, 0x01, // bcdHID
  525. 0, // bCountryCode
  526. 1, // bNumDescriptors
  527. 0x22, // bDescriptorType
  528. LSB(sizeof(rawhid_report_desc)), // wDescriptorLength
  529. MSB(sizeof(rawhid_report_desc)),
  530. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  531. 7, // bLength
  532. 5, // bDescriptorType
  533. RAWHID_TX_ENDPOINT | 0x80, // bEndpointAddress
  534. 0x03, // bmAttributes (0x03=intr)
  535. RAWHID_TX_SIZE, 0, // wMaxPacketSize
  536. RAWHID_TX_INTERVAL, // bInterval
  537. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  538. 7, // bLength
  539. 5, // bDescriptorType
  540. RAWHID_RX_ENDPOINT, // bEndpointAddress
  541. 0x03, // bmAttributes (0x03=intr)
  542. RAWHID_RX_SIZE, 0, // wMaxPacketSize
  543. RAWHID_RX_INTERVAL, // bInterval
  544. #endif // RAWHID_INTERFACE
  545. #ifdef FLIGHTSIM_INTERFACE
  546. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  547. 9, // bLength
  548. 4, // bDescriptorType
  549. FLIGHTSIM_INTERFACE, // bInterfaceNumber
  550. 0, // bAlternateSetting
  551. 2, // bNumEndpoints
  552. 0x03, // bInterfaceClass (0x03 = HID)
  553. 0x00, // bInterfaceSubClass
  554. 0x00, // bInterfaceProtocol
  555. 0, // iInterface
  556. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  557. 9, // bLength
  558. 0x21, // bDescriptorType
  559. 0x11, 0x01, // bcdHID
  560. 0, // bCountryCode
  561. 1, // bNumDescriptors
  562. 0x22, // bDescriptorType
  563. LSB(sizeof(flightsim_report_desc)), // wDescriptorLength
  564. MSB(sizeof(flightsim_report_desc)),
  565. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  566. 7, // bLength
  567. 5, // bDescriptorType
  568. FLIGHTSIM_TX_ENDPOINT | 0x80, // bEndpointAddress
  569. 0x03, // bmAttributes (0x03=intr)
  570. FLIGHTSIM_TX_SIZE, 0, // wMaxPacketSize
  571. FLIGHTSIM_TX_INTERVAL, // bInterval
  572. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  573. 7, // bLength
  574. 5, // bDescriptorType
  575. FLIGHTSIM_RX_ENDPOINT, // bEndpointAddress
  576. 0x03, // bmAttributes (0x03=intr)
  577. FLIGHTSIM_RX_SIZE, 0, // wMaxPacketSize
  578. FLIGHTSIM_RX_INTERVAL, // bInterval
  579. #endif // FLIGHTSIM_INTERFACE
  580. #ifdef SEREMU_INTERFACE
  581. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  582. 9, // bLength
  583. 4, // bDescriptorType
  584. SEREMU_INTERFACE, // bInterfaceNumber
  585. 0, // bAlternateSetting
  586. 2, // bNumEndpoints
  587. 0x03, // bInterfaceClass (0x03 = HID)
  588. 0x00, // bInterfaceSubClass
  589. 0x00, // bInterfaceProtocol
  590. 0, // iInterface
  591. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  592. 9, // bLength
  593. 0x21, // bDescriptorType
  594. 0x11, 0x01, // bcdHID
  595. 0, // bCountryCode
  596. 1, // bNumDescriptors
  597. 0x22, // bDescriptorType
  598. LSB(sizeof(seremu_report_desc)), // wDescriptorLength
  599. MSB(sizeof(seremu_report_desc)),
  600. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  601. 7, // bLength
  602. 5, // bDescriptorType
  603. SEREMU_TX_ENDPOINT | 0x80, // bEndpointAddress
  604. 0x03, // bmAttributes (0x03=intr)
  605. SEREMU_TX_SIZE, 0, // wMaxPacketSize
  606. SEREMU_TX_INTERVAL, // bInterval
  607. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  608. 7, // bLength
  609. 5, // bDescriptorType
  610. SEREMU_RX_ENDPOINT, // bEndpointAddress
  611. 0x03, // bmAttributes (0x03=intr)
  612. SEREMU_RX_SIZE, 0, // wMaxPacketSize
  613. SEREMU_RX_INTERVAL, // bInterval
  614. #endif // SEREMU_INTERFACE
  615. #ifdef JOYSTICK_INTERFACE
  616. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  617. 9, // bLength
  618. 4, // bDescriptorType
  619. JOYSTICK_INTERFACE, // bInterfaceNumber
  620. 0, // bAlternateSetting
  621. 1, // bNumEndpoints
  622. 0x03, // bInterfaceClass (0x03 = HID)
  623. 0x00, // bInterfaceSubClass
  624. 0x00, // bInterfaceProtocol
  625. 0, // iInterface
  626. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  627. 9, // bLength
  628. 0x21, // bDescriptorType
  629. 0x11, 0x01, // bcdHID
  630. 0, // bCountryCode
  631. 1, // bNumDescriptors
  632. 0x22, // bDescriptorType
  633. LSB(sizeof(joystick_report_desc)), // wDescriptorLength
  634. MSB(sizeof(joystick_report_desc)),
  635. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  636. 7, // bLength
  637. 5, // bDescriptorType
  638. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  639. 0x03, // bmAttributes (0x03=intr)
  640. JOYSTICK_SIZE, 0, // wMaxPacketSize
  641. JOYSTICK_INTERVAL, // bInterval
  642. #endif // JOYSTICK_INTERFACE
  643. };
  644. // **************************************************************
  645. // String Descriptors
  646. // **************************************************************
  647. // The descriptors above can provide human readable strings,
  648. // referenced by index numbers. These descriptors are the
  649. // actual string data
  650. /* defined in usb_names.h
  651. struct usb_string_descriptor_struct {
  652. uint8_t bLength;
  653. uint8_t bDescriptorType;
  654. uint16_t wString[];
  655. };
  656. */
  657. extern struct usb_string_descriptor_struct usb_string_manufacturer_name
  658. __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
  659. extern struct usb_string_descriptor_struct usb_string_product_name
  660. __attribute__ ((weak, alias("usb_string_product_name_default")));
  661. extern struct usb_string_descriptor_struct usb_string_serial_number
  662. __attribute__ ((weak, alias("usb_string_serial_number_default")));
  663. struct usb_string_descriptor_struct string0 = {
  664. 4,
  665. 3,
  666. {0x0409}
  667. };
  668. struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
  669. 2 + MANUFACTURER_NAME_LEN * 2,
  670. 3,
  671. MANUFACTURER_NAME
  672. };
  673. struct usb_string_descriptor_struct usb_string_product_name_default = {
  674. 2 + PRODUCT_NAME_LEN * 2,
  675. 3,
  676. PRODUCT_NAME
  677. };
  678. struct usb_string_descriptor_struct usb_string_serial_number_default = {
  679. 12,
  680. 3,
  681. {0,0,0,0,0,0,0,0,0,0}
  682. };
  683. void usb_init_serialnumber(void)
  684. {
  685. char buf[11];
  686. uint32_t i, num;
  687. __disable_irq();
  688. FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
  689. FTFL_FCCOB0 = 0x41;
  690. FTFL_FCCOB1 = 15;
  691. FTFL_FSTAT = FTFL_FSTAT_CCIF;
  692. while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
  693. num = *(uint32_t *)&FTFL_FCCOB7;
  694. __enable_irq();
  695. // add extra zero to work around OS-X CDC-ACM driver bug
  696. if (num < 10000000) num = num * 10;
  697. ultoa(num, buf, 10);
  698. for (i=0; i<10; i++) {
  699. char c = buf[i];
  700. if (!c) break;
  701. usb_string_serial_number_default.wString[i] = c;
  702. }
  703. usb_string_serial_number_default.bLength = i * 2 + 2;
  704. }
  705. // **************************************************************
  706. // Descriptors List
  707. // **************************************************************
  708. // This table provides access to all the descriptor data above.
  709. const usb_descriptor_list_t usb_descriptor_list[] = {
  710. //wValue, wIndex, address, length
  711. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  712. {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
  713. #ifdef SEREMU_INTERFACE
  714. {0x2200, SEREMU_INTERFACE, seremu_report_desc, sizeof(seremu_report_desc)},
  715. {0x2100, SEREMU_INTERFACE, config_descriptor+SEREMU_DESC_OFFSET, 9},
  716. #endif
  717. #ifdef KEYBOARD_INTERFACE
  718. {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
  719. {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
  720. #endif
  721. #ifdef MOUSE_INTERFACE
  722. {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
  723. {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
  724. #endif
  725. #ifdef JOYSTICK_INTERFACE
  726. {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
  727. {0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_DESC_OFFSET, 9},
  728. #endif
  729. #ifdef RAWHID_INTERFACE
  730. {0x2200, RAWHID_INTERFACE, rawhid_report_desc, sizeof(rawhid_report_desc)},
  731. {0x2100, RAWHID_INTERFACE, config_descriptor+RAWHID_DESC_OFFSET, 9},
  732. #endif
  733. #ifdef FLIGHTSIM_INTERFACE
  734. {0x2200, FLIGHTSIM_INTERFACE, flightsim_report_desc, sizeof(flightsim_report_desc)},
  735. {0x2100, FLIGHTSIM_INTERFACE, config_descriptor+FLIGHTSIM_DESC_OFFSET, 9},
  736. #endif
  737. {0x0300, 0x0000, (const uint8_t *)&string0, 0},
  738. {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
  739. {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
  740. {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
  741. //{0x0301, 0x0409, (const uint8_t *)&string1, 0},
  742. //{0x0302, 0x0409, (const uint8_t *)&string2, 0},
  743. //{0x0303, 0x0409, (const uint8_t *)&string3, 0},
  744. {0, 0, NULL, 0}
  745. };
  746. // **************************************************************
  747. // Endpoint Configuration
  748. // **************************************************************
  749. #if 0
  750. // 0x00 = not used
  751. // 0x19 = Recieve only
  752. // 0x15 = Transmit only
  753. // 0x1D = Transmit & Recieve
  754. //
  755. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  756. {
  757. 0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00,
  758. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  759. };
  760. #endif
  761. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  762. {
  763. #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
  764. ENDPOINT1_CONFIG,
  765. #elif (NUM_ENDPOINTS >= 1)
  766. ENDPOINT_UNUSED,
  767. #endif
  768. #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
  769. ENDPOINT2_CONFIG,
  770. #elif (NUM_ENDPOINTS >= 2)
  771. ENDPOINT_UNUSED,
  772. #endif
  773. #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
  774. ENDPOINT3_CONFIG,
  775. #elif (NUM_ENDPOINTS >= 3)
  776. ENDPOINT_UNUSED,
  777. #endif
  778. #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
  779. ENDPOINT4_CONFIG,
  780. #elif (NUM_ENDPOINTS >= 4)
  781. ENDPOINT_UNUSED,
  782. #endif
  783. #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
  784. ENDPOINT5_CONFIG,
  785. #elif (NUM_ENDPOINTS >= 5)
  786. ENDPOINT_UNUSED,
  787. #endif
  788. #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
  789. ENDPOINT6_CONFIG,
  790. #elif (NUM_ENDPOINTS >= 6)
  791. ENDPOINT_UNUSED,
  792. #endif
  793. #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
  794. ENDPOINT7_CONFIG,
  795. #elif (NUM_ENDPOINTS >= 7)
  796. ENDPOINT_UNUSED,
  797. #endif
  798. #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
  799. ENDPOINT8_CONFIG,
  800. #elif (NUM_ENDPOINTS >= 8)
  801. ENDPOINT_UNUSED,
  802. #endif
  803. #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
  804. ENDPOINT9_CONFIG,
  805. #elif (NUM_ENDPOINTS >= 9)
  806. ENDPOINT_UNUSED,
  807. #endif
  808. #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
  809. ENDPOINT10_CONFIG,
  810. #elif (NUM_ENDPOINTS >= 10)
  811. ENDPOINT_UNUSED,
  812. #endif
  813. #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
  814. ENDPOINT11_CONFIG,
  815. #elif (NUM_ENDPOINTS >= 11)
  816. ENDPOINT_UNUSED,
  817. #endif
  818. #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
  819. ENDPOINT12_CONFIG,
  820. #elif (NUM_ENDPOINTS >= 12)
  821. ENDPOINT_UNUSED,
  822. #endif
  823. #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
  824. ENDPOINT13_CONFIG,
  825. #elif (NUM_ENDPOINTS >= 13)
  826. ENDPOINT_UNUSED,
  827. #endif
  828. #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
  829. ENDPOINT14_CONFIG,
  830. #elif (NUM_ENDPOINTS >= 14)
  831. ENDPOINT_UNUSED,
  832. #endif
  833. #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
  834. ENDPOINT15_CONFIG,
  835. #elif (NUM_ENDPOINTS >= 15)
  836. ENDPOINT_UNUSED,
  837. #endif
  838. };