chore: code cleanup and refactors

This commit is contained in:
Florian Didron
2019-11-04 16:30:23 +09:00
parent 8e142defed
commit 04113a76ac
4 changed files with 65 additions and 97 deletions

View File

@@ -31,10 +31,6 @@
#include "usb_descriptor.h" #include "usb_descriptor.h"
#include "usb_driver.h" #include "usb_driver.h"
#ifdef WEBUSB_ENABLE
# include "webusb.h"
#endif
#ifdef NKRO_ENABLE #ifdef NKRO_ENABLE
# include "keycode_config.h" # include "keycode_config.h"
@@ -159,44 +155,18 @@ static const USBEndpointConfig shared_ep_config = {
#endif #endif
#ifdef WEBUSB_ENABLE #ifdef WEBUSB_ENABLE
const MS_OS_20_Descriptor_t PROGMEM MS_OS_20_Descriptor = { /** Microsoft OS 2.0 Descriptor. This is used by Windows to select the USB driver for the device.
.Header = { *
.Length = CPU_TO_LE16(10), * For WebUSB in Chrome, the correct driver is WinUSB, which is selected via CompatibleID.
.DescriptorType = CPU_TO_LE16(MS_OS_20_SET_HEADER_DESCRIPTOR), *
.WindowsVersion = MS_OS_20_WINDOWS_VERSION_8_1, * Additionally, while Chrome is built using libusb, a magic registry key needs to be set containing a GUID for
.TotalLength = CPU_TO_LE16(MS_OS_20_DESCRIPTOR_SET_TOTAL_LENGTH) * the device.
}, */
.ConfigurationSubsetHeader = { const MS_OS_20_Descriptor_t PROGMEM MS_OS_20_Descriptor = MS_OS_20_DESCRIPTOR;
.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
}
};
/** 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); const WebUSB_URL_Descriptor_t PROGMEM WebUSB_LandingPage = WEBUSB_URL_DESCRIPTOR(WEBUSB_LANDING_PAGE_URL);
#endif #endif

View File

@@ -269,7 +269,6 @@ static void Console_Task(void) {
#endif #endif
#ifdef WEBUSB_ENABLE #ifdef WEBUSB_ENABLE
void webusb_send(uint8_t *data, uint8_t length) { void webusb_send(uint8_t *data, uint8_t length) {
if (USB_DeviceState != DEVICE_STATE_Configured) { if (USB_DeviceState != DEVICE_STATE_Configured) {
return; return;
@@ -320,43 +319,7 @@ static void webusb_task(void) {
* Additionally, while Chrome is built using libusb, a magic registry key needs to be set containing a GUID for * Additionally, while Chrome is built using libusb, a magic registry key needs to be set containing a GUID for
* the device. * the device.
*/ */
const MS_OS_20_Descriptor_t PROGMEM MS_OS_20_Descriptor = { const MS_OS_20_Descriptor_t PROGMEM MS_OS_20_Descriptor = 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
}
};
/** URL descriptor string. This is a UTF-8 string containing a URL excluding the prefix. At least one of these must be /** 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. * defined and returned when the Landing Page descriptor index is requested.
@@ -502,8 +465,11 @@ void EVENT_USB_Device_ConfigurationChanged(void) {
# endif # endif
#endif #endif
#ifdef WEBUSB_ENABLE
/* Setup Webusb Endpoints */
ConfigSuccess &= Endpoint_ConfigureEndpoint(WEBUSB_IN_EPADDR, EP_TYPE_INTERRUPT, WEBUSB_EPSIZE, 1); ConfigSuccess &= Endpoint_ConfigureEndpoint(WEBUSB_IN_EPADDR, EP_TYPE_INTERRUPT, WEBUSB_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint(WEBUSB_OUT_EPADDR, EP_TYPE_INTERRUPT, WEBUSB_EPSIZE, 1); ConfigSuccess &= Endpoint_ConfigureEndpoint(WEBUSB_OUT_EPADDR, EP_TYPE_INTERRUPT, WEBUSB_EPSIZE, 1);
#endif
#ifdef MIDI_ENABLE #ifdef MIDI_ENABLE
ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
@@ -636,6 +602,7 @@ void EVENT_USB_Device_ControlRequest(void) {
} }
break; break;
#ifdef WEBUSB_ENABLE
case WEBUSB_VENDOR_CODE: case WEBUSB_VENDOR_CODE:
if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE)) { if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_VENDOR | REQREC_DEVICE)) {
switch (USB_ControlRequest.wIndex) { switch (USB_ControlRequest.wIndex) {
@@ -675,6 +642,7 @@ void EVENT_USB_Device_ControlRequest(void) {
} }
} }
break; break;
#endif
} }
#ifdef VIRTSER_ENABLE #ifdef VIRTSER_ENABLE

View File

@@ -40,7 +40,9 @@
#include "report.h" #include "report.h"
#include "usb_descriptor.h" #include "usb_descriptor.h"
#include "print.h" #include "print.h"
#ifdef WEBUSB_ENABLE
# include "webusb.h"
#endif
/* /*
* HID report descriptors * HID report descriptors
*/ */
@@ -275,10 +277,12 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM ConsoleReport[] = {
}; };
#endif #endif
#ifdef WEBUSB_ENABLE
const USB_Descriptor_BOS_t PROGMEM BOSDescriptor = BOS_DESCRIPTOR( 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)) (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)) (WEBUSB_PLATFORM_DESCRIPTOR(WEBUSB_VENDOR_CODE, WEBUSB_LANDING_PAGE_INDEX))
); );
#endif
/* /*
@@ -555,10 +559,12 @@ uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const
Size = sizeof(USB_Descriptor_Device_t); Size = sizeof(USB_Descriptor_Device_t);
break; break;
case DTYPE_BOS: #ifdef WEBUSB_ENABLE
Address = &BOSDescriptor; case DTYPE_BOS:
Size = pgm_read_byte(&BOSDescriptor.TotalLength); Address = &BOSDescriptor;
break; Size = pgm_read_byte(&BOSDescriptor.TotalLength);
break;
#endif
case DTYPE_Configuration: case DTYPE_Configuration:
Address = &ConfigurationDescriptor; Address = &ConfigurationDescriptor;

View File

@@ -215,17 +215,41 @@ typedef struct {
MS_OS_20_Registry_Property_Descriptor RegistryProperty; MS_OS_20_Registry_Property_Descriptor RegistryProperty;
} MS_OS_20_Descriptor_t; } MS_OS_20_Descriptor_t;
typedef struct { #define MS_OS_20_DESCRIPTOR { \
MS_OS_20_Descriptor_Set_Header_t Header; .Header = { \
MS_OS_20_Configuration_Subset_Header ConfigurationSubsetHeader; .Length = CPU_TO_LE16(10), \
MS_OS_20_Function_Subset_Header FunctionSubsetHeader; .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 \
} \
}
} MS_OS_20_Descriptor_Prefix_t;
typedef struct {
MS_OS_20_CompatibleID_Descriptor CompatibleID;
} MS_OS_20_Descriptor_Suffix_t;
typedef struct {
MS_OS_20_Registry_Property_Descriptor RegistryProperty;
} MS_OS_20_Descriptor_Custom_Property_t;