Skip to content

Commit

Permalink
Microsoft WCID support in DFU mode
Browse files Browse the repository at this point in the history
  • Loading branch information
avtolstoy committed Jun 11, 2016
1 parent 9d741f5 commit db8a6ab
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
27 changes: 26 additions & 1 deletion bootloader/src/stm32f2xx/dfu_usb.c
Expand Up @@ -38,6 +38,7 @@ USB_OTG_CORE_HANDLE USB_OTG_dev;

/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static uint8_t HAL_DFU_USB_Handle_Vendor_Request(USB_SETUP_REQ* req, uint8_t dataStage);
/* Private functions ---------------------------------------------------------*/

uint8_t is_application_valid(uint32_t address)
Expand All @@ -49,6 +50,21 @@ uint8_t is_application_valid(uint32_t address)
#endif
}

static void dummy(void) {}
static void dummy1(uint8_t speed) {}

USBD_Usr_cb_TypeDef DFU_USR_cb =
{
dummy, // USBD_USR_Init,
dummy1, // USBD_USR_DeviceReset,
dummy, // USBD_USR_DeviceConfigured,
dummy, // USBD_USR_DeviceSuspended,
dummy, // USBD_USR_DeviceResumed,
dummy, // USBD_USR_DeviceConnected,
dummy, // USBD_USR_DeviceDisconnected,
HAL_DFU_USB_Handle_Vendor_Request
};

/*******************************************************************************
* Function Name : HAL_DFU_USB_Init.
* Description : Initialize USB for DFU class.
Expand All @@ -65,5 +81,14 @@ void HAL_DFU_USB_Init(void)
USB_OTG_HS_CORE_ID,
#endif
&USR_desc, &DFU_cb,
NULL); // Passing NULL here to reduce bootloader flash requirements
&DFU_USR_cb); // Passing NULL here to reduce bootloader flash requirements
}

uint8_t HAL_DFU_USB_Handle_Vendor_Request(USB_SETUP_REQ* req, uint8_t dataStage) {
// Forward to DFU class driver
if (req != NULL && req->bRequest == 0xee && req->wIndex == 0x0004 && req->wValue == 0x0000) {
return DFU_cb.Setup(&USB_OTG_dev, req);
}

return USBD_FAIL;
}
26 changes: 26 additions & 0 deletions bootloader/src/stm32f2xx/usbd_desc.c
Expand Up @@ -31,6 +31,7 @@
#include "usbd_desc.h"
#include "usbd_req.h"
#include "usb_regs.h"
#include "usbd_wcid.h"

#include "deviceid_hal.h"
extern char* bytes2hexbuf(const uint8_t* buf, unsigned len, char* out);
Expand All @@ -49,6 +50,8 @@ extern char* bytes2hexbuf(const uint8_t* buf, unsigned len, char* out);
#define USBD_CONFIGURATION_STRING "DFU"
#define USBD_INTERFACE_STRING "DFU"

static uint8_t * USBD_USR_MsftStrDescriptor( uint8_t speed , uint16_t *length);

USBD_DEVICE USR_desc =
{
USBD_USR_DeviceDescriptor,
Expand All @@ -58,6 +61,7 @@ USBD_DEVICE USR_desc =
USBD_USR_SerialStrDescriptor,
USBD_USR_ConfigStrDescriptor,
USBD_USR_InterfaceStrDescriptor,
USBD_USR_MsftStrDescriptor
};

/* USB Standard Device Descriptor */
Expand Down Expand Up @@ -107,6 +111,15 @@ uint8_t USBD_LangIDDesc[USB_SIZ_STRING_LANGID] =
HIBYTE(USBD_LANGID_STRING),
};

/* MS OS String Descriptor */
static const uint8_t USBD_MsftStrDesc[] = {
USB_WCID_MS_OS_STRING_DESCRIPTOR(
// "MSFT100"
USB_WCID_DATA('M', '\0', 'S', '\0', 'F', '\0', 'T', '\0', '1', '\0', '0', '\0', '0', '\0'),
0xee
)
};

