06f3b8b449fbf482d42123fd3d21f8c5b0d315bf
[openwrt/staging/mkresin.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 $(ls /sys/class/ieee80211 2>/dev/null); do
13 case "$(readlink -f /sys/class/ieee80211/$phy/device)" in
14 *$devpath) return;;
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 case "$path" in
105 platform*/pci*) path="${path##platform/}";;
106 esac
107 dev_id=" option path '$path'"
108 else
109 dev_id=" option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)"
110 fi
111
112 cat <<EOF
113 config wifi-device radio$devidx
114 option type mac80211
115 option channel ${channel}
116 option hwmode 11${mode_band}
117 $dev_id
118 $ht_capab
119 # REMOVE THIS LINE TO ENABLE WIFI:
120 option disabled 1
121
122 config wifi-iface
123 option device radio$devidx
124 option network lan
125 option mode ap
126 option ssid OpenWrt
127 option encryption none
128
129 EOF
130 devidx=$(($devidx + 1))
131 done
132 }
133