[sysupgrade] Don't try to pivot to new ramfs if already running on one
[openwrt.git] / package / base-files / files / sbin / wifi
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /etc/functions.sh
5
6 find_net_config() {(
7         local vif="$1"
8         local cfg
9         local ifname
10
11         config_get cfg "$vif" network
12
13         [ -z "$cfg" ] && {
14                 include /lib/network
15                 scan_interfaces
16
17                 config_get ifname "$vif" ifname
18
19                 cfg="$(find_config "$ifname")"
20         }
21         [ -z "$cfg" ] && return 0
22         echo "$cfg"
23 )}
24
25
26 bridge_interface() {(
27         local cfg="$1"
28         [ -z "$cfg" ] && return 0
29
30         include /lib/network
31         scan_interfaces
32
33         config_get iftype "$cfg" type
34         [ "$iftype" = bridge ] && config_get "$cfg" ifname
35 )}
36
37 wifi_fixup_hwmode() {
38         local device="$1"
39         local default="$2"
40         local hwmode hwmode_11n
41
42         config_get channel "$device" channel
43         config_get hwmode "$device" hwmode
44         case "$hwmode" in
45                 11bg) hwmode=bg;;
46                 11a) hwmode=a;;
47                 11b) hwmode=b;;
48                 11g) hwmode=g;;
49                 11n*)
50                         hwmode_11n="${hwmode##11n}"
51                         case "$hwmode" in
52                                 a|g) ;;
53                                 default) hwmode_11n="$default"
54                         esac
55                         config_set "$device" hwmode_11n "$hwmode_11n"
56                 ;;
57                 *)
58                         hwmode=
59                         if [ "${channel:-0}" -gt 0 ]; then 
60                                 if [ "${channel:-0}" -gt 14 ]; then
61                                         hwmode=a
62                                 else
63                                         hwmode=g
64                                 fi
65                         else
66                                 hwmode="$default"
67                         fi
68                 ;;
69         esac
70         config_set "$device" hwmode "$hwmode"
71 }
72
73 wifi_updown() {
74         [ enable = "$1" ] && wifi_updown disable "$2"
75         for device in ${2:-$DEVICES}; do (
76                 config_get disabled "$device" disabled
77                 [ 1 == "$disabled" ] && {
78                         echo "'$device' is disabled"
79                         set disable
80                 }
81                 config_get iftype "$device" type
82                 if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
83                         eval "scan_$iftype '$device'"
84                         eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
85                 else
86                         echo "$device($iftype): Interface type not supported"
87                 fi
88         ); done
89 }
90
91 wifi_detect() {
92         for driver in ${2:-$DRIVERS}; do (
93                 if eval "type detect_$driver" 2>/dev/null >/dev/null; then
94                         eval "detect_$driver" || echo "$driver: Detect failed" >&2
95                 else
96                         echo "$driver: Hardware detection not supported" >&2
97                 fi
98         ); done
99 }
100
101 start_net() {(
102         local iface="$1"
103         local config="$2"
104         local vifmac="$3"
105
106         [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
107         include /lib/network
108         scan_interfaces
109         setup_interface "$iface" "$config" "" "$vifmac"
110 )}
111
112 set_wifi_up() {
113         local cfg="$1"
114         local ifname="$2"
115         uci_set_state wireless "$cfg" up 1
116         uci_set_state wireless "$cfg" ifname "$ifname"
117 }
118
119 set_wifi_down() {
120         local cfg="$1"
121         local vifs vif vifstr
122
123         [ -f "/var/run/wifi-${cfg}.pid" ] &&
124                 kill "$(cat "/var/run/wifi-${cfg}.pid")"
125         uci_revert_state wireless "$cfg"
126         config_get vifs "$cfg" vifs
127         for vif in $vifs; do
128                 uci_revert_state wireless "$vif"
129         done
130 }
131
132 scan_wifi() {
133         local cfgfile="$1"
134         config_cb() {
135                 config_get TYPE "$CONFIG_SECTION" TYPE
136                 case "$TYPE" in
137                         wifi-device)
138                                 append DEVICES "$CONFIG_SECTION"
139                         ;;
140                         wifi-iface)
141                                 config_get device "$CONFIG_SECTION" device
142                                 config_get vifs "$device" vifs 
143                                 append vifs "$CONFIG_SECTION"
144                                 config_set "$device" vifs "$vifs"
145                         ;;
146                 esac
147         }
148         config_load "${cfgfile:-wireless}"
149 }
150
151 DEVICES=
152 DRIVERS=
153 include /lib/wifi
154 scan_wifi
155
156 case "$1" in
157         down) wifi_updown "disable" "$2";;
158         detect) wifi_detect "$2";;
159         *) wifi_updown "enable" "$2";;
160 esac