/**
* @brief USBD_USR_DeviceDescriptor
* return the device descriptor
Expand Down Expand Up @@ -202,3 +215,16 @@ uint8_t * USBD_USR_InterfaceStrDescriptor( uint8_t speed , uint16_t *length)
USBD_GetString (USBD_INTERFACE_STRING, USBD_StrDesc, length);
return USBD_StrDesc;
}

/**
* @brief USBD_USR_MsftStrDescriptor
* return MS OS String Descriptor
* @param speed : current device speed
* @param length : pointer to data length variable
* @retval pointer to descriptor buffer
*/
uint8_t * USBD_USR_MsftStrDescriptor( uint8_t speed , uint16_t *length)
{
*length = sizeof(USBD_MsftStrDesc);
return (uint8_t*)USBD_MsftStrDesc;
}
Expand Up @@ -67,7 +67,7 @@
#include "usbd_req.h"
#include "usb_bsp.h"
#include "usbd_dfu_mal.h"

#include "usbd_wcid.h"

/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
* @{
Expand Down Expand Up @@ -390,6 +390,33 @@ __ALIGN_BEGIN static uint8_t usbd_dfu_Desc[USB_DFU_DESC_SIZ] __ALIGN_END =
};
#endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */

/* Extended Compat ID OS Descriptor */
static const uint8_t USBD_DFU_MsftExtCompatIdOsDescr[] = {
USB_WCID_EXT_COMPAT_ID_OS_DESCRIPTOR(
0x00,
USB_WCID_DATA('W', 'I', 'N', 'U', 'S', 'B', '\0', '\0'),
USB_WCID_DATA(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
)
};

/* Extended Properties OS Descriptor */
static const uint8_t USBD_DFU_MsftExtPropOsDescr[] = {
USB_WCID_EXT_PROP_OS_DESCRIPTOR(
USB_WCID_DATA(
/* bPropertyData "{37fb5f90-1a34-4929-933b-8a27e1850033}" */
'{', 0x00, '3', 0x00, '7', 0x00, 'f', 0x00, 'b', 0x00,
'5', 0x00, 'f', 0x00, '9', 0x00, '0', 0x00, '-', 0x00,
'1', 0x00, 'a', 0x00, '3', 0x00, '4', 0x00, '-', 0x00,
'4', 0x00, '9', 0x00, '2', 0x00, '9', 0x00, '-', 0x00,
'9', 0x00, '3', 0x00, '3', 0x00, 'b', 0x00, '-', 0x00,
'8', 0x00, 'a', 0x00, '2', 0x00, '7', 0x00, 'e', 0x00,
'1', 0x00, '8', 0x00, '5', 0x00, '0', 0x00, '0', 0x00,
'3', 0x00, '3', 0x00, '}'
)
)
};


/**
* @}
*/
Expand Down Expand Up @@ -442,6 +469,25 @@ static uint8_t usbd_dfu_DeInit (void *pdev,
return USBD_OK;
}

uint8_t USBD_DFU_Handle_Msft_Request(void* pdev, USB_SETUP_REQ* req) {
if (req->wIndex == 0x0004) {
USBD_CtlSendData(pdev, USBD_DFU_MsftExtCompatIdOsDescr, req->wLength);
} else if (req->wIndex == 0x0005) {
if ((req->wValue & 0xff) == 0x00) {
USBD_CtlSendData(pdev, USBD_DFU_MsftExtPropOsDescr, req->wLength);
} else {
// Send dummy
uint8_t dummy[10] = {0};
USBD_CtlSendData(pdev, dummy, req->wLength);
}
} else {
return USBD_FAIL;
}

return USBD_OK;
}


/**
* @brief usbd_dfu_Setup
* Handles the DFU request parsing.
Expand All @@ -455,6 +501,11 @@ static uint8_t usbd_dfu_Setup (void *pdev,
uint16_t len = 0;
uint8_t *pbuf = NULL;

if ((req->bRequest == 0xee && req->bmRequest == 0b11000001 && req->wIndex == 0x0005) ||
(req->bRequest == 0xee && req->bmRequest == 0xc0 && req->wIndex == 0x0004)) {
return USBD_DFU_Handle_Msft_Request(pdev, req);
}

switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
/* DFU Class Requests -------------------------------*/
Expand Down

0 comments on commit db8a6ab

Please sign in to comment.