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