Skip to content

Commit 985ae1a

Browse files
authored
Fix for USB host causing lag on force reboot (#1055)
* Potential fix for USB host causing reboot to lag out * Adding 10 second aux time-out (will change to 1 second when verified working) * Moved 10 seconds to 1 second for auxiliary ONLY when usb host is enabled
1 parent ef0f471 commit 985ae1a

1 file changed

Lines changed: 6 additions & 76 deletions

File tree

src/usbhostmanager.cpp

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212

1313
void USBHostManager::start() {
1414
// This will happen after Gamepad has initialized
15-
if (PeripheralManager::getInstance().isUSBEnabled(0)) {
15+
if (PeripheralManager::getInstance().isUSBEnabled(0) && listeners.size() > 0) {
16+
sleep_ms(1000); // TinyUSB HOST Start-Up Temporary Fix : TO-DO 06.11.2024
1617
pio_usb_configuration_t* pio_cfg = PeripheralManager::getInstance().getUSB(0)->getController();
1718
tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, pio_cfg);
1819
tuh_init(BOARD_TUH_RHPORT);
1920
sleep_us(10); // ensure we are ready
2021
tuh_ready = true;
22+
} else {
23+
tuh_ready = false;
2124
}
2225
}
2326

2427
// Shut down the USB bus if we are running USB right now
2528
void USBHostManager::shutdown() {
26-
if (PeripheralManager::getInstance().isUSBEnabled(0)) {
27-
tuh_rhport_reset_bus(BOARD_TUH_RHPORT, false);
29+
if ( tuh_ready ) {
30+
tuh_deinit(BOARD_TUH_RHPORT);
2831
}
2932
}
3033

@@ -102,81 +105,8 @@ void USBHostManager::xinput_report_sent_cb(uint8_t dev_addr, uint8_t instance, u
102105
}
103106
}
104107

105-
// HID: USB Host
106-
static uint8_t _intf_num = 0;
107-
108-
// Required helper class for HID_REQ_CONTROL_GET_REPORT addition
109-
uint16_t count_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len)
110-
{
111-
uint8_t const* p_desc = (uint8_t const*) desc_itf;
112-
uint16_t len = 0;
113-
114-
while (itf_count--)
115-
{
116-
// Next on interface desc
117-
len += tu_desc_len(desc_itf);
118-
p_desc = tu_desc_next(p_desc);
119-
120-
while (len < max_len)
121-
{
122-
// return on IAD regardless of itf count
123-
if ( tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION ) return len;
124-
125-
if ( (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) &&
126-
((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0 )
127-
{
128-
break;
129-
}
130-
131-
len += tu_desc_len(p_desc);
132-
p_desc = tu_desc_next(p_desc);
133-
}
134-
}
135-
136-
return len;
137-
}
138-
139108
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
140109
{
141-
// Get Interface Number for our HID class
142-
uint16_t temp_buf[128];
143-
144-
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_configuration_sync(dev_addr, 0, temp_buf, sizeof(temp_buf)))
145-
{
146-
tusb_desc_configuration_t const* desc_cfg = (tusb_desc_configuration_t*) temp_buf;
147-
uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
148-
uint8_t const* p_desc = tu_desc_next(desc_cfg);
149-
150-
// parse each interfaces
151-
while( p_desc < desc_end )
152-
{
153-
uint8_t assoc_itf_count = 1;
154-
// Class will always starts with Interface Association (if any) and then Interface descriptor
155-
if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) )
156-
{
157-
tusb_desc_interface_assoc_t const * desc_iad = (tusb_desc_interface_assoc_t const *) p_desc;
158-
assoc_itf_count = desc_iad->bInterfaceCount;
159-
160-
p_desc = tu_desc_next(p_desc); // next to Interface
161-
}
162-
163-
// must be interface from now
164-
if( TUSB_DESC_INTERFACE != tu_desc_type(p_desc) ) return;
165-
tusb_desc_interface_t const* desc_itf = (tusb_desc_interface_t const*) p_desc;
166-
167-
// only open and listen to HID endpoint IN (PS4)
168-
if (desc_itf->bInterfaceClass == TUSB_CLASS_HID)
169-
{
170-
_intf_num = desc_itf->bInterfaceNumber;
171-
break; // we got the interface number
172-
}
173-
174-
// next Interface or IAD descriptor
175-
uint16_t const drv_len = count_interface_total_len(desc_itf, assoc_itf_count, (uint16_t) (desc_end-p_desc));
176-
p_desc += drv_len;
177-
}
178-
} // This block can be removed once TinyUSB library incorporates HID_REQ_CONTROL_GET_REPORT callback
179-
180110
USBHostManager::getInstance().hid_mount_cb(dev_addr, instance, desc_report, desc_len);
181111
if ( !tuh_hid_receive_report(dev_addr, instance) ) {
182112
// Error: cannot request report

0 commit comments

Comments
 (0)