Skip to content

USB host keyboard resume fix - #11968

Open
FoamyGuy wants to merge 1 commit into
adafruit:mainfrom
FoamyGuy:keep_core_keyboard_endpoint
Open

FoamyGuy wants to merge 1 commit into
adafruit:mainfrom
FoamyGuy:keep_core_keyboard_endpoint

Conversation

@FoamyGuy

Copy link
Copy Markdown
Collaborator

This fixes an issue where code that detaches, uses, and then attempts to re-attach the USB host keyboard when it ends leaves the keyboard in a non-working state.

Here is a reproducer that illustrates this issue on the Fruit Jam. If you set this as code.py on 11.0.0-beta.0 it will run once, you can type and see reports. After it ends the keyboard is unresponsive, ctrl-C and ctrl-D do not work. With the changes from this branch and the same code.py the keyboard continues working after code.py ends. ctrl-C and ctrl-D work as expected to access REPL and re-launch code.py.

import array
import time

import adafruit_usb_host_descriptors
import usb.core
import time

time.sleep(5)
keyboard = None
interface = endpoint = None
for device in usb.core.find(find_all=True):
    interface, endpoint = adafruit_usb_host_descriptors.find_boot_keyboard_endpoint(device)
    if endpoint is not None:
        keyboard = device
        break
if keyboard is None:
    raise RuntimeError("no USB boot keyboard found on the host port")
print("keyboard %04x:%04x, interface %d, endpoint 0x%02x"
      % (keyboard.idVendor, keyboard.idProduct, interface, endpoint))

# Take the keyboard from the keyboard workflow and read it directly.
if keyboard.is_kernel_driver_active(interface):
    keyboard.detach_kernel_driver(interface)
keyboard.set_configuration()

print("Press some keys within 5 seconds...")
buf = array.array("B", [0] * 64)
end = time.monotonic() + 5
while time.monotonic() < end:
    try:
        n = keyboard.read(endpoint, buf, timeout=100)  # opens the endpoint
    except usb.core.USBTimeoutError:
        continue
    print("report:", " ".join("%02x" % b for b in buf[:n]))

# Give the keyboard back to the keyboard workflow, as a program should when it's done.
keyboard.attach_kernel_driver(interface)
print("attach_kernel_driver() done. code.py ends now: type on the USB keyboard at the REPL.")

@dhalbert
dhalbert requested a review from tannewt November 26, 2026 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant