Skip to content

Commit

Permalink
stm32/usb: Allow using CDC in WinUSB mode on windows.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
pi-anl committed Nov 10, 2023
1 parent 2428f78 commit d93d990
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
18 changes: 18 additions & 0 deletions ports/stm32/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ __ALIGN_BEGIN static const uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __AL
HIBYTE(MICROPY_HW_USB_LANGID_STRING),
};


__ALIGN_BEGIN static const uint8_t msft100[18] __ALIGN_END = {
0x12, 0x03,
'M', 0x00,
'S', 0x00,
'F', 0x00,
'T', 0x00,
'1', 0x00,
'0', 0x00,
'0', 0x00,
USB_MSFT100_VENDOR_CODE,
0x00,
};

// set the VID, PID and device release number
void USBD_SetVIDPIDRelease(usbd_cdc_msc_hid_state_t *usbd, uint16_t vid, uint16_t pid, uint16_t device_release_num, int cdc_only) {
uint8_t *dev_desc = &usbd->usbd_device_desc[0];
Expand Down Expand Up @@ -174,6 +188,10 @@ STATIC uint8_t *USBD_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, uint16
break;
#endif

case 0xee:
*length = sizeof(msft100);
return (uint8_t *)msft100; // the data should only be read from this buf

default:
// invalid string index
return NULL;
Expand Down
31 changes: 28 additions & 3 deletions ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@
#define CONFIG_DESC_MAXPOWER (0xfa) // 500mA in units of 2mA
#endif


__ALIGN_BEGIN static const uint8_t msft100_id[40] __ALIGN_END = {
0x28, 0x00, 0x00, 0x00,
0x00, 0x01, // 1.00
0x04, 0x00,
0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0x01,
'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

#if USBD_SUPPORT_HS_MODE
// USB Standard Device Descriptor
__ALIGN_BEGIN static uint8_t USBD_CDC_MSC_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = {
Expand Down Expand Up @@ -205,9 +219,9 @@ static const uint8_t cdc_class_desc_data[CDC_CLASS_DESC_SIZE] = {
USB_DESC_TYPE_ASSOCIATION, // bDescriptorType: IAD
0x00, // bFirstInterface: first interface for this association -- to be filled in
0x02, // bInterfaceCount: number of interfaces for this association
0x02, // bFunctionClass: Communication Interface Class
0x02, // bFunctionSubClass: Abstract Control Model
0x01, // bFunctionProtocol: Common AT commands
0x00, // bFunctionClass: Communication Interface Class
0x00, // bFunctionSubClass: Abstract Control Model
0x00, // bFunctionProtocol: Common AT commands
0x00, // iFunction: index of string for this function

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -827,6 +841,17 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp
uint8_t mode = usbd->usbd_mode;
uint8_t recipient = 0;
usbd_cdc_state_t *cdc = NULL;
if ((req->bmRequest & USB_REQ_MSFT_MASK) == USB_REQ_MSFT_REQ) {
// device-to-host vendor request
if (req->wIndex == 0x04 && req->bRequest == USB_MSFT100_VENDOR_CODE) {
// Compatible ID Feature Descriptor
int len = MIN(req->wLength, 40);
// memcpy(self->tx_buf, msft100_id, len);
USBD_CtlSendData(pdev, (uint8_t *)msft100_id, len);
return USBD_OK;
}
}

switch (req->bmRequest & USB_REQ_RECIPIENT_MASK) {
case USB_REQ_RECIPIENT_INTERFACE: {
uint16_t iface = req->wIndex;
Expand Down
5 changes: 5 additions & 0 deletions ports/stm32/usbdev/core/inc/usbd_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
#define USB_REQ_SET_INTERFACE 0x0B
#define USB_REQ_SYNCH_FRAME 0x0C

#define USB_REQ_MSFT_MASK 0xE0
#define USB_REQ_MSFT_REQ 0xC0
// Vendor code, can be anything.
#define USB_MSFT100_VENDOR_CODE 0x42

#define USB_DESC_TYPE_DEVICE 1
#define USB_DESC_TYPE_CONFIGURATION 2
#define USB_DESC_TYPE_STRING 3
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/usbdev/core/src/usbd_ctlreq.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev , USBD_SetupReqType
{
USBD_StatusTypeDef ret = USBD_OK;

if ((req->bmRequest & 0xe0) == 0xc0) {
if ((req->bmRequest & USB_REQ_MSFT_MASK) == USB_REQ_MSFT_REQ) {
return pdev->pClass->Setup (pdev, req);
}

Expand Down

0 comments on commit d93d990

Please sign in to comment.