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

usb_main.c 45KB

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