From 39ce2db181ec1e06426e2a3a371d2cd535723a17 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Feb 2015 20:52:48 +0000 Subject: [PATCH] stmhal: Add "CDC" option to pyb.usb_mode, for CDC device only. --- stmhal/Makefile | 4 +- stmhal/main.c | 2 +- stmhal/usb.c | 12 +- stmhal/usb.h | 7 +- stmhal/{usbd_desc_cdc_msc.c => usbd_desc.c} | 45 ++-- stmhal/usbd_desc.h | 4 +- stmhal/usbdev/class/src/usbd_cdc_msc_hid.c | 275 +++++++++++++++----- tools/insert-usb-ids.py | 2 +- 8 files changed, 252 insertions(+), 99 deletions(-) rename stmhal/{usbd_desc_cdc_msc.c => usbd_desc.c} (80%) diff --git a/stmhal/Makefile b/stmhal/Makefile index e1231f143..5298ef283 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -94,7 +94,7 @@ SRC_C = \ system_stm32f4xx.c \ stm32f4xx_it.c \ usbd_conf.c \ - usbd_desc_cdc_msc.c \ + usbd_desc.c \ usbd_cdc_interface.c \ usbd_msc_storage.c \ mphal.c \ @@ -281,7 +281,7 @@ GEN_PINS_AF_PY = $(BUILD)/pins_af.py INSERT_USB_IDS = ../tools/insert-usb-ids.py FILE2H = ../tools/file2h.py -USB_IDS_FILE = usbd_desc_cdc_msc.c +USB_IDS_FILE = usbd_desc.c CDCINF_TEMPLATE = pybcdc.inf_template GEN_CDCINF_FILE = $(HEADER_BUILD)/pybcdc.inf GEN_CDCINF_HEADER = $(HEADER_BUILD)/pybcdc_inf.h diff --git a/stmhal/main.c b/stmhal/main.c index 5938c7e36..d2aaf89ac 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -473,7 +473,7 @@ soft_reset: #if defined(USE_DEVICE_MODE) // init USB device to default setting if it was not already configured if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) { - pyb_usb_dev_init(USBD_PID_DEFAULT, USBD_MODE_CDC_MSC, NULL); + pyb_usb_dev_init(USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL); } #endif diff --git a/stmhal/usb.c b/stmhal/usb.c index 220d01c4e..b6b9c9664 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2013, 2014, 2015 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -60,7 +60,7 @@ const mp_obj_tuple_t pyb_usb_hid_mouse_obj = { {&mp_type_tuple}, 5, { - MP_OBJ_NEW_SMALL_INT(USBD_PID_SECONDARY), + MP_OBJ_NEW_SMALL_INT(USBD_PID_CDC_HID), MP_OBJ_NEW_SMALL_INT(1), // subclass: boot MP_OBJ_NEW_SMALL_INT(2), // protocol: mouse MP_OBJ_NEW_SMALL_INT(USBD_HID_MOUSE_MAX_PACKET), @@ -79,7 +79,7 @@ const mp_obj_tuple_t pyb_usb_hid_keyboard_obj = { {&mp_type_tuple}, 5, { - MP_OBJ_NEW_SMALL_INT(USBD_PID_SECONDARY), + MP_OBJ_NEW_SMALL_INT(USBD_PID_CDC_HID), MP_OBJ_NEW_SMALL_INT(1), // subclass: boot MP_OBJ_NEW_SMALL_INT(1), // protocol: keyboard MP_OBJ_NEW_SMALL_INT(USBD_HID_KEYBOARD_MAX_PACKET), @@ -100,7 +100,7 @@ void pyb_usb_dev_init(uint16_t pid, usb_device_mode_t mode, USBD_HID_ModeInfoTyp // only init USB once in the device's power-lifetime USBD_SetPID(pid); USBD_SelectMode(mode, hid_info); - USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&VCP_Desc, 0); + USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&USBD_Descriptors, 0); USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID); USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops); switch (pyb_usb_storage_medium) { @@ -213,7 +213,7 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *args) { #elif defined(USE_DEVICE_MODE) // USB device if (strcmp(mode_str, "CDC+MSC") == 0) { - pyb_usb_dev_init(USBD_PID_DEFAULT, USBD_MODE_CDC_MSC, NULL); + pyb_usb_dev_init(USBD_PID_CDC_MSC, USBD_MODE_CDC_MSC, NULL); } else if (strcmp(mode_str, "CDC+HID") == 0) { mp_obj_t hid_info_obj = (mp_obj_t)&pyb_usb_hid_mouse_obj; // default is mouse mode if (n_args == 2) { @@ -232,6 +232,8 @@ STATIC mp_obj_t pyb_usb_mode(mp_uint_t n_args, const mp_obj_t *args) { hid_info.report_desc = bufinfo.buf; hid_info.report_desc_len = bufinfo.len; pyb_usb_dev_init(pid, USBD_MODE_CDC_HID, &hid_info); + } else if (strcmp(mode_str, "CDC") == 0) { + pyb_usb_dev_init(USBD_PID_CDC, USBD_MODE_CDC, NULL); } else { goto bad_mode; } diff --git a/stmhal/usb.h b/stmhal/usb.h index 4ac51f322..70441c578 100644 --- a/stmhal/usb.h +++ b/stmhal/usb.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2013, 2014, 2015 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,8 +30,9 @@ #define PYB_USB_FLAG_USB_MODE_CALLED (0x0002) // Windows needs a different PID to distinguish different device configurations -#define USBD_PID_DEFAULT (0x9800) -#define USBD_PID_SECONDARY (0x9801) +#define USBD_PID_CDC_MSC (0x9800) +#define USBD_PID_CDC_HID (0x9801) +#define USBD_PID_CDC (0x9802) typedef enum { PYB_USB_STORAGE_MEDIUM_NONE = 0, diff --git a/stmhal/usbd_desc_cdc_msc.c b/stmhal/usbd_desc.c similarity index 80% rename from stmhal/usbd_desc_cdc_msc.c rename to stmhal/usbd_desc.c index 3556d2bf8..c893b4c66 100644 --- a/stmhal/usbd_desc_cdc_msc.c +++ b/stmhal/usbd_desc.c @@ -43,20 +43,21 @@ #define USBD_SERIALNUMBER_HS_STRING "000000000010" #define USBD_PRODUCT_FS_STRING "Pyboard Virtual Comm Port in FS Mode" #define USBD_SERIALNUMBER_FS_STRING "000000000011" -#define USBD_CONFIGURATION_HS_STRING "VCP Config" -#define USBD_INTERFACE_HS_STRING "VCP Interface" -#define USBD_CONFIGURATION_FS_STRING "VCP Config" -#define USBD_INTERFACE_FS_STRING "VCP Interface" +#define USBD_CONFIGURATION_HS_STRING "Pyboard Config" +#define USBD_INTERFACE_HS_STRING "Pyboard Interface" +#define USBD_CONFIGURATION_FS_STRING "Pyboard Config" +#define USBD_INTERFACE_FS_STRING "Pyboard Interface" // USB Standard Device Descriptor +// needs to be in RAM because we modify the VID and PID __ALIGN_BEGIN static uint8_t hUSBDDeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = { 0x12, // bLength USB_DESC_TYPE_DEVICE, // bDescriptorType 0x00, // bcdUSB 0x02, - 0x00, // bDeviceClass - 0x00, // bDeviceSubClass - 0x00, // bDeviceProtocol + 0xef, // bDeviceClass: Miscellaneous Device Class + 0x02, // bDeviceSubClass: Common Class + 0x01, // bDeviceProtocol: Interface Association Descriptor USB_MAX_EP0_SIZE, // bMaxPacketSize LOBYTE(USBD_VID), // idVendor HIBYTE(USBD_VID), // idVendor @@ -91,7 +92,7 @@ void USBD_SetPID(uint16_t pid) { * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { *length = sizeof(hUSBDDeviceDesc); return hUSBDDeviceDesc; } @@ -102,7 +103,7 @@ STATIC uint8_t *USBD_VCP_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *len * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { *length = sizeof(USBD_LangIDDesc); return USBD_LangIDDesc; } @@ -113,7 +114,7 @@ STATIC uint8_t *USBD_VCP_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t * * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { if(speed == 0) { USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); } else { @@ -128,7 +129,7 @@ STATIC uint8_t *USBD_VCP_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); return USBD_StrDesc; } @@ -139,7 +140,7 @@ STATIC uint8_t *USBD_VCP_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { if(speed == USBD_SPEED_HIGH) { USBD_GetString((uint8_t *)USBD_SERIALNUMBER_HS_STRING, USBD_StrDesc, length); } else { @@ -154,7 +155,7 @@ STATIC uint8_t *USBD_VCP_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t * * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { if(speed == USBD_SPEED_HIGH) { USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length); } else { @@ -169,7 +170,7 @@ STATIC uint8_t *USBD_VCP_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t * * @param length: Pointer to data length variable * @retval Pointer to descriptor buffer */ -STATIC uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { +STATIC uint8_t *USBD_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { if(speed == 0) { USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); } else { @@ -178,14 +179,14 @@ STATIC uint8_t *USBD_VCP_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_ return USBD_StrDesc; } -const USBD_DescriptorsTypeDef VCP_Desc = { - USBD_VCP_DeviceDescriptor, - USBD_VCP_LangIDStrDescriptor, - USBD_VCP_ManufacturerStrDescriptor, - USBD_VCP_ProductStrDescriptor, - USBD_VCP_SerialStrDescriptor, - USBD_VCP_ConfigStrDescriptor, - USBD_VCP_InterfaceStrDescriptor, +const USBD_DescriptorsTypeDef USBD_Descriptors = { + USBD_DeviceDescriptor, + USBD_LangIDStrDescriptor, + USBD_ManufacturerStrDescriptor, + USBD_ProductStrDescriptor, + USBD_SerialStrDescriptor, + USBD_ConfigStrDescriptor, + USBD_InterfaceStrDescriptor, }; /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmhal/usbd_desc.h b/stmhal/usbd_desc.h index 49e19382a..c16808b8a 100644 --- a/stmhal/usbd_desc.h +++ b/stmhal/usbd_desc.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2013, 2014, 2015 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,6 +24,6 @@ * THE SOFTWARE. */ -extern const USBD_DescriptorsTypeDef VCP_Desc; +extern const USBD_DescriptorsTypeDef USBD_Descriptors; void USBD_SetPID(uint16_t pid); diff --git a/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c b/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c index 4386ce814..e72ba7602 100644 --- a/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c @@ -1,19 +1,49 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2015 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #include "usbd_ioreq.h" #include "usbd_cdc_msc_hid.h" -#define CDC_MSC_CONFIG_DESC_SIZE (98) -#define CDC_HID_CONFIG_DESC_SIZE (100) -#define CDC_HID_HID_DESC_OFFSET (CDC_HID_CONFIG_DESC_SIZE - 25) -#define CDC_HID_HID_DESC_OFFSET_SUBCLASS (CDC_HID_HID_DESC_OFFSET + 6) -#define CDC_HID_HID_DESC_OFFSET_PROTOCOL (CDC_HID_HID_DESC_OFFSET + 7) -#define CDC_HID_HID_DESC_OFFSET_SUBDESC (CDC_HID_HID_DESC_OFFSET + 9) -#define CDC_HID_HID_DESC_OFFSET_REPORT_DESC_LEN (CDC_HID_HID_DESC_OFFSET + 16) -#define CDC_HID_HID_DESC_OFFSET_MAX_PACKET_LO (CDC_HID_HID_DESC_OFFSET + 22) -#define CDC_HID_HID_DESC_OFFSET_MAX_PACKET_HI (CDC_HID_HID_DESC_OFFSET + 23) -#define CDC_HID_HID_SUBDESC_LEN (9) +#define MAX_TEMPLATE_CONFIG_DESC_SIZE (100) // should be maximum of all template config desc's +#define CDC_TEMPLATE_CONFIG_DESC_SIZE (67) +#define CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE (98) +#define CDC_HID_TEMPLATE_CONFIG_DESC_SIZE (100) +#define CDC_HID_TEMPLATE_HID_DESC_OFFSET (CDC_HID_TEMPLATE_CONFIG_DESC_SIZE - 25) +#define HID_DESC_OFFSET_SUBCLASS (6) +#define HID_DESC_OFFSET_PROTOCOL (7) +#define HID_DESC_OFFSET_SUBDESC (9) +#define HID_DESC_OFFSET_REPORT_DESC_LEN (16) +#define HID_DESC_OFFSET_MAX_PACKET_LO (22) +#define HID_DESC_OFFSET_MAX_PACKET_HI (23) +#define HID_SUBDESC_LEN (9) -#define CDC_IFACE_NUM (1) -#define MSC_IFACE_NUM (0) +#define CDC_IFACE_NUM_ALONE (0) +#define CDC_IFACE_NUM_WITH_MSC (1) +#define CDC_IFACE_NUM_WITH_HID (1) +#define MSC_IFACE_NUM_WITH_CDC (0) #define HID_IFACE_NUM_WITH_CDC (0) #define HID_IFACE_NUM_WITH_MSC (1) #define HID_IN_EP_WITH_CDC (0x81) @@ -50,8 +80,11 @@ typedef struct { } USBD_HID_HandleTypeDef; static uint8_t usbd_mode; +static uint8_t cdc_iface_num; static uint8_t hid_in_ep; static uint8_t hid_iface_num; +static uint8_t usbd_config_desc_size; +static uint8_t *hid_desc; static const uint8_t *hid_report_desc; static USBD_CDC_ItfTypeDef *CDC_fops; @@ -61,9 +94,11 @@ static USBD_CDC_HandleTypeDef CDC_ClassData; static USBD_MSC_BOT_HandleTypeDef MSC_BOT_ClassData; static USBD_HID_HandleTypeDef HID_ClassData; -// I don't think we can make these descriptors constant because they are -// modified (perhaps unnecessarily) by the USB driver. +// RAM to hold the current configuration descriptor, which we configure on the fly +__ALIGN_BEGIN static uint8_t usbd_config_desc[MAX_TEMPLATE_CONFIG_DESC_SIZE] __ALIGN_END; +/* +// this is used only in high-speed mode, which we don't support // USB Standard Device Descriptor __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { USB_LEN_DEV_QUALIFIER_DESC, @@ -77,15 +112,16 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_HID_DeviceQualifierDesc[USB_LEN_DEV_QU 0x01, 0x00, }; +*/ // USB CDC MSC device Configuration Descriptor -__ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __ALIGN_END = { +static const uint8_t cdc_msc_template_config_desc[CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE] = { //-------------------------------------------------------------------------- // Configuration Descriptor 0x09, // bLength: Configuration Descriptor size USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration - LOBYTE(CDC_MSC_CONFIG_DESC_SIZE), // wTotalLength: no of returned bytes - HIBYTE(CDC_MSC_CONFIG_DESC_SIZE), + LOBYTE(CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE), // wTotalLength: no of returned bytes + HIBYTE(CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE), 0x03, // bNumInterfaces: 3 interfaces 0x01, // bConfigurationValue: Configuration value 0x00, // iConfiguration: Index of string descriptor describing the configuration @@ -96,7 +132,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL // Interface Association for CDC VCP 0x08, // bLength: 8 bytes USB_DESC_TYPE_ASSOCIATION, // bDescriptorType: IAD - CDC_IFACE_NUM, // bFirstInterface: first interface for this association + CDC_IFACE_NUM_WITH_MSC, // bFirstInterface: first interface for this association 0x02, // bInterfaceCount: nummber of interfaces for this association 0x02, // bFunctionClass: Communication Interface Class 0x02, // bFunctionSubClass: Abstract Control Model @@ -107,7 +143,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL // Interface Descriptor 0x09, // bLength: Interface Descriptor size USB_DESC_TYPE_INTERFACE, // bDescriptorType: Interface - CDC_IFACE_NUM, // bInterfaceNumber: Number of Interface + CDC_IFACE_NUM_WITH_MSC, // bInterfaceNumber: Number of Interface 0x00, // bAlternateSetting: Alternate setting 0x01, // bNumEndpoints: One endpoints used 0x02, // bInterfaceClass: Communication Interface Class @@ -127,7 +163,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL 0x24, // bDescriptorType: CS_INTERFACE 0x01, // bDescriptorSubtype: Call Management Func Desc 0x00, // bmCapabilities: D0+D1 - CDC_IFACE_NUM + 1, // bDataInterface: 1 + CDC_IFACE_NUM_WITH_MSC + 1, // bDataInterface: 1 // ACM Functional Descriptor 0x04, // bFunctionLength @@ -139,8 +175,8 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL 0x05, // bFunctionLength 0x24, // bDescriptorType: CS_INTERFACE 0x06, // bDescriptorSubtype: Union func desc - CDC_IFACE_NUM + 0, // bMasterInterface: Communication class interface - CDC_IFACE_NUM + 1, // bSlaveInterface0: Data Class Interface + CDC_IFACE_NUM_WITH_MSC + 0, // bMasterInterface: Communication class interface + CDC_IFACE_NUM_WITH_MSC + 1, // bSlaveInterface0: Data Class Interface // Endpoint 2 Descriptor 0x07, // bLength: Endpoint Descriptor size @@ -155,7 +191,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL // Data class interface descriptor 0x09, // bLength: Endpoint Descriptor size USB_DESC_TYPE_INTERFACE, // bDescriptorType: interface - CDC_IFACE_NUM + 1, // bInterfaceNumber: Number of Interface + CDC_IFACE_NUM_WITH_MSC + 1, // bInterfaceNumber: Number of Interface 0x00, // bAlternateSetting: Alternate setting 0x02, // bNumEndpoints: Two endpoints used 0x0A, // bInterfaceClass: CDC @@ -188,7 +224,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL // Interface Descriptor 0x09, // bLength: Interface Descriptor size USB_DESC_TYPE_INTERFACE, // bDescriptorType: interface descriptor - MSC_IFACE_NUM, // bInterfaceNumber: Number of Interface + MSC_IFACE_NUM_WITH_CDC, // bInterfaceNumber: Number of Interface 0x00, // bAlternateSetting: Alternate setting 0x02, // bNumEndpoints 0x08, // bInterfaceClass: MSC Class @@ -216,13 +252,13 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_CfgDesc[CDC_MSC_CONFIG_DESC_SIZE] __AL }; // USB CDC HID device Configuration Descriptor -__ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __ALIGN_END = { +static const uint8_t cdc_hid_template_config_desc[CDC_HID_TEMPLATE_CONFIG_DESC_SIZE] = { //-------------------------------------------------------------------------- // Configuration Descriptor 0x09, // bLength: Configuration Descriptor size USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration - LOBYTE(CDC_HID_CONFIG_DESC_SIZE), // wTotalLength: no of returned bytes - HIBYTE(CDC_HID_CONFIG_DESC_SIZE), + LOBYTE(CDC_HID_TEMPLATE_CONFIG_DESC_SIZE), // wTotalLength: no of returned bytes + HIBYTE(CDC_HID_TEMPLATE_CONFIG_DESC_SIZE), 0x03, // bNumInterfaces: 3 interfaces 0x01, // bConfigurationValue: Configuration value 0x00, // iConfiguration: Index of string descriptor describing the configuration @@ -233,7 +269,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __AL // Interface Association for CDC VCP 0x08, // bLength: 8 bytes USB_DESC_TYPE_ASSOCIATION, // bDescriptorType: IAD - CDC_IFACE_NUM, // bFirstInterface: first interface for this association + CDC_IFACE_NUM_WITH_HID, // bFirstInterface: first interface for this association 0x02, // bInterfaceCount: nummber of interfaces for this association 0x02, // bFunctionClass: Communication Interface Class 0x02, // bFunctionSubClass: Abstract Control Model @@ -244,7 +280,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __AL // Interface Descriptor 0x09, // bLength: Interface Descriptor size USB_DESC_TYPE_INTERFACE, // bDescriptorType: Interface - CDC_IFACE_NUM, // bInterfaceNumber: Number of Interface + CDC_IFACE_NUM_WITH_HID, // bInterfaceNumber: Number of Interface 0x00, // bAlternateSetting: Alternate setting 0x01, // bNumEndpoints: One endpoints used 0x02, // bInterfaceClass: Communication Interface Class @@ -264,7 +300,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __AL 0x24, // bDescriptorType: CS_INTERFACE 0x01, // bDescriptorSubtype: Call Management Func Desc 0x00, // bmCapabilities: D0+D1 - CDC_IFACE_NUM + 1, // bDataInterface: 1 + CDC_IFACE_NUM_WITH_HID + 1, // bDataInterface: 1 // ACM Functional Descriptor 0x04, // bFunctionLength @@ -276,8 +312,8 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __AL 0x05, // bFunctionLength 0x24, // bDescriptorType: CS_INTERFACE 0x06, // bDescriptorSubtype: Union func desc - CDC_IFACE_NUM + 0, // bMasterInterface: Communication class interface - CDC_IFACE_NUM + 1, // bSlaveInterface0: Data Class Interface + CDC_IFACE_NUM_WITH_HID + 0, // bMasterInterface: Communication class interface + CDC_IFACE_NUM_WITH_HID + 1, // bSlaveInterface0: Data Class Interface // Endpoint 2 Descriptor 0x07, // bLength: Endpoint Descriptor size @@ -292,7 +328,7 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __AL // Data class interface descriptor 0x09, // bLength: Endpoint Descriptor size USB_DESC_TYPE_INTERFACE, // bDescriptorType: interface - CDC_IFACE_NUM + 1, // bInterfaceNumber: Number of Interface + CDC_IFACE_NUM_WITH_HID + 1, // bInterfaceNumber: Number of Interface 0x00, // bAlternateSetting: Alternate setting 0x02, // bNumEndpoints: Two endpoints used 0x0A, // bInterfaceClass: CDC @@ -354,6 +390,98 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_HID_CfgDesc[CDC_HID_CONFIG_DESC_SIZE] __AL 0x08, // bInterval: Polling interval }; +static const uint8_t cdc_template_config_desc[CDC_TEMPLATE_CONFIG_DESC_SIZE] = { + //-------------------------------------------------------------------------- + // Configuration Descriptor + 0x09, // bLength: Configuration Descriptor size + USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration + LOBYTE(CDC_TEMPLATE_CONFIG_DESC_SIZE), // wTotalLength:no of returned bytes + HIBYTE(CDC_TEMPLATE_CONFIG_DESC_SIZE), + 0x02, // bNumInterfaces: 2 interface + 0x01, // bConfigurationValue: Configuration value + 0x00, // iConfiguration: Index of string descriptor describing the configuration + 0x80, // bmAttributes: bus powered; 0xc0 for self powered + 0xfa, // bMaxPower: in units of 2mA + + //-------------------------------------------------------------------------- + // Interface Descriptor + 0x09, // bLength: Interface Descriptor size + USB_DESC_TYPE_INTERFACE, // bDescriptorType: Interface + CDC_IFACE_NUM_ALONE, // bInterfaceNumber: Number of Interface + 0x00, // bAlternateSetting: Alternate setting + 0x01, // bNumEndpoints: One endpoints used + 0x02, // bInterfaceClass: Communication Interface Class + 0x02, // bInterfaceSubClass: Abstract Control Model + 0x01, // bInterfaceProtocol: Common AT commands + 0x00, // iInterface: + + // Header Functional Descriptor + 0x05, // bLength: Endpoint Descriptor size + 0x24, // bDescriptorType: CS_INTERFACE + 0x00, // bDescriptorSubtype: Header Func Desc + 0x10, // bcdCDC: spec release number + 0x01, // ? + + // Call Management Functional Descriptor + 0x05, // bFunctionLength + 0x24, // bDescriptorType: CS_INTERFACE + 0x01, // bDescriptorSubtype: Call Management Func Desc + 0x00, // bmCapabilities: D0+D1 + CDC_IFACE_NUM_ALONE + 1, // bDataInterface: 1 + + // ACM Functional Descriptor + 0x04, // bFunctionLength + 0x24, // bDescriptorType: CS_INTERFACE + 0x02, // bDescriptorSubtype: Abstract Control Management desc + 0x02, // bmCapabilities + + // Union Functional Descriptor + 0x05, // bFunctionLength + 0x24, // bDescriptorType: CS_INTERFACE + 0x06, // bDescriptorSubtype: Union func desc + CDC_IFACE_NUM_ALONE + 0, // bMasterInterface: Communication class interface + CDC_IFACE_NUM_ALONE + 1, // bSlaveInterface0: Data Class Interface + + // Endpoint 2 Descriptor + 0x07, // bLength: Endpoint Descriptor size + USB_DESC_TYPE_ENDPOINT, // bDescriptorType: Endpoint + CDC_CMD_EP, // bEndpointAddress + 0x03, // bmAttributes: Interrupt + LOBYTE(CDC_CMD_PACKET_SIZE), // wMaxPacketSize: + HIBYTE(CDC_CMD_PACKET_SIZE), + 0x20, // bInterval: polling interval in frames of 1ms + + //-------------------------------------------------------------------------- + // Data class interface descriptor + 0x09, // bLength: Endpoint Descriptor size + USB_DESC_TYPE_INTERFACE, // bDescriptorType: + CDC_IFACE_NUM_ALONE + 1, // bInterfaceNumber: Number of Interface + 0x00, // bAlternateSetting: Alternate setting + 0x02, // bNumEndpoints: Two endpoints used + 0x0a, // bInterfaceClass: CDC + 0x00, // bInterfaceSubClass: ? + 0x00, // bInterfaceProtocol: ? + 0x00, // iInterface: + + // Endpoint OUT Descriptor + 0x07, // bLength: Endpoint Descriptor size + USB_DESC_TYPE_ENDPOINT, // bDescriptorType: Endpoint + CDC_OUT_EP, // bEndpointAddress + 0x02, // bmAttributes: Bulk + LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),// wMaxPacketSize: + HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), + 0x00, // bInterval: ignore for Bulk transfer + + // Endpoint IN Descriptor + 0x07, // bLength: Endpoint Descriptor size + USB_DESC_TYPE_ENDPOINT, // bDescriptorType: Endpoint + CDC_IN_EP, // bEndpointAddress + 0x02, // bmAttributes: Bulk + LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),// wMaxPacketSize: + HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), + 0x00 // bInterval: ignore for Bulk transfer +}; + __ALIGN_BEGIN const uint8_t USBD_HID_MOUSE_ReportDesc[USBD_HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { 0x05, 0x01, // Usage Page (Generic Desktop), 0x09, 0x02, // Usage (Mouse), @@ -435,24 +563,48 @@ void USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info) { // save mode usbd_mode = mode; - // set up HID parameters if HID is selected - if (mode & USBD_MODE_HID) { - if (mode & USBD_MODE_CDC) { + // construct config desc + switch (usbd_mode) { + case USBD_MODE_CDC_MSC: + usbd_config_desc_size = sizeof(cdc_msc_template_config_desc); + memcpy(usbd_config_desc, cdc_msc_template_config_desc, sizeof(cdc_msc_template_config_desc)); + cdc_iface_num = CDC_IFACE_NUM_WITH_MSC; + break; + + case USBD_MODE_CDC_HID: + usbd_config_desc_size = sizeof(cdc_hid_template_config_desc); + memcpy(usbd_config_desc, cdc_hid_template_config_desc, sizeof(cdc_hid_template_config_desc)); + cdc_iface_num = CDC_IFACE_NUM_WITH_HID; hid_in_ep = HID_IN_EP_WITH_CDC; hid_iface_num = HID_IFACE_NUM_WITH_CDC; - } else if (mode & USBD_MODE_MSC) { + hid_desc = usbd_config_desc + CDC_HID_TEMPLATE_HID_DESC_OFFSET; + break; + + case USBD_MODE_CDC: + usbd_config_desc_size = sizeof(cdc_template_config_desc); + memcpy(usbd_config_desc, cdc_template_config_desc, sizeof(cdc_template_config_desc)); + cdc_iface_num = CDC_IFACE_NUM_ALONE; + break; + + /* + // not implemented + case USBD_MODE_MSC_HID: hid_in_ep = HID_IN_EP_WITH_MSC; hid_iface_num = HID_IFACE_NUM_WITH_MSC; - } + break; + */ + } - // configure the descriptor - USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_SUBCLASS] = hid_info->subclass; - USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_PROTOCOL] = hid_info->protocol; - USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_REPORT_DESC_LEN] = hid_info->report_desc_len; - USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_MAX_PACKET_LO] = hid_info->max_packet_len; - USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_MAX_PACKET_HI] = 0; + if (usbd_mode & USBD_MODE_HID) { + // configure the HID descriptor + hid_desc[HID_DESC_OFFSET_SUBCLASS] = hid_info->subclass; + hid_desc[HID_DESC_OFFSET_PROTOCOL] = hid_info->protocol; + hid_desc[HID_DESC_OFFSET_REPORT_DESC_LEN] = hid_info->report_desc_len; + hid_desc[HID_DESC_OFFSET_MAX_PACKET_LO] = hid_info->max_packet_len; + hid_desc[HID_DESC_OFFSET_MAX_PACKET_HI] = 0; hid_report_desc = hid_info->report_desc; } + } static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { @@ -520,8 +672,8 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { // get max packet length from descriptor uint16_t mps = - USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_MAX_PACKET_LO] - | (USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_MAX_PACKET_HI] << 8); + hid_desc[HID_DESC_OFFSET_MAX_PACKET_LO] + | (hid_desc[HID_DESC_OFFSET_MAX_PACKET_HI] << 8); // Open EP IN USBD_LL_OpenEP(pdev, @@ -595,7 +747,7 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp // Class request case USB_REQ_TYPE_CLASS: // req->wIndex is the recipient interface number - if ((usbd_mode & USBD_MODE_CDC) && req->wIndex == CDC_IFACE_NUM) { + if ((usbd_mode & USBD_MODE_CDC) && req->wIndex == cdc_iface_num) { // CDC component if (req->wLength) { if (req->bmRequest & 0x80) { @@ -613,7 +765,7 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp // Transfer the command to the interface layer return CDC_fops->Control(req->bRequest, NULL, req->wValue); } - } else if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM) { + } else if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { // MSC component switch (req->bRequest) { case BOT_GET_MAX_LUN: @@ -666,7 +818,7 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp // Interface & Endpoint request case USB_REQ_TYPE_STANDARD: - if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM) { + if ((usbd_mode & USBD_MODE_MSC) && req->wIndex == MSC_IFACE_NUM_WITH_CDC) { switch (req->bRequest) { case USB_REQ_GET_INTERFACE : USBD_CtlSendData(pdev, (uint8_t *)&MSC_BOT_ClassData.interface, 1); @@ -699,12 +851,12 @@ static uint8_t USBD_CDC_MSC_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTyp uint16_t len = 0; const uint8_t *pbuf = NULL; if (req->wValue >> 8 == HID_REPORT_DESC) { - len = USBD_CDC_HID_CfgDesc[CDC_HID_HID_DESC_OFFSET_REPORT_DESC_LEN]; + len = hid_desc[HID_DESC_OFFSET_REPORT_DESC_LEN]; len = MIN(len, req->wLength); pbuf = hid_report_desc; } else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { - len = MIN(CDC_HID_HID_SUBDESC_LEN, req->wLength); - pbuf = USBD_CDC_HID_CfgDesc + CDC_HID_HID_DESC_OFFSET_SUBDESC; + len = MIN(HID_SUBDESC_LEN, req->wLength); + pbuf = hid_desc + HID_DESC_OFFSET_SUBDESC; } USBD_CtlSendData(pdev, (uint8_t*)pbuf, len); break; @@ -773,21 +925,18 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) } static uint8_t *USBD_CDC_MSC_HID_GetCfgDesc(uint16_t *length) { - switch (usbd_mode) { - case USBD_MODE_CDC_MSC: - *length = sizeof(USBD_CDC_MSC_CfgDesc); - return USBD_CDC_MSC_CfgDesc; - - case USBD_MODE_CDC_HID: - default: - *length = sizeof(USBD_CDC_HID_CfgDesc); - return USBD_CDC_HID_CfgDesc; - } + *length = usbd_config_desc_size; + return usbd_config_desc; } +// this is used only in high-speed mode, which we don't support uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor (uint16_t *length) { + /* *length = sizeof(USBD_CDC_MSC_HID_DeviceQualifierDesc); return USBD_CDC_MSC_HID_DeviceQualifierDesc; + */ + *length = 0; + return NULL; } uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops) { @@ -902,7 +1051,7 @@ uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t return USBD_OK; } -// CDC + MSC interface class callbacks structure +// CDC/MSC/HID interface class callback structure USBD_ClassTypeDef USBD_CDC_MSC_HID = { USBD_CDC_MSC_HID_Init, USBD_CDC_MSC_HID_DeInit, diff --git a/tools/insert-usb-ids.py b/tools/insert-usb-ids.py index 42303da8b..4f773b72f 100644 --- a/tools/insert-usb-ids.py +++ b/tools/insert-usb-ids.py @@ -10,7 +10,7 @@ import string def parse_usb_ids(filename): rv = dict() - if filename == 'usbd_desc_cdc_msc.c': + if filename == 'usbd_desc.c': for line in open(filename).readlines(): line = line.rstrip('\r\n') match = re.match('^#define\s+(\w+)\s+0x([0-9A-Fa-f]+)$', line)