usb_main.c 50 KB

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