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