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