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