What's new

Run script on USB Ethernet gadget plugged in

  • 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!

yukky

New Around Here
Hello,

Hardware: RT-N66U
Firmware: 380.62

I am trying to connect a Raspberry Pi Zero device as an Ethernet gadget via the USB port on the router. Everything works, except I need to manually put the usb0 interface up and add it to the br0 bridge. How can I automate this?

When plugging in the device:
Code:
usb 1-1.2: new high speed USB device using ehci_hcd and address 5
hub 1-1:1.0: unable to enumerate USB device on port 2
usb 1-1.2: new high speed USB device using ehci_hcd and address 6
hub 1-1:1.0: unable to enumerate USB device on port 2
usb 1-1.2: new high speed USB device using ehci_hcd and address 7
usb 1-1.2: configuration #1 chosen from 2 choices
usb0: register 'cdc_ether' at usb-0000:00:04.1-1.2, CDC Ethernet Device, 06:b2:42:13:0d:a2

commands I would like to run upon device registered
Code:
ifconfig usb0 up
brctl addif br0 usb0

The commands need to be re-run everytime the USB gadget is plugged in/out.

What would be the best way to automate this, or is there a way to have this configuration persist?
 
Last edited:
I'm also seeing this, so I'm not sure if I should be in promiscuous mode or not...
Code:
device usb0 entered promiscuous mode
br0: port 5(usb0) entering listening state
br0: port 5(usb0) entering learning state
br0: topology change detected, propagating
br0: port 5(usb0) entering forwarding state
 
I'm not sure if I should be in promiscuous mode
I think promiscuous mode is normal. See this:
Code:
dmesg|grep prom
 
Right, everything is in promiscuous mode.

Any clue about running a script on USB connect? Perhaps via hotplug2?
 
I've got everything working now!

I didn't have any luck with script_usbhotplug in nvram

/jffs/scripts/init-start
Code:
#!/bin/sh

# Replace hotplug2 rules with our own, and restart hotplug2
ln -sf /jffs/hotplug2.rules /etc/hotplug2.rules
killall hotplug2

/jffs/hotplug2.rules
line added for SUBSYSTEM == net
Code:
DEVPATH is set, ACTION == add {
makedev /dev/%DEVICENAME% 0644
}
SUBSYSTEM ~~ ^(usb|tty|block|scsi_generic)$, ACTION == remove {
remove /dev/%DEVICENAME%
}
MODALIAS is set, ACTION == add {
exec /sbin/modprobe -q %MODALIAS% ;
}
SUBSYSTEM == usb, DEVICENAME ~~ ^([1-3]-[1-3][:.0-9]*+)$, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_usb_interface %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == usb, DEVICENAME ~~ ^(lp[0-9]+)$, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_lp %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == tty, DEVICENAME ~~ ^(ttyUSB[0-9]+)$, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_tty %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == tty, DEVICENAME ~~ ^(ttyACM[0-9]+)$, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_tty %DEVICENAME% %ACTION% ;
}
DEVICENAME ~~ ^(sg[0-9]+)$, ACTION == add {
exec /sbin/asus_sg %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == block, DEVICENAME ~~ ^(sr[0-9]+)$, ACTION == add {
exec /sbin/asus_sr %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == block, DEVICENAME ~~ ^(sd[a-z][0-9]*+)$, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_sd %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == block, DEVICENAME ~~ ^(mmcblk[0-9]p[0-9]*+)$, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_mmc %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == usb, DEVICENAME == usbbcm, ACTION ~~ ^(add|remove)$ {
exec /sbin/asus_usbbcm %DEVICENAME% %ACTION% ;
}
SUBSYSTEM == net, ACTION is set {
exec /sbin/hotplug %SUBSYSTEM% ;
exec /jffs/scripts/script_hotplug.sh ;
}
SUBSYSTEM == misc, DEVICENAME ~~ ^(tun|tap)$, DEVPATH is set, ACTION == add {
exec mkdir -p -m 0777 /dev/net ;
symlink /dev/%DEVICENAME% /dev/net/%DEVICENAME%
}

/jffs/scripts/script_hotplug.sh
Code:
#!/bin/sh

echo $(date)" Action: "$ACTION" Product: "$PRODUCT" Interface: "$INTERFACE >> /tmp/hotplug.log

if [ "${INTERFACE}" = "usb0" ]; then
  if [ "${ACTION}" = "add" ]; then
    ifconfig usb0 up
    brctl addif br0 usb0
  fi
fi

Thanks for the help all!
 
Is there a unique ID for the USB device? How would I recognize a specific USB device?
 
Is there a unique ID for the USB device? How would I recognize a specific USB device?
You can check the line that gets added to /tmp/hotplug.log, however in my case with the USB ethernet gadget, I only get:
Code:
Fri Sep 30 19:44:12 DST 2016 Action: add Product:  Interface: usb0
Fri Sep 30 19:53:33 DST 2016 Action: remove Product:  Interface: usb0

$PRODUCT is unfortunately blank for me.

The OpenWrt Hotplug wiki page says you can try to grab this information:
Code:
logger -t DEBUG "hotplug usb: action='$ACTION' devicename='$DEVICENAME' devname='$DEVNAME' devpath='$DEVPATH' product='$PRODUCT' type='$TYPE' interface='$INTERFACE'"
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

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