blob: 73c5f54c9b420e9fe873cbda78c0aa195d0c75f2 [file] [log] [blame]
Holger Schurigff9fc792009-10-06 16:31:54 +02001/*
2 * Implement cfg80211 ("iw") support.
3 *
4 * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
5 * Holger Schurig <hs4233@mail.mn-solutions.de>
6 *
7 */
8
Joe Perches0e4e06a2011-05-02 16:49:14 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Andrew Morton241a6a52010-08-10 18:01:06 -070011#include <linux/sched.h>
12#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Kiran Divekar1047d5e2010-06-04 23:20:42 -070014#include <linux/ieee80211.h>
Holger Schurigff9fc792009-10-06 16:31:54 +020015#include <net/cfg80211.h>
Kiran Divekare86dc1c2010-06-14 22:01:26 +053016#include <asm/unaligned.h>
Holger Schurigff9fc792009-10-06 16:31:54 +020017
Kiran Divekare86dc1c2010-06-14 22:01:26 +053018#include "decl.h"
Holger Schurigff9fc792009-10-06 16:31:54 +020019#include "cfg.h"
20#include "cmd.h"
21
22
23#define CHAN2G(_channel, _freq, _flags) { \
24 .band = IEEE80211_BAND_2GHZ, \
25 .center_freq = (_freq), \
26 .hw_value = (_channel), \
27 .flags = (_flags), \
28 .max_antenna_gain = 0, \
29 .max_power = 30, \
30}
31
32static struct ieee80211_channel lbs_2ghz_channels[] = {
33 CHAN2G(1, 2412, 0),
34 CHAN2G(2, 2417, 0),
35 CHAN2G(3, 2422, 0),
36 CHAN2G(4, 2427, 0),
37 CHAN2G(5, 2432, 0),
38 CHAN2G(6, 2437, 0),
39 CHAN2G(7, 2442, 0),
40 CHAN2G(8, 2447, 0),
41 CHAN2G(9, 2452, 0),
42 CHAN2G(10, 2457, 0),
43 CHAN2G(11, 2462, 0),
44 CHAN2G(12, 2467, 0),
45 CHAN2G(13, 2472, 0),
46 CHAN2G(14, 2484, 0),
47};
48
Kiran Divekare86dc1c2010-06-14 22:01:26 +053049#define RATETAB_ENT(_rate, _hw_value, _flags) { \
50 .bitrate = (_rate), \
51 .hw_value = (_hw_value), \
52 .flags = (_flags), \
Holger Schurigff9fc792009-10-06 16:31:54 +020053}
54
55
Kiran Divekare86dc1c2010-06-14 22:01:26 +053056/* Table 6 in section 3.2.1.1 */
Holger Schurigff9fc792009-10-06 16:31:54 +020057static struct ieee80211_rate lbs_rates[] = {
Kiran Divekare86dc1c2010-06-14 22:01:26 +053058 RATETAB_ENT(10, 0, 0),
59 RATETAB_ENT(20, 1, 0),
60 RATETAB_ENT(55, 2, 0),
61 RATETAB_ENT(110, 3, 0),
62 RATETAB_ENT(60, 9, 0),
63 RATETAB_ENT(90, 6, 0),
64 RATETAB_ENT(120, 7, 0),
65 RATETAB_ENT(180, 8, 0),
66 RATETAB_ENT(240, 9, 0),
67 RATETAB_ENT(360, 10, 0),
68 RATETAB_ENT(480, 11, 0),
69 RATETAB_ENT(540, 12, 0),
Holger Schurigff9fc792009-10-06 16:31:54 +020070};
71
72static struct ieee80211_supported_band lbs_band_2ghz = {
73 .channels = lbs_2ghz_channels,
74 .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
75 .bitrates = lbs_rates,
76 .n_bitrates = ARRAY_SIZE(lbs_rates),
77};
78
79
80static const u32 cipher_suites[] = {
81 WLAN_CIPHER_SUITE_WEP40,
82 WLAN_CIPHER_SUITE_WEP104,
83 WLAN_CIPHER_SUITE_TKIP,
84 WLAN_CIPHER_SUITE_CCMP,
85};
86
Kiran Divekare86dc1c2010-06-14 22:01:26 +053087/* Time to stay on the channel */
88#define LBS_DWELL_PASSIVE 100
89#define LBS_DWELL_ACTIVE 40
Holger Schurigff9fc792009-10-06 16:31:54 +020090
91
Kiran Divekare86dc1c2010-06-14 22:01:26 +053092/***************************************************************************
93 * Misc utility functions
94 *
95 * TLVs are Marvell specific. They are very similar to IEs, they have the
96 * same structure: type, length, data*. The only difference: for IEs, the
97 * type and length are u8, but for TLVs they're __le16.
98 */
99
100/*
101 * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
102 * in the firmware spec
103 */
104static u8 lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
105{
106 int ret = -ENOTSUPP;
107
108 switch (auth_type) {
109 case NL80211_AUTHTYPE_OPEN_SYSTEM:
110 case NL80211_AUTHTYPE_SHARED_KEY:
111 ret = auth_type;
112 break;
113 case NL80211_AUTHTYPE_AUTOMATIC:
114 ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
115 break;
116 case NL80211_AUTHTYPE_NETWORK_EAP:
117 ret = 0x80;
118 break;
119 default:
120 /* silence compiler */
121 break;
122 }
123 return ret;
124}
125
126
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700127/*
128 * Various firmware commands need the list of supported rates, but with
129 * the hight-bit set for basic rates
130 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530131static int lbs_add_rates(u8 *rates)
132{
133 size_t i;
134
135 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
136 u8 rate = lbs_rates[i].bitrate / 5;
137 if (rate == 0x02 || rate == 0x04 ||
138 rate == 0x0b || rate == 0x16)
139 rate |= 0x80;
140 rates[i] = rate;
141 }
142 return ARRAY_SIZE(lbs_rates);
143}
144
145
146/***************************************************************************
147 * TLV utility functions
148 *
149 * TLVs are Marvell specific. They are very similar to IEs, they have the
150 * same structure: type, length, data*. The only difference: for IEs, the
151 * type and length are u8, but for TLVs they're __le16.
152 */
153
154
155/*
156 * Add ssid TLV
157 */
158#define LBS_MAX_SSID_TLV_SIZE \
159 (sizeof(struct mrvl_ie_header) \
160 + IEEE80211_MAX_SSID_LEN)
161
162static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
163{
164 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
165
166 /*
167 * TLV-ID SSID 00 00
168 * length 06 00
169 * ssid 4d 4e 54 45 53 54
170 */
171 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
172 ssid_tlv->header.len = cpu_to_le16(ssid_len);
173 memcpy(ssid_tlv->ssid, ssid, ssid_len);
174 return sizeof(ssid_tlv->header) + ssid_len;
175}
176
177
178/*
179 * Add channel list TLV (section 8.4.2)
180 *
181 * Actual channel data comes from priv->wdev->wiphy->channels.
182 */
183#define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
184 (sizeof(struct mrvl_ie_header) \
185 + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
186
187static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
188 int last_channel, int active_scan)
189{
190 int chanscanparamsize = sizeof(struct chanscanparamset) *
191 (last_channel - priv->scan_channel);
192
193 struct mrvl_ie_header *header = (void *) tlv;
194
195 /*
196 * TLV-ID CHANLIST 01 01
197 * length 0e 00
198 * channel 00 01 00 00 00 64 00
199 * radio type 00
200 * channel 01
201 * scan type 00
202 * min scan time 00 00
203 * max scan time 64 00
204 * channel 2 00 02 00 00 00 64 00
205 *
206 */
207
208 header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
209 header->len = cpu_to_le16(chanscanparamsize);
210 tlv += sizeof(struct mrvl_ie_header);
211
212 /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
213 last_channel); */
214 memset(tlv, 0, chanscanparamsize);
215
216 while (priv->scan_channel < last_channel) {
217 struct chanscanparamset *param = (void *) tlv;
218
219 param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
220 param->channumber =
221 priv->scan_req->channels[priv->scan_channel]->hw_value;
222 if (active_scan) {
223 param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
224 } else {
225 param->chanscanmode.passivescan = 1;
226 param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
227 }
228 tlv += sizeof(struct chanscanparamset);
229 priv->scan_channel++;
230 }
231 return sizeof(struct mrvl_ie_header) + chanscanparamsize;
232}
233
234
235/*
236 * Add rates TLV
237 *
238 * The rates are in lbs_bg_rates[], but for the 802.11b
239 * rates the high bit is set. We add this TLV only because
240 * there's a firmware which otherwise doesn't report all
241 * APs in range.
242 */
243#define LBS_MAX_RATES_TLV_SIZE \
244 (sizeof(struct mrvl_ie_header) \
245 + (ARRAY_SIZE(lbs_rates)))
246
247/* Adds a TLV with all rates the hardware supports */
248static int lbs_add_supported_rates_tlv(u8 *tlv)
249{
250 size_t i;
251 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
252
253 /*
254 * TLV-ID RATES 01 00
255 * length 0e 00
256 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
257 */
258 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
259 tlv += sizeof(rate_tlv->header);
260 i = lbs_add_rates(tlv);
261 tlv += i;
262 rate_tlv->header.len = cpu_to_le16(i);
263 return sizeof(rate_tlv->header) + i;
264}
265
Dan Williams19757532010-07-29 23:16:01 -0700266/* Add common rates from a TLV and return the new end of the TLV */
267static u8 *
268add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
269{
270 int hw, ap, ap_max = ie[1];
271 u8 hw_rate;
272
273 /* Advance past IE header */
274 ie += 2;
275
276 lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
277
278 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
279 hw_rate = lbs_rates[hw].bitrate / 5;
280 for (ap = 0; ap < ap_max; ap++) {
281 if (hw_rate == (ie[ap] & 0x7f)) {
282 *tlv++ = ie[ap];
283 *nrates = *nrates + 1;
284 }
285 }
286 }
287 return tlv;
288}
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530289
290/*
291 * Adds a TLV with all rates the hardware *and* BSS supports.
292 */
293static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
294{
295 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
Dan Williams19757532010-07-29 23:16:01 -0700296 const u8 *rates_eid, *ext_rates_eid;
297 int n = 0;
298
299 rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
300 ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530301
302 /*
303 * 01 00 TLV_TYPE_RATES
304 * 04 00 len
305 * 82 84 8b 96 rates
306 */
307 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
308 tlv += sizeof(rate_tlv->header);
309
Dan Williams19757532010-07-29 23:16:01 -0700310 /* Add basic rates */
311 if (rates_eid) {
312 tlv = add_ie_rates(tlv, rates_eid, &n);
313
314 /* Add extended rates, if any */
315 if (ext_rates_eid)
316 tlv = add_ie_rates(tlv, ext_rates_eid, &n);
317 } else {
318 lbs_deb_assoc("assoc: bss had no basic rate IE\n");
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530319 /* Fallback: add basic 802.11b rates */
320 *tlv++ = 0x82;
321 *tlv++ = 0x84;
322 *tlv++ = 0x8b;
323 *tlv++ = 0x96;
324 n = 4;
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530325 }
326
327 rate_tlv->header.len = cpu_to_le16(n);
328 return sizeof(rate_tlv->header) + n;
329}
330
331
332/*
333 * Add auth type TLV.
334 *
335 * This is only needed for newer firmware (V9 and up).
336 */
337#define LBS_MAX_AUTH_TYPE_TLV_SIZE \
338 sizeof(struct mrvl_ie_auth_type)
339
340static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
341{
342 struct mrvl_ie_auth_type *auth = (void *) tlv;
343
344 /*
345 * 1f 01 TLV_TYPE_AUTH_TYPE
346 * 01 00 len
347 * 01 auth type
348 */
349 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
350 auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
351 auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
352 return sizeof(*auth);
353}
354
355
356/*
357 * Add channel (phy ds) TLV
358 */
359#define LBS_MAX_CHANNEL_TLV_SIZE \
360 sizeof(struct mrvl_ie_header)
361
362static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
363{
364 struct mrvl_ie_ds_param_set *ds = (void *) tlv;
365
366 /*
367 * 03 00 TLV_TYPE_PHY_DS
368 * 01 00 len
369 * 06 channel
370 */
371 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
372 ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
373 ds->channel = channel;
374 return sizeof(*ds);
375}
376
377
378/*
379 * Add (empty) CF param TLV of the form:
380 */
381#define LBS_MAX_CF_PARAM_TLV_SIZE \
382 sizeof(struct mrvl_ie_header)
383
384static int lbs_add_cf_param_tlv(u8 *tlv)
385{
386 struct mrvl_ie_cf_param_set *cf = (void *)tlv;
387
388 /*
389 * 04 00 TLV_TYPE_CF
390 * 06 00 len
391 * 00 cfpcnt
392 * 00 cfpperiod
393 * 00 00 cfpmaxduration
394 * 00 00 cfpdurationremaining
395 */
396 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
397 cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
398 return sizeof(*cf);
399}
400
401/*
402 * Add WPA TLV
403 */
404#define LBS_MAX_WPA_TLV_SIZE \
405 (sizeof(struct mrvl_ie_header) \
406 + 128 /* TODO: I guessed the size */)
407
408static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
409{
410 size_t tlv_len;
411
412 /*
413 * We need just convert an IE to an TLV. IEs use u8 for the header,
414 * u8 type
415 * u8 len
416 * u8[] data
417 * but TLVs use __le16 instead:
418 * __le16 type
419 * __le16 len
420 * u8[] data
421 */
422 *tlv++ = *ie++;
423 *tlv++ = 0;
424 tlv_len = *tlv++ = *ie++;
425 *tlv++ = 0;
426 while (tlv_len--)
427 *tlv++ = *ie++;
428 /* the TLV is two bytes larger than the IE */
429 return ie_len + 2;
430}
431
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700432/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530433 * Set Channel
434 */
435
Holger Schurigff9fc792009-10-06 16:31:54 +0200436static int lbs_cfg_set_channel(struct wiphy *wiphy,
Johannes Bergf444de02010-05-05 15:25:02 +0200437 struct net_device *netdev,
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530438 struct ieee80211_channel *channel,
Holger Schurigff9fc792009-10-06 16:31:54 +0200439 enum nl80211_channel_type channel_type)
440{
441 struct lbs_private *priv = wiphy_priv(wiphy);
442 int ret = -ENOTSUPP;
443
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530444 lbs_deb_enter_args(LBS_DEB_CFG80211, "freq %d, type %d",
445 channel->center_freq, channel_type);
Holger Schurigff9fc792009-10-06 16:31:54 +0200446
447 if (channel_type != NL80211_CHAN_NO_HT)
448 goto out;
449
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530450 ret = lbs_set_channel(priv, channel->hw_value);
451
452 out:
453 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
454 return ret;
455}
456
457
458
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700459/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530460 * Scanning
461 */
462
463/*
464 * When scanning, the firmware doesn't send a nul packet with the power-safe
465 * bit to the AP. So we cannot stay away from our current channel too long,
466 * otherwise we loose data. So take a "nap" while scanning every other
467 * while.
468 */
469#define LBS_SCAN_BEFORE_NAP 4
470
471
472/*
473 * When the firmware reports back a scan-result, it gives us an "u8 rssi",
474 * which isn't really an RSSI, as it becomes larger when moving away from
475 * the AP. Anyway, we need to convert that into mBm.
476 */
477#define LBS_SCAN_RSSI_TO_MBM(rssi) \
478 ((-(int)rssi + 3)*100)
479
480static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
481 struct cmd_header *resp)
482{
483 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
484 int bsssize;
485 const u8 *pos;
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530486 const u8 *tsfdesc;
487 int tsfsize;
488 int i;
489 int ret = -EILSEQ;
490
491 lbs_deb_enter(LBS_DEB_CFG80211);
492
493 bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
Dan Williamsaebb6282010-07-29 23:11:30 -0700494
495 lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
John W. Linville65a602d2010-09-15 15:40:12 -0400496 scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
Dan Williamsaebb6282010-07-29 23:11:30 -0700497
John W. Linville65a602d2010-09-15 15:40:12 -0400498 if (scanresp->nr_sets == 0) {
Dan Williamsaebb6282010-07-29 23:11:30 -0700499 ret = 0;
500 goto done;
501 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530502
503 /*
504 * The general layout of the scan response is described in chapter
505 * 5.7.1. Basically we have a common part, then any number of BSS
506 * descriptor sections. Finally we have section with the same number
507 * of TSFs.
508 *
509 * cmd_ds_802_11_scan_rsp
510 * cmd_header
511 * pos_size
512 * nr_sets
513 * bssdesc 1
514 * bssid
515 * rssi
516 * timestamp
517 * intvl
518 * capa
519 * IEs
520 * bssdesc 2
521 * bssdesc n
522 * MrvlIEtypes_TsfFimestamp_t
523 * TSF for BSS 1
524 * TSF for BSS 2
525 * TSF for BSS n
526 */
527
528 pos = scanresp->bssdesc_and_tlvbuffer;
529
Dan Williams40838582010-07-29 23:12:53 -0700530 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
531 scanresp->bssdescriptsize);
532
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530533 tsfdesc = pos + bsssize;
534 tsfsize = 4 + 8 * scanresp->nr_sets;
Dan Williams40838582010-07-29 23:12:53 -0700535 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530536
537 /* Validity check: we expect a Marvell-Local TLV */
538 i = get_unaligned_le16(tsfdesc);
539 tsfdesc += 2;
Dan Williams40838582010-07-29 23:12:53 -0700540 if (i != TLV_TYPE_TSFTIMESTAMP) {
541 lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530542 goto done;
Dan Williams40838582010-07-29 23:12:53 -0700543 }
544
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700545 /*
546 * Validity check: the TLV holds TSF values with 8 bytes each, so
547 * the size in the TLV must match the nr_sets value
548 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530549 i = get_unaligned_le16(tsfdesc);
550 tsfdesc += 2;
Dan Williams40838582010-07-29 23:12:53 -0700551 if (i / 8 != scanresp->nr_sets) {
552 lbs_deb_scan("scan response: invalid number of TSF timestamp "
553 "sets (expected %d got %d)\n", scanresp->nr_sets,
554 i / 8);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530555 goto done;
Dan Williams40838582010-07-29 23:12:53 -0700556 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530557
558 for (i = 0; i < scanresp->nr_sets; i++) {
559 const u8 *bssid;
560 const u8 *ie;
561 int left;
562 int ielen;
563 int rssi;
564 u16 intvl;
565 u16 capa;
566 int chan_no = -1;
567 const u8 *ssid = NULL;
568 u8 ssid_len = 0;
569 DECLARE_SSID_BUF(ssid_buf);
570
571 int len = get_unaligned_le16(pos);
572 pos += 2;
573
574 /* BSSID */
575 bssid = pos;
576 pos += ETH_ALEN;
577 /* RSSI */
578 rssi = *pos++;
579 /* Packet time stamp */
580 pos += 8;
581 /* Beacon interval */
582 intvl = get_unaligned_le16(pos);
583 pos += 2;
584 /* Capabilities */
585 capa = get_unaligned_le16(pos);
586 pos += 2;
587
588 /* To find out the channel, we must parse the IEs */
589 ie = pos;
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700590 /*
591 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
592 * interval, capabilities
593 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530594 ielen = left = len - (6 + 1 + 8 + 2 + 2);
595 while (left >= 2) {
596 u8 id, elen;
597 id = *pos++;
598 elen = *pos++;
599 left -= 2;
Dan Williams40838582010-07-29 23:12:53 -0700600 if (elen > left || elen == 0) {
601 lbs_deb_scan("scan response: invalid IE fmt\n");
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530602 goto done;
Dan Williams40838582010-07-29 23:12:53 -0700603 }
604
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530605 if (id == WLAN_EID_DS_PARAMS)
606 chan_no = *pos;
607 if (id == WLAN_EID_SSID) {
608 ssid = pos;
609 ssid_len = elen;
610 }
611 left -= elen;
612 pos += elen;
613 }
614
615 /* No channel, no luck */
616 if (chan_no != -1) {
617 struct wiphy *wiphy = priv->wdev->wiphy;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900618 int freq = ieee80211_channel_to_frequency(chan_no,
619 IEEE80211_BAND_2GHZ);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530620 struct ieee80211_channel *channel =
621 ieee80211_get_channel(wiphy, freq);
622
623 lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
624 "%d dBm\n",
625 bssid, capa, chan_no,
626 print_ssid(ssid_buf, ssid, ssid_len),
627 LBS_SCAN_RSSI_TO_MBM(rssi)/100);
628
Sven Neumann4a55d582010-12-09 09:38:36 +0100629 if (channel &&
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530630 !(channel->flags & IEEE80211_CHAN_DISABLED))
631 cfg80211_inform_bss(wiphy, channel,
632 bssid, le64_to_cpu(*(__le64 *)tsfdesc),
633 capa, intvl, ie, ielen,
634 LBS_SCAN_RSSI_TO_MBM(rssi),
635 GFP_KERNEL);
Dan Williams40838582010-07-29 23:12:53 -0700636 } else
637 lbs_deb_scan("scan response: missing BSS channel IE\n");
638
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530639 tsfdesc += 8;
640 }
641 ret = 0;
642
643 done:
644 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
645 return ret;
646}
647
648
649/*
650 * Our scan command contains a TLV, consting of a SSID TLV, a channel list
651 * TLV and a rates TLV. Determine the maximum size of them:
652 */
653#define LBS_SCAN_MAX_CMD_SIZE \
654 (sizeof(struct cmd_ds_802_11_scan) \
655 + LBS_MAX_SSID_TLV_SIZE \
656 + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
657 + LBS_MAX_RATES_TLV_SIZE)
658
659/*
660 * Assumes priv->scan_req is initialized and valid
661 * Assumes priv->scan_channel is initialized
662 */
663static void lbs_scan_worker(struct work_struct *work)
664{
665 struct lbs_private *priv =
666 container_of(work, struct lbs_private, scan_work.work);
667 struct cmd_ds_802_11_scan *scan_cmd;
668 u8 *tlv; /* pointer into our current, growing TLV storage area */
669 int last_channel;
670 int running, carrier;
671
672 lbs_deb_enter(LBS_DEB_SCAN);
673
674 scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
675 if (scan_cmd == NULL)
676 goto out_no_scan_cmd;
677
678 /* prepare fixed part of scan command */
679 scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
680
681 /* stop network while we're away from our main channel */
682 running = !netif_queue_stopped(priv->dev);
683 carrier = netif_carrier_ok(priv->dev);
684 if (running)
685 netif_stop_queue(priv->dev);
686 if (carrier)
687 netif_carrier_off(priv->dev);
688
689 /* prepare fixed part of scan command */
690 tlv = scan_cmd->tlvbuffer;
691
692 /* add SSID TLV */
693 if (priv->scan_req->n_ssids)
694 tlv += lbs_add_ssid_tlv(tlv,
695 priv->scan_req->ssids[0].ssid,
696 priv->scan_req->ssids[0].ssid_len);
697
698 /* add channel TLVs */
699 last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
700 if (last_channel > priv->scan_req->n_channels)
701 last_channel = priv->scan_req->n_channels;
702 tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
703 priv->scan_req->n_ssids);
704
705 /* add rates TLV */
706 tlv += lbs_add_supported_rates_tlv(tlv);
707
708 if (priv->scan_channel < priv->scan_req->n_channels) {
709 cancel_delayed_work(&priv->scan_work);
Daniel Drake2e301682010-11-04 21:21:52 +0000710 if (!priv->stopping)
711 queue_delayed_work(priv->work_thread, &priv->scan_work,
712 msecs_to_jiffies(300));
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530713 }
714
715 /* This is the final data we are about to send */
716 scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
717 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
718 sizeof(*scan_cmd));
719 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
720 tlv - scan_cmd->tlvbuffer);
721
722 __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
723 le16_to_cpu(scan_cmd->hdr.size),
724 lbs_ret_scan, 0);
725
726 if (priv->scan_channel >= priv->scan_req->n_channels) {
727 /* Mark scan done */
Dan Williamscc026812010-08-04 00:43:47 -0500728 if (priv->internal_scan)
729 kfree(priv->scan_req);
730 else
731 cfg80211_scan_done(priv->scan_req, false);
732
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530733 priv->scan_req = NULL;
Dan Williamscc026812010-08-04 00:43:47 -0500734 priv->last_scan = jiffies;
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530735 }
736
737 /* Restart network */
738 if (carrier)
739 netif_carrier_on(priv->dev);
740 if (running && !priv->tx_pending_len)
741 netif_wake_queue(priv->dev);
742
743 kfree(scan_cmd);
744
Dan Williamscc026812010-08-04 00:43:47 -0500745 /* Wake up anything waiting on scan completion */
746 if (priv->scan_req == NULL) {
747 lbs_deb_scan("scan: waking up waiters\n");
748 wake_up_all(&priv->scan_q);
749 }
750
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530751 out_no_scan_cmd:
752 lbs_deb_leave(LBS_DEB_SCAN);
753}
754
Dan Williamscc026812010-08-04 00:43:47 -0500755static void _internal_start_scan(struct lbs_private *priv, bool internal,
756 struct cfg80211_scan_request *request)
757{
758 lbs_deb_enter(LBS_DEB_CFG80211);
759
760 lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
761 request->n_ssids, request->n_channels, request->ie_len);
762
763 priv->scan_channel = 0;
764 queue_delayed_work(priv->work_thread, &priv->scan_work,
765 msecs_to_jiffies(50));
766
767 priv->scan_req = request;
768 priv->internal_scan = internal;
769
770 lbs_deb_leave(LBS_DEB_CFG80211);
771}
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530772
773static int lbs_cfg_scan(struct wiphy *wiphy,
774 struct net_device *dev,
775 struct cfg80211_scan_request *request)
776{
777 struct lbs_private *priv = wiphy_priv(wiphy);
778 int ret = 0;
779
780 lbs_deb_enter(LBS_DEB_CFG80211);
781
782 if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
783 /* old scan request not yet processed */
784 ret = -EAGAIN;
785 goto out;
786 }
787
Dan Williamscc026812010-08-04 00:43:47 -0500788 _internal_start_scan(priv, false, request);
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530789
790 if (priv->surpriseremoved)
791 ret = -EIO;
792
Holger Schurigff9fc792009-10-06 16:31:54 +0200793 out:
794 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
795 return ret;
796}
797
798
799
800
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700801/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530802 * Events
803 */
804
805void lbs_send_disconnect_notification(struct lbs_private *priv)
806{
807 lbs_deb_enter(LBS_DEB_CFG80211);
808
809 cfg80211_disconnected(priv->dev,
810 0,
811 NULL, 0,
812 GFP_KERNEL);
813
814 lbs_deb_leave(LBS_DEB_CFG80211);
815}
816
817void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
818{
819 lbs_deb_enter(LBS_DEB_CFG80211);
820
821 cfg80211_michael_mic_failure(priv->dev,
822 priv->assoc_bss,
823 event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
824 NL80211_KEYTYPE_GROUP :
825 NL80211_KEYTYPE_PAIRWISE,
826 -1,
827 NULL,
828 GFP_KERNEL);
829
830 lbs_deb_leave(LBS_DEB_CFG80211);
831}
832
833
834
835
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700836/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530837 * Connect/disconnect
838 */
839
840
841/*
842 * This removes all WEP keys
843 */
844static int lbs_remove_wep_keys(struct lbs_private *priv)
845{
846 struct cmd_ds_802_11_set_wep cmd;
847 int ret;
848
849 lbs_deb_enter(LBS_DEB_CFG80211);
850
851 memset(&cmd, 0, sizeof(cmd));
852 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
853 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
854 cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
855
856 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
857
858 lbs_deb_leave(LBS_DEB_CFG80211);
859 return ret;
860}
861
862/*
863 * Set WEP keys
864 */
865static int lbs_set_wep_keys(struct lbs_private *priv)
866{
867 struct cmd_ds_802_11_set_wep cmd;
868 int i;
869 int ret;
870
871 lbs_deb_enter(LBS_DEB_CFG80211);
872
873 /*
874 * command 13 00
875 * size 50 00
876 * sequence xx xx
877 * result 00 00
878 * action 02 00 ACT_ADD
879 * transmit key 00 00
880 * type for key 1 01 WEP40
881 * type for key 2 00
882 * type for key 3 00
883 * type for key 4 00
884 * key 1 39 39 39 39 39 00 00 00
885 * 00 00 00 00 00 00 00 00
886 * key 2 00 00 00 00 00 00 00 00
887 * 00 00 00 00 00 00 00 00
888 * key 3 00 00 00 00 00 00 00 00
889 * 00 00 00 00 00 00 00 00
890 * key 4 00 00 00 00 00 00 00 00
891 */
892 if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
893 priv->wep_key_len[2] || priv->wep_key_len[3]) {
894 /* Only set wep keys if we have at least one of them */
895 memset(&cmd, 0, sizeof(cmd));
896 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
897 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
898 cmd.action = cpu_to_le16(CMD_ACT_ADD);
899
900 for (i = 0; i < 4; i++) {
901 switch (priv->wep_key_len[i]) {
902 case WLAN_KEY_LEN_WEP40:
903 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
904 break;
905 case WLAN_KEY_LEN_WEP104:
906 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
907 break;
908 default:
909 cmd.keytype[i] = 0;
910 break;
911 }
912 memcpy(cmd.keymaterial[i], priv->wep_key[i],
913 priv->wep_key_len[i]);
914 }
915
916 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
917 } else {
918 /* Otherwise remove all wep keys */
919 ret = lbs_remove_wep_keys(priv);
920 }
921
922 lbs_deb_leave(LBS_DEB_CFG80211);
923 return ret;
924}
925
926
927/*
928 * Enable/Disable RSN status
929 */
930static int lbs_enable_rsn(struct lbs_private *priv, int enable)
931{
932 struct cmd_ds_802_11_enable_rsn cmd;
933 int ret;
934
935 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
936
937 /*
938 * cmd 2f 00
939 * size 0c 00
940 * sequence xx xx
941 * result 00 00
942 * action 01 00 ACT_SET
943 * enable 01 00
944 */
945 memset(&cmd, 0, sizeof(cmd));
946 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
947 cmd.action = cpu_to_le16(CMD_ACT_SET);
948 cmd.enable = cpu_to_le16(enable);
949
950 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
951
952 lbs_deb_leave(LBS_DEB_CFG80211);
953 return ret;
954}
955
956
957/*
958 * Set WPA/WPA key material
959 */
960
Randy Dunlap8973a6e2011-04-26 15:25:29 -0700961/*
962 * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
963 * get rid of WEXT, this should go into host.h
964 */
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530965
966struct cmd_key_material {
967 struct cmd_header hdr;
968
969 __le16 action;
970 struct MrvlIEtype_keyParamSet param;
John W. Linvillebeabe912010-07-14 10:37:03 -0400971} __packed;
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530972
973static int lbs_set_key_material(struct lbs_private *priv,
974 int key_type,
975 int key_info,
976 u8 *key, u16 key_len)
977{
978 struct cmd_key_material cmd;
979 int ret;
980
981 lbs_deb_enter(LBS_DEB_CFG80211);
982
983 /*
984 * Example for WPA (TKIP):
985 *
986 * cmd 5e 00
987 * size 34 00
988 * sequence xx xx
989 * result 00 00
990 * action 01 00
991 * TLV type 00 01 key param
992 * length 00 26
993 * key type 01 00 TKIP
994 * key info 06 00 UNICAST | ENABLED
995 * key len 20 00
996 * key 32 bytes
997 */
998 memset(&cmd, 0, sizeof(cmd));
999 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1000 cmd.action = cpu_to_le16(CMD_ACT_SET);
1001 cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
1002 cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
1003 cmd.param.keytypeid = cpu_to_le16(key_type);
1004 cmd.param.keyinfo = cpu_to_le16(key_info);
1005 cmd.param.keylen = cpu_to_le16(key_len);
1006 if (key && key_len)
1007 memcpy(cmd.param.key, key, key_len);
1008
1009 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
1010
1011 lbs_deb_leave(LBS_DEB_CFG80211);
1012 return ret;
1013}
1014
1015
1016/*
1017 * Sets the auth type (open, shared, etc) in the firmware. That
1018 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1019 * command doesn't send an authentication frame at all, it just
1020 * stores the auth_type.
1021 */
1022static int lbs_set_authtype(struct lbs_private *priv,
1023 struct cfg80211_connect_params *sme)
1024{
1025 struct cmd_ds_802_11_authenticate cmd;
1026 int ret;
1027
1028 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
1029
1030 /*
1031 * cmd 11 00
1032 * size 19 00
1033 * sequence xx xx
1034 * result 00 00
1035 * BSS id 00 13 19 80 da 30
1036 * auth type 00
1037 * reserved 00 00 00 00 00 00 00 00 00 00
1038 */
1039 memset(&cmd, 0, sizeof(cmd));
1040 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1041 if (sme->bssid)
1042 memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
1043 /* convert auth_type */
1044 ret = lbs_auth_to_authtype(sme->auth_type);
1045 if (ret < 0)
1046 goto done;
1047
1048 cmd.authtype = ret;
1049 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
1050
1051 done:
1052 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1053 return ret;
1054}
1055
1056
1057/*
1058 * Create association request
1059 */
1060#define LBS_ASSOC_MAX_CMD_SIZE \
1061 (sizeof(struct cmd_ds_802_11_associate) \
1062 - 512 /* cmd_ds_802_11_associate.iebuf */ \
1063 + LBS_MAX_SSID_TLV_SIZE \
1064 + LBS_MAX_CHANNEL_TLV_SIZE \
1065 + LBS_MAX_CF_PARAM_TLV_SIZE \
1066 + LBS_MAX_AUTH_TYPE_TLV_SIZE \
1067 + LBS_MAX_WPA_TLV_SIZE)
1068
1069static int lbs_associate(struct lbs_private *priv,
1070 struct cfg80211_bss *bss,
1071 struct cfg80211_connect_params *sme)
1072{
1073 struct cmd_ds_802_11_associate_response *resp;
1074 struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
1075 GFP_KERNEL);
1076 const u8 *ssid_eid;
1077 size_t len, resp_ie_len;
1078 int status;
1079 int ret;
1080 u8 *pos = &(cmd->iebuf[0]);
Dan Williams19757532010-07-29 23:16:01 -07001081 u8 *tmp;
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301082
1083 lbs_deb_enter(LBS_DEB_CFG80211);
1084
1085 if (!cmd) {
1086 ret = -ENOMEM;
1087 goto done;
1088 }
1089
1090 /*
1091 * cmd 50 00
1092 * length 34 00
1093 * sequence xx xx
1094 * result 00 00
1095 * BSS id 00 13 19 80 da 30
1096 * capabilities 11 00
1097 * listen interval 0a 00
1098 * beacon interval 00 00
1099 * DTIM period 00
1100 * TLVs xx (up to 512 bytes)
1101 */
1102 cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1103
1104 /* Fill in static fields */
1105 memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1106 cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1107 cmd->capability = cpu_to_le16(bss->capability);
1108
1109 /* add SSID TLV */
1110 ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1111 if (ssid_eid)
1112 pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
1113 else
1114 lbs_deb_assoc("no SSID\n");
1115
1116 /* add DS param TLV */
1117 if (bss->channel)
1118 pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1119 else
1120 lbs_deb_assoc("no channel\n");
1121
1122 /* add (empty) CF param TLV */
1123 pos += lbs_add_cf_param_tlv(pos);
1124
1125 /* add rates TLV */
Dan Williams19757532010-07-29 23:16:01 -07001126 tmp = pos + 4; /* skip Marvell IE header */
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301127 pos += lbs_add_common_rates_tlv(pos, bss);
Dan Williams19757532010-07-29 23:16:01 -07001128 lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301129
1130 /* add auth type TLV */
Dan Williams86df5f72010-07-29 23:14:33 -07001131 if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301132 pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1133
1134 /* add WPA/WPA2 TLV */
1135 if (sme->ie && sme->ie_len)
1136 pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1137
1138 len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
1139 (u16)(pos - (u8 *) &cmd->iebuf);
1140 cmd->hdr.size = cpu_to_le16(len);
1141
Dan Williams86df5f72010-07-29 23:14:33 -07001142 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1143 le16_to_cpu(cmd->hdr.size));
1144
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301145 /* store for later use */
1146 memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1147
1148 ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1149 if (ret)
1150 goto done;
1151
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301152 /* generate connect message to cfg80211 */
1153
1154 resp = (void *) cmd; /* recast for easier field access */
1155 status = le16_to_cpu(resp->statuscode);
1156
Dan Williams86df5f72010-07-29 23:14:33 -07001157 /* Older FW versions map the IEEE 802.11 Status Code in the association
1158 * response to the following values returned in resp->statuscode:
1159 *
1160 * IEEE Status Code Marvell Status Code
1161 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1162 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1163 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1164 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1165 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1166 * others -> 0x0003 ASSOC_RESULT_REFUSED
1167 *
1168 * Other response codes:
1169 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1170 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1171 * association response from the AP)
1172 */
1173 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301174 switch (status) {
1175 case 0:
1176 break;
1177 case 1:
1178 lbs_deb_assoc("invalid association parameters\n");
1179 status = WLAN_STATUS_CAPS_UNSUPPORTED;
1180 break;
1181 case 2:
1182 lbs_deb_assoc("timer expired while waiting for AP\n");
1183 status = WLAN_STATUS_AUTH_TIMEOUT;
1184 break;
1185 case 3:
1186 lbs_deb_assoc("association refused by AP\n");
1187 status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1188 break;
1189 case 4:
1190 lbs_deb_assoc("authentication refused by AP\n");
1191 status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1192 break;
1193 default:
1194 lbs_deb_assoc("association failure %d\n", status);
Dan Williams86df5f72010-07-29 23:14:33 -07001195 /* v5 OLPC firmware does return the AP status code if
1196 * it's not one of the values above. Let that through.
1197 */
1198 break;
1199 }
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301200 }
1201
Dan Williams86df5f72010-07-29 23:14:33 -07001202 lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1203 "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1204 le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301205
1206 resp_ie_len = le16_to_cpu(resp->hdr.size)
1207 - sizeof(resp->hdr)
1208 - 6;
1209 cfg80211_connect_result(priv->dev,
1210 priv->assoc_bss,
1211 sme->ie, sme->ie_len,
1212 resp->iebuf, resp_ie_len,
1213 status,
1214 GFP_KERNEL);
1215
1216 if (status == 0) {
1217 /* TODO: get rid of priv->connect_status */
1218 priv->connect_status = LBS_CONNECTED;
1219 netif_carrier_on(priv->dev);
1220 if (!priv->tx_pending_len)
1221 netif_tx_wake_all_queues(priv->dev);
1222 }
1223
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301224done:
1225 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1226 return ret;
1227}
1228
Dan Williamscc026812010-08-04 00:43:47 -05001229static struct cfg80211_scan_request *
1230_new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1231{
1232 struct cfg80211_scan_request *creq = NULL;
1233 int i, n_channels = 0;
1234 enum ieee80211_band band;
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301235
Dan Williamscc026812010-08-04 00:43:47 -05001236 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1237 if (wiphy->bands[band])
1238 n_channels += wiphy->bands[band]->n_channels;
1239 }
1240
1241 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1242 n_channels * sizeof(void *),
1243 GFP_ATOMIC);
1244 if (!creq)
1245 return NULL;
1246
1247 /* SSIDs come after channels */
1248 creq->ssids = (void *)&creq->channels[n_channels];
1249 creq->n_channels = n_channels;
1250 creq->n_ssids = 1;
1251
1252 /* Scan all available channels */
1253 i = 0;
1254 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1255 int j;
1256
1257 if (!wiphy->bands[band])
1258 continue;
1259
1260 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1261 /* ignore disabled channels */
1262 if (wiphy->bands[band]->channels[j].flags &
1263 IEEE80211_CHAN_DISABLED)
1264 continue;
1265
1266 creq->channels[i] = &wiphy->bands[band]->channels[j];
1267 i++;
1268 }
1269 }
1270 if (i) {
1271 /* Set real number of channels specified in creq->channels[] */
1272 creq->n_channels = i;
1273
1274 /* Scan for the SSID we're going to connect to */
1275 memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1276 creq->ssids[0].ssid_len = sme->ssid_len;
1277 } else {
1278 /* No channels found... */
1279 kfree(creq);
1280 creq = NULL;
1281 }
1282
1283 return creq;
1284}
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301285
1286static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1287 struct cfg80211_connect_params *sme)
1288{
1289 struct lbs_private *priv = wiphy_priv(wiphy);
1290 struct cfg80211_bss *bss = NULL;
1291 int ret = 0;
1292 u8 preamble = RADIO_PREAMBLE_SHORT;
1293
1294 lbs_deb_enter(LBS_DEB_CFG80211);
1295
Dan Williamscc026812010-08-04 00:43:47 -05001296 if (!sme->bssid) {
1297 /* Run a scan if one isn't in-progress already and if the last
1298 * scan was done more than 2 seconds ago.
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301299 */
Dan Williamscc026812010-08-04 00:43:47 -05001300 if (priv->scan_req == NULL &&
1301 time_after(jiffies, priv->last_scan + (2 * HZ))) {
1302 struct cfg80211_scan_request *creq;
1303
1304 creq = _new_connect_scan_req(wiphy, sme);
1305 if (!creq) {
1306 ret = -EINVAL;
1307 goto done;
1308 }
1309
1310 lbs_deb_assoc("assoc: scanning for compatible AP\n");
1311 _internal_start_scan(priv, true, creq);
1312 }
1313
1314 /* Wait for any in-progress scan to complete */
1315 lbs_deb_assoc("assoc: waiting for scan to complete\n");
1316 wait_event_interruptible_timeout(priv->scan_q,
1317 (priv->scan_req == NULL),
1318 (15 * HZ));
1319 lbs_deb_assoc("assoc: scanning competed\n");
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301320 }
1321
Dan Williamscc026812010-08-04 00:43:47 -05001322 /* Find the BSS we want using available scan results */
1323 bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1324 sme->ssid, sme->ssid_len,
1325 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301326 if (!bss) {
Joe Perches0e4e06a2011-05-02 16:49:14 -07001327 pr_err("assoc: bss %pM not in scan results\n", sme->bssid);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301328 ret = -ENOENT;
1329 goto done;
1330 }
Dan Williamscc026812010-08-04 00:43:47 -05001331 lbs_deb_assoc("trying %pM\n", bss->bssid);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301332 lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1333 sme->crypto.cipher_group,
1334 sme->key_idx, sme->key_len);
1335
1336 /* As this is a new connection, clear locally stored WEP keys */
1337 priv->wep_tx_key = 0;
1338 memset(priv->wep_key, 0, sizeof(priv->wep_key));
1339 memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1340
1341 /* set/remove WEP keys */
1342 switch (sme->crypto.cipher_group) {
1343 case WLAN_CIPHER_SUITE_WEP40:
1344 case WLAN_CIPHER_SUITE_WEP104:
1345 /* Store provided WEP keys in priv-> */
1346 priv->wep_tx_key = sme->key_idx;
1347 priv->wep_key_len[sme->key_idx] = sme->key_len;
1348 memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1349 /* Set WEP keys and WEP mode */
1350 lbs_set_wep_keys(priv);
1351 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1352 lbs_set_mac_control(priv);
1353 /* No RSN mode for WEP */
1354 lbs_enable_rsn(priv, 0);
1355 break;
1356 case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1357 /*
1358 * If we don't have no WEP, no WPA and no WPA2,
1359 * we remove all keys like in the WPA/WPA2 setup,
1360 * we just don't set RSN.
1361 *
1362 * Therefore: fall-throught
1363 */
1364 case WLAN_CIPHER_SUITE_TKIP:
1365 case WLAN_CIPHER_SUITE_CCMP:
1366 /* Remove WEP keys and WEP mode */
1367 lbs_remove_wep_keys(priv);
1368 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1369 lbs_set_mac_control(priv);
1370
1371 /* clear the WPA/WPA2 keys */
1372 lbs_set_key_material(priv,
1373 KEY_TYPE_ID_WEP, /* doesn't matter */
1374 KEY_INFO_WPA_UNICAST,
1375 NULL, 0);
1376 lbs_set_key_material(priv,
1377 KEY_TYPE_ID_WEP, /* doesn't matter */
1378 KEY_INFO_WPA_MCAST,
1379 NULL, 0);
1380 /* RSN mode for WPA/WPA2 */
1381 lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1382 break;
1383 default:
Joe Perches0e4e06a2011-05-02 16:49:14 -07001384 pr_err("unsupported cipher group 0x%x\n",
1385 sme->crypto.cipher_group);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301386 ret = -ENOTSUPP;
1387 goto done;
1388 }
1389
1390 lbs_set_authtype(priv, sme);
1391 lbs_set_radio(priv, preamble, 1);
1392
1393 /* Do the actual association */
Dan Williamscc026812010-08-04 00:43:47 -05001394 ret = lbs_associate(priv, bss, sme);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301395
1396 done:
1397 if (bss)
1398 cfg80211_put_bss(bss);
1399 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1400 return ret;
1401}
1402
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301403static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1404 u16 reason_code)
1405{
1406 struct lbs_private *priv = wiphy_priv(wiphy);
1407 struct cmd_ds_802_11_deauthenticate cmd;
1408
1409 lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
1410
1411 /* store for lbs_cfg_ret_disconnect() */
1412 priv->disassoc_reason = reason_code;
1413
1414 memset(&cmd, 0, sizeof(cmd));
1415 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1416 /* Mildly ugly to use a locally store my own BSSID ... */
1417 memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1418 cmd.reasoncode = cpu_to_le16(reason_code);
1419
Kiran Divekare4fe4ea2010-06-04 23:20:37 -07001420 if (lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd))
1421 return -EFAULT;
1422
1423 cfg80211_disconnected(priv->dev,
1424 priv->disassoc_reason,
1425 NULL, 0,
1426 GFP_KERNEL);
1427 priv->connect_status = LBS_DISCONNECTED;
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301428
1429 return 0;
1430}
1431
1432
1433static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1434 struct net_device *netdev,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001435 u8 key_index, bool unicast,
1436 bool multicast)
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301437{
1438 struct lbs_private *priv = wiphy_priv(wiphy);
1439
1440 lbs_deb_enter(LBS_DEB_CFG80211);
1441
1442 if (key_index != priv->wep_tx_key) {
1443 lbs_deb_assoc("set_default_key: to %d\n", key_index);
1444 priv->wep_tx_key = key_index;
1445 lbs_set_wep_keys(priv);
1446 }
1447
1448 return 0;
1449}
1450
1451
1452static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
Johannes Berge31b8212010-10-05 19:39:30 +02001453 u8 idx, bool pairwise, const u8 *mac_addr,
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301454 struct key_params *params)
1455{
1456 struct lbs_private *priv = wiphy_priv(wiphy);
1457 u16 key_info;
1458 u16 key_type;
1459 int ret = 0;
1460
1461 lbs_deb_enter(LBS_DEB_CFG80211);
1462
1463 lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1464 params->cipher, mac_addr);
1465 lbs_deb_assoc("add_key: key index %d, key len %d\n",
1466 idx, params->key_len);
1467 if (params->key_len)
1468 lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1469 params->key, params->key_len);
1470
1471 lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1472 if (params->seq_len)
1473 lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1474 params->seq, params->seq_len);
1475
1476 switch (params->cipher) {
1477 case WLAN_CIPHER_SUITE_WEP40:
1478 case WLAN_CIPHER_SUITE_WEP104:
1479 /* actually compare if something has changed ... */
1480 if ((priv->wep_key_len[idx] != params->key_len) ||
1481 memcmp(priv->wep_key[idx],
1482 params->key, params->key_len) != 0) {
1483 priv->wep_key_len[idx] = params->key_len;
1484 memcpy(priv->wep_key[idx],
1485 params->key, params->key_len);
1486 lbs_set_wep_keys(priv);
1487 }
1488 break;
1489 case WLAN_CIPHER_SUITE_TKIP:
1490 case WLAN_CIPHER_SUITE_CCMP:
1491 key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1492 ? KEY_INFO_WPA_UNICAST
1493 : KEY_INFO_WPA_MCAST);
1494 key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1495 ? KEY_TYPE_ID_TKIP
1496 : KEY_TYPE_ID_AES;
1497 lbs_set_key_material(priv,
1498 key_type,
1499 key_info,
1500 params->key, params->key_len);
1501 break;
1502 default:
Joe Perches0e4e06a2011-05-02 16:49:14 -07001503 pr_err("unhandled cipher 0x%x\n", params->cipher);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301504 ret = -ENOTSUPP;
1505 break;
1506 }
1507
1508 return ret;
1509}
1510
1511
1512static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
Johannes Berge31b8212010-10-05 19:39:30 +02001513 u8 key_index, bool pairwise, const u8 *mac_addr)
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301514{
1515
1516 lbs_deb_enter(LBS_DEB_CFG80211);
1517
1518 lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1519 key_index, mac_addr);
1520
1521#ifdef TODO
1522 struct lbs_private *priv = wiphy_priv(wiphy);
1523 /*
1524 * I think can keep this a NO-OP, because:
1525
1526 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1527 * - neither "iw" nor "wpa_supplicant" won't call this during
1528 * an ongoing connection
1529 * - TODO: but I have to check if this is still true when
1530 * I set the AP to periodic re-keying
1531 * - we've not kzallec() something when we've added a key at
1532 * lbs_cfg_connect() or lbs_cfg_add_key().
1533 *
1534 * This causes lbs_cfg_del_key() only called at disconnect time,
1535 * where we'd just waste time deleting a key that is not going
1536 * to be used anyway.
1537 */
1538 if (key_index < 3 && priv->wep_key_len[key_index]) {
1539 priv->wep_key_len[key_index] = 0;
1540 lbs_set_wep_keys(priv);
1541 }
1542#endif
1543
1544 return 0;
1545}
1546
1547
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001548/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301549 * Get station
1550 */
1551
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301552static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1553 u8 *mac, struct station_info *sinfo)
1554{
1555 struct lbs_private *priv = wiphy_priv(wiphy);
1556 s8 signal, noise;
1557 int ret;
1558 size_t i;
1559
1560 lbs_deb_enter(LBS_DEB_CFG80211);
1561
1562 sinfo->filled |= STATION_INFO_TX_BYTES |
1563 STATION_INFO_TX_PACKETS |
1564 STATION_INFO_RX_BYTES |
1565 STATION_INFO_RX_PACKETS;
1566 sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1567 sinfo->tx_packets = priv->dev->stats.tx_packets;
1568 sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1569 sinfo->rx_packets = priv->dev->stats.rx_packets;
1570
1571 /* Get current RSSI */
Dan Williams9fb76632010-07-27 12:55:21 -07001572 ret = lbs_get_rssi(priv, &signal, &noise);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301573 if (ret == 0) {
1574 sinfo->signal = signal;
1575 sinfo->filled |= STATION_INFO_SIGNAL;
1576 }
1577
1578 /* Convert priv->cur_rate from hw_value to NL80211 value */
1579 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1580 if (priv->cur_rate == lbs_rates[i].hw_value) {
1581 sinfo->txrate.legacy = lbs_rates[i].bitrate;
1582 sinfo->filled |= STATION_INFO_TX_BITRATE;
1583 break;
1584 }
1585 }
1586
1587 return 0;
1588}
1589
1590
1591
1592
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001593/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301594 * "Site survey", here just current channel and noise level
1595 */
1596
1597static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
1598 int idx, struct survey_info *survey)
1599{
1600 struct lbs_private *priv = wiphy_priv(wiphy);
1601 s8 signal, noise;
1602 int ret;
1603
1604 if (idx != 0)
1605 ret = -ENOENT;
1606
1607 lbs_deb_enter(LBS_DEB_CFG80211);
1608
1609 survey->channel = ieee80211_get_channel(wiphy,
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001610 ieee80211_channel_to_frequency(priv->channel,
1611 IEEE80211_BAND_2GHZ));
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301612
Dan Williams9fb76632010-07-27 12:55:21 -07001613 ret = lbs_get_rssi(priv, &signal, &noise);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301614 if (ret == 0) {
1615 survey->filled = SURVEY_INFO_NOISE_DBM;
1616 survey->noise = noise;
1617 }
1618
1619 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1620 return ret;
1621}
1622
1623
1624
1625
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001626/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301627 * Change interface
1628 */
1629
1630static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1631 enum nl80211_iftype type, u32 *flags,
1632 struct vif_params *params)
1633{
1634 struct lbs_private *priv = wiphy_priv(wiphy);
1635 int ret = 0;
1636
1637 lbs_deb_enter(LBS_DEB_CFG80211);
1638
1639 switch (type) {
1640 case NL80211_IFTYPE_MONITOR:
Dan Williamsa45b6f42010-07-27 12:54:34 -07001641 ret = lbs_set_monitor_mode(priv, 1);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301642 break;
1643 case NL80211_IFTYPE_STATION:
1644 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Dan Williamsa45b6f42010-07-27 12:54:34 -07001645 ret = lbs_set_monitor_mode(priv, 0);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301646 if (!ret)
1647 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1);
1648 break;
1649 case NL80211_IFTYPE_ADHOC:
1650 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Dan Williamsa45b6f42010-07-27 12:54:34 -07001651 ret = lbs_set_monitor_mode(priv, 0);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301652 if (!ret)
1653 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2);
1654 break;
1655 default:
1656 ret = -ENOTSUPP;
1657 }
1658
1659 if (!ret)
1660 priv->wdev->iftype = type;
1661
1662 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1663 return ret;
1664}
1665
1666
1667
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001668/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301669 * IBSS (Ad-Hoc)
1670 */
1671
Randy Dunlap8973a6e2011-04-26 15:25:29 -07001672/*
1673 * The firmware needs the following bits masked out of the beacon-derived
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301674 * capability field when associating/joining to a BSS:
1675 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1676 */
1677#define CAPINFO_MASK (~(0xda00))
1678
1679
1680static void lbs_join_post(struct lbs_private *priv,
1681 struct cfg80211_ibss_params *params,
1682 u8 *bssid, u16 capability)
1683{
1684 u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1685 2 + 4 + /* basic rates */
1686 2 + 1 + /* DS parameter */
1687 2 + 2 + /* atim */
1688 2 + 8]; /* extended rates */
1689 u8 *fake = fake_ie;
1690
1691 lbs_deb_enter(LBS_DEB_CFG80211);
1692
1693 /*
1694 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1695 * the real IE from the firmware. So we fabricate a fake IE based on
1696 * what the firmware actually sends (sniffed with wireshark).
1697 */
1698 /* Fake SSID IE */
1699 *fake++ = WLAN_EID_SSID;
1700 *fake++ = params->ssid_len;
1701 memcpy(fake, params->ssid, params->ssid_len);
1702 fake += params->ssid_len;
1703 /* Fake supported basic rates IE */
1704 *fake++ = WLAN_EID_SUPP_RATES;
1705 *fake++ = 4;
1706 *fake++ = 0x82;
1707 *fake++ = 0x84;
1708 *fake++ = 0x8b;
1709 *fake++ = 0x96;
1710 /* Fake DS channel IE */
1711 *fake++ = WLAN_EID_DS_PARAMS;
1712 *fake++ = 1;
1713 *fake++ = params->channel->hw_value;
1714 /* Fake IBSS params IE */
1715 *fake++ = WLAN_EID_IBSS_PARAMS;
1716 *fake++ = 2;
1717 *fake++ = 0; /* ATIM=0 */
1718 *fake++ = 0;
1719 /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1720 * but I don't know how this could be checked */
1721 *fake++ = WLAN_EID_EXT_SUPP_RATES;
1722 *fake++ = 8;
1723 *fake++ = 0x0c;
1724 *fake++ = 0x12;
1725 *fake++ = 0x18;
1726 *fake++ = 0x24;
1727 *fake++ = 0x30;
1728 *fake++ = 0x48;
1729 *fake++ = 0x60;
1730 *fake++ = 0x6c;
1731 lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1732
1733 cfg80211_inform_bss(priv->wdev->wiphy,
1734 params->channel,
1735 bssid,
1736 0,
1737 capability,
1738 params->beacon_interval,
1739 fake_ie, fake - fake_ie,
1740 0, GFP_KERNEL);
Kiran Divekare4fe4ea2010-06-04 23:20:37 -07001741
1742 memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
1743 priv->wdev->ssid_len = params->ssid_len;
1744
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301745 cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
1746
1747 /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1748 priv->connect_status = LBS_CONNECTED;
1749 netif_carrier_on(priv->dev);
1750 if (!priv->tx_pending_len)
1751 netif_wake_queue(priv->dev);
1752
1753 lbs_deb_leave(LBS_DEB_CFG80211);
1754}
1755
1756static int lbs_ibss_join_existing(struct lbs_private *priv,
1757 struct cfg80211_ibss_params *params,
1758 struct cfg80211_bss *bss)
1759{
1760 const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1761 struct cmd_ds_802_11_ad_hoc_join cmd;
1762 u8 preamble = RADIO_PREAMBLE_SHORT;
1763 int ret = 0;
1764
1765 lbs_deb_enter(LBS_DEB_CFG80211);
1766
1767 /* TODO: set preamble based on scan result */
1768 ret = lbs_set_radio(priv, preamble, 1);
1769 if (ret)
1770 goto out;
1771
1772 /*
1773 * Example CMD_802_11_AD_HOC_JOIN command:
1774 *
1775 * command 2c 00 CMD_802_11_AD_HOC_JOIN
1776 * size 65 00
1777 * sequence xx xx
1778 * result 00 00
1779 * bssid 02 27 27 97 2f 96
1780 * ssid 49 42 53 53 00 00 00 00
1781 * 00 00 00 00 00 00 00 00
1782 * 00 00 00 00 00 00 00 00
1783 * 00 00 00 00 00 00 00 00
1784 * type 02 CMD_BSS_TYPE_IBSS
1785 * beacon period 64 00
1786 * dtim period 00
1787 * timestamp 00 00 00 00 00 00 00 00
1788 * localtime 00 00 00 00 00 00 00 00
1789 * IE DS 03
1790 * IE DS len 01
1791 * IE DS channel 01
1792 * reserveed 00 00 00 00
1793 * IE IBSS 06
1794 * IE IBSS len 02
1795 * IE IBSS atim 00 00
1796 * reserved 00 00 00 00
1797 * capability 02 00
1798 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1799 * fail timeout ff 00
1800 * probe delay 00 00
1801 */
1802 memset(&cmd, 0, sizeof(cmd));
1803 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1804
1805 memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1806 memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1807 cmd.bss.type = CMD_BSS_TYPE_IBSS;
1808 cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1809 cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1810 cmd.bss.ds.header.len = 1;
1811 cmd.bss.ds.channel = params->channel->hw_value;
1812 cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1813 cmd.bss.ibss.header.len = 2;
1814 cmd.bss.ibss.atimwindow = 0;
1815 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1816
1817 /* set rates to the intersection of our rates and the rates in the
1818 bss */
1819 if (!rates_eid) {
1820 lbs_add_rates(cmd.bss.rates);
1821 } else {
1822 int hw, i;
1823 u8 rates_max = rates_eid[1];
1824 u8 *rates = cmd.bss.rates;
1825 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1826 u8 hw_rate = lbs_rates[hw].bitrate / 5;
1827 for (i = 0; i < rates_max; i++) {
1828 if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1829 u8 rate = rates_eid[i+2];
1830 if (rate == 0x02 || rate == 0x04 ||
1831 rate == 0x0b || rate == 0x16)
1832 rate |= 0x80;
1833 *rates++ = rate;
1834 }
1835 }
1836 }
1837 }
1838
1839 /* Only v8 and below support setting this */
1840 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1841 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1842 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1843 }
1844 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1845 if (ret)
1846 goto out;
1847
1848 /*
1849 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1850 *
1851 * response 2c 80
1852 * size 09 00
1853 * sequence xx xx
1854 * result 00 00
1855 * reserved 00
1856 */
1857 lbs_join_post(priv, params, bss->bssid, bss->capability);
1858
1859 out:
1860 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1861 return ret;
1862}
1863
1864
1865
1866static int lbs_ibss_start_new(struct lbs_private *priv,
1867 struct cfg80211_ibss_params *params)
1868{
1869 struct cmd_ds_802_11_ad_hoc_start cmd;
1870 struct cmd_ds_802_11_ad_hoc_result *resp =
1871 (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1872 u8 preamble = RADIO_PREAMBLE_SHORT;
1873 int ret = 0;
1874 u16 capability;
1875
1876 lbs_deb_enter(LBS_DEB_CFG80211);
1877
1878 ret = lbs_set_radio(priv, preamble, 1);
1879 if (ret)
1880 goto out;
1881
1882 /*
1883 * Example CMD_802_11_AD_HOC_START command:
1884 *
1885 * command 2b 00 CMD_802_11_AD_HOC_START
1886 * size b1 00
1887 * sequence xx xx
1888 * result 00 00
1889 * ssid 54 45 53 54 00 00 00 00
1890 * 00 00 00 00 00 00 00 00
1891 * 00 00 00 00 00 00 00 00
1892 * 00 00 00 00 00 00 00 00
1893 * bss type 02
1894 * beacon period 64 00
1895 * dtim period 00
1896 * IE IBSS 06
1897 * IE IBSS len 02
1898 * IE IBSS atim 00 00
1899 * reserved 00 00 00 00
1900 * IE DS 03
1901 * IE DS len 01
1902 * IE DS channel 01
1903 * reserved 00 00 00 00
1904 * probe delay 00 00
1905 * capability 02 00
1906 * rates 82 84 8b 96 (basic rates with have bit 7 set)
1907 * 0c 12 18 24 30 48 60 6c
1908 * padding 100 bytes
1909 */
1910 memset(&cmd, 0, sizeof(cmd));
1911 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1912 memcpy(cmd.ssid, params->ssid, params->ssid_len);
1913 cmd.bsstype = CMD_BSS_TYPE_IBSS;
1914 cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1915 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1916 cmd.ibss.header.len = 2;
1917 cmd.ibss.atimwindow = 0;
1918 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1919 cmd.ds.header.len = 1;
1920 cmd.ds.channel = params->channel->hw_value;
1921 /* Only v8 and below support setting probe delay */
1922 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1923 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1924 /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1925 capability = WLAN_CAPABILITY_IBSS;
1926 cmd.capability = cpu_to_le16(capability);
1927 lbs_add_rates(cmd.rates);
1928
1929
1930 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1931 if (ret)
1932 goto out;
1933
1934 /*
1935 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1936 *
1937 * response 2b 80
1938 * size 14 00
1939 * sequence xx xx
1940 * result 00 00
1941 * reserved 00
1942 * bssid 02 2b 7b 0f 86 0e
1943 */
1944 lbs_join_post(priv, params, resp->bssid, capability);
1945
1946 out:
1947 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1948 return ret;
1949}
1950
1951
1952static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1953 struct cfg80211_ibss_params *params)
1954{
1955 struct lbs_private *priv = wiphy_priv(wiphy);
1956 int ret = 0;
1957 struct cfg80211_bss *bss;
1958 DECLARE_SSID_BUF(ssid_buf);
1959
1960 lbs_deb_enter(LBS_DEB_CFG80211);
1961
1962 if (!params->channel) {
1963 ret = -ENOTSUPP;
1964 goto out;
1965 }
1966
1967 ret = lbs_set_channel(priv, params->channel->hw_value);
1968 if (ret)
1969 goto out;
1970
1971 /* Search if someone is beaconing. This assumes that the
1972 * bss list is populated already */
1973 bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
1974 params->ssid, params->ssid_len,
1975 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
1976
1977 if (bss) {
1978 ret = lbs_ibss_join_existing(priv, params, bss);
1979 cfg80211_put_bss(bss);
1980 } else
1981 ret = lbs_ibss_start_new(priv, params);
1982
1983
1984 out:
1985 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1986 return ret;
1987}
1988
1989
1990static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1991{
1992 struct lbs_private *priv = wiphy_priv(wiphy);
1993 struct cmd_ds_802_11_ad_hoc_stop cmd;
1994 int ret = 0;
1995
1996 lbs_deb_enter(LBS_DEB_CFG80211);
1997
1998 memset(&cmd, 0, sizeof(cmd));
1999 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2000 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2001
2002 /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
2003 lbs_mac_event_disconnected(priv);
2004
2005 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2006 return ret;
2007}
2008
2009
2010
2011
Randy Dunlap8973a6e2011-04-26 15:25:29 -07002012/*
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302013 * Initialization
2014 */
2015
Holger Schurigff9fc792009-10-06 16:31:54 +02002016static struct cfg80211_ops lbs_cfg80211_ops = {
2017 .set_channel = lbs_cfg_set_channel,
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302018 .scan = lbs_cfg_scan,
2019 .connect = lbs_cfg_connect,
2020 .disconnect = lbs_cfg_disconnect,
2021 .add_key = lbs_cfg_add_key,
2022 .del_key = lbs_cfg_del_key,
2023 .set_default_key = lbs_cfg_set_default_key,
2024 .get_station = lbs_cfg_get_station,
2025 .dump_survey = lbs_get_survey,
2026 .change_virtual_intf = lbs_change_intf,
2027 .join_ibss = lbs_join_ibss,
2028 .leave_ibss = lbs_leave_ibss,
Holger Schurigff9fc792009-10-06 16:31:54 +02002029};
2030
2031
2032/*
2033 * At this time lbs_private *priv doesn't even exist, so we just allocate
2034 * memory and don't initialize the wiphy further. This is postponed until we
2035 * can talk to the firmware and happens at registration time in
2036 * lbs_cfg_wiphy_register().
2037 */
2038struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2039{
2040 int ret = 0;
2041 struct wireless_dev *wdev;
2042
2043 lbs_deb_enter(LBS_DEB_CFG80211);
2044
2045 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2046 if (!wdev) {
2047 dev_err(dev, "cannot allocate wireless device\n");
2048 return ERR_PTR(-ENOMEM);
2049 }
2050
2051 wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2052 if (!wdev->wiphy) {
2053 dev_err(dev, "cannot allocate wiphy\n");
2054 ret = -ENOMEM;
2055 goto err_wiphy_new;
2056 }
2057
2058 lbs_deb_leave(LBS_DEB_CFG80211);
2059 return wdev;
2060
2061 err_wiphy_new:
2062 kfree(wdev);
2063 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2064 return ERR_PTR(ret);
2065}
2066
2067
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302068static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2069{
2070 struct region_code_mapping {
2071 const char *cn;
2072 int code;
2073 };
2074
2075 /* Section 5.17.2 */
Joe Perches482e0392010-11-20 18:38:58 -08002076 static const struct region_code_mapping regmap[] = {
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302077 {"US ", 0x10}, /* US FCC */
2078 {"CA ", 0x20}, /* Canada */
2079 {"EU ", 0x30}, /* ETSI */
2080 {"ES ", 0x31}, /* Spain */
2081 {"FR ", 0x32}, /* France */
2082 {"JP ", 0x40}, /* Japan */
2083 };
2084 size_t i;
2085
2086 lbs_deb_enter(LBS_DEB_CFG80211);
2087
2088 for (i = 0; i < ARRAY_SIZE(regmap); i++)
2089 if (regmap[i].code == priv->regioncode) {
2090 regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2091 break;
2092 }
2093
2094 lbs_deb_leave(LBS_DEB_CFG80211);
2095}
2096
2097
Holger Schurigff9fc792009-10-06 16:31:54 +02002098/*
2099 * This function get's called after lbs_setup_firmware() determined the
2100 * firmware capabities. So we can setup the wiphy according to our
2101 * hardware/firmware.
2102 */
2103int lbs_cfg_register(struct lbs_private *priv)
2104{
2105 struct wireless_dev *wdev = priv->wdev;
2106 int ret;
2107
2108 lbs_deb_enter(LBS_DEB_CFG80211);
2109
2110 wdev->wiphy->max_scan_ssids = 1;
2111 wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2112
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302113 wdev->wiphy->interface_modes =
2114 BIT(NL80211_IFTYPE_STATION) |
2115 BIT(NL80211_IFTYPE_ADHOC);
2116 if (lbs_rtap_supported(priv))
2117 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
Holger Schurigff9fc792009-10-06 16:31:54 +02002118
Holger Schurigff9fc792009-10-06 16:31:54 +02002119 wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
2120
2121 /*
2122 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2123 * never seen a firmware without WPA
2124 */
2125 wdev->wiphy->cipher_suites = cipher_suites;
2126 wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
Kiran Divekar1047d5e2010-06-04 23:20:42 -07002127 wdev->wiphy->reg_notifier = lbs_reg_notifier;
Holger Schurigff9fc792009-10-06 16:31:54 +02002128
2129 ret = wiphy_register(wdev->wiphy);
2130 if (ret < 0)
Joe Perches0e4e06a2011-05-02 16:49:14 -07002131 pr_err("cannot register wiphy device\n");
Holger Schurigff9fc792009-10-06 16:31:54 +02002132
Daniel Mack73714002010-03-29 17:14:18 +02002133 priv->wiphy_registered = true;
2134
Holger Schurigff9fc792009-10-06 16:31:54 +02002135 ret = register_netdev(priv->dev);
2136 if (ret)
Joe Perches0e4e06a2011-05-02 16:49:14 -07002137 pr_err("cannot register network device\n");
Holger Schurigff9fc792009-10-06 16:31:54 +02002138
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302139 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2140
2141 lbs_cfg_set_regulatory_hint(priv);
2142
Holger Schurigff9fc792009-10-06 16:31:54 +02002143 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2144 return ret;
2145}
2146
Kiran Divekar1047d5e2010-06-04 23:20:42 -07002147int lbs_reg_notifier(struct wiphy *wiphy,
2148 struct regulatory_request *request)
2149{
Dan Williamscc4b9d32010-07-27 12:56:05 -07002150 struct lbs_private *priv = wiphy_priv(wiphy);
2151 int ret;
2152
Kiran Divekar1047d5e2010-06-04 23:20:42 -07002153 lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
2154 "callback for domain %c%c\n", request->alpha2[0],
2155 request->alpha2[1]);
2156
Dan Williamscc4b9d32010-07-27 12:56:05 -07002157 ret = lbs_set_11d_domain_info(priv, request, wiphy->bands);
Kiran Divekar1047d5e2010-06-04 23:20:42 -07002158
2159 lbs_deb_leave(LBS_DEB_CFG80211);
Dan Williamscc4b9d32010-07-27 12:56:05 -07002160 return ret;
Kiran Divekar1047d5e2010-06-04 23:20:42 -07002161}
Holger Schurigff9fc792009-10-06 16:31:54 +02002162
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302163void lbs_scan_deinit(struct lbs_private *priv)
2164{
2165 lbs_deb_enter(LBS_DEB_CFG80211);
2166 cancel_delayed_work_sync(&priv->scan_work);
2167}
2168
2169
Holger Schurigff9fc792009-10-06 16:31:54 +02002170void lbs_cfg_free(struct lbs_private *priv)
2171{
2172 struct wireless_dev *wdev = priv->wdev;
2173
2174 lbs_deb_enter(LBS_DEB_CFG80211);
2175
2176 if (!wdev)
2177 return;
2178
Daniel Mack73714002010-03-29 17:14:18 +02002179 if (priv->wiphy_registered)
Holger Schurigff9fc792009-10-06 16:31:54 +02002180 wiphy_unregister(wdev->wiphy);
Daniel Mack73714002010-03-29 17:14:18 +02002181
2182 if (wdev->wiphy)
Holger Schurigff9fc792009-10-06 16:31:54 +02002183 wiphy_free(wdev->wiphy);
Daniel Mack73714002010-03-29 17:14:18 +02002184
Holger Schurigff9fc792009-10-06 16:31:54 +02002185 kfree(wdev);
2186}