Keyboard firmwares for Atmel AVR and Cortex-M
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.

usb_main.c 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * (c) 2015 flabberast <[email protected]>
  3. *
  4. * Based on the following work:
  5. * - Guillaume Duc's raw hid example (MIT License)
  6. * https://github.com/guiduc/usb-hid-chibios-example
  7. * - PJRC Teensy examples (MIT License)
  8. * https://www.pjrc.com/teensy/usb_keyboard.html
  9. * - hasu's TMK keyboard code (GPL v2 and some code Modified BSD)
  10. * https://github.com/tmk/tmk_keyboard/
  11. * - ChibiOS demo code (Apache 2.0 License)
  12. * http://www.chibios.org
  13. *
  14. * Since some GPL'd code is used, this work is licensed under
  15. * GPL v2 or later.
  16. */
  17. #include "ch.h"
  18. #include "hal.h"
  19. #include "usb_main.h"
  20. #include "host.h"
  21. #include "debug.h"
  22. #ifdef SLEEP_LED_ENABLE
  23. #include "sleep_led.h"
  24. #include "led.h"
  25. #endif
  26. /* ---------------------------------------------------------
  27. * Global interface variables and declarations
  28. * ---------------------------------------------------------
  29. */
  30. uint8_t keyboard_idle = 0;
  31. uint8_t keyboard_protocol = 1;
  32. uint16_t keyboard_led_stats = 0;
  33. volatile uint16_t keyboard_idle_count = 0;
  34. static virtual_timer_t keyboard_idle_timer;
  35. static void keyboard_idle_timer_cb(void *arg);
  36. #ifdef NKRO_ENABLE
  37. extern bool keyboard_nkro;
  38. #endif /* NKRO_ENABLE */
  39. report_keyboard_t keyboard_report_sent = {{0}};
  40. #ifdef MOUSE_ENABLE
  41. report_mouse_t mouse_report_blank = {0};
  42. #endif /* MOUSE_ENABLE */
  43. #ifdef EXTRAKEY_ENABLE
  44. uint8_t extra_report_blank[3] = {0};
  45. #endif /* EXTRAKEY_ENABLE */
  46. #ifdef CONSOLE_ENABLE
  47. /* The emission queue */
  48. output_queue_t console_queue;
  49. static uint8_t console_queue_buffer[CONSOLE_QUEUE_BUFFER_SIZE];
  50. static virtual_timer_t console_flush_timer;
  51. void console_queue_onotify(io_queue_t *qp);
  52. static void console_flush_cb(void *arg);
  53. #endif /* CONSOLE_ENABLE */
  54. /* ---------------------------------------------------------
  55. * Descriptors and USB driver objects
  56. * ---------------------------------------------------------
  57. */
  58. /* HID specific constants */
  59. #define USB_DESCRIPTOR_HID 0x21
  60. #define USB_DESCRIPTOR_HID_REPORT 0x22
  61. #define HID_GET_REPORT 0x01
  62. #define HID_GET_IDLE 0x02
  63. #define HID_GET_PROTOCOL 0x03
  64. #define HID_SET_REPORT 0x09
  65. #define HID_SET_IDLE 0x0A
  66. #define HID_SET_PROTOCOL 0x0B
  67. /* USB Device Descriptor */
  68. static const uint8_t usb_device_descriptor_data[] = {
  69. USB_DESC_DEVICE(0x0200, // bcdUSB (1.1)
  70. 0, // bDeviceClass (defined in later in interface)
  71. 0, // bDeviceSubClass
  72. 0, // bDeviceProtocol
  73. 64, // bMaxPacketSize (64 bytes) (the driver didn't work with 32)
  74. VENDOR_ID, // idVendor
  75. PRODUCT_ID, // idProduct
  76. DEVICE_VER, // bcdDevice
  77. 1, // iManufacturer
  78. 2, // iProduct
  79. 3, // iSerialNumber
  80. 1) // bNumConfigurations
  81. };
  82. /* Device Descriptor wrapper */
  83. static const USBDescriptor usb_device_descriptor = {
  84. sizeof usb_device_descriptor_data,
  85. usb_device_descriptor_data
  86. };
  87. /*
  88. * HID Report Descriptor
  89. *
  90. * See "Device Class Definition for Human Interface Devices (HID)"
  91. * (http://www.usb.org/developers/hidpage/HID1_11.pdf) for the
  92. * detailed descrition of all the fields
  93. */
  94. /* Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60 */
  95. static const uint8_t keyboard_hid_report_desc_data[] = {
  96. 0x05, 0x01, // Usage Page (Generic Desktop),
  97. 0x09, 0x06, // Usage (Keyboard),
  98. 0xA1, 0x01, // Collection (Application),
  99. 0x75, 0x01, // Report Size (1),
  100. 0x95, 0x08, // Report Count (8),
  101. 0x05, 0x07, // Usage Page (Key Codes),
  102. 0x19, 0xE0, // Usage Minimum (224),
  103. 0x29, 0xE7, // Usage Maximum (231),
  104. 0x15, 0x00, // Logical Minimum (0),
  105. 0x25, 0x01, // Logical Maximum (1),
  106. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  107. 0x95, 0x01, // Report Count (1),
  108. 0x75, 0x08, // Report Size (8),
  109. 0x81, 0x03, // Input (Constant), ;Reserved byte
  110. 0x95, 0x05, // Report Count (5),
  111. 0x75, 0x01, // Report Size (1),
  112. 0x05, 0x08, // Usage Page (LEDs),
  113. 0x19, 0x01, // Usage Minimum (1),
  114. 0x29, 0x05, // Usage Maximum (5),
  115. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  116. 0x95, 0x01, // Report Count (1),
  117. 0x75, 0x03, // Report Size (3),
  118. 0x91, 0x03, // Output (Constant), ;LED report padding
  119. 0x95, KBD_REPORT_KEYS, // Report Count (),
  120. 0x75, 0x08, // Report Size (8),
  121. 0x15, 0x00, // Logical Minimum (0),
  122. 0x25, 0xFF, // Logical Maximum(255),
  123. 0x05, 0x07, // Usage Page (Key Codes),
  124. 0x19, 0x00, // Usage Minimum (0),
  125. 0x29, 0xFF, // Usage Maximum (255),
  126. 0x81, 0x00, // Input (Data, Array),
  127. 0xc0 // End Collection
  128. };
  129. /* wrapper */
  130. static const USBDescriptor keyboard_hid_report_descriptor = {
  131. sizeof keyboard_hid_report_desc_data,
  132. keyboard_hid_report_desc_data
  133. };
  134. #ifdef NKRO_ENABLE
  135. static const uint8_t nkro_hid_report_desc_data[] = {
  136. 0x05, 0x01, // Usage Page (Generic Desktop),
  137. 0x09, 0x06, // Usage (Keyboard),
  138. 0xA1, 0x01, // Collection (Application),
  139. // bitmap of modifiers
  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. // LED output report
  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),
  155. 0x95, 0x01, // Report Count (1),
  156. 0x75, 0x03, // Report Size (3),
  157. 0x91, 0x03, // Output (Constant),
  158. // bitmap of keys
  159. 0x95, NKRO_REPORT_KEYS * 8, // Report Count (),
  160. 0x75, 0x01, // Report Size (1),
  161. 0x15, 0x00, // Logical Minimum (0),
  162. 0x25, 0x01, // Logical Maximum(1),
  163. 0x05, 0x07, // Usage Page (Key Codes),
  164. 0x19, 0x00, // Usage Minimum (0),
  165. 0x29, NKRO_REPORT_KEYS * 8 - 1, // Usage Maximum (),
  166. 0x81, 0x02, // Input (Data, Variable, Absolute),
  167. 0xc0 // End Collection
  168. };
  169. /* wrapper */
  170. static const USBDescriptor nkro_hid_report_descriptor = {
  171. sizeof nkro_hid_report_desc_data,
  172. nkro_hid_report_desc_data
  173. };
  174. #endif /* NKRO_ENABLE */
  175. #ifdef MOUSE_ENABLE
  176. /* Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  177. * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
  178. * http://www.keil.com/forum/15671/
  179. * http://www.microsoft.com/whdc/device/input/wheel.mspx */
  180. static const uint8_t mouse_hid_report_desc_data[] = {
  181. /* mouse */
  182. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  183. 0x09, 0x02, // USAGE (Mouse)
  184. 0xa1, 0x01, // COLLECTION (Application)
  185. //0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
  186. 0x09, 0x01, // USAGE (Pointer)
  187. 0xa1, 0x00, // COLLECTION (Physical)
  188. // ---------------------------- Buttons
  189. 0x05, 0x09, // USAGE_PAGE (Button)
  190. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  191. 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
  192. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  193. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  194. 0x75, 0x01, // REPORT_SIZE (1)
  195. 0x95, 0x05, // REPORT_COUNT (5)
  196. 0x81, 0x02, // INPUT (Data,Var,Abs)
  197. 0x75, 0x03, // REPORT_SIZE (3)
  198. 0x95, 0x01, // REPORT_COUNT (1)
  199. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  200. // ---------------------------- X,Y position
  201. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  202. 0x09, 0x30, // USAGE (X)
  203. 0x09, 0x31, // USAGE (Y)
  204. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  205. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  206. 0x75, 0x08, // REPORT_SIZE (8)
  207. 0x95, 0x02, // REPORT_COUNT (2)
  208. 0x81, 0x06, // INPUT (Data,Var,Rel)
  209. // ---------------------------- Vertical wheel
  210. 0x09, 0x38, // USAGE (Wheel)
  211. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  212. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  213. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  214. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  215. 0x75, 0x08, // REPORT_SIZE (8)
  216. 0x95, 0x01, // REPORT_COUNT (1)
  217. 0x81, 0x06, // INPUT (Data,Var,Rel)
  218. // ---------------------------- Horizontal wheel
  219. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  220. 0x0a, 0x38, 0x02, // USAGE (AC Pan)
  221. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  222. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  223. 0x75, 0x08, // REPORT_SIZE (8)
  224. 0x95, 0x01, // REPORT_COUNT (1)
  225. 0x81, 0x06, // INPUT (Data,Var,Rel)
  226. 0xc0, // END_COLLECTION
  227. 0xc0, // END_COLLECTION
  228. };
  229. /* wrapper */
  230. static const USBDescriptor mouse_hid_report_descriptor = {
  231. sizeof mouse_hid_report_desc_data,
  232. mouse_hid_report_desc_data
  233. };
  234. #endif /* MOUSE_ENABLE */
  235. #ifdef CONSOLE_ENABLE
  236. static const uint8_t console_hid_report_desc_data[] = {
  237. 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
  238. 0x09, 0x74, // Usage 0x74
  239. 0xA1, 0x53, // Collection 0x53
  240. 0x75, 0x08, // report size = 8 bits
  241. 0x15, 0x00, // logical minimum = 0
  242. 0x26, 0xFF, 0x00, // logical maximum = 255
  243. 0x95, CONSOLE_EPSIZE, // report count
  244. 0x09, 0x75, // usage
  245. 0x81, 0x02, // Input (array)
  246. 0xC0 // end collection
  247. };
  248. /* wrapper */
  249. static const USBDescriptor console_hid_report_descriptor = {
  250. sizeof console_hid_report_desc_data,
  251. console_hid_report_desc_data
  252. };
  253. #endif /* CONSOLE_ENABLE */
  254. #ifdef EXTRAKEY_ENABLE
  255. /* audio controls & system controls
  256. * http://www.microsoft.com/whdc/archive/w2kbd.mspx */
  257. static const uint8_t extra_hid_report_desc_data[] = {
  258. /* system control */
  259. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  260. 0x09, 0x80, // USAGE (System Control)
  261. 0xa1, 0x01, // COLLECTION (Application)
  262. 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
  263. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  264. 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
  265. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  266. 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
  267. 0x75, 0x10, // REPORT_SIZE (16)
  268. 0x95, 0x01, // REPORT_COUNT (1)
  269. 0x81, 0x00, // INPUT (Data,Array,Abs)
  270. 0xc0, // END_COLLECTION
  271. /* consumer */
  272. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  273. 0x09, 0x01, // USAGE (Consumer Control)
  274. 0xa1, 0x01, // COLLECTION (Application)
  275. 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
  276. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  277. 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
  278. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  279. 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
  280. 0x75, 0x10, // REPORT_SIZE (16)
  281. 0x95, 0x01, // REPORT_COUNT (1)
  282. 0x81, 0x00, // INPUT (Data,Array,Abs)
  283. 0xc0, // END_COLLECTION
  284. };
  285. /* wrapper */
  286. static const USBDescriptor extra_hid_report_descriptor = {
  287. sizeof extra_hid_report_desc_data,
  288. extra_hid_report_desc_data
  289. };
  290. #endif /* EXTRAKEY_ENABLE */
  291. /*
  292. * Configuration Descriptor tree for a HID device
  293. *
  294. * The HID Specifications version 1.11 require the following order:
  295. * - Configuration Descriptor
  296. * - Interface Descriptor
  297. * - HID Descriptor
  298. * - Endpoints Descriptors
  299. */
  300. #define KBD_HID_DESC_NUM 0
  301. #define KBD_HID_DESC_OFFSET (9 + (9 + 9 + 7) * KBD_HID_DESC_NUM + 9)
  302. #ifdef MOUSE_ENABLE
  303. # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1)
  304. # define MOUSE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * MOUSE_HID_DESC_NUM + 9)
  305. #else /* MOUSE_ENABLE */
  306. # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
  307. #endif /* MOUSE_ENABLE */
  308. #ifdef CONSOLE_ENABLE
  309. #define CONSOLE_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1)
  310. #define CONSOLE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * CONSOLE_HID_DESC_NUM + 9)
  311. #else /* CONSOLE_ENABLE */
  312. # define CONSOLE_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0)
  313. #endif /* CONSOLE_ENABLE */
  314. #ifdef EXTRAKEY_ENABLE
  315. # define EXTRA_HID_DESC_NUM (CONSOLE_HID_DESC_NUM + 1)
  316. # define EXTRA_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9)
  317. #else /* EXTRAKEY_ENABLE */
  318. # define EXTRA_HID_DESC_NUM (CONSOLE_HID_DESC_NUM + 0)
  319. #endif /* EXTRAKEY_ENABLE */
  320. #ifdef NKRO_ENABLE
  321. # define NKRO_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1)
  322. # define NKRO_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9)
  323. #else /* NKRO_ENABLE */
  324. # define NKRO_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
  325. #endif /* NKRO_ENABLE */
  326. #define NUM_INTERFACES (NKRO_HID_DESC_NUM + 1)
  327. #define CONFIG1_DESC_SIZE (9 + (9 + 9 + 7) * NUM_INTERFACES)
  328. static const uint8_t hid_configuration_descriptor_data[] = {
  329. /* Configuration Descriptor (9 bytes) USB spec 9.6.3, page 264-266, Table 9-10 */
  330. USB_DESC_CONFIGURATION(CONFIG1_DESC_SIZE, // wTotalLength
  331. NUM_INTERFACES, // bNumInterfaces
  332. 1, // bConfigurationValue
  333. 0, // iConfiguration
  334. 0xA0, // bmAttributes
  335. 50), // bMaxPower (100mA)
  336. /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
  337. USB_DESC_INTERFACE(KBD_INTERFACE, // bInterfaceNumber
  338. 0, // bAlternateSetting
  339. 1, // bNumEndpoints
  340. 0x03, // bInterfaceClass: HID
  341. 0x01, // bInterfaceSubClass: Boot
  342. 0x01, // bInterfaceProtocol: Keyboard
  343. 0), // iInterface
  344. /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
  345. USB_DESC_BYTE(9), // bLength
  346. USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
  347. USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
  348. USB_DESC_BYTE(0), // bCountryCode
  349. USB_DESC_BYTE(1), // bNumDescriptors
  350. USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
  351. USB_DESC_WORD(sizeof(keyboard_hid_report_desc_data)), // wDescriptorLength
  352. /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
  353. USB_DESC_ENDPOINT(KBD_ENDPOINT | 0x80, // bEndpointAddress
  354. 0x03, // bmAttributes (Interrupt)
  355. KBD_EPSIZE,// wMaxPacketSize
  356. 10), // bInterval
  357. #ifdef MOUSE_ENABLE
  358. /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
  359. USB_DESC_INTERFACE(MOUSE_INTERFACE, // bInterfaceNumber
  360. 0, // bAlternateSetting
  361. 1, // bNumEndpoints
  362. 0x03, // bInterfaceClass (0x03 = HID)
  363. // ThinkPad T23 BIOS doesn't work with boot mouse.
  364. 0x00, // bInterfaceSubClass (0x01 = Boot)
  365. 0x00, // bInterfaceProtocol (0x02 = Mouse)
  366. /*
  367. 0x01, // bInterfaceSubClass (0x01 = Boot)
  368. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  369. */
  370. 0), // iInterface
  371. /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
  372. USB_DESC_BYTE(9), // bLength
  373. USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
  374. USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
  375. USB_DESC_BYTE(0), // bCountryCode
  376. USB_DESC_BYTE(1), // bNumDescriptors
  377. USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
  378. USB_DESC_WORD(sizeof(mouse_hid_report_desc_data)), // wDescriptorLength
  379. /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
  380. USB_DESC_ENDPOINT(MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  381. 0x03, // bmAttributes (Interrupt)
  382. MOUSE_EPSIZE, // wMaxPacketSize
  383. 1), // bInterval
  384. #endif /* MOUSE_ENABLE */
  385. #ifdef CONSOLE_ENABLE
  386. /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
  387. USB_DESC_INTERFACE(CONSOLE_INTERFACE, // bInterfaceNumber
  388. 0, // bAlternateSetting
  389. 1, // bNumEndpoints
  390. 0x03, // bInterfaceClass: HID
  391. 0x00, // bInterfaceSubClass: None
  392. 0x00, // bInterfaceProtocol: None
  393. 0), // iInterface
  394. /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
  395. USB_DESC_BYTE(9), // bLength
  396. USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
  397. USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
  398. USB_DESC_BYTE(0), // bCountryCode
  399. USB_DESC_BYTE(1), // bNumDescriptors
  400. USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
  401. USB_DESC_WORD(sizeof(console_hid_report_desc_data)), // wDescriptorLength
  402. /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
  403. USB_DESC_ENDPOINT(CONSOLE_ENDPOINT | 0x80, // bEndpointAddress
  404. 0x03, // bmAttributes (Interrupt)
  405. CONSOLE_EPSIZE, // wMaxPacketSize
  406. 1), // bInterval
  407. #endif /* CONSOLE_ENABLE */
  408. #ifdef EXTRAKEY_ENABLE
  409. /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
  410. USB_DESC_INTERFACE(EXTRA_INTERFACE, // bInterfaceNumber
  411. 0, // bAlternateSetting
  412. 1, // bNumEndpoints
  413. 0x03, // bInterfaceClass: HID
  414. 0x00, // bInterfaceSubClass: None
  415. 0x00, // bInterfaceProtocol: None
  416. 0), // iInterface
  417. /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
  418. USB_DESC_BYTE(9), // bLength
  419. USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
  420. USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
  421. USB_DESC_BYTE(0), // bCountryCode
  422. USB_DESC_BYTE(1), // bNumDescriptors
  423. USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
  424. USB_DESC_WORD(sizeof(extra_hid_report_desc_data)), // wDescriptorLength
  425. /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
  426. USB_DESC_ENDPOINT(EXTRA_ENDPOINT | 0x80, // bEndpointAddress
  427. 0x03, // bmAttributes (Interrupt)
  428. EXTRA_EPSIZE, // wMaxPacketSize
  429. 10), // bInterval
  430. #endif /* EXTRAKEY_ENABLE */
  431. #ifdef NKRO_ENABLE
  432. /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
  433. USB_DESC_INTERFACE(NKRO_INTERFACE, // bInterfaceNumber
  434. 0, // bAlternateSetting
  435. 1, // bNumEndpoints
  436. 0x03, // bInterfaceClass: HID
  437. 0x00, // bInterfaceSubClass: None
  438. 0x00, // bInterfaceProtocol: None
  439. 0), // iInterface
  440. /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
  441. USB_DESC_BYTE(9), // bLength
  442. USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
  443. USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
  444. USB_DESC_BYTE(0), // bCountryCode
  445. USB_DESC_BYTE(1), // bNumDescriptors
  446. USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
  447. USB_DESC_WORD(sizeof(nkro_hid_report_desc_data)), // wDescriptorLength
  448. /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
  449. USB_DESC_ENDPOINT(NKRO_ENDPOINT | 0x80, // bEndpointAddress
  450. 0x03, // bmAttributes (Interrupt)
  451. NKRO_EPSIZE, // wMaxPacketSize
  452. 1), // bInterval
  453. #endif /* NKRO_ENABLE */
  454. };
  455. /* Configuration Descriptor wrapper */
  456. static const USBDescriptor hid_configuration_descriptor = {
  457. sizeof hid_configuration_descriptor_data,
  458. hid_configuration_descriptor_data
  459. };
  460. /* wrappers */
  461. #define HID_DESCRIPTOR_SIZE 9
  462. static const USBDescriptor keyboard_hid_descriptor = {
  463. HID_DESCRIPTOR_SIZE,
  464. &hid_configuration_descriptor_data[KBD_HID_DESC_OFFSET]
  465. };
  466. #ifdef MOUSE_ENABLE
  467. static const USBDescriptor mouse_hid_descriptor = {
  468. HID_DESCRIPTOR_SIZE,
  469. &hid_configuration_descriptor_data[MOUSE_HID_DESC_OFFSET]
  470. };
  471. #endif /* MOUSE_ENABLE */
  472. #ifdef CONSOLE_ENABLE
  473. static const USBDescriptor console_hid_descriptor = {
  474. HID_DESCRIPTOR_SIZE,
  475. &hid_configuration_descriptor_data[CONSOLE_HID_DESC_OFFSET]
  476. };
  477. #endif /* CONSOLE_ENABLE */
  478. #ifdef EXTRAKEY_ENABLE
  479. static const USBDescriptor extra_hid_descriptor = {
  480. HID_DESCRIPTOR_SIZE,
  481. &hid_configuration_descriptor_data[EXTRA_HID_DESC_OFFSET]
  482. };
  483. #endif /* EXTRAKEY_ENABLE */
  484. #ifdef NKRO_ENABLE
  485. static const USBDescriptor nkro_hid_descriptor = {
  486. HID_DESCRIPTOR_SIZE,
  487. &hid_configuration_descriptor_data[NKRO_HID_DESC_OFFSET]
  488. };
  489. #endif /* NKRO_ENABLE */
  490. /* U.S. English language identifier */
  491. static const uint8_t usb_string_langid[] = {
  492. USB_DESC_BYTE(4), // bLength
  493. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
  494. USB_DESC_WORD(0x0409) // wLANGID (U.S. English)
  495. };
  496. /* ugly ugly hack */
  497. #define PP_NARG(...) \
  498. PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
  499. #define PP_NARG_(...) \
  500. PP_ARG_N(__VA_ARGS__)
  501. #define PP_ARG_N( \
  502. _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
  503. _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
  504. _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
  505. _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
  506. _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
  507. _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
  508. _61,_62,_63,N,...) N
  509. #define PP_RSEQ_N() \
  510. 63,62,61,60, \
  511. 59,58,57,56,55,54,53,52,51,50, \
  512. 49,48,47,46,45,44,43,42,41,40, \
  513. 39,38,37,36,35,34,33,32,31,30, \
  514. 29,28,27,26,25,24,23,22,21,20, \
  515. 19,18,17,16,15,14,13,12,11,10, \
  516. 9,8,7,6,5,4,3,2,1,0
  517. /* Vendor string = manufacturer */
  518. static const uint8_t usb_string_vendor[] = {
  519. USB_DESC_BYTE(PP_NARG(USBSTR_MANUFACTURER)+2), // bLength
  520. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
  521. USBSTR_MANUFACTURER
  522. };
  523. /* Device Description string = product */
  524. static const uint8_t usb_string_description[] = {
  525. USB_DESC_BYTE(PP_NARG(USBSTR_PRODUCT)+2), // bLength
  526. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
  527. USBSTR_PRODUCT
  528. };
  529. /* Serial Number string (will be filled by the function init_usb_serial_string) */
  530. static uint8_t usb_string_serial[] = {
  531. USB_DESC_BYTE(22), // bLength
  532. USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
  533. '0', 0, 'x', 0, 'D', 0, 'E', 0, 'A', 0, 'D', 0, 'B', 0, 'E', 0, 'E', 0, 'F', 0
  534. };
  535. /* Strings wrappers array */
  536. static const USBDescriptor usb_strings[] = {
  537. { sizeof usb_string_langid, usb_string_langid }
  538. ,
  539. { sizeof usb_string_vendor, usb_string_vendor }
  540. ,
  541. { sizeof usb_string_description, usb_string_description }
  542. ,
  543. { sizeof usb_string_serial, usb_string_serial }
  544. };
  545. /*
  546. * Handles the GET_DESCRIPTOR callback
  547. *
  548. * Returns the proper descriptor
  549. */
  550. static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) {
  551. (void)usbp;
  552. (void)lang;
  553. switch(dtype) {
  554. /* Generic descriptors */
  555. case USB_DESCRIPTOR_DEVICE: /* Device Descriptor */
  556. return &usb_device_descriptor;
  557. case USB_DESCRIPTOR_CONFIGURATION: /* Configuration Descriptor */
  558. return &hid_configuration_descriptor;
  559. case USB_DESCRIPTOR_STRING: /* Strings */
  560. if(dindex < 4)
  561. return &usb_strings[dindex];
  562. break;
  563. /* HID specific descriptors */
  564. case USB_DESCRIPTOR_HID: /* HID Descriptors */
  565. switch(lang) { /* yea, poor label, it's actually wIndex from the setup packet */
  566. case KBD_INTERFACE:
  567. return &keyboard_hid_descriptor;
  568. #ifdef MOUSE_ENABLE
  569. case MOUSE_INTERFACE:
  570. return &mouse_hid_descriptor;
  571. #endif /* MOUSE_ENABLE */
  572. #ifdef CONSOLE_ENABLE
  573. case CONSOLE_INTERFACE:
  574. return &console_hid_descriptor;
  575. #endif /* CONSOLE_ENABLE */
  576. #ifdef EXTRAKEY_ENABLE
  577. case EXTRA_INTERFACE:
  578. return &extra_hid_descriptor;
  579. #endif /* EXTRAKEY_ENABLE */
  580. #ifdef NKRO_ENABLE
  581. case NKRO_INTERFACE:
  582. return &nkro_hid_descriptor;
  583. #endif /* NKRO_ENABLE */
  584. }
  585. case USB_DESCRIPTOR_HID_REPORT: /* HID Report Descriptor */
  586. switch(lang) {
  587. case KBD_INTERFACE:
  588. return &keyboard_hid_report_descriptor;
  589. #ifdef MOUSE_ENABLE
  590. case MOUSE_INTERFACE:
  591. return &mouse_hid_report_descriptor;
  592. #endif /* MOUSE_ENABLE */
  593. #ifdef CONSOLE_ENABLE
  594. case CONSOLE_INTERFACE:
  595. return &console_hid_report_descriptor;
  596. #endif /* CONSOLE_ENABLE */
  597. #ifdef EXTRAKEY_ENABLE
  598. case EXTRA_INTERFACE:
  599. return &extra_hid_report_descriptor;
  600. #endif /* EXTRAKEY_ENABLE */
  601. #ifdef NKRO_ENABLE
  602. case NKRO_INTERFACE:
  603. return &nkro_hid_report_descriptor;
  604. #endif /* NKRO_ENABLE */
  605. }
  606. }
  607. return NULL;
  608. }
  609. /* keyboard endpoint state structure */
  610. static USBInEndpointState kbd_ep_state;
  611. /* keyboard endpoint initialization structure (IN) */
  612. static const USBEndpointConfig kbd_ep_config = {
  613. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  614. NULL, /* SETUP packet notification callback */
  615. kbd_in_cb, /* IN notification callback */
  616. NULL, /* OUT notification callback */
  617. KBD_EPSIZE, /* IN maximum packet size */
  618. 0, /* OUT maximum packet size */
  619. &kbd_ep_state, /* IN Endpoint state */
  620. NULL, /* OUT endpoint state */
  621. 2, /* IN multiplier */
  622. NULL /* SETUP buffer (not a SETUP endpoint) */
  623. };
  624. #ifdef MOUSE_ENABLE
  625. /* mouse endpoint state structure */
  626. static USBInEndpointState mouse_ep_state;
  627. /* mouse endpoint initialization structure (IN) */
  628. static const USBEndpointConfig mouse_ep_config = {
  629. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  630. NULL, /* SETUP packet notification callback */
  631. mouse_in_cb, /* IN notification callback */
  632. NULL, /* OUT notification callback */
  633. MOUSE_EPSIZE, /* IN maximum packet size */
  634. 0, /* OUT maximum packet size */
  635. &mouse_ep_state, /* IN Endpoint state */
  636. NULL, /* OUT endpoint state */
  637. 2, /* IN multiplier */
  638. NULL /* SETUP buffer (not a SETUP endpoint) */
  639. };
  640. #endif /* MOUSE_ENABLE */
  641. #ifdef CONSOLE_ENABLE
  642. /* console endpoint state structure */
  643. static USBInEndpointState console_ep_state;
  644. /* console endpoint initialization structure (IN) */
  645. static const USBEndpointConfig console_ep_config = {
  646. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  647. NULL, /* SETUP packet notification callback */
  648. console_in_cb, /* IN notification callback */
  649. NULL, /* OUT notification callback */
  650. CONSOLE_EPSIZE, /* IN maximum packet size */
  651. 0, /* OUT maximum packet size */
  652. &console_ep_state, /* IN Endpoint state */
  653. NULL, /* OUT endpoint state */
  654. 2, /* IN multiplier */
  655. NULL /* SETUP buffer (not a SETUP endpoint) */
  656. };
  657. #endif /* CONSOLE_ENABLE */
  658. #ifdef EXTRAKEY_ENABLE
  659. /* extrakey endpoint state structure */
  660. static USBInEndpointState extra_ep_state;
  661. /* extrakey endpoint initialization structure (IN) */
  662. static const USBEndpointConfig extra_ep_config = {
  663. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  664. NULL, /* SETUP packet notification callback */
  665. extra_in_cb, /* IN notification callback */
  666. NULL, /* OUT notification callback */
  667. EXTRA_EPSIZE, /* IN maximum packet size */
  668. 0, /* OUT maximum packet size */
  669. &extra_ep_state, /* IN Endpoint state */
  670. NULL, /* OUT endpoint state */
  671. 2, /* IN multiplier */
  672. NULL /* SETUP buffer (not a SETUP endpoint) */
  673. };
  674. #endif /* EXTRAKEY_ENABLE */
  675. #ifdef NKRO_ENABLE
  676. /* nkro endpoint state structure */
  677. static USBInEndpointState nkro_ep_state;
  678. /* nkro endpoint initialization structure (IN) */
  679. static const USBEndpointConfig nkro_ep_config = {
  680. USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
  681. NULL, /* SETUP packet notification callback */
  682. nkro_in_cb, /* IN notification callback */
  683. NULL, /* OUT notification callback */
  684. NKRO_EPSIZE, /* IN maximum packet size */
  685. 0, /* OUT maximum packet size */
  686. &nkro_ep_state, /* IN Endpoint state */
  687. NULL, /* OUT endpoint state */
  688. 2, /* IN multiplier */
  689. NULL /* SETUP buffer (not a SETUP endpoint) */
  690. };
  691. #endif /* NKRO_ENABLE */
  692. /* ---------------------------------------------------------
  693. * USB driver functions
  694. * ---------------------------------------------------------
  695. */
  696. /* Handles the USB driver global events
  697. * TODO: maybe disable some things when connection is lost? */
  698. static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
  699. switch(event) {
  700. case USB_EVENT_RESET:
  701. //TODO: from ISR! print("[R]");
  702. return;
  703. case USB_EVENT_ADDRESS:
  704. return;
  705. case USB_EVENT_CONFIGURED:
  706. osalSysLockFromISR();
  707. /* Enable the endpoints specified into the configuration. */
  708. usbInitEndpointI(usbp, KBD_ENDPOINT, &kbd_ep_config);
  709. #ifdef MOUSE_ENABLE
  710. usbInitEndpointI(usbp, MOUSE_ENDPOINT, &mouse_ep_config);
  711. #endif /* MOUSE_ENABLE */
  712. #ifdef CONSOLE_ENABLE
  713. usbInitEndpointI(usbp, CONSOLE_ENDPOINT, &console_ep_config);
  714. /* don't need to start the flush timer, it starts from console_in_cb automatically */
  715. #endif /* CONSOLE_ENABLE */
  716. #ifdef EXTRAKEY_ENABLE
  717. usbInitEndpointI(usbp, EXTRA_ENDPOINT, &extra_ep_config);
  718. #endif /* EXTRAKEY_ENABLE */
  719. #ifdef NKRO_ENABLE
  720. usbInitEndpointI(usbp, NKRO_ENDPOINT, &nkro_ep_config);
  721. #endif /* NKRO_ENABLE */
  722. osalSysUnlockFromISR();
  723. return;
  724. case USB_EVENT_SUSPEND:
  725. //TODO: from ISR! print("[S]");
  726. //TODO: signal suspend?
  727. #ifdef SLEEP_LED_ENABLE
  728. sleep_led_enable();
  729. #endif /* SLEEP_LED_ENABLE */
  730. return;
  731. case USB_EVENT_WAKEUP:
  732. //TODO: from ISR! print("[W]");
  733. //TODO: suspend_wakeup_init();
  734. #ifdef SLEEP_LED_ENABLE
  735. sleep_led_disable();
  736. // NOTE: converters may not accept this
  737. led_set(host_keyboard_leds());
  738. #endif /* SLEEP_LED_ENABLE */
  739. return;
  740. case USB_EVENT_STALLED:
  741. return;
  742. }
  743. }
  744. /* Function used locally in os/hal/src/usb.c for getting descriptors
  745. * need it here for HID descriptor */
  746. static uint16_t get_hword(uint8_t *p) {
  747. uint16_t hw;
  748. hw = (uint16_t)*p++;
  749. hw |= (uint16_t)*p << 8U;
  750. return hw;
  751. }
  752. /*
  753. * Appendix G: HID Request Support Requirements
  754. *
  755. * The following table enumerates the requests that need to be supported by various types of HID class devices.
  756. * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
  757. * ------------------------------------------------------------------------------------------
  758. * Boot Mouse Required Optional Optional Optional Required Required
  759. * Non-Boot Mouse Required Optional Optional Optional Optional Optional
  760. * Boot Keyboard Required Optional Required Required Required Required
  761. * Non-Boot Keybrd Required Optional Required Required Optional Optional
  762. * Other Device Required Optional Optional Optional Optional Optional
  763. */
  764. /* Callback for SETUP request on the endpoint 0 (control) */
  765. static bool usb_request_hook_cb(USBDriver *usbp) {
  766. const USBDescriptor *dp;
  767. /* usbp->setup fields:
  768. * 0: bmRequestType (bitmask)
  769. * 1: bRequest
  770. * 2,3: (LSB,MSB) wValue
  771. * 4,5: (LSB,MSB) wIndex
  772. * 6,7: (LSB,MSB) wLength (number of bytes to transfer if there is a data phase) */
  773. /* Handle HID class specific requests */
  774. if(((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) &&
  775. ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) {
  776. switch(usbp->setup[0] & USB_RTYPE_DIR_MASK) {
  777. case USB_RTYPE_DIR_DEV2HOST:
  778. switch(usbp->setup[1]) { /* bRequest */
  779. case HID_GET_REPORT:
  780. switch(usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */
  781. case KBD_INTERFACE:
  782. #ifdef NKRO_ENABLE
  783. case NKRO_INTERFACE:
  784. #endif /* NKRO_ENABLE */
  785. usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL);
  786. return TRUE;
  787. break;
  788. #ifdef MOUSE_ENABLE
  789. case MOUSE_INTERFACE:
  790. usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL);
  791. return TRUE;
  792. break;
  793. #endif /* MOUSE_ENABLE */
  794. #ifdef CONSOLE_ENABLE
  795. case CONSOLE_INTERFACE:
  796. usbSetupTransfer(usbp, console_queue_buffer, CONSOLE_EPSIZE, NULL);
  797. return TRUE;
  798. break;
  799. #endif /* CONSOLE_ENABLE */
  800. #ifdef EXTRAKEY_ENABLE
  801. case EXTRA_INTERFACE:
  802. if(usbp->setup[3] == 1) { /* MSB(wValue) [Report Type] == 1 [Input Report] */
  803. switch(usbp->setup[2]) { /* LSB(wValue) [Report ID] */
  804. case REPORT_ID_SYSTEM:
  805. extra_report_blank[0] = REPORT_ID_SYSTEM;
  806. usbSetupTransfer(usbp, (uint8_t *)extra_report_blank, sizeof(extra_report_blank), NULL);
  807. return TRUE;
  808. break;
  809. case REPORT_ID_CONSUMER:
  810. extra_report_blank[0] = REPORT_ID_CONSUMER;
  811. usbSetupTransfer(usbp, (uint8_t *)extra_report_blank, sizeof(extra_report_blank), NULL);
  812. return TRUE;
  813. break;
  814. default:
  815. return FALSE;
  816. }
  817. } else {
  818. return FALSE;
  819. }
  820. break;
  821. #endif /* EXTRAKEY_ENABLE */
  822. default:
  823. usbSetupTransfer(usbp, NULL, 0, NULL);
  824. return TRUE;
  825. break;
  826. }
  827. break;
  828. case HID_GET_PROTOCOL:
  829. if((usbp->setup[4] == KBD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
  830. usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL);
  831. return TRUE;
  832. }
  833. break;
  834. case HID_GET_IDLE:
  835. usbSetupTransfer(usbp, &keyboard_idle, 1, NULL);
  836. return TRUE;
  837. break;
  838. }
  839. break;
  840. case USB_RTYPE_DIR_HOST2DEV:
  841. switch(usbp->setup[1]) { /* bRequest */
  842. case HID_SET_REPORT:
  843. switch(usbp->setup[4]) { /* LSB(wIndex) (check MSB==0 and wLength==1?) */
  844. case KBD_INTERFACE:
  845. #ifdef NKRO_ENABLE
  846. case NKRO_INTERFACE:
  847. #endif /* NKRO_ENABLE */
  848. /* keyboard_led_stats = <read byte from next OUT report>
  849. * keyboard_led_stats needs be word (or dword), otherwise we get an exception on F0 */
  850. usbSetupTransfer(usbp, (uint8_t *)&keyboard_led_stats, 1, NULL);
  851. return TRUE;
  852. break;
  853. }
  854. break;
  855. case HID_SET_PROTOCOL:
  856. if((usbp->setup[4] == KBD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
  857. keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */
  858. #ifdef NKRO_ENABLE
  859. keyboard_nkro = !!keyboard_protocol;
  860. if(!keyboard_nkro && keyboard_idle) {
  861. #else /* NKRO_ENABLE */
  862. if(keyboard_idle) {
  863. #endif /* NKRO_ENABLE */
  864. /* arm the idle timer if boot protocol & idle */
  865. osalSysLockFromISR();
  866. chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
  867. osalSysUnlockFromISR();
  868. }
  869. }
  870. usbSetupTransfer(usbp, NULL, 0, NULL);
  871. return TRUE;
  872. break;
  873. case HID_SET_IDLE:
  874. keyboard_idle = usbp->setup[3]; /* MSB(wValue) */
  875. /* arm the timer */
  876. #ifdef NKRO_ENABLE
  877. if(!keyboard_nkro && keyboard_idle) {
  878. #else /* NKRO_ENABLE */
  879. if(keyboard_idle) {
  880. #endif /* NKRO_ENABLE */
  881. osalSysLockFromISR();
  882. chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
  883. osalSysUnlockFromISR();
  884. }
  885. usbSetupTransfer(usbp, NULL, 0, NULL);
  886. return TRUE;
  887. break;
  888. }
  889. break;
  890. }
  891. }
  892. /* Handle the Get_Descriptor Request for HID class (not handled by the default hook) */
  893. if((usbp->setup[0] == 0x81) && (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)) {
  894. dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], get_hword(&usbp->setup[4]));
  895. if(dp == NULL)
  896. return FALSE;
  897. usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
  898. return TRUE;
  899. }
  900. return FALSE;
  901. }
  902. /* Start-of-frame callback */
  903. static void usb_sof_cb(USBDriver *usbp) {
  904. kbd_sof_cb(usbp);
  905. }
  906. /* USB driver configuration */
  907. static const USBConfig usbcfg = {
  908. usb_event_cb, /* USB events callback */
  909. usb_get_descriptor_cb, /* Device GET_DESCRIPTOR request callback */
  910. usb_request_hook_cb, /* Requests hook callback */
  911. usb_sof_cb /* Start Of Frame callback */
  912. };
  913. /*
  914. * Initialize the USB driver
  915. */
  916. void init_usb_driver(USBDriver *usbp) {
  917. /*
  918. * Activates the USB driver and then the USB bus pull-up on D+.
  919. * Note, a delay is inserted in order to not have to disconnect the cable
  920. * after a reset.
  921. */
  922. usbDisconnectBus(usbp);
  923. chThdSleepMilliseconds(1500);
  924. usbStart(usbp, &usbcfg);
  925. usbConnectBus(usbp);
  926. chVTObjectInit(&keyboard_idle_timer);
  927. #ifdef CONSOLE_ENABLE
  928. oqObjectInit(&console_queue, console_queue_buffer, sizeof(console_queue_buffer), console_queue_onotify, (void *)usbp);
  929. chVTObjectInit(&console_flush_timer);
  930. #endif
  931. }
  932. /* ---------------------------------------------------------
  933. * Keyboard functions
  934. * ---------------------------------------------------------
  935. */
  936. /* keyboard IN callback hander (a kbd report has made it IN) */
  937. void kbd_in_cb(USBDriver *usbp, usbep_t ep) {
  938. /* STUB */
  939. (void)usbp;
  940. (void)ep;
  941. }
  942. #ifdef NKRO_ENABLE
  943. /* nkro IN callback hander (a nkro report has made it IN) */
  944. void nkro_in_cb(USBDriver *usbp, usbep_t ep) {
  945. /* STUB */
  946. (void)usbp;
  947. (void)ep;
  948. }
  949. #endif /* NKRO_ENABLE */
  950. /* start-of-frame handler
  951. * TODO: i guess it would be better to re-implement using timers,
  952. * so that this is not going to have to be checked every 1ms */
  953. void kbd_sof_cb(USBDriver *usbp) {
  954. (void)usbp;
  955. }
  956. /* Idle requests timer code
  957. * callback (called from ISR, unlocked state) */
  958. static void keyboard_idle_timer_cb(void *arg) {
  959. USBDriver *usbp = (USBDriver *)arg;
  960. osalSysLockFromISR();
  961. /* check that the states of things are as they're supposed to */
  962. if(usbGetDriverStateI(usbp) != USB_ACTIVE) {
  963. /* do not rearm the timer, should be enabled on IDLE request */
  964. osalSysUnlockFromISR();
  965. return;
  966. }
  967. #ifdef NKRO_ENABLE
  968. if(!keyboard_nkro && keyboard_idle) {
  969. #else /* NKRO_ENABLE */
  970. if(keyboard_idle) {
  971. #endif /* NKRO_ENABLE */
  972. /* TODO: are we sure we want the KBD_ENDPOINT? */
  973. osalSysUnlockFromISR();
  974. usbPrepareTransmit(usbp, KBD_ENDPOINT, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent));
  975. osalSysLockFromISR();
  976. usbStartTransmitI(usbp, KBD_ENDPOINT);
  977. /* rearm the timer */
  978. chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
  979. }
  980. /* do not rearm the timer if the condition above fails
  981. * it should be enabled again on either IDLE or SET_PROTOCOL requests */
  982. osalSysUnlockFromISR();
  983. }
  984. /* LED status */
  985. uint8_t keyboard_leds(void) {
  986. return (uint8_t)(keyboard_led_stats & 0xFF);
  987. }
  988. /* prepare and start sending a report IN
  989. * not callable from ISR or locked state */
  990. void send_keyboard(report_keyboard_t *report) {
  991. osalSysLock();
  992. if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  993. osalSysUnlock();
  994. return;
  995. }
  996. osalSysUnlock();
  997. #ifdef NKRO_ENABLE
  998. if(keyboard_nkro) { /* NKRO protocol */
  999. usbPrepareTransmit(&USB_DRIVER, NKRO_ENDPOINT, (uint8_t *)report, sizeof(report_keyboard_t));
  1000. osalSysLock();
  1001. usbStartTransmitI(&USB_DRIVER, NKRO_ENDPOINT);
  1002. osalSysUnlock();
  1003. } else
  1004. #endif /* NKRO_ENABLE */
  1005. { /* boot protocol */
  1006. usbPrepareTransmit(&USB_DRIVER, KBD_ENDPOINT, (uint8_t *)report, sizeof(report_keyboard_t));
  1007. osalSysLock();
  1008. usbStartTransmitI(&USB_DRIVER, KBD_ENDPOINT);
  1009. osalSysUnlock();
  1010. }
  1011. keyboard_report_sent = *report;
  1012. }
  1013. /* ---------------------------------------------------------
  1014. * Mouse functions
  1015. * ---------------------------------------------------------
  1016. */
  1017. #ifdef MOUSE_ENABLE
  1018. /* mouse IN callback hander (a mouse report has made it IN) */
  1019. void mouse_in_cb(USBDriver *usbp, usbep_t ep) {
  1020. (void)usbp;
  1021. (void)ep;
  1022. }
  1023. void send_mouse(report_mouse_t *report) {
  1024. osalSysLock();
  1025. if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  1026. osalSysUnlock();
  1027. return;
  1028. }
  1029. osalSysUnlock();
  1030. /* TODO: LUFA manually waits for the endpoint to become ready
  1031. * for about 10ms for mouse, kbd, system; 1ms for nkro
  1032. * is this really needed?
  1033. */
  1034. usbPrepareTransmit(&USB_DRIVER, MOUSE_ENDPOINT, (uint8_t *)report, sizeof(report_mouse_t));
  1035. osalSysLock();
  1036. usbStartTransmitI(&USB_DRIVER, MOUSE_ENDPOINT);
  1037. osalSysUnlock();
  1038. }
  1039. #else /* MOUSE_ENABLE */
  1040. void send_mouse(report_mouse_t *report) {
  1041. (void)report;
  1042. }
  1043. #endif /* MOUSE_ENABLE */
  1044. /* ---------------------------------------------------------
  1045. * Extrakey functions
  1046. * ---------------------------------------------------------
  1047. */
  1048. #ifdef EXTRAKEY_ENABLE
  1049. /* extrakey IN callback hander */
  1050. void extra_in_cb(USBDriver *usbp, usbep_t ep) {
  1051. /* STUB */
  1052. (void)usbp;
  1053. (void)ep;
  1054. }
  1055. static void send_extra_report(uint8_t report_id, uint16_t data) {
  1056. osalSysLock();
  1057. if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  1058. osalSysUnlock();
  1059. return;
  1060. }
  1061. report_extra_t report = {
  1062. .report_id = report_id,
  1063. .usage = data
  1064. };
  1065. osalSysUnlock();
  1066. usbPrepareTransmit(&USB_DRIVER, EXTRA_ENDPOINT, (uint8_t *)&report, sizeof(report_extra_t));
  1067. osalSysLock();
  1068. usbStartTransmitI(&USB_DRIVER, EXTRA_ENDPOINT);
  1069. osalSysUnlock();
  1070. }
  1071. void send_system(uint16_t data) {
  1072. send_extra_report(REPORT_ID_SYSTEM, data);
  1073. }
  1074. void send_consumer(uint16_t data) {
  1075. send_extra_report(REPORT_ID_CONSUMER, data);
  1076. }
  1077. #else /* EXTRAKEY_ENABLE */
  1078. void send_system(uint16_t data) {
  1079. (void)data;
  1080. }
  1081. void send_consumer(uint16_t data) {
  1082. (void)data;
  1083. }
  1084. #endif /* EXTRAKEY_ENABLE */
  1085. /* ---------------------------------------------------------
  1086. * Console functions
  1087. * ---------------------------------------------------------
  1088. */
  1089. #ifdef CONSOLE_ENABLE
  1090. /* debug IN callback hander */
  1091. void console_in_cb(USBDriver *usbp, usbep_t ep) {
  1092. (void)ep;
  1093. osalSysLockFromISR();
  1094. /* rearm the timer */
  1095. chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
  1096. /* Check if there is data to send left in the output queue */
  1097. if(chOQGetFullI(&console_queue) >= CONSOLE_EPSIZE) {
  1098. osalSysUnlockFromISR();
  1099. usbPrepareQueuedTransmit(usbp, CONSOLE_ENDPOINT, &console_queue, CONSOLE_EPSIZE);
  1100. osalSysLockFromISR();
  1101. usbStartTransmitI(usbp, CONSOLE_ENDPOINT);
  1102. }
  1103. osalSysUnlockFromISR();
  1104. }
  1105. /* Callback when data is inserted into the output queue
  1106. * Called from a locked state */
  1107. void console_queue_onotify(io_queue_t *qp) {
  1108. USBDriver *usbp = qGetLink(qp);
  1109. if(usbGetDriverStateI(usbp) != USB_ACTIVE)
  1110. return;
  1111. if(!usbGetTransmitStatusI(usbp, CONSOLE_ENDPOINT)
  1112. && (chOQGetFullI(&console_queue) >= CONSOLE_EPSIZE)) {
  1113. osalSysUnlock();
  1114. usbPrepareQueuedTransmit(usbp, CONSOLE_ENDPOINT, &console_queue, CONSOLE_EPSIZE);
  1115. osalSysLock();
  1116. usbStartTransmitI(usbp, CONSOLE_ENDPOINT);
  1117. }
  1118. }
  1119. /* Flush timer code
  1120. * callback (called from ISR, unlocked state) */
  1121. static void console_flush_cb(void *arg) {
  1122. USBDriver *usbp = (USBDriver *)arg;
  1123. size_t i, n;
  1124. uint8_t buf[CONSOLE_EPSIZE]; /* TODO: a solution without extra buffer? */
  1125. osalSysLockFromISR();
  1126. /* check that the states of things are as they're supposed to */
  1127. if(usbGetDriverStateI(usbp) != USB_ACTIVE) {
  1128. /* rearm the timer */
  1129. chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
  1130. osalSysUnlockFromISR();
  1131. return;
  1132. }
  1133. /* don't do anything if the queue is empty or has enough stuff in it */
  1134. if(((n = oqGetFullI(&console_queue)) == 0) || (n >= CONSOLE_EPSIZE)) {
  1135. /* rearm the timer */
  1136. chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
  1137. osalSysUnlockFromISR();
  1138. return;
  1139. }
  1140. /* there's stuff hanging in the queue - so dequeue and send */
  1141. for(i = 0; i < n; i++)
  1142. buf[i] = (uint8_t)oqGetI(&console_queue);
  1143. for(i = n; i < CONSOLE_EPSIZE; i++)
  1144. buf[i] = 0;
  1145. osalSysUnlockFromISR();
  1146. usbPrepareTransmit(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
  1147. osalSysLockFromISR();
  1148. (void)usbStartTransmitI(usbp, CONSOLE_ENDPOINT);
  1149. /* rearm the timer */
  1150. chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
  1151. osalSysUnlockFromISR();
  1152. }
  1153. int8_t sendchar(uint8_t c) {
  1154. osalSysLock();
  1155. if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
  1156. osalSysUnlock();
  1157. return 0;
  1158. }
  1159. osalSysUnlock();
  1160. /* should get suspended and wait if the queue is full
  1161. * but it's not blocking even if noone is listening,
  1162. * because the USB packets are sent anyway */
  1163. return(chOQPut(&console_queue, c));
  1164. }
  1165. #else /* CONSOLE_ENABLE */
  1166. int8_t sendchar(uint8_t c) {
  1167. (void)c;
  1168. return 0;
  1169. }
  1170. #endif /* CONSOLE_ENABLE */
  1171. void sendchar_pf(void *p, char c) {
  1172. (void)p;
  1173. sendchar((uint8_t)c);
  1174. }