blob: 5c34ac588189147234afc9e81087756a30d488a9 [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>
Dan Williamsfcdb53d2007-05-25 16:15:56 -04008#include <linux/etherdevice.h>
Vladimir Davydovac630c22007-09-06 21:45:36 -04009#include <asm/unaligned.h>
10
John W. Linville7e272fc2008-09-24 18:13:14 -040011#include <net/lib80211.h>
12
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "host.h"
14#include "decl.h"
15#include "dev.h"
16#include "scan.h"
David Woodhousefa62f992008-03-03 12:18:03 +010017#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018
19//! Approximate amount of data needed to pass a scan result back to iwlist
20#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
21 + IW_ESSID_MAX_SIZE \
22 + IW_EV_UINT_LEN \
23 + IW_EV_FREQ_LEN \
24 + IW_EV_QUAL_LEN \
25 + IW_ESSID_MAX_SIZE \
26 + IW_EV_PARAM_LEN \
27 + 40) /* 40 for WPAIE */
28
29//! Memory needed to store a max sized channel List TLV for a firmware scan
30#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
31 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
32 * sizeof(struct chanscanparamset)))
33
34//! Memory needed to store a max number/size SSID TLV for a firmware scan
35#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
36
David Woodhousefa62f992008-03-03 12:18:03 +010037//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
38#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
39 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020040
41//! The maximum number of channels the firmware can scan per command
42#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
43
44/**
45 * @brief Number of channels to scan per firmware scan command issuance.
46 *
47 * Number restricted to prevent hitting the limit on the amount of scan data
48 * returned in a single firmware scan command.
49 */
50#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
51
52//! Scan time specified in the channel TLV for each channel for passive scans
53#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
54
55//! Scan time specified in the channel TLV for each channel for active scans
56#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
57
David Woodhousefa62f992008-03-03 12:18:03 +010058static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
59 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020060
61/*********************************************************************/
62/* */
63/* Misc helper functions */
64/* */
65/*********************************************************************/
66
Holger Schurig23ff5032008-01-28 17:27:03 +010067/**
68 * @brief Unsets the MSB on basic rates
69 *
70 * Scan through an array and unset the MSB for basic data rates.
71 *
72 * @param rates buffer of data rates
73 * @param len size of buffer
74 */
75static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
76{
77 int i;
78
79 for (i = 0; i < len; i++)
80 rates[i] &= 0x7f;
81}
82
83
David Woodhousef137e052008-03-03 12:18:59 +010084static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040085{
86 /* Don't blow away ->list, just BSS data */
87 memset(bss, 0, offsetof(struct bss_descriptor, list));
88}
89
Holger Schurigffd074f2007-12-07 16:52:10 +010090/**
91 * @brief Compare two SSIDs
92 *
93 * @param ssid1 A pointer to ssid to compare
94 * @param ssid2 A pointer to ssid to compare
95 *
96 * @return 0: ssid is same, otherwise is different
97 */
David Woodhousef137e052008-03-03 12:18:59 +010098int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
99 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100100{
101 if (ssid1_len != ssid2_len)
102 return -1;
103
104 return memcmp(ssid1, ssid2, ssid1_len);
105}
106
Holger Schurigffd074f2007-12-07 16:52:10 +0100107static inline int is_same_network(struct bss_descriptor *src,
108 struct bss_descriptor *dst)
109{
110 /* A network is only a duplicate if the channel, BSSID, and ESSID
111 * all match. We treat all <hidden> with the same BSSID and channel
112 * as one network */
113 return ((src->ssid_len == dst->ssid_len) &&
114 (src->channel == dst->channel) &&
115 !compare_ether_addr(src->bssid, dst->bssid) &&
116 !memcmp(src->ssid, dst->ssid, src->ssid_len));
117}
118
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200119
Holger Schurige56188a2007-10-09 14:15:19 +0200120
121
Holger Schurige56188a2007-10-09 14:15:19 +0200122/*********************************************************************/
123/* */
124/* Main scanning support */
125/* */
126/*********************************************************************/
127
Holger Schurige56188a2007-10-09 14:15:19 +0200128/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129 * @brief Create a channel list for the driver to scan based on region info
130 *
Holger Schurig10078322007-11-15 18:05:47 -0500131 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200132 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200133 * Use the driver region/band information to construct a comprehensive list
134 * of channels to scan. This routine is used for any scan that is not
135 * provided a specific channel list to scan.
136 *
Holger Schurig69f90322007-11-23 15:43:44 +0100137 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200138 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200139 *
140 * @return void
141 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100142static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100143 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200144{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200145 struct region_channel *scanregion;
146 struct chan_freq_power *cfp;
147 int rgnidx;
148 int chanidx;
149 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100150 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200151
152 chanidx = 0;
153
154 /* Set the default scan type to the user specified type, will later
155 * be changed to passive on a per channel basis if restricted by
156 * regulatory requirements (11d or 11h)
157 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400158 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159
David Woodhouseaa21c002007-12-08 20:04:36 +0000160 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
David Woodhousef137e052008-03-03 12:18:59 +0100161 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
162 && (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200163 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000164 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000166 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
168 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000169 memset(&priv->parsed_region_chan, 0x00,
170 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200171 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000172 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200173 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000174 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175 }
176
David Woodhousef137e052008-03-03 12:18:59 +0100177 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
178 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179
180 cfp = scanregion->CFP + nextchan;
181
David Woodhousef137e052008-03-03 12:18:59 +0100182 if (priv->enable11d)
183 scantype = lbs_get_scan_type_11d(cfp->channel,
184 &priv->parsed_region_chan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185
David Woodhousef137e052008-03-03 12:18:59 +0100186 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
187 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188
Dan Williams0aef64d2007-08-02 11:31:18 -0400189 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100190 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
191 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200192 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100193 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
194 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 }
196
David Woodhousef137e052008-03-03 12:18:59 +0100197 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200198 }
199 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100200 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200201}
202
Holger Schurige56188a2007-10-09 14:15:19 +0200203/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100204 * Add SSID TLV of the form:
205 *
206 * TLV-ID SSID 00 00
207 * length 06 00
208 * ssid 4d 4e 54 45 53 54
209 */
Holger Schurig52933d82008-03-05 07:05:32 +0100210static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400211{
David Woodhousef137e052008-03-03 12:18:59 +0100212 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
213
Holger Schurigffd074f2007-12-07 16:52:10 +0100214 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100215 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
216 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
217 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100218}
Dan Williamseb8f7332007-05-25 16:25:21 -0400219
Holger Schurigffd074f2007-12-07 16:52:10 +0100220/*
221 * Add CHANLIST TLV of the form
222 *
223 * TLV-ID CHANLIST 01 01
224 * length 5b 00
225 * channel 1 00 01 00 00 00 64 00
226 * radio type 00
227 * channel 01
228 * scan type 00
229 * min scan time 00 00
230 * max scan time 64 00
231 * channel 2 00 02 00 00 00 64 00
232 * channel 3 00 03 00 00 00 64 00
233 * channel 4 00 04 00 00 00 64 00
234 * channel 5 00 05 00 00 00 64 00
235 * channel 6 00 06 00 00 00 64 00
236 * channel 7 00 07 00 00 00 64 00
237 * channel 8 00 08 00 00 00 64 00
238 * channel 9 00 09 00 00 00 64 00
239 * channel 10 00 0a 00 00 00 64 00
240 * channel 11 00 0b 00 00 00 64 00
241 * channel 12 00 0c 00 00 00 64 00
242 * channel 13 00 0d 00 00 00 64 00
243 *
244 */
David Woodhousef137e052008-03-03 12:18:59 +0100245static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
246 struct chanscanparamset *chan_list,
247 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100248{
David Woodhousef137e052008-03-03 12:18:59 +0100249 size_t size = sizeof(struct chanscanparamset) *chan_count;
250 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400251
Holger Schurigffd074f2007-12-07 16:52:10 +0100252 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
253 memcpy(chan_tlv->chanscanparam, chan_list, size);
254 chan_tlv->header.len = cpu_to_le16(size);
255 return sizeof(chan_tlv->header) + size;
256}
Dan Williamseb8f7332007-05-25 16:25:21 -0400257
Holger Schurigffd074f2007-12-07 16:52:10 +0100258/*
259 * Add RATES TLV of the form
260 *
261 * TLV-ID RATES 01 00
262 * length 0e 00
263 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
264 *
265 * The rates are in lbs_bg_rates[], but for the 802.11b
266 * rates the high bit isn't set.
267 */
David Woodhousef137e052008-03-03 12:18:59 +0100268static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100269{
270 int i;
David Woodhousef137e052008-03-03 12:18:59 +0100271 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100272
273 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
274 tlv += sizeof(rate_tlv->header);
275 for (i = 0; i < MAX_RATES; i++) {
276 *tlv = lbs_bg_rates[i];
277 if (*tlv == 0)
278 break;
279 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
280 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
281 Note that the values are MBit/s * 2, to mark them as
282 basic rates so that the firmware likes it better */
283 if (*tlv == 0x02 || *tlv == 0x04 ||
284 *tlv == 0x0b || *tlv == 0x16)
285 *tlv |= 0x80;
286 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400287 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100288 rate_tlv->header.len = cpu_to_le16(i);
289 return sizeof(rate_tlv->header) + i;
290}
Dan Williamseb8f7332007-05-25 16:25:21 -0400291
Holger Schurigffd074f2007-12-07 16:52:10 +0100292/*
293 * Generate the CMD_802_11_SCAN command with the proper tlv
294 * for a bunch of channels.
295 */
David Woodhousefa62f992008-03-03 12:18:03 +0100296static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100297 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100298{
299 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100300 struct cmd_ds_802_11_scan *scan_cmd;
301 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100302
David Woodhousefa62f992008-03-03 12:18:03 +0100303 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200304 bsstype, chan_list ? chan_list[0].channumber : -1,
305 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100306
307 /* create the fixed part for scan command */
308 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
309 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200310 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100311
Holger Schurigffd074f2007-12-07 16:52:10 +0100312 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100313 /* TODO: do we need to scan for a specific BSSID?
314 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100315 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400316
Holger Schurigffd074f2007-12-07 16:52:10 +0100317 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100318 if (priv->scan_ssid_len)
319 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100320 if (chan_list && chan_count)
321 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
322 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400323
Holger Schurigffd074f2007-12-07 16:52:10 +0100324 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100325 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
326 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
327 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100328 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100329 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400330
David Woodhousefa62f992008-03-03 12:18:03 +0100331 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
332 le16_to_cpu(scan_cmd->hdr.size),
333 lbs_ret_80211_scan, 0);
334
Holger Schurige56188a2007-10-09 14:15:19 +0200335out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100336 kfree(scan_cmd);
337 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
338 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400339}
340
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341/**
342 * @brief Internal function used to start a scan based on an input config
343 *
344 * Use the input user scan configuration information when provided in
345 * order to send the appropriate scan commands to firmware to populate or
346 * update the internal driver scan table
347 *
Holger Schurig69f90322007-11-23 15:43:44 +0100348 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100349 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350 *
351 * @return 0 or < 0 if error
352 */
Holger Schurig245bf202008-04-02 16:27:42 +0200353int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354{
Holger Schurigffd074f2007-12-07 16:52:10 +0100355 int ret = -ENOMEM;
356 struct chanscanparamset *chan_list;
357 struct chanscanparamset *curr_chans;
358 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100359 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100360 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100361 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400362#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100363 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400364 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400365 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400366#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367
David Woodhousef137e052008-03-03 12:18:59 +0100368 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400369
370 /* Cancel any partial outstanding partial scans if this scan
371 * is a full scan.
372 */
373 if (full_scan && delayed_work_pending(&priv->scan_work))
374 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200375
Holger Schurig52933d82008-03-05 07:05:32 +0100376 /* User-specified bsstype or channel list
377 TODO: this can be implemented if some user-space application
378 need the feature. Formerly, it was accessible from debugfs,
379 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100380 if (user_cfg) {
381 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100382 bsstype = user_cfg->bsstype;
383 } */
384
385 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200386
Holger Schurigffd074f2007-12-07 16:52:10 +0100387 /* Create list of channels to scan */
388 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100389 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100390 if (!chan_list) {
391 lbs_pr_alert("SCAN: chan_list empty\n");
392 goto out;
393 }
394
395 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100396 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100397
398 netif_stop_queue(priv->dev);
399 netif_carrier_off(priv->dev);
400 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500401 netif_stop_queue(priv->mesh_dev);
402 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100403 }
404
405 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100406 lbs_deb_scan("chan_count %d, scan_channel %d\n",
407 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100408 curr_chans = chan_list;
409 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100410 if (priv->scan_channel > 0) {
411 curr_chans += priv->scan_channel;
412 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100413 }
414
415 /* Send scan command(s)
416 * numchannels contains the number of channels we should maximally scan
417 * chan_count is the total number of channels to scan
418 */
419
420 while (chan_count) {
421 int to_scan = min(numchannels, chan_count);
422 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100423 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100424 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100425 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100426 if (ret) {
427 lbs_pr_err("SCAN_CMD failed\n");
428 goto out2;
429 }
430 curr_chans += to_scan;
431 chan_count -= to_scan;
432
433 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100434 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000435 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100436 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100437 if (priv->scan_channel < 0)
438 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100439 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100440 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100441 cancel_delayed_work(&priv->scan_work);
442 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100443 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100444 /* skip over GIWSCAN event */
445 goto out;
446 }
447
448 }
449 memset(&wrqu, 0, sizeof(union iwreq_data));
450 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451
Dan Williamsf8f55102007-05-30 10:12:55 -0400452#ifdef CONFIG_LIBERTAS_DEBUG
453 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000454 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100455 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000456 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700457 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
458 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400459 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000460 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400461#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462
Holger Schurigffd074f2007-12-07 16:52:10 +0100463out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100464 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100465
466out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000467 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400468 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500469 if (!priv->tx_pending_len)
470 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500471 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000472 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500473 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500474 if (!priv->tx_pending_len)
475 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100477 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478
Holger Schurig9012b282007-05-25 11:27:16 -0400479 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480 return ret;
481}
482
Holger Schurig52933d82008-03-05 07:05:32 +0100483void lbs_scan_worker(struct work_struct *work)
484{
485 struct lbs_private *priv =
486 container_of(work, struct lbs_private, scan_work.work);
487
488 lbs_deb_enter(LBS_DEB_SCAN);
489 lbs_scan_networks(priv, 0);
490 lbs_deb_leave(LBS_DEB_SCAN);
491}
492
493
Holger Schurigffd074f2007-12-07 16:52:10 +0100494/*********************************************************************/
495/* */
496/* Result interpretation */
497/* */
498/*********************************************************************/
499
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200501 * @brief Interpret a BSS scan response returned from the firmware
502 *
503 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100504 * response or beacon from the scan command. Record information as needed
505 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400507 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508 *
509 * @return 0 or -1
510 */
Holger Schurig10078322007-11-15 18:05:47 -0500511static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100512 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 struct ieeetypes_fhparamset *pFH;
515 struct ieeetypes_dsparamset *pDS;
516 struct ieeetypes_cfparamset *pCF;
517 struct ieeetypes_ibssparamset *pibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400518 DECLARE_SSID_BUF(ssid);
Dan Williams8c512762007-08-02 11:40:45 -0400519 struct ieeetypes_countryinfoset *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100520 uint8_t *pos, *end, *p;
521 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
522 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400523 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524
Holger Schurige56188a2007-10-09 14:15:19 +0200525 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527 if (*bytesleft >= sizeof(beaconsize)) {
528 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700529 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 *bytesleft -= sizeof(beaconsize);
531 *pbeaconinfo += sizeof(beaconsize);
532 }
533
534 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200535 *pbeaconinfo += *bytesleft;
536 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200537 ret = -1;
538 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539 }
540
541 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400542 pos = *pbeaconinfo;
543 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544
545 /* Advance the return beacon pointer past the current beacon */
546 *pbeaconinfo += beaconsize;
547 *bytesleft -= beaconsize;
548
Dan Williamsab617972007-08-02 10:48:02 -0400549 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700550 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400551 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552
Dan Williamsab617972007-08-02 10:48:02 -0400553 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400554 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200555 ret = -1;
556 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557 }
558
559 /*
560 * next 4 fields are RSSI, time stamp, beacon interval,
561 * and capability information
562 */
563
564 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400565 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100566 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400567 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200568
569 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400570 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571
572 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300573 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400574 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575
576 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300577 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100578 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400579 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580
Dan Williams0c9ca692007-08-02 10:43:44 -0400581 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100582 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400583 if (bss->capability & WLAN_CAPABILITY_IBSS)
584 bss->mode = IW_MODE_ADHOC;
585 else
586 bss->mode = IW_MODE_INFRA;
587
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100589 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400590 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400593 while (pos <= end - 2) {
David Woodhousef137e052008-03-03 12:18:59 +0100594 struct ieee80211_info_element * elem = (void *)pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595
Dan Williamsab617972007-08-02 10:48:02 -0400596 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400597 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100598 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400599 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200600 }
601
Dan Williamsab617972007-08-02 10:48:02 -0400602 switch (elem->id) {
603 case MFIE_TYPE_SSID:
Johannes Berg48735d82008-10-29 11:43:32 +0100604 bss->ssid_len = min_t(int, 32, elem->len);
605 memcpy(bss->ssid, elem->data, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100606 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400607 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400608 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 break;
610
Dan Williamsab617972007-08-02 10:48:02 -0400611 case MFIE_TYPE_RATES:
David Woodhousef137e052008-03-03 12:18:59 +0100612 n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
Dan Williams8c512762007-08-02 11:40:45 -0400613 memcpy(bss->rates, elem->data, n_basic_rates);
614 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100615 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616 break;
617
Dan Williamsab617972007-08-02 10:48:02 -0400618 case MFIE_TYPE_FH_SET:
619 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400620 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621 sizeof(struct ieeetypes_fhparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100622 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 break;
624
Dan Williamsab617972007-08-02 10:48:02 -0400625 case MFIE_TYPE_DS_SET:
626 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400627 bss->channel = pDS->currentchan;
628 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629 sizeof(struct ieeetypes_dsparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100630 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631 break;
632
Dan Williamsab617972007-08-02 10:48:02 -0400633 case MFIE_TYPE_CF_SET:
634 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400635 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200636 sizeof(struct ieeetypes_cfparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100637 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638 break;
639
Dan Williamsab617972007-08-02 10:48:02 -0400640 case MFIE_TYPE_IBSS_SET:
641 pibss = (struct ieeetypes_ibssparamset *) pos;
David Woodhousee7240ac2007-12-11 17:54:36 -0500642 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400643 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644 sizeof(struct ieeetypes_ibssparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100645 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200646 break;
647
Dan Williamsab617972007-08-02 10:48:02 -0400648 case MFIE_TYPE_COUNTRY:
649 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100650 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400651 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 || pcountryinfo->len > 254) {
David Woodhousef137e052008-03-03 12:18:59 +0100653 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
654 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400655 ret = -1;
656 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 }
658
David Woodhousef137e052008-03-03 12:18:59 +0100659 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400660 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100661 (uint8_t *) pcountryinfo,
662 (int) (pcountryinfo->len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 break;
664
Dan Williamsab617972007-08-02 10:48:02 -0400665 case MFIE_TYPE_RATES_EX:
666 /* only process extended supported rate if data rate is
667 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668 * extended supported rate IE
669 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100670 lbs_deb_scan("got RATESEX IE\n");
671 if (!got_basic_rates) {
672 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400673 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100674 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675
Dan Williams8c512762007-08-02 11:40:45 -0400676 n_ex_rates = elem->len;
677 if (n_basic_rates + n_ex_rates > MAX_RATES)
678 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400679
Dan Williams8c512762007-08-02 11:40:45 -0400680 p = bss->rates + n_basic_rates;
681 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400682 break;
683
684 case MFIE_TYPE_GENERIC:
685 if (elem->len >= 4 &&
David Woodhousef137e052008-03-03 12:18:59 +0100686 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
687 elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
688 bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
Dan Williamsab617972007-08-02 10:48:02 -0400689 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100690 lbs_deb_scan("got WPA IE\n");
David Woodhousef137e052008-03-03 12:18:59 +0100691 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400692 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
David Woodhousef137e052008-03-03 12:18:59 +0100693 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
694 elem->data[2] == 0x43 && elem->data[3] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100695 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400696 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100697 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100698 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Holger Schurigffd074f2007-12-07 16:52:10 +0100699 elem->data[0], elem->data[1],
700 elem->data[2], elem->data[3],
701 elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 }
703 break;
704
Dan Williamsab617972007-08-02 10:48:02 -0400705 case MFIE_TYPE_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100706 lbs_deb_scan("got RSN IE\n");
Dan Williamsab617972007-08-02 10:48:02 -0400707 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
708 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100709 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
David Woodhousef137e052008-03-03 12:18:59 +0100710 bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711 break;
712
Dan Williamsab617972007-08-02 10:48:02 -0400713 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100714 lbs_deb_scan("got IE 0x%04x, len %d\n",
David Woodhousef137e052008-03-03 12:18:59 +0100715 elem->id, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716 break;
717 }
718
Dan Williamsab617972007-08-02 10:48:02 -0400719 pos += elem->len + 2;
720 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400721
722 /* Timestamp */
723 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500724 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400725
Holger Schurig9012b282007-05-25 11:27:16 -0400726 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200727
Holger Schurig9012b282007-05-25 11:27:16 -0400728done:
729 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
730 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731}
732
733/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734 * @brief Send a scan command for all available channels filtered on a spec
735 *
Holger Schurige56188a2007-10-09 14:15:19 +0200736 * Used in association code and from debugfs
737 *
Holger Schurig69f90322007-11-23 15:43:44 +0100738 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200739 * @param ssid A pointer to the SSID to scan for
740 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741 *
742 * @return 0-success, otherwise fail
743 */
David Woodhousef137e052008-03-03 12:18:59 +0100744int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100745 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746{
John W. Linville9387b7c2008-09-30 20:59:05 -0400747 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400748 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749
Holger Schurig52933d82008-03-05 07:05:32 +0100750 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400751 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752
Dan Williamsd8efea22007-05-28 23:54:55 -0400753 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400754 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755
Holger Schurig52933d82008-03-05 07:05:32 +0100756 memcpy(priv->scan_ssid, ssid, ssid_len);
757 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758
Holger Schurig52933d82008-03-05 07:05:32 +0100759 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000760 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200761 ret = -1;
762 goto out;
763 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764
Dan Williamseb8f7332007-05-25 16:25:21 -0400765out:
Holger Schurige56188a2007-10-09 14:15:19 +0200766 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400767 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768}
769
Holger Schurige56188a2007-10-09 14:15:19 +0200770
771
772
773/*********************************************************************/
774/* */
775/* Support for Wireless Extensions */
776/* */
777/*********************************************************************/
778
Holger Schurigffd074f2007-12-07 16:52:10 +0100779
Dan Williams00af0152007-08-02 13:14:56 -0400780#define MAX_CUSTOM_LEN 64
781
Holger Schurig69f90322007-11-23 15:43:44 +0100782static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700783 struct iw_request_info *info,
784 char *start, char *stop,
785 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400786{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400787 struct chan_freq_power *cfp;
788 char *current_val; /* For rates */
789 struct iw_event iwe; /* Temporary buffer */
790 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100791#define PERFECT_RSSI ((uint8_t)50)
792#define WORST_RSSI ((uint8_t)0)
793#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
794 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400795
Holger Schurige56188a2007-10-09 14:15:19 +0200796 lbs_deb_enter(LBS_DEB_SCAN);
797
David Woodhouseaa21c002007-12-08 20:04:36 +0000798 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400799 if (!cfp) {
800 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200801 start = NULL;
802 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400803 }
804
Holger Schurigffd074f2007-12-07 16:52:10 +0100805 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400806 iwe.cmd = SIOCGIWAP;
807 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
808 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700809 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400810
811 /* SSID */
812 iwe.cmd = SIOCGIWESSID;
813 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +0100814 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
David S. Millerccc58052008-06-16 18:50:49 -0700815 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400816
817 /* Mode */
818 iwe.cmd = SIOCGIWMODE;
819 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700820 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400821
822 /* Frequency */
823 iwe.cmd = SIOCGIWFREQ;
824 iwe.u.freq.m = (long)cfp->freq * 100000;
825 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700826 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400827
828 /* Add quality statistics */
829 iwe.cmd = IWEVQUAL;
830 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
831 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
832
833 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
834 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100835 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
836 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
837 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400838 if (iwe.u.qual.qual > 100)
839 iwe.u.qual.qual = 100;
840
David Woodhouseaa21c002007-12-08 20:04:36 +0000841 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400842 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
843 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100844 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400845 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400846
Dan Williams80e78ef2007-05-25 22:18:47 -0400847 /* Locally created ad-hoc BSSs won't have beacons if this is the
848 * only station in the adhoc network; so get signal strength
849 * from receive statistics.
850 */
David Woodhousef137e052008-03-03 12:18:59 +0100851 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000852 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100853 priv->curbssparams.ssid_len,
854 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400855 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000856 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
857 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -0400858 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400859 }
David S. Millerccc58052008-06-16 18:50:49 -0700860 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400861
862 /* Add encryption capability */
863 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400864 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400865 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
866 } else {
867 iwe.u.data.flags = IW_ENCODE_DISABLED;
868 }
869 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -0700870 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400871
David S. Millerccc58052008-06-16 18:50:49 -0700872 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400873
874 iwe.cmd = SIOCGIWRATE;
875 iwe.u.bitrate.fixed = 0;
876 iwe.u.bitrate.disabled = 0;
877 iwe.u.bitrate.value = 0;
878
Dan Williams8c512762007-08-02 11:40:45 -0400879 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
880 /* Bit rate given in 500 kb/s units */
881 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700882 current_val = iwe_stream_add_value(info, start, current_val,
883 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400884 }
David Woodhousef137e052008-03-03 12:18:59 +0100885 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000886 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100887 priv->curbssparams.ssid_len,
888 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400889 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700890 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +0100891 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400892 }
893 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -0700894 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400895 start = current_val;
896
897 memset(&iwe, 0, sizeof(iwe));
898 if (bss->wpa_ie_len) {
899 char buf[MAX_WPA_IE_LEN];
900 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
901 iwe.cmd = IWEVGENIE;
902 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700903 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400904 }
905
906 memset(&iwe, 0, sizeof(iwe));
907 if (bss->rsn_ie_len) {
908 char buf[MAX_WPA_IE_LEN];
909 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
910 iwe.cmd = IWEVGENIE;
911 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700912 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400913 }
914
Dan Williams00af0152007-08-02 13:14:56 -0400915 if (bss->mesh) {
916 char custom[MAX_CUSTOM_LEN];
917 char *p = custom;
918
919 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +0100920 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -0400921 iwe.u.data.length = p - custom;
922 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -0700923 start = iwe_stream_add_point(info, start, stop,
924 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -0400925 }
926
Holger Schurige56188a2007-10-09 14:15:19 +0200927out:
928 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400929 return start;
930}
931
Holger Schurigffd074f2007-12-07 16:52:10 +0100932
933/**
934 * @brief Handle Scan Network ioctl
935 *
936 * @param dev A pointer to net_device structure
937 * @param info A pointer to iw_request_info structure
938 * @param vwrq A pointer to iw_param structure
939 * @param extra A pointer to extra data buf
940 *
941 * @return 0 --success, otherwise fail
942 */
943int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +0100944 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +0100945{
John W. Linville9387b7c2008-09-30 20:59:05 -0400946 DECLARE_SSID_BUF(ssid);
Holger Schurigffd074f2007-12-07 16:52:10 +0100947 struct lbs_private *priv = dev->priv;
Holger Schurig52933d82008-03-05 07:05:32 +0100948 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100949
Holger Schurig52933d82008-03-05 07:05:32 +0100950 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +0100951
Dan Williamsd5db2df2008-08-21 17:51:07 -0400952 if (!priv->radio_on) {
953 ret = -EINVAL;
954 goto out;
955 }
956
Holger Schurig52933d82008-03-05 07:05:32 +0100957 if (!netif_running(dev)) {
958 ret = -ENETDOWN;
959 goto out;
960 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100961
962 /* mac80211 does this:
963 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +0100964 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
965 ret = -EOPNOTSUPP;
966 goto out;
967 }
968 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100969
970 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
971 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +0100972 struct iw_scan_req *req = (struct iw_scan_req *)extra;
973 priv->scan_ssid_len = req->essid_len;
974 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
975 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400976 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +0100977 } else {
978 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100979 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100980
981 if (!delayed_work_pending(&priv->scan_work))
982 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100983 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +0100984 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +0100985 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100986
David Woodhouseaa21c002007-12-08 20:04:36 +0000987 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +0100988 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +0100989
Holger Schurig52933d82008-03-05 07:05:32 +0100990out:
991 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
992 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +0100993}
994
995
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996/**
Holger Schurige56188a2007-10-09 14:15:19 +0200997 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998 *
999 * @param dev A pointer to net_device structure
1000 * @param info A pointer to iw_request_info structure
1001 * @param dwrq A pointer to iw_point structure
1002 * @param extra A pointer to extra data buf
1003 *
1004 * @return 0 --success, otherwise fail
1005 */
Holger Schurig10078322007-11-15 18:05:47 -05001006int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001007 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001009#define SCAN_ITEM_SIZE 128
Holger Schurig69f90322007-11-23 15:43:44 +01001010 struct lbs_private *priv = dev->priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001011 int err = 0;
1012 char *ev = extra;
1013 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001014 struct bss_descriptor *iter_bss;
1015 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001016
Holger Schurig52933d82008-03-05 07:05:32 +01001017 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018
Holger Schurigffd074f2007-12-07 16:52:10 +01001019 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001020 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001021 return -EAGAIN;
1022
Dan Williams80e78ef2007-05-25 22:18:47 -04001023 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhousef137e052008-03-03 12:18:59 +01001024 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
Holger Schurig10078322007-11-15 18:05:47 -05001025 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
David Woodhousef137e052008-03-03 12:18:59 +01001026 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001027
David Woodhouseaa21c002007-12-08 20:04:36 +00001028 mutex_lock(&priv->lock);
1029 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001030 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001031 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001032
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001033 if (stop - ev < SCAN_ITEM_SIZE) {
1034 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035 break;
1036 }
1037
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001038 /* For mesh device, list only mesh networks */
1039 if (dev == priv->mesh_dev && !iter_bss->mesh)
1040 continue;
1041
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001042 /* Prune old an old scan result */
1043 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1044 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001045 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001046 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 continue;
1048 }
1049
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001050 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001051 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001052 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001054 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001056 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001058 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 dwrq->flags = 0;
1060
Holger Schurig52933d82008-03-05 07:05:32 +01001061 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001062 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001063}
1064
Holger Schurige56188a2007-10-09 14:15:19 +02001065
1066
1067
1068/*********************************************************************/
1069/* */
1070/* Command execution */
1071/* */
1072/*********************************************************************/
1073
1074
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001075/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 * @brief This function handles the command response of scan
1077 *
Holger Schurige56188a2007-10-09 14:15:19 +02001078 * Called from handle_cmd_response() in cmdrespc.
1079 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080 * The response buffer for the scan command has the following
1081 * memory layout:
1082 *
1083 * .-----------------------------------------------------------.
1084 * | header (4 * sizeof(u16)): Standard command response hdr |
1085 * .-----------------------------------------------------------.
1086 * | bufsize (u16) : sizeof the BSS Description data |
1087 * .-----------------------------------------------------------.
1088 * | NumOfSet (u8) : Number of BSS Descs returned |
1089 * .-----------------------------------------------------------.
1090 * | BSSDescription data (variable, size given in bufsize) |
1091 * .-----------------------------------------------------------.
1092 * | TLV data (variable, size calculated using header->size, |
1093 * | bufsize and sizeof the fixed fields above) |
1094 * .-----------------------------------------------------------.
1095 *
Holger Schurig69f90322007-11-23 15:43:44 +01001096 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001097 * @param resp A pointer to cmd_ds_command
1098 *
1099 * @return 0 or -1
1100 */
David Woodhousefa62f992008-03-03 12:18:03 +01001101static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1102 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103{
David Woodhousefa62f992008-03-03 12:18:03 +01001104 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001105 struct bss_descriptor *iter_bss;
1106 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001107 uint8_t *bssinfo;
1108 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110 int idx;
1111 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001112 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113
Holger Schurige56188a2007-10-09 14:15:19 +02001114 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001116 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001117 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001118 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1119 if (time_before(jiffies, stale_time))
1120 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001121 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001122 clear_bss_descriptor(iter_bss);
1123 }
1124
David Woodhousefa62f992008-03-03 12:18:03 +01001125 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1126 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1127 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001128 ret = -1;
1129 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001130 }
1131
David Woodhousefa62f992008-03-03 12:18:03 +01001132 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001133 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001134
David Woodhousee7240ac2007-12-11 17:54:36 -05001135 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001136 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001137
David Woodhousefa62f992008-03-03 12:18:03 +01001138 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139
1140 /* The size of the TLV buffer is equal to the entire command response
1141 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1142 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1143 * response header (S_DS_GEN)
1144 */
David Woodhousefa62f992008-03-03 12:18:03 +01001145 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1146 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001147 + S_DS_GEN);
1148
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001150 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151 * the information in the newbssentry and then insert into the
1152 * driver scan table either as an update to an existing entry
1153 * or as an addition at the end of the table
1154 */
David Woodhousefa62f992008-03-03 12:18:03 +01001155 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001156 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001157 struct bss_descriptor *found = NULL;
1158 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001159
1160 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001161 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001162 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001163 /* error parsing the scan response, skipped */
1164 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1165 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001166 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001167
1168 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001169 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001170 if (is_same_network(iter_bss, &new)) {
1171 found = iter_bss;
1172 break;
1173 }
1174
1175 if ((oldest == NULL) ||
1176 (iter_bss->last_scanned < oldest->last_scanned))
1177 oldest = iter_bss;
1178 }
1179
1180 if (found) {
1181 /* found, clear it */
1182 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001183 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001184 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001185 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001186 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001187 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001188 } else if (oldest) {
1189 /* If there are no more slots, expire the oldest */
1190 found = oldest;
1191 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001192 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001193 } else {
1194 continue;
1195 }
1196
Johannes Berge1749612008-10-27 15:59:26 -07001197 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001198
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001199 /* Copy the locally created newbssentry to the scan table */
1200 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201 }
1202
Holger Schurig9012b282007-05-25 11:27:16 -04001203 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001204
Holger Schurig9012b282007-05-25 11:27:16 -04001205done:
1206 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1207 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001208}