wwan.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . ../netifd-proto.sh
  4. init_proto "$@"
  5. INCLUDE_ONLY=1
  6. ctl_device=""
  7. dat_device=""
  8. proto_mbim_setup() { echo "wwan[$$] mbim proto is missing"; }
  9. proto_qmi_setup() { echo "wwan[$$] qmi proto is missing"; }
  10. proto_ncm_setup() { echo "wwan[$$] ncm proto is missing"; }
  11. proto_3g_setup() { echo "wwan[$$] 3g proto is missing"; }
  12. proto_directip_setup() { echo "wwan[$$] directip proto is missing"; }
  13. [ -f ./mbim.sh ] && . ./mbim.sh
  14. [ -f ./ncm.sh ] && . ./ncm.sh
  15. [ -f ./qmi.sh ] && . ./qmi.sh
  16. [ -f ./3g.sh ] && { . ./ppp.sh; . ./3g.sh; }
  17. [ -f ./directip.sh ] && . ./directip.sh
  18. proto_wwan_init_config() {
  19. available=1
  20. no_device=1
  21. proto_config_add_string apn
  22. proto_config_add_string auth
  23. proto_config_add_string username
  24. proto_config_add_string password
  25. proto_config_add_string pincode
  26. proto_config_add_string delay
  27. proto_config_add_string modes
  28. proto_config_add_string bus
  29. }
  30. proto_wwan_setup() {
  31. local driver usb devicename desc bus
  32. json_get_vars bus
  33. if [ -L "/sys/bus/usb/devices/${bus}" ]; then
  34. if [ -f "/sys/bus/usb/devices/${bus}/idVendor" ] \
  35. && [ -f "/sys/bus/usb/devices/${bus}/idProduct" ]; then
  36. local vendor product
  37. vendor=$(cat /sys/bus/usb/devices/${bus}/idVendor)
  38. product=$(cat /sys/bus/usb/devices/${bus}/idProduct)
  39. [ -f /lib/network/wwan/$vendor:$product ] && {
  40. usb=/lib/network/wwan/$vendor:$product
  41. devicename=$bus
  42. }
  43. else
  44. echo "wwan[$$]" "Specified usb bus ${bus} was not found"
  45. proto_notify_error "$interface" NO_USB
  46. proto_block_restart "$interface"
  47. return 1
  48. fi
  49. else
  50. echo "wwan[$$]" "Searching for a valid wwan usb device..."
  51. for a in $(ls /sys/bus/usb/devices); do
  52. local vendor product
  53. [ -z "$usb" -a -f /sys/bus/usb/devices/$a/idVendor -a -f /sys/bus/usb/devices/$a/idProduct ] || continue
  54. vendor=$(cat /sys/bus/usb/devices/$a/idVendor)
  55. product=$(cat /sys/bus/usb/devices/$a/idProduct)
  56. [ -f /lib/network/wwan/$vendor:$product ] && {
  57. usb=/lib/network/wwan/$vendor:$product
  58. devicename=$a
  59. }
  60. done
  61. fi
  62. echo "wwan[$$]" "Using wwan usb device on bus $devicename"
  63. [ -n "$usb" ] && {
  64. local old_cb control data
  65. json_set_namespace wwan old_cb
  66. json_init
  67. json_load "$(cat "$usb")"
  68. json_select
  69. json_get_vars desc control data
  70. json_set_namespace "$old_cb"
  71. [ -n "$control" -a -n "$data" ] && {
  72. ttys=$(ls -d /sys/bus/usb/devices/$devicename/${devicename}*/tty?* /sys/bus/usb/devices/$devicename/${devicename}*/tty/tty?* | sed "s/.*\///g" | tr "\n" " ")
  73. ctl_device=/dev/$(echo $ttys | cut -d" " -f $((control + 1)))
  74. dat_device=/dev/$(echo $ttys | cut -d" " -f $((data + 1)))
  75. driver=comgt
  76. }
  77. }
  78. [ -z "$ctl_device" ] && for net in $(ls /sys/class/net/ | grep -e wwan -e usb); do
  79. [ -z "$ctl_device" ] || continue
  80. [ -n "$bus" ] && {
  81. [ $(readlink /sys/class/net/$net | grep $bus) ] || continue
  82. }
  83. driver=$(grep DRIVER /sys/class/net/$net/device/uevent | cut -d= -f2)
  84. case "$driver" in
  85. qmi_wwan|cdc_mbim)
  86. ctl_device=/dev/$(ls /sys/class/net/$net/device/usbmisc)
  87. ;;
  88. sierra_net|cdc_ether|*cdc_ncm)
  89. ctl_device=/dev/$(cd /sys/class/net/$net/; find ../../../ -name ttyUSB* |xargs -n1 basename | head -n1)
  90. ;;
  91. *) continue;;
  92. esac
  93. echo "wwan[$$]" "Using proto:$proto device:$ctl_device iface:$net desc:$desc"
  94. done
  95. [ -n "$ctl_device" ] || {
  96. echo "wwan[$$]" "No valid device was found"
  97. proto_notify_error "$interface" NO_DEVICE
  98. proto_block_restart "$interface"
  99. return 1
  100. }
  101. uci_set_state network "$interface" driver "$driver"
  102. uci_set_state network "$interface" ctl_device "$ctl_device"
  103. uci_set_state network "$interface" dat_device "$dat_device"
  104. case $driver in
  105. qmi_wwan) proto_qmi_setup $@ ;;
  106. cdc_mbim) proto_mbim_setup $@ ;;
  107. sierra_net) proto_directip_setup $@ ;;
  108. comgt) proto_3g_setup $@ ;;
  109. cdc_ether|*cdc_ncm) proto_ncm_setup $@ ;;
  110. esac
  111. }
  112. proto_wwan_teardown() {
  113. local interface=$1
  114. local driver=$(uci_get_state network "$interface" driver)
  115. ctl_device=$(uci_get_state network "$interface" ctl_device)
  116. dat_device=$(uci_get_state network "$interface" dat_device)
  117. case $driver in
  118. qmi_wwan) proto_qmi_teardown $@ ;;
  119. cdc_mbim) proto_mbim_teardown $@ ;;
  120. sierra_net) proto_directip_teardown $@ ;;
  121. comgt) proto_3g_teardown $@ ;;
  122. cdc_ether|*cdc_ncm) proto_ncm_teardown $@ ;;
  123. esac
  124. }
  125. add_protocol wwan