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