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