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