What's new

Raspberry Pi Zero W as USB Ethernet Gadget Not Working

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

bennor

Part of the Furniture
Are there any special settings that need to be enabled/changed to get USB Ethernet Gadget (Pi Zero W) to work with Merlin firmware 394.10_2? What I'm finding is the MAC address of the Pi Zero W USB Ethernet Gadget appears to be randomly generated by the Merlin firmware rather than pulling the MAC address from the Pi Zero W.

On the Pi Zero W the following values have been set and do work when connected to a Windows PC. 00:22:82:ff:ff:22 is the fake MAC address used for the Pi Zero.

config.txt: dtoverlay=dwc2
cmdline.txt (after rootwait): modules-load=dwc2,g_ether g_ether.dev_addr=00:22:82:ff:ff:22


What appears to be happening is every time the Pi Zero W is connected to the router's USB 2.0 port the Merlin firmware generates a line similar to the following with the CDC Ethernet Device address being changed.

Apr 18 10:08:13 kernel: cdc_ether 2-2:1.0: usb0: register 'cdc_ether' at usb-0000:00:0b.0-2, CDC Ethernet Device, 2e:24:df:a7:6f:8c

The end result is the router does not see or access the Pi Zero as an USB Ethernet type device. The Pi Zero is unreachable using SSH on the local network. Setting a static IP address using either the Pi Zero W fake MAC address or the firmware generated MAC address does not work to allow access to the Pi Zero W.

I have tried various methods detailed elsewhere for setting a Pi Zero up for USB Ethernet Gadget. Tried both new and used microSD cards using the Raspberry 2019-04-08-raspbian-stretch-lite image file. With the edits previously mentioned. The Merlin firmware just doesn't seem to want to properly mount the Pi Zero W.

Any suggestions or ideas?
Thanks
 
Do a search on the forums, as I believe someone did get USB gadgets working... might have been on one of the HND based Asus routers, not the older 2.6 kernel...

At least for power - Pi Zero W at max load pulls around 230 mA, which is within the capability of the USB port on the router...

Pi-Power-Usage-Zero-W-table-768x184.png
 
Do a search on the forums, as I believe someone did get USB gadgets working... might have been on one of the HND based Asus routers, not the older 2.6 kernel...

At least for power - Pi Zero W at max load pulls around 230 mA, which is within the capability of the USB port on the router...
Did some searching before posting didn't see anything offhand. But after additional searching there is this thread from 2016 where it appears it might be possible with some extra scripting.

https://www.snbforums.com/threads/run-script-on-usb-ethernet-gadget-plugged-in.34898/

I currently have the Pi Zero W being powered by the router's USB 2.0 port and a USB-OTG cable with USB 2.0 to Ethernet adapter attached to it, was just looking for a way to reduce cutter and experiment with different methods by using the USB Ethernet Gadget
 
After some experimentation one can connect the Pi Zero W to the Asus router (AC68U running Merlin at least) and have the Zero show up as a USB Ethernet Gadget connected wire device with an IP address assigned by the router.

On the Pi Zero in the Boot folder edit the file config.txt and put the following code at the bottom:
Code:
dtoverlay=dwc2

On the Pi Zero in the Boot folder edit the file cmdline.txt (Warning: if using Windows Note Pad turn off word wrap) and add the following code after rootwait (at the end of the line). When done one should have one single line of code.
Code:
modules-load=dwc2,g_ether

Connect USB cable to the Pi Zero W's micro USB port, not the micro USB power port, connect the other end of the cable to the Asus router's USB port (I used the USB 2.0 port). The Pi will power on. No other cables or adapters are needed on the Pi.

On the Asus router issue the following two commands via SSH:
Code:
ifconfig usb0 up
brctl addif br0 usb0

Problems:
The router will assign a random MAC address to the Pi Zero W every time the Pi Zero W USB Ethernet Gadget device is connected. No matter what MAC address was assigned in the cmdline.txt file using g_ether.host_addr=<MAC Address>, the router would assign a random MAC address anyway to the Pi Zero W.

After issuing the two router commands, the router will issue a random IP address (from the DHCP IP address pool) to the Pi Zero W.

If the router or Pi Zero W is rebooted one must to reissue the two router commands. This presents obvious problems that might be solvable via additional scripting per discussions in this thread.

If one is trying to use the Pi Zero W as a Pi-Hole or other service that requires a static IP address assigned to the Pi this presents a problem (when using the Pi Zero W via USB Ethernet Gadget) since the IP address and MAC address are changed each time the Pi or router is restarted and the two router commands issued.
 
Last edited:
Some updates: To fix the random MAC address being assigned the following worked for me.'

With the Pi Zero W connected to the router and mounted with the router SSH two commands above, connect to the Pi via SSH and issue the following command which will create a file called g_ether.conf:
Code:
echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | sudo tee /etc/modprobe.d/g_ether.conf
Make note/copy the output text as you will need it for the cmdline.txt file. It should look something like this:
Code:
options g_ether host_addr=82:ac:db:0f:e9:74 dev_addr=1e:30:e9:48:84:95
While still connected to the Pi via SSH issue the following command and paste the g_ether information at the end after “modules-load=dwc2,g_ether”. Keep everything on one single line. Make sure to use the values that match the output from your router.
Code:
sudo nano /boot/cmdline.txt
g_ether host_addr=82:ac:db:0f:e9:74 dev_addr=1e:30:e9:48:84:95
Example after the rootwait text: modules-load=dwc2,g_ether g_ether host_addr=82:ac:db:0f:e9:74 dev_addr=1e:30:e9:48:84:95

