acx-mac80211: update to 20122204
[librecmc/librecmc.git] / package / acx / files / lib / wifi / acx.sh
1 #!/bin/sh
2 append DRIVERS "acx"
3
4 find_acx_phy() {
5         local device="$1"
6
7         local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
8         config_get phy "$device" phy
9         [ -z "$phy" -a -n "$macaddr" ] && {
10                 cd /proc/driver
11                 for phy in $(ls acx_wlan[0-9] 2>&-); do
12                         phy=${phy#acx_}
13                         [ "$macaddr" = "$(cat /sys/class/net/${phy}/address)" ] || continue
14                         config_set "$device" phy "$phy"
15                         break
16                 done
17                 config_get phy "$device" phy
18         }
19         [ -n "$phy" -a -f "/proc/driver/acx_$phy" ] || {
20                 echo "phy for wifi device $1 not found"
21                 return 1
22         }
23         [ -z "$macaddr" ] && {
24                 config_set "$device" macaddr "$(cat /sys/class/net/${phy}/address)"
25         }
26         return 0
27 }
28
29 scan_acx() {
30         local device="$1"
31
32         [ ${device%[0-9]} = "wlan" ] && config_set "$device" phy "$device" || find_acx_phy "$device" || {
33                 config_unset "$device" vifs
34                 return 0
35         }
36         config_get phy "$device" phy
37
38         config_get vifs "$device" vifs
39         for vif in $vifs; do
40                 config_get mode "$vif" mode
41                 case "$mode" in
42                         adhoc|sta|ap|monitor)
43                                 # Only one vif is allowed
44                                 config_set "$vif" ifname "$phy"
45                                 break
46                         ;;
47                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
48                 esac
49         done
50         config_set "$device" vifs "$vif"
51 }
52
53 disable_acx() (
54         local device="$1"
55
56         find_acx_phy "$device" || return 0
57         config_get phy "$device" phy
58
59         set_wifi_down "$device"
60
61         include /lib/network
62         unbridge "$phy"
63         return 0
64 )
65
66 enable_acx() {
67         local device="$1"
68
69         find_acx_phy "$device" || return 0
70         config_get phy "$device" phy
71
72         config_get regdomain "$device" regdomain
73         [ -n "$regdomain" ] && iwpriv "$device" SetRegDomain "$regdomain"
74
75         config_get rxantenna "$device" rxantenna
76         config_get txantenna "$device" txantenna
77         config_get_bool diversity "$device" diversity
78         [ -n "$diversity" ] && {
79                 rxantenna="2"
80         }
81         [ -n "$rxantenna" ] && iwpriv "$phy" SetRxAnt "$rxantenna"
82         [ -n "$txantenna" ] && iwpriv "$phy" SetTxAnt "$txantenna"
83
84         config_get channel "$device" channel
85         [ -n "$channel" ] && iwconfig "$phy" channel "$channel" >/dev/null 2>/dev/null
86
87         config_get txpower "$device" txpower
88         [ -n "$txpower" ] && iwconfig "$phy" txpower "${txpower%%.*}"
89
90         config_get vif "$device" vifs
91
92         config_get ifname "$vif" ifname
93         config_get ssid "$vif" ssid
94         config_get mode "$vif" mode
95
96         iwconfig "$phy" essid ${ssid:+-- }"${ssid:-any}"
97
98         case "$mode" in
99                 sta)
100                         iwconfig "$phy" mode managed
101                         config_get addr "$device" bssid
102                         [ -z "$addr" ] || { 
103                                 iwconfig "$phy" ap "$addr"
104                         }
105                 ;;
106                 ap) iwconfig "$phy" mode master;;
107                 adhoc) iwconfig "$phy" mode ad-hoc;;
108                 *) iwconfig "$phy" mode "$mode";;
109         esac
110
111         config_get frag "$vif" frag
112         [ -n "$frag" ] && iwconfig "$phy" frag "${frag%%.*}"
113
114         config_get rts "$vif" rts
115         [ -n "$rts" ] && iwconfig "$phy" rts "${rts%%.*}"
116
117         config_get enc "$vif" encryption
118         case "$enc" in
119                 wep)
120                         for idx in 1 2 3 4; do
121                                 config_get key "$vif" "key${idx}"
122                                 iwconfig "$ifname" enc restricted "[$idx]" "${key:-off}"
123                         done
124                         config_get key "$vif" key
125                         key="${key:-1}"
126                         case "$key" in
127                                 [1234]) iwconfig "$ifname" enc restricted "[$key]";;
128                                 *) iwconfig "$ifname" enc restricted "$key";;
129                         esac
130                 ;;
131                 psk*|wpa*)
132                         echo "$device($vif): WPA/WPA2 not supported by acx driver"
133                         return 1
134                 ;;
135         esac
136
137         local net_cfg bridge
138         net_cfg="$(find_net_config "$vif")"
139         [ -z "$net_cfg" ] || {
140                 bridge="$(bridge_interface "$net_cfg")"
141                 config_set "$vif" bridge "$bridge"
142                 start_net "$ifname" "$net_cfg"
143         }
144         set_wifi_up "$vif" "$ifname"
145
146 }
147
148 check_acx_device() {
149         [ ${1%[0-9]} = "wlan" ] && config_set "$1" phy "$1"
150         config_get phy "$1" phy
151         [ -z "$phy" ] && {
152                 find_acx_phy "$1" >/dev/null || return 0
153                 config_get phy "$1" phy
154         }
155         [ "$phy" = "$dev" ] && found=1
156 }
157
158 detect_acx() {
159         devidx=0
160         config_load wireless
161         while :; do
162                 config_get type "radio$devidx" type
163                 [ -n "$type" ] || break
164                 devidx=$(($devidx + 1))
165         done
166         cd /proc/driver
167         for dev in $(ls acx_wlan[0-9] 2>&-); do
168                 dev=${dev#acx_}
169                 found=0
170                 config_foreach check_acx_device wifi-device
171                 [ "$found" -gt 0 ] && continue
172                 cat <<EOF
173 config wifi-device radio$devidx
174         option type     acx
175         option channel  11
176         option macaddr  $(cat /sys/class/net/${dev}/address)
177
178         # REMOVE THIS LINE TO ENABLE WIFI:
179         option disabled 1
180
181 config wifi-iface
182         option device   radio$devidx
183         option network  lan
184         option mode     ap
185         option ssid     OpenWrt
186         option encryption none
187
188 EOF
189         devidx=$(($devidx + 1))
190         done
191 }