I have a USB GSM modem that does not alwasys work property (Huawei E367u-2) Sometimes it gets reset (USB device disconnect/reconnect in logs) and when it comes back up, it's has different ttyUSB numbers. Sometimes on boot, usb_modswitch seems to just not get fired. The computer is a Raspberry Pi running Raspbian.
I have a simple solution to this, every minute CRON runs the following script:
If WVDIAL is not running:
Run WVDIAL
I want to change the script to be this:
If /dev/ttyUSB0 is not present:
If DevicePresent(12d1:1446):
ResetDevice(12d1:1446)
ElseIs DevicePresemt(12d1:1506)
ResetUSB(12d1:1506)
If WVDIAL is not running:
Run WVDIAL
Obviously this is pseudo code, but I have the following lines that I need to string together but I can't figure out how:
This loads wvdial if it is not running:
#! /bin/sh
# /etc/init.d/wvdial
### BEGIN INIT INFO
# Provides: TheInternet
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple script to start a program at boot
# Description: A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting GPRS Internet"
# run application you want to start
/sbin/start-stop-daemon --start --background --quiet --exec /usr/bin/wvdial internet
;;
stop)
echo "Stopping GPRS Internet"
# kill application you want to stop
/sbin/start-stop-daemon --stop --exec /usr/bin/wvdial
;;
*)
echo "Usage: /etc/init.d/noip {start|stop}"
exit 1
;;
esac
exit 0
This lets me find the /sys
path to a certain device:
for X in /sys/bus/usb/devices/*; do
echo "$X"
cat "$X/idVendor" 2>/dev/null
cat "$X/idProduct" 2>/dev/null
echo
done
And this resets a USB device if you know the correct /sys path:
echo 0 > /sys/bus/usb/devices/1-1.2.1.1/authorized
echo 1 > /sys/bus/usb/devices/1-1.2.1.1/authorized
So, I need to string the last 2 sections and a test to /dev/ttyUSB0
into a section that goes under the "If you want a command to always run. put it here" section
UPDATE 1
usbreset, although functioning, does not do the job.
Only echo 0 the 1 into authroized
correctly simulates a device disconnect/reconnect.
This is what does the trick of booting the device or usb_modeswitch back into life.
As such I've managed to cobble together this:
echo "Searching for $1"
devPath=`lsusb | grep $1 | sed -r 's/Bus ([0-9]{3}) Device ([0-9]{3}).*/bus\/usb\/\1\/\2/g;'`
echo "Found $1 @ $devPath"
echo "Searching for sysPath"
for sysPath in /sys/bus/usb/devices/*; do
echo "$sysPath/uevent"
devName=`cat "$sysPath/uevent" | grep $devPath`
#echo devName=$devName
if [ ! -z $devName ]
then
break
fi
done
if [ ! -z $devName ]
then
echo "Found $1 @ $sysPath, Resetting"
echo "echo 0 > $sysPath/authorized"
echo 0 > $sysPath/authorized
echo "echo 1 > $sysPath/authorized"
echo 1 > $sysPath/authorized
else
echo "Could not find $1"
fi
I think therefore all I have to do now is put this in the init.d script:
if ttyUSB0 not present
if 12d1:1446 present
/usr/sbin/resetdevicebyauthorized 12d1:1446
else if 12d1:1506 present
/usr/sbin/resetdevicebyauthorized 12d1:1506
fi
fi
usbreset
fails with Huawei modems, the modem is not able to recover and freezes at the reboot. I'm currently solving the same problem, and might have a fully working solution soon.