1

I'm following this tutorial for converting RPI to usb gadget. I've modified setup script a little to fit my needs:

err_file=$HOME/usb_setup_err

echo "Time: $(date -Ins)" >> $err_file
echo 'Running setup script' >> $err_file

{
##= Create usb gadget
cd /sys/kernel/config/usb_gadget/
mkdir -p hax_usb
cd hax_usb


##= Provide gadget configuration info
echo 0x1d6b > idVendor
echo 0x0104 > idProduct
echo 0x0100 > bcdDevice
echo 0x0200 > bcdUSB

mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "Tobias Girstmair" > strings/0x409/manufacturer
echo "iSticktoit.net USB Device" > strings/0x409/product

##= Create gadget functions
mkdir -p functions/ecm.usb0
mkdir -p functions/hid.usb0

##== Describe emc function
echo "48:6f:73:74:50:43" > functions/ecm.usb0/host_addr
echo "42:61:64:55:53:42" > functions/ecm.usb0/dev_addr
echo 'Created ethernet functions'

##== Descrive hid function
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\\x
01\\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/re
port_desc
echo 'Created USB HID function'

##= Create gadget configuration
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
echo 'Creating configuration'

##== Add functions to gadget configuration
ln -s functions/ecm.usb0 configs/c.1/
ln -s functions/hid.usb0 configs/c.1/
echo 'Added functions'

ls /sys/class/udc > UDC

ifconfig usb0 192.168.2.152 netmask 255.255.255.0 up
route add -net default gw 192.168.2.150

ifconfig -a
route -n
ls /dev | grep hid

echo 'Done'
} >> $err_file 2>&1

Script works correctly for the most part (usb interface is created, ip address and gateway assigned), but avahi does not see my raspberry when connected to the pc. My network setup looks like this:

img

Host pc interface:

enp3s0f0u6u4u1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.150  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::7dfe:77a0:ed21:f67c  prefixlen 64  scopeid 0x20<link>
        ether 48:6f:73:74:50:43  txqueuelen 1000  (Ethernet)
        RX packets 458  bytes 35426 (34.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 85  bytes 18024 (17.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Raspberry pi interface:

usb0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.2.152  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 42:61:64:55:53:42  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Raspberry pi routing table:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.150   0.0.0.0         UG    0      0        0 usb0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 usb0

Tutorial checklist:

  • [X] Added setup script to rc.local
  • [X] Added required modules to /etc/modules
  • [X] Added dtoverlay=dwc2 to /boot/config.txt

Other info:

  • For raspberry I'm using 2019-04-08-raspbian-stretch-full
  • On desktop I'm using Gentoo Linux
  • No networking configuration other than in script were performed on the RPI
  • When using avahi-browse (I use avahi-browse -atr) I can see enp3s0f0u6u4u1.
  • Pinging RPI results in 100% packet loss but no errors.
  • I cannot ssh to the rpi (ssh pi@192.168.2.152) - it gives me error: ssh: connect to host 192.168.2.152 port 22: No route to host
  • Restarting interface on the host (sudo ifconfig enp3s0f0u6u4u1 down and sudo ifconfig enp3s0f0u6u4u1 up) didn't result in anything.
  • All logs files can be viewed here
  • I've already tried using g_ether - it works but does not fit my needs (I need to have Ethernet and usb hid at the same time `g_ether -- does not allow it).
  • I've omitted rpi-update from tutorial because it was necessary only for old kernel (pre-4.4)
1

I assume you are using a Raspberry Pi Zero.

I only had a glance at this tutorial, tldr; but it is more than 3 years old and seems to be complete outdated. With current Raspbian images you never need to fiddle with the kernel and scripts for config and gadget, serial- and ethernet adapter, keyboard/mouse and mass storage. There are some odd issues with the tutorial:

1) The tutorial is for Raspbian Jessie. It isn't usable for Raspbian Stretch.

2) The tutorial uses rpi-update. You should not use it without cause. Please read Do I still need rpi-update if I am using the latest version of Raspbian?.

3) The tutorial uses /etc/rc.local. Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it.

So I suggest to start over again with Raspbian Stretch Lite. Then just configure two options and it should do. Add dtoverlay=dwc2 to /boot/config.txt and modules-load=dwc2,g_ether to /boot/cmdline.txt. You can look at HowToOTGFast how to do it and for details look at Raspberry Pi Zero OTG Mode. Because I don't have a Raspberry Pi Zero I can't test it but I'm quite sure it also works with Raspbian Stretch.

  • I've already tried using g_ether - it works but does not fit my needs (I need to have Ethernet and usb hid at the same time g_ether -- does not allow it). I'm using rc.local only because it simpler - in future I will switch to systemd service. Tutorial uses rpi-update only because at the time of writing the kernel for raspbian didn't include libcomposite yet. From my perspective it seems to be a purely networking issue not hardware/kernel-related. – Kamanji May 5 at 10:54

Your Answer

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

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