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 46KB

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