]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/libertas/cfg.c
089f0722fa20926161447ece6e1e688aecc1bef5
[~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 <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 } __attribute__ ((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 /***************************************************************************
1387  * Monitor mode
1388  */
1389
1390 /* like "struct cmd_ds_802_11_monitor_mode", but with cmd_header. Once we
1391  * get rid of WEXT, this should go into host.h */
1392 struct cmd_monitor_mode {
1393         struct cmd_header hdr;
1394
1395         __le16 action;
1396         __le16 mode;
1397 } __attribute__ ((packed));
1398
1399 static int lbs_enable_monitor_mode(struct lbs_private *priv, int mode)
1400 {
1401         struct cmd_monitor_mode cmd;
1402         int ret;
1403
1404         lbs_deb_enter(LBS_DEB_CFG80211);
1405
1406         /*
1407          * cmd       98 00
1408          * size      0c 00
1409          * sequence  xx xx
1410          * result    00 00
1411          * action    01 00    ACT_SET
1412          * enable    01 00
1413          */
1414         memset(&cmd, 0, sizeof(cmd));
1415         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1416         cmd.action = cpu_to_le16(CMD_ACT_SET);
1417         cmd.mode = cpu_to_le16(mode);
1418
1419         ret = lbs_cmd_with_response(priv, CMD_802_11_MONITOR_MODE, &cmd);
1420
1421         if (ret == 0)
1422                 priv->dev->type = ARPHRD_IEEE80211_RADIOTAP;
1423         else
1424                 priv->dev->type = ARPHRD_ETHER;
1425
1426         lbs_deb_leave(LBS_DEB_CFG80211);
1427         return ret;
1428 }
1429
1430
1431
1432
1433
1434
1435 /***************************************************************************
1436  * Get station
1437  */
1438
1439 /*
1440  * Returns the signal or 0 in case of an error.
1441  */
1442
1443 /* like "struct cmd_ds_802_11_rssi", but with cmd_header. Once we get rid
1444  * of WEXT, this should go into host.h */
1445 struct cmd_rssi {
1446         struct cmd_header hdr;
1447
1448         __le16 n_or_snr;
1449         __le16 nf;
1450         __le16 avg_snr;
1451         __le16 avg_nf;
1452 } __attribute__ ((packed));
1453
1454 static int lbs_get_signal(struct lbs_private *priv, s8 *signal, s8 *noise)
1455 {
1456         struct cmd_rssi cmd;
1457         int ret;
1458
1459         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1460         cmd.n_or_snr = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
1461         ret = lbs_cmd_with_response(priv, CMD_802_11_RSSI, &cmd);
1462
1463         if (ret == 0) {
1464                 *signal = CAL_RSSI(le16_to_cpu(cmd.n_or_snr),
1465                                 le16_to_cpu(cmd.nf));
1466                 *noise  = CAL_NF(le16_to_cpu(cmd.nf));
1467         }
1468         return ret;
1469 }
1470
1471
1472 static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1473                               u8 *mac, struct station_info *sinfo)
1474 {
1475         struct lbs_private *priv = wiphy_priv(wiphy);
1476         s8 signal, noise;
1477         int ret;
1478         size_t i;
1479
1480         lbs_deb_enter(LBS_DEB_CFG80211);
1481
1482         sinfo->filled |= STATION_INFO_TX_BYTES |
1483                          STATION_INFO_TX_PACKETS |
1484                          STATION_INFO_RX_BYTES |
1485                          STATION_INFO_RX_PACKETS;
1486         sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1487         sinfo->tx_packets = priv->dev->stats.tx_packets;
1488         sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1489         sinfo->rx_packets = priv->dev->stats.rx_packets;
1490
1491         /* Get current RSSI */
1492         ret = lbs_get_signal(priv, &signal, &noise);
1493         if (ret == 0) {
1494                 sinfo->signal = signal;
1495                 sinfo->filled |= STATION_INFO_SIGNAL;
1496         }
1497
1498         /* Convert priv->cur_rate from hw_value to NL80211 value */
1499         for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1500                 if (priv->cur_rate == lbs_rates[i].hw_value) {
1501                         sinfo->txrate.legacy = lbs_rates[i].bitrate;
1502                         sinfo->filled |= STATION_INFO_TX_BITRATE;
1503                         break;
1504                 }
1505         }
1506
1507         return 0;
1508 }
1509
1510
1511
1512
1513 /***************************************************************************
1514  * "Site survey", here just current channel and noise level
1515  */
1516
1517 static int lbs_get_survey(struct wiphy *wiphy, struct net_device *dev,
1518         int idx, struct survey_info *survey)
1519 {
1520         struct lbs_private *priv = wiphy_priv(wiphy);
1521         s8 signal, noise;
1522         int ret;
1523
1524         if (idx != 0)
1525                 ret = -ENOENT;
1526
1527         lbs_deb_enter(LBS_DEB_CFG80211);
1528
1529         survey->channel = ieee80211_get_channel(wiphy,
1530                 ieee80211_channel_to_frequency(priv->channel));
1531
1532         ret = lbs_get_signal(priv, &signal, &noise);
1533         if (ret == 0) {
1534                 survey->filled = SURVEY_INFO_NOISE_DBM;
1535                 survey->noise = noise;
1536         }
1537
1538         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1539         return ret;
1540 }
1541
1542
1543
1544
1545 /***************************************************************************
1546  * Change interface
1547  */
1548
1549 static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1550         enum nl80211_iftype type, u32 *flags,
1551                struct vif_params *params)
1552 {
1553         struct lbs_private *priv = wiphy_priv(wiphy);
1554         int ret = 0;
1555
1556         lbs_deb_enter(LBS_DEB_CFG80211);
1557
1558         switch (type) {
1559         case NL80211_IFTYPE_MONITOR:
1560                 ret = lbs_enable_monitor_mode(priv, 1);
1561                 break;
1562         case NL80211_IFTYPE_STATION:
1563                 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
1564                         ret = lbs_enable_monitor_mode(priv, 0);
1565                 if (!ret)
1566                         ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 1);
1567                 break;
1568         case NL80211_IFTYPE_ADHOC:
1569                 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
1570                         ret = lbs_enable_monitor_mode(priv, 0);
1571                 if (!ret)
1572                         ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, 2);
1573                 break;
1574         default:
1575                 ret = -ENOTSUPP;
1576         }
1577
1578         if (!ret)
1579                 priv->wdev->iftype = type;
1580
1581         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1582         return ret;
1583 }
1584
1585
1586
1587 /***************************************************************************
1588  * IBSS (Ad-Hoc)
1589  */
1590
1591 /* The firmware needs the following bits masked out of the beacon-derived
1592  * capability field when associating/joining to a BSS:
1593  *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1594  */
1595 #define CAPINFO_MASK (~(0xda00))
1596
1597
1598 static void lbs_join_post(struct lbs_private *priv,
1599                           struct cfg80211_ibss_params *params,
1600                           u8 *bssid, u16 capability)
1601 {
1602         u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1603                    2 + 4 +                      /* basic rates */
1604                    2 + 1 +                      /* DS parameter */
1605                    2 + 2 +                      /* atim */
1606                    2 + 8];                      /* extended rates */
1607         u8 *fake = fake_ie;
1608
1609         lbs_deb_enter(LBS_DEB_CFG80211);
1610
1611         /*
1612          * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1613          * the real IE from the firmware. So we fabricate a fake IE based on
1614          * what the firmware actually sends (sniffed with wireshark).
1615          */
1616         /* Fake SSID IE */
1617         *fake++ = WLAN_EID_SSID;
1618         *fake++ = params->ssid_len;
1619         memcpy(fake, params->ssid, params->ssid_len);
1620         fake += params->ssid_len;
1621         /* Fake supported basic rates IE */
1622         *fake++ = WLAN_EID_SUPP_RATES;
1623         *fake++ = 4;
1624         *fake++ = 0x82;
1625         *fake++ = 0x84;
1626         *fake++ = 0x8b;
1627         *fake++ = 0x96;
1628         /* Fake DS channel IE */
1629         *fake++ = WLAN_EID_DS_PARAMS;
1630         *fake++ = 1;
1631         *fake++ = params->channel->hw_value;
1632         /* Fake IBSS params IE */
1633         *fake++ = WLAN_EID_IBSS_PARAMS;
1634         *fake++ = 2;
1635         *fake++ = 0; /* ATIM=0 */
1636         *fake++ = 0;
1637         /* Fake extended rates IE, TODO: don't add this for 802.11b only,
1638          * but I don't know how this could be checked */
1639         *fake++ = WLAN_EID_EXT_SUPP_RATES;
1640         *fake++ = 8;
1641         *fake++ = 0x0c;
1642         *fake++ = 0x12;
1643         *fake++ = 0x18;
1644         *fake++ = 0x24;
1645         *fake++ = 0x30;
1646         *fake++ = 0x48;
1647         *fake++ = 0x60;
1648         *fake++ = 0x6c;
1649         lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1650
1651         cfg80211_inform_bss(priv->wdev->wiphy,
1652                             params->channel,
1653                             bssid,
1654                             0,
1655                             capability,
1656                             params->beacon_interval,
1657                             fake_ie, fake - fake_ie,
1658                             0, GFP_KERNEL);
1659
1660         memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
1661         priv->wdev->ssid_len = params->ssid_len;
1662
1663         cfg80211_ibss_joined(priv->dev, bssid, GFP_KERNEL);
1664
1665         /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1666         priv->connect_status = LBS_CONNECTED;
1667         netif_carrier_on(priv->dev);
1668         if (!priv->tx_pending_len)
1669                 netif_wake_queue(priv->dev);
1670
1671         lbs_deb_leave(LBS_DEB_CFG80211);
1672 }
1673
1674 static int lbs_ibss_join_existing(struct lbs_private *priv,
1675         struct cfg80211_ibss_params *params,
1676         struct cfg80211_bss *bss)
1677 {
1678         const u8 *rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1679         struct cmd_ds_802_11_ad_hoc_join cmd;
1680         u8 preamble = RADIO_PREAMBLE_SHORT;
1681         int ret = 0;
1682
1683         lbs_deb_enter(LBS_DEB_CFG80211);
1684
1685         /* TODO: set preamble based on scan result */
1686         ret = lbs_set_radio(priv, preamble, 1);
1687         if (ret)
1688                 goto out;
1689
1690         /*
1691          * Example CMD_802_11_AD_HOC_JOIN command:
1692          *
1693          * command         2c 00         CMD_802_11_AD_HOC_JOIN
1694          * size            65 00
1695          * sequence        xx xx
1696          * result          00 00
1697          * bssid           02 27 27 97 2f 96
1698          * ssid            49 42 53 53 00 00 00 00
1699          *                 00 00 00 00 00 00 00 00
1700          *                 00 00 00 00 00 00 00 00
1701          *                 00 00 00 00 00 00 00 00
1702          * type            02            CMD_BSS_TYPE_IBSS
1703          * beacon period   64 00
1704          * dtim period     00
1705          * timestamp       00 00 00 00 00 00 00 00
1706          * localtime       00 00 00 00 00 00 00 00
1707          * IE DS           03
1708          * IE DS len       01
1709          * IE DS channel   01
1710          * reserveed       00 00 00 00
1711          * IE IBSS         06
1712          * IE IBSS len     02
1713          * IE IBSS atim    00 00
1714          * reserved        00 00 00 00
1715          * capability      02 00
1716          * rates           82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1717          * fail timeout    ff 00
1718          * probe delay     00 00
1719          */
1720         memset(&cmd, 0, sizeof(cmd));
1721         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1722
1723         memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1724         memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1725         cmd.bss.type = CMD_BSS_TYPE_IBSS;
1726         cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1727         cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1728         cmd.bss.ds.header.len = 1;
1729         cmd.bss.ds.channel = params->channel->hw_value;
1730         cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1731         cmd.bss.ibss.header.len = 2;
1732         cmd.bss.ibss.atimwindow = 0;
1733         cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1734
1735         /* set rates to the intersection of our rates and the rates in the
1736            bss */
1737         if (!rates_eid) {
1738                 lbs_add_rates(cmd.bss.rates);
1739         } else {
1740                 int hw, i;
1741                 u8 rates_max = rates_eid[1];
1742                 u8 *rates = cmd.bss.rates;
1743                 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1744                         u8 hw_rate = lbs_rates[hw].bitrate / 5;
1745                         for (i = 0; i < rates_max; i++) {
1746                                 if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1747                                         u8 rate = rates_eid[i+2];
1748                                         if (rate == 0x02 || rate == 0x04 ||
1749                                             rate == 0x0b || rate == 0x16)
1750                                                 rate |= 0x80;
1751                                         *rates++ = rate;
1752                                 }
1753                         }
1754                 }
1755         }
1756
1757         /* Only v8 and below support setting this */
1758         if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1759                 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1760                 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1761         }
1762         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1763         if (ret)
1764                 goto out;
1765
1766         /*
1767          * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1768          *
1769          * response        2c 80
1770          * size            09 00
1771          * sequence        xx xx
1772          * result          00 00
1773          * reserved        00
1774          */
1775         lbs_join_post(priv, params, bss->bssid, bss->capability);
1776
1777  out:
1778         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1779         return ret;
1780 }
1781
1782
1783
1784 static int lbs_ibss_start_new(struct lbs_private *priv,
1785         struct cfg80211_ibss_params *params)
1786 {
1787         struct cmd_ds_802_11_ad_hoc_start cmd;
1788         struct cmd_ds_802_11_ad_hoc_result *resp =
1789                 (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1790         u8 preamble = RADIO_PREAMBLE_SHORT;
1791         int ret = 0;
1792         u16 capability;
1793
1794         lbs_deb_enter(LBS_DEB_CFG80211);
1795
1796         ret = lbs_set_radio(priv, preamble, 1);
1797         if (ret)
1798                 goto out;
1799
1800         /*
1801          * Example CMD_802_11_AD_HOC_START command:
1802          *
1803          * command         2b 00         CMD_802_11_AD_HOC_START
1804          * size            b1 00
1805          * sequence        xx xx
1806          * result          00 00
1807          * ssid            54 45 53 54 00 00 00 00
1808          *                 00 00 00 00 00 00 00 00
1809          *                 00 00 00 00 00 00 00 00
1810          *                 00 00 00 00 00 00 00 00
1811          * bss type        02
1812          * beacon period   64 00
1813          * dtim period     00
1814          * IE IBSS         06
1815          * IE IBSS len     02
1816          * IE IBSS atim    00 00
1817          * reserved        00 00 00 00
1818          * IE DS           03
1819          * IE DS len       01
1820          * IE DS channel   01
1821          * reserved        00 00 00 00
1822          * probe delay     00 00
1823          * capability      02 00
1824          * rates           82 84 8b 96   (basic rates with have bit 7 set)
1825          *                 0c 12 18 24 30 48 60 6c
1826          * padding         100 bytes
1827          */
1828         memset(&cmd, 0, sizeof(cmd));
1829         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1830         memcpy(cmd.ssid, params->ssid, params->ssid_len);
1831         cmd.bsstype = CMD_BSS_TYPE_IBSS;
1832         cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1833         cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1834         cmd.ibss.header.len = 2;
1835         cmd.ibss.atimwindow = 0;
1836         cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1837         cmd.ds.header.len = 1;
1838         cmd.ds.channel = params->channel->hw_value;
1839         /* Only v8 and below support setting probe delay */
1840         if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1841                 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1842         /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1843         capability = WLAN_CAPABILITY_IBSS;
1844         cmd.capability = cpu_to_le16(capability);
1845         lbs_add_rates(cmd.rates);
1846
1847
1848         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1849         if (ret)
1850                 goto out;
1851
1852         /*
1853          * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1854          *
1855          * response        2b 80
1856          * size            14 00
1857          * sequence        xx xx
1858          * result          00 00
1859          * reserved        00
1860          * bssid           02 2b 7b 0f 86 0e
1861          */
1862         lbs_join_post(priv, params, resp->bssid, capability);
1863
1864  out:
1865         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1866         return ret;
1867 }
1868
1869
1870 static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1871                 struct cfg80211_ibss_params *params)
1872 {
1873         struct lbs_private *priv = wiphy_priv(wiphy);
1874         int ret = 0;
1875         struct cfg80211_bss *bss;
1876         DECLARE_SSID_BUF(ssid_buf);
1877
1878         lbs_deb_enter(LBS_DEB_CFG80211);
1879
1880         if (!params->channel) {
1881                 ret = -ENOTSUPP;
1882                 goto out;
1883         }
1884
1885         ret = lbs_set_channel(priv, params->channel->hw_value);
1886         if (ret)
1887                 goto out;
1888
1889         /* Search if someone is beaconing. This assumes that the
1890          * bss list is populated already */
1891         bss = cfg80211_get_bss(wiphy, params->channel, params->bssid,
1892                 params->ssid, params->ssid_len,
1893                 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
1894
1895         if (bss) {
1896                 ret = lbs_ibss_join_existing(priv, params, bss);
1897                 cfg80211_put_bss(bss);
1898         } else
1899                 ret = lbs_ibss_start_new(priv, params);
1900
1901
1902  out:
1903         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1904         return ret;
1905 }
1906
1907
1908 static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1909 {
1910         struct lbs_private *priv = wiphy_priv(wiphy);
1911         struct cmd_ds_802_11_ad_hoc_stop cmd;
1912         int ret = 0;
1913
1914         lbs_deb_enter(LBS_DEB_CFG80211);
1915
1916         memset(&cmd, 0, sizeof(cmd));
1917         cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1918         ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
1919
1920         /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
1921         lbs_mac_event_disconnected(priv);
1922
1923         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1924         return ret;
1925 }
1926
1927
1928
1929
1930 /***************************************************************************
1931  * Initialization
1932  */
1933
1934 static struct cfg80211_ops lbs_cfg80211_ops = {
1935         .set_channel = lbs_cfg_set_channel,
1936         .scan = lbs_cfg_scan,
1937         .connect = lbs_cfg_connect,
1938         .disconnect = lbs_cfg_disconnect,
1939         .add_key = lbs_cfg_add_key,
1940         .del_key = lbs_cfg_del_key,
1941         .set_default_key = lbs_cfg_set_default_key,
1942         .get_station = lbs_cfg_get_station,
1943         .dump_survey = lbs_get_survey,
1944         .change_virtual_intf = lbs_change_intf,
1945         .join_ibss = lbs_join_ibss,
1946         .leave_ibss = lbs_leave_ibss,
1947 };
1948
1949
1950 /*
1951  * At this time lbs_private *priv doesn't even exist, so we just allocate
1952  * memory and don't initialize the wiphy further. This is postponed until we
1953  * can talk to the firmware and happens at registration time in
1954  * lbs_cfg_wiphy_register().
1955  */
1956 struct wireless_dev *lbs_cfg_alloc(struct device *dev)
1957 {
1958         int ret = 0;
1959         struct wireless_dev *wdev;
1960
1961         lbs_deb_enter(LBS_DEB_CFG80211);
1962
1963         wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
1964         if (!wdev) {
1965                 dev_err(dev, "cannot allocate wireless device\n");
1966                 return ERR_PTR(-ENOMEM);
1967         }
1968
1969         wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
1970         if (!wdev->wiphy) {
1971                 dev_err(dev, "cannot allocate wiphy\n");
1972                 ret = -ENOMEM;
1973                 goto err_wiphy_new;
1974         }
1975
1976         lbs_deb_leave(LBS_DEB_CFG80211);
1977         return wdev;
1978
1979  err_wiphy_new:
1980         kfree(wdev);
1981         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1982         return ERR_PTR(ret);
1983 }
1984
1985
1986 static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
1987 {
1988         struct region_code_mapping {
1989                 const char *cn;
1990                 int code;
1991         };
1992
1993         /* Section 5.17.2 */
1994         static struct region_code_mapping regmap[] = {
1995                 {"US ", 0x10}, /* US FCC */
1996                 {"CA ", 0x20}, /* Canada */
1997                 {"EU ", 0x30}, /* ETSI   */
1998                 {"ES ", 0x31}, /* Spain  */
1999                 {"FR ", 0x32}, /* France */
2000                 {"JP ", 0x40}, /* Japan  */
2001         };
2002         size_t i;
2003
2004         lbs_deb_enter(LBS_DEB_CFG80211);
2005
2006         for (i = 0; i < ARRAY_SIZE(regmap); i++)
2007                 if (regmap[i].code == priv->regioncode) {
2008                         regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2009                         break;
2010                 }
2011
2012         lbs_deb_leave(LBS_DEB_CFG80211);
2013 }
2014
2015
2016 /*
2017  * This function get's called after lbs_setup_firmware() determined the
2018  * firmware capabities. So we can setup the wiphy according to our
2019  * hardware/firmware.
2020  */
2021 int lbs_cfg_register(struct lbs_private *priv)
2022 {
2023         struct wireless_dev *wdev = priv->wdev;
2024         int ret;
2025
2026         lbs_deb_enter(LBS_DEB_CFG80211);
2027
2028         wdev->wiphy->max_scan_ssids = 1;
2029         wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2030
2031         wdev->wiphy->interface_modes =
2032                         BIT(NL80211_IFTYPE_STATION) |
2033                         BIT(NL80211_IFTYPE_ADHOC);
2034         if (lbs_rtap_supported(priv))
2035                 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2036
2037         wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &lbs_band_2ghz;
2038
2039         /*
2040          * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2041          * never seen a firmware without WPA
2042          */
2043         wdev->wiphy->cipher_suites = cipher_suites;
2044         wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2045
2046         ret = wiphy_register(wdev->wiphy);
2047         if (ret < 0)
2048                 lbs_pr_err("cannot register wiphy device\n");
2049
2050         priv->wiphy_registered = true;
2051
2052         ret = register_netdev(priv->dev);
2053         if (ret)
2054                 lbs_pr_err("cannot register network device\n");
2055
2056         INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2057
2058         lbs_cfg_set_regulatory_hint(priv);
2059
2060         lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2061         return ret;
2062 }
2063
2064
2065 void lbs_scan_deinit(struct lbs_private *priv)
2066 {
2067         lbs_deb_enter(LBS_DEB_CFG80211);
2068         cancel_delayed_work_sync(&priv->scan_work);
2069 }
2070
2071
2072 void lbs_cfg_free(struct lbs_private *priv)
2073 {
2074         struct wireless_dev *wdev = priv->wdev;
2075
2076         lbs_deb_enter(LBS_DEB_CFG80211);
2077
2078         if (!wdev)
2079                 return;
2080
2081         if (priv->wiphy_registered)
2082                 wiphy_unregister(wdev->wiphy);
2083
2084         if (wdev->wiphy)
2085                 wiphy_free(wdev->wiphy);
2086
2087         kfree(wdev);
2088 }