Skip to content

Commit 4118217

Browse files
committed
update tinyusb to commit 51cfae6e97b08e9c34a4551cfa45c85a2498a943
1 parent aba2328 commit 4118217

File tree

6 files changed

+118
-47
lines changed

6 files changed

+118
-47
lines changed

src/common/tusb_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ TU_VERIFY_STATIC( sizeof(tusb_desc_interface_assoc_t) == 8, "size is not correct
462462
typedef struct TU_ATTR_PACKED {
463463
uint8_t bLength ; ///< Size of this descriptor in bytes
464464
uint8_t bDescriptorType ; ///< Descriptor Type
465-
uint16_t unicode_string[];
465+
uint16_t utf16le[];
466466
} tusb_desc_string_t;
467467

468468
// USB Binary Device Object Store (BOS)

src/host/hub.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef struct {
5151

5252
// from hub descriptor
5353
uint8_t bNbrPorts;
54-
uint8_t bPwrOn2PwrGood; // port power on to good, in 2ms unit
54+
uint8_t bPwrOn2PwrGood_2ms; // port power on to good, in 2ms unit
5555
// uint16_t wHubCharacteristics;
5656

5757
hub_port_status_response_t port_status;
@@ -284,7 +284,7 @@ static void config_set_port_power (tuh_xfer_t* xfer) {
284284
// only use number of ports in hub descriptor
285285
hub_desc_cs_t const* desc_hub = (hub_desc_cs_t const*) p_epbuf->ctrl_buf;
286286
p_hub->bNbrPorts = desc_hub->bNbrPorts;
287-
p_hub->bPwrOn2PwrGood = desc_hub->bPwrOn2PwrGood;
287+
p_hub->bPwrOn2PwrGood_2ms = desc_hub->bPwrOn2PwrGood;
288288

289289
// May need to GET_STATUS
290290

@@ -306,7 +306,6 @@ static void config_port_power_complete (tuh_xfer_t* xfer) {
306306
TU_MESS_FAILED();
307307
TU_BREAKPOINT();
308308
}
309-
// delay bPwrOn2PwrGood * 2 ms before set configuration complete
310309
usbh_driver_set_config_complete(daddr, p_hub->itf_num);
311310
} else {
312311
// power next port

src/host/usbh.c

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,13 +1287,17 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
12871287
removing_hubs |= TU_BIT(dev_id - CFG_TUH_DEVICE_MAX);
12881288
} else {
12891289
// Invoke callback before closing driver (maybe call it later ?)
1290-
if (tuh_umount_cb) tuh_umount_cb(daddr);
1290+
if (tuh_umount_cb) {
1291+
tuh_umount_cb(daddr);
1292+
}
12911293
}
12921294

12931295
// Close class driver
12941296
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) {
12951297
usbh_class_driver_t const* driver = get_driver(drv_id);
1296-
if (driver) driver->close(daddr);
1298+
if (driver) {
1299+
driver->close(daddr);
1300+
}
12971301
}
12981302

12991303
hcd_device_close(rhport, daddr);
@@ -1350,8 +1354,11 @@ enum {
13501354
ENUM_HUB_GET_STATUS_2,
13511355
ENUM_HUB_CLEAR_RESET_2,
13521356
ENUM_SET_ADDR,
1353-
13541357
ENUM_GET_DEVICE_DESC,
1358+
ENUM_GET_STRING_LANGUAGE_ID,
1359+
ENUM_GET_STRING_MANUFACTURER,
1360+
ENUM_GET_STRING_PRODUCT,
1361+
ENUM_GET_STRING_SERIAL,
13551362
ENUM_GET_9BYTE_CONFIG_DESC,
13561363
ENUM_GET_FULL_CONFIG_DESC,
13571364
ENUM_SET_CONFIG,
@@ -1364,25 +1371,25 @@ static void enum_full_complete(void);
13641371

13651372
// process device enumeration
13661373
static void process_enumeration(tuh_xfer_t* xfer) {
1367-
// Retry a few times with transfers in enumeration since device can be unstable when starting up
1368-
enum {
1369-
ATTEMPT_COUNT_MAX = 3,
1370-
ATTEMPT_DELAY_MS = 100
1371-
};
1374+
// Retry a few times while enumerating since device can be unstable when starting up
13721375
static uint8_t failed_count = 0;
1376+
if (XFER_RESULT_FAILED == xfer->result) {
1377+
enum {
1378+
ATTEMPT_COUNT_MAX = 3,
1379+
ATTEMPT_DELAY_MS = 100
1380+
};
13731381

1374-
if (XFER_RESULT_SUCCESS != xfer->result) {
13751382
// retry if not reaching max attempt
1383+
failed_count++;
13761384
bool retry = _dev0.enumerating && (failed_count < ATTEMPT_COUNT_MAX);
1377-
if ( retry ) {
1378-
failed_count++;
1385+
if (retry) {
13791386
tusb_time_delay_ms_api(ATTEMPT_DELAY_MS); // delay a bit
1380-
TU_LOG1("Enumeration attempt %u\r\n", failed_count);
1387+
TU_LOG1("Enumeration attempt %u/%u\r\n", failed_count+1, ATTEMPT_COUNT_MAX);
13811388
retry = tuh_control_xfer(xfer);
13821389
}
13831390

13841391
if (!retry) {
1385-
enum_full_complete();
1392+
enum_full_complete(); // complete as failed
13861393
}
13871394

13881395
return;
@@ -1391,6 +1398,8 @@ static void process_enumeration(tuh_xfer_t* xfer) {
13911398

13921399
uint8_t const daddr = xfer->daddr;
13931400
uintptr_t const state = xfer->user_data;
1401+
usbh_device_t* dev = get_device(daddr);
1402+
uint16_t langid = 0x0409; // default is English
13941403

13951404
switch (state) {
13961405
#if CFG_TUH_HUB
@@ -1481,7 +1490,6 @@ static void process_enumeration(tuh_xfer_t* xfer) {
14811490
tusb_time_delay_ms_api(2);
14821491

14831492
const uint8_t new_addr = (uint8_t) tu_le16toh(xfer->setup->wValue);
1484-
14851493
usbh_device_t* new_dev = get_device(new_addr);
14861494
TU_ASSERT(new_dev,);
14871495
new_dev->addressed = 1;
@@ -1495,21 +1503,69 @@ static void process_enumeration(tuh_xfer_t* xfer) {
14951503
// Get full device descriptor
14961504
TU_LOG_USBH("Get Device Descriptor\r\n");
14971505
TU_ASSERT(tuh_descriptor_get_device(new_addr, _usbh_epbuf.ctrl, sizeof(tusb_desc_device_t),
1498-
process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC),);
1506+
process_enumeration, ENUM_GET_STRING_LANGUAGE_ID),);
14991507
break;
15001508
}
15011509

1502-
case ENUM_GET_9BYTE_CONFIG_DESC: {
1503-
tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_epbuf.ctrl;
1504-
usbh_device_t* dev = get_device(daddr);
1510+
case ENUM_GET_STRING_LANGUAGE_ID: {
1511+
// save the received device descriptor
15051512
TU_ASSERT(dev,);
1506-
1513+
tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_epbuf.ctrl;
15071514
dev->vid = desc_device->idVendor;
15081515
dev->pid = desc_device->idProduct;
15091516
dev->i_manufacturer = desc_device->iManufacturer;
15101517
dev->i_product = desc_device->iProduct;
15111518
dev->i_serial = desc_device->iSerialNumber;
15121519

1520+
tuh_descriptor_get_string_langid(daddr, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
1521+
process_enumeration, ENUM_GET_STRING_MANUFACTURER);
1522+
break;
1523+
}
1524+
1525+
case ENUM_GET_STRING_MANUFACTURER: {
1526+
TU_ASSERT(dev,);
1527+
const tusb_desc_string_t* desc_langid = (tusb_desc_string_t const*) _usbh_epbuf.ctrl;
1528+
if (desc_langid->bLength >= 4) {
1529+
langid = tu_le16toh(desc_langid->utf16le[0]);
1530+
}
1531+
if (dev->i_manufacturer != 0) {
1532+
tuh_descriptor_get_string(daddr, dev->i_manufacturer, langid, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
1533+
process_enumeration, ENUM_GET_STRING_PRODUCT);
1534+
break;
1535+
} else {
1536+
TU_ATTR_FALLTHROUGH;
1537+
}
1538+
}
1539+
1540+
case ENUM_GET_STRING_PRODUCT: {
1541+
TU_ASSERT(dev,);
1542+
if (state == ENUM_GET_STRING_PRODUCT) {
1543+
langid = tu_le16toh(xfer->setup->wIndex); // if not fall through, get langid from previous setup packet
1544+
}
1545+
if (dev->i_product != 0) {
1546+
tuh_descriptor_get_string(daddr, dev->i_product, 0x0409, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
1547+
process_enumeration, ENUM_GET_STRING_SERIAL);
1548+
break;
1549+
} else {
1550+
TU_ATTR_FALLTHROUGH;
1551+
}
1552+
}
1553+
1554+
case ENUM_GET_STRING_SERIAL: {
1555+
TU_ASSERT(dev,);
1556+
if (state == ENUM_GET_STRING_SERIAL) {
1557+
langid = tu_le16toh(xfer->setup->wIndex); // if not fall through, get langid from previous setup packet
1558+
}
1559+
if (dev->i_serial != 0) {
1560+
tuh_descriptor_get_string(daddr, dev->i_serial, langid, _usbh_epbuf.ctrl, CFG_TUH_ENUMERATION_BUFSIZE,
1561+
process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC);
1562+
break;
1563+
} else {
1564+
TU_ATTR_FALLTHROUGH;
1565+
}
1566+
}
1567+
1568+
case ENUM_GET_9BYTE_CONFIG_DESC: {
15131569
// Get 9-byte for total length
15141570
uint8_t const config_idx = CONFIG_NUM - 1;
15151571
TU_LOG_USBH("Get Configuration[0] Descriptor (9 bytes)\r\n");
@@ -1542,7 +1598,6 @@ static void process_enumeration(tuh_xfer_t* xfer) {
15421598

15431599
case ENUM_CONFIG_DRIVER: {
15441600
TU_LOG_USBH("Device configured\r\n");
1545-
usbh_device_t* dev = get_device(daddr);
15461601
TU_ASSERT(dev,);
15471602

15481603
dev->configured = 1;

src/host/usbh.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_
265265
bool tuh_descriptor_get_string(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len,
266266
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
267267

268+
// Get language id string descriptor (control transfer)
269+
TU_ATTR_ALWAYS_INLINE static inline
270+
bool tuh_descriptor_get_string_langid(uint8_t daddr, void* buffer, uint16_t len,
271+
tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
272+
return tuh_descriptor_get_string(daddr, 0, 0, buffer, len, complete_cb, user_data);
273+
}
274+
268275
// Get manufacturer string descriptor (control transfer)
269276
// true on success, false if there is on-going control transfer or incorrect parameters
270277
bool tuh_descriptor_get_manufacturer_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len,
@@ -304,6 +311,12 @@ uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8
304311
// return transfer result
305312
uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len);
306313

314+
// Sync (blocking) version of tuh_descriptor_get_string_langid()
315+
TU_ATTR_ALWAYS_INLINE static inline
316+
uint8_t tuh_descriptor_get_string_langid_sync(uint8_t daddr, void* buffer, uint16_t len) {
317+
return tuh_descriptor_get_string_sync(daddr, 0, 0, buffer, len);
318+
}
319+
307320
// Sync (blocking) version of tuh_descriptor_get_manufacturer_string()
308321
// return transfer result
309322
uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len);

src/portable/raspberrypi/rp2040/hcd_rp2040.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -459,28 +459,33 @@ tusb_speed_t hcd_port_speed_get(uint8_t rhport)
459459
}
460460

461461
// Close all opened endpoint belong to this device
462-
void hcd_device_close(uint8_t rhport, uint8_t dev_addr)
463-
{
462+
void hcd_device_close(uint8_t rhport, uint8_t dev_addr) {
464463
pico_trace("hcd_device_close %d\n", dev_addr);
465464
(void) rhport;
466465

467-
if (dev_addr == 0) return;
468-
469-
for (size_t i = 1; i < TU_ARRAY_SIZE(ep_pool); i++)
470-
{
471-
hw_endpoint_t* ep = &ep_pool[i];
466+
// reset epx if it is currently active with unplugged device
467+
if (epx.configured && epx.active && epx.dev_addr == dev_addr) {
468+
epx.configured = false;
469+
*epx.endpoint_control = 0;
470+
*epx.buffer_control = 0;
471+
hw_endpoint_reset_transfer(&epx);
472+
}
472473

473-
if (ep->dev_addr == dev_addr && ep->configured)
474-
{
475-
// in case it is an interrupt endpoint, disable it
476-
usb_hw_clear->int_ep_ctrl = (1 << (ep->interrupt_num + 1));
477-
usb_hw->int_ep_addr_ctrl[ep->interrupt_num] = 0;
478-
479-
// unconfigure the endpoint
480-
ep->configured = false;
481-
*ep->endpoint_control = 0;
482-
*ep->buffer_control = 0;
483-
hw_endpoint_reset_transfer(ep);
474+
// dev0 only has ep0
475+
if (dev_addr != 0) {
476+
for (size_t i = 1; i < TU_ARRAY_SIZE(ep_pool); i++) {
477+
hw_endpoint_t *ep = &ep_pool[i];
478+
if (ep->dev_addr == dev_addr && ep->configured) {
479+
// in case it is an interrupt endpoint, disable it
480+
usb_hw_clear->int_ep_ctrl = (1 << (ep->interrupt_num + 1));
481+
usb_hw->int_ep_addr_ctrl[ep->interrupt_num] = 0;
482+
483+
// unconfigure the endpoint
484+
ep->configured = false;
485+
*ep->endpoint_control = 0;
486+
*ep->buffer_control = 0;
487+
hw_endpoint_reset_transfer(ep);
488+
}
484489
}
485490
}
486491
}
@@ -557,7 +562,7 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *
557562
}
558563

559564
// If a normal transfer (non-interrupt) then initiate using
560-
// sie ctrl registers. Otherwise interrupt ep registers should
565+
// sie ctrl registers. Otherwise, interrupt ep registers should
561566
// already be configured
562567
if ( ep == &epx )
563568
{
@@ -597,13 +602,12 @@ bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet
597602
(void) rhport;
598603

599604
// Copy data into setup packet buffer
600-
for ( uint8_t i = 0; i < 8; i++ )
601-
{
605+
for (uint8_t i = 0; i < 8; i++) {
602606
usbh_dpram->setup_packet[i] = setup_packet[i];
603607
}
604608

605609
// Configure EP0 struct with setup info for the trans complete
606-
struct hw_endpoint * ep = _hw_endpoint_allocate(0);
610+
struct hw_endpoint * ep = _hw_endpoint_allocate( (uint8_t) TUSB_XFER_CONTROL);
607611
TU_ASSERT(ep);
608612

609613
// EPX should be inactive

src/portable/raspberrypi/rp2040/rp2040_usb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoi
110110
*ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL;
111111
// 4.1.2.5.1 Con-current access: 12 cycles (should be good for 48*12Mhz = 576Mhz) after write to buffer control
112112
// Don't need delay in host mode as host is in charge
113-
if ( !is_host_mode()) {
113+
if (!is_host_mode()) {
114114
busy_wait_at_least_cycles(12);
115115
}
116116
}

0 commit comments

Comments
 (0)