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