1

Since I have exactly the same issue and no solution so far, please allow me to repost the question by Charlotte J at superuser again on this board:

I am trying to turn Raspberry Pi Zero W as a "boot compatible" keyboard (USB gadget). I've followed this tutorial to make it work in OS, i.e. when plugged into a Mac computer that has been powered on and logged into a user, I can send keystrokes via Raspberry Pi to the Mac by running scripts writing bytes to the device file (/dev/hidg0). The tutorial makes use of two modules: dwc2 and libcomposite.

However, I need the Pi to work as a keyboard in Mac's EFI (BIOS). Specifically, I want to send Command+R keystroke as the Mac is booting up to enter the recovery mode.

[AlanT: My application would be to type in the user password to unlock rootFs encryption and let the macOS boot up to allow ssh login.]

Looking at the official document from the USB org, boot keyboards require support for SET_PROTOCOL and GET_PROTOCOL requests from the host (Appendix B.1 and F.3). I did modify the report descriptor as shown in Appendix B.1.

According to line 496 to 606 in this class in Raspberry Pi Linux source code, it seems like the module supports SET_PROTOCOL and GET_PROTOCOL requests.

However, Pi as a keyboard gadget does not work in EFI; only in OS.

Does libcomposite not include f_hid.c class? If not, does anyone have any insight into how to implement a boot keyboard gadget? I feel like I am missing something.

Thank you very much in advance!

Fyi (this is the descriptor cited by 1):

enter image description here

PS: My current descriptor looks like this (from https://github.com/pikvm/kvmd/blob/master/kvmd/apps/otg/hid/keyboard.py):

KEYBOARD_HID = Hid(
    protocol=1,  # Keyboard protocol
    subclass=1,  # Boot interface subclass

    report_length=8,

    report_descriptor=bytes([
        # https://www.kernel.org/doc/Documentation/usb/gadget_hid.txt

        # Keyboard
        0x05, 0x01,  # USAGE_PAGE (Generic Desktop)
        0x09, 0x06,  # USAGE (Keyboard)
        0xA1, 0x01,  # COLLECTION (Application)

        # Modifiers
        0x05, 0x07,  # USAGE_PAGE (Keyboard)
        0x19, 0xE0,  # USAGE_MINIMUM (Keyboard LeftControl)
        0x29, 0xE7,  # USAGE_MAXIMUM (Keyboard Right GUI)
        0x15, 0x00,  # LOGICAL_MINIMUM (0)
        0x25, 0x01,  # LOGICAL_MAXIMUM (1)
        0x75, 0x01,  # REPORT_SIZE (1)
        0x95, 0x08,  # REPORT_COUNT (8)
        0x81, 0x02,  # INPUT (Data,Var,Abs)

        # Reserved byte
        0x95, 0x01,  # REPORT_COUNT (1)
        0x75, 0x08,  # REPORT_SIZE (8)
        0x81, 0x03,  # INPUT (Cnst,Var,Abs)

        # LEDs output
        0x95, 0x05,  # REPORT_COUNT (5)
        0x75, 0x01,  # REPORT_SIZE (1)
        0x05, 0x08,  # USAGE_PAGE (LEDs)
        0x19, 0x01,  # USAGE_MINIMUM (Num Lock)
        0x29, 0x05,  # USAGE_MAXIMUM (Kana)
        0x91, 0x02,  # OUTPUT (Data,Var,Abs)

        # Reserved 3 bits in output
        0x95, 0x01,  # REPORT_COUNT (1)
        0x75, 0x03,  # REPORT_SIZE (3)
        0x91, 0x03,  # OUTPUT (Cnst,Var,Abs)

        # 6 keys
        0x95, 0x06,  # REPORT_COUNT (6)
        0x75, 0x08,  # REPORT_SIZE (8)
        0x15, 0x00,  # LOGICAL_MINIMUM (0)
        0x25, 0x65,  # LOGICAL_MAXIMUM (101)
        0x05, 0x07,  # USAGE_PAGE (Keyboard)
        0x19, 0x00,  # USAGE_MINIMUM (Reserved)
        0x29, 0x65,  # USAGE_MAXIMUM (Keyboard Application)
        0x81, 0x00,  # INPUT (Data,Ary,Abs)

        0xC0,  # END_COLLECTION
    ]),
)
  • Thanks, power should not be a problem. The pi zero is connected over the PWR port to a power adapter. It is rather a problem with the boot protocol described above. – AlanT Jan 10 at 15:35
  • The pi zero is connected over the PWR port to a power adapter. It is rather a problem ... when you use OTG for the gadget it's a problem because OTG power your device trought USB – Ephemeral Jan 10 at 17:59
  • OK, I removed the power adapter from PWR. The zerow now gets power over OTG. After rebooting the host machine, I simply have to wait for the zerow to fully reboot. Still the keyboard function over OTG does not work on the Mac login screen. – AlanT Jan 10 at 18:14
  • What is your configuration ? Maybe follow this good guide – Ephemeral Jan 10 at 18:18
  • 1
    yes, waited long enough after reboot. pi is up and running. connecting with ssh to works fine. BTW: output of lsusb on macOS is very brief, not comparable to Linux, no useful details. and: I am not using an Arduino, only zerow – AlanT Jan 10 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.