1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372 |
- #include "ch.h"
- #include "hal.h"
- #include "usb_main.h"
- #include "host.h"
- #include "debug.h"
- #include "suspend.h"
- #ifdef SLEEP_LED_ENABLE
- #include "sleep_led.h"
- #include "led.h"
- #endif
- uint8_t keyboard_idle __attribute__((aligned(2))) = 0;
- uint8_t keyboard_protocol __attribute__((aligned(2))) = 1;
- uint16_t keyboard_led_stats __attribute__((aligned(2))) = 0;
- volatile uint16_t keyboard_idle_count = 0;
- static virtual_timer_t keyboard_idle_timer;
- static void keyboard_idle_timer_cb(void *arg);
- #ifdef NKRO_ENABLE
- extern bool keyboard_nkro;
- #endif
- report_keyboard_t keyboard_report_sent = {{0}};
- #ifdef MOUSE_ENABLE
- report_mouse_t mouse_report_blank = {0};
- #endif
- #ifdef EXTRAKEY_ENABLE
- uint8_t extra_report_blank[3] = {0};
- #endif
- #ifdef CONSOLE_ENABLE
- output_buffers_queue_t console_buf_queue;
- static uint8_t console_queue_buffer[BQ_BUFFER_SIZE(CONSOLE_QUEUE_CAPACITY, CONSOLE_EPSIZE)];
- static virtual_timer_t console_flush_timer;
- void console_queue_onotify(io_buffers_queue_t *bqp);
- static void console_flush_cb(void *arg);
- #endif
- #define USB_DESCRIPTOR_HID 0x21
- #define USB_DESCRIPTOR_HID_REPORT 0x22
- #define HID_GET_REPORT 0x01
- #define HID_GET_IDLE 0x02
- #define HID_GET_PROTOCOL 0x03
- #define HID_SET_REPORT 0x09
- #define HID_SET_IDLE 0x0A
- #define HID_SET_PROTOCOL 0x0B
- static const uint8_t usb_device_descriptor_data[] = {
- USB_DESC_DEVICE(0x0200,
- 0,
- 0,
- 0,
- 64,
- VENDOR_ID,
- PRODUCT_ID,
- DEVICE_VER,
- 1,
- 2,
- 3,
- 1)
- };
- static const USBDescriptor usb_device_descriptor = {
- sizeof usb_device_descriptor_data,
- usb_device_descriptor_data
- };
- static const uint8_t keyboard_hid_report_desc_data[] = {
- 0x05, 0x01,
- 0x09, 0x06,
- 0xA1, 0x01,
- 0x75, 0x01,
- 0x95, 0x08,
- 0x05, 0x07,
- 0x19, 0xE0,
- 0x29, 0xE7,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x81, 0x02,
- 0x95, 0x01,
- 0x75, 0x08,
- 0x81, 0x03,
- 0x95, 0x05,
- 0x75, 0x01,
- 0x05, 0x08,
- 0x19, 0x01,
- 0x29, 0x05,
- 0x91, 0x02,
- 0x95, 0x01,
- 0x75, 0x03,
- 0x91, 0x03,
- 0x95, KBD_REPORT_KEYS,
- 0x75, 0x08,
- 0x15, 0x00,
- 0x25, 0xFF,
- 0x05, 0x07,
- 0x19, 0x00,
- 0x29, 0xFF,
- 0x81, 0x00,
- 0xc0
- };
- static const USBDescriptor keyboard_hid_report_descriptor = {
- sizeof keyboard_hid_report_desc_data,
- keyboard_hid_report_desc_data
- };
- #ifdef NKRO_ENABLE
- static const uint8_t nkro_hid_report_desc_data[] = {
- 0x05, 0x01,
- 0x09, 0x06,
- 0xA1, 0x01,
-
- 0x75, 0x01,
- 0x95, 0x08,
- 0x05, 0x07,
- 0x19, 0xE0,
- 0x29, 0xE7,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x81, 0x02,
-
- 0x95, 0x05,
- 0x75, 0x01,
- 0x05, 0x08,
- 0x19, 0x01,
- 0x29, 0x05,
- 0x91, 0x02,
- 0x95, 0x01,
- 0x75, 0x03,
- 0x91, 0x03,
-
- 0x95, NKRO_REPORT_KEYS * 8,
- 0x75, 0x01,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x05, 0x07,
- 0x19, 0x00,
- 0x29, NKRO_REPORT_KEYS * 8 - 1,
- 0x81, 0x02,
- 0xc0
- };
- static const USBDescriptor nkro_hid_report_descriptor = {
- sizeof nkro_hid_report_desc_data,
- nkro_hid_report_desc_data
- };
- #endif
- #ifdef MOUSE_ENABLE
- static const uint8_t mouse_hid_report_desc_data[] = {
-
- 0x05, 0x01,
- 0x09, 0x02,
- 0xa1, 0x01,
-
- 0x09, 0x01,
- 0xa1, 0x00,
-
- 0x05, 0x09,
- 0x19, 0x01,
- 0x29, 0x05,
- 0x15, 0x00,
- 0x25, 0x01,
- 0x75, 0x01,
- 0x95, 0x05,
- 0x81, 0x02,
- 0x75, 0x03,
- 0x95, 0x01,
- 0x81, 0x03,
-
- 0x05, 0x01,
- 0x09, 0x30,
- 0x09, 0x31,
- 0x15, 0x81,
- 0x25, 0x7f,
- 0x75, 0x08,
- 0x95, 0x02,
- 0x81, 0x06,
-
- 0x09, 0x38,
- 0x15, 0x81,
- 0x25, 0x7f,
- 0x35, 0x00,
- 0x45, 0x00,
- 0x75, 0x08,
- 0x95, 0x01,
- 0x81, 0x06,
-
- 0x05, 0x0c,
- 0x0a, 0x38, 0x02,
- 0x15, 0x81,
- 0x25, 0x7f,
- 0x75, 0x08,
- 0x95, 0x01,
- 0x81, 0x06,
- 0xc0,
- 0xc0,
- };
- static const USBDescriptor mouse_hid_report_descriptor = {
- sizeof mouse_hid_report_desc_data,
- mouse_hid_report_desc_data
- };
- #endif
- #ifdef CONSOLE_ENABLE
- static const uint8_t console_hid_report_desc_data[] = {
- 0x06, 0x31, 0xFF,
- 0x09, 0x74,
- 0xA1, 0x53,
- 0x75, 0x08,
- 0x15, 0x00,
- 0x26, 0xFF, 0x00,
- 0x95, CONSOLE_EPSIZE,
- 0x09, 0x75,
- 0x81, 0x02,
- 0xC0
- };
- static const USBDescriptor console_hid_report_descriptor = {
- sizeof console_hid_report_desc_data,
- console_hid_report_desc_data
- };
- #endif
- #ifdef EXTRAKEY_ENABLE
- static const uint8_t extra_hid_report_desc_data[] = {
-
- 0x05, 0x01,
- 0x09, 0x80,
- 0xa1, 0x01,
- 0x85, REPORT_ID_SYSTEM,
- 0x15, 0x01,
- 0x25, 0xb7,
- 0x19, 0x01,
- 0x29, 0xb7,
- 0x75, 0x10,
- 0x95, 0x01,
- 0x81, 0x00,
- 0xc0,
-
- 0x05, 0x0c,
- 0x09, 0x01,
- 0xa1, 0x01,
- 0x85, REPORT_ID_CONSUMER,
- 0x15, 0x01,
- 0x26, 0x9c, 0x02,
- 0x19, 0x01,
- 0x2a, 0x9c, 0x02,
- 0x75, 0x10,
- 0x95, 0x01,
- 0x81, 0x00,
- 0xc0,
- };
- static const USBDescriptor extra_hid_report_descriptor = {
- sizeof extra_hid_report_desc_data,
- extra_hid_report_desc_data
- };
- #endif
- #define KBD_HID_DESC_NUM 0
- #define KBD_HID_DESC_OFFSET (9 + (9 + 9 + 7) * KBD_HID_DESC_NUM + 9)
- #ifdef MOUSE_ENABLE
- # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 1)
- # define MOUSE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * MOUSE_HID_DESC_NUM + 9)
- #else
- # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
- #endif
- #ifdef CONSOLE_ENABLE
- #define CONSOLE_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 1)
- #define CONSOLE_HID_DESC_OFFSET (9 + (9 + 9 + 7) * CONSOLE_HID_DESC_NUM + 9)
- #else
- # define CONSOLE_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0)
- #endif
- #ifdef EXTRAKEY_ENABLE
- # define EXTRA_HID_DESC_NUM (CONSOLE_HID_DESC_NUM + 1)
- # define EXTRA_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9)
- #else
- # define EXTRA_HID_DESC_NUM (CONSOLE_HID_DESC_NUM + 0)
- #endif
- #ifdef NKRO_ENABLE
- # define NKRO_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 1)
- # define NKRO_HID_DESC_OFFSET (9 + (9 + 9 + 7) * EXTRA_HID_DESC_NUM + 9)
- #else
- # define NKRO_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
- #endif
- #define NUM_INTERFACES (NKRO_HID_DESC_NUM + 1)
- #define CONFIG1_DESC_SIZE (9 + (9 + 9 + 7) * NUM_INTERFACES)
- static const uint8_t hid_configuration_descriptor_data[] = {
-
- USB_DESC_CONFIGURATION(CONFIG1_DESC_SIZE,
- NUM_INTERFACES,
- 1,
- 0,
- 0xA0,
- 50),
-
- USB_DESC_INTERFACE(KBD_INTERFACE,
- 0,
- 1,
- 0x03,
- 0x01,
- 0x01,
- 0),
-
- USB_DESC_BYTE(9),
- USB_DESC_BYTE(0x21),
- USB_DESC_BCD(0x0111),
- USB_DESC_BYTE(0),
- USB_DESC_BYTE(1),
- USB_DESC_BYTE(0x22),
- USB_DESC_WORD(sizeof(keyboard_hid_report_desc_data)),
-
- USB_DESC_ENDPOINT(KBD_ENDPOINT | 0x80,
- 0x03,
- KBD_EPSIZE,
- 10),
- #ifdef MOUSE_ENABLE
-
- USB_DESC_INTERFACE(MOUSE_INTERFACE,
- 0,
- 1,
- 0x03,
-
- 0x00,
- 0x00,
-
- 0),
-
- USB_DESC_BYTE(9),
- USB_DESC_BYTE(0x21),
- USB_DESC_BCD(0x0111),
- USB_DESC_BYTE(0),
- USB_DESC_BYTE(1),
- USB_DESC_BYTE(0x22),
- USB_DESC_WORD(sizeof(mouse_hid_report_desc_data)),
-
- USB_DESC_ENDPOINT(MOUSE_ENDPOINT | 0x80,
- 0x03,
- MOUSE_EPSIZE,
- 1),
- #endif
- #ifdef CONSOLE_ENABLE
-
- USB_DESC_INTERFACE(CONSOLE_INTERFACE,
- 0,
- 1,
- 0x03,
- 0x00,
- 0x00,
- 0),
-
- USB_DESC_BYTE(9),
- USB_DESC_BYTE(0x21),
- USB_DESC_BCD(0x0111),
- USB_DESC_BYTE(0),
- USB_DESC_BYTE(1),
- USB_DESC_BYTE(0x22),
- USB_DESC_WORD(sizeof(console_hid_report_desc_data)),
-
- USB_DESC_ENDPOINT(CONSOLE_ENDPOINT | 0x80,
- 0x03,
- CONSOLE_EPSIZE,
- 1),
- #endif
- #ifdef EXTRAKEY_ENABLE
-
- USB_DESC_INTERFACE(EXTRA_INTERFACE,
- 0,
- 1,
- 0x03,
- 0x00,
- 0x00,
- 0),
-
- USB_DESC_BYTE(9),
- USB_DESC_BYTE(0x21),
- USB_DESC_BCD(0x0111),
- USB_DESC_BYTE(0),
- USB_DESC_BYTE(1),
- USB_DESC_BYTE(0x22),
- USB_DESC_WORD(sizeof(extra_hid_report_desc_data)),
-
- USB_DESC_ENDPOINT(EXTRA_ENDPOINT | 0x80,
- 0x03,
- EXTRA_EPSIZE,
- 10),
- #endif
- #ifdef NKRO_ENABLE
-
- USB_DESC_INTERFACE(NKRO_INTERFACE,
- 0,
- 1,
- 0x03,
- 0x00,
- 0x00,
- 0),
-
- USB_DESC_BYTE(9),
- USB_DESC_BYTE(0x21),
- USB_DESC_BCD(0x0111),
- USB_DESC_BYTE(0),
- USB_DESC_BYTE(1),
- USB_DESC_BYTE(0x22),
- USB_DESC_WORD(sizeof(nkro_hid_report_desc_data)),
-
- USB_DESC_ENDPOINT(NKRO_ENDPOINT | 0x80,
- 0x03,
- NKRO_EPSIZE,
- 1),
- #endif
- };
- static const USBDescriptor hid_configuration_descriptor = {
- sizeof hid_configuration_descriptor_data,
- hid_configuration_descriptor_data
- };
- #define HID_DESCRIPTOR_SIZE 9
- static const USBDescriptor keyboard_hid_descriptor = {
- HID_DESCRIPTOR_SIZE,
- &hid_configuration_descriptor_data[KBD_HID_DESC_OFFSET]
- };
- #ifdef MOUSE_ENABLE
- static const USBDescriptor mouse_hid_descriptor = {
- HID_DESCRIPTOR_SIZE,
- &hid_configuration_descriptor_data[MOUSE_HID_DESC_OFFSET]
- };
- #endif
- #ifdef CONSOLE_ENABLE
- static const USBDescriptor console_hid_descriptor = {
- HID_DESCRIPTOR_SIZE,
- &hid_configuration_descriptor_data[CONSOLE_HID_DESC_OFFSET]
- };
- #endif
- #ifdef EXTRAKEY_ENABLE
- static const USBDescriptor extra_hid_descriptor = {
- HID_DESCRIPTOR_SIZE,
- &hid_configuration_descriptor_data[EXTRA_HID_DESC_OFFSET]
- };
- #endif
- #ifdef NKRO_ENABLE
- static const USBDescriptor nkro_hid_descriptor = {
- HID_DESCRIPTOR_SIZE,
- &hid_configuration_descriptor_data[NKRO_HID_DESC_OFFSET]
- };
- #endif
- static const uint8_t usb_string_langid[] = {
- USB_DESC_BYTE(4),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- USB_DESC_WORD(0x0409)
- };
- #define PP_NARG(...) \
- PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
- #define PP_NARG_(...) \
- PP_ARG_N(__VA_ARGS__)
- #define PP_ARG_N( \
- _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
- _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
- _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
- _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
- _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
- _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
- _61,_62,_63,N,...) N
- #define PP_RSEQ_N() \
- 63,62,61,60, \
- 59,58,57,56,55,54,53,52,51,50, \
- 49,48,47,46,45,44,43,42,41,40, \
- 39,38,37,36,35,34,33,32,31,30, \
- 29,28,27,26,25,24,23,22,21,20, \
- 19,18,17,16,15,14,13,12,11,10, \
- 9,8,7,6,5,4,3,2,1,0
- static const uint8_t usb_string_vendor[] = {
- USB_DESC_BYTE(PP_NARG(USBSTR_MANUFACTURER)+2),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- USBSTR_MANUFACTURER
- };
- static const uint8_t usb_string_description[] = {
- USB_DESC_BYTE(PP_NARG(USBSTR_PRODUCT)+2),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- USBSTR_PRODUCT
- };
- static uint8_t usb_string_serial[] = {
- USB_DESC_BYTE(22),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- '0', 0, 'x', 0, 'D', 0, 'E', 0, 'A', 0, 'D', 0, 'B', 0, 'E', 0, 'E', 0, 'F', 0
- };
- static const USBDescriptor usb_strings[] = {
- { sizeof usb_string_langid, usb_string_langid }
- ,
- { sizeof usb_string_vendor, usb_string_vendor }
- ,
- { sizeof usb_string_description, usb_string_description }
- ,
- { sizeof usb_string_serial, usb_string_serial }
- };
- static const USBDescriptor *usb_get_descriptor_cb(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) {
- (void)usbp;
- (void)lang;
- switch(dtype) {
-
- case USB_DESCRIPTOR_DEVICE:
- return &usb_device_descriptor;
- case USB_DESCRIPTOR_CONFIGURATION:
- return &hid_configuration_descriptor;
- case USB_DESCRIPTOR_STRING:
- if(dindex < 4)
- return &usb_strings[dindex];
- break;
-
- case USB_DESCRIPTOR_HID:
- switch(lang) {
- case KBD_INTERFACE:
- return &keyboard_hid_descriptor;
- #ifdef MOUSE_ENABLE
- case MOUSE_INTERFACE:
- return &mouse_hid_descriptor;
- #endif
- #ifdef CONSOLE_ENABLE
- case CONSOLE_INTERFACE:
- return &console_hid_descriptor;
- #endif
- #ifdef EXTRAKEY_ENABLE
- case EXTRA_INTERFACE:
- return &extra_hid_descriptor;
- #endif
- #ifdef NKRO_ENABLE
- case NKRO_INTERFACE:
- return &nkro_hid_descriptor;
- #endif
- }
- case USB_DESCRIPTOR_HID_REPORT:
- switch(lang) {
- case KBD_INTERFACE:
- return &keyboard_hid_report_descriptor;
- #ifdef MOUSE_ENABLE
- case MOUSE_INTERFACE:
- return &mouse_hid_report_descriptor;
- #endif
- #ifdef CONSOLE_ENABLE
- case CONSOLE_INTERFACE:
- return &console_hid_report_descriptor;
- #endif
- #ifdef EXTRAKEY_ENABLE
- case EXTRA_INTERFACE:
- return &extra_hid_report_descriptor;
- #endif
- #ifdef NKRO_ENABLE
- case NKRO_INTERFACE:
- return &nkro_hid_report_descriptor;
- #endif
- }
- }
- return NULL;
- }
- static USBInEndpointState kbd_ep_state;
- static const USBEndpointConfig kbd_ep_config = {
- USB_EP_MODE_TYPE_INTR,
- NULL,
- kbd_in_cb,
- NULL,
- KBD_EPSIZE,
- 0,
- &kbd_ep_state,
- NULL,
- 2,
- NULL
- };
- #ifdef MOUSE_ENABLE
- static USBInEndpointState mouse_ep_state;
- static const USBEndpointConfig mouse_ep_config = {
- USB_EP_MODE_TYPE_INTR,
- NULL,
- mouse_in_cb,
- NULL,
- MOUSE_EPSIZE,
- 0,
- &mouse_ep_state,
- NULL,
- 2,
- NULL
- };
- #endif
- #ifdef CONSOLE_ENABLE
- static USBInEndpointState console_ep_state;
- static const USBEndpointConfig console_ep_config = {
- USB_EP_MODE_TYPE_INTR,
- NULL,
- console_in_cb,
- NULL,
- CONSOLE_EPSIZE,
- 0,
- &console_ep_state,
- NULL,
- 2,
- NULL
- };
- #endif
- #ifdef EXTRAKEY_ENABLE
- static USBInEndpointState extra_ep_state;
- static const USBEndpointConfig extra_ep_config = {
- USB_EP_MODE_TYPE_INTR,
- NULL,
- extra_in_cb,
- NULL,
- EXTRA_EPSIZE,
- 0,
- &extra_ep_state,
- NULL,
- 2,
- NULL
- };
- #endif
- #ifdef NKRO_ENABLE
- static USBInEndpointState nkro_ep_state;
- static const USBEndpointConfig nkro_ep_config = {
- USB_EP_MODE_TYPE_INTR,
- NULL,
- nkro_in_cb,
- NULL,
- NKRO_EPSIZE,
- 0,
- &nkro_ep_state,
- NULL,
- 2,
- NULL
- };
- #endif
- static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
- switch(event) {
- case USB_EVENT_RESET:
-
- return;
- case USB_EVENT_ADDRESS:
- return;
- case USB_EVENT_CONFIGURED:
- osalSysLockFromISR();
-
- usbInitEndpointI(usbp, KBD_ENDPOINT, &kbd_ep_config);
- #ifdef MOUSE_ENABLE
- usbInitEndpointI(usbp, MOUSE_ENDPOINT, &mouse_ep_config);
- #endif
- #ifdef CONSOLE_ENABLE
- usbInitEndpointI(usbp, CONSOLE_ENDPOINT, &console_ep_config);
-
- #endif
- #ifdef EXTRAKEY_ENABLE
- usbInitEndpointI(usbp, EXTRA_ENDPOINT, &extra_ep_config);
- #endif
- #ifdef NKRO_ENABLE
- usbInitEndpointI(usbp, NKRO_ENDPOINT, &nkro_ep_config);
- #endif
- osalSysUnlockFromISR();
- return;
- case USB_EVENT_SUSPEND:
-
- #ifdef SLEEP_LED_ENABLE
- sleep_led_enable();
- #endif
- return;
- case USB_EVENT_WAKEUP:
-
- suspend_wakeup_init();
- #ifdef SLEEP_LED_ENABLE
- sleep_led_disable();
-
- led_set(host_keyboard_leds());
- #endif
- return;
- case USB_EVENT_STALLED:
- return;
- }
- }
- static uint16_t get_hword(uint8_t *p) {
- uint16_t hw;
- hw = (uint16_t)*p++;
- hw |= (uint16_t)*p << 8U;
- return hw;
- }
- static bool usb_request_hook_cb(USBDriver *usbp) {
- const USBDescriptor *dp;
-
-
- if(((usbp->setup[0] & USB_RTYPE_TYPE_MASK) == USB_RTYPE_TYPE_CLASS) &&
- ((usbp->setup[0] & USB_RTYPE_RECIPIENT_MASK) == USB_RTYPE_RECIPIENT_INTERFACE)) {
- switch(usbp->setup[0] & USB_RTYPE_DIR_MASK) {
- case USB_RTYPE_DIR_DEV2HOST:
- switch(usbp->setup[1]) {
- case HID_GET_REPORT:
- switch(usbp->setup[4]) {
- case KBD_INTERFACE:
- #ifdef NKRO_ENABLE
- case NKRO_INTERFACE:
- #endif
- usbSetupTransfer(usbp, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent), NULL);
- return TRUE;
- break;
- #ifdef MOUSE_ENABLE
- case MOUSE_INTERFACE:
- usbSetupTransfer(usbp, (uint8_t *)&mouse_report_blank, sizeof(mouse_report_blank), NULL);
- return TRUE;
- break;
- #endif
- #ifdef CONSOLE_ENABLE
- case CONSOLE_INTERFACE:
- usbSetupTransfer(usbp, console_queue_buffer, CONSOLE_EPSIZE, NULL);
- return TRUE;
- break;
- #endif
- #ifdef EXTRAKEY_ENABLE
- case EXTRA_INTERFACE:
- if(usbp->setup[3] == 1) {
- switch(usbp->setup[2]) {
- case REPORT_ID_SYSTEM:
- extra_report_blank[0] = REPORT_ID_SYSTEM;
- usbSetupTransfer(usbp, (uint8_t *)extra_report_blank, sizeof(extra_report_blank), NULL);
- return TRUE;
- break;
- case REPORT_ID_CONSUMER:
- extra_report_blank[0] = REPORT_ID_CONSUMER;
- usbSetupTransfer(usbp, (uint8_t *)extra_report_blank, sizeof(extra_report_blank), NULL);
- return TRUE;
- break;
- default:
- return FALSE;
- }
- } else {
- return FALSE;
- }
- break;
- #endif
- default:
- usbSetupTransfer(usbp, NULL, 0, NULL);
- return TRUE;
- break;
- }
- break;
- case HID_GET_PROTOCOL:
- if((usbp->setup[4] == KBD_INTERFACE) && (usbp->setup[5] == 0)) {
- usbSetupTransfer(usbp, &keyboard_protocol, 1, NULL);
- return TRUE;
- }
- break;
- case HID_GET_IDLE:
- usbSetupTransfer(usbp, &keyboard_idle, 1, NULL);
- return TRUE;
- break;
- }
- break;
- case USB_RTYPE_DIR_HOST2DEV:
- switch(usbp->setup[1]) {
- case HID_SET_REPORT:
- switch(usbp->setup[4]) {
- case KBD_INTERFACE:
- #ifdef NKRO_ENABLE
- case NKRO_INTERFACE:
- #endif
-
- usbSetupTransfer(usbp, (uint8_t *)&keyboard_led_stats, 1, NULL);
- return TRUE;
- break;
- }
- break;
- case HID_SET_PROTOCOL:
- if((usbp->setup[4] == KBD_INTERFACE) && (usbp->setup[5] == 0)) {
- keyboard_protocol = ((usbp->setup[2]) != 0x00);
- #ifdef NKRO_ENABLE
- keyboard_nkro = !!keyboard_protocol;
- if(!keyboard_nkro && keyboard_idle) {
- #else
- if(keyboard_idle) {
- #endif
-
- osalSysLockFromISR();
- chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
- osalSysUnlockFromISR();
- }
- }
- usbSetupTransfer(usbp, NULL, 0, NULL);
- return TRUE;
- break;
- case HID_SET_IDLE:
- keyboard_idle = usbp->setup[3];
-
- #ifdef NKRO_ENABLE
- if(!keyboard_nkro && keyboard_idle) {
- #else
- if(keyboard_idle) {
- #endif
- osalSysLockFromISR();
- chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
- osalSysUnlockFromISR();
- }
- usbSetupTransfer(usbp, NULL, 0, NULL);
- return TRUE;
- break;
- }
- break;
- }
- }
-
- if((usbp->setup[0] == 0x81) && (usbp->setup[1] == USB_REQ_GET_DESCRIPTOR)) {
- dp = usbp->config->get_descriptor_cb(usbp, usbp->setup[3], usbp->setup[2], get_hword(&usbp->setup[4]));
- if(dp == NULL)
- return FALSE;
- usbSetupTransfer(usbp, (uint8_t *)dp->ud_string, dp->ud_size, NULL);
- return TRUE;
- }
- return FALSE;
- }
- static void usb_sof_cb(USBDriver *usbp) {
- kbd_sof_cb(usbp);
- }
- static const USBConfig usbcfg = {
- usb_event_cb,
- usb_get_descriptor_cb,
- usb_request_hook_cb,
- usb_sof_cb
- };
- void init_usb_driver(USBDriver *usbp) {
-
- usbDisconnectBus(usbp);
- chThdSleepMilliseconds(1500);
- usbStart(usbp, &usbcfg);
- usbConnectBus(usbp);
- chVTObjectInit(&keyboard_idle_timer);
- #ifdef CONSOLE_ENABLE
- obqObjectInit(&console_buf_queue, console_queue_buffer, CONSOLE_EPSIZE, CONSOLE_QUEUE_CAPACITY, console_queue_onotify, (void*)usbp);
- chVTObjectInit(&console_flush_timer);
- #endif
- }
- void send_remote_wakeup(USBDriver *usbp) {
- (void)usbp;
- #if defined(K20x) || defined(KL2x)
- #if KINETIS_USB_USE_USB0
- USB0->CTL |= USBx_CTL_RESUME;
- chThdSleepMilliseconds(15);
- USB0->CTL &= ~USBx_CTL_RESUME;
- #endif
- #elif defined(STM32F0XX) || defined(STM32F1XX)
- STM32_USB->CNTR |= CNTR_RESUME;
- chThdSleepMilliseconds(15);
- STM32_USB->CNTR &= ~CNTR_RESUME;
- #else
- #warning Sending remote wakeup packet not implemented for your platform.
- #endif
- }
- void kbd_in_cb(USBDriver *usbp, usbep_t ep) {
-
- (void)usbp;
- (void)ep;
- }
- #ifdef NKRO_ENABLE
- void nkro_in_cb(USBDriver *usbp, usbep_t ep) {
-
- (void)usbp;
- (void)ep;
- }
- #endif
- void kbd_sof_cb(USBDriver *usbp) {
- (void)usbp;
- }
- static void keyboard_idle_timer_cb(void *arg) {
- USBDriver *usbp = (USBDriver *)arg;
- osalSysLockFromISR();
-
- if(usbGetDriverStateI(usbp) != USB_ACTIVE) {
-
- osalSysUnlockFromISR();
- return;
- }
- #ifdef NKRO_ENABLE
- if(!keyboard_nkro && keyboard_idle) {
- #else
- if(keyboard_idle) {
- #endif
-
- if(!usbGetTransmitStatusI(usbp, KBD_ENDPOINT)) {
- usbStartTransmitI(usbp, KBD_ENDPOINT, (uint8_t *)&keyboard_report_sent, KBD_EPSIZE);
- }
-
- chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp);
- }
-
- osalSysUnlockFromISR();
- }
- uint8_t keyboard_leds(void) {
- return (uint8_t)(keyboard_led_stats & 0xFF);
- }
- void send_keyboard(report_keyboard_t *report) {
- osalSysLock();
- if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
- osalSysUnlock();
- return;
- }
- osalSysUnlock();
- #ifdef NKRO_ENABLE
- if(keyboard_nkro) {
-
-
-
- osalSysLock();
- if(usbGetTransmitStatusI(&USB_DRIVER, NKRO_ENDPOINT)) {
-
- osalThreadSuspendS(&(&USB_DRIVER)->epc[NKRO_ENDPOINT]->in_state->thread);
- }
- usbStartTransmitI(&USB_DRIVER, NKRO_ENDPOINT, (uint8_t *)report, sizeof(report_keyboard_t));
- osalSysUnlock();
- } else
- #endif
- {
-
-
- osalSysLock();
- if(usbGetTransmitStatusI(&USB_DRIVER, KBD_ENDPOINT)) {
-
- osalThreadSuspendS(&(&USB_DRIVER)->epc[KBD_ENDPOINT]->in_state->thread);
- }
- usbStartTransmitI(&USB_DRIVER, KBD_ENDPOINT, (uint8_t *)report, KBD_EPSIZE);
- osalSysUnlock();
- }
- keyboard_report_sent = *report;
- }
- #ifdef MOUSE_ENABLE
- void mouse_in_cb(USBDriver *usbp, usbep_t ep) {
- (void)usbp;
- (void)ep;
- }
- void send_mouse(report_mouse_t *report) {
- osalSysLock();
- if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
- osalSysUnlock();
- return;
- }
- osalSysUnlock();
-
- osalSysLock();
- usbStartTransmitI(&USB_DRIVER, MOUSE_ENDPOINT, (uint8_t *)report, sizeof(report_mouse_t));
- osalSysUnlock();
- }
- #else
- void send_mouse(report_mouse_t *report) {
- (void)report;
- }
- #endif
- #ifdef EXTRAKEY_ENABLE
- void extra_in_cb(USBDriver *usbp, usbep_t ep) {
-
- (void)usbp;
- (void)ep;
- }
- static void send_extra_report(uint8_t report_id, uint16_t data) {
- osalSysLock();
- if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
- osalSysUnlock();
- return;
- }
- report_extra_t report = {
- .report_id = report_id,
- .usage = data
- };
- usbStartTransmitI(&USB_DRIVER, EXTRA_ENDPOINT, (uint8_t *)&report, sizeof(report_extra_t));
- osalSysUnlock();
- }
- void send_system(uint16_t data) {
- send_extra_report(REPORT_ID_SYSTEM, data);
- }
- void send_consumer(uint16_t data) {
- send_extra_report(REPORT_ID_CONSUMER, data);
- }
- #else
- void send_system(uint16_t data) {
- (void)data;
- }
- void send_consumer(uint16_t data) {
- (void)data;
- }
- #endif
- #ifdef CONSOLE_ENABLE
- void console_in_cb(USBDriver *usbp, usbep_t ep) {
- (void)ep;
- uint8_t *buf;
- size_t n;
- osalSysLockFromISR();
-
- chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
-
- if (usbp->epc[CONSOLE_ENDPOINT]->in_state->txsize > 0U) {
- obqReleaseEmptyBufferI(&console_buf_queue);
- }
-
- buf = obqGetFullBufferI(&console_buf_queue, &n);
- if (buf != NULL) {
-
-
- usbStartTransmitI(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
- } else {
-
- }
- osalSysUnlockFromISR();
- }
- void console_queue_onotify(io_buffers_queue_t *bqp) {
- size_t n;
- USBDriver *usbp = bqGetLinkX(bqp);
- if(usbGetDriverStateI(usbp) != USB_ACTIVE)
- return;
-
- if (!usbGetTransmitStatusI(usbp, CONSOLE_ENDPOINT)) {
-
- uint8_t *buf = obqGetFullBufferI(&console_buf_queue, &n);
- if (buf != NULL) {
-
-
- usbStartTransmitI(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
- }
- }
- }
- static void console_flush_cb(void *arg) {
- USBDriver *usbp = (USBDriver *)arg;
- osalSysLockFromISR();
-
- if(usbGetDriverStateI(usbp) != USB_ACTIVE) {
-
- chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
- osalSysUnlockFromISR();
- return;
- }
-
- if (usbGetTransmitStatusI(usbp, CONSOLE_ENDPOINT)) {
-
- chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
- osalSysUnlockFromISR();
- return;
- }
-
- if(obqTryFlushI(&console_buf_queue)) {
- size_t n,i;
- uint8_t *buf = obqGetFullBufferI(&console_buf_queue, &n);
- osalDbgAssert(buf != NULL, "queue is empty");
-
- for(i=n; i<CONSOLE_EPSIZE; i++)
- buf[i]=0;
- usbStartTransmitI(usbp, CONSOLE_ENDPOINT, buf, CONSOLE_EPSIZE);
- }
-
- chVTSetI(&console_flush_timer, MS2ST(CONSOLE_FLUSH_MS), console_flush_cb, (void *)usbp);
- osalSysUnlockFromISR();
- }
- int8_t sendchar(uint8_t c) {
- osalSysLock();
- if(usbGetDriverStateI(&USB_DRIVER) != USB_ACTIVE) {
- osalSysUnlock();
- return 0;
- }
- osalSysUnlock();
-
- return(obqPutTimeout(&console_buf_queue, c, US2ST(100)));
- }
- #else
- int8_t sendchar(uint8_t c) {
- (void)c;
- return 0;
- }
- #endif
- void sendchar_pf(void *p, char c) {
- (void)p;
- sendchar((uint8_t)c);
- }
|