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

usb.c 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_keyboard.html
  3. * Copyright (c) 2009 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 <stdint.h>
  24. #include <stdbool.h>
  25. #include <avr/io.h>
  26. #include <avr/pgmspace.h>
  27. #include <avr/interrupt.h>
  28. #include "usb.h"
  29. #include "usb_keyboard.h"
  30. #include "usb_mouse.h"
  31. #include "usb_debug.h"
  32. #include "usb_extra.h"
  33. #include "led.h"
  34. #include "print.h"
  35. #include "util.h"
  36. #include "sleep_led.h"
  37. #include "suspend.h"
  38. /**************************************************************************
  39. *
  40. * Configurable Options
  41. *
  42. **************************************************************************/
  43. // You can change these to give your code its own name.
  44. #ifndef MANUFACTURER
  45. # define STR_MANUFACTURER L"t.m.k."
  46. #else
  47. # define STR_MANUFACTURER LSTR(MANUFACTURER)
  48. #endif
  49. #ifndef PRODUCT
  50. # define STR_PRODUCT L"t.m.k. keyboard"
  51. #else
  52. # define STR_PRODUCT LSTR(PRODUCT)
  53. #endif
  54. // Mac OS-X and Linux automatically load the correct drivers. On
  55. // Windows, even though the driver is supplied by Microsoft, an
  56. // INF file is needed to load the driver. These numbers need to
  57. // match the INF file.
  58. #ifndef VENDOR_ID
  59. # define VENDOR_ID 0xFEED
  60. #endif
  61. #ifndef PRODUCT_ID
  62. # define PRODUCT_ID 0xBABE
  63. #endif
  64. #ifndef DEVICE_VER
  65. # define DEVICE_VER 0x0100
  66. #endif
  67. // USB devices are supposed to implment a halt feature, which is
  68. // rarely (if ever) used. If you comment this line out, the halt
  69. // code will be removed, saving 102 bytes of space (gcc 4.3.0).
  70. // This is not strictly USB compliant, but works with all major
  71. // operating systems.
  72. #define SUPPORT_ENDPOINT_HALT
  73. /**************************************************************************
  74. *
  75. * Endpoint Buffer Configuration
  76. *
  77. **************************************************************************/
  78. #define ENDPOINT0_SIZE 32
  79. bool remote_wakeup = false;
  80. bool suspend = false;
  81. // 0:control endpoint is enabled automatically by controller.
  82. static const uint8_t PROGMEM endpoint_config_table[] = {
  83. // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
  84. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD_SIZE) | KBD_BUFFER, // 1
  85. #ifdef MOUSE_ENABLE
  86. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER, // 2
  87. #else
  88. 0, // 2
  89. #endif
  90. #ifdef CONSOLE_ENABLE
  91. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3
  92. #else
  93. 0,
  94. #endif
  95. #ifdef EXTRAKEY_ENABLE
  96. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4
  97. #else
  98. 0, // 4
  99. #endif
  100. #ifdef NKRO_ENABLE
  101. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD2_SIZE) | KBD2_BUFFER, // 5
  102. #else
  103. 0, // 5
  104. #endif
  105. 0, // 6
  106. };
  107. /**************************************************************************
  108. *
  109. * Descriptor Data
  110. *
  111. **************************************************************************/
  112. // Descriptors are the data that your computer reads when it auto-detects
  113. // this USB device (called "enumeration" in USB lingo). The most commonly
  114. // changed items are editable at the top of this file. Changing things
  115. // in here should only be done by those who've read chapter 9 of the USB
  116. // spec and relevant portions of any USB class specifications!
  117. static const uint8_t PROGMEM device_descriptor[] = {
  118. 18, // bLength
  119. 1, // bDescriptorType
  120. 0x00, 0x02, // bcdUSB
  121. 0, // bDeviceClass
  122. 0, // bDeviceSubClass
  123. 0, // bDeviceProtocol
  124. ENDPOINT0_SIZE, // bMaxPacketSize0
  125. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  126. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  127. LSB(DEVICE_VER), MSB(DEVICE_VER), // bcdDevice
  128. 1, // iManufacturer
  129. 2, // iProduct
  130. 0, // iSerialNumber
  131. 1 // bNumConfigurations
  132. };
  133. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  134. static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
  135. 0x05, 0x01, // Usage Page (Generic Desktop),
  136. 0x09, 0x06, // Usage (Keyboard),
  137. 0xA1, 0x01, // Collection (Application),
  138. 0x75, 0x01, // Report Size (1),
  139. 0x95, 0x08, // Report Count (8),
  140. 0x05, 0x07, // Usage Page (Key Codes),
  141. 0x19, 0xE0, // Usage Minimum (224),
  142. 0x29, 0xE7, // Usage Maximum (231),
  143. 0x15, 0x00, // Logical Minimum (0),
  144. 0x25, 0x01, // Logical Maximum (1),
  145. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  146. 0x95, 0x01, // Report Count (1),
  147. 0x75, 0x08, // Report Size (8),
  148. 0x81, 0x03, // Input (Constant), ;Reserved byte
  149. 0x95, 0x05, // Report Count (5),
  150. 0x75, 0x01, // Report Size (1),
  151. 0x05, 0x08, // Usage Page (LEDs),
  152. 0x19, 0x01, // Usage Minimum (1),
  153. 0x29, 0x05, // Usage Maximum (5),
  154. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  155. 0x95, 0x01, // Report Count (1),
  156. 0x75, 0x03, // Report Size (3),
  157. 0x91, 0x03, // Output (Constant), ;LED report padding
  158. 0x95, KBD_REPORT_KEYS, // Report Count (),
  159. 0x75, 0x08, // Report Size (8),
  160. 0x15, 0x00, // Logical Minimum (0),
  161. 0x25, 0xFF, // Logical Maximum(255),
  162. 0x05, 0x07, // Usage Page (Key Codes),
  163. 0x19, 0x00, // Usage Minimum (0),
  164. 0x29, 0xFF, // Usage Maximum (255),
  165. 0x81, 0x00, // Input (Data, Array),
  166. 0xc0 // End Collection
  167. };
  168. #ifdef NKRO_ENABLE
  169. static const uint8_t PROGMEM keyboard2_hid_report_desc[] = {
  170. 0x05, 0x01, // Usage Page (Generic Desktop),
  171. 0x09, 0x06, // Usage (Keyboard),
  172. 0xA1, 0x01, // Collection (Application),
  173. // bitmap of modifiers
  174. 0x75, 0x01, // Report Size (1),
  175. 0x95, 0x08, // Report Count (8),
  176. 0x05, 0x07, // Usage Page (Key Codes),
  177. 0x19, 0xE0, // Usage Minimum (224),
  178. 0x29, 0xE7, // Usage Maximum (231),
  179. 0x15, 0x00, // Logical Minimum (0),
  180. 0x25, 0x01, // Logical Maximum (1),
  181. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  182. // LED output report
  183. 0x95, 0x05, // Report Count (5),
  184. 0x75, 0x01, // Report Size (1),
  185. 0x05, 0x08, // Usage Page (LEDs),
  186. 0x19, 0x01, // Usage Minimum (1),
  187. 0x29, 0x05, // Usage Maximum (5),
  188. 0x91, 0x02, // Output (Data, Variable, Absolute),
  189. 0x95, 0x01, // Report Count (1),
  190. 0x75, 0x03, // Report Size (3),
  191. 0x91, 0x03, // Output (Constant),
  192. // bitmap of keys
  193. 0x95, KBD2_REPORT_KEYS*8, // Report Count (),
  194. 0x75, 0x01, // Report Size (1),
  195. 0x15, 0x00, // Logical Minimum (0),
  196. 0x25, 0x01, // Logical Maximum(1),
  197. 0x05, 0x07, // Usage Page (Key Codes),
  198. 0x19, 0x00, // Usage Minimum (0),
  199. 0x29, KBD2_REPORT_KEYS*8-1, // Usage Maximum (),
  200. 0x81, 0x02, // Input (Data, Variable, Absolute),
  201. 0xc0 // End Collection
  202. };
  203. #endif
  204. #ifdef MOUSE_ENABLE
  205. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  206. // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
  207. // http://www.keil.com/forum/15671/
  208. // http://www.microsoft.com/whdc/device/input/wheel.mspx
  209. static const uint8_t PROGMEM mouse_hid_report_desc[] = {
  210. /* mouse */
  211. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  212. 0x09, 0x02, // USAGE (Mouse)
  213. 0xa1, 0x01, // COLLECTION (Application)
  214. //0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
  215. 0x09, 0x01, // USAGE (Pointer)
  216. 0xa1, 0x00, // COLLECTION (Physical)
  217. // ---------------------------- Buttons
  218. 0x05, 0x09, // USAGE_PAGE (Button)
  219. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  220. 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
  221. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  222. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  223. 0x75, 0x01, // REPORT_SIZE (1)
  224. 0x95, 0x05, // REPORT_COUNT (5)
  225. 0x81, 0x02, // INPUT (Data,Var,Abs)
  226. 0x75, 0x03, // REPORT_SIZE (3)
  227. 0x95, 0x01, // REPORT_COUNT (1)
  228. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  229. // ---------------------------- X,Y position
  230. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  231. 0x09, 0x30, // USAGE (X)
  232. 0x09, 0x31, // USAGE (Y)
  233. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  234. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  235. 0x75, 0x08, // REPORT_SIZE (8)
  236. 0x95, 0x02, // REPORT_COUNT (2)
  237. 0x81, 0x06, // INPUT (Data,Var,Rel)
  238. // ---------------------------- Vertical wheel
  239. 0x09, 0x38, // USAGE (Wheel)
  240. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  241. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  242. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  243. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  244. 0x75, 0x08, // REPORT_SIZE (8)
  245. 0x95, 0x01, // REPORT_COUNT (1)
  246. 0x81, 0x06, // INPUT (Data,Var,Rel)
  247. // ---------------------------- Horizontal wheel
  248. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  249. 0x0a, 0x38, 0x02, // USAGE (AC Pan)
  250. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  251. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  252. 0x75, 0x08, // REPORT_SIZE (8)
  253. 0x95, 0x01, // REPORT_COUNT (1)
  254. 0x81, 0x06, // INPUT (Data,Var,Rel)
  255. 0xc0, // END_COLLECTION
  256. 0xc0, // END_COLLECTION
  257. };
  258. #endif
  259. static const uint8_t PROGMEM debug_hid_report_desc[] = {
  260. 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
  261. 0x09, 0x74, // Usage 0x74
  262. 0xA1, 0x53, // Collection 0x53
  263. 0x75, 0x08, // report size = 8 bits
  264. 0x15, 0x00, // logical minimum = 0
  265. 0x26, 0xFF, 0x00, // logical maximum = 255
  266. 0x95, DEBUG_TX_SIZE, // report count
  267. 0x09, 0x75, // usage
  268. 0x81, 0x02, // Input (array)
  269. 0xC0 // end collection
  270. };
  271. #ifdef EXTRAKEY_ENABLE
  272. // audio controls & system controls
  273. // http://www.microsoft.com/whdc/archive/w2kbd.mspx
  274. static const uint8_t PROGMEM extra_hid_report_desc[] = {
  275. /* system control */
  276. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  277. 0x09, 0x80, // USAGE (System Control)
  278. 0xa1, 0x01, // COLLECTION (Application)
  279. 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
  280. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  281. 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
  282. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  283. 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
  284. 0x75, 0x10, // REPORT_SIZE (16)
  285. 0x95, 0x01, // REPORT_COUNT (1)
  286. 0x81, 0x00, // INPUT (Data,Array,Abs)
  287. 0xc0, // END_COLLECTION
  288. /* consumer */
  289. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  290. 0x09, 0x01, // USAGE (Consumer Control)
  291. 0xa1, 0x01, // COLLECTION (Application)
  292. 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
  293. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  294. 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
  295. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  296. 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
  297. 0x75, 0x10, // REPORT_SIZE (16)
  298. 0x95, 0x01, // REPORT_COUNT (1)
  299. 0x81, 0x00, // INPUT (Data,Array,Abs)
  300. 0xc0, // END_COLLECTION
  301. };
  302. #endif
  303. #define KBD_HID_DESC_NUM 0
  304. #define KBD_HID_DESC_OFFSET (9+(9+9+7)*KBD_HID_DESC_NUM+9)
  305. #ifdef MOUSE_ENABLE
  306. # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1)
  307. # define MOUSE_HID_DESC_OFFSET (9+(9+9+7)*MOUSE_HID_DESC_NUM+9)
  308. #else
  309. # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
  310. #endif
  311. #ifdef CONSOLE_ENABLE
  312. #define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1)
  313. #define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*DEBUG_HID_DESC_NUM+9)
  314. #else
  315. # define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0)
  316. #endif
  317. #ifdef EXTRAKEY_ENABLE
  318. # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 1)
  319. # define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
  320. #else
  321. # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 0)
  322. #endif
  323. #ifdef NKRO_ENABLE
  324. # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1)
  325. # define KBD2_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
  326. #else
  327. # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
  328. #endif
  329. #define NUM_INTERFACES (KBD2_HID_DESC_NUM + 1)
  330. #define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES)
  331. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  332. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  333. 9, // bLength;
  334. 2, // bDescriptorType;
  335. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  336. MSB(CONFIG1_DESC_SIZE),
  337. NUM_INTERFACES, // bNumInterfaces
  338. 1, // bConfigurationValue
  339. 0, // iConfiguration
  340. 0xA0, // bmAttributes
  341. 50, // bMaxPower
  342. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  343. 9, // bLength
  344. 4, // bDescriptorType
  345. KBD_INTERFACE, // bInterfaceNumber
  346. 0, // bAlternateSetting
  347. 1, // bNumEndpoints
  348. 0x03, // bInterfaceClass (0x03 = HID)
  349. 0x01, // bInterfaceSubClass (0x01 = Boot)
  350. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  351. 0, // iInterface
  352. // HID descriptor, HID 1.11 spec, section 6.2.1
  353. 9, // bLength
  354. 0x21, // bDescriptorType
  355. 0x11, 0x01, // bcdHID
  356. 0, // bCountryCode
  357. 1, // bNumDescriptors
  358. 0x22, // bDescriptorType
  359. sizeof(keyboard_hid_report_desc), // wDescriptorLength
  360. 0,
  361. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  362. 7, // bLength
  363. 5, // bDescriptorType
  364. KBD_ENDPOINT | 0x80, // bEndpointAddress
  365. 0x03, // bmAttributes (0x03=intr)
  366. KBD_SIZE, 0, // wMaxPacketSize
  367. 10, // bInterval
  368. #ifdef MOUSE_ENABLE
  369. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  370. 9, // bLength
  371. 4, // bDescriptorType
  372. MOUSE_INTERFACE, // bInterfaceNumber
  373. 0, // bAlternateSetting
  374. 1, // bNumEndpoints
  375. 0x03, // bInterfaceClass (0x03 = HID)
  376. // ThinkPad T23 BIOS doesn't work with boot mouse.
  377. 0x00, // bInterfaceSubClass (0x01 = Boot)
  378. 0x00, // bInterfaceProtocol (0x02 = Mouse)
  379. /*
  380. 0x01, // bInterfaceSubClass (0x01 = Boot)
  381. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  382. */
  383. 0, // iInterface
  384. // HID descriptor, HID 1.11 spec, section 6.2.1
  385. 9, // bLength
  386. 0x21, // bDescriptorType
  387. 0x11, 0x01, // bcdHID
  388. 0, // bCountryCode
  389. 1, // bNumDescriptors
  390. 0x22, // bDescriptorType
  391. sizeof(mouse_hid_report_desc), // wDescriptorLength
  392. 0,
  393. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  394. 7, // bLength
  395. 5, // bDescriptorType
  396. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  397. 0x03, // bmAttributes (0x03=intr)
  398. MOUSE_SIZE, 0, // wMaxPacketSize
  399. 1, // bInterval
  400. #endif
  401. #ifdef CONSOLE_ENABLE
  402. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  403. 9, // bLength
  404. 4, // bDescriptorType
  405. DEBUG_INTERFACE, // bInterfaceNumber
  406. 0, // bAlternateSetting
  407. 1, // bNumEndpoints
  408. 0x03, // bInterfaceClass (0x03 = HID)
  409. 0x00, // bInterfaceSubClass
  410. 0x00, // bInterfaceProtocol
  411. 0, // iInterface
  412. // HID descriptor, HID 1.11 spec, section 6.2.1
  413. 9, // bLength
  414. 0x21, // bDescriptorType
  415. 0x11, 0x01, // bcdHID
  416. 0, // bCountryCode
  417. 1, // bNumDescriptors
  418. 0x22, // bDescriptorType
  419. sizeof(debug_hid_report_desc), // wDescriptorLength
  420. 0,
  421. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  422. 7, // bLength
  423. 5, // bDescriptorType
  424. DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
  425. 0x03, // bmAttributes (0x03=intr)
  426. DEBUG_TX_SIZE, 0, // wMaxPacketSize
  427. 1, // bInterval
  428. #endif
  429. #ifdef EXTRAKEY_ENABLE
  430. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  431. 9, // bLength
  432. 4, // bDescriptorType
  433. EXTRA_INTERFACE, // bInterfaceNumber
  434. 0, // bAlternateSetting
  435. 1, // bNumEndpoints
  436. 0x03, // bInterfaceClass (0x03 = HID)
  437. 0x00, // bInterfaceSubClass
  438. 0x00, // bInterfaceProtocol
  439. 0, // iInterface
  440. // HID descriptor, HID 1.11 spec, section 6.2.1
  441. 9, // bLength
  442. 0x21, // bDescriptorType
  443. 0x11, 0x01, // bcdHID
  444. 0, // bCountryCode
  445. 1, // bNumDescriptors
  446. 0x22, // bDescriptorType
  447. sizeof(extra_hid_report_desc), // wDescriptorLength
  448. 0,
  449. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  450. 7, // bLength
  451. 5, // bDescriptorType
  452. EXTRA_ENDPOINT | 0x80, // bEndpointAddress
  453. 0x03, // bmAttributes (0x03=intr)
  454. EXTRA_SIZE, 0, // wMaxPacketSize
  455. 10, // bInterval
  456. #endif
  457. #ifdef NKRO_ENABLE
  458. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  459. 9, // bLength
  460. 4, // bDescriptorType
  461. KBD2_INTERFACE, // bInterfaceNumber
  462. 0, // bAlternateSetting
  463. 1, // bNumEndpoints
  464. 0x03, // bInterfaceClass (0x03 = HID)
  465. 0x00, // bInterfaceSubClass (0x01 = Boot)
  466. 0x00, // bInterfaceProtocol (0x01 = Keyboard)
  467. 0, // iInterface
  468. // HID descriptor, HID 1.11 spec, section 6.2.1
  469. 9, // bLength
  470. 0x21, // bDescriptorType
  471. 0x11, 0x01, // bcdHID
  472. 0, // bCountryCode
  473. 1, // bNumDescriptors
  474. 0x22, // bDescriptorType
  475. sizeof(keyboard2_hid_report_desc), // wDescriptorLength
  476. 0,
  477. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  478. 7, // bLength
  479. 5, // bDescriptorType
  480. KBD2_ENDPOINT | 0x80, // bEndpointAddress
  481. 0x03, // bmAttributes (0x03=intr)
  482. KBD2_SIZE, 0, // wMaxPacketSize
  483. 1, // bInterval
  484. #endif
  485. };
  486. // If you're desperate for a little extra code memory, these strings
  487. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  488. // in the device desciptor are changed to zeros.
  489. struct usb_string_descriptor_struct {
  490. uint8_t bLength;
  491. uint8_t bDescriptorType;
  492. int16_t wString[];
  493. };
  494. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  495. 4,
  496. 3,
  497. {0x0409}
  498. };
  499. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  500. sizeof(STR_MANUFACTURER),
  501. 3,
  502. STR_MANUFACTURER
  503. };
  504. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  505. sizeof(STR_PRODUCT),
  506. 3,
  507. STR_PRODUCT
  508. };
  509. // This table defines which descriptor data is sent for each specific
  510. // request from the host (in wValue and wIndex).
  511. static const struct descriptor_list_struct {
  512. uint16_t wValue; // descriptor type
  513. uint16_t wIndex;
  514. const uint8_t *addr;
  515. uint8_t length;
  516. } PROGMEM descriptor_list[] = {
  517. // DEVICE descriptor
  518. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  519. // CONFIGURATION descriptor
  520. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  521. // HID/REPORT descriptors
  522. {0x2100, KBD_INTERFACE, config1_descriptor+KBD_HID_DESC_OFFSET, 9},
  523. {0x2200, KBD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  524. #ifdef MOUSE_ENABLE
  525. {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
  526. {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
  527. #endif
  528. #ifdef CONSOLE_ENABLE
  529. {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
  530. {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
  531. #endif
  532. #ifdef EXTRAKEY_ENABLE
  533. {0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9},
  534. {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)},
  535. #endif
  536. #ifdef NKRO_ENABLE
  537. {0x2100, KBD2_INTERFACE, config1_descriptor+KBD2_HID_DESC_OFFSET, 9},
  538. {0x2200, KBD2_INTERFACE, keyboard2_hid_report_desc, sizeof(keyboard2_hid_report_desc)},
  539. #endif
  540. // STRING descriptors
  541. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  542. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  543. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
  544. };
  545. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  546. /**************************************************************************
  547. *
  548. * Variables - these are the only non-stack RAM usage
  549. *
  550. **************************************************************************/
  551. // zero when we are not configured, non-zero when enumerated
  552. static volatile uint8_t usb_configuration=0;
  553. /**************************************************************************
  554. *
  555. * Public Functions - these are the API intended for the user
  556. *
  557. **************************************************************************/
  558. // initialize USB
  559. void usb_init(void)
  560. {
  561. HW_CONFIG();
  562. USB_FREEZE(); // enable USB
  563. PLL_CONFIG(); // config PLL
  564. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  565. USB_CONFIG(); // start USB clock
  566. UDCON = 0; // enable attach resistor
  567. usb_configuration = 0;
  568. suspend = false;
  569. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE)|(1<<WAKEUPE);
  570. sei();
  571. }
  572. // return 0 if the USB is not configured, or the configuration
  573. // number selected by the HOST
  574. uint8_t usb_configured(void)
  575. {
  576. return usb_configuration && !suspend;
  577. }
  578. void usb_remote_wakeup(void)
  579. {
  580. UDCON |= (1<<RMWKUP);
  581. }
  582. /**************************************************************************
  583. *
  584. * Private Functions - not intended for general user consumption....
  585. *
  586. **************************************************************************/
  587. // USB Device Interrupt - handle all device-level events
  588. // the transmit buffer flushing is triggered by the start of frame
  589. //
  590. ISR(USB_GEN_vect)
  591. {
  592. uint8_t intbits, t;
  593. static uint8_t div4=0;
  594. intbits = UDINT;
  595. UDINT = 0;
  596. if ((intbits & (1<<SUSPI)) && (UDIEN & (1<<SUSPE)) && usb_configuration) {
  597. #ifdef SLEEP_LED_ENABLE
  598. sleep_led_enable();
  599. #endif
  600. UDIEN &= ~(1<<SUSPE);
  601. UDIEN |= (1<<WAKEUPE);
  602. suspend = true;
  603. }
  604. if ((intbits & (1<<WAKEUPI)) && (UDIEN & (1<<WAKEUPE)) && usb_configuration) {
  605. suspend_wakeup_init();
  606. #ifdef SLEEP_LED_ENABLE
  607. sleep_led_disable();
  608. #endif
  609. led_set(host_keyboard_leds());
  610. UDIEN |= (1<<SUSPE);
  611. UDIEN &= ~(1<<WAKEUPE);
  612. suspend = false;
  613. }
  614. if (intbits & (1<<EORSTI)) {
  615. UENUM = 0;
  616. UECONX = 1;
  617. UECFG0X = EP_TYPE_CONTROL;
  618. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  619. UEIENX = (1<<RXSTPE);
  620. usb_configuration = 0;
  621. }
  622. if ((intbits & (1<<SOFI)) && usb_configuration) {
  623. t = debug_flush_timer;
  624. if (t) {
  625. debug_flush_timer = -- t;
  626. if (!t) {
  627. UENUM = DEBUG_TX_ENDPOINT;
  628. while ((UEINTX & (1<<RWAL))) {
  629. UEDATX = 0;
  630. }
  631. UEINTX = 0x3A;
  632. }
  633. }
  634. /* TODO: should keep IDLE rate on each keyboard interface */
  635. #ifdef NKRO_ENABLE
  636. if (!keyboard_nkro && usb_keyboard_idle_config && (++div4 & 3) == 0) {
  637. #else
  638. if (usb_keyboard_idle_config && (++div4 & 3) == 0) {
  639. #endif
  640. UENUM = KBD_ENDPOINT;
  641. if (UEINTX & (1<<RWAL)) {
  642. usb_keyboard_idle_count++;
  643. if (usb_keyboard_idle_count == usb_keyboard_idle_config) {
  644. usb_keyboard_idle_count = 0;
  645. /* TODO: fix keyboard_report inconsistency */
  646. /* To avoid Mac SET_IDLE behaviour.
  647. UEDATX = keyboard_report_prev->mods;
  648. UEDATX = 0;
  649. uint8_t keys = usb_keyboard_protocol ? KBD_REPORT_KEYS : 6;
  650. for (uint8_t i=0; i<keys; i++) {
  651. UEDATX = keyboard_report_prev->keys[i];
  652. }
  653. UEINTX = 0x3A;
  654. */
  655. }
  656. }
  657. }
  658. }
  659. }
  660. // Misc functions to wait for ready and send/receive packets
  661. static inline void usb_wait_in_ready(void)
  662. {
  663. while (!(UEINTX & (1<<TXINI))) ;
  664. }
  665. static inline void usb_send_in(void)
  666. {
  667. UEINTX = ~(1<<TXINI);
  668. }
  669. static inline void usb_wait_receive_out(void)
  670. {
  671. while (!(UEINTX & (1<<RXOUTI))) ;
  672. }
  673. static inline void usb_ack_out(void)
  674. {
  675. UEINTX = ~(1<<RXOUTI);
  676. }
  677. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  678. // other endpoints are manipulated by the user-callable
  679. // functions, and the start-of-frame interrupt.
  680. //
  681. ISR(USB_COM_vect)
  682. {
  683. uint8_t intbits;
  684. const uint8_t *list;
  685. const uint8_t *cfg;
  686. uint8_t i, n, len, en;
  687. uint8_t bmRequestType;
  688. uint8_t bRequest;
  689. uint16_t wValue;
  690. uint16_t wIndex;
  691. uint16_t wLength;
  692. uint16_t desc_val;
  693. const uint8_t *desc_addr;
  694. uint8_t desc_length;
  695. UENUM = 0;
  696. intbits = UEINTX;
  697. if (intbits & (1<<RXSTPI)) {
  698. bmRequestType = UEDATX;
  699. bRequest = UEDATX;
  700. wValue = UEDATX;
  701. wValue |= (UEDATX << 8);
  702. wIndex = UEDATX;
  703. wIndex |= (UEDATX << 8);
  704. wLength = UEDATX;
  705. wLength |= (UEDATX << 8);
  706. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  707. if (bRequest == GET_DESCRIPTOR) {
  708. list = (const uint8_t *)descriptor_list;
  709. for (i=0; ; i++) {
  710. if (i >= NUM_DESC_LIST) {
  711. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  712. return;
  713. }
  714. desc_val = pgm_read_word(list);
  715. if (desc_val != wValue) {
  716. list += sizeof(struct descriptor_list_struct);
  717. continue;
  718. }
  719. list += 2;
  720. desc_val = pgm_read_word(list);
  721. if (desc_val != wIndex) {
  722. list += sizeof(struct descriptor_list_struct)-2;
  723. continue;
  724. }
  725. list += 2;
  726. desc_addr = (const uint8_t *)pgm_read_word(list);
  727. list += 2;
  728. desc_length = pgm_read_byte(list);
  729. break;
  730. }
  731. len = (wLength < 256) ? wLength : 255;
  732. if (len > desc_length) len = desc_length;
  733. do {
  734. // wait for host ready for IN packet
  735. do {
  736. i = UEINTX;
  737. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  738. if (i & (1<<RXOUTI)) return; // abort
  739. // send IN packet
  740. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  741. for (i = n; i; i--) {
  742. UEDATX = pgm_read_byte(desc_addr++);
  743. }
  744. len -= n;
  745. usb_send_in();
  746. } while (len || n == ENDPOINT0_SIZE);
  747. return;
  748. }
  749. if (bRequest == SET_ADDRESS) {
  750. usb_send_in();
  751. usb_wait_in_ready();
  752. UDADDR = wValue | (1<<ADDEN);
  753. return;
  754. }
  755. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  756. usb_configuration = wValue;
  757. usb_send_in();
  758. cfg = endpoint_config_table;
  759. for (i=1; i<=MAX_ENDPOINT; i++) {
  760. UENUM = i;
  761. en = pgm_read_byte(cfg++);
  762. if (en) {
  763. UECONX = (1<<EPEN);
  764. UECFG0X = pgm_read_byte(cfg++);
  765. UECFG1X = pgm_read_byte(cfg++);
  766. } else {
  767. UECONX = 0;
  768. }
  769. }
  770. UERST = UERST_MASK;
  771. UERST = 0;
  772. return;
  773. }
  774. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  775. usb_wait_in_ready();
  776. UEDATX = usb_configuration;
  777. usb_send_in();
  778. return;
  779. }
  780. if (bRequest == GET_STATUS) {
  781. usb_wait_in_ready();
  782. i = 0;
  783. #ifdef SUPPORT_ENDPOINT_HALT
  784. if (bmRequestType == 0x82) {
  785. UENUM = wIndex;
  786. if (UECONX & (1<<STALLRQ)) i = 1;
  787. UENUM = 0;
  788. }
  789. #endif
  790. UEDATX = i;
  791. UEDATX = 0;
  792. usb_send_in();
  793. return;
  794. }
  795. if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) {
  796. #ifdef SUPPORT_ENDPOINT_HALT
  797. if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) {
  798. i = wIndex & 0x7F;
  799. if (i >= 1 && i <= MAX_ENDPOINT) {
  800. usb_send_in();
  801. UENUM = i;
  802. if (bRequest == SET_FEATURE) {
  803. UECONX = (1<<STALLRQ)|(1<<EPEN);
  804. } else {
  805. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  806. UERST = (1 << i);
  807. UERST = 0;
  808. }
  809. return;
  810. }
  811. }
  812. #endif
  813. if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) {
  814. if (bRequest == SET_FEATURE) {
  815. remote_wakeup = true;
  816. } else {
  817. remote_wakeup = false;
  818. }
  819. usb_send_in();
  820. return;
  821. }
  822. }
  823. if (wIndex == KBD_INTERFACE) {
  824. if (bmRequestType == 0xA1) {
  825. if (bRequest == HID_GET_REPORT) {
  826. usb_wait_in_ready();
  827. UEDATX = keyboard_report->mods;
  828. UEDATX = 0;
  829. for (i=0; i<6; i++) {
  830. UEDATX = keyboard_report->keys[i];
  831. }
  832. usb_send_in();
  833. return;
  834. }
  835. if (bRequest == HID_GET_IDLE) {
  836. usb_wait_in_ready();
  837. UEDATX = usb_keyboard_idle_config;
  838. usb_send_in();
  839. return;
  840. }
  841. if (bRequest == HID_GET_PROTOCOL) {
  842. usb_wait_in_ready();
  843. UEDATX = usb_keyboard_protocol;
  844. usb_send_in();
  845. return;
  846. }
  847. }
  848. if (bmRequestType == 0x21) {
  849. if (bRequest == HID_SET_REPORT) {
  850. usb_wait_receive_out();
  851. usb_keyboard_leds = UEDATX;
  852. usb_ack_out();
  853. usb_send_in();
  854. return;
  855. }
  856. if (bRequest == HID_SET_IDLE) {
  857. usb_keyboard_idle_config = (wValue >> 8);
  858. usb_keyboard_idle_count = 0;
  859. //usb_wait_in_ready();
  860. usb_send_in();
  861. return;
  862. }
  863. if (bRequest == HID_SET_PROTOCOL) {
  864. usb_keyboard_protocol = wValue;
  865. //usb_wait_in_ready();
  866. usb_send_in();
  867. return;
  868. }
  869. }
  870. }
  871. #ifdef MOUSE_ENABLE
  872. if (wIndex == MOUSE_INTERFACE) {
  873. if (bmRequestType == 0xA1) {
  874. if (bRequest == HID_GET_REPORT) {
  875. if (wValue == HID_REPORT_INPUT) {
  876. usb_wait_in_ready();
  877. UEDATX = 0;
  878. UEDATX = 0;
  879. UEDATX = 0;
  880. UEDATX = 0;
  881. usb_send_in();
  882. return;
  883. }
  884. if (wValue == HID_REPORT_FEATURE) {
  885. usb_wait_in_ready();
  886. UEDATX = 0x05;
  887. usb_send_in();
  888. return;
  889. }
  890. }
  891. if (bRequest == HID_GET_PROTOCOL) {
  892. usb_wait_in_ready();
  893. UEDATX = usb_mouse_protocol;
  894. usb_send_in();
  895. return;
  896. }
  897. }
  898. if (bmRequestType == 0x21) {
  899. if (bRequest == HID_SET_PROTOCOL) {
  900. usb_mouse_protocol = wValue;
  901. usb_send_in();
  902. return;
  903. }
  904. }
  905. }
  906. #endif
  907. if (wIndex == DEBUG_INTERFACE) {
  908. if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
  909. len = wLength;
  910. do {
  911. // wait for host ready for IN packet
  912. do {
  913. i = UEINTX;
  914. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  915. if (i & (1<<RXOUTI)) return; // abort
  916. // send IN packet
  917. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  918. for (i = n; i; i--) {
  919. UEDATX = 0;
  920. }
  921. len -= n;
  922. usb_send_in();
  923. } while (len || n == ENDPOINT0_SIZE);
  924. return;
  925. }
  926. }
  927. }
  928. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  929. }