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