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