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