Reboot the Pi Zero W.

Then reissue the two router SSH commands (ifconfig usb0 up & brctl addif br0 usb0) to mount the Pi Zero W in the router. The Pi should be mounted with the same MAC address now.

Additionally one can now create a static IP address on the router's DHCP server so the Pi mounts each time with the same IP address.

Note: There is still the issue of having to reissue the two router commands each time the Pi or router is restarted. This may possibly be fixed by using the scripting mentioned in a URL above. There is also the issue (now) that the Pi may have trouble accessing the internet using website URL names versus website URL values/numbers.
 
Another minor update. Found that by using the Pi Zero W MAC address (for eth0) in the
dev_addr= entry above it solved the inability to ping/access by website name (ex: www.google.com).

So if using info above replace the MAC address in the dev_addr= with your own Pi Zero W's MAC address.

Also discovered (at least for me) that if one has a static IP address, within the router, already set with the Pi Zero W's Ethernet (eth0) MAC address one may need to change that IP address to something else (or remove it) otherwise the Pi Zero W's usb0 connection may not load after issuing the two router commands.
 
More updates. Now have the Pi Zero loading automatically when inserted into the router by following the procedures in this post: https://www.snbforums.com/threads/run-script-on-usb-ethernet-gadget-plugged-in.34898/#post-283331

Note: copy the code from “/etc/hotplug2.rules” to paste into “/jffs/hotplug2.rules” file in the directions in the link above as the code in the current /etc/hotplug2.rules may be different than the one used in the link above. Then modified the code as indicated in the link's instructions.

Needed to set permissions on the following two files. I set the permission to 0755.
/jffs/scripts/init-start
/jffs/scripts/script_hotplug.sh

And set the permission to 0644 on the following file:
/jffs/hotplug2.rules

If using Pi-Hole one will have to run "pihole –r" on the Pi Zero W then select "Reconfigure" to rerun the Pi-Hole setup so it properly configures to use the new fixed IP address and use usb0 rather than eth0.
 
If it was’nt for your ELI can follow tuto, my pi-hole project would have gone the well. Ordering this PI for its W was a regret, but not being able to make the wifi work felt good when I saw usb was possible. I would’ve ssh to nowhere and thrown the thing for an ethernet adapter if it wasn’t for these mac fixing and auto remount scripts.

Side note, link provided in last post was really useful since the noob in me didn’t found etc/hotplug2.rules. I’ve just copy/paste from there on and I’ve set permissions like you’ve showed. You’re condensed « how to » was useful as well* Such a sweet thing this custom firmware and its community!


Thanks for helping generation yoututorial mate!


* https://discourse.pi-hole.net/t/pi-zero-w-usb-ethernet-gadget-with-asus-router-fix/19352
 
@bennor : I've swapped my good old AC1900P for an AX86U Pro. Did you succeed with this method on an HND based router? I cannot get the AX to mount my PI as an USB gadget and get an IP address assigned to it : (
 
Did you succeed with this method on an HND based router? I cannot get the AX to mount my PI as an USB gadget and get an IP address assigned to it : (
No I haven't tried to connect my Pi Zero W to a RT-AX86U Pro router in Ethernet Gadget Mode.

I only used that Gadget Mode for just under a year on the RT-AC68U before moving back to using an Ethernet adapter on that Pi Zero W. No idea why it doesn't work on the AX series router. Could be due to different router hardware or different firmware, or even a problem with the Raspberry Pi OS which has changed since that time. It's been three years since I initially worked up those directions things may have changed. There was a more recent post, from 2022, in another discussion that may have some additional code modifications that might, or might not, help. But I have not tried them.
https://www.snbforums.com/threads/usb-hotplug-event.81708/#post-802765
 
Just gonna add that I've been successful at doing this with stock AX58Uv2 using my own script - https://github.com/jacklul/asuswrt-usb-raspberry-pi - running Pi-hole this way for over 5 months now.

I did add a theoretical support for Merlin in that script but I can't test it because my device does not support it.

The guide linked uses the old method of creating the device which might not always work, using libcomposite seems to be the recommended way now on the Raspberry Pi.
 
Thanks to you both for your work: I wouldn’t be able to achieve this kind of things without your knowledge, scripts and tutorials. I even thought that a JFFS partition was needed in order to run scripts.

@jacklul I’ll give it a shot! Still, I’m wondering the same thing as bennor in case things go south (not a fan of nuke reset and starting from scratch) since I’m prone to noob mistakes.
 
Interesting. Is there a way to uninstall your script(s) once installed to the Pi and the Router?
@jacklul I’ll give it a shot! Still, I’m wondering the same thing as bennor in case things go south (not a fan of nuke reset and starting from scratch) since I’m prone to noob mistakes.
There is nothing permanent applied, just peak into the install scripts to see which files will be created/modified and all you have to do is to reverse the changes manually.
 
Shortly after the Pi Zero was released I was running one in gadget mode connected to an RT-AC68u based on this tutorial:
https://www.circuitbasics.com/raspberry-pi-zero-ethernet-gadget/
I don't believe the method in that tutorial works any longer, but I still have the Pi Zero (not the W version), so I may just give it a shot!
Regards messing it up - I'm not precious about messing it up, on a single purpose project like this it can be quicker to bite the bullet and reflash rather than try to repair. And as for initial set up you just use a mini-USB for power and a separate mini-USB ethernet adapter (100Mbps is more than enough).
IMG_20240110_223950.jpg
Everything you need to set it up, but once working you should be able to drop everything but a single USB cable
 
It's been packed away for a couple of years, just posed for the photo ;)
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top