v1.5 branch refresh based upon upstream master @ c8677ca89e53e3be7988d54280fce166cc894a7e
[librecmc/librecmc.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 }
36
37 proto_wwan_setup() {
38         local driver usb devicename desc
39
40         for a in `ls /sys/bus/usb/devices`; do
41                 local vendor product
42                 [ -z "$usb" -a -f /sys/bus/usb/devices/$a/idVendor -a -f /sys/bus/usb/devices/$a/idProduct ] || continue
43                 vendor=$(cat /sys/bus/usb/devices/$a/idVendor)
44                 product=$(cat /sys/bus/usb/devices/$a/idProduct)
45                 [ -f /lib/network/wwan/$vendor:$product ] && {
46                         usb=/lib/network/wwan/$vendor:$product
47                         devicename=$a
48                 }
49         done
50
51         [ -n "$usb" ] && {
52                 local old_cb control data
53
54                 json_set_namespace wwan old_cb
55                 json_init
56                 json_load "$(cat $usb)"
57                 json_select
58                 json_get_vars desc control data
59                 json_set_namespace $old_cb
60
61                 [ -n "$control" -a -n "$data" ] && {
62                         ttys=$(ls -d /sys/bus/usb/devices/$devicename/${devicename}*/tty* | sed "s/.*\///g" | tr "\n" " ")
63                         ctl_device=/dev/$(echo $ttys | cut -d" " -f $((control + 1)))
64                         dat_device=/dev/$(echo $ttys | cut -d" " -f $((data + 1)))
65                         driver=comgt
66                 }
67         }
68
69         [ -z "$ctl_device" ] && for net in $(ls /sys/class/net/ | grep -e wwan -e usb); do
70                 [ -z "$ctl_device" ] || continue
71                 driver=$(grep DRIVER /sys/class/net/$net/device/uevent | cut -d= -f2)
72                 case "$driver" in
73                 qmi_wwan|cdc_mbim)
74                         ctl_device=/dev/$(ls /sys/class/net/$net/device/usbmisc)
75                         ;;
76                 sierra_net|cdc_ether|*cdc_ncm)
77                         ctl_device=/dev/$(cd /sys/class/net/$net/; find ../../../ -name ttyUSB* |xargs -n1 basename | head -n1)
78                         ;;
79                 *) continue;;
80                 esac
81                 echo "wwan[$$]" "Using proto:$proto device:$ctl_device iface:$net desc:$desc"
82         done
83
84         [ -n "$ctl_device" ] || {
85                 echo "wwan[$$]" "No valid device was found"
86                 proto_notify_error "$interface" NO_DEVICE
87                 proto_block_restart "$interface"
88                 return 1
89         }
90
91         uci_set_state network $interface driver "$driver"
92         uci_set_state network $interface ctl_device "$ctl_device"
93         uci_set_state network $interface dat_device "$dat_device"
94
95         case $driver in
96         qmi_wwan)               proto_qmi_setup $@ ;;
97         cdc_mbim)               proto_mbim_setup $@ ;;
98         sierra_net)             proto_directip_setup $@ ;;
99         comgt)                  proto_3g_setup $@ ;;
100         cdc_ether|*cdc_ncm)     proto_ncm_setup $@ ;;
101         esac
102 }
103
104 proto_wwan_teardown() {
105         local interface=$1
106         local driver=$(uci_get_state network $interface driver)
107         ctl_device=$(uci_get_state network $interface ctl_device)
108         dat_device=$(uci_get_state network $interface dat_device)
109
110         case $driver in
111         qmi_wwan)               proto_qmi_teardown $@ ;;
112         cdc_mbim)               proto_mbim_teardown $@ ;;
113         sierra_net)             proto_mbim_teardown $@ ;;
114         comgt)                  proto_3g_teardown $@ ;;
115         cdc_ether|*cdc_ncm)     proto_ncm_teardown $@ ;;
116         esac
117 }
118
119 add_protocol wwan