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