Skip to content

Commit

Permalink
Fix for USB host causing lag on force reboot (#1055)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arntsonl authored Jun 11, 2024
1 parent ef0f471 commit 985ae1a
Showing 1 changed file with 6 additions and 76 deletions.
82 changes: 6 additions & 76 deletions src/usbhostmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@

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

// Shut down the USB bus if we are running USB right now
void USBHostManager::shutdown() {
if (PeripheralManager::getInstance().isUSBEnabled(0)) {
tuh_rhport_reset_bus(BOARD_TUH_RHPORT, false);
if ( tuh_ready ) {
tuh_deinit(BOARD_TUH_RHPORT);
}
}

Expand Down Expand Up @@ -102,81 +105,8 @@ void USBHostManager::xinput_report_sent_cb(uint8_t dev_addr, uint8_t instance, u
}
}

// HID: USB Host
static uint8_t _intf_num = 0;

// Required helper class for HID_REQ_CONTROL_GET_REPORT addition
uint16_t count_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len)
{
uint8_t const* p_desc = (uint8_t const*) desc_itf;
uint16_t len = 0;

while (itf_count--)
{
// Next on interface desc
len += tu_desc_len(desc_itf);
p_desc = tu_desc_next(p_desc);

while (len < max_len)
{
// return on IAD regardless of itf count
if ( tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION ) return len;

if ( (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) &&
((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0 )
{
break;
}

len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
}

return len;
}

void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
{
// Get Interface Number for our HID class
uint16_t temp_buf[128];

if (XFER_RESULT_SUCCESS == tuh_descriptor_get_configuration_sync(dev_addr, 0, temp_buf, sizeof(temp_buf)))
{
tusb_desc_configuration_t const* desc_cfg = (tusb_desc_configuration_t*) temp_buf;
uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
uint8_t const* p_desc = tu_desc_next(desc_cfg);

// parse each interfaces
while( p_desc < desc_end )
{
uint8_t assoc_itf_count = 1;
// Class will always starts with Interface Association (if any) and then Interface descriptor
if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) )
{
tusb_desc_interface_assoc_t const * desc_iad = (tusb_desc_interface_assoc_t const *) p_desc;
assoc_itf_count = desc_iad->bInterfaceCount;

p_desc = tu_desc_next(p_desc); // next to Interface
}

// must be interface from now
if( TUSB_DESC_INTERFACE != tu_desc_type(p_desc) ) return;
tusb_desc_interface_t const* desc_itf = (tusb_desc_interface_t const*) p_desc;

// only open and listen to HID endpoint IN (PS4)
if (desc_itf->bInterfaceClass == TUSB_CLASS_HID)
{
_intf_num = desc_itf->bInterfaceNumber;
break; // we got the interface number
}

// next Interface or IAD descriptor
uint16_t const drv_len = count_interface_total_len(desc_itf, assoc_itf_count, (uint16_t) (desc_end-p_desc));
p_desc += drv_len;
}
} // This block can be removed once TinyUSB library incorporates HID_REQ_CONTROL_GET_REPORT callback

USBHostManager::getInstance().hid_mount_cb(dev_addr, instance, desc_report, desc_len);
if ( !tuh_hid_receive_report(dev_addr, instance) ) {
// Error: cannot request report
Expand Down

0 comments on commit 985ae1a

Please sign in to comment.