n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: Pi Zero as a USB keyboard (HID gadget)

Sat Jun 16, 2018 11:18 pm

RDPUser wrote:
Sun Apr 08, 2018 7:36 pm
I've also found this tutorial https://www.rmedgar.com/blog/using-rpi- ... definition and https://gist.github.com/gbaman/50b6cca61dd1c3f88f41 and I'm sure everything works, so that the Pi is recognized as keyboard. But how do I send keystrokes or mouse input to the PC?
Has anyone actually gotten this to work?

I followed this tutorial:

http://isticktoit.net/?p=1383

which seems to have basically the same content as the first URL you quoted (the one at medgar.com).

I started with a Zero W that I had working as an ethernet gadget. As far as I can tell, the only thing I needed to do to make that work is put dtoverlay=dwc2 in config.txt. Following the tutorial above, I put these two lines in /etc/modules

dwc2
libcomposite

These are the only non-comment lines in the file.

Upon reboot, the directory /sys/kernel/config/usb-gadget now exists (it did not exist before). So, this part is working. Note, incidentally, that the ethernet gadget functionality is still there - still working. Do I need to turn that off somehow?

Next, I created this executable file (note that I changed all references to iStick or whatever to something more neutral - the device is now called "mykeyboard" (as recommend in the URL that you quote)):

Code: Select all

#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p mykeyboard
cd mykeyboard
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "Tobias Girstmair" > strings/0x409/manufacturer
echo "mykeyboard USB Device" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
# Add functions here
# see gadget configurations below
mkdir -p functions/hid.usb0
echo 1 > functions/hid.usb0/protocol
echo 1 > functions/hid.usb0/subclass
echo 8 > functions/hid.usb0/report_length
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.usb0/report_desc
ln -s functions/hid.usb0 configs/c.1/
# End functions
ls /sys/class/udc > UDC
When I run this (as root), it seems to work, but fails (at the end) with:

ls: device write protected or busy

(something like that - exact wording may not be right - but it was definitely "ls" that failed - that is the last command in the script)

And there is no file: /dev/hidg0
(I'm not sure where that name comes from, but it is my understanding that that is supposed to be the end result of all this messing about).

I also don't understand what the point of that "ls" command is, but apparently it is critical.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
thagrol
Posts: 1150
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pi Zero as a USB keyboard (HID gadget)

Sun Jun 17, 2018 4:24 pm

I've got the isticktoit tutorial working. My code is here: https://github.com/thagrol/pwdgen/blob/ ... idsetup.sh for comparison.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: Pi Zero as a USB keyboard (HID gadget)

Sun Jun 17, 2018 4:29 pm

thagrol wrote:
Sun Jun 17, 2018 4:24 pm
I've got the isticktoit tutorial working. My code is here: https://github.com/thagrol/pwdgen/blob/ ... idsetup.sh for comparison.
Thanks. I'll give it a go at some point, and let you know how it comes out.

BTW, do you know what the point of the 'ls' command at the end is? It doesn't seem to be doing anything particularly useful.

Also, do you know how/where the actual device name (hidg0, I think) is specified?
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: Pi Zero as a USB keyboard (HID gadget)

Sun Jun 17, 2018 10:08 pm

Well, it still doesn't work. I downloaded your script and ... :

Code: Select all

# ./hidsetup.sh 
ls: write error: Device or resource busy
# dir /dev/h*
/dev/hwrng
See also:

Code: Select all

# grep '^[^#]' /boot/config.txt
dtparam=audio=on
dtoverlay=dwc2
enable_uart=1
# cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

# These two lines added: 6/16/18
dwc2
libcomposite
Is there any other file or setup that needs to be done?

Aha! I did a little more checking, and here's the other piece:

Code: Select all

# cat /boot/cmdline.txt 
dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether
The piece at the end is what makes the ethernet gadget work. Do I have to remove that?
Is there a similar piece needed for the HID gadget to work?
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
thagrol
Posts: 1150
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pi Zero as a USB keyboard (HID gadget)

Sun Jun 17, 2018 10:47 pm

n67 wrote:
Sun Jun 17, 2018 10:08 pm

Code: Select all

# cat /boot/cmdline.txt 
dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether
The piece at the end is what makes the ethernet gadget work. Do I have to remove that?
Is there a similar piece needed for the HID gadget to work?
You can't have more than one usb gadget module loaded at a time. replace "g_ether" with "libcomposite". If you want both, yoou'll have to combine the apropriate sections of the tutorial. Yes it's doable (I've done it) but you'll likely have driver issues with windows hosts.

