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