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