[HOWTO] Odroid as a USB HID keyboard

Post Reply
mad_ady
Posts: 9090
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 597 times
Been thanked: 584 times
Contact:

[HOWTO] Odroid as a USB HID keyboard

Post by mad_ady »

I've compiled together a few scripts that can be used to turn your micro-usb enabled Odroid (C1, C2, C4, N2) into a HID keyboard, so that you can inject keystrokes into a different system. I personally use it to keep the screensaver off (and look busy) on a work PC. :lol:

First of all - all the thanks for this go to @ashren0 who put together the script to set up a keyboard/joypad (and saved the script to pastebin :D).

Here is the code: https://github.com/mad-ady/linux-gadget-hid

You will need a device with a microusb host port (e.g. Odroid C1, C2, C4, N2), and recent-enough kernel (>3.19) with support for libcomposite and usb_f_hid. Check that your kernel has support for them (either built-in or as modules):

Code: Select all

$ zcat /proc/config.gz | egrep -i "libcomposite|usb_f_hid"
CONFIG_USB_LIBCOMPOSITE=y
CONFIG_USB_F_HID=y

Dependencies:

* python3
* https://github.com/aagallag/hid_gadget_test

How-to:

* Edit create-hid.py and set vendor and description to the strings you want
* Create a keyboard as a HID device:

Code: Select all

$ sudo ./create-hid.py keyboard
Leave this process running (e.g. in the background). You can check on the USB host that the device is recognized as a keyboard when plugging in the USB cable:

Code: Select all

$ dmesg
...
[ 6377.913325] usb 3-3: new high-speed USB device number 3 using xhci_hcd
[ 6378.062977] usb 3-3: New USB device found, idVendor=16c0, idProduct=0488, bcdDevice= 1.00
[ 6378.062981] usb 3-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 6378.062984] usb 3-3: Product: Odroid Keyboard HID
[ 6378.062986] usb 3-3: Manufacturer: Gadget
[ 6378.062988] usb 3-3: SerialNumber: fedcba9876543210
[ 6378.065886] input: Gadget Odroid Keyboard HID as /devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.0/0003:16C0:0488.0004/input/input33
[ 6378.129950] hid-generic 0003:16C0:0488.0004: input,hidraw3: USB HID v1.01 Keyboard [Gadget Odroid Keyboard HID] on usb-0000:00:14.0-3/input0
* Edit alt-tab.py and set the correct paths to hid_gadget_test binary and hid (should be /dev/hidg0 if you're running only one instance)
* Edit alt-tab.py to send the key sequences you want. You can see hid_gadget_test's syntax by running it:

Code: Select all

./hid_gadget_test /dev/hidg0 keyboard
* Run sudo ./alt-tab.py
* Plugging and unplugging the USB cable does not stop the processes!

Troubleshooting

* make sure the kernel has the necessary modules (default kernels don't!)
* make sure the microusb port is in OTG mode (not host) (Set the dr_mode to dr_mode = "peripheral"). You might need to tweak the DTB as described here: viewtopic.php?f=139&t=36602 (this was not needed for me on a C4!).
* you can see keystrokes with evtest on the USB host device
* viewtopic.php?t=30267
* viewtopic.php?p=270591


Future plans
I'm currently using it on an Odroid C4 running Kernel 4.9 (Ubuntu 20.04). I'd like to move the code to a C1 which gathers dust in a drawer, but I need to find a suitable OS that has a recent-enough kernel... More improvements would be having a way to pipe arbitrary text/characters from external processes (e.g. inject on demand), and also add support to emulate a mouse (so I can build a mouse jiggler). Ideas and feedback welcome.
These users thanked the author mad_ady for the post:
tobetter (Fri Jan 08, 2021 9:03 pm)

User avatar
odroid
Site Admin
Posts: 36468
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English, Korean
ODROIDs: ODROID
Has thanked: 1451 times
Been thanked: 990 times
Contact:

Re: [HOWTO] Odroid as a USB HID keyboard

Post by odroid »

Very nice indeed. :D

mad_ady
Posts: 9090
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 597 times
Been thanked: 584 times
Contact:

Re: [HOWTO] Odroid as a USB HID keyboard

Post by mad_ady »

Ok, I took some time to try it with my old C1+, running the latest armbian (with kernel 5.9). The good news is that Armbian kernel comes with the necessary modules, so no kernel recompilation is necessary. The bad news is that because the C1 with mainline kernel does not support USB hotplugging (or needs to have at least one device connected for the bus to activate), it doesn't work. I even enabled the OTG port and set it to peripheral in the DTB, but there was no change... So, scratch C1 from the list...

Code: Select all

fdtput -t s /boot/dtb/meson8b-odroidc1.dtb /soc/usb@c9040000 dr_mode peripheral
fdtput -t s /boot/dtb/meson8b-odroidc1.dtb /soc/usb@c9040000 status okay
Edit: I had forgotten about the OTG jumper. I removed it and redid my tests. Still nothing :(

User avatar
rooted
Posts: 8280
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 728 times
Been thanked: 307 times
Contact:

Re: [HOWTO] Odroid as a USB HID keyboard

Post by rooted »

Nice write-up, thanks

Post Reply

Return to “Ubuntu (All Linux'es)”

Who is online

Users browsing this forum: No registered users and 3 guests