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