usb.c 33 KB

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