OpenWrt Forum Archive

Topic: [HOWTO] Rebind a UMTS USB modem

The content of this topic has been archived on 7 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

This should be added to a FAQ I think because has been asked constantly.
UMTS dongle installation instructions are [url=http://wiki.openwrt.org/doc/recipes/3gdongle?s[]=umts]here[/url]
There is nasty bug somewhere in the firmware (hotplug event handling I suppose) that reattaches a modem to the wrong character device name. Normally if a USB modem is attached several character devices are automatically created with the name - /dev/ttyUSBx where x is from 0 to N. With my modem for example I get 4 devices created:

root@Alix:~# ls -la /dev/ttyUSB*
crw-rw-rw-    1 root     root     188,   0 Jul 30 09:21 /dev/ttyUSB0
crw-rw-rw-    1 root     root     188,   1 Jul 30 09:21 /dev/ttyUSB1
crw-rw-rw-    1 root     root     188,   2 Jul 30 09:21 /dev/ttyUSB2
crw-rw-rw-    1 root     root     188,   3 Jul 30 09:21 /dev/ttyUSB3

In the network config I'm specifying the /dev/ttyUSB0 as the device to be used to establish the connection

root@Alix:~# cat /etc/config/network | grep "'wan'" -A 5
config 'interface' 'wan'
        option 'proto' '3g'
        option 'service' 'umts'
        option 'apn' 'flux'
        option 'pincode' '242'
        option 'device' '/dev/ttyUSB0

Everything works but for some reason the modem gets detached and reattached again. This could happen if the modem looses connection to the UMTS base station. The problem is the modem is attached to the /dev/ttyUSBx but now x changes from 1 to 4! Which means that I need to change the device name to /dev/ttyUSB1 in the /etc/config/network or physically reattach the modem to bring up the wan interface! Ain't that a bitch.

A workaround is to manually rebind the modem driver. I've written two scripts for that purpose.
The first script is to unbind the driver:

root@Alix:~# cat /mnt/sd/bin/unbind 
#!/bin/sh

binddev=/sys/bus/usb/drivers/usbserial_generic/unbind

for link in /sys/bus/usb/drivers/usbserial_generic/1-1*; do
  id=$(echo "$link" | sed -n -r "s/.*\/(.*)/\1/p")
  echo -n "$id" > $binddev 
done

The second one is a bit more complex:

root@Alix:~# cat /mnt/sd/bin/bind 
#!/bin/sh

vid=12d1
pid=1003
devpath=/sys/bus/usb/devices
binddev=/sys/bus/usb/drivers/usbserial_generic/bind

for dev in $devpath/*; do
  cvid=$([ -f $dev/idVendor ] && cat $dev/idVendor)
  cpid=$([ -f $dev/idVendor ] && cat $dev/idProduct)
  #echo "$dev, $cvid:$cpid"
  [ "$vid" = "$cvid" ] && [ "$pid" = "$cpid" ] && devdir=$dev 
done

if [ -z "$devdir" ]; then
 echo "Device $vid:$pid not found"
 return 1
fi

devpref=$(echo "$devdir" | sed -n -r "s/.*\/(.*)/\1/p")

for subdev in $devdir/$devpref*; do
  id=$(echo "$subdev" | sed -n -r "s/.*\/(.*)/\1/p")
  #echo "$subdev"
  echo -n "$id" > $binddev 
done

The complexity comes from the necessity to find proper device directory. vid and pid variables shall be adjusted to correspond to vendor id and product id of the modem you use.
So to bring the wan interface up one has to call:

unbind
bind
ifup wan

I have defined a rebind script that does exactly this

(Last edited by flux on 30 Jul 2011, 14:30)

Its very interesting script - thank You for sharing.

I presume first should be put into file unbind and second should be called bind - is that right?

Could You tell me how to make it automatic so if USB reattaching happen, script runs automatically?

janptak wrote:

Its very interesting script - thank You for sharing.

I presume first should be put into file unbind and second should be called bind - is that right?

Could You tell me how to make it automatic so if USB reattaching happen, script runs automatically?

you should use these scripts if you experience described problem i.e modem is reassigned to a different device name.
Automatic reattaching is handled by a hotplug script already. This script can be extended to always rebind the modem before the interface goes up. I'm personally not using this hotplug script because having it enabled makes it impossible to bring the wan interface down (it will reconnect automatically). And I do not want the modem to be constantly connected to the base station. I'm using a IR remote to bring the interface up or down so I do not have to open a shell for this (http://flux242.blogspot.com/2010/11/ir- … outer.html)

flux this is a great share!
i need to reconnet an usb wifi adapter from time to time without having physical access to the device its connected to.
So i tried to pickup your work and take it for my Demand:

root@Fensterbrett:~# echo -n 1-0:1.0 > /sys/bus/usb/drivers/hub/unbind

[ 1585.620000] usb 1-1: USB disconnect, device number 2

root@Fensterbrett:~# echo -n 1-0:1.0 > /sys/bus/usb/drivers/hub/bind

[ 1606.780000] hub 1-0:1.0: USB hub found
[ 1606.790000] hub 1-0:1.0: 2 ports detected
[ 1607.010000] usb 1-1: new high-speed USB device number 3 using ehci-platform
[ 1607.180000] usb 1-1: config 1 interface 0 altsetting 0 bulk endpoint 0x83 has invalid maxpacket 64
[ 1607.180000] usb 1-1: config 1 interface 0 altsetting 0 bulk endpoint 0x4 has invalid maxpacket 64
[ 1607.200000] usb 1-1: ath9k_htc: Firmware htc_9271.fw requested
[ 1607.640000] usb 1-1: ath9k_htc: Transferred FW: htc_9271.fw, size: 51272
[ 1608.650000] ath9k_htc 1-1:1.0: ath9k_htc: Target is unresponsive
[ 1608.650000] ath9k_htc: Failed to initialize the device
[ 1608.670000] usb 1-1: ath9k_htc: USB layer deinitialized

So it seems that the Device isnt reset corectly. Ok sir so lets try to find our "1-1:1.0"

root@Fensterbrett:~# find /sys | grep 1-1:1.0

gives me no result... Any idea how to reset this device without unplugging it physicaly?
Or power it down electrically?

derdigge

(Last edited by derdigge on 9 Jul 2013, 21:13)

The discussion might have continued from here.