blob: 2700f4b455e39db53752cc63ff54f6797b1c4569 [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 */
John W. Linville7e272fc2008-09-24 18:13:14 -04007#include <linux/types.h>
Roel Kluin430453f2009-07-28 09:59:47 +02008#include <linux/kernel.h>
Dan Williamsfcdb53d2007-05-25 16:15:56 -04009#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010010#include <linux/if_arp.h>
Vladimir Davydovac630c22007-09-06 21:45:36 -040011#include <asm/unaligned.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040012#include <net/lib80211.h>
13
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014#include "host.h"
15#include "decl.h"
16#include "dev.h"
17#include "scan.h"
David Woodhousefa62f992008-03-03 12:18:03 +010018#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
20//! Approximate amount of data needed to pass a scan result back to iwlist
21#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
Holger Schurig243e84e2009-10-22 15:30:47 +020022 + IEEE80211_MAX_SSID_LEN \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023 + IW_EV_UINT_LEN \
24 + IW_EV_FREQ_LEN \
25 + IW_EV_QUAL_LEN \
Holger Schurig243e84e2009-10-22 15:30:47 +020026 + IEEE80211_MAX_SSID_LEN \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027 + IW_EV_PARAM_LEN \
28 + 40) /* 40 for WPAIE */
29
30//! Memory needed to store a max sized channel List TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040031#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvl_ie_header) \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020032 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
33 * sizeof(struct chanscanparamset)))
34
35//! Memory needed to store a max number/size SSID TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040036#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvl_ie_ssid_param_set))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020037
David Woodhousefa62f992008-03-03 12:18:03 +010038//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
39#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
40 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020041
42//! The maximum number of channels the firmware can scan per command
43#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
44
45/**
46 * @brief Number of channels to scan per firmware scan command issuance.
47 *
48 * Number restricted to prevent hitting the limit on the amount of scan data
49 * returned in a single firmware scan command.
50 */
51#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
52
53//! Scan time specified in the channel TLV for each channel for passive scans
54#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
55
56//! Scan time specified in the channel TLV for each channel for active scans
57#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
58
Johannes Berg2c7060022008-10-30 22:09:54 +010059#define DEFAULT_MAX_SCAN_AGE (15 * HZ)
60
David Woodhousefa62f992008-03-03 12:18:03 +010061static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
62 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020063
64/*********************************************************************/
65/* */
66/* Misc helper functions */
67/* */
68/*********************************************************************/
69
Holger Schurig23ff5032008-01-28 17:27:03 +010070/**
71 * @brief Unsets the MSB on basic rates
72 *
73 * Scan through an array and unset the MSB for basic data rates.
74 *
75 * @param rates buffer of data rates
76 * @param len size of buffer
77 */
78static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
79{
80 int i;
81
82 for (i = 0; i < len; i++)
83 rates[i] &= 0x7f;
84}
85
86
David Woodhousef137e052008-03-03 12:18:59 +010087static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040088{
89 /* Don't blow away ->list, just BSS data */
90 memset(bss, 0, offsetof(struct bss_descriptor, list));
91}
92
Holger Schurigffd074f2007-12-07 16:52:10 +010093/**
94 * @brief Compare two SSIDs
95 *
96 * @param ssid1 A pointer to ssid to compare
97 * @param ssid2 A pointer to ssid to compare
98 *
99 * @return 0: ssid is same, otherwise is different
100 */
David Woodhousef137e052008-03-03 12:18:59 +0100101int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
102 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100103{
104 if (ssid1_len != ssid2_len)
105 return -1;
106
107 return memcmp(ssid1, ssid2, ssid1_len);
108}
109
Holger Schurigffd074f2007-12-07 16:52:10 +0100110static inline int is_same_network(struct bss_descriptor *src,
111 struct bss_descriptor *dst)
112{
113 /* A network is only a duplicate if the channel, BSSID, and ESSID
114 * all match. We treat all <hidden> with the same BSSID and channel
115 * as one network */
116 return ((src->ssid_len == dst->ssid_len) &&
117 (src->channel == dst->channel) &&
118 !compare_ether_addr(src->bssid, dst->bssid) &&
119 !memcmp(src->ssid, dst->ssid, src->ssid_len));
120}
121
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200122
Holger Schurige56188a2007-10-09 14:15:19 +0200123
124
Holger Schurige56188a2007-10-09 14:15:19 +0200125/*********************************************************************/
126/* */
127/* Main scanning support */
128/* */
129/*********************************************************************/
130
Holger Schurige56188a2007-10-09 14:15:19 +0200131/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 * @brief Create a channel list for the driver to scan based on region info
133 *
Holger Schurig10078322007-11-15 18:05:47 -0500134 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200135 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 * Use the driver region/band information to construct a comprehensive list
137 * of channels to scan. This routine is used for any scan that is not
138 * provided a specific channel list to scan.
139 *
Holger Schurig69f90322007-11-23 15:43:44 +0100140 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 *
143 * @return void
144 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100145static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100146 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 struct region_channel *scanregion;
149 struct chan_freq_power *cfp;
150 int rgnidx;
151 int chanidx;
152 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100153 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154
155 chanidx = 0;
156
157 /* Set the default scan type to the user specified type, will later
158 * be changed to passive on a per channel basis if restricted by
159 * regulatory requirements (11d or 11h)
160 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400161 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162
David Woodhouseaa21c002007-12-08 20:04:36 +0000163 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
Holger Schurigd37b4fd2009-10-22 15:30:45 +0200164 if (!priv->region_channel[rgnidx].valid)
165 continue;
166 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
David Woodhousef137e052008-03-03 12:18:59 +0100168 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
169 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170
171 cfp = scanregion->CFP + nextchan;
172
David Woodhousef137e052008-03-03 12:18:59 +0100173 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
174 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175
Dan Williams0aef64d2007-08-02 11:31:18 -0400176 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100177 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
178 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100180 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
181 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 }
183
David Woodhousef137e052008-03-03 12:18:59 +0100184 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185 }
186 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100187 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188}
189
Holger Schurige56188a2007-10-09 14:15:19 +0200190/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100191 * Add SSID TLV of the form:
192 *
193 * TLV-ID SSID 00 00
194 * length 06 00
195 * ssid 4d 4e 54 45 53 54
196 */
Holger Schurig52933d82008-03-05 07:05:32 +0100197static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400198{
Dan Williams75b6a612009-05-22 20:03:09 -0400199 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
David Woodhousef137e052008-03-03 12:18:59 +0100200
Holger Schurigffd074f2007-12-07 16:52:10 +0100201 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100202 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
203 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
204 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100205}
Dan Williamseb8f7332007-05-25 16:25:21 -0400206
Holger Schurigffd074f2007-12-07 16:52:10 +0100207/*
208 * Add CHANLIST TLV of the form
209 *
210 * TLV-ID CHANLIST 01 01
211 * length 5b 00
212 * channel 1 00 01 00 00 00 64 00
213 * radio type 00
214 * channel 01
215 * scan type 00
216 * min scan time 00 00
217 * max scan time 64 00
218 * channel 2 00 02 00 00 00 64 00
219 * channel 3 00 03 00 00 00 64 00
220 * channel 4 00 04 00 00 00 64 00
221 * channel 5 00 05 00 00 00 64 00
222 * channel 6 00 06 00 00 00 64 00
223 * channel 7 00 07 00 00 00 64 00
224 * channel 8 00 08 00 00 00 64 00
225 * channel 9 00 09 00 00 00 64 00
226 * channel 10 00 0a 00 00 00 64 00
227 * channel 11 00 0b 00 00 00 64 00
228 * channel 12 00 0c 00 00 00 64 00
229 * channel 13 00 0d 00 00 00 64 00
230 *
231 */
David Woodhousef137e052008-03-03 12:18:59 +0100232static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
233 struct chanscanparamset *chan_list,
234 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100235{
David Woodhousef137e052008-03-03 12:18:59 +0100236 size_t size = sizeof(struct chanscanparamset) *chan_count;
Dan Williams75b6a612009-05-22 20:03:09 -0400237 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400238
Holger Schurigffd074f2007-12-07 16:52:10 +0100239 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
240 memcpy(chan_tlv->chanscanparam, chan_list, size);
241 chan_tlv->header.len = cpu_to_le16(size);
242 return sizeof(chan_tlv->header) + size;
243}
Dan Williamseb8f7332007-05-25 16:25:21 -0400244
Holger Schurigffd074f2007-12-07 16:52:10 +0100245/*
246 * Add RATES TLV of the form
247 *
248 * TLV-ID RATES 01 00
249 * length 0e 00
250 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
251 *
252 * The rates are in lbs_bg_rates[], but for the 802.11b
253 * rates the high bit isn't set.
254 */
David Woodhousef137e052008-03-03 12:18:59 +0100255static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100256{
257 int i;
Dan Williams75b6a612009-05-22 20:03:09 -0400258 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100259
260 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
261 tlv += sizeof(rate_tlv->header);
262 for (i = 0; i < MAX_RATES; i++) {
263 *tlv = lbs_bg_rates[i];
264 if (*tlv == 0)
265 break;
266 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
267 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
268 Note that the values are MBit/s * 2, to mark them as
269 basic rates so that the firmware likes it better */
270 if (*tlv == 0x02 || *tlv == 0x04 ||
271 *tlv == 0x0b || *tlv == 0x16)
272 *tlv |= 0x80;
273 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400274 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100275 rate_tlv->header.len = cpu_to_le16(i);
276 return sizeof(rate_tlv->header) + i;
277}
Dan Williamseb8f7332007-05-25 16:25:21 -0400278
Holger Schurigffd074f2007-12-07 16:52:10 +0100279/*
280 * Generate the CMD_802_11_SCAN command with the proper tlv
281 * for a bunch of channels.
282 */
David Woodhousefa62f992008-03-03 12:18:03 +0100283static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100284 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100285{
286 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100287 struct cmd_ds_802_11_scan *scan_cmd;
288 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100289
David Woodhousefa62f992008-03-03 12:18:03 +0100290 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200291 bsstype, chan_list ? chan_list[0].channumber : -1,
292 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100293
294 /* create the fixed part for scan command */
295 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
296 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200297 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100298
Holger Schurigffd074f2007-12-07 16:52:10 +0100299 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100300 /* TODO: do we need to scan for a specific BSSID?
301 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100302 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400303
Holger Schurigffd074f2007-12-07 16:52:10 +0100304 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100305 if (priv->scan_ssid_len)
306 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100307 if (chan_list && chan_count)
308 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
309 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400310
Holger Schurigffd074f2007-12-07 16:52:10 +0100311 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100312 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
313 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
314 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100315 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100316 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400317
David Woodhousefa62f992008-03-03 12:18:03 +0100318 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
319 le16_to_cpu(scan_cmd->hdr.size),
320 lbs_ret_80211_scan, 0);
321
Holger Schurige56188a2007-10-09 14:15:19 +0200322out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100323 kfree(scan_cmd);
324 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
325 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400326}
327
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328/**
329 * @brief Internal function used to start a scan based on an input config
330 *
331 * Use the input user scan configuration information when provided in
332 * order to send the appropriate scan commands to firmware to populate or
333 * update the internal driver scan table
334 *
Holger Schurig69f90322007-11-23 15:43:44 +0100335 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100336 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 *
338 * @return 0 or < 0 if error
339 */
Holger Schurig245bf202008-04-02 16:27:42 +0200340int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341{
Holger Schurigffd074f2007-12-07 16:52:10 +0100342 int ret = -ENOMEM;
343 struct chanscanparamset *chan_list;
344 struct chanscanparamset *curr_chans;
345 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100346 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100347 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100348 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400349#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100350 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400351 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400352 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400353#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354
David Woodhousef137e052008-03-03 12:18:59 +0100355 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400356
357 /* Cancel any partial outstanding partial scans if this scan
358 * is a full scan.
359 */
360 if (full_scan && delayed_work_pending(&priv->scan_work))
361 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
Holger Schurig52933d82008-03-05 07:05:32 +0100363 /* User-specified bsstype or channel list
364 TODO: this can be implemented if some user-space application
365 need the feature. Formerly, it was accessible from debugfs,
366 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100367 if (user_cfg) {
368 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100369 bsstype = user_cfg->bsstype;
370 } */
371
372 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373
Holger Schurigffd074f2007-12-07 16:52:10 +0100374 /* Create list of channels to scan */
375 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100376 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100377 if (!chan_list) {
378 lbs_pr_alert("SCAN: chan_list empty\n");
379 goto out;
380 }
381
382 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100383 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100384
385 netif_stop_queue(priv->dev);
386 netif_carrier_off(priv->dev);
387 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500388 netif_stop_queue(priv->mesh_dev);
389 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100390 }
391
392 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100393 lbs_deb_scan("chan_count %d, scan_channel %d\n",
394 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100395 curr_chans = chan_list;
396 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100397 if (priv->scan_channel > 0) {
398 curr_chans += priv->scan_channel;
399 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100400 }
401
402 /* Send scan command(s)
403 * numchannels contains the number of channels we should maximally scan
404 * chan_count is the total number of channels to scan
405 */
406
407 while (chan_count) {
408 int to_scan = min(numchannels, chan_count);
409 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100410 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100411 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100412 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100413 if (ret) {
414 lbs_pr_err("SCAN_CMD failed\n");
415 goto out2;
416 }
417 curr_chans += to_scan;
418 chan_count -= to_scan;
419
420 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100421 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000422 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100423 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100424 if (priv->scan_channel < 0)
425 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100426 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100427 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100428 cancel_delayed_work(&priv->scan_work);
429 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100430 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100431 /* skip over GIWSCAN event */
432 goto out;
433 }
434
435 }
436 memset(&wrqu, 0, sizeof(union iwreq_data));
437 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438
Dan Williamsf8f55102007-05-30 10:12:55 -0400439#ifdef CONFIG_LIBERTAS_DEBUG
440 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000441 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100442 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000443 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700444 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
445 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400446 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000447 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400448#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449
Holger Schurigffd074f2007-12-07 16:52:10 +0100450out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100451 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100452
453out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000454 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400455 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500456 if (!priv->tx_pending_len)
457 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500458 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000459 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500460 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500461 if (!priv->tx_pending_len)
462 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100464 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465
Holger Schurig9012b282007-05-25 11:27:16 -0400466 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467 return ret;
468}
469
Holger Schurig52933d82008-03-05 07:05:32 +0100470void lbs_scan_worker(struct work_struct *work)
471{
472 struct lbs_private *priv =
473 container_of(work, struct lbs_private, scan_work.work);
474
475 lbs_deb_enter(LBS_DEB_SCAN);
476 lbs_scan_networks(priv, 0);
477 lbs_deb_leave(LBS_DEB_SCAN);
478}
479
480
Holger Schurigffd074f2007-12-07 16:52:10 +0100481/*********************************************************************/
482/* */
483/* Result interpretation */
484/* */
485/*********************************************************************/
486
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488 * @brief Interpret a BSS scan response returned from the firmware
489 *
490 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100491 * response or beacon from the scan command. Record information as needed
492 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400494 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 *
496 * @return 0 or -1
497 */
Holger Schurig10078322007-11-15 18:05:47 -0500498static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100499 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500{
Dan Williams5fd164e2009-05-22 20:01:21 -0400501 struct ieee_ie_fh_param_set *fh;
502 struct ieee_ie_ds_param_set *ds;
503 struct ieee_ie_cf_param_set *cf;
504 struct ieee_ie_ibss_param_set *ibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400505 DECLARE_SSID_BUF(ssid);
David Woodhousef137e052008-03-03 12:18:59 +0100506 uint8_t *pos, *end, *p;
507 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
508 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400509 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510
Holger Schurige56188a2007-10-09 14:15:19 +0200511 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 if (*bytesleft >= sizeof(beaconsize)) {
514 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700515 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516 *bytesleft -= sizeof(beaconsize);
517 *pbeaconinfo += sizeof(beaconsize);
518 }
519
520 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 *pbeaconinfo += *bytesleft;
522 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200523 ret = -1;
524 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 }
526
527 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400528 pos = *pbeaconinfo;
529 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530
531 /* Advance the return beacon pointer past the current beacon */
532 *pbeaconinfo += beaconsize;
533 *bytesleft -= beaconsize;
534
Dan Williamsab617972007-08-02 10:48:02 -0400535 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700536 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400537 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538
Dan Williamsab617972007-08-02 10:48:02 -0400539 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400540 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200541 ret = -1;
542 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200543 }
544
545 /*
546 * next 4 fields are RSSI, time stamp, beacon interval,
547 * and capability information
548 */
549
550 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400551 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100552 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400553 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554
555 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400556 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557
558 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300559 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400560 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561
562 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300563 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100564 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400565 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566
Dan Williams0c9ca692007-08-02 10:43:44 -0400567 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100568 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400569 if (bss->capability & WLAN_CAPABILITY_IBSS)
570 bss->mode = IW_MODE_ADHOC;
571 else
572 bss->mode = IW_MODE_INFRA;
573
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100575 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400576 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400579 while (pos <= end - 2) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100580 if (pos + pos[1] > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400581 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100582 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400583 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200584 }
585
Johannes Berg2c7060022008-10-30 22:09:54 +0100586 switch (pos[0]) {
587 case WLAN_EID_SSID:
588 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
589 memcpy(bss->ssid, pos + 2, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100590 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400591 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400592 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593 break;
594
Johannes Berg2c7060022008-10-30 22:09:54 +0100595 case WLAN_EID_SUPP_RATES:
596 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
597 memcpy(bss->rates, pos + 2, n_basic_rates);
Dan Williams8c512762007-08-02 11:40:45 -0400598 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100599 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200600 break;
601
Johannes Berg2c7060022008-10-30 22:09:54 +0100602 case WLAN_EID_FH_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400603 fh = (struct ieee_ie_fh_param_set *) pos;
604 memcpy(&bss->phy.fh, fh, sizeof(*fh));
Holger Schurigffd074f2007-12-07 16:52:10 +0100605 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606 break;
607
Johannes Berg2c7060022008-10-30 22:09:54 +0100608 case WLAN_EID_DS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400609 ds = (struct ieee_ie_ds_param_set *) pos;
610 bss->channel = ds->channel;
611 memcpy(&bss->phy.ds, ds, sizeof(*ds));
Holger Schurigffd074f2007-12-07 16:52:10 +0100612 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613 break;
614
Johannes Berg2c7060022008-10-30 22:09:54 +0100615 case WLAN_EID_CF_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400616 cf = (struct ieee_ie_cf_param_set *) pos;
617 memcpy(&bss->ss.cf, cf, sizeof(*cf));
Holger Schurigffd074f2007-12-07 16:52:10 +0100618 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619 break;
620
Johannes Berg2c7060022008-10-30 22:09:54 +0100621 case WLAN_EID_IBSS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400622 ibss = (struct ieee_ie_ibss_param_set *) pos;
623 bss->atimwindow = ibss->atimwindow;
624 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
Holger Schurigffd074f2007-12-07 16:52:10 +0100625 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 break;
627
Johannes Berg2c7060022008-10-30 22:09:54 +0100628 case WLAN_EID_EXT_SUPP_RATES:
Dan Williamsab617972007-08-02 10:48:02 -0400629 /* only process extended supported rate if data rate is
630 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631 * extended supported rate IE
632 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100633 lbs_deb_scan("got RATESEX IE\n");
634 if (!got_basic_rates) {
635 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400636 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100637 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638
Johannes Berg2c7060022008-10-30 22:09:54 +0100639 n_ex_rates = pos[1];
Dan Williams8c512762007-08-02 11:40:45 -0400640 if (n_basic_rates + n_ex_rates > MAX_RATES)
641 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400642
Dan Williams8c512762007-08-02 11:40:45 -0400643 p = bss->rates + n_basic_rates;
Johannes Berg2c7060022008-10-30 22:09:54 +0100644 memcpy(p, pos + 2, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400645 break;
646
Johannes Berg2c7060022008-10-30 22:09:54 +0100647 case WLAN_EID_GENERIC:
648 if (pos[1] >= 4 &&
649 pos[2] == 0x00 && pos[3] == 0x50 &&
650 pos[4] == 0xf2 && pos[5] == 0x01) {
651 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
652 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100653 lbs_deb_scan("got WPA IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100654 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
655 bss->wpa_ie_len);
656 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
657 pos[2] == 0x00 && pos[3] == 0x50 &&
Roel Kluinbaf62ee2009-02-03 17:15:57 +0100658 pos[4] == 0x43 && pos[5] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100659 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400660 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100661 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100662 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100663 pos[2], pos[3],
664 pos[4], pos[5],
665 pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200666 }
667 break;
668
Johannes Berg2c7060022008-10-30 22:09:54 +0100669 case WLAN_EID_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100670 lbs_deb_scan("got RSN IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100671 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
672 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100673 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
Johannes Berg2c7060022008-10-30 22:09:54 +0100674 bss->rsn_ie, bss->rsn_ie_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675 break;
676
Dan Williamsab617972007-08-02 10:48:02 -0400677 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100678 lbs_deb_scan("got IE 0x%04x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100679 pos[0], pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 break;
681 }
682
Johannes Berg2c7060022008-10-30 22:09:54 +0100683 pos += pos[1] + 2;
Dan Williamsab617972007-08-02 10:48:02 -0400684 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400685
686 /* Timestamp */
687 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500688 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400689
Holger Schurig9012b282007-05-25 11:27:16 -0400690 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691
Holger Schurig9012b282007-05-25 11:27:16 -0400692done:
693 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
694 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695}
696
697/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698 * @brief Send a scan command for all available channels filtered on a spec
699 *
Holger Schurige56188a2007-10-09 14:15:19 +0200700 * Used in association code and from debugfs
701 *
Holger Schurig69f90322007-11-23 15:43:44 +0100702 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200703 * @param ssid A pointer to the SSID to scan for
704 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200705 *
706 * @return 0-success, otherwise fail
707 */
David Woodhousef137e052008-03-03 12:18:59 +0100708int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100709 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710{
John W. Linville9387b7c2008-09-30 20:59:05 -0400711 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400712 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713
Holger Schurig52933d82008-03-05 07:05:32 +0100714 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400715 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
Dan Williamsd8efea22007-05-28 23:54:55 -0400717 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400718 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719
Holger Schurig52933d82008-03-05 07:05:32 +0100720 memcpy(priv->scan_ssid, ssid, ssid_len);
721 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722
Holger Schurig52933d82008-03-05 07:05:32 +0100723 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000724 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200725 ret = -1;
726 goto out;
727 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728
Dan Williamseb8f7332007-05-25 16:25:21 -0400729out:
Holger Schurige56188a2007-10-09 14:15:19 +0200730 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400731 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732}
733
Holger Schurige56188a2007-10-09 14:15:19 +0200734
735
736
737/*********************************************************************/
738/* */
739/* Support for Wireless Extensions */
740/* */
741/*********************************************************************/
742
Holger Schurigffd074f2007-12-07 16:52:10 +0100743
Dan Williams00af0152007-08-02 13:14:56 -0400744#define MAX_CUSTOM_LEN 64
745
Holger Schurig69f90322007-11-23 15:43:44 +0100746static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700747 struct iw_request_info *info,
748 char *start, char *stop,
749 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400750{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400751 struct chan_freq_power *cfp;
752 char *current_val; /* For rates */
753 struct iw_event iwe; /* Temporary buffer */
754 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100755#define PERFECT_RSSI ((uint8_t)50)
756#define WORST_RSSI ((uint8_t)0)
757#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
758 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400759
Holger Schurige56188a2007-10-09 14:15:19 +0200760 lbs_deb_enter(LBS_DEB_SCAN);
761
David Woodhouseaa21c002007-12-08 20:04:36 +0000762 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400763 if (!cfp) {
764 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200765 start = NULL;
766 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400767 }
768
Holger Schurigffd074f2007-12-07 16:52:10 +0100769 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400770 iwe.cmd = SIOCGIWAP;
771 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
772 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700773 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400774
775 /* SSID */
776 iwe.cmd = SIOCGIWESSID;
777 iwe.u.data.flags = 1;
Holger Schurig243e84e2009-10-22 15:30:47 +0200778 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IEEE80211_MAX_SSID_LEN);
David S. Millerccc58052008-06-16 18:50:49 -0700779 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400780
781 /* Mode */
782 iwe.cmd = SIOCGIWMODE;
783 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700784 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400785
786 /* Frequency */
787 iwe.cmd = SIOCGIWFREQ;
788 iwe.u.freq.m = (long)cfp->freq * 100000;
789 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700790 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400791
792 /* Add quality statistics */
793 iwe.cmd = IWEVQUAL;
794 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
795 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
796
797 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
798 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100799 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
800 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
801 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400802 if (iwe.u.qual.qual > 100)
803 iwe.u.qual.qual = 100;
804
David Woodhouseaa21c002007-12-08 20:04:36 +0000805 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400806 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
807 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100808 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400809 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400810
Dan Williams80e78ef2007-05-25 22:18:47 -0400811 /* Locally created ad-hoc BSSs won't have beacons if this is the
812 * only station in the adhoc network; so get signal strength
813 * from receive statistics.
814 */
David Woodhousef137e052008-03-03 12:18:59 +0100815 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000816 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100817 priv->curbssparams.ssid_len,
818 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400819 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000820 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
821 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -0400822 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400823 }
David S. Millerccc58052008-06-16 18:50:49 -0700824 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400825
826 /* Add encryption capability */
827 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400828 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400829 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
830 } else {
831 iwe.u.data.flags = IW_ENCODE_DISABLED;
832 }
833 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -0700834 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400835
David S. Millerccc58052008-06-16 18:50:49 -0700836 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400837
838 iwe.cmd = SIOCGIWRATE;
839 iwe.u.bitrate.fixed = 0;
840 iwe.u.bitrate.disabled = 0;
841 iwe.u.bitrate.value = 0;
842
Roel Kluin430453f2009-07-28 09:59:47 +0200843 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
Dan Williams8c512762007-08-02 11:40:45 -0400844 /* Bit rate given in 500 kb/s units */
845 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700846 current_val = iwe_stream_add_value(info, start, current_val,
847 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400848 }
David Woodhousef137e052008-03-03 12:18:59 +0100849 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000850 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100851 priv->curbssparams.ssid_len,
852 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400853 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700854 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +0100855 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400856 }
857 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -0700858 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400859 start = current_val;
860
861 memset(&iwe, 0, sizeof(iwe));
862 if (bss->wpa_ie_len) {
863 char buf[MAX_WPA_IE_LEN];
864 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
865 iwe.cmd = IWEVGENIE;
866 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700867 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400868 }
869
870 memset(&iwe, 0, sizeof(iwe));
871 if (bss->rsn_ie_len) {
872 char buf[MAX_WPA_IE_LEN];
873 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
874 iwe.cmd = IWEVGENIE;
875 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700876 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400877 }
878
Dan Williams00af0152007-08-02 13:14:56 -0400879 if (bss->mesh) {
880 char custom[MAX_CUSTOM_LEN];
881 char *p = custom;
882
883 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +0100884 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -0400885 iwe.u.data.length = p - custom;
886 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -0700887 start = iwe_stream_add_point(info, start, stop,
888 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -0400889 }
890
Holger Schurige56188a2007-10-09 14:15:19 +0200891out:
892 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400893 return start;
894}
895
Holger Schurigffd074f2007-12-07 16:52:10 +0100896
897/**
898 * @brief Handle Scan Network ioctl
899 *
900 * @param dev A pointer to net_device structure
901 * @param info A pointer to iw_request_info structure
902 * @param vwrq A pointer to iw_param structure
903 * @param extra A pointer to extra data buf
904 *
905 * @return 0 --success, otherwise fail
906 */
907int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +0100908 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +0100909{
John W. Linville9387b7c2008-09-30 20:59:05 -0400910 DECLARE_SSID_BUF(ssid);
Kiran Divekarab65f642009-02-19 19:32:39 -0500911 struct lbs_private *priv = dev->ml_priv;
Holger Schurig52933d82008-03-05 07:05:32 +0100912 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100913
Holger Schurig52933d82008-03-05 07:05:32 +0100914 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +0100915
Dan Williamsd5db2df2008-08-21 17:51:07 -0400916 if (!priv->radio_on) {
917 ret = -EINVAL;
918 goto out;
919 }
920
Holger Schurig52933d82008-03-05 07:05:32 +0100921 if (!netif_running(dev)) {
922 ret = -ENETDOWN;
923 goto out;
924 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100925
926 /* mac80211 does this:
927 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +0100928 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
929 ret = -EOPNOTSUPP;
930 goto out;
931 }
932 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100933
934 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
935 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +0100936 struct iw_scan_req *req = (struct iw_scan_req *)extra;
937 priv->scan_ssid_len = req->essid_len;
938 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
939 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400940 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +0100941 } else {
942 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100943 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100944
945 if (!delayed_work_pending(&priv->scan_work))
946 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100947 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +0100948 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +0100949 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100950
David Woodhouseaa21c002007-12-08 20:04:36 +0000951 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +0100952 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +0100953
Holger Schurig52933d82008-03-05 07:05:32 +0100954out:
955 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
956 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +0100957}
958
959
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200960/**
Holger Schurige56188a2007-10-09 14:15:19 +0200961 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200962 *
963 * @param dev A pointer to net_device structure
964 * @param info A pointer to iw_request_info structure
965 * @param dwrq A pointer to iw_point structure
966 * @param extra A pointer to extra data buf
967 *
968 * @return 0 --success, otherwise fail
969 */
Holger Schurig10078322007-11-15 18:05:47 -0500970int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +0100971 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400973#define SCAN_ITEM_SIZE 128
Kiran Divekarab65f642009-02-19 19:32:39 -0500974 struct lbs_private *priv = dev->ml_priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400975 int err = 0;
976 char *ev = extra;
977 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +0100978 struct bss_descriptor *iter_bss;
979 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200980
Holger Schurig52933d82008-03-05 07:05:32 +0100981 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982
Holger Schurigffd074f2007-12-07 16:52:10 +0100983 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +0100984 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +0100985 return -EAGAIN;
986
Dan Williams80e78ef2007-05-25 22:18:47 -0400987 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -0700988 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
989 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
990 CMD_OPTION_WAITFORRSP, 0, NULL);
991 if (err)
992 goto out;
993 }
Dan Williams80e78ef2007-05-25 22:18:47 -0400994
David Woodhouseaa21c002007-12-08 20:04:36 +0000995 mutex_lock(&priv->lock);
996 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +0100997 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400998 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200999
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001000 if (stop - ev < SCAN_ITEM_SIZE) {
1001 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001002 break;
1003 }
1004
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001005 /* For mesh device, list only mesh networks */
1006 if (dev == priv->mesh_dev && !iter_bss->mesh)
1007 continue;
1008
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001009 /* Prune old an old scan result */
1010 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1011 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001012 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001013 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 continue;
1015 }
1016
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001017 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001018 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001019 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001021 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001023 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001025 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026 dwrq->flags = 0;
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001027out:
Holger Schurig52933d82008-03-05 07:05:32 +01001028 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001029 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001030}
1031
Holger Schurige56188a2007-10-09 14:15:19 +02001032
1033
1034
1035/*********************************************************************/
1036/* */
1037/* Command execution */
1038/* */
1039/*********************************************************************/
1040
1041
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043 * @brief This function handles the command response of scan
1044 *
Holger Schurige56188a2007-10-09 14:15:19 +02001045 * Called from handle_cmd_response() in cmdrespc.
1046 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 * The response buffer for the scan command has the following
1048 * memory layout:
1049 *
1050 * .-----------------------------------------------------------.
1051 * | header (4 * sizeof(u16)): Standard command response hdr |
1052 * .-----------------------------------------------------------.
1053 * | bufsize (u16) : sizeof the BSS Description data |
1054 * .-----------------------------------------------------------.
1055 * | NumOfSet (u8) : Number of BSS Descs returned |
1056 * .-----------------------------------------------------------.
1057 * | BSSDescription data (variable, size given in bufsize) |
1058 * .-----------------------------------------------------------.
1059 * | TLV data (variable, size calculated using header->size, |
1060 * | bufsize and sizeof the fixed fields above) |
1061 * .-----------------------------------------------------------.
1062 *
Holger Schurig69f90322007-11-23 15:43:44 +01001063 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064 * @param resp A pointer to cmd_ds_command
1065 *
1066 * @return 0 or -1
1067 */
David Woodhousefa62f992008-03-03 12:18:03 +01001068static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1069 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001070{
David Woodhousefa62f992008-03-03 12:18:03 +01001071 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001072 struct bss_descriptor *iter_bss;
1073 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001074 uint8_t *bssinfo;
1075 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001077 int idx;
1078 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001079 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080
Holger Schurige56188a2007-10-09 14:15:19 +02001081 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001082
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001083 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001084 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001085 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1086 if (time_before(jiffies, stale_time))
1087 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001088 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001089 clear_bss_descriptor(iter_bss);
1090 }
1091
David Woodhousefa62f992008-03-03 12:18:03 +01001092 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1093 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1094 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001095 ret = -1;
1096 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001097 }
1098
Cliff Cai2c5b9e512009-05-27 14:03:09 -04001099 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001100 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001101
David Woodhousee7240ac2007-12-11 17:54:36 -05001102 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001103 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001104
David Woodhousefa62f992008-03-03 12:18:03 +01001105 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106
1107 /* The size of the TLV buffer is equal to the entire command response
1108 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1109 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1110 * response header (S_DS_GEN)
1111 */
David Woodhousefa62f992008-03-03 12:18:03 +01001112 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1113 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 + S_DS_GEN);
1115
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001117 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118 * the information in the newbssentry and then insert into the
1119 * driver scan table either as an update to an existing entry
1120 * or as an addition at the end of the table
1121 */
David Woodhousefa62f992008-03-03 12:18:03 +01001122 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001123 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001124 struct bss_descriptor *found = NULL;
1125 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126
1127 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001128 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001129 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001130 /* error parsing the scan response, skipped */
1131 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1132 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001133 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001134
1135 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001136 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001137 if (is_same_network(iter_bss, &new)) {
1138 found = iter_bss;
1139 break;
1140 }
1141
1142 if ((oldest == NULL) ||
1143 (iter_bss->last_scanned < oldest->last_scanned))
1144 oldest = iter_bss;
1145 }
1146
1147 if (found) {
1148 /* found, clear it */
1149 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001150 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001151 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001152 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001153 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001154 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001155 } else if (oldest) {
1156 /* If there are no more slots, expire the oldest */
1157 found = oldest;
1158 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001159 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001160 } else {
1161 continue;
1162 }
1163
Johannes Berge1749612008-10-27 15:59:26 -07001164 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001165
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001166 /* Copy the locally created newbssentry to the scan table */
1167 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168 }
1169
Holger Schurig9012b282007-05-25 11:27:16 -04001170 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171
Holger Schurig9012b282007-05-25 11:27:16 -04001172done:
1173 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1174 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175}