Skip to content

Commit

Permalink
Xbox 360 Fix (#1148)
Browse files Browse the repository at this point in the history
* Fix for when descriptor is called repeatedly for xbox 360

* We can remove our host wait now that we properly sync core0 and core1 start-up.

Moved tud_init to directly before tud_task so we don't stall out waiting
  • Loading branch information
arntsonl authored Sep 26, 2024
1 parent 2712ecf commit 10f8bc4
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 53 deletions.
3 changes: 0 additions & 3 deletions src/drivermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,4 @@ void DriverManager::setup(InputMode mode) {
// Initialize our chosen driver
driver->initialize();
inputMode = mode;

// Start the TinyUSB Device functionality
tud_init(TUD_OPT_RHPORT);
}
50 changes: 5 additions & 45 deletions src/drivers/xinput/XInputDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,15 @@
#define DESC_EXTENDED_PROPERTIES_DESCRIPTOR 0x0005

#define XINPUT_OUT_SIZE 32
#define CFG_TUD_XINPUT 4

#define XINPUT_DESC_TYPE_RESERVED 0x21
#define XINPUT_SECURITY_DESC_TYPE_RESERVED 0x41

//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
typedef struct {
uint8_t itf_num;
uint8_t ep_in;
uint8_t ep_out; // optional Out endpoint
uint8_t boot_protocol; // Boot mouse or keyboard
bool boot_mode; // default = false (Report)

CFG_TUSB_MEM_ALIGN uint8_t epin_buf[XINPUT_OUT_SIZE];
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[XINPUT_OUT_SIZE];
} xinputd_interface_t;

CFG_TUSB_MEM_SECTION static xinputd_interface_t _xinputd_itf[CFG_TUD_XINPUT];

uint8_t endpoint_in = 0;
uint8_t endpoint_out = 0;
uint8_t xinput_out_buffer[XINPUT_OUT_SIZE] = {};
static uint8_t endpoint_in = 0;
static uint8_t endpoint_out = 0;
static uint8_t xinput_out_buffer[XINPUT_OUT_SIZE] = {};
static XInputAuthData * xinputAuthData = nullptr;

/*------------- Helpers -------------*/
static inline uint8_t get_index_by_itfnum(uint8_t itf_num) {
for (uint8_t i = 0; i < CFG_TUD_XINPUT; i++) {
if (itf_num == _xinputd_itf[i].itf_num) return i;
}

return 0xFF;
}

// Move to Proto Enums
typedef enum
{
Expand Down Expand Up @@ -90,15 +64,6 @@ static uint16_t xinput_open(uint8_t rhport, tusb_desc_interface_t const *itf_des
driver_length = sizeof(tusb_desc_interface_t) + (itf_descriptor->bNumEndpoints * sizeof(tusb_desc_endpoint_t));
TU_VERIFY(max_length >= driver_length, 0);

// Find available interface
xinputd_interface_t *p_xinput = NULL;
for (uint8_t i = 0; i < CFG_TUD_XINPUT; i++) {
if (_xinputd_itf[i].ep_in == 0 && _xinputd_itf[i].ep_out == 0) {
p_xinput = &_xinputd_itf[i];
break;
}
}

tusb_desc_interface_t *p_desc = (tusb_desc_interface_t *)itf_descriptor;
// Xbox 360 Interfaces (Control 0x01, Audio 0x02, Plug-in 0x03)
if (itf_descriptor->bInterfaceSubClass == 0x5D &&
Expand All @@ -109,16 +74,11 @@ static uint16_t xinput_open(uint8_t rhport, tusb_desc_interface_t const *itf_des
p_desc = (tusb_desc_interface_t *)tu_desc_next(p_desc);
TU_VERIFY(XINPUT_DESC_TYPE_RESERVED == p_desc->bDescriptorType, 0);
driver_length += p_desc->bLength;

p_desc = (tusb_desc_interface_t *)tu_desc_next(p_desc);
TU_ASSERT(usbd_open_edpt_pair(rhport, (const uint8_t*)p_desc, itf_descriptor->bNumEndpoints,
TUSB_XFER_INTERRUPT, &p_xinput->ep_out, &p_xinput->ep_in), 0);
p_xinput->itf_num = itf_descriptor->bInterfaceNumber;

// Control Endpoints are used for gamepad input/output
if ( itf_descriptor->bInterfaceProtocol == 0x01 ) {
endpoint_in = p_xinput->ep_in;
endpoint_out = p_xinput->ep_out;
TU_ASSERT(usbd_open_edpt_pair(rhport, (const uint8_t*)p_desc, itf_descriptor->bNumEndpoints,
TUSB_XFER_INTERRUPT, &endpoint_out, &endpoint_in), 0);
}
// Xbox 360 Security Interface
} else if (itf_descriptor->bInterfaceSubClass == 0xFD &&
Expand Down
4 changes: 4 additions & 0 deletions src/gp2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ void GP2040::run() {
Gamepad * gamepad = Storage::getInstance().GetGamepad();
Gamepad * processedGamepad = Storage::getInstance().GetProcessedGamepad();
bool configMode = Storage::getInstance().GetConfigMode();

// Start the TinyUSB Device functionality
tud_init(TUD_OPT_RHPORT);

while (1) { // LOOP
this->getReinitGamepad(gamepad);

Expand Down
6 changes: 3 additions & 3 deletions src/gp2040aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ void GP2040Aux::setup() {
}
}

// Initialize our USB manager
USBHostManager::getInstance().start();

// Setup Add-ons
addons.LoadAddon(new DisplayAddon(), CORE1_LOOP);
addons.LoadAddon(new NeoPicoLEDAddon(), CORE1_LOOP);
addons.LoadAddon(new PlayerLEDAddon(), CORE1_LOOP);
addons.LoadAddon(new BoardLedAddon(), CORE1_LOOP);
addons.LoadAddon(new BuzzerSpeakerAddon(), CORE1_LOOP);

// Initialize our USB manager
USBHostManager::getInstance().start();

// Ready to sync Core0 and Core1
isReady = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main() {

// Sync Core0 and Core1
while(gp2040Core1->ready() == false ) {
tud_task();
__asm volatile ("nop\n");
}
gp2040Core0->run();

Expand Down
1 change: 0 additions & 1 deletion src/usbhostmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
void USBHostManager::start() {
// This will happen after Gamepad has initialized
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);
Expand Down

0 comments on commit 10f8bc4

Please sign in to comment.