usb_main.c 50 KB

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