The hidg0 thing comes from udev I think. Might be worth your looking at the other files in that git hub repository, specifically 10-hidg.rules

And, no, I don't know why the ls command is there, but it does appear to be necessary.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: Pi Zero as a USB keyboard (HID gadget)

Sun Jun 17, 2018 11:05 pm

OK - that makes sense.

FWIW, there doesn't seem to be any reference to this in the StickIt tut. I just loaded the page and searched for "cmdline" and didn't find any.

Also, how much more complicated is it to do both (both hid and ethernet).

I'm not using Windows, so that is not a consideration.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: Pi Zero as a USB keyboard (HID gadget)

Mon Jun 18, 2018 12:39 am

Getting closer...

I changed g_ether to libcomposite and rebooted.

Note: I am connected to the Pi Zero via serial (that's my only connection to the board now).

Now, when I run hidsetup.sh, I get a slew of messages from the kernel, ending with:
Message from syslogd@zero at Jun 17 00:31:46 ...
kernel:[ 72.409930] 3fa0: c000fe40 c0146144 0000000d b6fd5000 00000001 b6fd5000 0000000d 00000000

Message from syslogd@zero at Jun 17 00:31:46 ...
kernel:[ 72.423665] 3fc0: 0000000d b6fd5000 b6f4cb40 00000004 0000000d b6fd5000 0000000d 00000000

Message from syslogd@zero at Jun 17 00:31:46 ...
kernel:[ 72.437397] 3fe0: 00000000 bec4378c b6e7a008 b6ed189c 60000010 00000001 00020009 30796870

Message from syslogd@zero at Jun 17 00:31:46 ...
kernel:[ 72.616009] Code: ebff044b e2506000 0a000013 e5952024 (e1d230d2)
./hidsetup.sh: line 32: 632 Segmentation fault ls /sys/class/udc > UDC
And still no (new) device in /dev/h*.

Edit: The previous test was with cmdline.txt ending in: dwc2,libcomposite

So, I tried removing that (so that cmdline.txt ends with: rootwait) - assuming that the modules would get loaded by modprobe in the script. But the same result happened.

Incidentally, after rebooting, but not yet running the script, I did "lsmod" and found that dwc2 and libcomposite were already loaded (even though there was no reference to it in cmdline.txt and the script had not yet been run):
# lsmod
Module Size Used by
bnep 11949 2
hci_uart 21190 1
btbcm 7885 1 hci_uart
bluetooth 374820 22 hci_uart,bnep,btbcm
brcmfmac 213463 0
brcmutil 9063 1 brcmfmac
cfg80211 525742 1 brcmfmac
snd_bcm2835 23131 0
rfkill 21373 4 bluetooth,cfg80211
snd_pcm 97825 1 snd_bcm2835
snd_timer 22706 1 snd_pcm
snd 68784 3 snd_timer,snd_bcm2835,snd_pcm
bcm2835_gpiomem 3791 0
uio_pdrv_genirq 3718 0
uio 10166 1 uio_pdrv_genirq
fixed 3029 0
libcomposite 49619 0
dwc2 122113 0
udc_core 40356 2 dwc2,libcomposite
ipv6 384468 20
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

AssetBurned
Posts: 11
Joined: Wed Mar 02, 2016 1:59 am

Re: Pi Zero as a USB keyboard (HID gadget)

Wed Aug 01, 2018 8:37 pm

Hi,

And did you guys found any solution for this?

I would like to setup a new raspberry pi zero with networking and HID abilities. My old one is still with a Jessie kernel and I'm pretty much afraid to perform any updates to its software.
Every time I try to set this up with a stretch kernel I can't get the keyboard part to work :-/

CU AssetBurned

n67
Posts: 939
Joined: Mon Oct 30, 2017 4:55 pm

Re: Pi Zero as a USB keyboard (HID gadget)

Thu Aug 09, 2018 8:30 pm

There really doesn't seem to be any "solution".

It just works for some people and not for others.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

grundyoso
Posts: 1
Joined: Sat Dec 29, 2018 11:28 am

Re: Pi Zero as a USB keyboard (HID gadget)

Sat Dec 29, 2018 11:51 am

davidcoton wrote:
Sun Mar 26, 2017 11:54 am
The problem is that you can't run USB gadget mode through a Pi-connected hub -- that takes out the B models. And since there is only one USB port on the SOC, the Zeros and the A models can't have a USB peripheral and run in gadget mode. The Zero power socket does not have any data connections.

So for your project you would need to find a card reader that connects some way other than USB.
You could use the ACR1255U-J1 which can connect over BLE... we offer a JavaScript SDK for easy integration.

Return to “Advanced users”