diff --git a/headers/addonmanager.h b/headers/addonmanager.h index 62cec6a13..079349d34 100644 --- a/headers/addonmanager.h +++ b/headers/addonmanager.h @@ -2,6 +2,7 @@ #define _ADDONMANAGER_H_ #include "gpaddon.h" +#include "usbaddon.h" #include #include @@ -9,7 +10,6 @@ enum ADDON_PROCESS { CORE0_INPUT, CORE0_USBREPORT, - CORE0_LOOP, CORE1_LOOP }; @@ -22,7 +22,8 @@ class AddonManager { public: AddonManager() {} ~AddonManager() {} - bool LoadAddon(GPAddon*, ADDON_PROCESS); + void LoadAddon(GPAddon*, ADDON_PROCESS); + void LoadUSBAddon(USBAddon*, ADDON_PROCESS); void PreprocessAddons(ADDON_PROCESS); void ProcessAddons(ADDON_PROCESS); GPAddon * GetAddon(std::string); // hack for NeoPicoLED diff --git a/headers/addons/keyboard_host.h b/headers/addons/keyboard_host.h index 3fd379789..af9c61b04 100644 --- a/headers/addons/keyboard_host.h +++ b/headers/addons/keyboard_host.h @@ -40,7 +40,7 @@ class KeyboardHostAddon : public USBAddon { public: virtual bool available(); virtual void setup(); // KeyboardHost Setup - virtual void process() {} // KeyboardHost Process + virtual void process() {} // KeyboardHost Process virtual void preprocess(); virtual std::string name() { return KeyboardHostName; } // USB Add-on Features @@ -51,6 +51,7 @@ class KeyboardHostAddon : public USBAddon { virtual void get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) {} private: bool _keyboard_host_enabled; + uint8_t getKeycodeFromModifier(uint8_t modifier); void process_kbd_report(uint8_t dev_addr, hid_keyboard_report_t const *report); GamepadState _keyboard_host_state; KeyboardButtonMapping _keyboard_host_mapDpadUp; diff --git a/headers/addons/pspassthrough.h b/headers/addons/pspassthrough.h index aa81e595b..6275a9520 100644 --- a/headers/addons/pspassthrough.h +++ b/headers/addons/pspassthrough.h @@ -34,7 +34,13 @@ class PSPassthroughAddon : public USBAddon { virtual void get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len); virtual void report_received(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) {} private: - bool ps_device_mounted = false; + uint8_t ps_dev_addr; + uint8_t ps_instance; + int8_t nonce_page; + PS4State passthrough_state; + int8_t send_nonce_part; + uint8_t report_buffer[64]; + bool awaiting_cb; }; #endif // _PSPassthrough_H_ \ No newline at end of file diff --git a/headers/usbhostmanager.h b/headers/usbhostmanager.h index f5964fbf5..566c3bb95 100644 --- a/headers/usbhostmanager.h +++ b/headers/usbhostmanager.h @@ -17,7 +17,8 @@ class USBHostManager { static USBHostManager instance; // Guaranteed to be destroyed. // Instantiated on first use. return instance; } - void init(uint8_t); + void setDataPin(uint8_t); // start USB host (change CPU, setup PIO PICO usb pin) + void start(); void pushAddon(USBAddon *); // If anything needs to update in the gpconfig driver void process(); void hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len); @@ -26,9 +27,13 @@ class USBHostManager { void hid_set_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len); void hid_get_report_complete_cb(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len); private: - USBHostManager() {} + USBHostManager() : tuh_ready(false) { + + } std::vector addons; usb_device_t *usb_device; + uint8_t dataPin; + bool tuh_ready; }; #endif \ No newline at end of file diff --git a/src/addonmanager.cpp b/src/addonmanager.cpp index 8b82c1cad..fa1376c95 100644 --- a/src/addonmanager.cpp +++ b/src/addonmanager.cpp @@ -1,18 +1,30 @@ #include "addonmanager.h" +#include "usbhostmanager.h" -bool AddonManager::LoadAddon(GPAddon* addon, ADDON_PROCESS processAt) { +void AddonManager::LoadAddon(GPAddon* addon, ADDON_PROCESS processAt) { if (addon->available()) { AddonBlock * block = new AddonBlock; addon->setup(); block->ptr = addon; block->process = processAt; addons.push_back(block); - return true; - } - delete addon; // Don't use the memory if we don't have to - return false; + } else { + delete addon; // Don't use the memory if we don't have to + } } +void AddonManager::LoadUSBAddon(USBAddon* addon, ADDON_PROCESS processAt) { + if (addon->available()) { + AddonBlock * block = new AddonBlock; + addon->setup(); + block->ptr = addon; + block->process = processAt; + addons.push_back(block); + USBHostManager::getInstance().pushAddon(addon); + } else { + delete addon; // Don't use the memory if we don't have to + } +} void AddonManager::PreprocessAddons(ADDON_PROCESS processType) { // Loop through all addons and process any that match our type diff --git a/src/addons/keyboard_host.cpp b/src/addons/keyboard_host.cpp index e58f2664b..a5294e940 100644 --- a/src/addons/keyboard_host.cpp +++ b/src/addons/keyboard_host.cpp @@ -2,10 +2,8 @@ #include "storagemanager.h" #include "usbhostmanager.h" -#include "pio_usb.h" - bool KeyboardHostAddon::available() { - const KeyboardHostOptions& keyboardHostOptions = Storage::getInstance().getAddonOptions().keyboardHostOptions; + const KeyboardHostOptions& keyboardHostOptions = Storage::getInstance().getAddonOptions().keyboardHostOptions; return keyboardHostOptions.enabled && isValidPin(keyboardHostOptions.pinDplus) && (keyboardHostOptions.pin5V == -1 || isValidPin(keyboardHostOptions.pin5V)); @@ -16,13 +14,13 @@ void KeyboardHostAddon::setup() { const KeyboardMapping& keyboardMapping = keyboardHostOptions.mapping; if (keyboardHostOptions.pin5V != -1) { - const int32_t pin5V = keyboardHostOptions.pin5V; + const int32_t pin5V = keyboardHostOptions.pin5V; gpio_init(pin5V); gpio_set_dir(pin5V, GPIO_IN); gpio_pull_up(pin5V); } - USBHostManager::getInstance().init((uint8_t)keyboardHostOptions.pinDplus); + USBHostManager::getInstance().setDataPin((uint8_t)keyboardHostOptions.pinDplus); _keyboard_host_enabled = false; _keyboard_host_mapDpadUp.setMask(GAMEPAD_MASK_UP); @@ -76,14 +74,11 @@ void KeyboardHostAddon::preprocess() { } void KeyboardHostAddon::mount(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { - if ( _keyboard_host_enabled == false ) - return; // do nothing if our add-on is not enabled _keyboard_host_enabled = true; } void KeyboardHostAddon::unmount(uint8_t dev_addr) { - if ( _keyboard_host_enabled == true ) - _keyboard_host_enabled = false; + _keyboard_host_enabled = false; } void KeyboardHostAddon::report_received(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ @@ -100,7 +95,7 @@ void KeyboardHostAddon::report_received(uint8_t dev_addr, uint8_t instance, uint process_kbd_report(dev_addr, (hid_keyboard_report_t const*) report ); } -uint8_t getKeycodeFromModifier(uint8_t modifier) { +uint8_t KeyboardHostAddon::getKeycodeFromModifier(uint8_t modifier) { switch (modifier) { case KEYBOARD_MODIFIER_LEFTCTRL : return HID_KEY_CONTROL_LEFT ; case KEYBOARD_MODIFIER_LEFTSHIFT : return HID_KEY_SHIFT_LEFT ; @@ -118,8 +113,6 @@ uint8_t getKeycodeFromModifier(uint8_t modifier) { // convert hid keycode to ascii and print via usb device CDC (ignore non-printable) void KeyboardHostAddon::process_kbd_report(uint8_t dev_addr, hid_keyboard_report_t const *report) { - (void) dev_addr; - _keyboard_host_state.dpad = 0; _keyboard_host_state.buttons = 0; _keyboard_host_state.lx = GAMEPAD_JOYSTICK_MID; @@ -168,4 +161,4 @@ void KeyboardHostAddon::process_kbd_report(uint8_t dev_addr, hid_keyboard_report _keyboard_host_state.rt = 0; } } -} +} \ No newline at end of file diff --git a/src/addons/pspassthrough.cpp b/src/addons/pspassthrough.cpp index afdbfbd70..1e92fd2cd 100644 --- a/src/addons/pspassthrough.cpp +++ b/src/addons/pspassthrough.cpp @@ -4,17 +4,7 @@ #include "CRC32.h" -#include "pio_usb.h" - // Data passed between PS Passthrough and TinyUSB Host callbacks -static uint8_t ps_dev_addr = 0; -static uint8_t ps_instance = 0; -static tusb_desc_device_t desc_device; -static int8_t nonce_page; -static PS4State passthrough_state; -static int8_t send_nonce_part; -static uint8_t report_buffer[64]; -static bool awaiting_cb; bool PSPassthroughAddon::available() { const PSPassthroughOptions& psOptions = Storage::getInstance().getAddonOptions().psPassthroughOptions; @@ -32,7 +22,7 @@ void PSPassthroughAddon::setup() { gpio_pull_up(pin5V); } - USBHostManager::getInstance().init((uint8_t)psOptions.pinDplus); + USBHostManager::getInstance().setDataPin((uint8_t)psOptions.pinDplus); nonce_page = 0; // no nonce yet send_nonce_part = 0; // which part of the nonce are we getting from send? @@ -100,7 +90,6 @@ void PSPassthroughAddon::process() { case PS4State::sending_nonce: if ( !awaiting_cb ) { uint8_t nonce_buffer[64]; - uint32_t crc32; nonce_buffer[0] = PS4AuthReport::PS4_GET_SIGNATURE_NONCE; nonce_buffer[1] = PS4Data::getInstance().nonce_id; // nonce_id nonce_buffer[2] = send_nonce_part; // next_part @@ -119,7 +108,6 @@ void PSPassthroughAddon::process() { void PSPassthroughAddon::mount(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) { ps_dev_addr = dev_addr; ps_instance = instance; - ps_device_mounted = true; // Reset as soon as its connected memset(report_buffer, 0, sizeof(report_buffer)); @@ -131,7 +119,6 @@ void PSPassthroughAddon::mount(uint8_t dev_addr, uint8_t instance, uint8_t const } void PSPassthroughAddon::unmount(uint8_t dev_addr) { - ps_device_mounted = false; nonce_page = 0; // no nonce yet send_nonce_part = 0; // which part of the nonce are we getting from send? awaiting_cb = false; // did we receive the sign state yet @@ -141,7 +128,6 @@ void PSPassthroughAddon::unmount(uint8_t dev_addr) { void PSPassthroughAddon::set_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) { switch(report_id) { case PS4AuthReport::PS4_SET_AUTH_PAYLOAD: - printf("Set Auth Payload\r\n"); if (nonce_page == 5) { nonce_page = 0; passthrough_state = PS4State::signed_nonce_ready; @@ -156,25 +142,16 @@ void PSPassthroughAddon::set_report_complete(uint8_t dev_addr, uint8_t instance, void PSPassthroughAddon::get_report_complete(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, uint16_t len) { switch(report_id) { case PS4AuthReport::PS4_DEFINITION: - printf("Get Definition: "); - for (uint8_t i = 0; i < 48; i++) { - printf("%02x,", report_buffer[i]); } - printf("\r\n"); - if ( PS4Data::getInstance().ps4State == PS4State::nonce_ready) - passthrough_state = PS4State::receiving_nonce; break; case PS4AuthReport::PS4_RESET_AUTH: - printf("Reset Auth!\r\n"); if ( PS4Data::getInstance().ps4State == PS4State::nonce_ready) passthrough_state = PS4State::receiving_nonce; break; case PS4AuthReport::PS4_GET_SIGNING_STATE: - printf("Get signing state!\r\n"); if (report_buffer[2] == 0) passthrough_state = PS4State::sending_nonce; break; case PS4AuthReport::PS4_GET_SIGNATURE_NONCE: - printf("Get signature nonce!\r\n"); memcpy(&PS4Data::getInstance().ps4_auth_buffer[(send_nonce_part-1)*56], &report_buffer[4], 56); if (send_nonce_part == 19) { // 0 = ready, 16 = not ready send_nonce_part = 0; diff --git a/src/gp2040.cpp b/src/gp2040.cpp index 4e24754b3..ab62100ad 100644 --- a/src/gp2040.cpp +++ b/src/gp2040.cpp @@ -99,14 +99,11 @@ void GP2040::setup() { break; } } - // Initialize our ADC (various add-ons) adc_init(); - // Setup USB Host Add-ons - KeyboardHostAddon * keyboardHostAddon = new KeyboardHostAddon(); - if( addons.LoadAddon(keyboardHostAddon, CORE0_INPUT) ) - USBHostManager::getInstance().pushAddon(keyboardHostAddon); + // Setup USB add-ons + addons.LoadUSBAddon(new KeyboardHostAddon(), CORE0_INPUT); // Setup Regular Add-ons addons.LoadAddon(new AnalogInput(), CORE0_INPUT); @@ -146,7 +143,7 @@ void GP2040::run() { // We can't send faster than USB can poll if (nextRuntime > getMicro()) { // fix for unsigned - //sleep_us(50); // Give some time back to our CPU (lower power consumption) + sleep_us(50); // Give some time back to our CPU (lower power consumption) continue; } @@ -179,9 +176,6 @@ void GP2040::run() { tud_task(); // TinyUSB Task update - // Process Loops - addons.ProcessAddons(ADDON_PROCESS::CORE0_LOOP); - nextRuntime = getMicro() + GAMEPAD_POLL_MICRO; } } diff --git a/src/gp2040aux.cpp b/src/gp2040aux.cpp index eae031200..b0240c913 100644 --- a/src/gp2040aux.cpp +++ b/src/gp2040aux.cpp @@ -25,6 +25,9 @@ GP2040Aux::~GP2040Aux() { } void GP2040Aux::setup() { + // Setup USB add-ons + addons.LoadUSBAddon(new PSPassthroughAddon(), CORE1_LOOP); + // Setup Regular Add-ons addons.LoadAddon(new I2CDisplayAddon(), CORE1_LOOP); addons.LoadAddon(new NeoPicoLEDAddon(), CORE1_LOOP); @@ -33,10 +36,9 @@ void GP2040Aux::setup() { addons.LoadAddon(new BuzzerSpeakerAddon(), CORE1_LOOP); addons.LoadAddon(new PS4ModeAddon(), CORE1_LOOP); - // Setup USB add-ons - PSPassthroughAddon * psPassthroughAddon = new PSPassthroughAddon(); - if( addons.LoadAddon(psPassthroughAddon, CORE1_LOOP) ) - USBHostManager::getInstance().pushAddon(psPassthroughAddon); + + + USBHostManager::getInstance().start(); } void GP2040Aux::run() { diff --git a/src/usbhostmanager.cpp b/src/usbhostmanager.cpp index 0eecf2522..f944157ab 100644 --- a/src/usbhostmanager.cpp +++ b/src/usbhostmanager.cpp @@ -4,14 +4,21 @@ #include "tusb.h" #include "host/usbh_classdriver.h" -void USBHostManager::init(uint8_t dataPin) { - set_sys_clock_khz(120000, true); // Set Clock to 120MHz to avoid potential USB timing issues - - pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; - pio_cfg.alarm_pool = (void*)alarm_pool_create(2, 1); // Alarms go to Core1 - pio_cfg.pin_dp = dataPin; - tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg); - tuh_init(BOARD_TUH_RHPORT); +void USBHostManager::setDataPin(uint8_t inPin) { + dataPin = inPin; +} + +void USBHostManager::start() { + if ( !addons.empty() ) { + set_sys_clock_khz(120000, true); // Set Clock to 120MHz to avoid potential USB timing issues + //stdio_init_all(); + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; + pio_cfg.alarm_pool = (void*)alarm_pool_create(2, 1); // Alarms go to Core1 + pio_cfg.pin_dp = dataPin; + tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg); + tuh_init(BOARD_TUH_RHPORT); + tuh_ready = true; + } } void USBHostManager::pushAddon(USBAddon * usbAddon) { // If anything needs to update in the gpconfig driver @@ -20,7 +27,7 @@ void USBHostManager::pushAddon(USBAddon * usbAddon) { // If anything needs to up // Host manager should call tuh_task as fast as possible void USBHostManager::process() { - if ( !addons.empty() ){ + if ( tuh_ready ){ tuh_task(); } } @@ -33,7 +40,7 @@ void USBHostManager::hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t co void USBHostManager::hid_umount_cb(uint8_t daddr, uint8_t instance) { for( std::vector::iterator it = addons.begin(); it != addons.end(); it++ ){ - (*it)->unmount(daddr); + (*it)->unmount(dev_addr); } }