a7c4d2dbc2bce08b95ee3f5bbaf86a6d01180a2f
[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 [ "$vht_cap" -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