blob: e175b9b8561b594299d69d237624e3604932d22f [file] [log] [blame]
David Kilroyfb791b12009-02-04 23:05:50 +00001/* Helpers for managing scan queues
2 *
3 * See copyright notice in main.c
4 */
5
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/gfp.h>
David Kilroyfb791b12009-02-04 23:05:50 +00007#include <linux/kernel.h>
8#include <linux/string.h>
David Kilroyc63cdbe2009-06-18 23:21:33 +01009#include <linux/ieee80211.h>
10#include <net/cfg80211.h>
David Kilroyfb791b12009-02-04 23:05:50 +000011
12#include "hermes.h"
13#include "orinoco.h"
David Kilroyc63cdbe2009-06-18 23:21:33 +010014#include "main.h"
David Kilroyfb791b12009-02-04 23:05:50 +000015
16#include "scan.h"
17
David Kilroyc63cdbe2009-06-18 23:21:33 +010018#define ZERO_DBM_OFFSET 0x95
19#define MAX_SIGNAL_LEVEL 0x8A
20#define MIN_SIGNAL_LEVEL 0x2F
David Kilroyfb791b12009-02-04 23:05:50 +000021
David Kilroyc63cdbe2009-06-18 23:21:33 +010022#define SIGNAL_TO_DBM(x) \
23 (clamp_t(s32, (x), MIN_SIGNAL_LEVEL, MAX_SIGNAL_LEVEL) \
24 - ZERO_DBM_OFFSET)
25#define SIGNAL_TO_MBM(x) (SIGNAL_TO_DBM(x) * 100)
David Kilroyfb791b12009-02-04 23:05:50 +000026
David Kilroyc63cdbe2009-06-18 23:21:33 +010027static int symbol_build_supp_rates(u8 *buf, const __le16 *rates)
David Kilroyfb791b12009-02-04 23:05:50 +000028{
David Kilroyc63cdbe2009-06-18 23:21:33 +010029 int i;
30 u8 rate;
David Kilroyfb791b12009-02-04 23:05:50 +000031
David Kilroyc63cdbe2009-06-18 23:21:33 +010032 buf[0] = WLAN_EID_SUPP_RATES;
33 for (i = 0; i < 5; i++) {
34 rate = le16_to_cpu(rates[i]);
35 /* NULL terminated */
36 if (rate == 0x0)
37 break;
38 buf[i + 2] = rate;
David Kilroyfb791b12009-02-04 23:05:50 +000039 }
David Kilroyc63cdbe2009-06-18 23:21:33 +010040 buf[1] = i;
41
42 return i + 2;
David Kilroyfb791b12009-02-04 23:05:50 +000043}
44
David Kilroyc63cdbe2009-06-18 23:21:33 +010045static int prism_build_supp_rates(u8 *buf, const u8 *rates)
David Kilroyfb791b12009-02-04 23:05:50 +000046{
47 int i;
48
David Kilroyc63cdbe2009-06-18 23:21:33 +010049 buf[0] = WLAN_EID_SUPP_RATES;
50 for (i = 0; i < 8; i++) {
51 /* NULL terminated */
52 if (rates[i] == 0x0)
53 break;
54 buf[i + 2] = rates[i];
David Kilroyfb791b12009-02-04 23:05:50 +000055 }
David Kilroyc63cdbe2009-06-18 23:21:33 +010056 buf[1] = i;
57
58 /* We might still have another 2 rates, which need to go in
59 * extended supported rates */
60 if (i == 8 && rates[i] > 0) {
61 buf[10] = WLAN_EID_EXT_SUPP_RATES;
62 for (; i < 10; i++) {
63 /* NULL terminated */
64 if (rates[i] == 0x0)
65 break;
66 buf[i + 2] = rates[i];
67 }
68 buf[11] = i - 8;
69 }
70
71 return (i < 8) ? i + 2 : i + 4;
David Kilroyfb791b12009-02-04 23:05:50 +000072}
73
David Kilroyc63cdbe2009-06-18 23:21:33 +010074static void orinoco_add_hostscan_result(struct orinoco_private *priv,
75 const union hermes_scan_info *bss)
David Kilroyfb791b12009-02-04 23:05:50 +000076{
David Kilroyc63cdbe2009-06-18 23:21:33 +010077 struct wiphy *wiphy = priv_to_wiphy(priv);
78 struct ieee80211_channel *channel;
David Kilroy9236b2a2011-10-28 12:47:56 +010079 struct cfg80211_bss *cbss;
David Kilroyc63cdbe2009-06-18 23:21:33 +010080 u8 *ie;
81 u8 ie_buf[46];
82 u64 timestamp;
83 s32 signal;
84 u16 capability;
85 u16 beacon_interval;
86 int ie_len;
87 int freq;
88 int len;
David Kilroyfb791b12009-02-04 23:05:50 +000089
David Kilroyc63cdbe2009-06-18 23:21:33 +010090 len = le16_to_cpu(bss->a.essid_len);
91
92 /* Reconstruct SSID and bitrate IEs to pass up */
93 ie_buf[0] = WLAN_EID_SSID;
94 ie_buf[1] = len;
95 memcpy(&ie_buf[2], bss->a.essid, len);
96
97 ie = ie_buf + len + 2;
98 ie_len = ie_buf[1] + 2;
99 switch (priv->firmware_type) {
100 case FIRMWARE_TYPE_SYMBOL:
101 ie_len += symbol_build_supp_rates(ie, bss->s.rates);
102 break;
103
104 case FIRMWARE_TYPE_INTERSIL:
105 ie_len += prism_build_supp_rates(ie, bss->p.rates);
106 break;
107
108 case FIRMWARE_TYPE_AGERE:
109 default:
David Kilroyfb791b12009-02-04 23:05:50 +0000110 break;
111 }
112
Zhao, Gang13c1ac52014-02-19 16:58:43 +0800113 freq = ieee80211_channel_to_frequency(
114 le16_to_cpu(bss->a.channel), IEEE80211_BAND_2GHZ);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100115 channel = ieee80211_get_channel(wiphy, freq);
Joe Gunn46c2cb82011-02-25 02:08:49 -0800116 if (!channel) {
117 printk(KERN_DEBUG "Invalid channel designation %04X(%04X)",
118 bss->a.channel, freq);
119 return; /* Then ignore it for now */
120 }
David Kilroyc63cdbe2009-06-18 23:21:33 +0100121 timestamp = 0;
122 capability = le16_to_cpu(bss->a.capabilities);
123 beacon_interval = le16_to_cpu(bss->a.beacon_interv);
124 signal = SIGNAL_TO_MBM(le16_to_cpu(bss->a.level));
David Kilroyfb791b12009-02-04 23:05:50 +0000125
David Kilroy9236b2a2011-10-28 12:47:56 +0100126 cbss = cfg80211_inform_bss(wiphy, channel, bss->a.bssid, timestamp,
127 capability, beacon_interval, ie_buf, ie_len,
128 signal, GFP_KERNEL);
Johannes Berg5b112d32013-02-01 01:49:58 +0100129 cfg80211_put_bss(wiphy, cbss);
David Kilroyfb791b12009-02-04 23:05:50 +0000130}
131
David Kilroyc63cdbe2009-06-18 23:21:33 +0100132void orinoco_add_extscan_result(struct orinoco_private *priv,
133 struct agere_ext_scan_info *bss,
134 size_t len)
David Kilroyfb791b12009-02-04 23:05:50 +0000135{
David Kilroyc63cdbe2009-06-18 23:21:33 +0100136 struct wiphy *wiphy = priv_to_wiphy(priv);
137 struct ieee80211_channel *channel;
David Kilroy9236b2a2011-10-28 12:47:56 +0100138 struct cfg80211_bss *cbss;
David Kilroy69c264d2010-04-19 08:16:22 +0100139 const u8 *ie;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100140 u64 timestamp;
141 s32 signal;
142 u16 capability;
143 u16 beacon_interval;
144 size_t ie_len;
145 int chan, freq;
146
147 ie_len = len - sizeof(*bss);
David Kilroy69c264d2010-04-19 08:16:22 +0100148 ie = cfg80211_find_ie(WLAN_EID_DS_PARAMS, bss->data, ie_len);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100149 chan = ie ? ie[2] : 0;
Zhao, Gang13c1ac52014-02-19 16:58:43 +0800150 freq = ieee80211_channel_to_frequency(chan, IEEE80211_BAND_2GHZ);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100151 channel = ieee80211_get_channel(wiphy, freq);
152
153 timestamp = le64_to_cpu(bss->timestamp);
154 capability = le16_to_cpu(bss->capabilities);
155 beacon_interval = le16_to_cpu(bss->beacon_interval);
156 ie = bss->data;
157 signal = SIGNAL_TO_MBM(bss->level);
158
David Kilroy9236b2a2011-10-28 12:47:56 +0100159 cbss = cfg80211_inform_bss(wiphy, channel, bss->bssid, timestamp,
160 capability, beacon_interval, ie, ie_len,
161 signal, GFP_KERNEL);
Johannes Berg5b112d32013-02-01 01:49:58 +0100162 cfg80211_put_bss(wiphy, cbss);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100163}
164
165void orinoco_add_hostscan_results(struct orinoco_private *priv,
166 unsigned char *buf,
167 size_t len)
168{
169 int offset; /* In the scan data */
170 size_t atom_len;
171 bool abort = false;
David Kilroyfb791b12009-02-04 23:05:50 +0000172
173 switch (priv->firmware_type) {
174 case FIRMWARE_TYPE_AGERE:
175 atom_len = sizeof(struct agere_scan_apinfo);
176 offset = 0;
177 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100178
David Kilroyfb791b12009-02-04 23:05:50 +0000179 case FIRMWARE_TYPE_SYMBOL:
180 /* Lack of documentation necessitates this hack.
181 * Different firmwares have 68 or 76 byte long atoms.
182 * We try modulo first. If the length divides by both,
183 * we check what would be the channel in the second
184 * frame for a 68-byte atom. 76-byte atoms have 0 there.
185 * Valid channel cannot be 0. */
186 if (len % 76)
187 atom_len = 68;
188 else if (len % 68)
189 atom_len = 76;
190 else if (len >= 1292 && buf[68] == 0)
191 atom_len = 76;
192 else
193 atom_len = 68;
194 offset = 0;
195 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100196
David Kilroyfb791b12009-02-04 23:05:50 +0000197 case FIRMWARE_TYPE_INTERSIL:
198 offset = 4;
199 if (priv->has_hostscan) {
200 atom_len = le16_to_cpup((__le16 *)buf);
201 /* Sanity check for atom_len */
202 if (atom_len < sizeof(struct prism2_scan_apinfo)) {
203 printk(KERN_ERR "%s: Invalid atom_len in scan "
David Kilroy4244f412009-07-02 20:26:45 +0100204 "data: %zu\n", priv->ndev->name,
David Kilroyfb791b12009-02-04 23:05:50 +0000205 atom_len);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100206 abort = true;
207 goto scan_abort;
David Kilroyfb791b12009-02-04 23:05:50 +0000208 }
209 } else
210 atom_len = offsetof(struct prism2_scan_apinfo, atim);
211 break;
David Kilroyc63cdbe2009-06-18 23:21:33 +0100212
David Kilroyfb791b12009-02-04 23:05:50 +0000213 default:
David Kilroyc63cdbe2009-06-18 23:21:33 +0100214 abort = true;
215 goto scan_abort;
David Kilroyfb791b12009-02-04 23:05:50 +0000216 }
217
218 /* Check that we got an whole number of atoms */
219 if ((len - offset) % atom_len) {
David Kilroy4244f412009-07-02 20:26:45 +0100220 printk(KERN_ERR "%s: Unexpected scan data length %zu, "
221 "atom_len %zu, offset %d\n", priv->ndev->name, len,
David Kilroyfb791b12009-02-04 23:05:50 +0000222 atom_len, offset);
David Kilroyc63cdbe2009-06-18 23:21:33 +0100223 abort = true;
224 goto scan_abort;
David Kilroyfb791b12009-02-04 23:05:50 +0000225 }
226
David Kilroyc63cdbe2009-06-18 23:21:33 +0100227 /* Process the entries one by one */
David Kilroyfb791b12009-02-04 23:05:50 +0000228 for (; offset + atom_len <= len; offset += atom_len) {
David Kilroyc63cdbe2009-06-18 23:21:33 +0100229 union hermes_scan_info *atom;
David Kilroyfb791b12009-02-04 23:05:50 +0000230
David Kilroyfb791b12009-02-04 23:05:50 +0000231 atom = (union hermes_scan_info *) (buf + offset);
232
David Kilroyc63cdbe2009-06-18 23:21:33 +0100233 orinoco_add_hostscan_result(priv, atom);
David Kilroyfb791b12009-02-04 23:05:50 +0000234 }
235
David Kilroyc63cdbe2009-06-18 23:21:33 +0100236 scan_abort:
237 if (priv->scan_request) {
238 cfg80211_scan_done(priv->scan_request, abort);
239 priv->scan_request = NULL;
240 }
David Kilroyfb791b12009-02-04 23:05:50 +0000241}
David Kilroycf634952010-11-24 20:33:02 +0000242
243void orinoco_scan_done(struct orinoco_private *priv, bool abort)
244{
245 if (priv->scan_request) {
246 cfg80211_scan_done(priv->scan_request, abort);
247 priv->scan_request = NULL;
248 }
249}