mac80211: support wildcard paths when matching phys
[oweals/openwrt.git] / package / kernel / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 lookup_phy() {
5         [ -n "$phy" ] && {
6                 [ -d /sys/class/ieee80211/$phy ] && return
7         }
8
9         local devpath
10         config_get devpath "$device" path
11         [ -n "$devpath" ] && {
12                 for _phy in /sys/devices/$devpath/ieee80211/phy*; do
13                         [ -e "$_phy" ] && {
14                                 phy="${_phy##*/}"
15                                 return
16                         }
17                 done
18         }
19
20         local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
21         [ -n "$macaddr" ] && {
22                 for _phy in $(ls /sys/class/ieee80211 2>/dev/null); do
23                         [ "$macaddr" = "$(cat /sys/class/ieee80211/${_phy}/macaddress)" ] || continue
24                         phy="$_phy"
25                         return
26                 done
27         }
28         phy=
29         return
30 }
31
32 find_mac80211_phy() {
33         local device="$1"
34
35         config_get phy "$device" phy
36         lookup_phy
37         [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
38                 echo "PHY for wifi device $1 not found"
39                 return 1
40         }
41         config_set "$device" phy "$phy"
42
43         config_get macaddr "$device" macaddr
44         [ -z "$macaddr" ] && {
45                 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
46         }
47
48         return 0
49 }
50
51 check_mac80211_device() {
52         config_get phy "$1" phy
53         [ -z "$phy" ] && {
54                 find_mac80211_phy "$1" >/dev/null || return 0
55                 config_get phy "$1" phy
56         }
57         [ "$phy" = "$dev" ] && found=1
58 }
59
60 detect_mac80211() {
61         devidx=0
62         config_load wireless
63         while :; do
64                 config_get type "radio$devidx" type
65                 [ -n "$type" ] || break
66                 devidx=$(($devidx + 1))
67         done
68         for dev in $(ls /sys/class/ieee80211); do
69                 found=0
70                 config_foreach check_mac80211_device wifi-device
71                 [ "$found" -gt 0 ] && continue
72
73                 mode_band="g"
74                 channel="11"
75                 htmode=""
76                 ht_capab=""
77
78                 iw phy "$dev" info | grep -q 'Capabilities:' && htmode=HT20
79                 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
80
81                 vht_cap=$(iw phy "$dev" info | grep -c 'VHT Capabilities')
82                 [ "$vht_cap" -gt 0 ] && {
83                         mode_band="a";
84                         channel="36"
85                         htmode="VHT80"
86                 }
87
88                 [ -n $htmode ] && append ht_capab "     option htmode   $htmode" "$N"
89
90                 if [ -x /usr/bin/readlink ]; then
91                         path="$(readlink -f /sys/class/ieee80211/${dev}/device)"
92                         path="${path##/sys/devices/}"
93                         dev_id="        option path     '$path'"
94                 else
95                         dev_id="        option macaddr  $(cat /sys/class/ieee80211/${dev}/macaddress)"
96                 fi
97
98                 cat <<EOF
99 config wifi-device  radio$devidx
100         option type     mac80211
101         option channel  ${channel}
102         option hwmode   11${mode_band}
103 $dev_id
104 $ht_capab
105         # REMOVE THIS LINE TO ENABLE WIFI:
106         option disabled 1
107
108 config wifi-iface
109         option device   radio$devidx
110         option network  lan
111         option mode     ap
112         option ssid     OpenWrt
113         option encryption none
114
115 EOF
116         devidx=$(($devidx + 1))
117         done
118 }
119