blob: fcaef38d5df95b1c91fa544bad3a59e3a7c5868b [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
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 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04007#include <linux/etherdevice.h>
Vladimir Davydovac630c22007-09-06 21:45:36 -04008#include <asm/unaligned.h>
9
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "host.h"
11#include "decl.h"
12#include "dev.h"
13#include "scan.h"
David Woodhousefa62f992008-03-03 12:18:03 +010014#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015
16//! Approximate amount of data needed to pass a scan result back to iwlist
17#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
18 + IW_ESSID_MAX_SIZE \
19 + IW_EV_UINT_LEN \
20 + IW_EV_FREQ_LEN \
21 + IW_EV_QUAL_LEN \
22 + IW_ESSID_MAX_SIZE \
23 + IW_EV_PARAM_LEN \
24 + 40) /* 40 for WPAIE */
25
26//! Memory needed to store a max sized channel List TLV for a firmware scan
27#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
28 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
29 * sizeof(struct chanscanparamset)))
30
31//! Memory needed to store a max number/size SSID TLV for a firmware scan
32#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
33
David Woodhousefa62f992008-03-03 12:18:03 +010034//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
35#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
36 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020037
38//! The maximum number of channels the firmware can scan per command
39#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
40
41/**
42 * @brief Number of channels to scan per firmware scan command issuance.
43 *
44 * Number restricted to prevent hitting the limit on the amount of scan data
45 * returned in a single firmware scan command.
46 */
47#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
48
49//! Scan time specified in the channel TLV for each channel for passive scans
50#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
51
52//! Scan time specified in the channel TLV for each channel for active scans
53#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
54
David Woodhousefa62f992008-03-03 12:18:03 +010055static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
56 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020057
58/*********************************************************************/
59/* */
60/* Misc helper functions */
61/* */
62/*********************************************************************/
63
Holger Schurig23ff5032008-01-28 17:27:03 +010064/**
65 * @brief Unsets the MSB on basic rates
66 *
67 * Scan through an array and unset the MSB for basic data rates.
68 *
69 * @param rates buffer of data rates
70 * @param len size of buffer
71 */
72static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
73{
74 int i;
75
76 for (i = 0; i < len; i++)
77 rates[i] &= 0x7f;
78}
79
80
David Woodhousef137e052008-03-03 12:18:59 +010081static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040082{
83 /* Don't blow away ->list, just BSS data */
84 memset(bss, 0, offsetof(struct bss_descriptor, list));
85}
86
Holger Schurigffd074f2007-12-07 16:52:10 +010087/**
88 * @brief Compare two SSIDs
89 *
90 * @param ssid1 A pointer to ssid to compare
91 * @param ssid2 A pointer to ssid to compare
92 *
93 * @return 0: ssid is same, otherwise is different
94 */
David Woodhousef137e052008-03-03 12:18:59 +010095int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
96 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +010097{
98 if (ssid1_len != ssid2_len)
99 return -1;
100
101 return memcmp(ssid1, ssid2, ssid1_len);
102}
103
Holger Schurigffd074f2007-12-07 16:52:10 +0100104static inline int is_same_network(struct bss_descriptor *src,
105 struct bss_descriptor *dst)
106{
107 /* A network is only a duplicate if the channel, BSSID, and ESSID
108 * all match. We treat all <hidden> with the same BSSID and channel
109 * as one network */
110 return ((src->ssid_len == dst->ssid_len) &&
111 (src->channel == dst->channel) &&
112 !compare_ether_addr(src->bssid, dst->bssid) &&
113 !memcmp(src->ssid, dst->ssid, src->ssid_len));
114}
115
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200116
Holger Schurige56188a2007-10-09 14:15:19 +0200117
118
Holger Schurige56188a2007-10-09 14:15:19 +0200119/*********************************************************************/
120/* */
121/* Main scanning support */
122/* */
123/*********************************************************************/
124
Holger Schurige56188a2007-10-09 14:15:19 +0200125/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200126 * @brief Create a channel list for the driver to scan based on region info
127 *
Holger Schurig10078322007-11-15 18:05:47 -0500128 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200129 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130 * Use the driver region/band information to construct a comprehensive list
131 * of channels to scan. This routine is used for any scan that is not
132 * provided a specific channel list to scan.
133 *
Holger Schurig69f90322007-11-23 15:43:44 +0100134 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 *
137 * @return void
138 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100139static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100140 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 struct region_channel *scanregion;
143 struct chan_freq_power *cfp;
144 int rgnidx;
145 int chanidx;
146 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100147 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148
149 chanidx = 0;
150
151 /* Set the default scan type to the user specified type, will later
152 * be changed to passive on a per channel basis if restricted by
153 * regulatory requirements (11d or 11h)
154 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400155 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200156
David Woodhouseaa21c002007-12-08 20:04:36 +0000157 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
David Woodhousef137e052008-03-03 12:18:59 +0100158 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
159 && (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200160 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000161 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000163 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164
165 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000166 memset(&priv->parsed_region_chan, 0x00,
167 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000169 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000171 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172 }
173
David Woodhousef137e052008-03-03 12:18:59 +0100174 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
175 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200176
177 cfp = scanregion->CFP + nextchan;
178
David Woodhousef137e052008-03-03 12:18:59 +0100179 if (priv->enable11d)
180 scantype = lbs_get_scan_type_11d(cfp->channel,
181 &priv->parsed_region_chan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182
David Woodhousef137e052008-03-03 12:18:59 +0100183 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
184 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185
Dan Williams0aef64d2007-08-02 11:31:18 -0400186 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100187 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
188 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200189 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100190 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
191 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 }
193
David Woodhousef137e052008-03-03 12:18:59 +0100194 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 }
196 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100197 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200198}
199
Holger Schurige56188a2007-10-09 14:15:19 +0200200/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100201 * Add SSID TLV of the form:
202 *
203 * TLV-ID SSID 00 00
204 * length 06 00
205 * ssid 4d 4e 54 45 53 54
206 */
Holger Schurig52933d82008-03-05 07:05:32 +0100207static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400208{
David Woodhousef137e052008-03-03 12:18:59 +0100209 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
210
Holger Schurigffd074f2007-12-07 16:52:10 +0100211 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100212 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
213 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
214 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100215}
Dan Williamseb8f7332007-05-25 16:25:21 -0400216
Holger Schurigffd074f2007-12-07 16:52:10 +0100217/*
218 * Add CHANLIST TLV of the form
219 *
220 * TLV-ID CHANLIST 01 01
221 * length 5b 00
222 * channel 1 00 01 00 00 00 64 00
223 * radio type 00
224 * channel 01
225 * scan type 00
226 * min scan time 00 00
227 * max scan time 64 00
228 * channel 2 00 02 00 00 00 64 00
229 * channel 3 00 03 00 00 00 64 00
230 * channel 4 00 04 00 00 00 64 00
231 * channel 5 00 05 00 00 00 64 00
232 * channel 6 00 06 00 00 00 64 00
233 * channel 7 00 07 00 00 00 64 00
234 * channel 8 00 08 00 00 00 64 00
235 * channel 9 00 09 00 00 00 64 00
236 * channel 10 00 0a 00 00 00 64 00
237 * channel 11 00 0b 00 00 00 64 00
238 * channel 12 00 0c 00 00 00 64 00
239 * channel 13 00 0d 00 00 00 64 00
240 *
241 */
David Woodhousef137e052008-03-03 12:18:59 +0100242static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
243 struct chanscanparamset *chan_list,
244 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100245{
David Woodhousef137e052008-03-03 12:18:59 +0100246 size_t size = sizeof(struct chanscanparamset) *chan_count;
247 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400248
Holger Schurigffd074f2007-12-07 16:52:10 +0100249 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
250 memcpy(chan_tlv->chanscanparam, chan_list, size);
251 chan_tlv->header.len = cpu_to_le16(size);
252 return sizeof(chan_tlv->header) + size;
253}
Dan Williamseb8f7332007-05-25 16:25:21 -0400254
Holger Schurigffd074f2007-12-07 16:52:10 +0100255/*
256 * Add RATES TLV of the form
257 *
258 * TLV-ID RATES 01 00
259 * length 0e 00
260 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
261 *
262 * The rates are in lbs_bg_rates[], but for the 802.11b
263 * rates the high bit isn't set.
264 */
David Woodhousef137e052008-03-03 12:18:59 +0100265static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100266{
267 int i;
David Woodhousef137e052008-03-03 12:18:59 +0100268 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100269
270 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
271 tlv += sizeof(rate_tlv->header);
272 for (i = 0; i < MAX_RATES; i++) {
273 *tlv = lbs_bg_rates[i];
274 if (*tlv == 0)
275 break;
276 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
277 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
278 Note that the values are MBit/s * 2, to mark them as
279 basic rates so that the firmware likes it better */
280 if (*tlv == 0x02 || *tlv == 0x04 ||
281 *tlv == 0x0b || *tlv == 0x16)
282 *tlv |= 0x80;
283 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400284 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100285 rate_tlv->header.len = cpu_to_le16(i);
286 return sizeof(rate_tlv->header) + i;
287}
Dan Williamseb8f7332007-05-25 16:25:21 -0400288
Holger Schurigffd074f2007-12-07 16:52:10 +0100289/*
290 * Generate the CMD_802_11_SCAN command with the proper tlv
291 * for a bunch of channels.
292 */
David Woodhousefa62f992008-03-03 12:18:03 +0100293static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100294 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100295{
296 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100297 struct cmd_ds_802_11_scan *scan_cmd;
298 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100299
David Woodhousefa62f992008-03-03 12:18:03 +0100300 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200301 bsstype, chan_list ? chan_list[0].channumber : -1,
302 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100303
304 /* create the fixed part for scan command */
305 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
306 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200307 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100308
Holger Schurigffd074f2007-12-07 16:52:10 +0100309 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100310 /* TODO: do we need to scan for a specific BSSID?
311 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100312 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400313
Holger Schurigffd074f2007-12-07 16:52:10 +0100314 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100315 if (priv->scan_ssid_len)
316 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100317 if (chan_list && chan_count)
318 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
319 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400320
Holger Schurigffd074f2007-12-07 16:52:10 +0100321 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100322 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
323 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
324 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100325 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100326 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400327
David Woodhousefa62f992008-03-03 12:18:03 +0100328 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
329 le16_to_cpu(scan_cmd->hdr.size),
330 lbs_ret_80211_scan, 0);
331
Holger Schurige56188a2007-10-09 14:15:19 +0200332out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100333 kfree(scan_cmd);
334 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
335 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400336}
337
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338/**
339 * @brief Internal function used to start a scan based on an input config
340 *
341 * Use the input user scan configuration information when provided in
342 * order to send the appropriate scan commands to firmware to populate or
343 * update the internal driver scan table
344 *
Holger Schurig69f90322007-11-23 15:43:44 +0100345 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100346 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 *
348 * @return 0 or < 0 if error
349 */
Holger Schurig245bf202008-04-02 16:27:42 +0200350int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351{
Holger Schurigffd074f2007-12-07 16:52:10 +0100352 int ret = -ENOMEM;
353 struct chanscanparamset *chan_list;
354 struct chanscanparamset *curr_chans;
355 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100356 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100357 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100358 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400359#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100360 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400361 int i = 0;
362#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363
David Woodhousef137e052008-03-03 12:18:59 +0100364 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400365
366 /* Cancel any partial outstanding partial scans if this scan
367 * is a full scan.
368 */
369 if (full_scan && delayed_work_pending(&priv->scan_work))
370 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200371
Holger Schurig52933d82008-03-05 07:05:32 +0100372 /* User-specified bsstype or channel list
373 TODO: this can be implemented if some user-space application
374 need the feature. Formerly, it was accessible from debugfs,
375 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100376 if (user_cfg) {
377 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100378 bsstype = user_cfg->bsstype;
379 } */
380
381 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200382
Holger Schurigffd074f2007-12-07 16:52:10 +0100383 /* Create list of channels to scan */
384 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100385 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100386 if (!chan_list) {
387 lbs_pr_alert("SCAN: chan_list empty\n");
388 goto out;
389 }
390
391 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100392 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100393
394 netif_stop_queue(priv->dev);
395 netif_carrier_off(priv->dev);
396 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500397 netif_stop_queue(priv->mesh_dev);
398 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100399 }
400
401 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100402 lbs_deb_scan("chan_count %d, scan_channel %d\n",
403 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100404 curr_chans = chan_list;
405 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100406 if (priv->scan_channel > 0) {
407 curr_chans += priv->scan_channel;
408 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100409 }
410
411 /* Send scan command(s)
412 * numchannels contains the number of channels we should maximally scan
413 * chan_count is the total number of channels to scan
414 */
415
416 while (chan_count) {
417 int to_scan = min(numchannels, chan_count);
418 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100419 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100420 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100421 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100422 if (ret) {
423 lbs_pr_err("SCAN_CMD failed\n");
424 goto out2;
425 }
426 curr_chans += to_scan;
427 chan_count -= to_scan;
428
429 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100430 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000431 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100432 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100433 if (priv->scan_channel < 0)
434 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100435 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100436 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100437 cancel_delayed_work(&priv->scan_work);
438 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100439 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100440 /* skip over GIWSCAN event */
441 goto out;
442 }
443
444 }
445 memset(&wrqu, 0, sizeof(union iwreq_data));
446 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447
Dan Williamsf8f55102007-05-30 10:12:55 -0400448#ifdef CONFIG_LIBERTAS_DEBUG
449 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000450 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100451 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000452 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700453 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
454 i++, iter->bssid, iter->rssi,
David Woodhousef137e052008-03-03 12:18:59 +0100455 escape_essid(iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000456 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400457#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200458
Holger Schurigffd074f2007-12-07 16:52:10 +0100459out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100460 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100461
462out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000463 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400464 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500465 if (!priv->tx_pending_len)
466 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500467 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000468 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500469 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500470 if (!priv->tx_pending_len)
471 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100473 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474
Holger Schurig9012b282007-05-25 11:27:16 -0400475 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 return ret;
477}
478
Holger Schurig52933d82008-03-05 07:05:32 +0100479void lbs_scan_worker(struct work_struct *work)
480{
481 struct lbs_private *priv =
482 container_of(work, struct lbs_private, scan_work.work);
483
484 lbs_deb_enter(LBS_DEB_SCAN);
485 lbs_scan_networks(priv, 0);
486 lbs_deb_leave(LBS_DEB_SCAN);
487}
488
489
Holger Schurigffd074f2007-12-07 16:52:10 +0100490/*********************************************************************/
491/* */
492/* Result interpretation */
493/* */
494/*********************************************************************/
495
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200496/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 * @brief Interpret a BSS scan response returned from the firmware
498 *
499 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100500 * response or beacon from the scan command. Record information as needed
501 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400503 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 *
505 * @return 0 or -1
506 */
Holger Schurig10078322007-11-15 18:05:47 -0500507static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100508 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 struct ieeetypes_fhparamset *pFH;
511 struct ieeetypes_dsparamset *pDS;
512 struct ieeetypes_cfparamset *pCF;
513 struct ieeetypes_ibssparamset *pibss;
Dan Williams8c512762007-08-02 11:40:45 -0400514 struct ieeetypes_countryinfoset *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100515 uint8_t *pos, *end, *p;
516 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
517 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400518 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519
Holger Schurige56188a2007-10-09 14:15:19 +0200520 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522 if (*bytesleft >= sizeof(beaconsize)) {
523 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700524 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 *bytesleft -= sizeof(beaconsize);
526 *pbeaconinfo += sizeof(beaconsize);
527 }
528
529 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 *pbeaconinfo += *bytesleft;
531 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200532 ret = -1;
533 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 }
535
536 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400537 pos = *pbeaconinfo;
538 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539
540 /* Advance the return beacon pointer past the current beacon */
541 *pbeaconinfo += beaconsize;
542 *bytesleft -= beaconsize;
543
Dan Williamsab617972007-08-02 10:48:02 -0400544 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700545 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400546 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547
Dan Williamsab617972007-08-02 10:48:02 -0400548 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400549 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200550 ret = -1;
551 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552 }
553
554 /*
555 * next 4 fields are RSSI, time stamp, beacon interval,
556 * and capability information
557 */
558
559 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400560 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100561 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400562 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200563
564 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400565 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566
567 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300568 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400569 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570
571 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300572 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100573 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400574 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575
Dan Williams0c9ca692007-08-02 10:43:44 -0400576 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100577 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400578 if (bss->capability & WLAN_CAPABILITY_IBSS)
579 bss->mode = IW_MODE_ADHOC;
580 else
581 bss->mode = IW_MODE_INFRA;
582
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100584 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400585 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200587 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400588 while (pos <= end - 2) {
David Woodhousef137e052008-03-03 12:18:59 +0100589 struct ieee80211_info_element * elem = (void *)pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590
Dan Williamsab617972007-08-02 10:48:02 -0400591 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400592 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100593 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400594 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595 }
596
Dan Williamsab617972007-08-02 10:48:02 -0400597 switch (elem->id) {
598 case MFIE_TYPE_SSID:
599 bss->ssid_len = elem->len;
600 memcpy(bss->ssid, elem->data, elem->len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100601 lbs_deb_scan("got SSID IE: '%s', len %u\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400602 escape_essid(bss->ssid, bss->ssid_len),
603 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604 break;
605
Dan Williamsab617972007-08-02 10:48:02 -0400606 case MFIE_TYPE_RATES:
David Woodhousef137e052008-03-03 12:18:59 +0100607 n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
Dan Williams8c512762007-08-02 11:40:45 -0400608 memcpy(bss->rates, elem->data, n_basic_rates);
609 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100610 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611 break;
612
Dan Williamsab617972007-08-02 10:48:02 -0400613 case MFIE_TYPE_FH_SET:
614 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400615 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616 sizeof(struct ieeetypes_fhparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100617 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 break;
619
Dan Williamsab617972007-08-02 10:48:02 -0400620 case MFIE_TYPE_DS_SET:
621 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400622 bss->channel = pDS->currentchan;
623 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624 sizeof(struct ieeetypes_dsparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100625 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 break;
627
Dan Williamsab617972007-08-02 10:48:02 -0400628 case MFIE_TYPE_CF_SET:
629 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400630 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631 sizeof(struct ieeetypes_cfparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100632 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633 break;
634
Dan Williamsab617972007-08-02 10:48:02 -0400635 case MFIE_TYPE_IBSS_SET:
636 pibss = (struct ieeetypes_ibssparamset *) pos;
David Woodhousee7240ac2007-12-11 17:54:36 -0500637 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400638 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639 sizeof(struct ieeetypes_ibssparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100640 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641 break;
642
Dan Williamsab617972007-08-02 10:48:02 -0400643 case MFIE_TYPE_COUNTRY:
644 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100645 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400646 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647 || pcountryinfo->len > 254) {
David Woodhousef137e052008-03-03 12:18:59 +0100648 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
649 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400650 ret = -1;
651 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 }
653
David Woodhousef137e052008-03-03 12:18:59 +0100654 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400655 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100656 (uint8_t *) pcountryinfo,
657 (int) (pcountryinfo->len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658 break;
659
Dan Williamsab617972007-08-02 10:48:02 -0400660 case MFIE_TYPE_RATES_EX:
661 /* only process extended supported rate if data rate is
662 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 * extended supported rate IE
664 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100665 lbs_deb_scan("got RATESEX IE\n");
666 if (!got_basic_rates) {
667 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400668 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100669 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670
Dan Williams8c512762007-08-02 11:40:45 -0400671 n_ex_rates = elem->len;
672 if (n_basic_rates + n_ex_rates > MAX_RATES)
673 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400674
Dan Williams8c512762007-08-02 11:40:45 -0400675 p = bss->rates + n_basic_rates;
676 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400677 break;
678
679 case MFIE_TYPE_GENERIC:
680 if (elem->len >= 4 &&
David Woodhousef137e052008-03-03 12:18:59 +0100681 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
682 elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
683 bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
Dan Williamsab617972007-08-02 10:48:02 -0400684 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100685 lbs_deb_scan("got WPA IE\n");
David Woodhousef137e052008-03-03 12:18:59 +0100686 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400687 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
David Woodhousef137e052008-03-03 12:18:59 +0100688 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
689 elem->data[2] == 0x43 && elem->data[3] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100690 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400691 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100692 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100693 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Holger Schurigffd074f2007-12-07 16:52:10 +0100694 elem->data[0], elem->data[1],
695 elem->data[2], elem->data[3],
696 elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 }
698 break;
699
Dan Williamsab617972007-08-02 10:48:02 -0400700 case MFIE_TYPE_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100701 lbs_deb_scan("got RSN IE\n");
Dan Williamsab617972007-08-02 10:48:02 -0400702 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
703 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100704 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
David Woodhousef137e052008-03-03 12:18:59 +0100705 bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706 break;
707
Dan Williamsab617972007-08-02 10:48:02 -0400708 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100709 lbs_deb_scan("got IE 0x%04x, len %d\n",
David Woodhousef137e052008-03-03 12:18:59 +0100710 elem->id, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711 break;
712 }
713
Dan Williamsab617972007-08-02 10:48:02 -0400714 pos += elem->len + 2;
715 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400716
717 /* Timestamp */
718 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500719 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400720
Holger Schurig9012b282007-05-25 11:27:16 -0400721 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
Holger Schurig9012b282007-05-25 11:27:16 -0400723done:
724 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
725 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726}
727
728/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 * @brief Send a scan command for all available channels filtered on a spec
730 *
Holger Schurige56188a2007-10-09 14:15:19 +0200731 * Used in association code and from debugfs
732 *
Holger Schurig69f90322007-11-23 15:43:44 +0100733 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200734 * @param ssid A pointer to the SSID to scan for
735 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736 *
737 * @return 0-success, otherwise fail
738 */
David Woodhousef137e052008-03-03 12:18:59 +0100739int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100740 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741{
Dan Williamseb8f7332007-05-25 16:25:21 -0400742 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
Holger Schurig52933d82008-03-05 07:05:32 +0100744 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
745 escape_essid(ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746
Dan Williamsd8efea22007-05-28 23:54:55 -0400747 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400748 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749
Holger Schurig52933d82008-03-05 07:05:32 +0100750 memcpy(priv->scan_ssid, ssid, ssid_len);
751 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752
Holger Schurig52933d82008-03-05 07:05:32 +0100753 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000754 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200755 ret = -1;
756 goto out;
757 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758
Dan Williamseb8f7332007-05-25 16:25:21 -0400759out:
Holger Schurige56188a2007-10-09 14:15:19 +0200760 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400761 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762}
763
Holger Schurige56188a2007-10-09 14:15:19 +0200764
765
766
767/*********************************************************************/
768/* */
769/* Support for Wireless Extensions */
770/* */
771/*********************************************************************/
772
Holger Schurigffd074f2007-12-07 16:52:10 +0100773
Dan Williams00af0152007-08-02 13:14:56 -0400774#define MAX_CUSTOM_LEN 64
775
Holger Schurig69f90322007-11-23 15:43:44 +0100776static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700777 struct iw_request_info *info,
778 char *start, char *stop,
779 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400780{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400781 struct chan_freq_power *cfp;
782 char *current_val; /* For rates */
783 struct iw_event iwe; /* Temporary buffer */
784 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100785#define PERFECT_RSSI ((uint8_t)50)
786#define WORST_RSSI ((uint8_t)0)
787#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
788 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400789
Holger Schurige56188a2007-10-09 14:15:19 +0200790 lbs_deb_enter(LBS_DEB_SCAN);
791
David Woodhouseaa21c002007-12-08 20:04:36 +0000792 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400793 if (!cfp) {
794 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200795 start = NULL;
796 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400797 }
798
Holger Schurigffd074f2007-12-07 16:52:10 +0100799 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400800 iwe.cmd = SIOCGIWAP;
801 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
802 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700803 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400804
805 /* SSID */
806 iwe.cmd = SIOCGIWESSID;
807 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +0100808 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
David S. Millerccc58052008-06-16 18:50:49 -0700809 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400810
811 /* Mode */
812 iwe.cmd = SIOCGIWMODE;
813 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700814 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400815
816 /* Frequency */
817 iwe.cmd = SIOCGIWFREQ;
818 iwe.u.freq.m = (long)cfp->freq * 100000;
819 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700820 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400821
822 /* Add quality statistics */
823 iwe.cmd = IWEVQUAL;
824 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
825 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
826
827 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
828 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100829 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
830 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
831 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400832 if (iwe.u.qual.qual > 100)
833 iwe.u.qual.qual = 100;
834
David Woodhouseaa21c002007-12-08 20:04:36 +0000835 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400836 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
837 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100838 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400839 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400840
Dan Williams80e78ef2007-05-25 22:18:47 -0400841 /* Locally created ad-hoc BSSs won't have beacons if this is the
842 * only station in the adhoc network; so get signal strength
843 * from receive statistics.
844 */
David Woodhousef137e052008-03-03 12:18:59 +0100845 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000846 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100847 priv->curbssparams.ssid_len,
848 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400849 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000850 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
851 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -0400852 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400853 }
David S. Millerccc58052008-06-16 18:50:49 -0700854 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400855
856 /* Add encryption capability */
857 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400858 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400859 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
860 } else {
861 iwe.u.data.flags = IW_ENCODE_DISABLED;
862 }
863 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -0700864 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400865
David S. Millerccc58052008-06-16 18:50:49 -0700866 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400867
868 iwe.cmd = SIOCGIWRATE;
869 iwe.u.bitrate.fixed = 0;
870 iwe.u.bitrate.disabled = 0;
871 iwe.u.bitrate.value = 0;
872
Dan Williams8c512762007-08-02 11:40:45 -0400873 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
874 /* Bit rate given in 500 kb/s units */
875 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700876 current_val = iwe_stream_add_value(info, start, current_val,
877 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400878 }
David Woodhousef137e052008-03-03 12:18:59 +0100879 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000880 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100881 priv->curbssparams.ssid_len,
882 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400883 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700884 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +0100885 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400886 }
887 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -0700888 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400889 start = current_val;
890
891 memset(&iwe, 0, sizeof(iwe));
892 if (bss->wpa_ie_len) {
893 char buf[MAX_WPA_IE_LEN];
894 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
895 iwe.cmd = IWEVGENIE;
896 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700897 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400898 }
899
900 memset(&iwe, 0, sizeof(iwe));
901 if (bss->rsn_ie_len) {
902 char buf[MAX_WPA_IE_LEN];
903 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
904 iwe.cmd = IWEVGENIE;
905 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700906 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400907 }
908
Dan Williams00af0152007-08-02 13:14:56 -0400909 if (bss->mesh) {
910 char custom[MAX_CUSTOM_LEN];
911 char *p = custom;
912
913 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +0100914 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -0400915 iwe.u.data.length = p - custom;
916 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -0700917 start = iwe_stream_add_point(info, start, stop,
918 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -0400919 }
920
Holger Schurige56188a2007-10-09 14:15:19 +0200921out:
922 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400923 return start;
924}
925
Holger Schurigffd074f2007-12-07 16:52:10 +0100926
927/**
928 * @brief Handle Scan Network ioctl
929 *
930 * @param dev A pointer to net_device structure
931 * @param info A pointer to iw_request_info structure
932 * @param vwrq A pointer to iw_param structure
933 * @param extra A pointer to extra data buf
934 *
935 * @return 0 --success, otherwise fail
936 */
937int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +0100938 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +0100939{
940 struct lbs_private *priv = dev->priv;
Holger Schurig52933d82008-03-05 07:05:32 +0100941 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100942
Holger Schurig52933d82008-03-05 07:05:32 +0100943 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +0100944
Dan Williamsd5db2df2008-08-21 17:51:07 -0400945 if (!priv->radio_on) {
946 ret = -EINVAL;
947 goto out;
948 }
949
Holger Schurig52933d82008-03-05 07:05:32 +0100950 if (!netif_running(dev)) {
951 ret = -ENETDOWN;
952 goto out;
953 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100954
955 /* mac80211 does this:
956 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +0100957 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
958 ret = -EOPNOTSUPP;
959 goto out;
960 }
961 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100962
963 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
964 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +0100965 struct iw_scan_req *req = (struct iw_scan_req *)extra;
966 priv->scan_ssid_len = req->essid_len;
967 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
968 lbs_deb_wext("set_scan, essid '%s'\n",
969 escape_essid(priv->scan_ssid, priv->scan_ssid_len));
970 } else {
971 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100972 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100973
974 if (!delayed_work_pending(&priv->scan_work))
975 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100976 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +0100977 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +0100978 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100979
David Woodhouseaa21c002007-12-08 20:04:36 +0000980 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +0100981 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +0100982
Holger Schurig52933d82008-03-05 07:05:32 +0100983out:
984 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
985 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +0100986}
987
988
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989/**
Holger Schurige56188a2007-10-09 14:15:19 +0200990 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200991 *
992 * @param dev A pointer to net_device structure
993 * @param info A pointer to iw_request_info structure
994 * @param dwrq A pointer to iw_point structure
995 * @param extra A pointer to extra data buf
996 *
997 * @return 0 --success, otherwise fail
998 */
Holger Schurig10078322007-11-15 18:05:47 -0500999int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001000 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001002#define SCAN_ITEM_SIZE 128
Holger Schurig69f90322007-11-23 15:43:44 +01001003 struct lbs_private *priv = dev->priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001004 int err = 0;
1005 char *ev = extra;
1006 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001007 struct bss_descriptor *iter_bss;
1008 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009
Holger Schurig52933d82008-03-05 07:05:32 +01001010 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001011
Holger Schurigffd074f2007-12-07 16:52:10 +01001012 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001013 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001014 return -EAGAIN;
1015
Dan Williams80e78ef2007-05-25 22:18:47 -04001016 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhousef137e052008-03-03 12:18:59 +01001017 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
Holger Schurig10078322007-11-15 18:05:47 -05001018 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
David Woodhousef137e052008-03-03 12:18:59 +01001019 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001020
David Woodhouseaa21c002007-12-08 20:04:36 +00001021 mutex_lock(&priv->lock);
1022 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001023 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001024 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001025
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001026 if (stop - ev < SCAN_ITEM_SIZE) {
1027 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028 break;
1029 }
1030
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001031 /* For mesh device, list only mesh networks */
1032 if (dev == priv->mesh_dev && !iter_bss->mesh)
1033 continue;
1034
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001035 /* Prune old an old scan result */
1036 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1037 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001038 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001039 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040 continue;
1041 }
1042
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001043 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001044 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001045 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001046 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001047 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001048 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001049 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001051 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001052 dwrq->flags = 0;
1053
Holger Schurig52933d82008-03-05 07:05:32 +01001054 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001055 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056}
1057
Holger Schurige56188a2007-10-09 14:15:19 +02001058
1059
1060
1061/*********************************************************************/
1062/* */
1063/* Command execution */
1064/* */
1065/*********************************************************************/
1066
1067
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001068/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069 * @brief This function handles the command response of scan
1070 *
Holger Schurige56188a2007-10-09 14:15:19 +02001071 * Called from handle_cmd_response() in cmdrespc.
1072 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073 * The response buffer for the scan command has the following
1074 * memory layout:
1075 *
1076 * .-----------------------------------------------------------.
1077 * | header (4 * sizeof(u16)): Standard command response hdr |
1078 * .-----------------------------------------------------------.
1079 * | bufsize (u16) : sizeof the BSS Description data |
1080 * .-----------------------------------------------------------.
1081 * | NumOfSet (u8) : Number of BSS Descs returned |
1082 * .-----------------------------------------------------------.
1083 * | BSSDescription data (variable, size given in bufsize) |
1084 * .-----------------------------------------------------------.
1085 * | TLV data (variable, size calculated using header->size, |
1086 * | bufsize and sizeof the fixed fields above) |
1087 * .-----------------------------------------------------------.
1088 *
Holger Schurig69f90322007-11-23 15:43:44 +01001089 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090 * @param resp A pointer to cmd_ds_command
1091 *
1092 * @return 0 or -1
1093 */
David Woodhousefa62f992008-03-03 12:18:03 +01001094static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1095 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001096{
David Woodhousefa62f992008-03-03 12:18:03 +01001097 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001098 struct bss_descriptor *iter_bss;
1099 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001100 uint8_t *bssinfo;
1101 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 int idx;
1104 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001105 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106
Holger Schurige56188a2007-10-09 14:15:19 +02001107 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001109 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001110 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001111 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1112 if (time_before(jiffies, stale_time))
1113 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001114 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001115 clear_bss_descriptor(iter_bss);
1116 }
1117
David Woodhousefa62f992008-03-03 12:18:03 +01001118 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1119 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1120 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001121 ret = -1;
1122 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123 }
1124
David Woodhousefa62f992008-03-03 12:18:03 +01001125 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001126 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001127
David Woodhousee7240ac2007-12-11 17:54:36 -05001128 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001129 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001130
David Woodhousefa62f992008-03-03 12:18:03 +01001131 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132
1133 /* The size of the TLV buffer is equal to the entire command response
1134 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1135 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1136 * response header (S_DS_GEN)
1137 */
David Woodhousefa62f992008-03-03 12:18:03 +01001138 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1139 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001140 + S_DS_GEN);
1141
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001142 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001143 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 * the information in the newbssentry and then insert into the
1145 * driver scan table either as an update to an existing entry
1146 * or as an addition at the end of the table
1147 */
David Woodhousefa62f992008-03-03 12:18:03 +01001148 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001149 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001150 struct bss_descriptor *found = NULL;
1151 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001152
1153 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001154 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001155 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001156 /* error parsing the scan response, skipped */
1157 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1158 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001159 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001160
1161 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001162 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001163 if (is_same_network(iter_bss, &new)) {
1164 found = iter_bss;
1165 break;
1166 }
1167
1168 if ((oldest == NULL) ||
1169 (iter_bss->last_scanned < oldest->last_scanned))
1170 oldest = iter_bss;
1171 }
1172
1173 if (found) {
1174 /* found, clear it */
1175 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001176 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001177 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001178 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001179 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001180 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001181 } else if (oldest) {
1182 /* If there are no more slots, expire the oldest */
1183 found = oldest;
1184 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001185 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001186 } else {
1187 continue;
1188 }
1189
Johannes Berge1749612008-10-27 15:59:26 -07001190 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001191
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001192 /* Copy the locally created newbssentry to the scan table */
1193 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001194 }
1195
Holger Schurig9012b282007-05-25 11:27:16 -04001196 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001197
Holger Schurig9012b282007-05-25 11:27:16 -04001198done:
1199 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1200 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201}