Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for USB host causing lag on force reboot #1055

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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