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

usb_main.c 46KB

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