Skip to content

Instantly share code, notes, and snippets.

@morwy
Last active July 29, 2024 14:16
Show Gist options
  • Select an option

  • Save morwy/b71fac4fa36e30f303980828aecf055d to your computer and use it in GitHub Desktop.

Select an option

Save morwy/b71fac4fa36e30f303980828aecf055d to your computer and use it in GitHub Desktop.
usb-proxy configuration on Raspberry Pi 4

usb-proxy configuration on Raspberry Pi 4

Raspberry Pi

  1. Download and install Rasperry Pi Imager application from here.
  2. Insert MicroSD card into your computer SD card reader.
  3. Select RASPBERRY PI OS LITE (64-BIT) Bullseye and newly inserted MicroSD card in Rasperry Pi Imager application.
  4. Configure required Raspberry Pi settings by clicking gear icon (SSH, hostname, etc.).
  5. Click Write and wait until completion.
  6. Safely eject MicroSD card from the SD card reader and insert into Raspberry Pi.
  7. Boot the Raspberry Pi.

Access to USB devices

  1. Create file at path:
/etc/udev/rules.d/100-usb-proxy.rules
  1. Enter following text into file and save it:
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="*", ATTR{idProduct}=="*", MODE="0666", NAME="bus/usb/$env{BUSNUM}/$env{DEVNUM}", RUN+="/bin/chmod 0666 %c"
  1. Restart udev with following command:
udevadm control --reload-rules && udevadm trigger

raw-gadget

Based on the following original instructions.

  1. Log in into Raspberry Pi via SSH.
  2. Install required dependencies:
sudo apt update && sudo apt install git bc bison flex libssl-dev libncurses5-dev -y && sudo wget https://raw.githubusercontent.com/RPi-Distro/rpi-source/master/rpi-source -O /usr/local/bin/rpi-source && sudo chmod +x /usr/local/bin/rpi-source && /usr/local/bin/rpi-source -q --tag-update && rpi-source
  1. Clone original Git repository.
git clone https://github.com/xairy/raw-gadget.git
  1. Clone original Git repository.
cd raw-gadget/raw_gadget && make
  1. Following error may happen:
raw_gadget.c:548:8: error: implicit declaration of function ‘usb_gadget_probe_driver’;

Rename all entries of usb_gadget_probe_driver() to usb_gadget_register_driver() and rebuild.

  1. Install newly build kernel module:
./insmod.sh
  1. Verify that kernel module somewhat started to work:
ls -l /dev/ 
# "/dev/raw-gadget" shall appear in the list.
  1. Edit insmod.sh to following for easier automatic startup:
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0

script_dir="$( dirname -- "$BASH_SOURCE"; )";

set -eux

sudo modprobe udc_core
sudo insmod $script_dir/raw_gadget

usb-proxy

Based on the following original instructions.

  1. Log in into Raspberry Pi via SSH.
  2. Install required dependencies:
sudo apt install libusb-1.0-0-dev libjsoncpp-dev -y && wget https://github.com/WiringPi/WiringPi/releases/download/2.61-1/wiringpi-2.61-1-arm64.deb && sudo apt install ./wiringpi-2.61-1-arm64.deb
  1. Enabled raw-gadget kernel module access:
echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txt && echo "dwc2" | sudo tee -a /etc/modules && sudo reboot
  1. Verify that access to raw-gadget kernel module has appeared:
ls /sys/class/udc/ 
# Should echo "fe980000.usb".

cat /sys/class/udc/fe980000.usb/uevent 
# Should echo "USB_UDC_NAME=fe980000.usb".
  1. Clone original Git repository.
git clone https://github.com/morwy/usb-proxy-rpi-gpio.git
  1. Clone original Git repository.
cd usb-proxy-rpi-gpio && make
  1. Test newly built application.
./usb-proxy --device=fe980000.usb --driver=fe980000.usb --vendor_id=044f --product_id=b66e
  1. Create a bash script start.sh for easier future startup:
#!/bin/bash
script_dir="$( dirname -- "$BASH_SOURCE"; )";

sudo $script_dir/usb-proxy --device=fe980000.usb --driver=fe980000.usb --vendor_id=044f --product_id=b66e --enable_injection --injection_file=$script_dir/injection-rpi-gpio.json &

Autostart on boot

  1. Create a new text file with 0644 permissions:
sudo nano /lib/systemd/system/usbproxy.service && sudo chmod 644 /lib/systemd/system/usbproxy.service
  1. Add following text to the file:
[Unit]
Description=Starting raw-gadget and usb-proxy at boot
After=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=true

# Load raw-gadget kernel module at first and then start usb-proxy.
ExecStart=/home/pi/raw-gadget/raw_gadget/insmod.sh
ExecStart=/home/pi/usb-proxy-rpi-gpio/start.sh

ExecReload=sudo rmmod raw_gadget
ExecReload=pkill -f "usb-proxy"
ExecReload=/home/pi/raw-gadget/raw_gadget/insmod.sh
ExecReload=/home/pi/usb-proxy-rpi-gpio/start.sh

# In case of stop or failure unload raw-gadget kernel module and kill usb-proxy.
ExecStopPost=sudo rmmod raw_gadget
ExecStopPost=pkill -f "usb-proxy"

[Install]
WantedBy=multi-user.target
  1. Restart systemd service:
sudo systemctl daemon-reload
  1. Enable autostart of newly created service:
sudo systemctl enable usbproxy.service
  1. Reboot Raspberry Pi:
sudo reboot now
  1. Verify that new service started and worked well:
sudo systemctl status usbproxy.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment