diff options
author | Florian Didron <fd@librem.one> | 2019-11-04 18:50:06 +0900 |
---|---|---|
committer | Drashna Jael're <drashna@live.com> | 2019-12-05 16:03:53 -0800 |
commit | ea6f99e05dbc617354fb5c503b31d46c8bf5cb18 (patch) | |
tree | 1aaa52dc94bd351a0654cf977c9f33240a700caf | |
parent | 5400211a9adf17e328f4d6962ddbf18c8f9026d9 (diff) |
feat: adds webusb
-rw-r--r-- | .gitmodules | 2 | ||||
-rw-r--r-- | keyboards/ergodox_ez/config.h | 1 | ||||
-rw-r--r-- | keyboards/planck/ez/config.h | 5 | ||||
-rw-r--r-- | tmk_core/common.mk | 4 | ||||
-rw-r--r-- | tmk_core/protocol/chibios/usb_main.c | 70 | ||||
-rw-r--r-- | tmk_core/protocol/lufa/lufa.c | 111 | ||||
-rw-r--r-- | tmk_core/protocol/usb_descriptor.c | 52 | ||||
-rw-r--r-- | tmk_core/protocol/usb_descriptor.h | 21 | ||||
-rw-r--r-- | tmk_core/protocol/webusb.h | 255 |
9 files changed, 518 insertions, 3 deletions
diff --git a/.gitmodules b/.gitmodules index f2e96bedc8..10ecd53e41 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,4 +13,4 @@ url = https://github.com/google/googletest [submodule "lib/lufa"] path = lib/lufa - url = https://github.com/qmk/lufa + url = https://github.com/drashna/lufa diff --git a/keyboards/ergodox_ez/config.h b/keyboards/ergodox_ez/config.h index 2ff452c400..dc6b7fafbf 100644 --- a/keyboards/ergodox_ez/config.h +++ b/keyboards/ergodox_ez/config.h @@ -28,6 +28,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define MANUFACTURER ErgoDox EZ #define PRODUCT ErgoDox EZ #define DESCRIPTION QMK keyboard firmware for Ergodox EZ +#define WEBUSB_LANDING_PAGE_URL u8"configure.ergodox-ez.com" /* key matrix size */ #define MATRIX_ROWS 14 diff --git a/keyboards/planck/ez/config.h b/keyboards/planck/ez/config.h index df27e7b629..c4effbc15f 100644 --- a/keyboards/planck/ez/config.h +++ b/keyboards/planck/ez/config.h @@ -18,7 +18,12 @@ #pragma once /* USB Device descriptor parameter */ +#undef MANUFACTURER +#define MANUFACTURER ErgoDox EZ +#undef PRODUCT +#define PRODUCT Planck EZ #define DEVICE_VER 0x0000 +#define WEBUSB_LANDING_PAGE_URL u8"configure.ergodox-ez.com" #undef MATRIX_ROWS #undef MATRIX_COLS diff --git a/tmk_core/common.mk b/tmk_core/common.mk index db55353465..a32abd8ebc 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -118,6 +118,10 @@ ifeq ($(strip $(RAW_ENABLE)), yes) TMK_COMMON_DEFS += -DRAW_ENABLE endif +ifeq ($(strip $(WEBUSB_ENABLE)), yes) + TMK_COMMON_DEFS += -DWEBUSB_ENABLE +endif + ifeq ($(strip $(CONSOLE_ENABLE)), yes) TMK_COMMON_DEFS += -DCONSOLE_ENABLE else diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index c8a6bbb43f..e2440601e3 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -151,6 +151,22 @@ 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 + typedef struct { size_t queue_capacity_in; size_t queue_capacity_out; @@ -233,6 +249,9 @@ typedef struct { #ifdef VIRTSER_ENABLE usb_driver_config_t serial_driver; #endif +#ifdef WEBUSB_ENABLE + usb_driver_config_t webusb_driver; +#endif }; usb_driver_config_t array[0]; }; @@ -269,6 +288,14 @@ static usb_driver_configs_t drivers = { # define CDC_OUT_MODE USB_EP_MODE_TYPE_BULK .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 }; #define NUM_USB_DRIVERS (sizeof(drivers) / sizeof(usb_driver_config_t)) @@ -496,6 +523,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])); @@ -817,6 +865,28 @@ void raw_hid_task(void) { #endif +#ifdef WEBUSB_ENABLE +void webusb_send(uint8_t *data, uint8_t length) { chnWrite(&drivers.webusb_driver.driver, data, length); } + +__attribute__((weak)) void webusb_receive(uint8_t *data, uint8_t length) { + // 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 12a6924fd0..998db1c6d8 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -268,6 +268,65 @@ 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_IsINReady()) { + Endpoint_Write_Stream_LE(data, length, NULL); + Endpoint_ClearIN(); + } +} + +__attribute__((weak)) void webusb_receive(uint8_t *data, uint8_t length) { } + +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 + /******************************************************************************* * USB Events ******************************************************************************/ @@ -405,6 +464,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 ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); @@ -536,6 +601,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 @@ -1018,6 +1125,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 ffcf0957f3..dff3d48bad 100644 --- a/tmk_core/protocol/usb_descriptor.c +++ b/tmk_core/protocol/usb_descriptor.c @@ -39,7 +39,9 @@ #include "util.h" #include "report.h" #include "usb_descriptor.h" - +#ifdef WEBUSB_ENABLE +#include "webusb.h" +#endif /* * HID report descriptors */ @@ -274,11 +276,18 @@ 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 + /* * Device descriptor */ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor = {.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, - .USBSpecification = VERSION_BCD(1, 1, 0), + .USBSpecification = VERSION_BCD(2, 1, 0), #if VIRTSER_ENABLE .Class = USB_CSCP_IADDeviceClass, .SubClass = USB_CSCP_IADDeviceSubclass, @@ -381,6 +390,38 @@ const USB_Descriptor_Configuration_t PROGMEM .Console_OUTEndpoint = {.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint}, .EndpointAddress = (ENDPOINT_DIR_OUT | CONSOLE_OUT_EPNUM), .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA), .EndpointSize = CONSOLE_EPSIZE, .PollingIntervalMS = 0x01}, #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 @@ -520,6 +561,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 b2423fa7e6..224068b674 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.h" +#endif /* * USB descriptor structure @@ -92,6 +95,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 @@ -164,6 +173,10 @@ enum usb_interfaces { CDI_INTERFACE, #endif +#ifdef WEBUSB_ENABLE + INTERFACE_ID_WebUSB, +#endif + TOTAL_INTERFACES }; @@ -224,6 +237,13 @@ enum usb_endpoints { # define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM) # define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM) #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 PROTOCOL_LUFA @@ -248,6 +268,7 @@ enum usb_endpoints { #define MIDI_STREAM_EPSIZE 64 #define CDC_NOTIFICATION_EPSIZE 8 #define CDC_EPSIZE 16 +#define WEBUSB_EPSIZE 64 uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const void** const DescriptorAddress); #endif diff --git a/tmk_core/protocol/webusb.h b/tmk_core/protocol/webusb.h new file mode 100644 index 0000000000..c0b45c758e --- /dev/null +++ b/tmk_core/protocol/webusb.h @@ -0,0 +1,255 @@ +#pragma once + +void webusb_receive(uint8_t *data, uint8_t length); + +void webusb_send(uint8_t *data, uint8_t length); + +#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 */ + +#define WEBUSB_LANDING_PAGE_INDEX 1 + +#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 u8"WINUSB\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 u8"DeviceInterfaceGUIDs\0" +#define MS_OS_20_PROPERTY_DATA_LENGTH 80 +#define MS_OS_20_PROPERTY_DATA u8"{9D32F82C-1FB2-4486-8501-B6145B5BA336}\0\0" + +#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 \ + } \ +} + |