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