tmk / tmk_keyboard

Keyboard firmwares for Atmel AVR and Cortex-M
3.96k stars 1.7k forks source link

USB-USB: Poker X startup issue #770

Closed tmk closed 6 months ago

tmk commented 7 months ago

Poker X is not enumerated when plugged in with converter simultaneously.

Protocol of switching USB to PS/2 causes probably. It seems to turn into PS/2 mode when USB interface is not configured for some period after plugin. After the keyboard enters into PS/2 mode, both D+ and D- are pull-up'd and it cannot be bus-reset anymore.

Holtek MCU: HT82K94E https://www.holtek.com/webapi/116711/HT82K94xv240.pdf

Converter startup code

This takes long and prevents host functions of MAX3421 from configuring the keyboard.

https://github.com/tmk/tmk_keyboard/blob/294403ffbfe0c1a3c3d2292e81fd99d1ee56c59a/tmk_core/protocol/lufa/lufa.c#L794-L809

500ms delay in USB enumeration

This may causes also.

https://github.com/tmk/USB_Host_Shield_2.0/commit/e37ed6cf28b45db8022fd513b338d22c5515afed

tmk commented 7 months ago

FIX for startup

call usb_host.Task() in statup delay loop.

diff --git a/converter/usb_usb/usb_usb.cpp b/converter/usb_usb/usb_usb.cpp
index e649a9a2..bb6c89ef 100644
--- a/converter/usb_usb/usb_usb.cpp
+++ b/converter/usb_usb/usb_usb.cpp
@@ -243,3 +243,17 @@ void hook_usb_suspend_loop(void)
         matrix_scan();
     }
 }
+
+void hook_usb_startup_wait_loop(void)
+{
+    usb_host.Task();
+
+    static uint8_t usb_state = 0;
+    if (usb_state != usb_host.getUsbTaskState()) {
+        usb_state = usb_host.getUsbTaskState();
+        xprintf("u:%02X\n", usb_state);
+        if (usb_state == USB_STATE_RUNNING) {
+            xprintf("s:%s\n", usb_host.getVbusState()==FSHOST ? "f" : "l");
+        }
+    }
+}

https://github.com/tmk/tmk_keyboard/commit/d825356dd5a3486e440390b1724e756fcfacc620

FIX for 500ms delay

diff --git a/Usb.cpp b/Usb.cpp
index f87b517..2038018 100644
--- a/Usb.cpp
+++ b/Usb.cpp
@@ -526,7 +526,7 @@ void USB::Task(void) //USB state machine
                                         usb_task_state = USB_STATE_CONFIGURING;
                                  */
                                 usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET;
-                                delay = (uint32_t)millis() + 500;
+                                delay = (uint32_t)millis() + 20;
                         }
                         break;
                 case USB_ATTACHED_SUBSTATE_WAIT_RESET:

Matrix Vita keyboard still needs this 500ms delay, perhaps.

https://geekhack.org/index.php?topic=69169.msg3073431

https://github.com/tmk/USB_Host_Shield_2.0/commit/e37ed6cf28b45db8022fd513b338d22c5515afed

This is left untouched for now.

tmk commented 6 months ago

Prohibit MAX3421 suspension at startup

This and d825356 fixed the Poker X startup problem.

86157dc4

commit 86157dc41d73914c5db1c4fc4b5f78f1bb0e6514
Author: tmk <hasu@tmk-kbd.com>
Date:   Sat Dec 30 00:03:02 2023 +0900

    usb_usb: Prohibit MAX3421 suspend at startup #770

    MAX3421 suspend/resume at startup can:
    - prevent LUFA startup
    - prevent Poker X from being enumerated

diff --git a/converter/usb_usb/usb_usb.cpp b/converter/usb_usb/usb_usb.cpp
index 95a3c103..aed3ff6e 100644
--- a/converter/usb_usb/usb_usb.cpp
+++ b/converter/usb_usb/usb_usb.cpp
@@ -226,8 +226,16 @@ void led_set(uint8_t usb_led)
     if (kbd4.isReady()) kbd4.SetLed(&usb_led);
 }

+static bool init_done = false;
+void hook_late_init()
+{
+    dprintf("[i]");
+    init_done = true;
+}
+
 void hook_usb_suspend_loop(void)
 {
+    dprintf("[s]");
 #ifndef TMK_LUFA_DEBUG_UART
     // This corrupts debug print when suspend
     suspend_power_down();
@@ -243,6 +251,8 @@ static uint8_t _led_stats = 0;
 void hook_usb_suspend_entry(void)
 {
     dprintf("[S]");
+    if (!init_done) return;
+
     matrix_clear();
     clear_keyboard();

@@ -260,6 +270,8 @@ void hook_usb_suspend_entry(void)
 void hook_usb_wakeup(void)
 {
     dprintf("[W]");
+    if (!init_done) return;
+
     suspend_wakeup_init();

 #ifdef UHS2_POWER_SAVING