Keyboard firmwares for Atmel AVR and Cortex-M
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

usb.c 33KB

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