|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Damien P. George |
| 7 | + * Copyright (c) 2022 Blake W. Felt & Angus Gratton |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "mpconfigport.h" |
| 29 | +#include "tusb.h" |
| 30 | +#include "mp_usbd.h" |
| 31 | +#include "mp_usbd_internal.h" |
| 32 | + |
| 33 | +#define USBD_CDC_CMD_MAX_SIZE (8) |
| 34 | +#define USBD_CDC_IN_OUT_MAX_SIZE (64) |
| 35 | + |
| 36 | +const tusb_desc_device_t mp_usbd_desc_device_static = { |
| 37 | + .bLength = sizeof(tusb_desc_device_t), |
| 38 | + .bDescriptorType = TUSB_DESC_DEVICE, |
| 39 | + .bcdUSB = 0x0200, |
| 40 | + .bDeviceClass = TUSB_CLASS_MISC, |
| 41 | + .bDeviceSubClass = MISC_SUBCLASS_COMMON, |
| 42 | + .bDeviceProtocol = MISC_PROTOCOL_IAD, |
| 43 | + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, |
| 44 | + .idVendor = MICROPY_HW_USB_VID, |
| 45 | + .idProduct = MICROPY_HW_USB_PID, |
| 46 | + .bcdDevice = 0x0100, |
| 47 | + .iManufacturer = USBD_STR_MANUF, |
| 48 | + .iProduct = USBD_STR_PRODUCT, |
| 49 | + .iSerialNumber = USBD_STR_SERIAL, |
| 50 | + .bNumConfigurations = 1, |
| 51 | +}; |
| 52 | + |
| 53 | +const uint8_t mp_usbd_desc_cfg_static[USBD_STATIC_DESC_LEN] = { |
| 54 | + TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_STATIC_MAX, USBD_STR_0, USBD_STATIC_DESC_LEN, |
| 55 | + 0, USBD_MAX_POWER_MA), |
| 56 | + |
| 57 | + TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, |
| 58 | + USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE), |
| 59 | + #if CFG_TUD_MSC |
| 60 | + TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64), |
| 61 | + #endif |
| 62 | +}; |
| 63 | + |
| 64 | +const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { |
| 65 | + char serial_buf[USBD_DESC_STR_MAX + 1]; // Includes terminating NUL byte |
| 66 | + static uint16_t desc_wstr[USBD_DESC_STR_MAX + 1]; // Includes prefix uint16_t |
| 67 | + const char *desc_str; |
| 68 | + uint16_t desc_len; |
| 69 | + |
| 70 | + switch (index) { |
| 71 | + case 0: |
| 72 | + desc_wstr[1] = 0x0409; // supported language is English |
| 73 | + desc_len = 4; |
| 74 | + break; |
| 75 | + case USBD_STR_SERIAL: |
| 76 | + // TODO: make a port-specific serial number callback |
| 77 | + mp_usbd_port_get_serial_number(serial_buf); |
| 78 | + desc_str = serial_buf; |
| 79 | + break; |
| 80 | + case USBD_STR_MANUF: |
| 81 | + desc_str = MICROPY_HW_USB_MANUFACTURER_STRING; |
| 82 | + break; |
| 83 | + case USBD_STR_PRODUCT: |
| 84 | + desc_str = MICROPY_HW_USB_PRODUCT_FS_STRING; |
| 85 | + break; |
| 86 | + case USBD_STR_CDC: |
| 87 | + desc_str = MICROPY_HW_USB_CDC_INTERFACE_STRING; |
| 88 | + break; |
| 89 | + #if CFG_TUD_MSC |
| 90 | + case USBD_STR_MSC: |
| 91 | + desc_str = MICROPY_HW_USB_MSC_INTERFACE_STRING; |
| 92 | + break; |
| 93 | + #endif |
| 94 | + default: |
| 95 | + desc_str = NULL; |
| 96 | + } |
| 97 | + |
| 98 | + if (index != 0) { |
| 99 | + if (desc_str == NULL) { |
| 100 | + return NULL; // Will STALL the endpoint |
| 101 | + } |
| 102 | + |
| 103 | + // Convert from narrow string to wide string |
| 104 | + desc_len = 2; |
| 105 | + for (int i = 0; i < USBD_DESC_STR_MAX && desc_str[i] != 0; i++) { |
| 106 | + desc_wstr[1 + i] = desc_str[i]; |
| 107 | + desc_len += 2; |
| 108 | + } |
| 109 | + } |
| 110 | + // first byte is length (including header), second byte is string type |
| 111 | + desc_wstr[0] = (TUSB_DESC_STRING << 8) | desc_len; |
| 112 | + |
| 113 | + return desc_wstr; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +const uint8_t *tud_descriptor_device_cb(void) { |
| 118 | + return (const void *)&mp_usbd_desc_device_static; |
| 119 | +} |
| 120 | + |
| 121 | +const uint8_t *tud_descriptor_configuration_cb(uint8_t index) { |
| 122 | + (void)index; |
| 123 | + return mp_usbd_desc_cfg_static; |
| 124 | +} |
0 commit comments