blob: 5110a771464bc0e248e8c1d114850d6cd71362c6 [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);
468 nr_sets = le16_to_cpu(resp->size);
469
470 /*
471 * The general layout of the scan response is described in chapter
472 * 5.7.1. Basically we have a common part, then any number of BSS
473 * descriptor sections. Finally we have section with the same number
474 * of TSFs.
475 *
476 * cmd_ds_802_11_scan_rsp
477 * cmd_header
478 * pos_size
479 * nr_sets
480 * bssdesc 1
481 * bssid
482 * rssi
483 * timestamp
484 * intvl
485 * capa
486 * IEs
487 * bssdesc 2
488 * bssdesc n
489 * MrvlIEtypes_TsfFimestamp_t
490 * TSF for BSS 1
491 * TSF for BSS 2
492 * TSF for BSS n
493 */
494
495 pos = scanresp->bssdesc_and_tlvbuffer;
496
497 tsfdesc = pos + bsssize;
498 tsfsize = 4 + 8 * scanresp->nr_sets;
499
500 /* Validity check: we expect a Marvell-Local TLV */
501 i = get_unaligned_le16(tsfdesc);
502 tsfdesc += 2;
503 if (i != TLV_TYPE_TSFTIMESTAMP)
504 goto done;
505 /* Validity check: the TLV holds TSF values with 8 bytes each, so
506 * the size in the TLV must match the nr_sets value */
507 i = get_unaligned_le16(tsfdesc);
508 tsfdesc += 2;
509 if (i / 8 != scanresp->nr_sets)
510 goto done;
511
512 for (i = 0; i < scanresp->nr_sets; i++) {
513 const u8 *bssid;
514 const u8 *ie;
515 int left;
516 int ielen;
517 int rssi;
518 u16 intvl;
519 u16 capa;
520 int chan_no = -1;
521 const u8 *ssid = NULL;
522 u8 ssid_len = 0;
523 DECLARE_SSID_BUF(ssid_buf);
524
525 int len = get_unaligned_le16(pos);
526 pos += 2;
527
528 /* BSSID */
529 bssid = pos;
530 pos += ETH_ALEN;
531 /* RSSI */
532 rssi = *pos++;
533 /* Packet time stamp */
534 pos += 8;
535 /* Beacon interval */
536 intvl = get_unaligned_le16(pos);
537 pos += 2;
538 /* Capabilities */
539 capa = get_unaligned_le16(pos);
540 pos += 2;
541
542 /* To find out the channel, we must parse the IEs */
543 ie = pos;
544 /* 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
545 interval, capabilities */
546 ielen = left = len - (6 + 1 + 8 + 2 + 2);
547 while (left >= 2) {
548 u8 id, elen;
549 id = *pos++;
550 elen = *pos++;
551 left -= 2;
552 if (elen > left || elen == 0)
553 goto done;
554 if (id == WLAN_EID_DS_PARAMS)
555 chan_no = *pos;
556 if (id == WLAN_EID_SSID) {
557 ssid = pos;
558 ssid_len = elen;
559 }
560 left -= elen;
561 pos += elen;
562 }
563
564 /* No channel, no luck */
565 if (chan_no != -1) {
566 struct wiphy *wiphy = priv->wdev->wiphy;
567 int freq = ieee80211_channel_to_frequency(chan_no);
568 struct ieee80211_channel *channel =
569 ieee80211_get_channel(wiphy, freq);
570
571 lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %s, "
572 "%d dBm\n",
573 bssid, capa, chan_no,
574 print_ssid(ssid_buf, ssid, ssid_len),
575 LBS_SCAN_RSSI_TO_MBM(rssi)/100);
576
577 if (channel ||
578 !(channel->flags & IEEE80211_CHAN_DISABLED))
579 cfg80211_inform_bss(wiphy, channel,
580 bssid, le64_to_cpu(*(__le64 *)tsfdesc),
581 capa, intvl, ie, ielen,
582 LBS_SCAN_RSSI_TO_MBM(rssi),
583 GFP_KERNEL);
584 }
585 tsfdesc += 8;
586 }
587 ret = 0;
588
589 done:
590 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
591 return ret;
592}
593
594
595/*
596 * Our scan command contains a TLV, consting of a SSID TLV, a channel list
597 * TLV and a rates TLV. Determine the maximum size of them:
598 */
599#define LBS_SCAN_MAX_CMD_SIZE \
600 (sizeof(struct cmd_ds_802_11_scan) \
601 + LBS_MAX_SSID_TLV_SIZE \
602 + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
603 + LBS_MAX_RATES_TLV_SIZE)
604
605/*
606 * Assumes priv->scan_req is initialized and valid
607 * Assumes priv->scan_channel is initialized
608 */
609static void lbs_scan_worker(struct work_struct *work)
610{
611 struct lbs_private *priv =
612 container_of(work, struct lbs_private, scan_work.work);
613 struct cmd_ds_802_11_scan *scan_cmd;
614 u8 *tlv; /* pointer into our current, growing TLV storage area */
615 int last_channel;
616 int running, carrier;
617
618 lbs_deb_enter(LBS_DEB_SCAN);
619
620 scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
621 if (scan_cmd == NULL)
622 goto out_no_scan_cmd;
623
624 /* prepare fixed part of scan command */
625 scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
626
627 /* stop network while we're away from our main channel */
628 running = !netif_queue_stopped(priv->dev);
629 carrier = netif_carrier_ok(priv->dev);
630 if (running)
631 netif_stop_queue(priv->dev);
632 if (carrier)
633 netif_carrier_off(priv->dev);
634
635 /* prepare fixed part of scan command */
636 tlv = scan_cmd->tlvbuffer;
637
638 /* add SSID TLV */
639 if (priv->scan_req->n_ssids)
640 tlv += lbs_add_ssid_tlv(tlv,
641 priv->scan_req->ssids[0].ssid,
642 priv->scan_req->ssids[0].ssid_len);
643
644 /* add channel TLVs */
645 last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
646 if (last_channel > priv->scan_req->n_channels)
647 last_channel = priv->scan_req->n_channels;
648 tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
649 priv->scan_req->n_ssids);
650
651 /* add rates TLV */
652 tlv += lbs_add_supported_rates_tlv(tlv);
653
654 if (priv->scan_channel < priv->scan_req->n_channels) {
655 cancel_delayed_work(&priv->scan_work);
656 queue_delayed_work(priv->work_thread, &priv->scan_work,
657 msecs_to_jiffies(300));
658 }
659
660 /* This is the final data we are about to send */
661 scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
662 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
663 sizeof(*scan_cmd));
664 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
665 tlv - scan_cmd->tlvbuffer);
666
667 __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
668 le16_to_cpu(scan_cmd->hdr.size),
669 lbs_ret_scan, 0);
670
671 if (priv->scan_channel >= priv->scan_req->n_channels) {
672 /* Mark scan done */
673 cfg80211_scan_done(priv->scan_req, false);
674 priv->scan_req = NULL;
675 }
676
677 /* Restart network */
678 if (carrier)
679 netif_carrier_on(priv->dev);
680 if (running && !priv->tx_pending_len)
681 netif_wake_queue(priv->dev);
682
683 kfree(scan_cmd);
684
685 out_no_scan_cmd:
686 lbs_deb_leave(LBS_DEB_SCAN);
687}
688
689
690static int lbs_cfg_scan(struct wiphy *wiphy,
691 struct net_device *dev,
692 struct cfg80211_scan_request *request)
693{
694 struct lbs_private *priv = wiphy_priv(wiphy);
695 int ret = 0;
696
697 lbs_deb_enter(LBS_DEB_CFG80211);
698
699 if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
700 /* old scan request not yet processed */
701 ret = -EAGAIN;
702 goto out;
703 }
704
705 lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
706 request->n_ssids, request->n_channels, request->ie_len);
707
708 priv->scan_channel = 0;
709 queue_delayed_work(priv->work_thread, &priv->scan_work,
710 msecs_to_jiffies(50));
711
712 if (priv->surpriseremoved)
713 ret = -EIO;
714
715 priv->scan_req = request;
Holger Schurigff9fc792009-10-06 16:31:54 +0200716
717 out:
718 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
719 return ret;
720}
721
722
723
724
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530725/***************************************************************************
726 * Events
727 */
728
729void lbs_send_disconnect_notification(struct lbs_private *priv)
730{
731 lbs_deb_enter(LBS_DEB_CFG80211);
732
733 cfg80211_disconnected(priv->dev,
734 0,
735 NULL, 0,
736 GFP_KERNEL);
737
738 lbs_deb_leave(LBS_DEB_CFG80211);
739}
740
741void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
742{
743 lbs_deb_enter(LBS_DEB_CFG80211);
744
745 cfg80211_michael_mic_failure(priv->dev,
746 priv->assoc_bss,
747 event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
748 NL80211_KEYTYPE_GROUP :
749 NL80211_KEYTYPE_PAIRWISE,
750 -1,
751 NULL,
752 GFP_KERNEL);
753
754 lbs_deb_leave(LBS_DEB_CFG80211);
755}
756
757
758
759
760/***************************************************************************
761 * Connect/disconnect
762 */
763
764
765/*
766 * This removes all WEP keys
767 */
768static int lbs_remove_wep_keys(struct lbs_private *priv)
769{
770 struct cmd_ds_802_11_set_wep cmd;
771 int ret;
772
773 lbs_deb_enter(LBS_DEB_CFG80211);
774
775 memset(&cmd, 0, sizeof(cmd));
776 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
777 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
778 cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
779
780 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
781
782 lbs_deb_leave(LBS_DEB_CFG80211);
783 return ret;
784}
785
786/*
787 * Set WEP keys
788 */
789static int lbs_set_wep_keys(struct lbs_private *priv)
790{
791 struct cmd_ds_802_11_set_wep cmd;
792 int i;
793 int ret;
794
795 lbs_deb_enter(LBS_DEB_CFG80211);
796
797 /*
798 * command 13 00
799 * size 50 00
800 * sequence xx xx
801 * result 00 00
802 * action 02 00 ACT_ADD
803 * transmit key 00 00
804 * type for key 1 01 WEP40
805 * type for key 2 00
806 * type for key 3 00
807 * type for key 4 00
808 * key 1 39 39 39 39 39 00 00 00
809 * 00 00 00 00 00 00 00 00
810 * key 2 00 00 00 00 00 00 00 00
811 * 00 00 00 00 00 00 00 00
812 * key 3 00 00 00 00 00 00 00 00
813 * 00 00 00 00 00 00 00 00
814 * key 4 00 00 00 00 00 00 00 00
815 */
816 if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
817 priv->wep_key_len[2] || priv->wep_key_len[3]) {
818 /* Only set wep keys if we have at least one of them */
819 memset(&cmd, 0, sizeof(cmd));
820 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
821 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
822 cmd.action = cpu_to_le16(CMD_ACT_ADD);
823
824 for (i = 0; i < 4; i++) {
825 switch (priv->wep_key_len[i]) {
826 case WLAN_KEY_LEN_WEP40:
827 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
828 break;
829 case WLAN_KEY_LEN_WEP104:
830 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
831 break;
832 default:
833 cmd.keytype[i] = 0;
834 break;
835 }
836 memcpy(cmd.keymaterial[i], priv->wep_key[i],
837 priv->wep_key_len[i]);
838 }
839
840 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
841 } else {
842 /* Otherwise remove all wep keys */
843 ret = lbs_remove_wep_keys(priv);
844 }
845
846 lbs_deb_leave(LBS_DEB_CFG80211);
847 return ret;
848}
849
850
851/*
852 * Enable/Disable RSN status
853 */
854static int lbs_enable_rsn(struct lbs_private *priv, int enable)
855{
856 struct cmd_ds_802_11_enable_rsn cmd;
857 int ret;
858
859 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
860
861 /*
862 * cmd 2f 00
863 * size 0c 00
864 * sequence xx xx
865 * result 00 00
866 * action 01 00 ACT_SET
867 * enable 01 00
868 */
869 memset(&cmd, 0, sizeof(cmd));
870 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
871 cmd.action = cpu_to_le16(CMD_ACT_SET);
872 cmd.enable = cpu_to_le16(enable);
873
874 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
875
876 lbs_deb_leave(LBS_DEB_CFG80211);
877 return ret;
878}
879
880
881/*
882 * Set WPA/WPA key material
883 */
884
885/* like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
886 * get rid of WEXT, this should go into host.h */
887
888struct cmd_key_material {
889 struct cmd_header hdr;
890
891 __le16 action;
892 struct MrvlIEtype_keyParamSet param;
John W. Linvillebeabe912010-07-14 10:37:03 -0400893} __packed;
Kiran Divekare86dc1c2010-06-14 22:01:26 +0530894
895static int lbs_set_key_material(struct lbs_private *priv,
896 int key_type,
897 int key_info,
898 u8 *key, u16 key_len)
899{
900 struct cmd_key_material cmd;
901 int ret;
902
903 lbs_deb_enter(LBS_DEB_CFG80211);
904
905 /*
906 * Example for WPA (TKIP):
907 *
908 * cmd 5e 00
909 * size 34 00
910 * sequence xx xx
911 * result 00 00
912 * action 01 00
913 * TLV type 00 01 key param
914 * length 00 26
915 * key type 01 00 TKIP
916 * key info 06 00 UNICAST | ENABLED
917 * key len 20 00
918 * key 32 bytes
919 */
920 memset(&cmd, 0, sizeof(cmd));
921 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
922 cmd.action = cpu_to_le16(CMD_ACT_SET);
923 cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
924 cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
925 cmd.param.keytypeid = cpu_to_le16(key_type);
926 cmd.param.keyinfo = cpu_to_le16(key_info);
927 cmd.param.keylen = cpu_to_le16(key_len);
928 if (key && key_len)
929 memcpy(cmd.param.key, key, key_len);
930
931 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
932
933 lbs_deb_leave(LBS_DEB_CFG80211);
934 return ret;
935}
936
937
938/*
939 * Sets the auth type (open, shared, etc) in the firmware. That
940 * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
941 * command doesn't send an authentication frame at all, it just
942 * stores the auth_type.
943 */
944static int lbs_set_authtype(struct lbs_private *priv,
945 struct cfg80211_connect_params *sme)
946{
947 struct cmd_ds_802_11_authenticate cmd;
948 int ret;
949
950 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
951
952 /*
953 * cmd 11 00
954 * size 19 00
955 * sequence xx xx
956 * result 00 00
957 * BSS id 00 13 19 80 da 30
958 * auth type 00
959 * reserved 00 00 00 00 00 00 00 00 00 00
960 */
961 memset(&cmd, 0, sizeof(cmd));
962 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
963 if (sme->bssid)
964 memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
965 /* convert auth_type */
966 ret = lbs_auth_to_authtype(sme->auth_type);
967 if (ret < 0)
968 goto done;
969
970 cmd.authtype = ret;
971 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
972
973 done:
974 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
975 return ret;
976}
977
978
979/*
980 * Create association request
981 */
982#define LBS_ASSOC_MAX_CMD_SIZE \
983 (sizeof(struct cmd_ds_802_11_associate) \
984 - 512 /* cmd_ds_802_11_associate.iebuf */ \
985 + LBS_MAX_SSID_TLV_SIZE \
986 + LBS_MAX_CHANNEL_TLV_SIZE \
987 + LBS_MAX_CF_PARAM_TLV_SIZE \
988 + LBS_MAX_AUTH_TYPE_TLV_SIZE \
989 + LBS_MAX_WPA_TLV_SIZE)
990
991static int lbs_associate(struct lbs_private *priv,
992 struct cfg80211_bss *bss,
993 struct cfg80211_connect_params *sme)
994{
995 struct cmd_ds_802_11_associate_response *resp;
996 struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
997 GFP_KERNEL);
998 const u8 *ssid_eid;
999 size_t len, resp_ie_len;
1000 int status;
1001 int ret;
1002 u8 *pos = &(cmd->iebuf[0]);
1003
1004 lbs_deb_enter(LBS_DEB_CFG80211);
1005
1006 if (!cmd) {
1007 ret = -ENOMEM;
1008 goto done;
1009 }
1010
1011 /*
1012 * cmd 50 00
1013 * length 34 00
1014 * sequence xx xx
1015 * result 00 00
1016 * BSS id 00 13 19 80 da 30
1017 * capabilities 11 00
1018 * listen interval 0a 00
1019 * beacon interval 00 00
1020 * DTIM period 00
1021 * TLVs xx (up to 512 bytes)
1022 */
1023 cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1024
1025 /* Fill in static fields */
1026 memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1027 cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1028 cmd->capability = cpu_to_le16(bss->capability);
1029
1030 /* add SSID TLV */
1031 ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1032 if (ssid_eid)
1033 pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
1034 else
1035 lbs_deb_assoc("no SSID\n");
1036
1037 /* add DS param TLV */
1038 if (bss->channel)
1039 pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1040 else
1041 lbs_deb_assoc("no channel\n");
1042
1043 /* add (empty) CF param TLV */
1044 pos += lbs_add_cf_param_tlv(pos);
1045
1046 /* add rates TLV */
1047 pos += lbs_add_common_rates_tlv(pos, bss);
1048
1049 /* add auth type TLV */
1050 if (priv->fwrelease >= 0x09000000)
1051 pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1052
1053 /* add WPA/WPA2 TLV */
1054 if (sme->ie && sme->ie_len)
1055 pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1056
1057 len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
1058 (u16)(pos - (u8 *) &cmd->iebuf);
1059 cmd->hdr.size = cpu_to_le16(len);
1060
1061 /* store for later use */
1062 memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1063
1064 ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1065 if (ret)
1066 goto done;
1067
1068
1069 /* generate connect message to cfg80211 */
1070
1071 resp = (void *) cmd; /* recast for easier field access */
1072 status = le16_to_cpu(resp->statuscode);
1073
1074 /* Convert statis code of old firmware */
1075 if (priv->fwrelease < 0x09000000)
1076 switch (status) {
1077 case 0:
1078 break;
1079 case 1:
1080 lbs_deb_assoc("invalid association parameters\n");
1081 status = WLAN_STATUS_CAPS_UNSUPPORTED;
1082 break;
1083 case 2:
1084 lbs_deb_assoc("timer expired while waiting for AP\n");
1085 status = WLAN_STATUS_AUTH_TIMEOUT;
1086 break;
1087 case 3:
1088 lbs_deb_assoc("association refused by AP\n");
1089 status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1090 break;
1091 case 4:
1092 lbs_deb_assoc("authentication refused by AP\n");
1093 status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1094 break;
1095 default:
1096 lbs_deb_assoc("association failure %d\n", status);
1097 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1098 }
1099
1100 lbs_deb_assoc("status %d, capability 0x%04x\n", status,
1101 le16_to_cpu(resp->capability));
1102
1103 resp_ie_len = le16_to_cpu(resp->hdr.size)
1104 - sizeof(resp->hdr)
1105 - 6;
1106 cfg80211_connect_result(priv->dev,
1107 priv->assoc_bss,
1108 sme->ie, sme->ie_len,
1109 resp->iebuf, resp_ie_len,
1110 status,
1111 GFP_KERNEL);
1112
1113 if (status == 0) {
1114 /* TODO: get rid of priv->connect_status */
1115 priv->connect_status = LBS_CONNECTED;
1116 netif_carrier_on(priv->dev);
1117 if (!priv->tx_pending_len)
1118 netif_tx_wake_all_queues(priv->dev);
1119 }
1120
1121
1122done:
1123 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1124 return ret;
1125}
1126
1127
1128
1129static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1130 struct cfg80211_connect_params *sme)
1131{
1132 struct lbs_private *priv = wiphy_priv(wiphy);
1133 struct cfg80211_bss *bss = NULL;
1134 int ret = 0;
1135 u8 preamble = RADIO_PREAMBLE_SHORT;
1136
1137 lbs_deb_enter(LBS_DEB_CFG80211);
1138
1139 if (sme->bssid) {
1140 bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1141 sme->ssid, sme->ssid_len,
1142 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
1143 } else {
1144 /*
1145 * Here we have an impedance mismatch. The firmware command
1146 * CMD_802_11_ASSOCIATE always needs a BSSID, it cannot
1147 * connect otherwise. However, for the connect-API of
1148 * cfg80211 the bssid is purely optional. We don't get one,
1149 * except the user specifies one on the "iw" command line.
1150 *
1151 * If we don't got one, we could initiate a scan and look
1152 * for the best matching cfg80211_bss entry.
1153 *
1154 * Or, better yet, net/wireless/sme.c get's rewritten into
1155 * something more generally useful.
1156 */
1157 lbs_pr_err("TODO: no BSS specified\n");
1158 ret = -ENOTSUPP;
1159 goto done;
1160 }
1161
1162
1163 if (!bss) {
1164 lbs_pr_err("assicate: bss %pM not in scan results\n",
1165 sme->bssid);
1166 ret = -ENOENT;
1167 goto done;
1168 }
1169 lbs_deb_assoc("trying %pM", sme->bssid);
1170 lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1171 sme->crypto.cipher_group,
1172 sme->key_idx, sme->key_len);
1173
1174 /* As this is a new connection, clear locally stored WEP keys */
1175 priv->wep_tx_key = 0;
1176 memset(priv->wep_key, 0, sizeof(priv->wep_key));
1177 memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1178
1179 /* set/remove WEP keys */
1180 switch (sme->crypto.cipher_group) {
1181 case WLAN_CIPHER_SUITE_WEP40:
1182 case WLAN_CIPHER_SUITE_WEP104:
1183 /* Store provided WEP keys in priv-> */
1184 priv->wep_tx_key = sme->key_idx;
1185 priv->wep_key_len[sme->key_idx] = sme->key_len;
1186 memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1187 /* Set WEP keys and WEP mode */
1188 lbs_set_wep_keys(priv);
1189 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1190 lbs_set_mac_control(priv);
1191 /* No RSN mode for WEP */
1192 lbs_enable_rsn(priv, 0);
1193 break;
1194 case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1195 /*
1196 * If we don't have no WEP, no WPA and no WPA2,
1197 * we remove all keys like in the WPA/WPA2 setup,
1198 * we just don't set RSN.
1199 *
1200 * Therefore: fall-throught
1201 */
1202 case WLAN_CIPHER_SUITE_TKIP:
1203 case WLAN_CIPHER_SUITE_CCMP:
1204 /* Remove WEP keys and WEP mode */
1205 lbs_remove_wep_keys(priv);
1206 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1207 lbs_set_mac_control(priv);
1208
1209 /* clear the WPA/WPA2 keys */
1210 lbs_set_key_material(priv,
1211 KEY_TYPE_ID_WEP, /* doesn't matter */
1212 KEY_INFO_WPA_UNICAST,
1213 NULL, 0);
1214 lbs_set_key_material(priv,
1215 KEY_TYPE_ID_WEP, /* doesn't matter */
1216 KEY_INFO_WPA_MCAST,
1217 NULL, 0);
1218 /* RSN mode for WPA/WPA2 */
1219 lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1220 break;
1221 default:
1222 lbs_pr_err("unsupported cipher group 0x%x\n",
1223 sme->crypto.cipher_group);
1224 ret = -ENOTSUPP;
1225 goto done;
1226 }
1227
1228 lbs_set_authtype(priv, sme);
1229 lbs_set_radio(priv, preamble, 1);
1230
1231 /* Do the actual association */
1232 lbs_associate(priv, bss, sme);
1233
1234 done:
1235 if (bss)
1236 cfg80211_put_bss(bss);
1237 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1238 return ret;
1239}
1240
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301241static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1242 u16 reason_code)
1243{
1244 struct lbs_private *priv = wiphy_priv(wiphy);
1245 struct cmd_ds_802_11_deauthenticate cmd;
1246
1247 lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
1248
1249 /* store for lbs_cfg_ret_disconnect() */
1250 priv->disassoc_reason = reason_code;
1251
1252 memset(&cmd, 0, sizeof(cmd));
1253 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1254 /* Mildly ugly to use a locally store my own BSSID ... */
1255 memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1256 cmd.reasoncode = cpu_to_le16(reason_code);
1257
Kiran Divekare4fe4ea2010-06-04 23:20:37 -07001258 if (lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd))
1259 return -EFAULT;
1260
1261 cfg80211_disconnected(priv->dev,
1262 priv->disassoc_reason,
1263 NULL, 0,
1264 GFP_KERNEL);
1265 priv->connect_status = LBS_DISCONNECTED;
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301266
1267 return 0;
1268}
1269
1270
1271static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1272 struct net_device *netdev,
1273 u8 key_index)
1274{
1275 struct lbs_private *priv = wiphy_priv(wiphy);
1276
1277 lbs_deb_enter(LBS_DEB_CFG80211);
1278
1279 if (key_index != priv->wep_tx_key) {
1280 lbs_deb_assoc("set_default_key: to %d\n", key_index);
1281 priv->wep_tx_key = key_index;
1282 lbs_set_wep_keys(priv);
1283 }
1284
1285 return 0;
1286}
1287
1288
1289static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1290 u8 idx, const u8 *mac_addr,
1291 struct key_params *params)
1292{
1293 struct lbs_private *priv = wiphy_priv(wiphy);
1294 u16 key_info;
1295 u16 key_type;
1296 int ret = 0;
1297
1298 lbs_deb_enter(LBS_DEB_CFG80211);
1299
1300 lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1301 params->cipher, mac_addr);
1302 lbs_deb_assoc("add_key: key index %d, key len %d\n",
1303 idx, params->key_len);
1304 if (params->key_len)
1305 lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1306 params->key, params->key_len);
1307
1308 lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1309 if (params->seq_len)
1310 lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1311 params->seq, params->seq_len);
1312
1313 switch (params->cipher) {
1314 case WLAN_CIPHER_SUITE_WEP40:
1315 case WLAN_CIPHER_SUITE_WEP104:
1316 /* actually compare if something has changed ... */
1317 if ((priv->wep_key_len[idx] != params->key_len) ||
1318 memcmp(priv->wep_key[idx],
1319 params->key, params->key_len) != 0) {
1320 priv->wep_key_len[idx] = params->key_len;
1321 memcpy(priv->wep_key[idx],
1322 params->key, params->key_len);
1323 lbs_set_wep_keys(priv);
1324 }
1325 break;
1326 case WLAN_CIPHER_SUITE_TKIP:
1327 case WLAN_CIPHER_SUITE_CCMP:
1328 key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1329 ? KEY_INFO_WPA_UNICAST
1330 : KEY_INFO_WPA_MCAST);
1331 key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1332 ? KEY_TYPE_ID_TKIP
1333 : KEY_TYPE_ID_AES;
1334 lbs_set_key_material(priv,
1335 key_type,
1336 key_info,
1337 params->key, params->key_len);
1338 break;
1339 default:
1340 lbs_pr_err("unhandled cipher 0x%x\n", params->cipher);
1341 ret = -ENOTSUPP;
1342 break;
1343 }
1344
1345 return ret;
1346}
1347
1348
1349static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1350 u8 key_index, const u8 *mac_addr)
1351{
1352
1353 lbs_deb_enter(LBS_DEB_CFG80211);
1354
1355 lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1356 key_index, mac_addr);
1357
1358#ifdef TODO
1359 struct lbs_private *priv = wiphy_priv(wiphy);
1360 /*
1361 * I think can keep this a NO-OP, because:
1362
1363 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1364 * - neither "iw" nor "wpa_supplicant" won't call this during
1365 * an ongoing connection
1366 * - TODO: but I have to check if this is still true when
1367 * I set the AP to periodic re-keying
1368 * - we've not kzallec() something when we've added a key at
1369 * lbs_cfg_connect() or lbs_cfg_add_key().
1370 *
1371 * This causes lbs_cfg_del_key() only called at disconnect time,
1372 * where we'd just waste time deleting a key that is not going
1373 * to be used anyway.
1374 */
1375 if (key_index < 3 && priv->wep_key_len[key_index]) {
1376 priv->wep_key_len[key_index] = 0;
1377 lbs_set_wep_keys(priv);
1378 }
1379#endif
1380
1381 return 0;
1382}
1383
1384
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301385/***************************************************************************
1386 * Get station
1387 */
1388
1389/*
1390 * Returns the signal or 0 in case of an error.
1391 */
1392
1393/* like "struct cmd_ds_802_11_rssi", but with cmd_header. Once we get rid
1394 * of WEXT, this should go into host.h */
1395struct cmd_rssi {
1396 struct cmd_header hdr;
1397
1398 __le16 n_or_snr;
1399 __le16 nf;
1400 __le16 avg_snr;
1401 __le16 avg_nf;
John W. Linvillebeabe912010-07-14 10:37:03 -04001402} __packed;
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301403
1404static int lbs_get_signal(struct lbs_private *priv, s8 *signal, s8 *noise)
1405{
1406 struct cmd_rssi cmd;
1407 int ret;
1408
1409 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1410 cmd.n_or_snr = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
1411 ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
1412
1413 if (ret == 0) {
1414 *signal = CAL_RSSI(le16_to_cpu(cmd.n_or_snr),
1415 le16_to_cpu(cmd.nf));
1416 *noise = CAL_NF(le16_to_cpu(cmd.nf));
1417 }
1418 return ret;
1419}
1420
1421
1422static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1423 u8 *mac, struct station_info *sinfo)
1424{
1425 struct lbs_private *priv = wiphy_priv(wiphy);
1426 s8 signal, noise;
1427 int ret;
1428 size_t i;
1429
1430 lbs_deb_enter(LBS_DEB_CFG80211);
1431
1432 sinfo->filled |= STATION_INFO_TX_BYTES |
1433 STATION_INFO_TX_PACKETS |
1434 STATION_INFO_RX_BYTES |
1435 STATION_INFO_RX_PACKETS;
1436 sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1437 sinfo->tx_packets = priv->dev->stats.tx_packets;
1438 sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1439 sinfo->rx_packets = priv->dev->stats.rx_packets;
1440
1441 /* Get current RSSI */
1442 ret = lbs_get_signal(priv, &signal, &noise);
1443 if (ret == 0) {
1444 sinfo->signal = signal;
1445 sinfo->filled |= STATION_INFO_SIGNAL;
1446 }
1447
1448 /* Convert priv->cur_rate from hw_value to NL80211 value */
1449 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1450 if (priv->cur_rate == lbs_rates[i].hw_value) {
1451 sinfo->txrate.legacy = lbs_rates[i].bitrate;
1452 sinfo->filled |= STATION_INFO_TX_BITRATE;
1453 break;
1454 }
1455 }
1456
1457 return 0;
1458}
1459
1460
1461
1462
1463/***************************************************************************
1464 * "Site survey", here just current channel and noise level
1465 */
1466
1467static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
1468 int idx, struct survey_info *survey)
1469{
1470 struct lbs_private *priv = wiphy_priv(wiphy);
1471 s8 signal, noise;
1472 int ret;
1473
1474 if (idx != 0)
1475 ret = -ENOENT;
1476
1477 lbs_deb_enter(LBS_DEB_CFG80211);
1478
1479 survey->channel = ieee80211_get_channel(wiphy,
1480 ieee80211_channel_to_frequency(priv->channel));
1481
1482 ret = lbs_get_signal(priv, &signal, &noise);
1483 if (ret == 0) {
1484 survey->filled = SURVEY_INFO_NOISE_DBM;
1485 survey->noise = noise;
1486 }
1487
1488 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1489 return ret;
1490}
1491
1492
1493
1494
1495/***************************************************************************
1496 * Change interface
1497 */
1498
1499static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1500 enum nl80211_iftype type, u32 *flags,
1501 struct vif_params *params)
1502{
1503 struct lbs_private *priv = wiphy_priv(wiphy);
1504 int ret = 0;
1505
1506 lbs_deb_enter(LBS_DEB_CFG80211);
1507
1508 switch (type) {
1509 case NL80211_IFTYPE_MONITOR:
Dan Williamsa45b6f42010-07-27 12:54:34 -07001510 ret = lbs_set_monitor_mode(priv, 1);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301511 break;
1512 case NL80211_IFTYPE_STATION:
1513 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Dan Williamsa45b6f42010-07-27 12:54:34 -07001514 ret = lbs_set_monitor_mode(priv, 0);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301515 if (!ret)
1516 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1);
1517 break;
1518 case NL80211_IFTYPE_ADHOC:
1519 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
Dan Williamsa45b6f42010-07-27 12:54:34 -07001520 ret = lbs_set_monitor_mode(priv, 0);
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301521 if (!ret)
1522 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2);
1523 break;
1524 default:
1525 ret = -ENOTSUPP;
1526 }
1527
1528 if (!ret)
1529 priv->wdev->iftype = type;
1530
1531 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1532 return ret;
1533}
1534
1535
1536
1537/***************************************************************************
1538 * IBSS (Ad-Hoc)
1539 */
1540
1541/* The firmware needs the following bits masked out of the beacon-derived
1542 * capability field when associating/joining to a BSS:
1543 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1544 */
1545#define CAPINFO_MASK (~(0xda00))
1546
1547
1548static void lbs_join_post(struct lbs_private *priv,
1549 struct cfg80211_ibss_params *params,
1550 u8 *bssid, u16 capability)
1551{
1552 u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1553 2 + 4 + /* basic rates */
1554 2 + 1 + /* DS parameter */
1555 2 + 2 + /* atim */
1556 2 + 8]; /* extended rates */
1557 u8 *fake = fake_ie;
1558
1559 lbs_deb_enter(LBS_DEB_CFG80211);
1560
1561 /*
1562 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1563 * the real IE from the firmware. So we fabricate a fake IE based on
1564 * what the firmware actually sends (sniffed with wireshark).
1565 */
1566 /* Fake SSID IE */
1567 *fake++ = WLAN_EID_SSID;
1568 *fake++ = params->ssid_len;
1569 memcpy(fake, params->ssid, params->ssid_len);
1570 fake += params->ssid_len;
1571 /* Fake supported basic rates IE */
1572 *fake++ = WLAN_EID_SUPP_RATES;
1573 *fake++ = 4;
1574 *fake++ = 0x82;
1575 *fake++ = 0x84;
1576 *fake++ = 0x8b;
1577 *fake++ = 0x96;
1578 /* Fake DS channel IE */
1579 *fake++ = WLAN_EID_DS_PARAMS;
1580 *fake++ = 1;
1581 *fake++ = params->channel->hw_value;
1582 /* Fake IBSS params IE */
1583 *fake++ = WLAN_EID_IBSS_PARAMS;
1584 *fake++ = 2;
1585 *fake++ = 0; /* ATIM=0 */
1586 *fake++ = 0;
1587 /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1588 * but I don't know how this could be checked */
1589 *fake++ = WLAN_EID_EXT_SUPP_RATES;
1590 *fake++ = 8;
1591 *fake++ = 0x0c;
1592 *fake++ = 0x12;
1593 *fake++ = 0x18;
1594 *fake++ = 0x24;
1595 *fake++ = 0x30;
1596 *fake++ = 0x48;
1597 *fake++ = 0x60;
1598 *fake++ = 0x6c;
1599 lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1600
1601 cfg80211_inform_bss(priv->wdev->wiphy,
1602 params->channel,
1603 bssid,
1604 0,
1605 capability,
1606 params->beacon_interval,
1607 fake_ie, fake - fake_ie,
1608 0, GFP_KERNEL);
Kiran Divekare4fe4ea2010-06-04 23:20:37 -07001609
1610 memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
1611 priv->wdev->ssid_len = params->ssid_len;
1612
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301613 cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
1614
1615 /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1616 priv->connect_status = LBS_CONNECTED;
1617 netif_carrier_on(priv->dev);
1618 if (!priv->tx_pending_len)
1619 netif_wake_queue(priv->dev);
1620
1621 lbs_deb_leave(LBS_DEB_CFG80211);
1622}
1623
1624static int lbs_ibss_join_existing(struct lbs_private *priv,
1625 struct cfg80211_ibss_params *params,
1626 struct cfg80211_bss *bss)
1627{
1628 const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1629 struct cmd_ds_802_11_ad_hoc_join cmd;
1630 u8 preamble = RADIO_PREAMBLE_SHORT;
1631 int ret = 0;
1632
1633 lbs_deb_enter(LBS_DEB_CFG80211);
1634
1635 /* TODO: set preamble based on scan result */
1636 ret = lbs_set_radio(priv, preamble, 1);
1637 if (ret)
1638 goto out;
1639
1640 /*
1641 * Example CMD_802_11_AD_HOC_JOIN command:
1642 *
1643 * command 2c 00 CMD_802_11_AD_HOC_JOIN
1644 * size 65 00
1645 * sequence xx xx
1646 * result 00 00
1647 * bssid 02 27 27 97 2f 96
1648 * ssid 49 42 53 53 00 00 00 00
1649 * 00 00 00 00 00 00 00 00
1650 * 00 00 00 00 00 00 00 00
1651 * 00 00 00 00 00 00 00 00
1652 * type 02 CMD_BSS_TYPE_IBSS
1653 * beacon period 64 00
1654 * dtim period 00
1655 * timestamp 00 00 00 00 00 00 00 00
1656 * localtime 00 00 00 00 00 00 00 00
1657 * IE DS 03
1658 * IE DS len 01
1659 * IE DS channel 01
1660 * reserveed 00 00 00 00
1661 * IE IBSS 06
1662 * IE IBSS len 02
1663 * IE IBSS atim 00 00
1664 * reserved 00 00 00 00
1665 * capability 02 00
1666 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1667 * fail timeout ff 00
1668 * probe delay 00 00
1669 */
1670 memset(&cmd, 0, sizeof(cmd));
1671 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1672
1673 memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1674 memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1675 cmd.bss.type = CMD_BSS_TYPE_IBSS;
1676 cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1677 cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1678 cmd.bss.ds.header.len = 1;
1679 cmd.bss.ds.channel = params->channel->hw_value;
1680 cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1681 cmd.bss.ibss.header.len = 2;
1682 cmd.bss.ibss.atimwindow = 0;
1683 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1684
1685 /* set rates to the intersection of our rates and the rates in the
1686 bss */
1687 if (!rates_eid) {
1688 lbs_add_rates(cmd.bss.rates);
1689 } else {
1690 int hw, i;
1691 u8 rates_max = rates_eid[1];
1692 u8 *rates = cmd.bss.rates;
1693 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1694 u8 hw_rate = lbs_rates[hw].bitrate / 5;
1695 for (i = 0; i < rates_max; i++) {
1696 if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1697 u8 rate = rates_eid[i+2];
1698 if (rate == 0x02 || rate == 0x04 ||
1699 rate == 0x0b || rate == 0x16)
1700 rate |= 0x80;
1701 *rates++ = rate;
1702 }
1703 }
1704 }
1705 }
1706
1707 /* Only v8 and below support setting this */
1708 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1709 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1710 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1711 }
1712 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1713 if (ret)
1714 goto out;
1715
1716 /*
1717 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1718 *
1719 * response 2c 80
1720 * size 09 00
1721 * sequence xx xx
1722 * result 00 00
1723 * reserved 00
1724 */
1725 lbs_join_post(priv, params, bss->bssid, bss->capability);
1726
1727 out:
1728 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1729 return ret;
1730}
1731
1732
1733
1734static int lbs_ibss_start_new(struct lbs_private *priv,
1735 struct cfg80211_ibss_params *params)
1736{
1737 struct cmd_ds_802_11_ad_hoc_start cmd;
1738 struct cmd_ds_802_11_ad_hoc_result *resp =
1739 (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1740 u8 preamble = RADIO_PREAMBLE_SHORT;
1741 int ret = 0;
1742 u16 capability;
1743
1744 lbs_deb_enter(LBS_DEB_CFG80211);
1745
1746 ret = lbs_set_radio(priv, preamble, 1);
1747 if (ret)
1748 goto out;
1749
1750 /*
1751 * Example CMD_802_11_AD_HOC_START command:
1752 *
1753 * command 2b 00 CMD_802_11_AD_HOC_START
1754 * size b1 00
1755 * sequence xx xx
1756 * result 00 00
1757 * ssid 54 45 53 54 00 00 00 00
1758 * 00 00 00 00 00 00 00 00
1759 * 00 00 00 00 00 00 00 00
1760 * 00 00 00 00 00 00 00 00
1761 * bss type 02
1762 * beacon period 64 00
1763 * dtim period 00
1764 * IE IBSS 06
1765 * IE IBSS len 02
1766 * IE IBSS atim 00 00
1767 * reserved 00 00 00 00
1768 * IE DS 03
1769 * IE DS len 01
1770 * IE DS channel 01
1771 * reserved 00 00 00 00
1772 * probe delay 00 00
1773 * capability 02 00
1774 * rates 82 84 8b 96 (basic rates with have bit 7 set)
1775 * 0c 12 18 24 30 48 60 6c
1776 * padding 100 bytes
1777 */
1778 memset(&cmd, 0, sizeof(cmd));
1779 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1780 memcpy(cmd.ssid, params->ssid, params->ssid_len);
1781 cmd.bsstype = CMD_BSS_TYPE_IBSS;
1782 cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1783 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1784 cmd.ibss.header.len = 2;
1785 cmd.ibss.atimwindow = 0;
1786 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1787 cmd.ds.header.len = 1;
1788 cmd.ds.channel = params->channel->hw_value;
1789 /* Only v8 and below support setting probe delay */
1790 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1791 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1792 /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1793 capability = WLAN_CAPABILITY_IBSS;
1794 cmd.capability = cpu_to_le16(capability);
1795 lbs_add_rates(cmd.rates);
1796
1797
1798 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1799 if (ret)
1800 goto out;
1801
1802 /*
1803 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1804 *
1805 * response 2b 80
1806 * size 14 00
1807 * sequence xx xx
1808 * result 00 00
1809 * reserved 00
1810 * bssid 02 2b 7b 0f 86 0e
1811 */
1812 lbs_join_post(priv, params, resp->bssid, capability);
1813
1814 out:
1815 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1816 return ret;
1817}
1818
1819
1820static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1821 struct cfg80211_ibss_params *params)
1822{
1823 struct lbs_private *priv = wiphy_priv(wiphy);
1824 int ret = 0;
1825 struct cfg80211_bss *bss;
1826 DECLARE_SSID_BUF(ssid_buf);
1827
1828 lbs_deb_enter(LBS_DEB_CFG80211);
1829
1830 if (!params->channel) {
1831 ret = -ENOTSUPP;
1832 goto out;
1833 }
1834
1835 ret = lbs_set_channel(priv, params->channel->hw_value);
1836 if (ret)
1837 goto out;
1838
1839 /* Search if someone is beaconing. This assumes that the
1840 * bss list is populated already */
1841 bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
1842 params->ssid, params->ssid_len,
1843 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
1844
1845 if (bss) {
1846 ret = lbs_ibss_join_existing(priv, params, bss);
1847 cfg80211_put_bss(bss);
1848 } else
1849 ret = lbs_ibss_start_new(priv, params);
1850
1851
1852 out:
1853 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1854 return ret;
1855}
1856
1857
1858static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1859{
1860 struct lbs_private *priv = wiphy_priv(wiphy);
1861 struct cmd_ds_802_11_ad_hoc_stop cmd;
1862 int ret = 0;
1863
1864 lbs_deb_enter(LBS_DEB_CFG80211);
1865
1866 memset(&cmd, 0, sizeof(cmd));
1867 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1868 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
1869
1870 /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
1871 lbs_mac_event_disconnected(priv);
1872
1873 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1874 return ret;
1875}
1876
1877
1878
1879
1880/***************************************************************************
1881 * Initialization
1882 */
1883
Holger Schurigff9fc792009-10-06 16:31:54 +02001884static struct cfg80211_ops lbs_cfg80211_ops = {
1885 .set_channel = lbs_cfg_set_channel,
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301886 .scan = lbs_cfg_scan,
1887 .connect = lbs_cfg_connect,
1888 .disconnect = lbs_cfg_disconnect,
1889 .add_key = lbs_cfg_add_key,
1890 .del_key = lbs_cfg_del_key,
1891 .set_default_key = lbs_cfg_set_default_key,
1892 .get_station = lbs_cfg_get_station,
1893 .dump_survey = lbs_get_survey,
1894 .change_virtual_intf = lbs_change_intf,
1895 .join_ibss = lbs_join_ibss,
1896 .leave_ibss = lbs_leave_ibss,
Holger Schurigff9fc792009-10-06 16:31:54 +02001897};
1898
1899
1900/*
1901 * At this time lbs_private *priv doesn't even exist, so we just allocate
1902 * memory and don't initialize the wiphy further. This is postponed until we
1903 * can talk to the firmware and happens at registration time in
1904 * lbs_cfg_wiphy_register().
1905 */
1906struct wireless_dev *lbs_cfg_alloc(struct device *dev)
1907{
1908 int ret = 0;
1909 struct wireless_dev *wdev;
1910
1911 lbs_deb_enter(LBS_DEB_CFG80211);
1912
1913 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
1914 if (!wdev) {
1915 dev_err(dev, "cannot allocate wireless device\n");
1916 return ERR_PTR(-ENOMEM);
1917 }
1918
1919 wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
1920 if (!wdev->wiphy) {
1921 dev_err(dev, "cannot allocate wiphy\n");
1922 ret = -ENOMEM;
1923 goto err_wiphy_new;
1924 }
1925
1926 lbs_deb_leave(LBS_DEB_CFG80211);
1927 return wdev;
1928
1929 err_wiphy_new:
1930 kfree(wdev);
1931 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1932 return ERR_PTR(ret);
1933}
1934
1935
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301936static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
1937{
1938 struct region_code_mapping {
1939 const char *cn;
1940 int code;
1941 };
1942
1943 /* Section 5.17.2 */
1944 static struct region_code_mapping regmap[] = {
1945 {"US ", 0x10}, /* US FCC */
1946 {"CA ", 0x20}, /* Canada */
1947 {"EU ", 0x30}, /* ETSI */
1948 {"ES ", 0x31}, /* Spain */
1949 {"FR ", 0x32}, /* France */
1950 {"JP ", 0x40}, /* Japan */
1951 };
1952 size_t i;
1953
1954 lbs_deb_enter(LBS_DEB_CFG80211);
1955
1956 for (i = 0; i < ARRAY_SIZE(regmap); i++)
1957 if (regmap[i].code == priv->regioncode) {
1958 regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
1959 break;
1960 }
1961
1962 lbs_deb_leave(LBS_DEB_CFG80211);
1963}
1964
1965
Holger Schurigff9fc792009-10-06 16:31:54 +02001966/*
1967 * This function get's called after lbs_setup_firmware() determined the
1968 * firmware capabities. So we can setup the wiphy according to our
1969 * hardware/firmware.
1970 */
1971int lbs_cfg_register(struct lbs_private *priv)
1972{
1973 struct wireless_dev *wdev = priv->wdev;
1974 int ret;
1975
1976 lbs_deb_enter(LBS_DEB_CFG80211);
1977
1978 wdev->wiphy->max_scan_ssids = 1;
1979 wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1980
Kiran Divekare86dc1c2010-06-14 22:01:26 +05301981 wdev->wiphy->interface_modes =
1982 BIT(NL80211_IFTYPE_STATION) |
1983 BIT(NL80211_IFTYPE_ADHOC);
1984 if (lbs_rtap_supported(priv))
1985 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
Holger Schurigff9fc792009-10-06 16:31:54 +02001986
Holger Schurigff9fc792009-10-06 16:31:54 +02001987 wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
1988
1989 /*
1990 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
1991 * never seen a firmware without WPA
1992 */
1993 wdev->wiphy->cipher_suites = cipher_suites;
1994 wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
Kiran Divekar1047d5e2010-06-04 23:20:42 -07001995 wdev->wiphy->reg_notifier = lbs_reg_notifier;
Holger Schurigff9fc792009-10-06 16:31:54 +02001996
1997 ret = wiphy_register(wdev->wiphy);
1998 if (ret < 0)
1999 lbs_pr_err("cannot register wiphy device\n");
2000
Daniel Mack73714002010-03-29 17:14:18 +02002001 priv->wiphy_registered = true;
2002
Holger Schurigff9fc792009-10-06 16:31:54 +02002003 ret = register_netdev(priv->dev);
2004 if (ret)
2005 lbs_pr_err("cannot register network device\n");
2006
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302007 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2008
2009 lbs_cfg_set_regulatory_hint(priv);
2010
Holger Schurigff9fc792009-10-06 16:31:54 +02002011 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2012 return ret;
2013}
2014
Kiran Divekar1047d5e2010-06-04 23:20:42 -07002015/**
2016 * @brief This function sets DOMAIN INFO to FW
2017 * @param priv pointer to struct lbs_private
2018 * @return 0; -1
2019*/
2020static int lbs_11d_set_domain_info(struct lbs_private *priv)
2021{
2022 int ret;
2023
2024 ret = lbs_prepare_and_send_command(priv, CMD_802_11D_DOMAIN_INFO,
2025 CMD_ACT_SET,
2026 CMD_OPTION_WAITFORRSP, 0, NULL);
2027 if (ret)
2028 lbs_deb_11d("fail to dnld domain info\n");
2029
2030 return ret;
2031}
2032
2033static void lbs_send_domain_info_cmd_fw(struct wiphy *wiphy,
2034 struct regulatory_request *request)
2035{
2036 u8 no_of_triplet = 0;
2037 u8 no_of_parsed_chan = 0;
2038 u8 first_channel = 0, next_chan = 0, max_pwr = 0;
2039 u8 i, flag = 0;
2040 enum ieee80211_band band;
2041 struct ieee80211_supported_band *sband;
2042 struct ieee80211_channel *ch;
2043 struct lbs_private *priv = wiphy_priv(wiphy);
2044 struct lbs_802_11d_domain_reg *domain_info = &priv->domain_reg;
2045 int ret = 0;
2046
2047 lbs_deb_enter(LBS_DEB_CFG80211);
2048
2049 /* Set country code */
2050 domain_info->country_code[0] = request->alpha2[0];
2051 domain_info->country_code[1] = request->alpha2[1];
2052 domain_info->country_code[2] = ' ';
2053
2054 for (band = 0; band < IEEE80211_NUM_BANDS ; band++) {
2055
2056 if (!wiphy->bands[band])
2057 continue;
2058
2059 sband = wiphy->bands[band];
2060
2061 for (i = 0; i < sband->n_channels ; i++) {
2062 ch = &sband->channels[i];
2063 if (ch->flags & IEEE80211_CHAN_DISABLED)
2064 continue;
2065
2066 if (!flag) {
2067 flag = 1;
2068 next_chan = first_channel = (u32) ch->hw_value;
2069 max_pwr = ch->max_power;
2070 no_of_parsed_chan = 1;
2071 continue;
2072 }
2073
2074 if (ch->hw_value == next_chan + 1 &&
2075 ch->max_power == max_pwr) {
2076 next_chan++;
2077 no_of_parsed_chan++;
2078 } else {
2079 domain_info->triplet[no_of_triplet]
2080 .chans.first_channel = first_channel;
2081 domain_info->triplet[no_of_triplet]
2082 .chans.num_channels = no_of_parsed_chan;
2083 domain_info->triplet[no_of_triplet]
2084 .chans.max_power = max_pwr;
2085 no_of_triplet++;
2086 flag = 0;
2087 }
2088 }
2089 if (flag) {
2090 domain_info->triplet[no_of_triplet]
2091 .chans.first_channel = first_channel;
2092 domain_info->triplet[no_of_triplet]
2093 .chans.num_channels = no_of_parsed_chan;
2094 domain_info->triplet[no_of_triplet]
2095 .chans.max_power = max_pwr;
2096 no_of_triplet++;
2097 }
2098 }
2099
2100 domain_info->no_triplet = no_of_triplet;
2101
2102 /* Set domain info */
2103 ret = lbs_11d_set_domain_info(priv);
2104 if (ret)
2105 lbs_pr_err("11D: error setting domain info in FW\n");
2106
2107 lbs_deb_leave(LBS_DEB_CFG80211);
2108}
2109
2110int lbs_reg_notifier(struct wiphy *wiphy,
2111 struct regulatory_request *request)
2112{
2113 lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
2114 "callback for domain %c%c\n", request->alpha2[0],
2115 request->alpha2[1]);
2116
2117 lbs_send_domain_info_cmd_fw(wiphy, request);
2118
2119 lbs_deb_leave(LBS_DEB_CFG80211);
2120
2121 return 0;
2122}
Holger Schurigff9fc792009-10-06 16:31:54 +02002123
Kiran Divekare86dc1c2010-06-14 22:01:26 +05302124void lbs_scan_deinit(struct lbs_private *priv)
2125{
2126 lbs_deb_enter(LBS_DEB_CFG80211);
2127 cancel_delayed_work_sync(&priv->scan_work);
2128}
2129
2130
Holger Schurigff9fc792009-10-06 16:31:54 +02002131void lbs_cfg_free(struct lbs_private *priv)
2132{
2133 struct wireless_dev *wdev = priv->wdev;
2134
2135 lbs_deb_enter(LBS_DEB_CFG80211);
2136
2137 if (!wdev)
2138 return;
2139
Daniel Mack73714002010-03-29 17:14:18 +02002140 if (priv->wiphy_registered)
Holger Schurigff9fc792009-10-06 16:31:54 +02002141 wiphy_unregister(wdev->wiphy);
Daniel Mack73714002010-03-29 17:14:18 +02002142
2143 if (wdev->wiphy)
Holger Schurigff9fc792009-10-06 16:31:54 +02002144 wiphy_free(wdev->wiphy);
Daniel Mack73714002010-03-29 17:14:18 +02002145
Holger Schurigff9fc792009-10-06 16:31:54 +02002146 kfree(wdev);
2147}