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