Skip to content

Commit f4a7b5b

Browse files
committed
add note for recursive calls of process_device_unplugged() in case of hub
1 parent 7df7590 commit f4a7b5b

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
// Size of buffer to hold descriptors and other data used for enumeration
8282
#define CFG_TUH_ENUMERATION_BUFSIZE 256
8383

84-
#define CFG_TUH_HUB 1
84+
#define CFG_TUH_HUB 1 // number of supported hubs
8585
#define CFG_TUH_CDC 1
8686
#define CFG_TUH_HID 4 // typical keyboard + mouse device can have 3-4 HID interfaces
8787
#define CFG_TUH_MSC 1

src/host/usbh.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,12 @@ uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_i
10891089
//
10901090
//--------------------------------------------------------------------+
10911091

1092+
TU_ATTR_ALWAYS_INLINE
1093+
static inline bool is_hub_addr(uint8_t daddr)
1094+
{
1095+
return (CFG_TUH_HUB > 0) && (daddr > CFG_TUH_DEVICE_MAX);
1096+
}
1097+
10921098
// a device unplugged from rhport:hub_addr:hub_port
10931099
static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port)
10941100
{
@@ -1101,21 +1107,24 @@ static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t h
11011107

11021108
// TODO Hub multiple level
11031109
if (dev->rhport == rhport &&
1104-
(hub_addr == 0 || dev->hub_addr == hub_addr) && // hub_addr == 0 & hub_port == 0 means roothub
1105-
(hub_port == 0 || dev->hub_port == hub_port) &&
1110+
(hub_addr == 0 || dev->hub_addr == hub_addr) && // hub_addr = 0 means roothub
1111+
(hub_port == 0 || dev->hub_port == hub_port) && // hub_port = 0 means all devices of downstream hub
11061112
dev->connected)
11071113
{
11081114
TU_LOG2(" Address = %u\r\n", dev_addr);
11091115

1110-
// If the device itself is a usb hub, unplug downstream devices.
1111-
if (dev_addr > CFG_TUH_DEVICE_MAX)
1116+
if (is_hub_addr(dev_addr))
11121117
{
1118+
TU_LOG(USBH_DBG_LVL, "HUB address = %u is unmounted\r\n", dev_addr);
1119+
// If the device itself is a usb hub, unplug downstream devices.
1120+
// FIXME un-roll recursive calls to prevent potential stack overflow
11131121
process_device_unplugged(rhport, dev_addr, 0);
1122+
}else
1123+
{
1124+
// Invoke callback before closing driver
1125+
if (tuh_umount_cb) tuh_umount_cb(dev_addr);
11141126
}
11151127

1116-
// Invoke callback before close driver
1117-
if (tuh_umount_cb) tuh_umount_cb(dev_addr);
1118-
11191128
// Close class driver
11201129
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
11211130
{
@@ -1395,12 +1404,6 @@ static bool enum_new_device(hcd_event_t* event)
13951404
return true;
13961405
}
13971406

1398-
TU_ATTR_ALWAYS_INLINE
1399-
static inline bool is_hub_addr(uint8_t daddr)
1400-
{
1401-
return daddr > CFG_TUH_DEVICE_MAX;
1402-
}
1403-
14041407
static uint8_t get_new_address(bool is_hub)
14051408
{
14061409
uint8_t start;
@@ -1577,10 +1580,10 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
15771580
{
15781581
enum_full_complete();
15791582

1580-
#if CFG_TUH_HUB
1581-
// skip device mount callback for hub
1582-
if ( !is_hub_addr(dev_addr) )
1583-
#endif
1583+
if (is_hub_addr(dev_addr))
1584+
{
1585+
TU_LOG(USBH_DBG_LVL, "HUB address = %u is mounted\r\n", dev_addr);
1586+
}else
15841587
{
15851588
// Invoke callback if available
15861589
if (tuh_mount_cb) tuh_mount_cb(dev_addr);

0 commit comments

Comments
 (0)