diff options
Diffstat (limited to 'tmk_core')
-rw-r--r-- | tmk_core/common.mk | 7 | ||||
-rw-r--r-- | tmk_core/common/action.c | 1 | ||||
-rw-r--r-- | tmk_core/common/action_layer.c | 7 | ||||
-rw-r--r-- | tmk_core/common/arm_atsam/printf.c | 5 | ||||
-rw-r--r-- | tmk_core/common/eeconfig.c | 7 | ||||
-rw-r--r-- | tmk_core/common/webusb.c | 59 | ||||
-rw-r--r-- | tmk_core/common/webusb.h | 46 | ||||
-rw-r--r-- | tmk_core/protocol/arm_atsam/md_rgb_matrix.c | 2 | ||||
-rw-r--r-- | tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c | 2 | ||||
-rw-r--r-- | tmk_core/protocol/chibios/main.c | 7 | ||||
-rw-r--r-- | tmk_core/protocol/chibios/usb_main.c | 83 | ||||
-rw-r--r-- | tmk_core/protocol/lufa/lufa.c | 131 | ||||
-rw-r--r-- | tmk_core/protocol/usb_descriptor.c | 52 | ||||
-rw-r--r-- | tmk_core/protocol/usb_descriptor.h | 22 | ||||
-rw-r--r-- | tmk_core/protocol/webusb_descriptor.h | 266 |
15 files changed, 690 insertions, 7 deletions
diff --git a/tmk_core/common.mk b/tmk_core/common.mk index 2f8f81126a..d34d8368f3 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -1,3 +1,5 @@ +PRINTF_PATH = $(LIB_PATH)/printf + COMMON_DIR = common PLATFORM_COMMON_DIR = $(COMMON_DIR)/$(PLATFORM_KEY) @@ -52,6 +54,11 @@ ifeq ($(strip $(RAW_ENABLE)), yes) TMK_COMMON_DEFS += -DRAW_ENABLE endif +ifeq ($(strip $(WEBUSB_ENABLE)), yes) + TMK_COMMON_SRC += $(TMK_DIR)/common/webusb.c + TMK_COMMON_DEFS += -DWEBUSB_ENABLE +endif + ifeq ($(strip $(CONSOLE_ENABLE)), yes) TMK_COMMON_DEFS += -DCONSOLE_ENABLE else diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c index bd41d28b66..5660c3fa1f 100644 --- a/tmk_core/common/action.c +++ b/tmk_core/common/action.c @@ -923,6 +923,7 @@ void unregister_mods(uint8_t mods) { } } + /** \brief Adds the given weak modifiers and sends a keyboard report immediately. * * \param mods A bitfield of modifiers to register. diff --git a/tmk_core/common/action_layer.c b/tmk_core/common/action_layer.c index af2d7d964b..9bd0948379 100644 --- a/tmk_core/common/action_layer.c +++ b/tmk_core/common/action_layer.c @@ -3,7 +3,9 @@ #include "action.h" #include "util.h" #include "action_layer.h" - +#ifdef ORYX_ENABLE +# include "oryx.h" +#endif #ifdef DEBUG_ACTION # include "debug.h" #else @@ -98,6 +100,9 @@ __attribute__((weak)) layer_state_t layer_state_set_kb(layer_state_t state) { re */ void layer_state_set(layer_state_t state) { state = layer_state_set_kb(state); +#ifdef ORYX_ENABLE + layer_state_set_oryx(state); +#endif dprint("layer_state: "); layer_debug(); dprint(" to "); diff --git a/tmk_core/common/arm_atsam/printf.c b/tmk_core/common/arm_atsam/printf.c index 2cb59706a8..6f7e8d343f 100644 --- a/tmk_core/common/arm_atsam/printf.c +++ b/tmk_core/common/arm_atsam/printf.c @@ -68,5 +68,8 @@ void console_printf(char *fmt, ...) { } #endif // CONSOLE_ENABLE +<<<<<<< HEAD +======= -void print_set_sendchar(sendchar_func_t send) {}
\ No newline at end of file +void print_set_sendchar(sendchar_func_t send) {} +>>>>>>> 0.12.52~1 diff --git a/tmk_core/common/eeconfig.c b/tmk_core/common/eeconfig.c index ffa56ab56d..57c468ed7a 100644 --- a/tmk_core/common/eeconfig.c +++ b/tmk_core/common/eeconfig.c @@ -3,6 +3,9 @@ #include "eeprom.h" #include "eeconfig.h" #include "action_layer.h" +#ifdef ORYX_ENABLE +# include "oryx.h" +#endif #ifdef STM32_EEPROM_ENABLE # include <hal.h> @@ -59,6 +62,10 @@ void eeconfig_init_quantum(void) { eeprom_update_dword(EECONFIG_RGB_MATRIX, 0); eeprom_update_word(EECONFIG_RGB_MATRIX_EXTENDED, 0); +#ifdef ORYX_ENABLE + eeconfig_init_oryx(); +#endif + // TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS // within the emulated eeprom via dfu-util or another tool #if defined INIT_EE_HANDS_LEFT diff --git a/tmk_core/common/webusb.c b/tmk_core/common/webusb.c new file mode 100644 index 0000000000..bc349e6afa --- /dev/null +++ b/tmk_core/common/webusb.c @@ -0,0 +1,59 @@ +#include "quantum.h" +#include <string.h> +#include "webusb.h" +#include "wait.h" + +webusb_state_t webusb_state = { + .paired = false, + .pairing = false, +}; + +__attribute__((weak)) bool webusb_receive_quantum(uint8_t *data, uint8_t length) { return false; } + +void webusb_receive(uint8_t *data, uint8_t length) { + uint8_t command = data[0]; + + if(command == WEBUSB_CMD_PAIR && webusb_state.pairing == true) { + uint8_t event[3]; + webusb_state.pairing = false; + webusb_state.paired = true; + event[0] = WEBUSB_STATUS_OK; + event[1] = WEBUSB_EVT_PAIRED; + event[2] = WEBUSB_STOP_BIT; + webusb_send(event, sizeof(event)); + return; + } + + if(command == WEBUSB_CMD_GET_FW_VERSION) { + // Landing page + packet headers(2) + stop bit(1) + uint8_t lp_size = sizeof(FIRMWARE_VERSION) + 3; + uint8_t url[lp_size]; + + uint8_t event[2]; + event[0] = WEBUSB_STATUS_OK; + event[1] = WEBUSB_EVT_FW_VERSION; + + uint8_t stop[1]; + stop[0] = WEBUSB_STOP_BIT; + + memcpy(url, event, 2); + memcpy(url + 2, FIRMWARE_VERSION, sizeof(FIRMWARE_VERSION)); + memcpy(url + 2 + sizeof(FIRMWARE_VERSION), stop, 1); + webusb_send(url, lp_size); + return; + } + + if(webusb_state.paired == true) { + if (!webusb_receive_quantum(data, length)) { + webusb_error(WEBUSB_STATUS_UNKNOWN_COMMAND); + } + } else { + webusb_error(WEBUSB_STATUS_NOT_PAIRED); + } +}; + +void webusb_error(uint8_t code) { + uint8_t buffer[1]; + buffer[0] = code; + webusb_send(buffer, 1); +} diff --git a/tmk_core/common/webusb.h b/tmk_core/common/webusb.h new file mode 100644 index 0000000000..7bec7c50e1 --- /dev/null +++ b/tmk_core/common/webusb.h @@ -0,0 +1,46 @@ +#pragma once + +#include <stdint.h> +#include <stdbool.h> + +#ifndef FIRMWARE_VERSION +#define FIRMWARE_VERSION u8"default" +#endif +#define WEBUSB_STOP_BIT -2 +#define WEBUSB_BLINK_STEPS 512 +#define WEBUSB_BLINK_END WEBUSB_BLINK_STEPS * 60 + +void webusb_receive(uint8_t *data, uint8_t length); +void webusb_send(uint8_t *data, uint8_t length); +void webusb_layer_event(void); +void webusb_error(uint8_t code); +void webusb_set_pairing_state(void); +bool webusb_receive_quantum(uint8_t *data, uint8_t length); + +typedef struct{ + bool paired; + bool pairing; +} webusb_state_t; + +extern webusb_state_t webusb_state; + +enum Webusb_Status_Code { + WEBUSB_STATUS_NOT_PAIRED = -1, + WEBUSB_STATUS_OK, + WEBUSB_STATUS_UNKNOWN_COMMAND, + WEBUSB_STATUS_SAFE_RANGE, +}; + +enum Webusb_Command_Code { + WEBUSB_CMD_PAIR, + WEBUSB_CMD_GET_FW_VERSION, + WEBUSB_CMD_SAFE_RANGE, + +}; + +enum Webusb_Event_Code { + WEBUSB_EVT_PAIRED, + WEBUSB_EVT_FW_VERSION, + WEBUSB_EVT_SAFE_RANGE, + +}; diff --git a/tmk_core/protocol/arm_atsam/md_rgb_matrix.c b/tmk_core/protocol/arm_atsam/md_rgb_matrix.c index 609ae047e6..2b0805dc86 100644 --- a/tmk_core/protocol/arm_atsam/md_rgb_matrix.c +++ b/tmk_core/protocol/arm_atsam/md_rgb_matrix.c @@ -471,4 +471,4 @@ static void md_rgb_matrix_config_override(int i) { } # endif // USE_MASSDROP_CONFIGURATOR -#endif // RGB_MATRIX_ENABLE
\ No newline at end of file +#endif // RGB_MATRIX_ENABLE diff --git a/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c b/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c index 92b40b5b85..fce225a6c4 100644 --- a/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c +++ b/tmk_core/protocol/arm_atsam/md_rgb_matrix_programs.c @@ -98,4 +98,4 @@ void *led_setups[] = {leds_rainbow_s, leds_rainbow_ns, leds_teal_salmon, leds_ye const uint8_t led_setups_count = sizeof(led_setups) / sizeof(led_setups[0]); # endif // USE_MASSDROP_CONFIGURATOR -#endif // RGB_MATRIX_ENABLE
\ No newline at end of file +#endif // RGB_MATRIX_ENABLE diff --git a/tmk_core/protocol/chibios/main.c b/tmk_core/protocol/chibios/main.c index 199741594a..2661e58873 100644 --- a/tmk_core/protocol/chibios/main.c +++ b/tmk_core/protocol/chibios/main.c @@ -84,6 +84,10 @@ void console_task(void); void midi_ep_task(void); #endif +#ifdef WEBUSB_ENABLE +void webusb_task(void); +#endif + /* TESTING * Amber LED blinker thread, times are in milliseconds. */ @@ -255,6 +259,9 @@ int main(void) { #ifdef RAW_ENABLE raw_hid_task(); #endif +#ifdef WEBUSB_ENABLE + webusb_task(); +#endif // Run housekeeping housekeeping_task(); diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index d04302acae..990dc1b91c 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -48,6 +48,10 @@ extern keymap_config_t keymap_config; #endif +#ifdef WEBUSB_ENABLE +# include "webusb.h" +#endif + #ifdef JOYSTICK_ENABLE # include "joystick.h" #endif @@ -166,6 +170,23 @@ static const USBEndpointConfig shared_ep_config = { }; #endif + +#ifdef WEBUSB_ENABLE +/** Microsoft OS 2.0 Descriptor. This is used by Windows to select the USB driver for the device. + * + * For WebUSB in Chrome, the correct driver is WinUSB, which is selected via CompatibleID. + * + * Additionally, while Chrome is built using libusb, a magic registry key needs to be set containing a GUID for + * the device. + */ +const MS_OS_20_Descriptor_t PROGMEM MS_OS_20_Descriptor = MS_OS_20_DESCRIPTOR; + +/** URL descriptor string. This is a UTF-8 string containing a URL excluding the prefix. At least one of these must be + * defined and returned when the Landing Page descriptor index is requested. + */ +const WebUSB_URL_Descriptor_t PROGMEM WebUSB_LandingPage = WEBUSB_URL_DESCRIPTOR(WEBUSB_LANDING_PAGE_URL); +#endif + #if STM32_USB_USE_OTG1 typedef struct { size_t queue_capacity_in; @@ -313,6 +334,9 @@ typedef struct { #ifdef VIRTSER_ENABLE usb_driver_config_t serial_driver; #endif +#ifdef WEBUSB_ENABLE + usb_driver_config_t webusb_driver; +#endif #ifdef JOYSTICK_ENABLE usb_driver_config_t joystick_driver; #endif @@ -353,6 +377,13 @@ static usb_driver_configs_t drivers = { .serial_driver = QMK_USB_DRIVER_CONFIG(CDC, CDC_NOTIFICATION_EPNUM, false), #endif +#ifdef WEBUSB_ENABLE +# define WEBUSB_IN_CAPACITY 4 +# define WEBUSB_OUT_CAPACITY 4 +# define WEBUSB_IN_MODE USB_EP_MODE_TYPE_INTR +# define WEBUSB_OUT_MODE USB_EP_MODE_TYPE_INTR + .webusb_driver = QMK_USB_DRIVER_CONFIG(WEBUSB, 0, false), +#endif #ifdef JOYSTICK_ENABLE # define JOYSTICK_IN_CAPACITY 4 # define JOYSTICK_OUT_CAPACITY 4 @@ -409,9 +440,9 @@ static inline void usb_event_suspend_handler(void) { static inline void usb_event_wakeup_handler(void) { suspend_wakeup_init(); #ifdef SLEEP_LED_ENABLE - sleep_led_disable(); - // NOTE: converters may not accept this - led_set(host_keyboard_leds()); + sleep_led_disable(); + // NOTE: converters may not accept this + led_set(host_keyboard_leds()); #endif /* SLEEP_LED_ENABLE */ } @@ -634,6 +665,27 @@ static bool usb_request_hook_cb(USBDriver *usbp) { } } +#ifdef WEBUSB_ENABLE + switch (usbp->setup[1]) { + case WEBUSB_VENDOR_CODE: + if (usbp->setup[4] == WebUSB_RTYPE_GetURL) { + if (usbp->setup[2] == WEBUSB_LANDING_PAGE_INDEX) { + usbSetupTransfer(usbp, (uint8_t *)&WebUSB_LandingPage, WebUSB_LandingPage.Header.Size, NULL); + return TRUE; + break; + } + } + break; + + case MS_OS_20_VENDOR_CODE: + if (usbp->setup[4] == MS_OS_20_DESCRIPTOR_INDEX) { + usbSetupTransfer(usbp, (uint8_t *)&MS_OS_20_Descriptor, MS_OS_20_Descriptor.Header.TotalLength, NULL); + return TRUE; + break; + } + break; + } +#endif /* Handle the Get_Descriptor Request for HID class (not handled by the default hook) */ 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])); @@ -1006,6 +1058,31 @@ void raw_hid_task(void) { #endif +#ifdef WEBUSB_ENABLE +void webusb_send(uint8_t *data, uint8_t length) { + if(chnWriteTimeout(&drivers.webusb_driver.driver, data, length, TIME_IMMEDIATE) != length){ + webusb_state.paired = false; + webusb_state.pairing = false; + } +} + + // Users should #include "raw_hid.h" in their own code + // and implement this function there. Leave this as weak linkage + // so users can opt to not handle data coming in. + +void webusb_task(void) { + uint8_t buffer[WEBUSB_EPSIZE]; + size_t size = 0; + do { + size_t size = chnReadTimeout(&drivers.webusb_driver.driver, buffer, sizeof(buffer), TIME_IMMEDIATE); + if (size > 0) { + webusb_receive(buffer, size); + } + } while (size > 0); +} + +#endif + #ifdef MIDI_ENABLE void send_midi_packet(MIDI_EventPacket_t *event) { chnWrite(&drivers.midi_driver.driver, (uint8_t *)event, sizeof(MIDI_EventPacket_t)); } diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 63619fdb3b..5976470b6e 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -85,6 +85,9 @@ extern keymap_config_t keymap_config; # include "raw_hid.h" #endif +#ifdef WEBUSB_ENABLE +# include "webusb.h" +#endif #ifdef JOYSTICK_ENABLE # include "joystick.h" #endif @@ -307,6 +310,66 @@ static void Console_Task(void) { } #endif +#ifdef WEBUSB_ENABLE +void webusb_send(uint8_t *data, uint8_t length) { + if (USB_DeviceState != DEVICE_STATE_Configured) { + return; + } + + Endpoint_SelectEndpoint(WEBUSB_IN_EPNUM); + + if(Endpoint_Write_Stream_LE(data, length, NULL)) { + // Stream failed to complete, resetting WEBUSB's state + webusb_state.paired = false; + webusb_state.pairing = false; + } + Endpoint_ClearIN(); +} + +static void webusb_task(void) { + // Create a temporary buffer to hold the read in data from the host + uint8_t data[WEBUSB_EPSIZE]; + bool data_read = false; + + + // Device must be connected and configured for the task to run + if (USB_DeviceState != DEVICE_STATE_Configured) return; + + Endpoint_SelectEndpoint(WEBUSB_OUT_EPNUM); + + // Check to see if a packet has been sent from the host + if (Endpoint_IsOUTReceived()) { + // Check to see if the packet contains data + if (Endpoint_IsReadWriteAllowed()) { + /* Read data */ + Endpoint_Read_Stream_LE(data, sizeof(data), NULL); + data_read = true; + } + + // Finalize the stream transfer to receive the last packet + Endpoint_ClearOUT(); + + if (data_read) { + webusb_receive(data, sizeof(data)); + } + } +} + +/** Microsoft OS 2.0 Descriptor. This is used by Windows to select the USB driver for the device. + * + * For WebUSB in Chrome, the correct driver is WinUSB, which is selected via CompatibleID. + * + * Additionally, while Chrome is built using libusb, a magic registry key needs to be set containing a GUID for + * the device. + */ +const MS_OS_20_Descriptor_t PROGMEM MS_OS_20_Descriptor = MS_OS_20_DESCRIPTOR; + +/** URL descriptor string. This is a UTF-8 string containing a URL excluding the prefix. At least one of these must be + * defined and returned when the Landing Page descriptor index is requested. + */ +const WebUSB_URL_Descriptor_t PROGMEM WebUSB_LandingPage = WEBUSB_URL_DESCRIPTOR(WEBUSB_LANDING_PAGE_URL); +#endif + /******************************************************************************* * Joystick ******************************************************************************/ @@ -509,6 +572,12 @@ void EVENT_USB_Device_ConfigurationChanged(void) { # endif #endif +#ifdef WEBUSB_ENABLE + /* Setup Webusb Endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint(WEBUSB_IN_EPADDR, EP_TYPE_INTERRUPT, WEBUSB_EPSIZE, 1); + ConfigSuccess &= Endpoint_ConfigureEndpoint(WEBUSB_OUT_EPADDR, EP_TYPE_INTERRUPT, WEBUSB_EPSIZE, 1); +#endif + #ifdef MIDI_ENABLE /* Setup MIDI stream endpoints */ ConfigSuccess &= Endpoint_ConfigureEndpoint((MIDI_STREAM_IN_EPNUM | ENDPOINT_DIR_IN), EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1); @@ -647,6 +716,48 @@ void EVENT_USB_Device_ControlRequest(void) { } break; +#ifdef WEBUSB_ENABLE + case WEBUSB_VENDOR_CODE: + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE)) { + switch (USB_ControlRequest.wIndex) { + case WebUSB_RTYPE_GetURL: + switch (USB_ControlRequest.wValue) { + case WEBUSB_LANDING_PAGE_INDEX: + Endpoint_ClearSETUP(); + /* Write the descriptor data to the control endpoint */ + Endpoint_Write_Control_PStream_LE(&WebUSB_LandingPage, WebUSB_LandingPage.Header.Size); + /* Release the endpoint after transaction. */ + Endpoint_ClearStatusStage(); + break; + default: /* Stall transfer on invalid index. */ + Endpoint_StallTransaction(); + break; + } + break; + default: /* Stall on unknown WebUSB request */ + Endpoint_StallTransaction(); + break; + } + } + + break; + case MS_OS_20_VENDOR_CODE: + if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE)) { + switch (USB_ControlRequest.wIndex) { + case MS_OS_20_DESCRIPTOR_INDEX: + Endpoint_ClearSETUP(); + /* Write the descriptor data to the control endpoint */ + Endpoint_Write_Control_PStream_LE(&MS_OS_20_Descriptor, MS_OS_20_Descriptor.Header.TotalLength); + /* Release the endpoint after transaction. */ + Endpoint_ClearStatusStage(); + break; + default: /* Stall on unknown MS OS 2.0 request */ + Endpoint_StallTransaction(); + break; + } + } + break; +#endif } #ifdef VIRTSER_ENABLE @@ -797,7 +908,13 @@ static void send_system(uint16_t data) { static void send_consumer(uint16_t data) { #ifdef EXTRAKEY_ENABLE # ifdef BLUETOOTH_ENABLE +<<<<<<< HEAD + uint8_t where = where_to_send(); + + if (where == OUTPUT_BLUETOOTH || where == OUTPUT_USB_AND_BT) { +======= if (where_to_send() == OUTPUT_BLUETOOTH) { +>>>>>>> 0.12.52~1 # ifdef MODULE_ADAFRUIT_BLE adafruit_ble_send_consumer_key(data); # elif MODULE_RN42 @@ -811,6 +928,12 @@ static void send_consumer(uint16_t data) { serial_send(bitmap & 0xFF); serial_send((bitmap >> 8) & 0xFF); # endif +<<<<<<< HEAD + } + + if (where != OUTPUT_USB && where != OUTPUT_USB_AND_BT) { +======= +>>>>>>> 0.12.52~1 return; } # endif @@ -829,7 +952,11 @@ static void send_consumer(uint16_t data) { * FIXME: Needs doc */ int8_t sendchar(uint8_t c) { +<<<<<<< HEAD + // Not wait once timeouted. +======= // Do not wait if the previous write has timed_out. +>>>>>>> 0.12.52~1 // Because sendchar() is called so many times, waiting each call causes big lag. // The `timed_out` state is an approximation of the ideal `is_listener_disconnected?` state. static bool timed_out = false; @@ -1102,6 +1229,10 @@ int main(void) { raw_hid_task(); #endif +#ifdef WEBUSB_ENABLE + webusb_task(); +#endif + #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); #endif diff --git a/tmk_core/protocol/usb_descriptor.c b/tmk_core/protocol/usb_descriptor.c index ba7760f283..9277554856 100644 --- a/tmk_core/protocol/usb_descriptor.c +++ b/tmk_core/protocol/usb_descriptor.c @@ -39,6 +39,9 @@ #include "util.h" #include "report.h" #include "usb_descriptor.h" +#ifdef WEBUSB_ENABLE +# include "webusb_descriptor.h" +#endif #include "usb_descriptor_common.h" #ifdef JOYSTICK_ENABLE @@ -279,6 +282,12 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM ConsoleReport[] = { }; #endif +#ifdef WEBUSB_ENABLE +const USB_Descriptor_BOS_t PROGMEM BOSDescriptor = BOS_DESCRIPTOR( + (MS_OS_20_PLATFORM_DESCRIPTOR(MS_OS_20_VENDOR_CODE, MS_OS_20_DESCRIPTOR_SET_TOTAL_LENGTH)) + (WEBUSB_PLATFORM_DESCRIPTOR(WEBUSB_VENDOR_CODE, WEBUSB_LANDING_PAGE_INDEX)) +); +#endif #ifdef JOYSTICK_ENABLE # if JOYSTICK_AXES_COUNT == 0 && JOYSTICK_BUTTON_COUNT == 0 # error Need at least one axis or button for joystick @@ -351,7 +360,11 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = { .Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device }, +#if WEBUSB_ENABLE + .USBSpecification = VERSION_BCD(2, 1, 0), +#else .USBSpecification = VERSION_BCD(1, 1, 0), +#endif #if VIRTSER_ENABLE .Class = USB_CSCP_IADDeviceClass, @@ -627,6 +640,38 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = { }, #endif +#ifdef WEBUSB_ENABLE + /* + * Webusb + */ + .WebUSB_Interface = {.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface}, + + .InterfaceNumber = INTERFACE_ID_WebUSB, + .AlternateSetting = 0x00, + + .TotalEndpoints = 2, + + .Class = USB_CSCP_VendorSpecificClass, + .SubClass = 0x00, + .Protocol = 0x00, + + .InterfaceStrIndex = NO_DESCRIPTOR}, + + .WebUSB_DataInEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = WEBUSB_IN_EPADDR, + .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = WEBUSB_EPSIZE, + .PollingIntervalMS = 0x05}, + + .WebUSB_DataOutEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, + + .EndpointAddress = WEBUSB_OUT_EPADDR, + .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), + .EndpointSize = WEBUSB_EPSIZE, + .PollingIntervalMS = 0x05}, +#endif + #ifdef MIDI_ENABLE /* * MIDI @@ -981,6 +1026,13 @@ uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const Size = sizeof(USB_Descriptor_Device_t); break; +#ifdef WEBUSB_ENABLE + case DTYPE_BOS: + Address = &BOSDescriptor; + Size = pgm_read_byte(&BOSDescriptor.TotalLength); + + break; +#endif case DTYPE_Configuration: Address = &ConfigurationDescriptor; Size = sizeof(USB_Descriptor_Configuration_t); diff --git a/tmk_core/protocol/usb_descriptor.h b/tmk_core/protocol/usb_descriptor.h index 867e549b4f..2ea4861f36 100644 --- a/tmk_core/protocol/usb_descriptor.h +++ b/tmk_core/protocol/usb_descriptor.h @@ -48,6 +48,9 @@ #ifdef PROTOCOL_CHIBIOS # include <hal.h> #endif +#ifdef WEBUSB_ENABLE +# include "webusb_descriptor.h" +#endif /* * USB descriptor structure @@ -97,6 +100,12 @@ typedef struct { USB_Descriptor_Endpoint_t Console_OUTEndpoint; #endif +#ifdef WEBUSB_ENABLE + USB_Descriptor_Interface_t WebUSB_Interface; + USB_Descriptor_Endpoint_t WebUSB_DataInEndpoint; + USB_Descriptor_Endpoint_t WebUSB_DataOutEndpoint; +#endif + #ifdef MIDI_ENABLE USB_Descriptor_Interface_Association_t Audio_Interface_Association; // MIDI Audio Control Interface @@ -177,6 +186,10 @@ enum usb_interfaces { CDI_INTERFACE, #endif +#ifdef WEBUSB_ENABLE + INTERFACE_ID_WebUSB, +#endif + #if defined(JOYSTICK_ENABLE) JOYSTICK_INTERFACE, #endif @@ -251,6 +264,14 @@ enum usb_endpoints { CDC_OUT_EPNUM = NEXT_EPNUM, # endif #endif + +#ifdef WEBUSB_ENABLE + WEBUSB_IN_EPNUM = NEXT_EPNUM, + WEBUSB_OUT_EPNUM = NEXT_EPNUM, +# define WEBUSB_IN_EPADDR (ENDPOINT_DIR_IN | WEBUSB_IN_EPNUM) +# define WEBUSB_OUT_EPADDR (ENDPOINT_DIR_OUT | WEBUSB_OUT_EPNUM) +#endif + #ifdef JOYSTICK_ENABLE JOYSTICK_IN_EPNUM = NEXT_EPNUM, # if STM32_USB_USE_OTG1 @@ -283,6 +304,7 @@ enum usb_endpoints { #define MIDI_STREAM_EPSIZE 64 #define CDC_NOTIFICATION_EPSIZE 8 #define CDC_EPSIZE 16 +#define WEBUSB_EPSIZE 64 #define JOYSTICK_EPSIZE 8 uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const void** const DescriptorAddress); diff --git a/tmk_core/protocol/webusb_descriptor.h b/tmk_core/protocol/webusb_descriptor.h new file mode 100644 index 0000000000..a293e5ebb6 --- /dev/null +++ b/tmk_core/protocol/webusb_descriptor.h @@ -0,0 +1,266 @@ +#pragma once + +#ifndef WORD_TO_BYTES_LE +# define WORD_TO_BYTES_LE(n) n % 256, (n / 256) % 256 +#endif +#ifndef LONG_TO_BYTES_LE +# define LONG_TO_BYTES_LE(n) n % 256, (n / 256) % 256, (n / 65536) % 256, (n / 16777216) % 256 +#endif + +#define WEBUSB_VENDOR_CODE 0x42 + +#ifndef WEBUSB_LANDING_PAGE_URL +# define WEBUSB_LANDING_PAGE_URL u8"docs.qmk.fm" +#endif + +#define WEBUSB_LANDING_PAGE_PROTOCOL 1 /* 0: http 1: https forced to 1 since https is a requirement to connect over webusb */ + +#ifndef WEBUSB_LANDING_PAGE_INDEX +# define WEBUSB_LANDING_PAGE_INDEX 0 +#endif + +#define WEBUSB_VERSION VERSION_BCD(1, 0, 0) + +/* $ python -c "import uuid;print(', '.join(map(hex, uuid.UUID('3408b638-09a9-47a0-8bfd-a0768815b665').bytes_le)))" */ +#define WEBUSB_PLATFORM_UUID 0x38, 0xb6, 0x8, 0x34, 0xa9, 0x9, 0xa0, 0x47, 0x8b, 0xfd, 0xa0, 0x76, 0x88, 0x15, 0xb6, 0x65 + +/** \brief Convenience macro to easily create device capability platform descriptors for the WebUSB platform. + * + * \note This macro is designed to be wrapped in parentheses and included in a sequence to the \ref BOS_DESCRIPTOR macro. + * + * \param[in] VendorCode Vendor Code that all control requests coming from the browser must use. + * + * \param[in] LandingPageIndex Index of the URL Descriptor to use as the Landing Page for the device. + * + */ +#define WEBUSB_PLATFORM_DESCRIPTOR(VendorCode, LandingPageIndex) /* WebUSB Platform Descriptor size */ 24, DTYPE_DeviceCapability, DCTYPE_Platform, /* Reserved */ 0, WEBUSB_PLATFORM_UUID, WORD_TO_BYTES_LE(WEBUSB_VERSION), VendorCode, LandingPageIndex + +/** \brief Convenience macro to easily create \ref WebUSB_URL_Descriptor_t instances from a wide character string. + * + * \note This macro is for little-endian systems only. + * + * \param[in] URL URL string to initialize a URL Descriptor structure with. + * + * \note Prefix String literal with u8 to ensure proper conversion: e.g. WEBUSB_URL_DESCRIPTOR(u8"www.google.com") + */ +#define WEBUSB_URL_DESCRIPTOR(URL) \ + { .Header = {.Size = sizeof(WebUSB_URL_Descriptor_t) + (sizeof(URL) - 1), .Type = WebUSB_DTYPE_URL}, .Scheme = (WEBUSB_LANDING_PAGE_PROTOCOL), .UTF8_URL = (URL) } + +/* WebUSB Protocol Data Structures */ +enum WebUSB_Request_t { + WebUSB_RTYPE_GetURL = 2, /**< Indicates the device should return the indicated WebUSB_URL descriptor. */ +}; + +enum WebUSB_Descriptor_t { + WebUSB_DTYPE_URL = 3, /**< Indicates that the descriptor is a URL descriptor. */ +}; + +/** \brief WebUSB URL Descriptor (LUFA naming convention). + * + * Type define for a WebUSB URL Descriptor. This structure uses LUFA-specific element names + * to make each element's purpose clearer. + * + * \note Regardless of CPU architecture, these values should be stored as little endian. + */ +typedef struct { + USB_Descriptor_Header_t Header; /**< Descriptor header, including type (WebUSB_DTYPE_URL) and size. */ + + uint8_t Scheme; /**< URL scheme prefix: 0 means http://, 1 means https://, 255 means included in URL */ + uint8_t UTF8_URL[]; /**< UTF-8 encoded URL (excluding scheme prefix). */ +} ATTR_PACKED WebUSB_URL_Descriptor_t; + +#define MS_OS_20_VENDOR_CODE 0x45 // Must be different than WEBUSB_VENDOR_CODE + +#define MS_OS_20_DESCRIPTOR_CONFIGURATION_HEADER_LENGTH 168 +#define MS_OS_20_DESCRIPTOR_FUNCTION_HEADER_LENGTH 160 +#define MS_OS_20_DESCRIPTOR_SET_TOTAL_LENGTH 178 // Sum of `.Length`s in MS_OS_20_Descriptor in WebUSB.c + +#define MS_OS_20_DESCRIPTOR_COMPATIBILITY_ID { 'W', 'I', 'N', 'U', 'S', 'B', 0, 0 } +#define MS_OS_20_DESCRIPTOR_SUB_COMPATIBILITY_ID {0, 0, 0, 0, 0, 0, 0, 0} + +#define MS_OS_20_PROPERTY_NAME_LENGTH 42 +#define MS_OS_20_PROPERTY_NAME { \ + 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, \ + 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00, 'r', 0x00, 'f', 0x00, \ + 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, \ + 'D', 0x00, 's', 0x00, 0x00, 0x00 \ + } +#define MS_OS_20_PROPERTY_DATA_LENGTH 80 +#define MS_OS_20_PROPERTY_DATA { \ + '{', 0x00, '9', 0x00, 'D', 0x00, 'B', 0x00, '7', 0x00, 'F', 0x00, \ + '1', 0x00, 'F', 0x00, 'D', 0x00, '-', 0x00, '7', 0x00, 'E', 0x00, \ + '7', 0x00, 'A', 0x00, '-', 0x00, '4', 0x00, '0', 0x00, '0', 0x00, \ + '8', 0x00, '-', 0x00, '9', 0x00, '3', 0x00, '6', 0x00, 'B', 0x00, \ + '-', 0x00, '6', 0x00, 'B', 0x00, 'A', 0x00, '6', 0x00, 'C', 0x00, \ + '1', 0x00, '9', 0x00, 'A', 0x00, '3', 0x00, '0', 0x00, '8', 0x00, \ + 'c', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00 \ + } + +#define MS_OS_20_PLATFORM_UUID 0xdf, 0x60, 0xdd, 0xd8, 0x89, 0x45, 0xc7, 0x4c, 0x9c, 0xd2, 0x65, 0x9d, 0x9e, 0x64, 0x8a, 0x9f + +#define MS_OS_20_WINDOWS_VERSION_8_1 0x06030000 // Windows version (8.1) + +#ifndef MS_OS_20_ALTERNATE_ENUMERATION_CODE +# define MS_OS_20_ALTERNATE_ENUMERATION_CODE 0 /**< Set to non-zero to enable Windows to allow device to return alternate USB descriptors. */ +#endif + +/** \brief Convenience macro to easily create device capability platform descriptors for the MS OS 2.0 platform. + * + * \note This macro is designed to be wrapped in parentheses and included in a sequence to the \ref BOS_DESCRIPTOR macro. + * + * \param[in] VendorCode Vendor Code that all control requests coming from Windows must use. + * + * \param[in] TotalLength The length, in bytes, of the MS OS 2.0 descriptor set to be retrieved by Windows. + */ +#define MS_OS_20_PLATFORM_DESCRIPTOR(VendorCode, TotalLength) /* Total size of this descriptor */ 28, DTYPE_DeviceCapability, DCTYPE_Platform, /* Reserved */ 0, MS_OS_20_PLATFORM_UUID, LONG_TO_BYTES_LE(MS_OS_20_WINDOWS_VERSION_8_1), WORD_TO_BYTES_LE(TotalLength), VendorCode, MS_OS_20_ALTERNATE_ENUMERATION_CODE + + /* MS OS 2.0 Descriptors Data Structures */ + enum MS_OS_20_wIndex_t { + MS_OS_20_DESCRIPTOR_INDEX = 0x07, /**< Indicates the device should return MS OS 2.0 Descriptor Set. */ + MS_OS_20_SET_ALT_ENUMERATION = 0x08, /**< Indicates the device may "subsequently return alternate USB descriptors when Windows requests the information." */ + }; + +enum MS_OS_20_Descriptor_Types { + MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00, + MS_OS_20_SUBSET_HEADER_CONFIGURATION = 0x01, + MS_OS_20_SUBSET_HEADER_FUNCTION = 0x02, + MS_OS_20_FEATURE_COMPATBLE_ID = 0x03, + MS_OS_20_FEATURE_REG_PROPERTY = 0x04, + // MS_OS_20_FEATURE_MIN_RESUME_TIME = 0x05, + // MS_OS_20_FEATURE_MODEL_ID = 0x06, + MS_OS_20_FEATURE_CCGP_DEVICE = 0x07, +}; + +/** \brief Microsoft OS 2.0 Descriptor Set Header (LUFA naming convention). + * + * \note Regardless of CPU architecture, these values should be stored as little endian. + */ +typedef struct { + uint16_t Length; /**< The length, in bytes, of this header. Shall be set to 10. */ + uint16_t DescriptorType; /**< Shall be set to MS_OS_20_SET_HEADER_DESCRIPTOR */ + uint32_t WindowsVersion; + uint16_t TotalLength; /**< The size of entire MS OS 2.0 descriptor set. The value shall match the value in the descriptor set information structure. */ +} ATTR_PACKED MS_OS_20_Descriptor_Set_Header_t; + +/** \brief Microsoft OS 2.0 configuration subset header. + * + */ +typedef struct { + uint16_t Length; /**< The length, in bytes, of this subset header. Shall be set to 8. */ + uint16_t DescriptorType; /**< MS_OS_20_SUBSET_HEADER_CONFIGURATION */ + uint8_t ConfigurationValue; /**< The configuration value for the USB configuration to which this subset applies. */ + uint8_t Reserved; /**< Shall be set to 0. */ + uint16_t TotalLength; /**< The size of entire configuration subset including this header. */ +} ATTR_PACKED MS_OS_20_Configuration_Subset_Header; + +/** \brief Microsoft OS 2.0 Function subset header. + * + */ +typedef struct { + uint16_t Length; /**< The length, in bytes, of this subset header. Shall be set to 8. */ + uint16_t DescriptorType; /**< MS_OS_20_SUBSET_HEADER_FUNCTION */ + uint8_t FirstInterface; /**< The interface number for the first interface of the function to which this subset applies. */ + uint8_t Reserved; /**< Shall be set to 0. */ + uint16_t SubsetLength; /**< The size of entire function subset including this header. */ +} ATTR_PACKED MS_OS_20_Function_Subset_Header; + +/** \brief Microsoft OS 2.0 Feature Descriptor for CompatibleID. + * + * These values are used by Windows to locate the appropriate driver for the device. + * + * For WebUSB in Chrome, the CompatibleID needs to be WINUSB, and the SubCompatibleID is null. + * + * \note ID values must be 8 bytes long and contain only the ASCII values for uppercase letters, numbers, underscores, and the NULL character. No other characters are allowed, and the last byte in the ID must be the NULL 0x00. + */ +typedef struct { + uint16_t Length; /**< The length, bytes, of the compatible ID descriptor including value descriptors. Shall be set to 20. */ + uint16_t DescriptorType; /**< MS_OS_20_FEATURE_COMPATIBLE_ID */ + uint8_t CompatibleID[8]; /**< Compatible ID ASCII String */ + uint8_t SubCompatibleID[8]; /**< Sub-compatible ID ASCII String */ +} ATTR_PACKED MS_OS_20_CompatibleID_Descriptor; + +/** \brief Property Data Type values for the Microsoft OS 2.0 Registry Property Descriptor. + * + */ +enum MS_OS_20_Property_Data_Types { + MS_OS_20_REG_SZ = 1, /**< A NULL-terminated Unicode String */ + MS_OS_20_REG_EXPAND_SZ = 2, /**< A NULL-terminated Unicode String that includes environment variables */ + MS_OS_20_REG_BINARY = 3, /**< Free-form binary */ + MS_OS_20_REG_DWORD_LITTLE_ENDIAN = 4, /**< A little-endian 32-bit integer */ + MS_OS_20_REG_DWORD_BIG_ENDIAN = 5, /**< A big-endian 32-bit integer */ + MS_OS_20_REG_LINK = 6, /**< A NULL-terminated Unicode string that contains a symbolic link */ + MS_OS_20_REG_MULTI_SZ = 7 /**< Multiple NULL-terminated Unicode strings */ +}; + +/** \brief Microsoft OS 2.0 Registry Property Descriptor. + * + * This descriptor is used to add per-device or per-function registry values that is read by the Windows USB driver stack or the device’s function driver. + */ +typedef struct { + uint16_t Length; /**< The length in bytes of is descriptor. */ + uint16_t DescriptorType; /**< MS_OS_20_FEATURE_REG_PROPERTY */ + uint16_t PropertyDataType; /**< MS_OS_20_Property_Data_types, MS_OS_20_REG_MULTI_SZ even for single interface because libusb. */ + uint16_t PropertyNameLength; /**< The length of the property name. */ + uint8_t PropertyName[MS_OS_20_PROPERTY_NAME_LENGTH]; /**< The name of registry property as NULL-terminated UTF-16 LE string. */ + uint16_t PropertyDataLength; /**< The length of property data. */ + uint8_t PropertyData[MS_OS_20_PROPERTY_DATA_LENGTH]; /**< Property Data. */ +} ATTR_PACKED MS_OS_20_Registry_Property_Descriptor; + +/** \brief Microsoft OS 2.0 Feature Descriptor for CCGP Devices. + * + * This descriptor indicates that the device should be treated as a composite device by Windows regardless of + * the number of interfaces, configuration, or class, subclass, and protocol codes, the device reports. + * + * \note The CCGP device descriptor must be applied to the entire device. + */ +typedef struct { + uint16_t Length; /**< The length, bytes, of the compatible ID descriptor including value descriptors. Shall be set to 4. */ + uint16_t DescriptorType; /**< MS_OS_20_FEATURE_CCGP_DEVICE */ +} ATTR_PACKED MS_OS_20_CCGP_Device_Descriptor; + +typedef struct { + MS_OS_20_Descriptor_Set_Header_t Header; + MS_OS_20_Configuration_Subset_Header ConfigurationSubsetHeader; + MS_OS_20_Function_Subset_Header FunctionSubsetHeader; + MS_OS_20_CompatibleID_Descriptor CompatibleID; + MS_OS_20_Registry_Property_Descriptor RegistryProperty; +} MS_OS_20_Descriptor_t; + +#define MS_OS_20_DESCRIPTOR { \ + .Header = { \ + .Length = CPU_TO_LE16(10), \ + .DescriptorType = CPU_TO_LE16(MS_OS_20_SET_HEADER_DESCRIPTOR), \ + .WindowsVersion = MS_OS_20_WINDOWS_VERSION_8_1, \ + .TotalLength = CPU_TO_LE16(MS_OS_20_DESCRIPTOR_SET_TOTAL_LENGTH) \ + }, \ + .ConfigurationSubsetHeader = { \ + .Length = CPU_TO_LE16(8), \ + .DescriptorType = CPU_TO_LE16(MS_OS_20_SUBSET_HEADER_CONFIGURATION), \ + .ConfigurationValue = 0, \ + .Reserved = 0, \ + .TotalLength = CPU_TO_LE16(MS_OS_20_DESCRIPTOR_CONFIGURATION_HEADER_LENGTH) \ + }, \ + .FunctionSubsetHeader = { \ + .Length = CPU_TO_LE16(8), \ + .DescriptorType = CPU_TO_LE16(MS_OS_20_SUBSET_HEADER_FUNCTION), \ + .FirstInterface = INTERFACE_ID_WebUSB, \ + .Reserved = 0, \ + .SubsetLength = CPU_TO_LE16(MS_OS_20_DESCRIPTOR_FUNCTION_HEADER_LENGTH) \ + }, \ + .CompatibleID = { \ + .Length = CPU_TO_LE16(20), \ + .DescriptorType = CPU_TO_LE16(MS_OS_20_FEATURE_COMPATBLE_ID), \ + .CompatibleID = MS_OS_20_DESCRIPTOR_COMPATIBILITY_ID, \ + .SubCompatibleID = MS_OS_20_DESCRIPTOR_SUB_COMPATIBILITY_ID \ + }, \ + .RegistryProperty = { \ + .Length = CPU_TO_LE16(132), \ + .DescriptorType = CPU_TO_LE16(MS_OS_20_FEATURE_REG_PROPERTY), \ + .PropertyDataType = CPU_TO_LE16(MS_OS_20_REG_MULTI_SZ), \ + .PropertyNameLength = CPU_TO_LE16(MS_OS_20_PROPERTY_NAME_LENGTH), \ + .PropertyName = MS_OS_20_PROPERTY_NAME, \ + .PropertyDataLength = CPU_TO_LE16(MS_OS_20_PROPERTY_DATA_LENGTH), \ + .PropertyData = MS_OS_20_PROPERTY_DATA \ + } \ +} + |