usb_modeswitch_status 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/sh
  2. #(c) Copyright Barry Kauler Nov. 2010. License: GPL v3 /usr/share/doc/legal.
  3. #called from /lib/udev/usb_modeswitch, which in turn is called by udev rule when a 3G modem inserted.
  4. #101208 rewrite, simpler.
  5. #101209 switch has occurred, but sometimes (my ZTE K3571-Z modem) usbserial.ko fails to attach to usb interface.
  6. #110914 think possible this script gets called almost simultaneously more than once.
  7. #110914 create /dev/gsmmodem symlink if usb-modeswitch fails to do it.
  8. #120109 usb_modeswitch 1.2.1 different success message.
  9. #120109 /usr/sbin/usb_modeswitch_special_status now just comes here (usb modems that do not need switch).
  10. #120202 rodin.s: internationalized.
  11. #130326 note: DEVICE variable no longer in 3.5+ kernel, as /proc/bus/usb removed.
  12. export TEXTDOMAIN=usb_modeswitch_status
  13. export OUTPUT_CHARSET=UTF-8
  14. export LANG="`cat /etc/profile|grep '^LANG='| cut -d '=' -f2`"
  15. #110914 think possible this script gets called almost simultaneously more than once...
  16. #start lock region#
  17. touch /tmp/usb_modeswitch_status_lockregion_${$}_
  18. SLEEPU1=`echo -n ${$} | rev`0 #ex: pid 3124 becomes 4213, more variability for sleep.
  19. SLEEPU2=${$}0
  20. for ASLEEP in $SLEEPU1 $SLEEPU2
  21. do
  22. SIMULT="`ls -1 /tmp/usb_modeswitch_status_lockregion_*_`"
  23. #random sleep means that this process will kill the others before they can kill this one...
  24. usleep ${ASLEEP} #ex: 42130 is 42 milliseconds.
  25. if [ `echo "$SIMULT" | wc -l` -gt 1 ];then
  26. for ONEPID in `ls -1 /tmp/usb_modeswitch_status_lockregion_*_ | rev | cut -f 2 -d '_' | rev | tr '\n' ' '`
  27. do
  28. [ $ONEPID -eq ${$} ] && continue
  29. [ -f /tmp/usb_modeswitch_status_lockregion_${ONEPID}_ ] && kill $ONEPID #other process within lock region.
  30. [ $? -eq 0 ] && rm -f /tmp/usb_modeswitch_status_lockregion_${ONEPID}_
  31. done
  32. fi
  33. done
  34. rm -f /tmp/usb_modeswitch_status_lockregion_${$}_
  35. #end lock region#
  36. rm -f /tmp/usb_modeswitch_status_flag_success 2>/dev/null
  37. DISPLAY=":0" /usr/lib/gtkdialog/box_splash -placement center -close never -bg yellow -fontsize large -text "$(gettext '3G USB modem inserted, please wait, configuring ...')" &
  38. yPID=$!
  39. BRKCNT=0
  40. while [ $BRKCNT -lt 30 ];do
  41. sleep 1
  42. LOGS="`cat /var/log/usb_modeswitch_*`"
  43. [ "`echo "$LOGS" | grep 'Mode switch succeeded'`" ] && break
  44. [ "`echo "$LOGS" | grep 'Mode switching was successful'`" ] && break #120109 usb_modeswitch 1.2.1 different success message.
  45. BRKCNT=$(($BRKCNT+1))
  46. done
  47. if [ $BRKCNT -lt 30 ];then
  48. #101209 switch has occurred, but sometimes (my ZTE K3571-Z modem) usbserial.ko fails to attach to usb interface...
  49. sleep 2
  50. if [ "`lsmod | grep '^usbserial '`" != "" ];then
  51. if ! ls /dev/ttyUSB* 2>/dev/null;then
  52. sleep 2
  53. if ! ls /dev/ttyUSB* 2>/dev/null;then
  54. VENDOR="`echo -n "$PRODUCT" | cut -f 1 -d '/'`"
  55. #101210 keep this simple, only one usb modem plugged in, claiming usbserial.ko...
  56. modREGEX="v${VENDOR}p[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]"
  57. CHIPNO="`cat /sys/bus/usb/devices/*/modalias | grep -i -o "$modREGEX" | cut -f 2 -d 'p' | tail -n 1`"
  58. UPMOD="`lsmod | grep '^usbserial ' | tr -s ' ' | cut -f 4 -d ' ' | cut -f 1 -d ','`"
  59. if [ -e /sys/bus/usb-serial/drivers/${UPMOD}1/new_id ];then
  60. echo "$VENDOR $CHIPNO" > /sys/bus/usb-serial/drivers/${UPMOD}1/new_id
  61. sleep 2
  62. fi
  63. if ! ls /dev/ttyUSB* 2>/dev/null;then
  64. rmmod $UPMOD
  65. rmmod usbserial
  66. modprobe usbserial vendor=0x${VENDOR} product=0x${CHIPNO}
  67. modprobe $UPMOD
  68. fi
  69. fi
  70. fi
  71. #110914 this works on my new laptop...
  72. if [ ! -h /dev/gsmmodem ];then #supposed to be auto-created, but what if it fails.
  73. sleep 5
  74. if [ ! -h /dev/gsmmodem ];then
  75. #looks for ep_8[0-9]/type with "Interrupt" in it then follows up path looking for a "ttyUSB[0-9]" dir...
  76. GSMMODEM="`find /sys/devices -regex '/sys/devices/.*/ep_[8][0-9]/type' | grep '/usb[0-9]' | xargs grep 'Interrupt' | sed -e 's%/ep_[8].*%%' | xargs -I ABCDEF find ABCDEF -maxdepth 1 -name 'ttyUSB*' | rev | cut -f 1 -d '/' | rev | sort -u | head -n 1`"
  77. [ "$GSMMODEM" ] && ln -s ${GSMMODEM} /dev/gsmmodem
  78. fi
  79. fi
  80. fi
  81. kill $yPID
  82. DISPLAY=":0" /usr/lib/gtkdialog/box_splash -placement center -close never -timeout 12 -bg green -fontsize large -text "$(gettext '3G USB modem now ready for use')" &
  83. touch /tmp/usb_modeswitch_status_flag_success
  84. else
  85. #do not display an error msg. in case no modeswitch was required?
  86. echo
  87. fi
  88. mkdir /tmp/usb_modeswitch${$}
  89. mv -f /var/log/usb_modeswitch_* /tmp/usb_modeswitch${$}/
  90. sleep 50 #just in case /lib/udev/usb_modeswitch tries to rerun this script.
  91. kill $yPID
  92. ###END###