include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / libertas / scan.c
CommitLineData
876c9d3a
MT
1/**
2 * Functions implementing wlan scan IOCTL and firmware command APIs
3 *
4 * IOCTL handlers as well as command preperation and response routines
5 * for sending scan commands to the firmware.
6 */
5a0e3ad6 7#include <linux/slab.h>
7e272fcf 8#include <linux/types.h>
430453fc 9#include <linux/kernel.h>
fcdb53db 10#include <linux/etherdevice.h>
2c706002 11#include <linux/if_arp.h>
ac630c2b 12#include <asm/unaligned.h>
7e272fcf
JL
13#include <net/lib80211.h>
14
876c9d3a 15#include "host.h"
876c9d3a
MT
16#include "dev.h"
17#include "scan.h"
2d46502d 18#include "assoc.h"
e93156e7 19#include "wext.h"
fa62f99c 20#include "cmd.h"
876c9d3a
MT
21
22//! Approximate amount of data needed to pass a scan result back to iwlist
23#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
243e84e9 24 + IEEE80211_MAX_SSID_LEN \
876c9d3a
MT
25 + IW_EV_UINT_LEN \
26 + IW_EV_FREQ_LEN \
27 + IW_EV_QUAL_LEN \
243e84e9 28 + IEEE80211_MAX_SSID_LEN \
876c9d3a
MT
29 + IW_EV_PARAM_LEN \
30 + 40) /* 40 for WPAIE */
31
32//! Memory needed to store a max sized channel List TLV for a firmware scan
75b6a61a 33#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvl_ie_header) \
876c9d3a
MT
34 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
35 * sizeof(struct chanscanparamset)))
36
37//! Memory needed to store a max number/size SSID TLV for a firmware scan
75b6a61a 38#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvl_ie_ssid_param_set))
876c9d3a 39
fa62f99c
DW
40//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
41#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
42 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
876c9d3a
MT
43
44//! The maximum number of channels the firmware can scan per command
45#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
46
47/**
48 * @brief Number of channels to scan per firmware scan command issuance.
49 *
50 * Number restricted to prevent hitting the limit on the amount of scan data
51 * returned in a single firmware scan command.
52 */
53#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
54
55//! Scan time specified in the channel TLV for each channel for passive scans
56#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
57
58//! Scan time specified in the channel TLV for each channel for active scans
59#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
60
2c706002
JB
61#define DEFAULT_MAX_SCAN_AGE (15 * HZ)
62
fa62f99c
DW
63static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
64 struct cmd_header *resp);
e56188ac
HS
65
66/*********************************************************************/
67/* */
68/* Misc helper functions */
69/* */
70/*********************************************************************/
71
23ff5036
HS
72/**
73 * @brief Unsets the MSB on basic rates
74 *
75 * Scan through an array and unset the MSB for basic data rates.
76 *
77 * @param rates buffer of data rates
78 * @param len size of buffer
79 */
80static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
81{
82 int i;
83
84 for (i = 0; i < len; i++)
85 rates[i] &= 0x7f;
86}
87
88
f137e054 89static inline void clear_bss_descriptor(struct bss_descriptor *bss)
fcdb53db
DW
90{
91 /* Don't blow away ->list, just BSS data */
92 memset(bss, 0, offsetof(struct bss_descriptor, list));
93}
94
ffd074fc
HS
95/**
96 * @brief Compare two SSIDs
97 *
98 * @param ssid1 A pointer to ssid to compare
99 * @param ssid2 A pointer to ssid to compare
100 *
101 * @return 0: ssid is same, otherwise is different
102 */
f137e054
DW
103int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
104 uint8_t ssid2_len)
ffd074fc
HS
105{
106 if (ssid1_len != ssid2_len)
107 return -1;
108
109 return memcmp(ssid1, ssid2, ssid1_len);
110}
111
ffd074fc
HS
112static inline int is_same_network(struct bss_descriptor *src,
113 struct bss_descriptor *dst)
114{
115 /* A network is only a duplicate if the channel, BSSID, and ESSID
116 * all match. We treat all <hidden> with the same BSSID and channel
117 * as one network */
118 return ((src->ssid_len == dst->ssid_len) &&
119 (src->channel == dst->channel) &&
120 !compare_ether_addr(src->bssid, dst->bssid) &&
121 !memcmp(src->ssid, dst->ssid, src->ssid_len));
122}
123
876c9d3a 124
e56188ac 125
2d46502d
HS
126/*********************************************************************/
127/* */
128/* Region channel support */
129/* */
130/*********************************************************************/
131
132#define LBS_TX_PWR_DEFAULT 20 /*100mW */
133#define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
134#define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
135#define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
136#define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
137
138/* Format { channel, frequency (MHz), maxtxpower } */
139/* band: 'B/G', region: USA FCC/Canada IC */
140static struct chan_freq_power channel_freq_power_US_BG[] = {
141 {1, 2412, LBS_TX_PWR_US_DEFAULT},
142 {2, 2417, LBS_TX_PWR_US_DEFAULT},
143 {3, 2422, LBS_TX_PWR_US_DEFAULT},
144 {4, 2427, LBS_TX_PWR_US_DEFAULT},
145 {5, 2432, LBS_TX_PWR_US_DEFAULT},
146 {6, 2437, LBS_TX_PWR_US_DEFAULT},
147 {7, 2442, LBS_TX_PWR_US_DEFAULT},
148 {8, 2447, LBS_TX_PWR_US_DEFAULT},
149 {9, 2452, LBS_TX_PWR_US_DEFAULT},
150 {10, 2457, LBS_TX_PWR_US_DEFAULT},
151 {11, 2462, LBS_TX_PWR_US_DEFAULT}
152};
153
154/* band: 'B/G', region: Europe ETSI */
155static struct chan_freq_power channel_freq_power_EU_BG[] = {
156 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
157 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
158 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
159 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
160 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
161 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
162 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
163 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
164 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
165 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
166 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
167 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
168 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
169};
170
171/* band: 'B/G', region: Spain */
172static struct chan_freq_power channel_freq_power_SPN_BG[] = {
173 {10, 2457, LBS_TX_PWR_DEFAULT},
174 {11, 2462, LBS_TX_PWR_DEFAULT}
175};
176
177/* band: 'B/G', region: France */
178static struct chan_freq_power channel_freq_power_FR_BG[] = {
179 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
180 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
181 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
182 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
183};
184
185/* band: 'B/G', region: Japan */
186static struct chan_freq_power channel_freq_power_JPN_BG[] = {
187 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
188 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
189 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
190 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
191 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
192 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
193 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
194 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
195 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
196 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
197 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
198 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
199 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
200 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
201};
202
203/**
204 * the structure for channel, frequency and power
205 */
206struct region_cfp_table {
207 u8 region;
208 struct chan_freq_power *cfp_BG;
209 int cfp_no_BG;
210};
211
212/**
213 * the structure for the mapping between region and CFP
214 */
215static struct region_cfp_table region_cfp_table[] = {
216 {0x10, /*US FCC */
217 channel_freq_power_US_BG,
218 ARRAY_SIZE(channel_freq_power_US_BG),
219 }
220 ,
221 {0x20, /*CANADA IC */
222 channel_freq_power_US_BG,
223 ARRAY_SIZE(channel_freq_power_US_BG),
224 }
225 ,
226 {0x30, /*EU*/ channel_freq_power_EU_BG,
227 ARRAY_SIZE(channel_freq_power_EU_BG),
228 }
229 ,
230 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
231 ARRAY_SIZE(channel_freq_power_SPN_BG),
232 }
233 ,
234 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
235 ARRAY_SIZE(channel_freq_power_FR_BG),
236 }
237 ,
238 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
239 ARRAY_SIZE(channel_freq_power_JPN_BG),
240 }
241 ,
242/*Add new region here */
243};
244
245/**
246 * @brief This function finds the CFP in
247 * region_cfp_table based on region and band parameter.
248 *
249 * @param region The region code
250 * @param band The band
251 * @param cfp_no A pointer to CFP number
252 * @return A pointer to CFP
253 */
254static struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
255{
256 int i, end;
257
258 lbs_deb_enter(LBS_DEB_MAIN);
259
260 end = ARRAY_SIZE(region_cfp_table);
261
262 for (i = 0; i < end ; i++) {
263 lbs_deb_main("region_cfp_table[i].region=%d\n",
264 region_cfp_table[i].region);
265 if (region_cfp_table[i].region == region) {
266 *cfp_no = region_cfp_table[i].cfp_no_BG;
267 lbs_deb_leave(LBS_DEB_MAIN);
268 return region_cfp_table[i].cfp_BG;
269 }
270 }
271
272 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
273 return NULL;
274}
275
276int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
277{
278 int ret = 0;
279 int i = 0;
280
281 struct chan_freq_power *cfp;
282 int cfp_no;
283
284 lbs_deb_enter(LBS_DEB_MAIN);
285
286 memset(priv->region_channel, 0, sizeof(priv->region_channel));
287
288 cfp = lbs_get_region_cfp_table(region, &cfp_no);
289 if (cfp != NULL) {
290 priv->region_channel[i].nrcfp = cfp_no;
291 priv->region_channel[i].CFP = cfp;
292 } else {
293 lbs_deb_main("wrong region code %#x in band B/G\n",
294 region);
295 ret = -1;
296 goto out;
297 }
298 priv->region_channel[i].valid = 1;
299 priv->region_channel[i].region = region;
300 priv->region_channel[i].band = band;
301 i++;
302out:
303 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
304 return ret;
305}
306
307
308
e56188ac 309
e56188ac
HS
310/*********************************************************************/
311/* */
312/* Main scanning support */
313/* */
314/*********************************************************************/
315
876c9d3a
MT
316/**
317 * @brief Create a channel list for the driver to scan based on region info
318 *
10078321 319 * Only used from lbs_scan_setup_scan_config()
e56188ac 320 *
876c9d3a
MT
321 * Use the driver region/band information to construct a comprehensive list
322 * of channels to scan. This routine is used for any scan that is not
323 * provided a specific channel list to scan.
324 *
69f9032d 325 * @param priv A pointer to struct lbs_private structure
876c9d3a 326 * @param scanchanlist Output parameter: resulting channel list to scan
876c9d3a
MT
327 *
328 * @return void
329 */
ffd074fc 330static int lbs_scan_create_channel_list(struct lbs_private *priv,
52933d81 331 struct chanscanparamset *scanchanlist)
876c9d3a 332{
876c9d3a
MT
333 struct region_channel *scanregion;
334 struct chan_freq_power *cfp;
335 int rgnidx;
336 int chanidx;
337 int nextchan;
f137e054 338 uint8_t scantype;
876c9d3a
MT
339
340 chanidx = 0;
341
342 /* Set the default scan type to the user specified type, will later
343 * be changed to passive on a per channel basis if restricted by
344 * regulatory requirements (11d or 11h)
345 */
4f2fdaaf 346 scantype = CMD_SCAN_TYPE_ACTIVE;
876c9d3a 347
aa21c004 348 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
d37b4fdd
HS
349 if (!priv->region_channel[rgnidx].valid)
350 continue;
351 scanregion = &priv->region_channel[rgnidx];
876c9d3a 352
f137e054
DW
353 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
354 struct chanscanparamset *chan = &scanchanlist[chanidx];
876c9d3a
MT
355
356 cfp = scanregion->CFP + nextchan;
357
f137e054
DW
358 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
359 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
876c9d3a 360
0aef64d7 361 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
f137e054
DW
362 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
363 chan->chanscanmode.passivescan = 1;
876c9d3a 364 } else {
f137e054
DW
365 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
366 chan->chanscanmode.passivescan = 0;
876c9d3a
MT
367 }
368
f137e054 369 chan->channumber = cfp->channel;
876c9d3a
MT
370 }
371 }
ffd074fc 372 return chanidx;
876c9d3a
MT
373}
374
ffd074fc
HS
375/*
376 * Add SSID TLV of the form:
377 *
378 * TLV-ID SSID 00 00
379 * length 06 00
380 * ssid 4d 4e 54 45 53 54
381 */
52933d81 382static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
2afc0c5d 383{
75b6a61a 384 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
f137e054 385
ffd074fc 386 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
52933d81
HS
387 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
388 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
389 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
2afc0c5d
DW
390}
391
ffd074fc
HS
392/*
393 * Add CHANLIST TLV of the form
876c9d3a 394 *
ffd074fc
HS
395 * TLV-ID CHANLIST 01 01
396 * length 5b 00
397 * channel 1 00 01 00 00 00 64 00
398 * radio type 00
399 * channel 01
400 * scan type 00
401 * min scan time 00 00
402 * max scan time 64 00
403 * channel 2 00 02 00 00 00 64 00
404 * channel 3 00 03 00 00 00 64 00
405 * channel 4 00 04 00 00 00 64 00
406 * channel 5 00 05 00 00 00 64 00
407 * channel 6 00 06 00 00 00 64 00
408 * channel 7 00 07 00 00 00 64 00
409 * channel 8 00 08 00 00 00 64 00
410 * channel 9 00 09 00 00 00 64 00
411 * channel 10 00 0a 00 00 00 64 00
412 * channel 11 00 0b 00 00 00 64 00
413 * channel 12 00 0c 00 00 00 64 00
414 * channel 13 00 0d 00 00 00 64 00
876c9d3a 415 *
876c9d3a 416 */
f137e054
DW
417static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
418 struct chanscanparamset *chan_list,
419 int chan_count)
876c9d3a 420{
f137e054 421 size_t size = sizeof(struct chanscanparamset) *chan_count;
75b6a61a 422 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
ffd074fc
HS
423
424 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
425 memcpy(chan_tlv->chanscanparam, chan_list, size);
426 chan_tlv->header.len = cpu_to_le16(size);
427 return sizeof(chan_tlv->header) + size;
876c9d3a
MT
428}
429
ffd074fc
HS
430/*
431 * Add RATES TLV of the form
876c9d3a 432 *
ffd074fc
HS
433 * TLV-ID RATES 01 00
434 * length 0e 00
435 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
876c9d3a 436 *
ffd074fc
HS
437 * The rates are in lbs_bg_rates[], but for the 802.11b
438 * rates the high bit isn't set.
876c9d3a 439 */
f137e054 440static int lbs_scan_add_rates_tlv(uint8_t *tlv)
876c9d3a 441{
ffd074fc 442 int i;
75b6a61a 443 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
ffd074fc
HS
444
445 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
446 tlv += sizeof(rate_tlv->header);
447 for (i = 0; i < MAX_RATES; i++) {
448 *tlv = lbs_bg_rates[i];
449 if (*tlv == 0)
450 break;
451 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
452 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
453 Note that the values are MBit/s * 2, to mark them as
454 basic rates so that the firmware likes it better */
455 if (*tlv == 0x02 || *tlv == 0x04 ||
456 *tlv == 0x0b || *tlv == 0x16)
457 *tlv |= 0x80;
458 tlv++;
2afc0c5d 459 }
ffd074fc
HS
460 rate_tlv->header.len = cpu_to_le16(i);
461 return sizeof(rate_tlv->header) + i;
876c9d3a
MT
462}
463
e56188ac 464/*
ffd074fc
HS
465 * Generate the CMD_802_11_SCAN command with the proper tlv
466 * for a bunch of channels.
467 */
fa62f99c 468static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
52933d81 469 struct chanscanparamset *chan_list, int chan_count)
eb8f7330 470{
ffd074fc 471 int ret = -ENOMEM;
fa62f99c
DW
472 struct cmd_ds_802_11_scan *scan_cmd;
473 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
eb8f7330 474
fa62f99c 475 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
c0d43990
HS
476 bsstype, chan_list ? chan_list[0].channumber : -1,
477 chan_count);
e56188ac 478
ffd074fc
HS
479 /* create the fixed part for scan command */
480 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
481 if (scan_cmd == NULL)
e56188ac 482 goto out;
fa62f99c 483
ffd074fc 484 tlv = scan_cmd->tlvbuffer;
52933d81
HS
485 /* TODO: do we need to scan for a specific BSSID?
486 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
ffd074fc
HS
487 scan_cmd->bsstype = bsstype;
488
489 /* add TLVs */
52933d81
HS
490 if (priv->scan_ssid_len)
491 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
ffd074fc
HS
492 if (chan_list && chan_count)
493 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
494 tlv += lbs_scan_add_rates_tlv(tlv);
495
496 /* This is the final data we are about to send */
fa62f99c
DW
497 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
498 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
499 sizeof(*scan_cmd));
ffd074fc 500 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
fa62f99c
DW
501 tlv - scan_cmd->tlvbuffer);
502
503 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
504 le16_to_cpu(scan_cmd->hdr.size),
505 lbs_ret_80211_scan, 0);
ffd074fc 506
e56188ac 507out:
ffd074fc
HS
508 kfree(scan_cmd);
509 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
510 return ret;
eb8f7330
DW
511}
512
876c9d3a
MT
513/**
514 * @brief Internal function used to start a scan based on an input config
515 *
516 * Use the input user scan configuration information when provided in
517 * order to send the appropriate scan commands to firmware to populate or
518 * update the internal driver scan table
519 *
69f9032d 520 * @param priv A pointer to struct lbs_private structure
52933d81 521 * @param full_scan Do a full-scan (blocking)
876c9d3a
MT
522 *
523 * @return 0 or < 0 if error
524 */
245bf20f 525int lbs_scan_networks(struct lbs_private *priv, int full_scan)
876c9d3a 526{
ffd074fc
HS
527 int ret = -ENOMEM;
528 struct chanscanparamset *chan_list;
529 struct chanscanparamset *curr_chans;
530 int chan_count;
f137e054 531 uint8_t bsstype = CMD_BSS_TYPE_ANY;
ffd074fc 532 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
ffd074fc 533 union iwreq_data wrqu;
f8f55108 534#ifdef CONFIG_LIBERTAS_DEBUG
ffd074fc 535 struct bss_descriptor *iter;
f8f55108 536 int i = 0;
9387b7ca 537 DECLARE_SSID_BUF(ssid);
f8f55108 538#endif
876c9d3a 539
f137e054 540 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
2afc0c5d
DW
541
542 /* Cancel any partial outstanding partial scans if this scan
543 * is a full scan.
544 */
545 if (full_scan && delayed_work_pending(&priv->scan_work))
546 cancel_delayed_work(&priv->scan_work);
876c9d3a 547
52933d81
HS
548 /* User-specified bsstype or channel list
549 TODO: this can be implemented if some user-space application
550 need the feature. Formerly, it was accessible from debugfs,
551 but then nowhere used.
ffd074fc
HS
552 if (user_cfg) {
553 if (user_cfg->bsstype)
52933d81
HS
554 bsstype = user_cfg->bsstype;
555 } */
556
557 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
876c9d3a 558
ffd074fc
HS
559 /* Create list of channels to scan */
560 chan_list = kzalloc(sizeof(struct chanscanparamset) *
f137e054 561 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
ffd074fc
HS
562 if (!chan_list) {
563 lbs_pr_alert("SCAN: chan_list empty\n");
876c9d3a
MT
564 goto out;
565 }
566
ffd074fc 567 /* We want to scan all channels */
52933d81 568 chan_count = lbs_scan_create_channel_list(priv, chan_list);
876c9d3a 569
ffd074fc 570 netif_stop_queue(priv->dev);
659c8e52 571 if (priv->mesh_dev)
a27b9f96 572 netif_stop_queue(priv->mesh_dev);
876c9d3a 573
ffd074fc 574 /* Prepare to continue an interrupted scan */
8816edce
HS
575 lbs_deb_scan("chan_count %d, scan_channel %d\n",
576 chan_count, priv->scan_channel);
ffd074fc
HS
577 curr_chans = chan_list;
578 /* advance channel list by already-scanned-channels */
8816edce
HS
579 if (priv->scan_channel > 0) {
580 curr_chans += priv->scan_channel;
581 chan_count -= priv->scan_channel;
ffd074fc
HS
582 }
583
584 /* Send scan command(s)
585 * numchannels contains the number of channels we should maximally scan
586 * chan_count is the total number of channels to scan
587 */
588
589 while (chan_count) {
590 int to_scan = min(numchannels, chan_count);
591 lbs_deb_scan("scanning %d of %d channels\n",
f137e054 592 to_scan, chan_count);
ffd074fc 593 ret = lbs_do_scan(priv, bsstype, curr_chans,
52933d81 594 to_scan);
ffd074fc
HS
595 if (ret) {
596 lbs_pr_err("SCAN_CMD failed\n");
597 goto out2;
598 }
599 curr_chans += to_scan;
600 chan_count -= to_scan;
601
602 /* somehow schedule the next part of the scan */
f137e054 603 if (chan_count && !full_scan &&
aa21c004 604 !priv->surpriseremoved) {
ffd074fc 605 /* -1 marks just that we're currently scanning */
8816edce
HS
606 if (priv->scan_channel < 0)
607 priv->scan_channel = to_scan;
ffd074fc 608 else
8816edce 609 priv->scan_channel += to_scan;
ffd074fc
HS
610 cancel_delayed_work(&priv->scan_work);
611 queue_delayed_work(priv->work_thread, &priv->scan_work,
f137e054 612 msecs_to_jiffies(300));
ffd074fc
HS
613 /* skip over GIWSCAN event */
614 goto out;
615 }
616
617 }
618 memset(&wrqu, 0, sizeof(union iwreq_data));
619 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
876c9d3a 620
f8f55108
DW
621#ifdef CONFIG_LIBERTAS_DEBUG
622 /* Dump the scan table */
aa21c004 623 mutex_lock(&priv->lock);
ffd074fc 624 lbs_deb_scan("scan table:\n");
aa21c004 625 list_for_each_entry(iter, &priv->network_list, list)
e174961c
JB
626 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
627 i++, iter->bssid, iter->rssi,
9387b7ca 628 print_ssid(ssid, iter->ssid, iter->ssid_len));
aa21c004 629 mutex_unlock(&priv->lock);
f8f55108 630#endif
876c9d3a 631
ffd074fc 632out2:
8816edce 633 priv->scan_channel = 0;
ffd074fc
HS
634
635out:
659c8e52
SO
636 if (priv->connect_status == LBS_CONNECTED && !priv->tx_pending_len)
637 netif_wake_queue(priv->dev);
638
891dc5e7 639 if (priv->mesh_dev && lbs_mesh_connected(priv) &&
659c8e52
SO
640 !priv->tx_pending_len)
641 netif_wake_queue(priv->mesh_dev);
642
ffd074fc 643 kfree(chan_list);
876c9d3a 644
9012b28a 645 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
876c9d3a
MT
646 return ret;
647}
648
52933d81
HS
649void lbs_scan_worker(struct work_struct *work)
650{
651 struct lbs_private *priv =
652 container_of(work, struct lbs_private, scan_work.work);
653
654 lbs_deb_enter(LBS_DEB_SCAN);
655 lbs_scan_networks(priv, 0);
656 lbs_deb_leave(LBS_DEB_SCAN);
657}
658
659
ffd074fc
HS
660/*********************************************************************/
661/* */
662/* Result interpretation */
663/* */
664/*********************************************************************/
665
876c9d3a
MT
666/**
667 * @brief Interpret a BSS scan response returned from the firmware
668 *
669 * Parse the various fixed fields and IEs passed back for a a BSS probe
ffd074fc
HS
670 * response or beacon from the scan command. Record information as needed
671 * in the scan table struct bss_descriptor for that entry.
876c9d3a 672 *
fcdb53db 673 * @param bss Output parameter: Pointer to the BSS Entry
876c9d3a
MT
674 *
675 * @return 0 or -1
676 */
10078321 677static int lbs_process_bss(struct bss_descriptor *bss,
f137e054 678 uint8_t **pbeaconinfo, int *bytesleft)
876c9d3a 679{
5fd164e9
DW
680 struct ieee_ie_fh_param_set *fh;
681 struct ieee_ie_ds_param_set *ds;
682 struct ieee_ie_cf_param_set *cf;
683 struct ieee_ie_ibss_param_set *ibss;
9387b7ca 684 DECLARE_SSID_BUF(ssid);
f137e054
DW
685 uint8_t *pos, *end, *p;
686 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
687 uint16_t beaconsize = 0;
8c512765 688 int ret;
876c9d3a 689
e56188ac 690 lbs_deb_enter(LBS_DEB_SCAN);
876c9d3a 691
876c9d3a
MT
692 if (*bytesleft >= sizeof(beaconsize)) {
693 /* Extract & convert beacon size from the command buffer */
533dd1b0 694 beaconsize = get_unaligned_le16(*pbeaconinfo);
876c9d3a
MT
695 *bytesleft -= sizeof(beaconsize);
696 *pbeaconinfo += sizeof(beaconsize);
697 }
698
699 if (beaconsize == 0 || beaconsize > *bytesleft) {
876c9d3a
MT
700 *pbeaconinfo += *bytesleft;
701 *bytesleft = 0;
e56188ac
HS
702 ret = -1;
703 goto done;
876c9d3a
MT
704 }
705
706 /* Initialize the current working beacon pointer for this BSS iteration */
ab617971
DW
707 pos = *pbeaconinfo;
708 end = pos + beaconsize;
876c9d3a
MT
709
710 /* Advance the return beacon pointer past the current beacon */
711 *pbeaconinfo += beaconsize;
712 *bytesleft -= beaconsize;
713
ab617971 714 memcpy(bss->bssid, pos, ETH_ALEN);
e174961c 715 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
ab617971 716 pos += ETH_ALEN;
876c9d3a 717
ab617971 718 if ((end - pos) < 12) {
fcdb53db 719 lbs_deb_scan("process_bss: Not enough bytes left\n");
e56188ac
HS
720 ret = -1;
721 goto done;
876c9d3a
MT
722 }
723
724 /*
725 * next 4 fields are RSSI, time stamp, beacon interval,
726 * and capability information
727 */
728
729 /* RSSI is 1 byte long */
ab617971 730 bss->rssi = *pos;
ffd074fc 731 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
ab617971 732 pos++;
876c9d3a
MT
733
734 /* time stamp is 8 bytes long */
ab617971 735 pos += 8;
876c9d3a
MT
736
737 /* beacon interval is 2 bytes long */
814feefa 738 bss->beaconperiod = get_unaligned_le16(pos);
ab617971 739 pos += 2;
876c9d3a
MT
740
741 /* capability information is 2 bytes long */
814feefa 742 bss->capability = get_unaligned_le16(pos);
ffd074fc 743 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
ab617971 744 pos += 2;
876c9d3a 745
0c9ca690 746 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
ffd074fc 747 lbs_deb_scan("process_bss: WEP enabled\n");
0c9ca690
DW
748 if (bss->capability & WLAN_CAPABILITY_IBSS)
749 bss->mode = IW_MODE_ADHOC;
750 else
751 bss->mode = IW_MODE_INFRA;
752
876c9d3a 753 /* rest of the current buffer are IE's */
ffd074fc 754 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
ece56191 755 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
876c9d3a 756
876c9d3a 757 /* process variable IE */
ab617971 758 while (pos <= end - 2) {
2c706002 759 if (pos + pos[1] > end) {
fcdb53db 760 lbs_deb_scan("process_bss: error in processing IE, "
f137e054 761 "bytes left < IE length\n");
ab617971 762 break;
876c9d3a
MT
763 }
764
2c706002
JB
765 switch (pos[0]) {
766 case WLAN_EID_SSID:
767 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
768 memcpy(bss->ssid, pos + 2, bss->ssid_len);
ffd074fc 769 lbs_deb_scan("got SSID IE: '%s', len %u\n",
9387b7ca 770 print_ssid(ssid, bss->ssid, bss->ssid_len),
d8efea25 771 bss->ssid_len);
876c9d3a
MT
772 break;
773
2c706002
JB
774 case WLAN_EID_SUPP_RATES:
775 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
776 memcpy(bss->rates, pos + 2, n_basic_rates);
8c512765 777 got_basic_rates = 1;
ffd074fc 778 lbs_deb_scan("got RATES IE\n");
876c9d3a
MT
779 break;
780
2c706002 781 case WLAN_EID_FH_PARAMS:
5fd164e9
DW
782 fh = (struct ieee_ie_fh_param_set *) pos;
783 memcpy(&bss->phy.fh, fh, sizeof(*fh));
ffd074fc 784 lbs_deb_scan("got FH IE\n");
876c9d3a
MT
785 break;
786
2c706002 787 case WLAN_EID_DS_PARAMS:
5fd164e9
DW
788 ds = (struct ieee_ie_ds_param_set *) pos;
789 bss->channel = ds->channel;
790 memcpy(&bss->phy.ds, ds, sizeof(*ds));
ffd074fc 791 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
876c9d3a
MT
792 break;
793
2c706002 794 case WLAN_EID_CF_PARAMS:
5fd164e9
DW
795 cf = (struct ieee_ie_cf_param_set *) pos;
796 memcpy(&bss->ss.cf, cf, sizeof(*cf));
ffd074fc 797 lbs_deb_scan("got CF IE\n");
876c9d3a
MT
798 break;
799
2c706002 800 case WLAN_EID_IBSS_PARAMS:
5fd164e9
DW
801 ibss = (struct ieee_ie_ibss_param_set *) pos;
802 bss->atimwindow = ibss->atimwindow;
803 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
ffd074fc 804 lbs_deb_scan("got IBSS IE\n");
876c9d3a
MT
805 break;
806
2c706002 807 case WLAN_EID_EXT_SUPP_RATES:
ab617971
DW
808 /* only process extended supported rate if data rate is
809 * already found. Data rate IE should come before
876c9d3a
MT
810 * extended supported rate IE
811 */
ffd074fc
HS
812 lbs_deb_scan("got RATESEX IE\n");
813 if (!got_basic_rates) {
814 lbs_deb_scan("... but ignoring it\n");
ab617971 815 break;
ffd074fc 816 }
876c9d3a 817
2c706002 818 n_ex_rates = pos[1];
8c512765
DW
819 if (n_basic_rates + n_ex_rates > MAX_RATES)
820 n_ex_rates = MAX_RATES - n_basic_rates;
876c9d3a 821
8c512765 822 p = bss->rates + n_basic_rates;
2c706002 823 memcpy(p, pos + 2, n_ex_rates);
876c9d3a 824 break;
ab617971 825
2c706002
JB
826 case WLAN_EID_GENERIC:
827 if (pos[1] >= 4 &&
828 pos[2] == 0x00 && pos[3] == 0x50 &&
829 pos[4] == 0xf2 && pos[5] == 0x01) {
830 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
831 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
ffd074fc 832 lbs_deb_scan("got WPA IE\n");
2c706002
JB
833 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
834 bss->wpa_ie_len);
835 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
836 pos[2] == 0x00 && pos[3] == 0x50 &&
baf62eec 837 pos[4] == 0x43 && pos[5] == 0x04) {
ffd074fc 838 lbs_deb_scan("got mesh IE\n");
1e838bf3 839 bss->mesh = 1;
ffd074fc 840 } else {
f137e054 841 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
2c706002
JB
842 pos[2], pos[3],
843 pos[4], pos[5],
844 pos[1]);
ab617971 845 }
876c9d3a 846 break;
ab617971 847
2c706002 848 case WLAN_EID_RSN:
ffd074fc 849 lbs_deb_scan("got RSN IE\n");
2c706002
JB
850 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
851 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
ffd074fc 852 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
2c706002 853 bss->rsn_ie, bss->rsn_ie_len);
876c9d3a
MT
854 break;
855
ab617971 856 default:
ffd074fc 857 lbs_deb_scan("got IE 0x%04x, len %d\n",
2c706002 858 pos[0], pos[1]);
876c9d3a
MT
859 break;
860 }
861
2c706002 862 pos += pos[1] + 2;
ab617971 863 }
fcdb53db
DW
864
865 /* Timestamp */
866 bss->last_scanned = jiffies;
10078321 867 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
fcdb53db 868
9012b28a 869 ret = 0;
876c9d3a 870
9012b28a
HS
871done:
872 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
873 return ret;
876c9d3a
MT
874}
875
876c9d3a
MT
876/**
877 * @brief Send a scan command for all available channels filtered on a spec
878 *
e56188ac
HS
879 * Used in association code and from debugfs
880 *
69f9032d 881 * @param priv A pointer to struct lbs_private structure
e56188ac
HS
882 * @param ssid A pointer to the SSID to scan for
883 * @param ssid_len Length of the SSID
876c9d3a
MT
884 *
885 * @return 0-success, otherwise fail
886 */
f137e054 887int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
52933d81 888 uint8_t ssid_len)
876c9d3a 889{
9387b7ca 890 DECLARE_SSID_BUF(ssid_buf);
eb8f7330 891 int ret = 0;
876c9d3a 892
52933d81 893 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
9387b7ca 894 print_ssid(ssid_buf, ssid, ssid_len));
876c9d3a 895
d8efea25 896 if (!ssid_len)
eb8f7330 897 goto out;
876c9d3a 898
52933d81
HS
899 memcpy(priv->scan_ssid, ssid, ssid_len);
900 priv->scan_ssid_len = ssid_len;
876c9d3a 901
52933d81 902 lbs_scan_networks(priv, 1);
aa21c004 903 if (priv->surpriseremoved) {
e56188ac
HS
904 ret = -1;
905 goto out;
906 }
876c9d3a 907
eb8f7330 908out:
e56188ac 909 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
eb8f7330 910 return ret;
876c9d3a
MT
911}
912
e56188ac
HS
913
914
915
916/*********************************************************************/
917/* */
918/* Support for Wireless Extensions */
919/* */
920/*********************************************************************/
921
ffd074fc 922
00af0157
DW
923#define MAX_CUSTOM_LEN 64
924
69f9032d 925static inline char *lbs_translate_scan(struct lbs_private *priv,
ccc58057
DM
926 struct iw_request_info *info,
927 char *start, char *stop,
928 struct bss_descriptor *bss)
876c9d3a 929{
876c9d3a 930 struct chan_freq_power *cfp;
876c9d3a
MT
931 char *current_val; /* For rates */
932 struct iw_event iwe; /* Temporary buffer */
876c9d3a 933 int j;
f137e054
DW
934#define PERFECT_RSSI ((uint8_t)50)
935#define WORST_RSSI ((uint8_t)0)
936#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
937 uint8_t rssi;
876c9d3a 938
e56188ac
HS
939 lbs_deb_enter(LBS_DEB_SCAN);
940
aa21c004 941 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
fcdb53db
DW
942 if (!cfp) {
943 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
e56188ac
HS
944 start = NULL;
945 goto out;
2be92196 946 }
876c9d3a 947
ffd074fc 948 /* First entry *MUST* be the BSSID */
fcdb53db
DW
949 iwe.cmd = SIOCGIWAP;
950 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
951 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
ccc58057 952 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
fcdb53db
DW
953
954 /* SSID */
955 iwe.cmd = SIOCGIWESSID;
956 iwe.u.data.flags = 1;
243e84e9 957 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IEEE80211_MAX_SSID_LEN);
ccc58057 958 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
fcdb53db
DW
959
960 /* Mode */
961 iwe.cmd = SIOCGIWMODE;
962 iwe.u.mode = bss->mode;
ccc58057 963 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
fcdb53db
DW
964
965 /* Frequency */
966 iwe.cmd = SIOCGIWFREQ;
967 iwe.u.freq.m = (long)cfp->freq * 100000;
968 iwe.u.freq.e = 1;
ccc58057 969 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
fcdb53db
DW
970
971 /* Add quality statistics */
972 iwe.cmd = IWEVQUAL;
973 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
974 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
975
976 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
977 iwe.u.qual.qual =
f137e054
DW
978 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
979 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
980 (RSSI_DIFF * RSSI_DIFF);
fcdb53db
DW
981 if (iwe.u.qual.qual > 100)
982 iwe.u.qual.qual = 100;
983
aa21c004 984 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
fcdb53db
DW
985 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
986 } else {
f137e054 987 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
fcdb53db 988 }
80e78ef7
DW
989
990 /* Locally created ad-hoc BSSs won't have beacons if this is the
991 * only station in the adhoc network; so get signal strength
992 * from receive statistics.
993 */
f137e054 994 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
aa21c004 995 && !lbs_ssid_cmp(priv->curbssparams.ssid,
f137e054
DW
996 priv->curbssparams.ssid_len,
997 bss->ssid, bss->ssid_len)) {
80e78ef7 998 int snr, nf;
aa21c004
DW
999 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1000 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
80e78ef7 1001 iwe.u.qual.level = CAL_RSSI(snr, nf);
fcdb53db 1002 }
ccc58057 1003 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
876c9d3a 1004
fcdb53db
DW
1005 /* Add encryption capability */
1006 iwe.cmd = SIOCGIWENCODE;
0c9ca690 1007 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
fcdb53db
DW
1008 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1009 } else {
1010 iwe.u.data.flags = IW_ENCODE_DISABLED;
1011 }
1012 iwe.u.data.length = 0;
ccc58057 1013 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
876c9d3a 1014
ccc58057 1015 current_val = start + iwe_stream_lcp_len(info);
876c9d3a 1016
fcdb53db
DW
1017 iwe.cmd = SIOCGIWRATE;
1018 iwe.u.bitrate.fixed = 0;
1019 iwe.u.bitrate.disabled = 0;
1020 iwe.u.bitrate.value = 0;
876c9d3a 1021
430453fc 1022 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
8c512765
DW
1023 /* Bit rate given in 500 kb/s units */
1024 iwe.u.bitrate.value = bss->rates[j] * 500000;
ccc58057
DM
1025 current_val = iwe_stream_add_value(info, start, current_val,
1026 stop, &iwe, IW_EV_PARAM_LEN);
fcdb53db 1027 }
f137e054 1028 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
aa21c004 1029 && !lbs_ssid_cmp(priv->curbssparams.ssid,
f137e054
DW
1030 priv->curbssparams.ssid_len,
1031 bss->ssid, bss->ssid_len)) {
fcdb53db 1032 iwe.u.bitrate.value = 22 * 500000;
ccc58057 1033 current_val = iwe_stream_add_value(info, start, current_val,
f137e054 1034 stop, &iwe, IW_EV_PARAM_LEN);
fcdb53db
DW
1035 }
1036 /* Check if we added any event */
ccc58057 1037 if ((current_val - start) > iwe_stream_lcp_len(info))
fcdb53db
DW
1038 start = current_val;
1039
1040 memset(&iwe, 0, sizeof(iwe));
1041 if (bss->wpa_ie_len) {
1042 char buf[MAX_WPA_IE_LEN];
1043 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1044 iwe.cmd = IWEVGENIE;
1045 iwe.u.data.length = bss->wpa_ie_len;
ccc58057 1046 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
fcdb53db 1047 }
876c9d3a 1048
fcdb53db
DW
1049 memset(&iwe, 0, sizeof(iwe));
1050 if (bss->rsn_ie_len) {
1051 char buf[MAX_WPA_IE_LEN];
1052 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1053 iwe.cmd = IWEVGENIE;
1054 iwe.u.data.length = bss->rsn_ie_len;
ccc58057 1055 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
fcdb53db 1056 }
876c9d3a 1057
00af0157
DW
1058 if (bss->mesh) {
1059 char custom[MAX_CUSTOM_LEN];
1060 char *p = custom;
1061
1062 iwe.cmd = IWEVCUSTOM;
f137e054 1063 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
00af0157
DW
1064 iwe.u.data.length = p - custom;
1065 if (iwe.u.data.length)
ccc58057
DM
1066 start = iwe_stream_add_point(info, start, stop,
1067 &iwe, custom);
00af0157
DW
1068 }
1069
e56188ac
HS
1070out:
1071 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
fcdb53db
DW
1072 return start;
1073}
876c9d3a 1074
ffd074fc
HS
1075
1076/**
1077 * @brief Handle Scan Network ioctl
1078 *
1079 * @param dev A pointer to net_device structure
1080 * @param info A pointer to iw_request_info structure
1081 * @param vwrq A pointer to iw_param structure
1082 * @param extra A pointer to extra data buf
1083 *
1084 * @return 0 --success, otherwise fail
1085 */
1086int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
52933d81 1087 union iwreq_data *wrqu, char *extra)
ffd074fc 1088{
9387b7ca 1089 DECLARE_SSID_BUF(ssid);
ab65f649 1090 struct lbs_private *priv = dev->ml_priv;
52933d81 1091 int ret = 0;
ffd074fc 1092
52933d81 1093 lbs_deb_enter(LBS_DEB_WEXT);
ffd074fc 1094
d5db2dfa
DW
1095 if (!priv->radio_on) {
1096 ret = -EINVAL;
1097 goto out;
1098 }
1099
52933d81
HS
1100 if (!netif_running(dev)) {
1101 ret = -ENETDOWN;
1102 goto out;
1103 }
ffd074fc
HS
1104
1105 /* mac80211 does this:
1106 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
52933d81
HS
1107 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
1108 ret = -EOPNOTSUPP;
1109 goto out;
1110 }
1111 */
ffd074fc
HS
1112
1113 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1114 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
52933d81
HS
1115 struct iw_scan_req *req = (struct iw_scan_req *)extra;
1116 priv->scan_ssid_len = req->essid_len;
1117 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
1118 lbs_deb_wext("set_scan, essid '%s'\n",
9387b7ca 1119 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
52933d81
HS
1120 } else {
1121 priv->scan_ssid_len = 0;
ffd074fc 1122 }
ffd074fc
HS
1123
1124 if (!delayed_work_pending(&priv->scan_work))
1125 queue_delayed_work(priv->work_thread, &priv->scan_work,
f137e054 1126 msecs_to_jiffies(50));
ffd074fc 1127 /* set marker that currently a scan is taking place */
8816edce 1128 priv->scan_channel = -1;
ffd074fc 1129
aa21c004 1130 if (priv->surpriseremoved)
52933d81 1131 ret = -EIO;
ffd074fc 1132
52933d81
HS
1133out:
1134 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1135 return ret;
ffd074fc
HS
1136}
1137
1138
fcdb53db 1139/**
e56188ac 1140 * @brief Handle Retrieve scan table ioctl
fcdb53db
DW
1141 *
1142 * @param dev A pointer to net_device structure
1143 * @param info A pointer to iw_request_info structure
1144 * @param dwrq A pointer to iw_point structure
1145 * @param extra A pointer to extra data buf
1146 *
1147 * @return 0 --success, otherwise fail
1148 */
10078321 1149int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
f137e054 1150 struct iw_point *dwrq, char *extra)
fcdb53db
DW
1151{
1152#define SCAN_ITEM_SIZE 128
ab65f649 1153 struct lbs_private *priv = dev->ml_priv;
fcdb53db
DW
1154 int err = 0;
1155 char *ev = extra;
1156 char *stop = ev + dwrq->length;
f137e054
DW
1157 struct bss_descriptor *iter_bss;
1158 struct bss_descriptor *safe;
876c9d3a 1159
52933d81 1160 lbs_deb_enter(LBS_DEB_WEXT);
876c9d3a 1161
ffd074fc 1162 /* iwlist should wait until the current scan is finished */
8816edce 1163 if (priv->scan_channel)
ffd074fc
HS
1164 return -EAGAIN;
1165
80e78ef7 1166 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
c0bbd576
AK
1167 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
1168 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1169 CMD_OPTION_WAITFORRSP, 0, NULL);
1170 if (err)
1171 goto out;
1172 }
80e78ef7 1173
aa21c004
DW
1174 mutex_lock(&priv->lock);
1175 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
f137e054 1176 char *next_ev;
fcdb53db 1177 unsigned long stale_time;
876c9d3a 1178
fcdb53db
DW
1179 if (stop - ev < SCAN_ITEM_SIZE) {
1180 err = -E2BIG;
1181 break;
876c9d3a 1182 }
876c9d3a 1183
1e838bf3
LCC
1184 /* For mesh device, list only mesh networks */
1185 if (dev == priv->mesh_dev && !iter_bss->mesh)
1186 continue;
1187
fcdb53db
DW
1188 /* Prune old an old scan result */
1189 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1190 if (time_after(jiffies, stale_time)) {
f137e054 1191 list_move_tail(&iter_bss->list, &priv->network_free_list);
fcdb53db
DW
1192 clear_bss_descriptor(iter_bss);
1193 continue;
876c9d3a
MT
1194 }
1195
fcdb53db 1196 /* Translate to WE format this entry */
ccc58057 1197 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
fcdb53db
DW
1198 if (next_ev == NULL)
1199 continue;
1200 ev = next_ev;
876c9d3a 1201 }
aa21c004 1202 mutex_unlock(&priv->lock);
876c9d3a 1203
fcdb53db 1204 dwrq->length = (ev - extra);
876c9d3a 1205 dwrq->flags = 0;
c0bbd576 1206out:
52933d81 1207 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
fcdb53db 1208 return err;
876c9d3a
MT
1209}
1210
e56188ac
HS
1211
1212
1213
1214/*********************************************************************/
1215/* */
1216/* Command execution */
1217/* */
1218/*********************************************************************/
1219
1220
876c9d3a
MT
1221/**
1222 * @brief This function handles the command response of scan
1223 *
e56188ac
HS
1224 * Called from handle_cmd_response() in cmdrespc.
1225 *
876c9d3a
MT
1226 * The response buffer for the scan command has the following
1227 * memory layout:
1228 *
1229 * .-----------------------------------------------------------.
1230 * | header (4 * sizeof(u16)): Standard command response hdr |
1231 * .-----------------------------------------------------------.
1232 * | bufsize (u16) : sizeof the BSS Description data |
1233 * .-----------------------------------------------------------.
1234 * | NumOfSet (u8) : Number of BSS Descs returned |
1235 * .-----------------------------------------------------------.
1236 * | BSSDescription data (variable, size given in bufsize) |
1237 * .-----------------------------------------------------------.
1238 * | TLV data (variable, size calculated using header->size, |
1239 * | bufsize and sizeof the fixed fields above) |
1240 * .-----------------------------------------------------------.
1241 *
69f9032d 1242 * @param priv A pointer to struct lbs_private structure
876c9d3a
MT
1243 * @param resp A pointer to cmd_ds_command
1244 *
1245 * @return 0 or -1
1246 */
fa62f99c
DW
1247static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1248 struct cmd_header *resp)
876c9d3a 1249{
fa62f99c 1250 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
f137e054
DW
1251 struct bss_descriptor *iter_bss;
1252 struct bss_descriptor *safe;
fa62f99c
DW
1253 uint8_t *bssinfo;
1254 uint16_t scanrespsize;
876c9d3a 1255 int bytesleft;
876c9d3a
MT
1256 int idx;
1257 int tlvbufsize;
9012b28a 1258 int ret;
876c9d3a 1259
e56188ac 1260 lbs_deb_enter(LBS_DEB_SCAN);
876c9d3a 1261
fcdb53db 1262 /* Prune old entries from scan table */
aa21c004 1263 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
fcdb53db
DW
1264 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1265 if (time_before(jiffies, stale_time))
1266 continue;
aa21c004 1267 list_move_tail (&iter_bss->list, &priv->network_free_list);
fcdb53db
DW
1268 clear_bss_descriptor(iter_bss);
1269 }
1270
fa62f99c
DW
1271 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1272 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1273 scanresp->nr_sets, MAX_NETWORK_COUNT);
9012b28a
HS
1274 ret = -1;
1275 goto done;
876c9d3a
MT
1276 }
1277
2c5b9e51 1278 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
9012b28a 1279 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
876c9d3a 1280
e7240aca 1281 scanrespsize = le16_to_cpu(resp->size);
fa62f99c 1282 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
876c9d3a 1283
fa62f99c 1284 bssinfo = scanresp->bssdesc_and_tlvbuffer;
876c9d3a
MT
1285
1286 /* The size of the TLV buffer is equal to the entire command response
1287 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1288 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
8ec97cc8 1289 * response header (sizeof(struct cmd_header))
876c9d3a 1290 */
fa62f99c
DW
1291 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1292 + sizeof(scanresp->nr_sets)
8ec97cc8 1293 + sizeof(struct cmd_header));
876c9d3a 1294
876c9d3a 1295 /*
fa62f99c 1296 * Process each scan response returned (scanresp->nr_sets). Save
876c9d3a
MT
1297 * the information in the newbssentry and then insert into the
1298 * driver scan table either as an update to an existing entry
1299 * or as an addition at the end of the table
1300 */
fa62f99c 1301 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
fcdb53db 1302 struct bss_descriptor new;
fa62f99c
DW
1303 struct bss_descriptor *found = NULL;
1304 struct bss_descriptor *oldest = NULL;
876c9d3a
MT
1305
1306 /* Process the data fields and IEs returned for this BSS */
fcdb53db 1307 memset(&new, 0, sizeof (struct bss_descriptor));
fa62f99c 1308 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
fcdb53db
DW
1309 /* error parsing the scan response, skipped */
1310 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1311 continue;
1312 }
876c9d3a 1313
fcdb53db 1314 /* Try to find this bss in the scan table */
aa21c004 1315 list_for_each_entry (iter_bss, &priv->network_list, list) {
fcdb53db
DW
1316 if (is_same_network(iter_bss, &new)) {
1317 found = iter_bss;
1318 break;
876c9d3a
MT
1319 }
1320
fcdb53db
DW
1321 if ((oldest == NULL) ||
1322 (iter_bss->last_scanned < oldest->last_scanned))
1323 oldest = iter_bss;
1324 }
876c9d3a 1325
fcdb53db
DW
1326 if (found) {
1327 /* found, clear it */
1328 clear_bss_descriptor(found);
aa21c004 1329 } else if (!list_empty(&priv->network_free_list)) {
fcdb53db 1330 /* Pull one from the free list */
aa21c004 1331 found = list_entry(priv->network_free_list.next,
fcdb53db 1332 struct bss_descriptor, list);
aa21c004 1333 list_move_tail(&found->list, &priv->network_list);
fcdb53db
DW
1334 } else if (oldest) {
1335 /* If there are no more slots, expire the oldest */
1336 found = oldest;
1337 clear_bss_descriptor(found);
aa21c004 1338 list_move_tail(&found->list, &priv->network_list);
876c9d3a 1339 } else {
fcdb53db
DW
1340 continue;
1341 }
876c9d3a 1342
e174961c 1343 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
fcdb53db 1344
fcdb53db
DW
1345 /* Copy the locally created newbssentry to the scan table */
1346 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1347 }
876c9d3a 1348
9012b28a 1349 ret = 0;
876c9d3a 1350
9012b28a
HS
1351done:
1352 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1353 return ret;
876c9d3a 1354}