2 * (c) 2015 flabberast <s3+flabbergast@sdfeu.org>
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
14 * Since some GPL'd code is used, this work is licensed under
26 #ifdef SLEEP_LED_ENABLE
27 #include "sleep_led.h"
33 #include "keycode_config.h"
35 extern keymap_config_t keymap_config;
38 /* ---------------------------------------------------------
39 * Global interface variables and declarations
40 * ---------------------------------------------------------
43 #ifndef usb_lld_connect_bus
44 #define usb_lld_connect_bus(usbp)
47 #ifndef usb_lld_disconnect_bus
48 #define usb_lld_disconnect_bus(usbp)
51 uint8_t keyboard_idle __attribute__((aligned(2))) = 0;
52 uint8_t keyboard_protocol __attribute__((aligned(2))) = 1;
53 uint16_t keyboard_led_stats __attribute__((aligned(2))) = 0;
54 volatile uint16_t keyboard_idle_count = 0;
55 static virtual_timer_t keyboard_idle_timer;
56 static void keyboard_idle_timer_cb(void *arg);
58 report_keyboard_t keyboard_report_sent = {{0}};
60 report_mouse_t mouse_report_blank = {0};
61 #endif /* MOUSE_ENABLE */
62 #ifdef EXTRAKEY_ENABLE
63 uint8_t extra_report_blank[3] = {0};
64 #endif /* EXTRAKEY_ENABLE */
67 /* The emission buffers queue */
68 output_buffers_queue_t console_buf_queue;
69 static uint8_t console_queue_buffer[BQ_BUFFER_SIZE(CONSOLE_QUEUE_CAPACITY, CONSOLE_EPSIZE)];
71 static virtual_timer_t console_flush_timer;
72 void console_queue_onotify(io_buffers_queue_t *bqp);
73 static void console_flush_cb(void *arg);
74 #endif /* CONSOLE_ENABLE */
76 /* ---------------------------------------------------------
77 * Descriptors and USB driver objects
78 * ---------------------------------------------------------
81 /* HID specific constants */
82 #define USB_DESCRIPTOR_HID 0x21
83 #define USB_DESCRIPTOR_HID_REPORT 0x22
84 #define HID_GET_REPORT 0x01
85 #define HID_GET_IDLE 0x02
86 #define HID_GET_PROTOCOL 0x03
87 #define HID_SET_REPORT 0x09
88 #define HID_SET_IDLE 0x0A
89 #define HID_SET_PROTOCOL 0x0B
91 /* USB Device Descriptor */
92 static const uint8_t usb_device_descriptor_data[] = {
93 USB_DESC_DEVICE(0x0200, // bcdUSB (1.1)
94 0, // bDeviceClass (defined in later in interface)
97 64, // bMaxPacketSize (64 bytes) (the driver didn't work with 32)
98 VENDOR_ID, // idVendor
99 PRODUCT_ID, // idProduct
100 DEVICE_VER, // bcdDevice
104 1) // bNumConfigurations
107 /* Device Descriptor wrapper */
108 static const USBDescriptor usb_device_descriptor = {
109 sizeof usb_device_descriptor_data,
110 usb_device_descriptor_data
114 * HID Report Descriptor
116 * See "Device Class Definition for Human Interface Devices (HID)"
117 * (http://www.usb.org/developers/hidpage/HID1_11.pdf) for the
118 * detailed descrition of all the fields
121 /* Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60 */
122 static const uint8_t keyboard_hid_report_desc_data[] = {
123 0x05, 0x01, // Usage Page (Generic Desktop),
124 0x09, 0x06, // Usage (Keyboard),
125 0xA1, 0x01, // Collection (Application),
126 0x75, 0x01, // Report Size (1),
127 0x95, 0x08, // Report Count (8),
128 0x05, 0x07, // Usage Page (Key Codes),
129 0x19, 0xE0, // Usage Minimum (224),
130 0x29, 0xE7, // Usage Maximum (231),
131 0x15, 0x00, // Logical Minimum (0),
132 0x25, 0x01, // Logical Maximum (1),
133 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
134 0x95, 0x01, // Report Count (1),
135 0x75, 0x08, // Report Size (8),
136 0x81, 0x03, // Input (Constant), ;Reserved byte
137 0x95, 0x05, // Report Count (5),
138 0x75, 0x01, // Report Size (1),
139 0x05, 0x08, // Usage Page (LEDs),
140 0x19, 0x01, // Usage Minimum (1),
141 0x29, 0x05, // Usage Maximum (5),
142 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
143 0x95, 0x01, // Report Count (1),
144 0x75, 0x03, // Report Size (3),
145 0x91, 0x03, // Output (Constant), ;LED report padding
146 0x95, KBD_REPORT_KEYS, // Report Count (),
147 0x75, 0x08, // Report Size (8),
148 0x15, 0x00, // Logical Minimum (0),
149 0x26, 0xFF, 0x00, // Logical Maximum(255),
150 0x05, 0x07, // Usage Page (Key Codes),
151 0x19, 0x00, // Usage Minimum (0),
152 0x29, 0xFF, // Usage Maximum (255),
153 0x81, 0x00, // Input (Data, Array),
154 0xc0 // End Collection
157 static const USBDescriptor keyboard_hid_report_descriptor = {
158 sizeof keyboard_hid_report_desc_data,
159 keyboard_hid_report_desc_data
163 static const uint8_t nkro_hid_report_desc_data[] = {
164 0x05, 0x01, // Usage Page (Generic Desktop),
165 0x09, 0x06, // Usage (Keyboard),
166 0xA1, 0x01, // Collection (Application),
167 // bitmap of modifiers
168 0x75, 0x01, // Report Size (1),
169 0x95, 0x08, // Report Count (8),
170 0x05, 0x07, // Usage Page (Key Codes),
171 0x19, 0xE0, // Usage Minimum (224),
172 0x29, 0xE7, // Usage Maximum (231),
173 0x15, 0x00, // Logical Minimum (0),
174 0x25, 0x01, // Logical Maximum (1),
175 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
177 0x95, 0x05, // Report Count (5),
178 0x75, 0x01, // Report Size (1),
179 0x05, 0x08, // Usage Page (LEDs),
180 0x19, 0x01, // Usage Minimum (1),
181 0x29, 0x05, // Usage Maximum (5),
182 0x91, 0x02, // Output (Data, Variable, Absolute),
183 0x95, 0x01, // Report Count (1),
184 0x75, 0x03, // Report Size (3),
185 0x91, 0x03, // Output (Constant),
187 0x95, NKRO_REPORT_KEYS * 8, // Report Count (),
188 0x75, 0x01, // Report Size (1),
189 0x15, 0x00, // Logical Minimum (0),
190 0x25, 0x01, // Logical Maximum(1),
191 0x05, 0x07, // Usage Page (Key Codes),
192 0x19, 0x00, // Usage Minimum (0),
193 0x29, NKRO_REPORT_KEYS * 8 - 1, // Usage Maximum (),
194 0x81, 0x02, // Input (Data, Variable, Absolute),
195 0xc0 // End Collection
198 static const USBDescriptor nkro_hid_report_descriptor = {
199 sizeof nkro_hid_report_desc_data,
200 nkro_hid_report_desc_data
202 #endif /* NKRO_ENABLE */
205 /* Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
206 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
207 * http://www.keil.com/forum/15671/
208 * http://www.microsoft.com/whdc/device/input/wheel.mspx */
209 static const uint8_t mouse_hid_report_desc_data[] = {
211 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
212 0x09, 0x02, // USAGE (Mouse)
213 0xa1, 0x01, // COLLECTION (Application)
214 //0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
215 0x09, 0x01, // USAGE (Pointer)
216 0xa1, 0x00, // COLLECTION (Physical)
217 // ---------------------------- Buttons
218 0x05, 0x09, // USAGE_PAGE (Button)
219 0x19, 0x01, // USAGE_MINIMUM (Button 1)
220 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
221 0x15, 0x00, // LOGICAL_MINIMUM (0)
222 0x25, 0x01, // LOGICAL_MAXIMUM (1)
223 0x75, 0x01, // REPORT_SIZE (1)
224 0x95, 0x05, // REPORT_COUNT (5)
225 0x81, 0x02, // INPUT (Data,Var,Abs)
226 0x75, 0x03, // REPORT_SIZE (3)
227 0x95, 0x01, // REPORT_COUNT (1)
228 0x81, 0x03, // INPUT (Cnst,Var,Abs)
229 // ---------------------------- X,Y position
230 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
231 0x09, 0x30, // USAGE (X)
232 0x09, 0x31, // USAGE (Y)
233 0x15, 0x81, // LOGICAL_MINIMUM (-127)
234 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
235 0x75, 0x08, // REPORT_SIZE (8)
236 0x95, 0x02, // REPORT_COUNT (2)
237 0x81, 0x06, // INPUT (Data,Var,Rel)
238 // ---------------------------- Vertical wheel
239 0x09, 0x38, // USAGE (Wheel)
240 0x15, 0x81, // LOGICAL_MINIMUM (-127)
241 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
242 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
243 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
244 0x75, 0x08, // REPORT_SIZE (8)
245 0x95, 0x01, // REPORT_COUNT (1)
246 0x81, 0x06, // INPUT (Data,Var,Rel)
247 // ---------------------------- Horizontal wheel
248 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
249 0x0a, 0x38, 0x02, // USAGE (AC Pan)
250 0x15, 0x81, // LOGICAL_MINIMUM (-127)
251 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
252 0x75, 0x08, // REPORT_SIZE (8)
253 0x95, 0x01, // REPORT_COUNT (1)
254 0x81, 0x06, // INPUT (Data,Var,Rel)
255 0xc0, // END_COLLECTION
256 0xc0, // END_COLLECTION
259 static const USBDescriptor mouse_hid_report_descriptor = {
260 sizeof mouse_hid_report_desc_data,
261 mouse_hid_report_desc_data
263 #endif /* MOUSE_ENABLE */
265 #ifdef CONSOLE_ENABLE
266 static const uint8_t console_hid_report_desc_data[] = {
267 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
268 0x09, 0x74, // Usage 0x74
269 0xA1, 0x53, // Collection 0x53
270 0x75, 0x08, // report size = 8 bits
271 0x15, 0x00, // logical minimum = 0
272 0x26, 0xFF, 0x00, // logical maximum = 255
273 0x95, CONSOLE_EPSIZE, // report count
275 0x81, 0x02, // Input (array)
276 0xC0 // end collection
279 static const USBDescriptor console_hid_report_descriptor = {
280 sizeof console_hid_report_desc_data,
281 console_hid_report_desc_data
283 #endif /* CONSOLE_ENABLE */
285 #ifdef EXTRAKEY_ENABLE
286 /* audio controls & system controls
287 * http://www.microsoft.com/whdc/archive/w2kbd.mspx */
288 static const uint8_t extra_hid_report_desc_data[] = {
290 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
291 0x09, 0x80, // USAGE (System Control)
292 0xa1, 0x01, // COLLECTION (Application)
293 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
294 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
295 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
296 0x19, 0x01, // USAGE_MINIMUM (0x1)
297 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
298 0x75, 0x10, // REPORT_SIZE (16)
299 0x95, 0x01, // REPORT_COUNT (1)
300 0x81, 0x00, // INPUT (Data,Array,Abs)
301 0xc0, // END_COLLECTION
303 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
304 0x09, 0x01, // USAGE (Consumer Control)
305 0xa1, 0x01, // COLLECTION (Application)
306 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
307 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
308 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
309 0x19, 0x01, // USAGE_MINIMUM (0x1)
310 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
311 0x75, 0x10, // REPORT_SIZE (16)
312 0x95, 0x01, // REPORT_COUNT (1)
313 0x81, 0x00, // INPUT (Data,Array,Abs)
314 0xc0, // END_COLLECTION
317 static const USBDescriptor extra_hid_report_descriptor = {
318 sizeof extra_hid_report_desc_data,
319 extra_hid_report_desc_data
321 #endif /* EXTRAKEY_ENABLE */
325 * Configuration Descriptor tree for a HID device
327 * The HID Specifications version 1.11 require the following order:
328 * - Configuration Descriptor
329 * - Interface Descriptor
331 * - Endpoints Descriptors
333 #define KBD_HID_DESC_NUM 0
334 #define KBD_HID_DESC_OFFSET (9 + (9 + 9 + 7) * KBD_HID_DESC_NUM + 9)
337 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1)
338 # define MOUSE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * MOUSE_HID_DESC_NUM + 9)
339 #else /* MOUSE_ENABLE */
340 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
341 #endif /* MOUSE_ENABLE */
343 #ifdef CONSOLE_ENABLE
344 #define CONSOLE_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1)
345 #define CONSOLE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * CONSOLE_HID_DESC_NUM + 9)
346 #else /* CONSOLE_ENABLE */
347 # define CONSOLE_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0)
348 #endif /* CONSOLE_ENABLE */
350 #ifdef EXTRAKEY_ENABLE
351 # define EXTRA_HID_DESC_NUM (CONSOLE_HID_DESC_NUM + 1)
352 # define EXTRA_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9)
353 #else /* EXTRAKEY_ENABLE */
354 # define EXTRA_HID_DESC_NUM (CONSOLE_HID_DESC_NUM + 0)
355 #endif /* EXTRAKEY_ENABLE */
358 # define NKRO_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1)
359 # define NKRO_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9)
360 #else /* NKRO_ENABLE */
361 # define NKRO_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
362 #endif /* NKRO_ENABLE */
364 #define NUM_INTERFACES (NKRO_HID_DESC_NUM + 1)
365 #define CONFIG1_DESC_SIZE (9 + (9 + 9 + 7) * NUM_INTERFACES)
367 static const uint8_t hid_configuration_descriptor_data[] = {
368 /* Configuration Descriptor (9 bytes) USB spec 9.6.3, page 264-266, Table 9-10 */
369 USB_DESC_CONFIGURATION(CONFIG1_DESC_SIZE, // wTotalLength
370 NUM_INTERFACES, // bNumInterfaces
371 1, // bConfigurationValue
373 0xA0, // bmAttributes (RESERVED|REMOTEWAKEUP)
374 50), // bMaxPower (50mA)
376 /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
377 USB_DESC_INTERFACE(KBD_INTERFACE, // bInterfaceNumber
378 0, // bAlternateSetting
380 0x03, // bInterfaceClass: HID
381 0x01, // bInterfaceSubClass: Boot
382 0x01, // bInterfaceProtocol: Keyboard
385 /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
386 USB_DESC_BYTE(9), // bLength
387 USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
388 USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
389 USB_DESC_BYTE(0), // bCountryCode
390 USB_DESC_BYTE(1), // bNumDescriptors
391 USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
392 USB_DESC_WORD(sizeof(keyboard_hid_report_desc_data)), // wDescriptorLength
394 /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
395 USB_DESC_ENDPOINT(KBD_ENDPOINT | 0x80, // bEndpointAddress
396 0x03, // bmAttributes (Interrupt)
397 KBD_EPSIZE,// wMaxPacketSize
401 /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
402 USB_DESC_INTERFACE(MOUSE_INTERFACE, // bInterfaceNumber
403 0, // bAlternateSetting
405 0x03, // bInterfaceClass (0x03 = HID)
406 // ThinkPad T23 BIOS doesn't work with boot mouse.
407 0x00, // bInterfaceSubClass (0x01 = Boot)
408 0x00, // bInterfaceProtocol (0x02 = Mouse)
410 0x01, // bInterfaceSubClass (0x01 = Boot)
411 0x02, // bInterfaceProtocol (0x02 = Mouse)
415 /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
416 USB_DESC_BYTE(9), // bLength
417 USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
418 USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
419 USB_DESC_BYTE(0), // bCountryCode
420 USB_DESC_BYTE(1), // bNumDescriptors
421 USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
422 USB_DESC_WORD(sizeof(mouse_hid_report_desc_data)), // wDescriptorLength
424 /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
425 USB_DESC_ENDPOINT(MOUSE_ENDPOINT | 0x80, // bEndpointAddress
426 0x03, // bmAttributes (Interrupt)
427 MOUSE_EPSIZE, // wMaxPacketSize
429 #endif /* MOUSE_ENABLE */
431 #ifdef CONSOLE_ENABLE
432 /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
433 USB_DESC_INTERFACE(CONSOLE_INTERFACE, // bInterfaceNumber
434 0, // bAlternateSetting
436 0x03, // bInterfaceClass: HID
437 0x00, // bInterfaceSubClass: None
438 0x00, // bInterfaceProtocol: None
441 /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
442 USB_DESC_BYTE(9), // bLength
443 USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
444 USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
445 USB_DESC_BYTE(0), // bCountryCode
446 USB_DESC_BYTE(1), // bNumDescriptors
447 USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
448 USB_DESC_WORD(sizeof(console_hid_report_desc_data)), // wDescriptorLength
450 /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
451 USB_DESC_ENDPOINT(CONSOLE_ENDPOINT | 0x80, // bEndpointAddress
452 0x03, // bmAttributes (Interrupt)
453 CONSOLE_EPSIZE, // wMaxPacketSize
455 #endif /* CONSOLE_ENABLE */
457 #ifdef EXTRAKEY_ENABLE
458 /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
459 USB_DESC_INTERFACE(EXTRA_INTERFACE, // bInterfaceNumber
460 0, // bAlternateSetting
462 0x03, // bInterfaceClass: HID
463 0x00, // bInterfaceSubClass: None
464 0x00, // bInterfaceProtocol: None
467 /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
468 USB_DESC_BYTE(9), // bLength
469 USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
470 USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
471 USB_DESC_BYTE(0), // bCountryCode
472 USB_DESC_BYTE(1), // bNumDescriptors
473 USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
474 USB_DESC_WORD(sizeof(extra_hid_report_desc_data)), // wDescriptorLength
476 /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
477 USB_DESC_ENDPOINT(EXTRA_ENDPOINT | 0x80, // bEndpointAddress
478 0x03, // bmAttributes (Interrupt)
479 EXTRA_EPSIZE, // wMaxPacketSize
481 #endif /* EXTRAKEY_ENABLE */
484 /* Interface Descriptor (9 bytes) USB spec 9.6.5, page 267-269, Table 9-12 */
485 USB_DESC_INTERFACE(NKRO_INTERFACE, // bInterfaceNumber
486 0, // bAlternateSetting
488 0x03, // bInterfaceClass: HID
489 0x00, // bInterfaceSubClass: None
490 0x00, // bInterfaceProtocol: None
493 /* HID descriptor (9 bytes) HID 1.11 spec, section 6.2.1 */
494 USB_DESC_BYTE(9), // bLength
495 USB_DESC_BYTE(0x21), // bDescriptorType (HID class)
496 USB_DESC_BCD(0x0111), // bcdHID: HID version 1.11
497 USB_DESC_BYTE(0), // bCountryCode
498 USB_DESC_BYTE(1), // bNumDescriptors
499 USB_DESC_BYTE(0x22), // bDescriptorType (report desc)
500 USB_DESC_WORD(sizeof(nkro_hid_report_desc_data)), // wDescriptorLength
502 /* Endpoint Descriptor (7 bytes) USB spec 9.6.6, page 269-271, Table 9-13 */
503 USB_DESC_ENDPOINT(NKRO_ENDPOINT | 0x80, // bEndpointAddress
504 0x03, // bmAttributes (Interrupt)
505 NKRO_EPSIZE, // wMaxPacketSize
507 #endif /* NKRO_ENABLE */
510 /* Configuration Descriptor wrapper */
511 static const USBDescriptor hid_configuration_descriptor = {
512 sizeof hid_configuration_descriptor_data,
513 hid_configuration_descriptor_data
517 #define HID_DESCRIPTOR_SIZE 9
518 static const USBDescriptor keyboard_hid_descriptor = {
520 &hid_configuration_descriptor_data[KBD_HID_DESC_OFFSET]
523 static const USBDescriptor mouse_hid_descriptor = {
525 &hid_configuration_descriptor_data[MOUSE_HID_DESC_OFFSET]
527 #endif /* MOUSE_ENABLE */
528 #ifdef CONSOLE_ENABLE
529 static const USBDescriptor console_hid_descriptor = {
531 &hid_configuration_descriptor_data[CONSOLE_HID_DESC_OFFSET]
533 #endif /* CONSOLE_ENABLE */
534 #ifdef EXTRAKEY_ENABLE
535 static const USBDescriptor extra_hid_descriptor = {
537 &hid_configuration_descriptor_data[EXTRA_HID_DESC_OFFSET]
539 #endif /* EXTRAKEY_ENABLE */
541 static const USBDescriptor nkro_hid_descriptor = {
543 &hid_configuration_descriptor_data[NKRO_HID_DESC_OFFSET]
545 #endif /* NKRO_ENABLE */
548 /* U.S. English language identifier */
549 static const uint8_t usb_string_langid[] = {
550 USB_DESC_BYTE(4), // bLength
551 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
552 USB_DESC_WORD(0x0409) // wLANGID (U.S. English)
556 #define PP_NARG(...) \
557 PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
558 #define PP_NARG_(...) \
559 PP_ARG_N(__VA_ARGS__)
561 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
562 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
563 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
564 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
565 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
566 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
568 #define PP_RSEQ_N() \
570 59,58,57,56,55,54,53,52,51,50, \
571 49,48,47,46,45,44,43,42,41,40, \
572 39,38,37,36,35,34,33,32,31,30, \
573 29,28,27,26,25,24,23,22,21,20, \
574 19,18,17,16,15,14,13,12,11,10, \
577 /* Vendor string = manufacturer */
578 static const uint8_t usb_string_vendor[] = {
579 USB_DESC_BYTE(PP_NARG(USBSTR_MANUFACTURER)+2), // bLength
580 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
584 /* Device Description string = product */
585 static const uint8_t usb_string_description[] = {
586 USB_DESC_BYTE(PP_NARG(USBSTR_PRODUCT)+2), // bLength
587 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
591 /* Serial Number string (will be filled by the function init_usb_serial_string) */
592 static uint8_t usb_string_serial[] = {
593 USB_DESC_BYTE(22), // bLength
594 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), // bDescriptorType
595 '0', 0, 'x', 0, 'D', 0, 'E', 0, 'A', 0, 'D', 0, 'B', 0, 'E', 0, 'E', 0, 'F', 0
598 /* Strings wrappers array */
599 static const USBDescriptor usb_strings[] = {
600 { sizeof usb_string_langid, usb_string_langid }
602 { sizeof usb_string_vendor, usb_string_vendor }
604 { sizeof usb_string_description, usb_string_description }
606 { sizeof usb_string_serial, usb_string_serial }
610 * Handles the GET_DESCRIPTOR callback
612 * Returns the proper descriptor
614 static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) {
618 /* Generic descriptors */
619 case USB_DESCRIPTOR_DEVICE: /* Device Descriptor */
620 return &usb_device_descriptor;
622 case USB_DESCRIPTOR_CONFIGURATION: /* Configuration Descriptor */
623 return &hid_configuration_descriptor;
625 case USB_DESCRIPTOR_STRING: /* Strings */
627 return &usb_strings[dindex];
630 /* HID specific descriptors */
631 case USB_DESCRIPTOR_HID: /* HID Descriptors */
632 switch(lang) { /* yea, poor label, it's actually wIndex from the setup packet */
634 return &keyboard_hid_descriptor;
637 case MOUSE_INTERFACE:
638 return &mouse_hid_descriptor;
639 #endif /* MOUSE_ENABLE */
640 #ifdef CONSOLE_ENABLE
641 case CONSOLE_INTERFACE:
642 return &console_hid_descriptor;
643 #endif /* CONSOLE_ENABLE */
644 #ifdef EXTRAKEY_ENABLE
645 case EXTRA_INTERFACE:
646 return &extra_hid_descriptor;
647 #endif /* EXTRAKEY_ENABLE */
650 return &nkro_hid_descriptor;
651 #endif /* NKRO_ENABLE */
654 case USB_DESCRIPTOR_HID_REPORT: /* HID Report Descriptor */
657 return &keyboard_hid_report_descriptor;
660 case MOUSE_INTERFACE:
661 return &mouse_hid_report_descriptor;
662 #endif /* MOUSE_ENABLE */
663 #ifdef CONSOLE_ENABLE
664 case CONSOLE_INTERFACE:
665 return &console_hid_report_descriptor;
666 #endif /* CONSOLE_ENABLE */
667 #ifdef EXTRAKEY_ENABLE
668 case EXTRA_INTERFACE:
669 return &extra_hid_report_descriptor;
670 #endif /* EXTRAKEY_ENABLE */
673 return &nkro_hid_report_descriptor;
674 #endif /* NKRO_ENABLE */
680 /* keyboard endpoint state structure */
681 static USBInEndpointState kbd_ep_state;
682 /* keyboard endpoint initialization structure (IN) */
683 static const USBEndpointConfig kbd_ep_config = {
684 USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
685 NULL, /* SETUP packet notification callback */
686 kbd_in_cb, /* IN notification callback */
687 NULL, /* OUT notification callback */
688 KBD_EPSIZE, /* IN maximum packet size */
689 0, /* OUT maximum packet size */
690 &kbd_ep_state, /* IN Endpoint state */
691 NULL, /* OUT endpoint state */
692 2, /* IN multiplier */
693 NULL /* SETUP buffer (not a SETUP endpoint) */
697 /* mouse endpoint state structure */
698 static USBInEndpointState mouse_ep_state;
700 /* mouse endpoint initialization structure (IN) */
701 static const USBEndpointConfig mouse_ep_config = {
702 USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
703 NULL, /* SETUP packet notification callback */
704 mouse_in_cb, /* IN notification callback */
705 NULL, /* OUT notification callback */
706 MOUSE_EPSIZE, /* IN maximum packet size */
707 0, /* OUT maximum packet size */
708 &mouse_ep_state, /* IN Endpoint state */
709 NULL, /* OUT endpoint state */
710 2, /* IN multiplier */
711 NULL /* SETUP buffer (not a SETUP endpoint) */
713 #endif /* MOUSE_ENABLE */
715 #ifdef CONSOLE_ENABLE
716 /* console endpoint state structure */
717 static USBInEndpointState console_ep_state;
719 /* console endpoint initialization structure (IN) */
720 static const USBEndpointConfig console_ep_config = {
721 USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
722 NULL, /* SETUP packet notification callback */
723 console_in_cb, /* IN notification callback */
724 NULL, /* OUT notification callback */
725 CONSOLE_EPSIZE, /* IN maximum packet size */
726 0, /* OUT maximum packet size */
727 &console_ep_state, /* IN Endpoint state */
728 NULL, /* OUT endpoint state */
729 2, /* IN multiplier */
730 NULL /* SETUP buffer (not a SETUP endpoint) */
732 #endif /* CONSOLE_ENABLE */
734 #ifdef EXTRAKEY_ENABLE
735 /* extrakey endpoint state structure */
736 static USBInEndpointState extra_ep_state;
738 /* extrakey endpoint initialization structure (IN) */
739 static const USBEndpointConfig extra_ep_config = {
740 USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
741 NULL, /* SETUP packet notification callback */
742 extra_in_cb, /* IN notification callback */
743 NULL, /* OUT notification callback */
744 EXTRA_EPSIZE, /* IN maximum packet size */
745 0, /* OUT maximum packet size */
746 &extra_ep_state, /* IN Endpoint state */
747 NULL, /* OUT endpoint state */
748 2, /* IN multiplier */
749 NULL /* SETUP buffer (not a SETUP endpoint) */
751 #endif /* EXTRAKEY_ENABLE */
754 /* nkro endpoint state structure */
755 static USBInEndpointState nkro_ep_state;
757 /* nkro endpoint initialization structure (IN) */
758 static const USBEndpointConfig nkro_ep_config = {
759 USB_EP_MODE_TYPE_INTR, /* Interrupt EP */
760 NULL, /* SETUP packet notification callback */
761 nkro_in_cb, /* IN notification callback */
762 NULL, /* OUT notification callback */
763 NKRO_EPSIZE, /* IN maximum packet size */
764 0, /* OUT maximum packet size */
765 &nkro_ep_state, /* IN Endpoint state */
766 NULL, /* OUT endpoint state */
767 2, /* IN multiplier */
768 NULL /* SETUP buffer (not a SETUP endpoint) */
770 #endif /* NKRO_ENABLE */
772 /* ---------------------------------------------------------
773 * USB driver functions
774 * ---------------------------------------------------------
777 /* Handles the USB driver global events
778 * TODO: maybe disable some things when connection is lost? */
779 static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
781 case USB_EVENT_ADDRESS:
784 case USB_EVENT_CONFIGURED:
785 osalSysLockFromISR();
786 /* Enable the endpoints specified into the configuration. */
787 usbInitEndpointI(usbp, KBD_ENDPOINT, &kbd_ep_config);
789 usbInitEndpointI(usbp, MOUSE_ENDPOINT, &mouse_ep_config);
790 #endif /* MOUSE_ENABLE */
791 #ifdef CONSOLE_ENABLE
792 usbInitEndpointI(usbp, CONSOLE_ENDPOINT, &console_ep_config);
793 /* don't need to start the flush timer, it starts from console_in_cb automatically */
794 #endif /* CONSOLE_ENABLE */
795 #ifdef EXTRAKEY_ENABLE
796 usbInitEndpointI(usbp, EXTRA_ENDPOINT, &extra_ep_config);
797 #endif /* EXTRAKEY_ENABLE */
799 usbInitEndpointI(usbp, NKRO_ENDPOINT, &nkro_ep_config);
800 #endif /* NKRO_ENABLE */
801 osalSysUnlockFromISR();
803 case USB_EVENT_SUSPEND:
804 //TODO: from ISR! print("[S]");
805 #ifdef SLEEP_LED_ENABLE
807 #endif /* SLEEP_LED_ENABLE */
809 case USB_EVENT_UNCONFIGURED:
811 case USB_EVENT_RESET:
814 case USB_EVENT_WAKEUP:
815 //TODO: from ISR! print("[W]");
816 suspend_wakeup_init();
817 #ifdef SLEEP_LED_ENABLE
819 // NOTE: converters may not accept this
820 led_set(host_keyboard_leds());
821 #endif /* SLEEP_LED_ENABLE */
824 case USB_EVENT_STALLED:
829 /* Function used locally in os/hal/src/usb.c for getting descriptors
830 * need it here for HID descriptor */
831 static uint16_t get_hword(uint8_t *p) {
835 hw |= (uint16_t)*p << 8U;
840 * Appendix G: HID Request Support Requirements
842 * The following table enumerates the requests that need to be supported by various types of HID class devices.
843 * Device type GetReport SetReport GetIdle SetIdle GetProtocol SetProtocol
844 * ------------------------------------------------------------------------------------------
845 * Boot Mouse Required Optional Optional Optional Required Required
846 * Non-Boot Mouse Required Optional Optional Optional Optional Optional
847 * Boot Keyboard Required Optional Required Required Required Required
848 * Non-Boot Keybrd Required Optional Required Required Optional Optional
849 * Other Device Required Optional Optional Optional Optional Optional
852 /* Callback for SETUP request on the endpoint 0 (control) */
853 static bool usb_request_hook_cb(USBDriver *usbp) {
854 const USBDescriptor *dp;
856 /* usbp->setup fields:
857 * 0: bmRequestType (bitmask)
859 * 2,3: (LSB,MSB) wValue
860 * 4,5: (LSB,MSB) wIndex
861 * 6,7: (LSB,MSB) wLength (number of bytes to transfer if there is a data phase) */
863 /* Handle HID class specific requests */
864 if(((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) &&
865 ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) {
866 switch(usbp->setup[0] & USB_RTYPE_DIR_MASK) {
867 case USB_RTYPE_DIR_DEV2HOST:
868 switch(usbp->setup[1]) { /* bRequest */
870 switch(usbp->setup[4]) { /* LSB(wIndex) (check MSB==0?) */
874 #endif /* NKRO_ENABLE */
875 usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL);
880 case MOUSE_INTERFACE:
881 usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL);
884 #endif /* MOUSE_ENABLE */
886 #ifdef CONSOLE_ENABLE
887 case CONSOLE_INTERFACE:
888 usbSetupTransfer(usbp, console_queue_buffer, CONSOLE_EPSIZE, NULL);
891 #endif /* CONSOLE_ENABLE */
893 #ifdef EXTRAKEY_ENABLE
894 case EXTRA_INTERFACE:
895 if(usbp->setup[3] == 1) { /* MSB(wValue) [Report Type] == 1 [Input Report] */
896 switch(usbp->setup[2]) { /* LSB(wValue) [Report ID] */
897 case REPORT_ID_SYSTEM:
898 extra_report_blank[0] = REPORT_ID_SYSTEM;
899 usbSetupTransfer(usbp, (uint8_t *)extra_report_blank, sizeof(extra_report_blank), NULL);
902 case REPORT_ID_CONSUMER:
903 extra_report_blank[0] = REPORT_ID_CONSUMER;
904 usbSetupTransfer(usbp, (uint8_t *)extra_report_blank, sizeof(extra_report_blank), NULL);
914 #endif /* EXTRAKEY_ENABLE */
917 usbSetupTransfer(usbp, NULL, 0, NULL);
923 case HID_GET_PROTOCOL:
924 if((usbp->setup[4] == KBD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
925 usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL);
931 usbSetupTransfer(usbp, &keyboard_idle, 1, NULL);
937 case USB_RTYPE_DIR_HOST2DEV:
938 switch(usbp->setup[1]) { /* bRequest */
940 switch(usbp->setup[4]) { /* LSB(wIndex) (check MSB==0 and wLength==1?) */
944 #endif /* NKRO_ENABLE */
945 /* keyboard_led_stats = <read byte from next OUT report>
946 * keyboard_led_stats needs be word (or dword), otherwise we get an exception on F0 */
947 usbSetupTransfer(usbp, (uint8_t *)&keyboard_led_stats, 1, NULL);
953 case HID_SET_PROTOCOL:
954 if((usbp->setup[4] == KBD_INTERFACE) && (usbp->setup[5] == 0)) { /* wIndex */
955 keyboard_protocol = ((usbp->setup[2]) != 0x00); /* LSB(wValue) */
957 keymap_config.nkro = !!keyboard_protocol;
958 if(!keymap_config.nkro && keyboard_idle) {
959 #else /* NKRO_ENABLE */
961 #endif /* NKRO_ENABLE */
962 /* arm the idle timer if boot protocol & idle */
963 osalSysLockFromISR();
964 chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
965 osalSysUnlockFromISR();
968 usbSetupTransfer(usbp, NULL, 0, NULL);
973 keyboard_idle = usbp->setup[3]; /* MSB(wValue) */
976 if(!keymap_config.nkro && keyboard_idle) {
977 #else /* NKRO_ENABLE */
979 #endif /* NKRO_ENABLE */
980 osalSysLockFromISR();
981 chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
982 osalSysUnlockFromISR();
984 usbSetupTransfer(usbp, NULL, 0, NULL);
992 /* Handle the Get_Descriptor Request for HID class (not handled by the default hook) */
993 if((usbp->setup[0] == 0x81) && (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)) {
994 dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], get_hword(&usbp->setup[4]));
997 usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
1004 /* Start-of-frame callback */
1005 static void usb_sof_cb(USBDriver *usbp) {
1010 /* USB driver configuration */
1011 static const USBConfig usbcfg = {
1012 usb_event_cb, /* USB events callback */
1013 usb_get_descriptor_cb, /* Device GET_DESCRIPTOR request callback */
1014 usb_request_hook_cb, /* Requests hook callback */
1015 usb_sof_cb /* Start Of Frame callback */
1019 * Initialize the USB driver
1021 void init_usb_driver(USBDriver *usbp) {
1023 * Activates the USB driver and then the USB bus pull-up on D+.
1024 * Note, a delay is inserted in order to not have to disconnect the cable
1027 usbDisconnectBus(usbp);
1029 usbStart(usbp, &usbcfg);
1030 usbConnectBus(usbp);
1032 chVTObjectInit(&keyboard_idle_timer);
1033 #ifdef CONSOLE_ENABLE
1034 obqObjectInit(&console_buf_queue, false, console_queue_buffer, CONSOLE_EPSIZE, CONSOLE_QUEUE_CAPACITY, console_queue_onotify, (void*)usbp);
1035 chVTObjectInit(&console_flush_timer);
1040 * Send remote wakeup packet
1041 * Note: should not be called from ISR
1043 void send_remote_wakeup(USBDriver *usbp) {
1045 #if defined(K20x) || defined(KL2x)
1046 #if KINETIS_USB_USE_USB0
1047 USB0->CTL |= USBx_CTL_RESUME;
1049 USB0->CTL &= ~USBx_CTL_RESUME;
1050 #endif /* KINETIS_USB_USE_USB0 */
1051 #elif defined(STM32F0XX) || defined(STM32F1XX) || defined(STM32F3XX) /* End K20x || KL2x */
1052 STM32_USB->CNTR |= CNTR_RESUME;
1054 STM32_USB->CNTR &= ~CNTR_RESUME;
1055 #else /* End STM32F0XX || STM32F1XX || STM32F3XX */
1056 #warning Sending remote wakeup packet not implemented for your platform.
1060 /* ---------------------------------------------------------
1061 * Keyboard functions
1062 * ---------------------------------------------------------
1065 /* keyboard IN callback hander (a kbd report has made it IN) */
1066 void kbd_in_cb(USBDriver *usbp, usbep_t ep) {
1073 /* nkro IN callback hander (a nkro report has made it IN) */
1074 void nkro_in_cb(USBDriver *usbp, usbep_t ep) {
1079 #endif /* NKRO_ENABLE */
1081 /* start-of-frame handler
1082 * TODO: i guess it would be better to re-implement using timers,
1083 * so that this is not going to have to be checked every 1ms */
1084 void kbd_sof_cb(USBDriver *usbp) {
1088 /* Idle requests timer code
1089 * callback (called from ISR, unlocked state) */
1090 static void keyboard_idle_timer_cb(void *arg) {
1091 USBDriver *usbp = (USBDriver *)arg;
1093 osalSysLockFromISR();
1095 /* check that the states of things are as they're supposed to */
1096 if(usbGetDriverStateI(usbp) != USB_ACTIVE) {
1097 /* do not rearm the timer, should be enabled on IDLE request */
1098 osalSysUnlockFromISR();
1103 if(!keymap_config.nkro && keyboard_idle) {
1104 #else /* NKRO_ENABLE */
1106 #endif /* NKRO_ENABLE */
1107 /* TODO: are we sure we want the KBD_ENDPOINT? */
1108 if(!usbGetTransmitStatusI(usbp, KBD_ENDPOINT)) {
1109 usbStartTransmitI(usbp, KBD_ENDPOINT, (uint8_t *)&keyboard_report_sent, KBD_EPSIZE);
1111 /* rearm the timer */
1112 chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
1115 /* do not rearm the timer if the condition above fails
1116 * it should be enabled again on either IDLE or SET_PROTOCOL requests */
1117 osalSysUnlockFromISR();
1121 uint8_t keyboard_leds(void) {
1122 return (uint8_t)(keyboard_led_stats & 0xFF);
1125 /* prepare and start sending a report IN
1126 * not callable from ISR or locked state */
1127 void send_keyboard(report_keyboard_t *report) {
1129 if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
1136 if(keymap_config.nkro) { /* NKRO protocol */
1137 /* need to wait until the previous packet has made it through */
1138 /* can rewrite this using the synchronous API, then would wait
1139 * until *after* the packet has been transmitted. I think
1140 * this is more efficient */
1141 /* busy wait, should be short and not very common */
1143 if(usbGetTransmitStatusI(&USB_DRIVER, NKRO_ENDPOINT)) {
1144 /* Need to either suspend, or loop and call unlock/lock during
1145 * every iteration - otherwise the system will remain locked,
1146 * no interrupts served, so USB not going through as well.
1147 * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
1148 osalThreadSuspendS(&(&USB_DRIVER)->epc[NKRO_ENDPOINT]->in_state->thread);
1150 usbStartTransmitI(&USB_DRIVER, NKRO_ENDPOINT, (uint8_t *)report, sizeof(report_keyboard_t));
1153 #endif /* NKRO_ENABLE */
1154 { /* boot protocol */
1155 /* need to wait until the previous packet has made it through */
1156 /* busy wait, should be short and not very common */
1158 if(usbGetTransmitStatusI(&USB_DRIVER, KBD_ENDPOINT)) {
1159 /* Need to either suspend, or loop and call unlock/lock during
1160 * every iteration - otherwise the system will remain locked,
1161 * no interrupts served, so USB not going through as well.
1162 * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */
1163 osalThreadSuspendS(&(&USB_DRIVER)->epc[KBD_ENDPOINT]->in_state->thread);
1165 usbStartTransmitI(&USB_DRIVER, KBD_ENDPOINT, (uint8_t *)report, KBD_EPSIZE);
1168 keyboard_report_sent = *report;
1171 /* ---------------------------------------------------------
1173 * ---------------------------------------------------------
1178 /* mouse IN callback hander (a mouse report has made it IN) */
1179 void mouse_in_cb(USBDriver *usbp, usbep_t ep) {
1184 void send_mouse(report_mouse_t *report) {
1186 if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
1192 /* TODO: LUFA manually waits for the endpoint to become ready
1193 * for about 10ms for mouse, kbd, system; 1ms for nkro
1194 * is this really needed?
1198 usbStartTransmitI(&USB_DRIVER, MOUSE_ENDPOINT, (uint8_t *)report, sizeof(report_mouse_t));
1202 #else /* MOUSE_ENABLE */
1203 void send_mouse(report_mouse_t *report) {
1206 #endif /* MOUSE_ENABLE */
1208 /* ---------------------------------------------------------
1209 * Extrakey functions
1210 * ---------------------------------------------------------
1213 #ifdef EXTRAKEY_ENABLE
1215 /* extrakey IN callback hander */
1216 void extra_in_cb(USBDriver *usbp, usbep_t ep) {
1222 static void send_extra_report(uint8_t report_id, uint16_t data) {
1224 if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
1229 report_extra_t report = {
1230 .report_id = report_id,
1234 usbStartTransmitI(&USB_DRIVER, EXTRA_ENDPOINT, (uint8_t *)&report, sizeof(report_extra_t));
1238 void send_system(uint16_t data) {
1239 send_extra_report(REPORT_ID_SYSTEM, data);
1242 void send_consumer(uint16_t data) {
1243 send_extra_report(REPORT_ID_CONSUMER, data);
1246 #else /* EXTRAKEY_ENABLE */
1247 void send_system(uint16_t data) {
1250 void send_consumer(uint16_t data) {
1253 #endif /* EXTRAKEY_ENABLE */
1255 /* ---------------------------------------------------------
1257 * ---------------------------------------------------------
1260 #ifdef CONSOLE_ENABLE
1262 /* console IN callback hander */
1263 void console_in_cb(USBDriver *usbp, usbep_t ep) {
1264 (void)ep; /* should have ep == CONSOLE_ENDPOINT, so use that to save time/space */
1268 osalSysLockFromISR();
1270 /* rearm the timer */
1271 chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
1273 /* Freeing the buffer just transmitted, if it was not a zero size packet.*/
1274 if (usbp->epc[CONSOLE_ENDPOINT]->in_state->txsize > 0U) {
1275 obqReleaseEmptyBufferI(&console_buf_queue);
1278 /* Checking if there is a buffer ready for transmission.*/
1279 buf = obqGetFullBufferI(&console_buf_queue, &n);
1282 /* The endpoint cannot be busy, we are in the context of the callback,
1283 so it is safe to transmit without a check.*/
1284 /* Should have n == CONSOLE_EPSIZE; check it? */
1285 usbStartTransmitI(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
1287 /* Nothing to transmit.*/
1290 osalSysUnlockFromISR();
1293 /* Callback when data is inserted into the output queue
1294 * Called from a locked state */
1295 void console_queue_onotify(io_buffers_queue_t *bqp) {
1297 USBDriver *usbp = bqGetLinkX(bqp);
1299 if(usbGetDriverStateI(usbp) != USB_ACTIVE)
1302 /* Checking if there is already a transaction ongoing on the endpoint.*/
1303 if (!usbGetTransmitStatusI(usbp, CONSOLE_ENDPOINT)) {
1304 /* Trying to get a full buffer.*/
1305 uint8_t *buf = obqGetFullBufferI(&console_buf_queue, &n);
1307 /* Buffer found, starting a new transaction.*/
1308 /* Should have n == CONSOLE_EPSIZE; check this? */
1309 usbStartTransmitI(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
1315 * callback (called from ISR, unlocked state) */
1316 static void console_flush_cb(void *arg) {
1317 USBDriver *usbp = (USBDriver *)arg;
1318 osalSysLockFromISR();
1320 /* check that the states of things are as they're supposed to */
1321 if(usbGetDriverStateI(usbp) != USB_ACTIVE) {
1322 /* rearm the timer */
1323 chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
1324 osalSysUnlockFromISR();
1328 /* If there is already a transaction ongoing then another one cannot be
1330 if (usbGetTransmitStatusI(usbp, CONSOLE_ENDPOINT)) {
1331 /* rearm the timer */
1332 chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
1333 osalSysUnlockFromISR();
1337 /* Checking if there only a buffer partially filled, if so then it is
1338 enforced in the queue and transmitted.*/
1339 if(obqTryFlushI(&console_buf_queue)) {
1341 uint8_t *buf = obqGetFullBufferI(&console_buf_queue, &n);
1343 osalDbgAssert(buf != NULL, "queue is empty");
1345 /* zero the rest of the buffer (buf should point to allocated space) */
1346 for(i=n; i<CONSOLE_EPSIZE; i++)
1348 usbStartTransmitI(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
1351 /* rearm the timer */
1352 chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
1353 osalSysUnlockFromISR();
1357 int8_t sendchar(uint8_t c) {
1359 if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
1364 /* Timeout after 100us if the queue is full.
1365 * Increase this timeout if too much stuff is getting
1366 * dropped (i.e. the buffer is getting full too fast
1367 * for USB/HIDRAW to dequeue). Another possibility
1368 * for fixing this kind of thing is to increase
1369 * CONSOLE_QUEUE_CAPACITY. */
1370 return(obqPutTimeout(&console_buf_queue, c, US2ST(100)));
1373 #else /* CONSOLE_ENABLE */
1374 int8_t sendchar(uint8_t c) {
1378 #endif /* CONSOLE_ENABLE */
1380 void sendchar_pf(void *p, char c) {
1382 sendchar((uint8_t)c);