1

Please comment if you think I can improve my question (like rephrase, add other tags) . I'm really at a loss at the moment.

There are many online instructions explaining how to make a raspberry pi zero behave like a USB keyboard / mouse / etc.

A few years ago I used my pi as a USB keyboard by just following instructions, that I found on the net everything was working fine.

Example: https://randomnerdtutorials.com/raspberry-pi-zero-usb-keyboard-hid/

I installed a new OS (Raspbian GNU/Linux 11 (bullseye)), which has a more recent kernel (6.1.21+) and all the instructions that I find don't work anymore. Whenever I perform the final command
ls /sys/class/udc > UDC
I get an error message:

ls: write error: Device or resource busy

Summary of what I tried.

  • add "dtoverlay=dwc2" to /boot/config.txt
  • add dwc2 and libcomposite to /etc/modules
  • add modules-load=dwc2,g_ether at the end of /boot/cmdline.txt

I created a script that shall be called after bootup to setup your usb device:

Example version:

#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p isticktoit
cd isticktoit
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 "iSticktoit.net 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
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

If my web searches are correct it seems that some things changed in newer kernels and therefore the old instructions don't work anymore. However I didn't find any article guiding me to a solution.

Does anyone have a raspberry pi zero W with a halfways recent distribution, that can act as a USB keyboard / mouse and have Wifi enabled at the same time?

I don't insist on Raspian.

In fact all that I want is a device, that can simulate mouse movements / clicks and be controlled via Wifi. in fact the major use case is even more simple.

I just want to be able to wake up a laptop, that is in standby mode and that doesn't have a working wake on lan feature. Send a command over Wwifi to the pizero and let it do a keyboard press / mouse click to wake it up would be all I really need.

3
  • kernel.org/doc/Documentation/usb/gadget_configfs.txt dates from 25th April 2013 not sure whether there are some minor changes since and where one could find the documentation
    – gelonida
    Commented May 14 at 21:47
  • do you have other configuration on the RPi Zero W.? Commented May 29 at 9:53
  • I have lots of configuration, but I don't really know what you are looking for except kernel version. you want full content of /boot/cmdline.txt or /boot/config.txt or output of lsmod? please tell me what could help and I'll add it to my question
    – gelonida
    Commented May 30 at 14:28

1 Answer 1

1

I have a RPi Zero W connected to a RPi Zero 2W in gadet mode for ethernet only through a powered usb hub. I never understood it as a keyboard. The cmdline.txt should also include modules-load=dwc2,g_ether modules-load=dwc2,g_hid. But this is my only configuration on the RPi Zero W, (if that is of any help):

pi@RPi0:~ $ cat /boot/cmdline.txt
console=serial0,115200 console=tty1 root=/dev/mmcblk0p8 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet splash plymouth.ignore-serial-consoles

pi@RPi0:~ $ cat /boot/config.txt | grep -v -E '^$|#'
hdmi_ignore_edid=0xa5000080
hdmi_cvt 800 480 60 6 0 0 1
hdmi_group=2
hdmi_mode=87
hdmi_blanking=1
hdmi_drive=2
[cm4]
otg_mode=1
[pi4]
arm_boost=1
[all]
dtoverlay=dwc2,dr_mode=peripheral
dtparam=act_led_activelow=off
gpu_mem=32

pi@RPi0:~ $ pinfo
RPi0  Raspberry Pi Zero W Rev 1.1 32bit
Raspbian 11.9 bullseye 6.1.21+
Mem 461M Swap 1,412M Gpu 32M
usb0 UP 192.168.9.10 wlan0 UP 192.168.0.10

My display is different and pinfo is my script to display pi info.

$ dmesg | grep -E 'g_hid|g_ether'

[ 0.000000] Kernel command line: coherent_pool=1M 8250.nr_uarts=0 snd_bcm2835.enable_headphones=0 bcm2708_fb.fbwidth=800 bcm2708_fb.fbheight=480 bcm2708_fb.fbswap=1 smsc95xx.macaddr=B8:27:EB:08:6D:9A vc_mem.mem_base=0x1ec00000 vc_mem.mem_size=0x20000000 console=ttyS0,115200 console=tty1 root=/dev/mmcblk0p8 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether modules=dwc2,g_hid quiet splash plymouth.ignore-serial-consoles [ 0.000000] Unknown kernel command line parameters "splash modules-load=dwc2,g_ether modules=dwc2,g_hid", will be passed to user space.

[ 2.930752] modules-load=dwc2,g_ether

[ 2.930762] modules=dwc2,g_hid

[ 11.119180] g_ether gadget.0: Ethernet Gadget, version: Memorial Day 2008 [ 11.119211] g_ether gadget.0: g_ether ready [ 11.119383] dwc2 20980000.usb: bound driver g_ether

7
  • thanks for this info. Do you have to do any writes at power up to /sys/kernel/config/usb_gadget/ if yes, then these writes might help me to see what's wrong with mine. My guess it that something change there between the kernels or that some ownership issues have to be fixed prior to writing to /sys/kernel/config/usb_gadget/
    – gelonida
    Commented May 25 at 14:56
  • No, I think that was before BUSTER for kernel config. I never did that.
    – bstipe
    Commented May 26 at 22:10
  • One piece of the puzzle, to load 2 modules, two modules-load=... are required. The next part is the driver??
    – bstipe
    Commented May 27 at 13:40
  • I am at a cmplete loss. If you don't write to /sys/kernel/config/usb_gadget/ then you must have the same information somewhere else. If not how shall I be able specify things like vendorid / device id the type of the device I emulate and it's report configuration. perhaps this works as the ethernet gadget device is predefined.
    – gelonida
    Commented May 27 at 15:38
  • in any cased thanks a lot. This week I don't have time to test whether having two modules-load statements fix the other issues or not. Will keep you updated and update the answer if this solves the problem
    – gelonida
    Commented May 27 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.