Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

922 lines
36KB

  1. /* USB Serial for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2008 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "usb_common.h"
  24. #include "usb_private.h"
  25. /**************************************************************************
  26. *
  27. * Endpoint Buffer Configuration
  28. *
  29. **************************************************************************/
  30. static const uint8_t PROGMEM endpoint_config_table[] = {
  31. EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
  32. EP_TYPE_INTERRUPT_IN, EP_SIZE(CDC_ACM_SIZE) | CDC_ACM_BUFFER,
  33. EP_TYPE_BULK_OUT, EP_SIZE(CDC_RX_SIZE) | CDC_RX_BUFFER,
  34. EP_TYPE_BULK_IN, EP_SIZE(CDC_TX_SIZE) | CDC_TX_BUFFER,
  35. EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER,
  36. EP_TYPE_INTERRUPT_IN, EP_SIZE(JOYSTICK_SIZE) | JOYSTICK_BUFFER,
  37. };
  38. /**************************************************************************
  39. *
  40. * Descriptor Data
  41. *
  42. **************************************************************************/
  43. // Descriptors are the data that your computer reads when it auto-detects
  44. // this USB device (called "enumeration" in USB lingo). The most commonly
  45. // changed items are editable at the top of this file. Changing things
  46. // in here should only be done by those who've read chapter 9 of the USB
  47. // spec and relevant portions of any USB class specifications!
  48. static const uint8_t PROGMEM device_descriptor[] = {
  49. 18, // bLength
  50. 1, // bDescriptorType
  51. 0x00, 0x02, // bcdUSB
  52. 0xEF, // bDeviceClass
  53. 0x02, // bDeviceSubClass
  54. 0x01, // bDeviceProtocol
  55. ENDPOINT0_SIZE, // bMaxPacketSize0
  56. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  57. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  58. 0x02, 0x01, // bcdDevice
  59. 1, // iManufacturer
  60. 2, // iProduct
  61. 3, // iSerialNumber
  62. 1 // bNumConfigurations
  63. };
  64. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  65. static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
  66. 0x05, 0x01, // Usage Page (Generic Desktop),
  67. 0x09, 0x06, // Usage (Keyboard),
  68. 0xA1, 0x01, // Collection (Application),
  69. 0x75, 0x01, // Report Size (1),
  70. 0x95, 0x08, // Report Count (8),
  71. 0x05, 0x07, // Usage Page (Key Codes),
  72. 0x19, 0xE0, // Usage Minimum (224),
  73. 0x29, 0xE7, // Usage Maximum (231),
  74. 0x15, 0x00, // Logical Minimum (0),
  75. 0x25, 0x01, // Logical Maximum (1),
  76. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  77. 0x95, 0x08, // Report Count (8),
  78. 0x75, 0x01, // Report Size (1),
  79. 0x15, 0x00, // Logical Minimum (0),
  80. 0x25, 0x01, // Logical Maximum (1),
  81. 0x05, 0x0C, // Usage Page (Consumer),
  82. 0x09, 0xE9, // Usage (Volume Increment),
  83. 0x09, 0xEA, // Usage (Volume Decrement),
  84. 0x09, 0xE2, // Usage (Mute),
  85. 0x09, 0xCD, // Usage (Play/Pause),
  86. 0x09, 0xB5, // Usage (Scan Next Track),
  87. 0x09, 0xB6, // Usage (Scan Previous Track),
  88. 0x09, 0xB7, // Usage (Stop),
  89. 0x09, 0xB8, // Usage (Eject),
  90. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys
  91. 0x95, 0x05, // Report Count (5),
  92. 0x75, 0x01, // Report Size (1),
  93. 0x05, 0x08, // Usage Page (LEDs),
  94. 0x19, 0x01, // Usage Minimum (1),
  95. 0x29, 0x05, // Usage Maximum (5),
  96. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  97. 0x95, 0x01, // Report Count (1),
  98. 0x75, 0x03, // Report Size (3),
  99. 0x91, 0x03, // Output (Constant), ;LED report padding
  100. 0x95, 0x06, // Report Count (6),
  101. 0x75, 0x08, // Report Size (8),
  102. 0x15, 0x00, // Logical Minimum (0),
  103. 0x25, 0x7F, // Logical Maximum(104),
  104. 0x05, 0x07, // Usage Page (Key Codes),
  105. 0x19, 0x00, // Usage Minimum (0),
  106. 0x29, 0x7F, // Usage Maximum (104),
  107. 0x81, 0x00, // Input (Data, Array), ;Normal keys
  108. 0xc0 // End Collection
  109. };
  110. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  111. static const uint8_t PROGMEM mouse_hid_report_desc[] = {
  112. 0x05, 0x01, // Usage Page (Generic Desktop)
  113. 0x09, 0x02, // Usage (Mouse)
  114. 0xA1, 0x01, // Collection (Application)
  115. 0x05, 0x09, // Usage Page (Button)
  116. 0x19, 0x01, // Usage Minimum (Button #1)
  117. 0x29, 0x03, // Usage Maximum (Button #3)
  118. 0x15, 0x00, // Logical Minimum (0)
  119. 0x25, 0x01, // Logical Maximum (1)
  120. 0x95, 0x03, // Report Count (3)
  121. 0x75, 0x01, // Report Size (1)
  122. 0x81, 0x02, // Input (Data, Variable, Absolute)
  123. 0x95, 0x01, // Report Count (1)
  124. 0x75, 0x05, // Report Size (5)
  125. 0x81, 0x03, // Input (Constant)
  126. 0x05, 0x01, // Usage Page (Generic Desktop)
  127. 0x09, 0x30, // Usage (X)
  128. 0x09, 0x31, // Usage (Y)
  129. 0x15, 0x81, // Logical Minimum (-127)
  130. 0x25, 0x7F, // Logical Maximum (127)
  131. 0x75, 0x08, // Report Size (8),
  132. 0x95, 0x02, // Report Count (2),
  133. 0x81, 0x06, // Input (Data, Variable, Relative)
  134. 0x09, 0x38, // Usage (Wheel)
  135. 0x95, 0x01, // Report Count (1),
  136. 0x81, 0x06, // Input (Data, Variable, Relative)
  137. 0xC0 // End Collection
  138. };
  139. static const uint8_t PROGMEM joystick_hid_report_desc[] = {
  140. 0x05, 0x01, // Usage Page (Generic Desktop)
  141. 0x09, 0x04, // Usage (Joystick)
  142. 0xA1, 0x01, // Collection (Application)
  143. 0x15, 0x00, // Logical Minimum (0)
  144. 0x25, 0x01, // Logical Maximum (1)
  145. 0x75, 0x01, // Report Size (1)
  146. 0x95, 0x20, // Report Count (32)
  147. 0x05, 0x09, // Usage Page (Button)
  148. 0x19, 0x01, // Usage Minimum (Button #1)
  149. 0x29, 0x20, // Usage Maximum (Button #32)
  150. 0x81, 0x02, // Input (variable,absolute)
  151. 0x15, 0x00, // Logical Minimum (0)
  152. 0x25, 0x07, // Logical Maximum (7)
  153. 0x35, 0x00, // Physical Minimum (0)
  154. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  155. 0x75, 0x04, // Report Size (4)
  156. 0x95, 0x01, // Report Count (1)
  157. 0x65, 0x14, // Unit (20)
  158. 0x05, 0x01, // Usage Page (Generic Desktop)
  159. 0x09, 0x39, // Usage (Hat switch)
  160. 0x81, 0x42, // Input (variable,absolute,null_state)
  161. 0x05, 0x01, // Usage Page (Generic Desktop)
  162. 0x09, 0x01, // Usage (Pointer)
  163. 0xA1, 0x00, // Collection ()
  164. 0x15, 0x00, // Logical Minimum (0)
  165. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  166. 0x75, 0x0A, // Report Size (10)
  167. 0x95, 0x04, // Report Count (4)
  168. 0x09, 0x30, // Usage (X)
  169. 0x09, 0x31, // Usage (Y)
  170. 0x09, 0x32, // Usage (Z)
  171. 0x09, 0x35, // Usage (Rz)
  172. 0x81, 0x02, // Input (variable,absolute)
  173. 0xC0, // End Collection
  174. 0x15, 0x00, // Logical Minimum (0)
  175. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  176. 0x75, 0x0A, // Report Size (10)
  177. 0x95, 0x02, // Report Count (2)
  178. 0x09, 0x36, // Usage (Slider)
  179. 0x09, 0x36, // Usage (Slider)
  180. 0x81, 0x02, // Input (variable,absolute)
  181. 0xC0 // End Collection
  182. };
  183. #define KEYBOARD_HID_DESC_OFFSET ( 9+8 + 9+5+5+4+5+7+9+7+7 + 9 )
  184. #define MOUSE_HID_DESC_OFFSET ( 9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9 )
  185. #define JOYSTICK_HID_DESC_OFFSET ( 9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9+9+7 + 9 )
  186. #define CONFIG1_DESC_SIZE ( 9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9+9+7 + 9+9+7 )
  187. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  188. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  189. 9, // bLength;
  190. 2, // bDescriptorType;
  191. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  192. MSB(CONFIG1_DESC_SIZE),
  193. 5, // bNumInterfaces
  194. 1, // bConfigurationValue
  195. 0, // iConfiguration
  196. 0xC0, // bmAttributes
  197. 50, // bMaxPower
  198. // interface association descriptor, USB ECN, Table 9-Z
  199. 8, // bLength
  200. 11, // bDescriptorType
  201. 0, // bFirstInterface
  202. 2, // bInterfaceCount
  203. 0x02, // bFunctionClass
  204. 0x02, // bFunctionSubClass
  205. 0x01, // bFunctionProtocol
  206. 4, // iFunction
  207. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  208. 9, // bLength
  209. 4, // bDescriptorType
  210. 0, // bInterfaceNumber
  211. 0, // bAlternateSetting
  212. 1, // bNumEndpoints
  213. 0x02, // bInterfaceClass
  214. 0x02, // bInterfaceSubClass
  215. 0x01, // bInterfaceProtocol
  216. 0, // iInterface
  217. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  218. 5, // bFunctionLength
  219. 0x24, // bDescriptorType
  220. 0x00, // bDescriptorSubtype
  221. 0x10, 0x01, // bcdCDC
  222. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  223. 5, // bFunctionLength
  224. 0x24, // bDescriptorType
  225. 0x01, // bDescriptorSubtype
  226. 0x00, // bmCapabilities
  227. 1, // bDataInterface
  228. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  229. 4, // bFunctionLength
  230. 0x24, // bDescriptorType
  231. 0x02, // bDescriptorSubtype
  232. 0x06, // bmCapabilities
  233. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  234. 5, // bFunctionLength
  235. 0x24, // bDescriptorType
  236. 0x06, // bDescriptorSubtype
  237. 0, // bMasterInterface
  238. 1, // bSlaveInterface0
  239. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  240. 7, // bLength
  241. 5, // bDescriptorType
  242. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  243. 0x03, // bmAttributes (0x03=intr)
  244. CDC_ACM_SIZE, 0, // wMaxPacketSize
  245. 64, // bInterval
  246. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  247. 9, // bLength
  248. 4, // bDescriptorType
  249. 1, // bInterfaceNumber
  250. 0, // bAlternateSetting
  251. 2, // bNumEndpoints
  252. 0x0A, // bInterfaceClass
  253. 0x00, // bInterfaceSubClass
  254. 0x00, // bInterfaceProtocol
  255. 0, // iInterface
  256. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  257. 7, // bLength
  258. 5, // bDescriptorType
  259. CDC_RX_ENDPOINT, // bEndpointAddress
  260. 0x02, // bmAttributes (0x02=bulk)
  261. CDC_RX_SIZE, 0, // wMaxPacketSize
  262. 0, // bInterval
  263. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  264. 7, // bLength
  265. 5, // bDescriptorType
  266. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  267. 0x02, // bmAttributes (0x02=bulk)
  268. CDC_TX_SIZE, 0, // wMaxPacketSize
  269. 0, // bInterval
  270. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  271. 9, // bLength
  272. 4, // bDescriptorType
  273. KEYBOARD_INTERFACE, // bInterfaceNumber
  274. 0, // bAlternateSetting
  275. 1, // bNumEndpoints
  276. 0x03, // bInterfaceClass (0x03 = HID)
  277. 0x01, // bInterfaceSubClass (0x01 = Boot)
  278. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  279. 0, // iInterface
  280. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  281. 9, // bLength
  282. 0x21, // bDescriptorType
  283. 0x11, 0x01, // bcdHID
  284. 0, // bCountryCode
  285. 1, // bNumDescriptors
  286. 0x22, // bDescriptorType
  287. sizeof(keyboard_hid_report_desc), // wDescriptorLength
  288. 0,
  289. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  290. 7, // bLength
  291. 5, // bDescriptorType
  292. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  293. 0x03, // bmAttributes (0x03=intr)
  294. KEYBOARD_SIZE, 0, // wMaxPacketSize
  295. KEYBOARD_INTERVAL, // bInterval
  296. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  297. 9, // bLength
  298. 4, // bDescriptorType
  299. MOUSE_INTERFACE, // bInterfaceNumber
  300. 0, // bAlternateSetting
  301. 1, // bNumEndpoints
  302. 0x03, // bInterfaceClass (0x03 = HID)
  303. 0x01, // bInterfaceSubClass (0x01 = Boot)
  304. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  305. 0, // iInterface
  306. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  307. 9, // bLength
  308. 0x21, // bDescriptorType
  309. 0x11, 0x01, // bcdHID
  310. 0, // bCountryCode
  311. 1, // bNumDescriptors
  312. 0x22, // bDescriptorType
  313. sizeof(mouse_hid_report_desc), // wDescriptorLength
  314. 0,
  315. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  316. 7, // bLength
  317. 5, // bDescriptorType
  318. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  319. 0x03, // bmAttributes (0x03=intr)
  320. MOUSE_SIZE, 0, // wMaxPacketSize
  321. MOUSE_INTERVAL, // bInterval
  322. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  323. 9, // bLength
  324. 4, // bDescriptorType
  325. JOYSTICK_INTERFACE, // bInterfaceNumber
  326. 0, // bAlternateSetting
  327. 1, // bNumEndpoints
  328. 0x03, // bInterfaceClass (0x03 = HID)
  329. 0x00, // bInterfaceSubClass
  330. 0x00, // bInterfaceProtocol
  331. 0, // iInterface
  332. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  333. 9, // bLength
  334. 0x21, // bDescriptorType
  335. 0x11, 0x01, // bcdHID
  336. 0, // bCountryCode
  337. 1, // bNumDescriptors
  338. 0x22, // bDescriptorType
  339. sizeof(joystick_hid_report_desc), // wDescriptorLength
  340. 0,
  341. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  342. 7, // bLength
  343. 5, // bDescriptorType
  344. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  345. 0x03, // bmAttributes (0x03=intr)
  346. 12, 0, // wMaxPacketSize
  347. JOYSTICK_INTERVAL // bInterval
  348. };
  349. // If you're desperate for a little extra code memory, these strings
  350. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  351. // in the device desciptor are changed to zeros.
  352. struct usb_string_descriptor_struct {
  353. uint8_t bLength;
  354. uint8_t bDescriptorType;
  355. int16_t wString[];
  356. };
  357. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  358. 4,
  359. 3,
  360. {0x0409}
  361. };
  362. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  363. sizeof(STR_MANUFACTURER),
  364. 3,
  365. STR_MANUFACTURER
  366. };
  367. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  368. sizeof(STR_PRODUCT),
  369. 3,
  370. STR_PRODUCT
  371. };
  372. static const struct usb_string_descriptor_struct PROGMEM string3 = {
  373. sizeof(STR_SERIAL_NUMBER),
  374. 3,
  375. STR_SERIAL_NUMBER
  376. };
  377. static const struct usb_string_descriptor_struct PROGMEM string4 = {
  378. sizeof(STR_SERIAL),
  379. 3,
  380. STR_SERIAL
  381. };
  382. // This table defines which descriptor data is sent for each specific
  383. // request from the host (in wValue and wIndex).
  384. static const struct descriptor_list_struct {
  385. uint16_t wValue;
  386. uint16_t wIndex;
  387. const uint8_t *addr;
  388. uint8_t length;
  389. } PROGMEM descriptor_list[] = {
  390. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  391. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  392. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  393. {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
  394. {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
  395. {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
  396. {0x2200, JOYSTICK_INTERFACE, joystick_hid_report_desc, sizeof(joystick_hid_report_desc)},
  397. {0x2100, JOYSTICK_INTERFACE, config1_descriptor+JOYSTICK_HID_DESC_OFFSET, 9},
  398. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  399. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  400. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
  401. {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL_NUMBER)},
  402. {0x0304, 0x0409, (const uint8_t *)&string4, sizeof(STR_SERIAL)},
  403. };
  404. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  405. /**************************************************************************
  406. *
  407. * Variables - these are the only non-stack RAM usage
  408. *
  409. **************************************************************************/
  410. // zero when we are not configured, non-zero when enumerated
  411. volatile uint8_t usb_configuration USBSTATE;
  412. volatile uint8_t usb_suspended USBSTATE;
  413. // the time remaining before we transmit any partially full
  414. // packet, or send a zero length packet.
  415. volatile uint8_t transmit_flush_timer=0;
  416. volatile uint8_t reboot_timer=0;
  417. uint8_t transmit_previous_timeout=0;
  418. // serial port settings (baud rate, control signals, etc) set
  419. // by the PC. These are ignored, but kept in RAM because the
  420. // CDC spec requires a read that returns the current settings.
  421. volatile uint8_t cdc_line_coding[7]={0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
  422. volatile uint8_t cdc_line_rtsdtr USBSTATE;
  423. // byte0: which modifier keys are currently pressed
  424. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  425. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  426. // byte1: media keys (TODO: document these)
  427. // bytes2-7: which keys are currently pressed, up to 6 keys may be down at once
  428. uint8_t keyboard_report_data[8] USBSTATE;
  429. // protocol setting from the host. We use exactly the same report
  430. // either way, so this variable only stores the setting since we
  431. // are required to be able to report which setting is in use.
  432. static uint8_t keyboard_protocol USBSTATE;
  433. // the idle configuration, how often we send the report to the
  434. // host (ms * 4) even when it hasn't changed
  435. static uint8_t keyboard_idle_config USBSTATE;
  436. // count until idle timeout
  437. uint8_t keyboard_idle_count USBSTATE;
  438. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  439. volatile uint8_t keyboard_leds USBSTATE;
  440. // which buttons are currently pressed
  441. uint8_t mouse_buttons USBSTATE;
  442. // protocol setting from the host. We use exactly the same report
  443. // either way, so this variable only stores the setting since we
  444. // are required to be able to report which setting is in use.
  445. static uint8_t mouse_protocol USBSTATE;
  446. // joystick data
  447. uint8_t joystick_report_data[12] USBSTATE;
  448. /**************************************************************************
  449. *
  450. * Public Functions - these are the API intended for the user
  451. *
  452. **************************************************************************/
  453. // initialize USB serial
  454. void usb_init(void)
  455. {
  456. uint8_t u;
  457. u = USBCON;
  458. if ((u & (1<<USBE)) && !(u & (1<<FRZCLK))) return;
  459. HW_CONFIG();
  460. USB_FREEZE(); // enable USB
  461. PLL_CONFIG(); // config PLL
  462. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  463. USB_CONFIG(); // start USB clock
  464. UDCON = 0; // enable attach resistor
  465. usb_configuration = 0;
  466. usb_suspended = 0;
  467. cdc_line_rtsdtr = 0;
  468. keyboard_report_data[0] = 0;
  469. keyboard_report_data[1] = 0;
  470. keyboard_report_data[2] = 0;
  471. keyboard_report_data[3] = 0;
  472. keyboard_report_data[4] = 0;
  473. keyboard_report_data[5] = 0;
  474. keyboard_report_data[6] = 0;
  475. keyboard_report_data[7] = 0;
  476. keyboard_protocol = 1;
  477. keyboard_idle_config = 125;
  478. keyboard_idle_count = 0;
  479. keyboard_leds = 0;
  480. mouse_buttons = 0;
  481. mouse_protocol = 1;
  482. joystick_report_data[0] = 0;
  483. joystick_report_data[1] = 0;
  484. joystick_report_data[2] = 0;
  485. joystick_report_data[3] = 0;
  486. joystick_report_data[4] = 0x0F;
  487. joystick_report_data[5] = 0x20;
  488. joystick_report_data[6] = 0x80;
  489. joystick_report_data[7] = 0x00;
  490. joystick_report_data[8] = 0x02;
  491. joystick_report_data[9] = 0x08;
  492. joystick_report_data[10] = 0x20;
  493. joystick_report_data[11] = 0x80;
  494. UDINT = 0;
  495. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
  496. }
  497. void usb_shutdown(void)
  498. {
  499. UDIEN = 0; // disable interrupts
  500. UDCON = 1; // disconnect attach resistor
  501. USBCON = 0; // shut off USB periperal
  502. PLLCSR = 0; // shut off PLL
  503. usb_configuration = 0;
  504. usb_suspended = 1;
  505. }
  506. // Public API functions moved to usb_api.cpp
  507. /**************************************************************************
  508. *
  509. * Private Functions - not intended for general user consumption....
  510. *
  511. **************************************************************************/
  512. // USB Device Interrupt - handle all device-level events
  513. // the transmit buffer flushing is triggered by the start of frame
  514. //
  515. ISR(USB_GEN_vect)
  516. {
  517. uint8_t intbits, t, i;
  518. static uint8_t div4=0;
  519. intbits = UDINT;
  520. UDINT = 0;
  521. if (intbits & (1<<EORSTI)) {
  522. // USB Reset
  523. UENUM = 0;
  524. UECONX = 1;
  525. UECFG0X = EP_TYPE_CONTROL;
  526. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  527. UEIENX = (1<<RXSTPE);
  528. usb_configuration = 0;
  529. cdc_line_rtsdtr = 0;
  530. }
  531. if (intbits & (1<<SOFI)) {
  532. // Start Of Frame
  533. if (usb_configuration) {
  534. t = transmit_flush_timer;
  535. if (t) {
  536. transmit_flush_timer = --t;
  537. if (!t) {
  538. UENUM = CDC_TX_ENDPOINT;
  539. UEINTX = 0x3A;
  540. }
  541. }
  542. t = reboot_timer;
  543. if (t) {
  544. reboot_timer = --t;
  545. if (!t) _reboot_Teensyduino_();
  546. }
  547. if (keyboard_idle_config && (++div4 & 3) == 0) {
  548. UENUM = KEYBOARD_ENDPOINT;
  549. if (UEINTX & (1<<RWAL)) {
  550. keyboard_idle_count++;
  551. if (keyboard_idle_count == keyboard_idle_config) {
  552. keyboard_idle_count = 0;
  553. //len = keyboard_protocol ? sizeof(keyboard_keys) : 8;
  554. for (i=0; i < 8; i++) {
  555. UEDATX = keyboard_report_data[i];
  556. }
  557. UEINTX = 0x3A;
  558. }
  559. }
  560. }
  561. }
  562. }
  563. // in active state
  564. if (intbits & (1<<SUSPI)) {
  565. // USB Suspend (inactivity for 3ms)
  566. UDIEN = (1<<WAKEUPE);
  567. usb_configuration = 0;
  568. usb_suspended = 1;
  569. #if (F_CPU >= 8000000L)
  570. // WAKEUPI does not work with USB clock freeze
  571. // when CPU is running less than 8 MHz.
  572. // Is this a hardware bug?
  573. USB_FREEZE(); // shut off USB
  574. PLLCSR = 0; // shut off PLL
  575. #endif
  576. // to properly meet the USB spec, current must
  577. // reduce to less than 2.5 mA, which means using
  578. // powerdown mode, but that breaks the Arduino
  579. // user's paradigm....
  580. }
  581. if (usb_suspended && (intbits & (1<<WAKEUPI))) {
  582. // USB Resume (pretty much any activity)
  583. #if (F_CPU >= 8000000L)
  584. PLL_CONFIG();
  585. while (!(PLLCSR & (1<<PLOCK))) ;
  586. USB_CONFIG();
  587. #endif
  588. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
  589. usb_suspended = 0;
  590. return;
  591. }
  592. }
  593. // Misc functions to wait for ready and send/receive packets
  594. static inline void usb_wait_in_ready(void)
  595. {
  596. while (!(UEINTX & (1<<TXINI))) ;
  597. }
  598. static inline void usb_send_in(void)
  599. {
  600. UEINTX = ~(1<<TXINI);
  601. }
  602. static inline void usb_wait_receive_out(void)
  603. {
  604. while (!(UEINTX & (1<<RXOUTI))) ;
  605. }
  606. static inline void usb_ack_out(void)
  607. {
  608. UEINTX = ~(1<<RXOUTI);
  609. }
  610. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  611. // other endpoints are manipulated by the user-callable
  612. // functions, and the start-of-frame interrupt.
  613. //
  614. ISR(USB_COM_vect)
  615. {
  616. uint8_t intbits;
  617. const uint8_t *list;
  618. const uint8_t *cfg;
  619. uint8_t i, n, len, en;
  620. volatile uint8_t *p;
  621. uint8_t bmRequestType;
  622. uint8_t bRequest;
  623. uint16_t wValue;
  624. uint16_t wIndex;
  625. uint16_t wLength;
  626. uint16_t desc_val;
  627. const uint8_t *desc_addr;
  628. uint8_t desc_length;
  629. UENUM = 0;
  630. intbits = UEINTX;
  631. if (intbits & (1<<RXSTPI)) {
  632. bmRequestType = UEDATX;
  633. bRequest = UEDATX;
  634. read_word_lsbfirst(wValue, UEDATX);
  635. read_word_lsbfirst(wIndex, UEDATX);
  636. read_word_lsbfirst(wLength, UEDATX);
  637. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  638. if (bRequest == GET_DESCRIPTOR) {
  639. list = (const uint8_t *)descriptor_list;
  640. for (i=0; ; i++) {
  641. if (i >= NUM_DESC_LIST) {
  642. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  643. return;
  644. }
  645. pgm_read_word_postinc(desc_val, list);
  646. if (desc_val != wValue) {
  647. list += sizeof(struct descriptor_list_struct)-2;
  648. continue;
  649. }
  650. pgm_read_word_postinc(desc_val, list);
  651. if (desc_val != wIndex) {
  652. list += sizeof(struct descriptor_list_struct)-4;
  653. continue;
  654. }
  655. pgm_read_word_postinc(desc_addr, list);
  656. desc_length = pgm_read_byte(list);
  657. break;
  658. }
  659. len = (wLength < 256) ? wLength : 255;
  660. if (len > desc_length) len = desc_length;
  661. list = desc_addr;
  662. do {
  663. // wait for host ready for IN packet
  664. do {
  665. i = UEINTX;
  666. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  667. if (i & (1<<RXOUTI)) return; // abort
  668. // send IN packet
  669. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  670. for (i = n; i; i--) {
  671. pgm_read_byte_postinc(UEDATX, list);
  672. }
  673. len -= n;
  674. usb_send_in();
  675. } while (len || n == ENDPOINT0_SIZE);
  676. return;
  677. }
  678. if (bRequest == SET_ADDRESS) {
  679. usb_send_in();
  680. usb_wait_in_ready();
  681. UDADDR = wValue | (1<<ADDEN);
  682. return;
  683. }
  684. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  685. usb_configuration = wValue;
  686. cdc_line_rtsdtr = 0;
  687. transmit_flush_timer = 0;
  688. usb_send_in();
  689. cfg = endpoint_config_table;
  690. for (i=1; i<7; i++) {
  691. UENUM = i;
  692. //pgm_read_byte_postinc(en, cfg);
  693. //UECONX = en;
  694. UECONX = 1;
  695. //if (en) {
  696. pgm_read_byte_postinc(UECFG0X, cfg);
  697. pgm_read_byte_postinc(UECFG1X, cfg);
  698. //}
  699. }
  700. UERST = 0x1E;
  701. UERST = 0;
  702. return;
  703. }
  704. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  705. usb_wait_in_ready();
  706. UEDATX = usb_configuration;
  707. usb_send_in();
  708. return;
  709. }
  710. if (bRequest == CDC_GET_LINE_CODING /* 0x21 */ && bmRequestType == 0xA1) {
  711. usb_wait_in_ready();
  712. p = cdc_line_coding;
  713. for (i=0; i<7; i++) {
  714. UEDATX = *p++;
  715. }
  716. usb_send_in();
  717. return;
  718. }
  719. if (bRequest == CDC_SET_LINE_CODING /* 0x20 */ && bmRequestType == 0x21) {
  720. usb_wait_receive_out();
  721. p = cdc_line_coding;
  722. for (i=0; i<7; i++) {
  723. *p++ = UEDATX;
  724. }
  725. usb_ack_out();
  726. usb_send_in();
  727. if (*(long *)cdc_line_coding == 134L) reboot_timer = 15;
  728. if (*(long *)cdc_line_coding == 150L) {
  729. UENUM = CDC_TX_ENDPOINT;
  730. while (UESTA0X & 0x03) {
  731. UEINTX = 0xFF;
  732. while (UEINTX & 0x04) /* TODO: timeout? */ ;
  733. }
  734. _restart_Teensyduino_();
  735. }
  736. return;
  737. }
  738. if (bRequest == CDC_SET_CONTROL_LINE_STATE /* 0x22 */ && bmRequestType == 0x21) {
  739. cdc_line_rtsdtr = wValue;
  740. usb_wait_in_ready();
  741. usb_send_in();
  742. return;
  743. }
  744. if (bRequest == CDC_SEND_BREAK /* 0x23 */ && bmRequestType == 0x21) {
  745. usb_wait_in_ready();
  746. usb_send_in();
  747. return;
  748. }
  749. if (bRequest == GET_STATUS) {
  750. usb_wait_in_ready();
  751. i = 0;
  752. if (bmRequestType == 0x82) {
  753. UENUM = wIndex;
  754. if (UECONX & (1<<STALLRQ)) i = 1;
  755. UENUM = 0;
  756. }
  757. UEDATX = i;
  758. UEDATX = 0;
  759. usb_send_in();
  760. return;
  761. }
  762. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  763. && bmRequestType == 0x02 && wValue == 0) {
  764. i = wIndex & 0x7F;
  765. if (i >= 1 && i <= MAX_ENDPOINT) {
  766. usb_send_in();
  767. UENUM = i;
  768. if (bRequest == SET_FEATURE) {
  769. UECONX = (1<<STALLRQ)|(1<<EPEN);
  770. } else {
  771. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  772. UERST = (1 << i);
  773. UERST = 0;
  774. }
  775. return;
  776. }
  777. }
  778. if (wIndex == KEYBOARD_INTERFACE) {
  779. if (bmRequestType == 0xA1) {
  780. if (bRequest == HID_GET_REPORT) {
  781. usb_wait_in_ready();
  782. //len = keyboard_protocol ? sizeof(keyboard_keys) : 8;
  783. for (i=0; i < 8; i++) {
  784. UEDATX = keyboard_report_data[i];
  785. }
  786. usb_send_in();
  787. return;
  788. }
  789. if (bRequest == HID_GET_IDLE) {
  790. usb_wait_in_ready();
  791. UEDATX = keyboard_idle_config;
  792. usb_send_in();
  793. return;
  794. }
  795. if (bRequest == HID_GET_PROTOCOL) {
  796. usb_wait_in_ready();
  797. UEDATX = keyboard_protocol;
  798. usb_send_in();
  799. return;
  800. }
  801. }
  802. if (bmRequestType == 0x21) {
  803. if (bRequest == HID_SET_REPORT) {
  804. usb_wait_receive_out();
  805. keyboard_leds = UEDATX;
  806. usb_ack_out();
  807. usb_send_in();
  808. return;
  809. }
  810. if (bRequest == HID_SET_IDLE) {
  811. keyboard_idle_config = (wValue >> 8);
  812. keyboard_idle_count = 0;
  813. //usb_wait_in_ready();
  814. usb_send_in();
  815. return;
  816. }
  817. if (bRequest == HID_SET_PROTOCOL) {
  818. keyboard_protocol = wValue;
  819. //usb_wait_in_ready();
  820. usb_send_in();
  821. return;
  822. }
  823. }
  824. }
  825. if (wIndex == MOUSE_INTERFACE) {
  826. if (bmRequestType == 0xA1) {
  827. if (bRequest == HID_GET_REPORT) {
  828. usb_wait_in_ready();
  829. UEDATX = mouse_buttons;
  830. UEDATX = 0;
  831. UEDATX = 0;
  832. UEDATX = 0;
  833. usb_send_in();
  834. return;
  835. }
  836. if (bRequest == HID_GET_PROTOCOL) {
  837. usb_wait_in_ready();
  838. UEDATX = mouse_protocol;
  839. usb_send_in();
  840. return;
  841. }
  842. }
  843. if (bmRequestType == 0x21) {
  844. if (bRequest == HID_SET_PROTOCOL) {
  845. mouse_protocol = wValue;
  846. usb_send_in();
  847. return;
  848. }
  849. }
  850. }
  851. if (wIndex == JOYSTICK_INTERFACE) {
  852. if (bmRequestType == 0xA1) {
  853. if (bRequest == HID_GET_REPORT) {
  854. usb_wait_in_ready();
  855. for (i=0; i<12; i++) {
  856. UEDATX = joystick_report_data[i];
  857. }
  858. usb_send_in();
  859. return;
  860. }
  861. }
  862. }
  863. }
  864. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  865. }