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