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