xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmsmac/mac80211_if.c (revision 34f2573661e3e644efaf383178af634a2fd67828)
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #define __UNDEF_NO_VERSION__
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 
21 #include <linux/etherdevice.h>
22 #include <linux/sched.h>
23 #include <linux/firmware.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/bcma/bcma.h>
27 #include <net/mac80211.h>
28 #include <defs.h>
29 #include "phy/phy_int.h"
30 #include "d11.h"
31 #include "channel.h"
32 #include "scb.h"
33 #include "pub.h"
34 #include "ucode_loader.h"
35 #include "mac80211_if.h"
36 #include "main.h"
37 #include "debug.h"
38 #include "led.h"
39 
40 #define N_TX_QUEUES	4 /* #tx queues on mac80211<->driver interface */
41 #define BRCMS_FLUSH_TIMEOUT	500 /* msec */
42 
43 /* Flags we support */
44 #define MAC_FILTERS (FIF_ALLMULTI | \
45 	FIF_FCSFAIL | \
46 	FIF_CONTROL | \
47 	FIF_OTHER_BSS | \
48 	FIF_BCN_PRBRESP_PROMISC | \
49 	FIF_PSPOLL)
50 
51 #define CHAN2GHZ(channel, frequency, chflags)  { \
52 	.band = NL80211_BAND_2GHZ, \
53 	.center_freq = (frequency), \
54 	.hw_value = (channel), \
55 	.flags = chflags, \
56 	.max_antenna_gain = 0, \
57 	.max_power = 19, \
58 }
59 
60 #define CHAN5GHZ(channel, chflags)  { \
61 	.band = NL80211_BAND_5GHZ, \
62 	.center_freq = 5000 + 5*(channel), \
63 	.hw_value = (channel), \
64 	.flags = chflags, \
65 	.max_antenna_gain = 0, \
66 	.max_power = 21, \
67 }
68 
69 #define RATE(rate100m, _flags) { \
70 	.bitrate = (rate100m), \
71 	.flags = (_flags), \
72 	.hw_value = (rate100m / 5), \
73 }
74 
75 struct firmware_hdr {
76 	__le32 offset;
77 	__le32 len;
78 	__le32 idx;
79 };
80 
81 static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
82 	"brcm/bcm43xx",
83 	NULL
84 };
85 
86 static int n_adapters_found;
87 
88 MODULE_AUTHOR("Broadcom Corporation");
89 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
90 MODULE_LICENSE("Dual BSD/GPL");
91 /* This needs to be adjusted when brcms_firmwares changes */
92 MODULE_FIRMWARE("brcm/bcm43xx-0.fw");
93 MODULE_FIRMWARE("brcm/bcm43xx_hdr-0.fw");
94 
95 /* recognized BCMA Core IDs */
96 static struct bcma_device_id brcms_coreid_table[] = {
97 	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 17, BCMA_ANY_CLASS),
98 	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 23, BCMA_ANY_CLASS),
99 	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 24, BCMA_ANY_CLASS),
100 	{},
101 };
102 MODULE_DEVICE_TABLE(bcma, brcms_coreid_table);
103 
104 #if defined(CONFIG_BRCMDBG)
105 /*
106  * Module parameter for setting the debug message level. Available
107  * flags are specified by the BRCM_DL_* macros in
108  * drivers/net/wireless/brcm80211/include/defs.h.
109  */
110 module_param_named(debug, brcm_msg_level, uint, 0644);
111 #endif
112 
113 static struct ieee80211_channel brcms_2ghz_chantable[] = {
114 	CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
115 	CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
116 	CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
117 	CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
118 	CHAN2GHZ(5, 2432, 0),
119 	CHAN2GHZ(6, 2437, 0),
120 	CHAN2GHZ(7, 2442, 0),
121 	CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
122 	CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
123 	CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
124 	CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
125 	CHAN2GHZ(12, 2467,
126 		 IEEE80211_CHAN_NO_IR |
127 		 IEEE80211_CHAN_NO_HT40PLUS),
128 	CHAN2GHZ(13, 2472,
129 		 IEEE80211_CHAN_NO_IR |
130 		 IEEE80211_CHAN_NO_HT40PLUS),
131 	CHAN2GHZ(14, 2484,
132 		 IEEE80211_CHAN_NO_IR |
133 		 IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS |
134 		 IEEE80211_CHAN_NO_OFDM)
135 };
136 
137 static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
138 	/* UNII-1 */
139 	CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
140 	CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
141 	CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
142 	CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
143 	/* UNII-2 */
144 	CHAN5GHZ(52,
145 		 IEEE80211_CHAN_RADAR |
146 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
147 	CHAN5GHZ(56,
148 		 IEEE80211_CHAN_RADAR |
149 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
150 	CHAN5GHZ(60,
151 		 IEEE80211_CHAN_RADAR |
152 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
153 	CHAN5GHZ(64,
154 		 IEEE80211_CHAN_RADAR |
155 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
156 	/* MID */
157 	CHAN5GHZ(100,
158 		 IEEE80211_CHAN_RADAR |
159 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
160 	CHAN5GHZ(104,
161 		 IEEE80211_CHAN_RADAR |
162 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
163 	CHAN5GHZ(108,
164 		 IEEE80211_CHAN_RADAR |
165 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
166 	CHAN5GHZ(112,
167 		 IEEE80211_CHAN_RADAR |
168 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
169 	CHAN5GHZ(116,
170 		 IEEE80211_CHAN_RADAR |
171 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
172 	CHAN5GHZ(120,
173 		 IEEE80211_CHAN_RADAR |
174 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
175 	CHAN5GHZ(124,
176 		 IEEE80211_CHAN_RADAR |
177 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
178 	CHAN5GHZ(128,
179 		 IEEE80211_CHAN_RADAR |
180 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
181 	CHAN5GHZ(132,
182 		 IEEE80211_CHAN_RADAR |
183 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40MINUS),
184 	CHAN5GHZ(136,
185 		 IEEE80211_CHAN_RADAR |
186 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS),
187 	CHAN5GHZ(140,
188 		 IEEE80211_CHAN_RADAR |
189 		 IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_HT40PLUS |
190 		 IEEE80211_CHAN_NO_HT40MINUS),
191 	/* UNII-3 */
192 	CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
193 	CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
194 	CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
195 	CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
196 	CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
197 };
198 
199 /*
200  * The rate table is used for both 2.4G and 5G rates. The
201  * latter being a subset as it does not support CCK rates.
202  */
203 static struct ieee80211_rate legacy_ratetable[] = {
204 	RATE(10, 0),
205 	RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
206 	RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
207 	RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
208 	RATE(60, 0),
209 	RATE(90, 0),
210 	RATE(120, 0),
211 	RATE(180, 0),
212 	RATE(240, 0),
213 	RATE(360, 0),
214 	RATE(480, 0),
215 	RATE(540, 0),
216 };
217 
218 static const struct ieee80211_supported_band brcms_band_2GHz_nphy_template = {
219 	.band = NL80211_BAND_2GHZ,
220 	.channels = brcms_2ghz_chantable,
221 	.n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
222 	.bitrates = legacy_ratetable,
223 	.n_bitrates = ARRAY_SIZE(legacy_ratetable),
224 	.ht_cap = {
225 		   /* from include/linux/ieee80211.h */
226 		   .cap = IEEE80211_HT_CAP_GRN_FLD |
227 			  IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40,
228 		   .ht_supported = true,
229 		   .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
230 		   .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
231 		   .mcs = {
232 			   /* placeholders for now */
233 			   .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
234 			   .rx_highest = cpu_to_le16(500),
235 			   .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
236 		   }
237 };
238 
239 static const struct ieee80211_supported_band brcms_band_5GHz_nphy_template = {
240 	.band = NL80211_BAND_5GHZ,
241 	.channels = brcms_5ghz_nphy_chantable,
242 	.n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
243 	.bitrates = legacy_ratetable + BRCMS_LEGACY_5G_RATE_OFFSET,
244 	.n_bitrates = ARRAY_SIZE(legacy_ratetable) -
245 			BRCMS_LEGACY_5G_RATE_OFFSET,
246 	.ht_cap = {
247 		   .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
248 			  IEEE80211_HT_CAP_SGI_40,
249 		   .ht_supported = true,
250 		   .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
251 		   .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
252 		   .mcs = {
253 			   /* placeholders for now */
254 			   .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
255 			   .rx_highest = cpu_to_le16(500),
256 			   .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
257 		   }
258 };
259 
260 /* flags the given rate in rateset as requested */
261 static void brcms_set_basic_rate(struct brcm_rateset *rs, u16 rate, bool is_br)
262 {
263 	u32 i;
264 
265 	for (i = 0; i < rs->count; i++) {
266 		if (rate != (rs->rates[i] & 0x7f))
267 			continue;
268 
269 		if (is_br)
270 			rs->rates[i] |= BRCMS_RATE_FLAG;
271 		else
272 			rs->rates[i] &= BRCMS_RATE_MASK;
273 		return;
274 	}
275 }
276 
277 /*
278  * This function frees the WL per-device resources.
279  *
280  * This function frees resources owned by the WL device pointed to
281  * by the wl parameter.
282  *
283  * precondition: can both be called locked and unlocked
284  */
285 static void brcms_free(struct brcms_info *wl)
286 {
287 	struct brcms_timer *t, *next;
288 
289 	/* free ucode data */
290 	if (wl->fw.fw_cnt)
291 		brcms_ucode_data_free(&wl->ucode);
292 	if (wl->irq)
293 		free_irq(wl->irq, wl);
294 
295 	/* kill dpc */
296 	tasklet_kill(&wl->tasklet);
297 
298 	if (wl->pub) {
299 		brcms_debugfs_detach(wl->pub);
300 		brcms_c_module_unregister(wl->pub, "linux", wl);
301 	}
302 
303 	/* free common resources */
304 	if (wl->wlc) {
305 		brcms_c_detach(wl->wlc);
306 		wl->wlc = NULL;
307 		wl->pub = NULL;
308 	}
309 
310 	/* virtual interface deletion is deferred so we cannot spinwait */
311 
312 	/* wait for all pending callbacks to complete */
313 	while (atomic_read(&wl->callbacks) > 0)
314 		schedule();
315 
316 	/* free timers */
317 	for (t = wl->timers; t; t = next) {
318 		next = t->next;
319 #ifdef DEBUG
320 		kfree(t->name);
321 #endif
322 		kfree(t);
323 	}
324 }
325 
326 /*
327 * called from both kernel as from this kernel module (error flow on attach)
328 * precondition: perimeter lock is not acquired.
329 */
330 static void brcms_remove(struct bcma_device *pdev)
331 {
332 	struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
333 	struct brcms_info *wl = hw->priv;
334 
335 	if (wl->wlc) {
336 		brcms_led_unregister(wl);
337 		wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
338 		wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
339 		ieee80211_unregister_hw(hw);
340 	}
341 
342 	brcms_free(wl);
343 
344 	bcma_set_drvdata(pdev, NULL);
345 	ieee80211_free_hw(hw);
346 }
347 
348 /*
349  * Precondition: Since this function is called in brcms_pci_probe() context,
350  * no locking is required.
351  */
352 static void brcms_release_fw(struct brcms_info *wl)
353 {
354 	int i;
355 	for (i = 0; i < MAX_FW_IMAGES; i++) {
356 		release_firmware(wl->fw.fw_bin[i]);
357 		release_firmware(wl->fw.fw_hdr[i]);
358 	}
359 }
360 
361 /*
362  * Precondition: Since this function is called in brcms_pci_probe() context,
363  * no locking is required.
364  */
365 static int brcms_request_fw(struct brcms_info *wl, struct bcma_device *pdev)
366 {
367 	int status;
368 	struct device *device = &pdev->dev;
369 	char fw_name[100];
370 	int i;
371 
372 	memset(&wl->fw, 0, sizeof(struct brcms_firmware));
373 	for (i = 0; i < MAX_FW_IMAGES; i++) {
374 		if (brcms_firmwares[i] == NULL)
375 			break;
376 		sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
377 			UCODE_LOADER_API_VER);
378 		status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
379 		if (status) {
380 			wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
381 				  KBUILD_MODNAME, fw_name);
382 			return status;
383 		}
384 		sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
385 			UCODE_LOADER_API_VER);
386 		status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
387 		if (status) {
388 			wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
389 				  KBUILD_MODNAME, fw_name);
390 			return status;
391 		}
392 		wl->fw.hdr_num_entries[i] =
393 		    wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
394 	}
395 	wl->fw.fw_cnt = i;
396 	status = brcms_ucode_data_init(wl, &wl->ucode);
397 	brcms_release_fw(wl);
398 	return status;
399 }
400 
401 static void brcms_ops_tx(struct ieee80211_hw *hw,
402 			 struct ieee80211_tx_control *control,
403 			 struct sk_buff *skb)
404 {
405 	struct brcms_info *wl = hw->priv;
406 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
407 
408 	spin_lock_bh(&wl->lock);
409 	if (!wl->pub->up) {
410 		brcms_err(wl->wlc->hw->d11core, "ops->tx called while down\n");
411 		kfree_skb(skb);
412 		goto done;
413 	}
414 	if (brcms_c_sendpkt_mac80211(wl->wlc, skb, hw))
415 		tx_info->rate_driver_data[0] = control->sta;
416  done:
417 	spin_unlock_bh(&wl->lock);
418 }
419 
420 static int brcms_ops_start(struct ieee80211_hw *hw)
421 {
422 	struct brcms_info *wl = hw->priv;
423 	bool blocked;
424 	int err;
425 
426 	if (!wl->ucode.bcm43xx_bomminor) {
427 		err = brcms_request_fw(wl, wl->wlc->hw->d11core);
428 		if (err)
429 			return -ENOENT;
430 	}
431 
432 	ieee80211_wake_queues(hw);
433 	spin_lock_bh(&wl->lock);
434 	blocked = brcms_rfkill_set_hw_state(wl);
435 	spin_unlock_bh(&wl->lock);
436 	if (!blocked)
437 		wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
438 
439 	spin_lock_bh(&wl->lock);
440 	/* avoid acknowledging frames before a non-monitor device is added */
441 	wl->mute_tx = true;
442 
443 	if (!wl->pub->up)
444 		if (!blocked)
445 			err = brcms_up(wl);
446 		else
447 			err = -ERFKILL;
448 	else
449 		err = -ENODEV;
450 	spin_unlock_bh(&wl->lock);
451 
452 	if (err != 0)
453 		brcms_err(wl->wlc->hw->d11core, "%s: brcms_up() returned %d\n",
454 			  __func__, err);
455 
456 	bcma_core_pci_power_save(wl->wlc->hw->d11core->bus, true);
457 	return err;
458 }
459 
460 static void brcms_ops_stop(struct ieee80211_hw *hw, bool suspend)
461 {
462 	struct brcms_info *wl = hw->priv;
463 	int status;
464 
465 	ieee80211_stop_queues(hw);
466 
467 	if (wl->wlc == NULL)
468 		return;
469 
470 	spin_lock_bh(&wl->lock);
471 	status = brcms_c_chipmatch(wl->wlc->hw->d11core);
472 	spin_unlock_bh(&wl->lock);
473 	if (!status) {
474 		brcms_err(wl->wlc->hw->d11core,
475 			  "wl: brcms_ops_stop: chipmatch failed\n");
476 		return;
477 	}
478 
479 	bcma_core_pci_power_save(wl->wlc->hw->d11core->bus, false);
480 
481 	/* put driver in down state */
482 	spin_lock_bh(&wl->lock);
483 	brcms_down(wl);
484 	spin_unlock_bh(&wl->lock);
485 }
486 
487 static int
488 brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
489 {
490 	struct brcms_info *wl = hw->priv;
491 
492 	/* Just STA, AP and ADHOC for now */
493 	if (vif->type != NL80211_IFTYPE_STATION &&
494 	    vif->type != NL80211_IFTYPE_AP &&
495 	    vif->type != NL80211_IFTYPE_ADHOC) {
496 		brcms_err(wl->wlc->hw->d11core,
497 			  "%s: Attempt to add type %d, only STA, AP and AdHoc for now\n",
498 			  __func__, vif->type);
499 		return -EOPNOTSUPP;
500 	}
501 
502 	spin_lock_bh(&wl->lock);
503 	wl->wlc->vif = vif;
504 	wl->mute_tx = false;
505 	brcms_c_mute(wl->wlc, false);
506 	if (vif->type == NL80211_IFTYPE_STATION)
507 		brcms_c_start_station(wl->wlc, vif->addr);
508 	else if (vif->type == NL80211_IFTYPE_AP)
509 		brcms_c_start_ap(wl->wlc, vif->addr, vif->bss_conf.bssid,
510 				 vif->cfg.ssid, vif->cfg.ssid_len);
511 	else if (vif->type == NL80211_IFTYPE_ADHOC)
512 		brcms_c_start_adhoc(wl->wlc, vif->addr);
513 	spin_unlock_bh(&wl->lock);
514 
515 	return 0;
516 }
517 
518 static void
519 brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
520 {
521 	struct brcms_info *wl = hw->priv;
522 
523 	spin_lock_bh(&wl->lock);
524 	wl->wlc->vif = NULL;
525 	spin_unlock_bh(&wl->lock);
526 }
527 
528 static int brcms_ops_config(struct ieee80211_hw *hw, int radio_idx,
529 			    u32 changed)
530 {
531 	struct ieee80211_conf *conf = &hw->conf;
532 	struct brcms_info *wl = hw->priv;
533 	struct bcma_device *core = wl->wlc->hw->d11core;
534 	int err = 0;
535 	int new_int;
536 
537 	spin_lock_bh(&wl->lock);
538 	if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
539 		brcms_c_set_beacon_listen_interval(wl->wlc,
540 						   conf->listen_interval);
541 	}
542 	if (changed & IEEE80211_CONF_CHANGE_MONITOR)
543 		brcms_dbg_info(core, "%s: change monitor mode: %s\n",
544 			       __func__, conf->flags & IEEE80211_CONF_MONITOR ?
545 			       "true" : "false");
546 	if (changed & IEEE80211_CONF_CHANGE_PS)
547 		brcms_err(core, "%s: change power-save mode: %s (implement)\n",
548 			  __func__, conf->flags & IEEE80211_CONF_PS ?
549 			  "true" : "false");
550 
551 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
552 		err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
553 		if (err < 0) {
554 			brcms_err(core, "%s: Error setting power_level\n",
555 				  __func__);
556 			goto config_out;
557 		}
558 		new_int = brcms_c_get_tx_power(wl->wlc);
559 		if (new_int != conf->power_level)
560 			brcms_err(core,
561 				  "%s: Power level req != actual, %d %d\n",
562 				  __func__, conf->power_level,
563 				  new_int);
564 	}
565 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
566 		if (conf->chandef.width == NL80211_CHAN_WIDTH_20 ||
567 		    conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
568 			err = brcms_c_set_channel(wl->wlc,
569 						  conf->chandef.chan->hw_value);
570 		else
571 			err = -ENOTSUPP;
572 	}
573 	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
574 		err = brcms_c_set_rate_limit(wl->wlc,
575 					     conf->short_frame_max_tx_count,
576 					     conf->long_frame_max_tx_count);
577 
578  config_out:
579 	spin_unlock_bh(&wl->lock);
580 	return err;
581 }
582 
583 static void
584 brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
585 			struct ieee80211_vif *vif,
586 			struct ieee80211_bss_conf *info, u64 changed)
587 {
588 	struct brcms_info *wl = hw->priv;
589 	struct bcma_device *core = wl->wlc->hw->d11core;
590 
591 	if (changed & BSS_CHANGED_ASSOC) {
592 		/* association status changed (associated/disassociated)
593 		 * also implies a change in the AID.
594 		 */
595 		brcms_err(core, "%s: %s: %sassociated\n", KBUILD_MODNAME,
596 			  __func__, vif->cfg.assoc ? "" : "dis");
597 		spin_lock_bh(&wl->lock);
598 		brcms_c_associate_upd(wl->wlc, vif->cfg.assoc);
599 		spin_unlock_bh(&wl->lock);
600 	}
601 	if (changed & BSS_CHANGED_ERP_SLOT) {
602 		s8 val;
603 
604 		/* slot timing changed */
605 		if (info->use_short_slot)
606 			val = 1;
607 		else
608 			val = 0;
609 		spin_lock_bh(&wl->lock);
610 		brcms_c_set_shortslot_override(wl->wlc, val);
611 		spin_unlock_bh(&wl->lock);
612 	}
613 
614 	if (changed & BSS_CHANGED_HT) {
615 		/* 802.11n parameters changed */
616 		u16 mode = info->ht_operation_mode;
617 
618 		spin_lock_bh(&wl->lock);
619 		brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
620 			mode & IEEE80211_HT_OP_MODE_PROTECTION);
621 		brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
622 			mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
623 		brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
624 			mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
625 		spin_unlock_bh(&wl->lock);
626 	}
627 	if (changed & BSS_CHANGED_BASIC_RATES) {
628 		struct ieee80211_supported_band *bi;
629 		u32 br_mask, i;
630 		u16 rate;
631 		struct brcm_rateset rs;
632 		int error;
633 
634 		/* retrieve the current rates */
635 		spin_lock_bh(&wl->lock);
636 		brcms_c_get_current_rateset(wl->wlc, &rs);
637 		spin_unlock_bh(&wl->lock);
638 
639 		br_mask = info->basic_rates;
640 		bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
641 		for (i = 0; i < bi->n_bitrates; i++) {
642 			/* convert to internal rate value */
643 			rate = (bi->bitrates[i].bitrate << 1) / 10;
644 
645 			/* set/clear basic rate flag */
646 			brcms_set_basic_rate(&rs, rate, br_mask & 1);
647 			br_mask >>= 1;
648 		}
649 
650 		/* update the rate set */
651 		spin_lock_bh(&wl->lock);
652 		error = brcms_c_set_rateset(wl->wlc, &rs);
653 		spin_unlock_bh(&wl->lock);
654 		if (error)
655 			brcms_err(core, "changing basic rates failed: %d\n",
656 				  error);
657 	}
658 	if (changed & BSS_CHANGED_BEACON_INT) {
659 		/* Beacon interval changed */
660 		spin_lock_bh(&wl->lock);
661 		brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
662 		spin_unlock_bh(&wl->lock);
663 	}
664 	if (changed & BSS_CHANGED_BSSID) {
665 		/* BSSID changed, for whatever reason (IBSS and managed mode) */
666 		spin_lock_bh(&wl->lock);
667 		brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
668 		spin_unlock_bh(&wl->lock);
669 	}
670 	if (changed & BSS_CHANGED_SSID) {
671 		/* BSSID changed, for whatever reason (IBSS and managed mode) */
672 		spin_lock_bh(&wl->lock);
673 		brcms_c_set_ssid(wl->wlc, vif->cfg.ssid, vif->cfg.ssid_len);
674 		spin_unlock_bh(&wl->lock);
675 	}
676 	if (changed & BSS_CHANGED_BEACON) {
677 		/* Beacon data changed, retrieve new beacon (beaconing modes) */
678 		struct sk_buff *beacon;
679 		u16 tim_offset = 0;
680 
681 		spin_lock_bh(&wl->lock);
682 		beacon = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL, 0);
683 		brcms_c_set_new_beacon(wl->wlc, beacon, tim_offset,
684 				       info->dtim_period);
685 		spin_unlock_bh(&wl->lock);
686 	}
687 
688 	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
689 		struct sk_buff *probe_resp;
690 
691 		spin_lock_bh(&wl->lock);
692 		probe_resp = ieee80211_proberesp_get(hw, vif);
693 		brcms_c_set_new_probe_resp(wl->wlc, probe_resp);
694 		spin_unlock_bh(&wl->lock);
695 	}
696 
697 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
698 		/* Beaconing should be enabled/disabled (beaconing modes) */
699 		brcms_err(core, "%s: Beacon enabled: %s\n", __func__,
700 			  info->enable_beacon ? "true" : "false");
701 		if (info->enable_beacon &&
702 		    hw->wiphy->flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) {
703 			brcms_c_enable_probe_resp(wl->wlc, true);
704 		} else {
705 			brcms_c_enable_probe_resp(wl->wlc, false);
706 		}
707 	}
708 
709 	if (changed & BSS_CHANGED_CQM) {
710 		/* Connection quality monitor config changed */
711 		brcms_err(core, "%s: cqm change: threshold %d, hys %d "
712 			  " (implement)\n", __func__, info->cqm_rssi_thold,
713 			  info->cqm_rssi_hyst);
714 	}
715 
716 	if (changed & BSS_CHANGED_IBSS) {
717 		/* IBSS join status changed */
718 		brcms_err(core, "%s: IBSS joined: %s (implement)\n",
719 			  __func__, vif->cfg.ibss_joined ? "true" : "false");
720 	}
721 
722 	if (changed & BSS_CHANGED_ARP_FILTER) {
723 		/* Hardware ARP filter address list or state changed */
724 		brcms_err(core, "%s: arp filtering: %d addresses"
725 			  " (implement)\n", __func__, vif->cfg.arp_addr_cnt);
726 	}
727 
728 	if (changed & BSS_CHANGED_QOS) {
729 		/*
730 		 * QoS for this association was enabled/disabled.
731 		 * Note that it is only ever disabled for station mode.
732 		 */
733 		brcms_err(core, "%s: qos enabled: %s (implement)\n",
734 			  __func__, info->qos ? "true" : "false");
735 	}
736 	return;
737 }
738 
739 static void
740 brcms_ops_configure_filter(struct ieee80211_hw *hw,
741 			unsigned int changed_flags,
742 			unsigned int *total_flags, u64 multicast)
743 {
744 	struct brcms_info *wl = hw->priv;
745 	struct bcma_device *core = wl->wlc->hw->d11core;
746 
747 	changed_flags &= MAC_FILTERS;
748 	*total_flags &= MAC_FILTERS;
749 
750 	if (changed_flags & FIF_ALLMULTI)
751 		brcms_dbg_info(core, "FIF_ALLMULTI\n");
752 	if (changed_flags & FIF_FCSFAIL)
753 		brcms_dbg_info(core, "FIF_FCSFAIL\n");
754 	if (changed_flags & FIF_CONTROL)
755 		brcms_dbg_info(core, "FIF_CONTROL\n");
756 	if (changed_flags & FIF_OTHER_BSS)
757 		brcms_dbg_info(core, "FIF_OTHER_BSS\n");
758 	if (changed_flags & FIF_PSPOLL)
759 		brcms_dbg_info(core, "FIF_PSPOLL\n");
760 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
761 		brcms_dbg_info(core, "FIF_BCN_PRBRESP_PROMISC\n");
762 
763 	spin_lock_bh(&wl->lock);
764 	brcms_c_mac_promisc(wl->wlc, *total_flags);
765 	spin_unlock_bh(&wl->lock);
766 	return;
767 }
768 
769 static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw,
770 				    struct ieee80211_vif *vif,
771 				    const u8 *mac_addr)
772 {
773 	struct brcms_info *wl = hw->priv;
774 	spin_lock_bh(&wl->lock);
775 	brcms_c_scan_start(wl->wlc);
776 	spin_unlock_bh(&wl->lock);
777 	return;
778 }
779 
780 static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw,
781 				       struct ieee80211_vif *vif)
782 {
783 	struct brcms_info *wl = hw->priv;
784 	spin_lock_bh(&wl->lock);
785 	brcms_c_scan_stop(wl->wlc);
786 	spin_unlock_bh(&wl->lock);
787 	return;
788 }
789 
790 static int
791 brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
792 		  unsigned int link_id, u16 queue,
793 		  const struct ieee80211_tx_queue_params *params)
794 {
795 	struct brcms_info *wl = hw->priv;
796 
797 	spin_lock_bh(&wl->lock);
798 	brcms_c_wme_setparams(wl->wlc, queue, params, true);
799 	spin_unlock_bh(&wl->lock);
800 
801 	return 0;
802 }
803 
804 static int
805 brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
806 	       struct ieee80211_sta *sta)
807 {
808 	struct brcms_info *wl = hw->priv;
809 	struct scb *scb = &wl->wlc->pri_scb;
810 
811 	brcms_c_init_scb(scb);
812 
813 	wl->pub->global_ampdu = &(scb->scb_ampdu);
814 	wl->pub->global_ampdu->max_pdu = 16;
815 
816 	/*
817 	 * minstrel_ht initiates addBA on our behalf by calling
818 	 * ieee80211_start_tx_ba_session()
819 	 */
820 	return 0;
821 }
822 
823 static int
824 brcms_ops_ampdu_action(struct ieee80211_hw *hw,
825 		    struct ieee80211_vif *vif,
826 		    struct ieee80211_ampdu_params *params)
827 {
828 	struct brcms_info *wl = hw->priv;
829 	struct scb *scb = &wl->wlc->pri_scb;
830 	int status;
831 	struct ieee80211_sta *sta = params->sta;
832 	enum ieee80211_ampdu_mlme_action action = params->action;
833 	u16 tid = params->tid;
834 
835 	if (WARN_ON(scb->magic != SCB_MAGIC))
836 		return -EIDRM;
837 	switch (action) {
838 	case IEEE80211_AMPDU_RX_START:
839 		break;
840 	case IEEE80211_AMPDU_RX_STOP:
841 		break;
842 	case IEEE80211_AMPDU_TX_START:
843 		spin_lock_bh(&wl->lock);
844 		status = brcms_c_aggregatable(wl->wlc, tid);
845 		spin_unlock_bh(&wl->lock);
846 		if (!status) {
847 			brcms_dbg_ht(wl->wlc->hw->d11core,
848 				     "START: tid %d is not agg\'able\n", tid);
849 			return -EINVAL;
850 		}
851 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
852 
853 	case IEEE80211_AMPDU_TX_STOP_CONT:
854 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
855 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
856 		spin_lock_bh(&wl->lock);
857 		brcms_c_ampdu_flush(wl->wlc, sta, tid);
858 		spin_unlock_bh(&wl->lock);
859 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
860 		break;
861 	case IEEE80211_AMPDU_TX_OPERATIONAL:
862 		/*
863 		 * BA window size from ADDBA response ('buf_size') defines how
864 		 * many outstanding MPDUs are allowed for the BA stream by
865 		 * recipient and traffic class (this is actually unused by the
866 		 * rest of the driver). 'ampdu_factor' gives maximum AMPDU size.
867 		 */
868 		spin_lock_bh(&wl->lock);
869 		brcms_c_ampdu_tx_operational(wl->wlc, tid,
870 			(1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
871 			 sta->deflink.ht_cap.ampdu_factor)) - 1);
872 		spin_unlock_bh(&wl->lock);
873 		/* Power save wakeup */
874 		break;
875 	default:
876 		brcms_err(wl->wlc->hw->d11core,
877 			  "%s: Invalid command, ignoring\n", __func__);
878 	}
879 
880 	return 0;
881 }
882 
883 static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
884 {
885 	struct brcms_info *wl = hw->priv;
886 	bool blocked;
887 
888 	spin_lock_bh(&wl->lock);
889 	blocked = brcms_c_check_radio_disabled(wl->wlc);
890 	spin_unlock_bh(&wl->lock);
891 
892 	wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
893 }
894 
895 static bool brcms_tx_flush_completed(struct brcms_info *wl)
896 {
897 	bool result;
898 
899 	spin_lock_bh(&wl->lock);
900 	result = brcms_c_tx_flush_completed(wl->wlc);
901 	spin_unlock_bh(&wl->lock);
902 	return result;
903 }
904 
905 static void brcms_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
906 			    u32 queues, bool drop)
907 {
908 	struct brcms_info *wl = hw->priv;
909 	int ret;
910 
911 	no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
912 
913 	ret = wait_event_timeout(wl->tx_flush_wq,
914 				 brcms_tx_flush_completed(wl),
915 				 msecs_to_jiffies(BRCMS_FLUSH_TIMEOUT));
916 
917 	brcms_dbg_mac80211(wl->wlc->hw->d11core,
918 			   "ret=%d\n", jiffies_to_msecs(ret));
919 }
920 
921 static u64 brcms_ops_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
922 {
923 	struct brcms_info *wl = hw->priv;
924 	u64 tsf;
925 
926 	spin_lock_bh(&wl->lock);
927 	tsf = brcms_c_tsf_get(wl->wlc);
928 	spin_unlock_bh(&wl->lock);
929 
930 	return tsf;
931 }
932 
933 static void brcms_ops_set_tsf(struct ieee80211_hw *hw,
934 			   struct ieee80211_vif *vif, u64 tsf)
935 {
936 	struct brcms_info *wl = hw->priv;
937 
938 	spin_lock_bh(&wl->lock);
939 	brcms_c_tsf_set(wl->wlc, tsf);
940 	spin_unlock_bh(&wl->lock);
941 }
942 
943 static int brcms_ops_beacon_set_tim(struct ieee80211_hw *hw,
944 				 struct ieee80211_sta *sta, bool set)
945 {
946 	struct brcms_info *wl = hw->priv;
947 	struct sk_buff *beacon = NULL;
948 	u16 tim_offset = 0;
949 
950 	spin_lock_bh(&wl->lock);
951 	if (wl->wlc->vif)
952 		beacon = ieee80211_beacon_get_tim(hw, wl->wlc->vif,
953 						  &tim_offset, NULL, 0);
954 	if (beacon)
955 		brcms_c_set_new_beacon(wl->wlc, beacon, tim_offset,
956 				       wl->wlc->vif->bss_conf.dtim_period);
957 	spin_unlock_bh(&wl->lock);
958 
959 	return 0;
960 }
961 
962 static const struct ieee80211_ops brcms_ops = {
963 	.add_chanctx = ieee80211_emulate_add_chanctx,
964 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
965 	.change_chanctx = ieee80211_emulate_change_chanctx,
966 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
967 	.tx = brcms_ops_tx,
968 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,
969 	.start = brcms_ops_start,
970 	.stop = brcms_ops_stop,
971 	.add_interface = brcms_ops_add_interface,
972 	.remove_interface = brcms_ops_remove_interface,
973 	.config = brcms_ops_config,
974 	.bss_info_changed = brcms_ops_bss_info_changed,
975 	.configure_filter = brcms_ops_configure_filter,
976 	.sw_scan_start = brcms_ops_sw_scan_start,
977 	.sw_scan_complete = brcms_ops_sw_scan_complete,
978 	.conf_tx = brcms_ops_conf_tx,
979 	.sta_add = brcms_ops_sta_add,
980 	.ampdu_action = brcms_ops_ampdu_action,
981 	.rfkill_poll = brcms_ops_rfkill_poll,
982 	.flush = brcms_ops_flush,
983 	.get_tsf = brcms_ops_get_tsf,
984 	.set_tsf = brcms_ops_set_tsf,
985 	.set_tim = brcms_ops_beacon_set_tim,
986 };
987 
988 void brcms_dpc(struct tasklet_struct *t)
989 {
990 	struct brcms_info *wl;
991 
992 	wl = from_tasklet(wl, t, tasklet);
993 
994 	spin_lock_bh(&wl->lock);
995 
996 	/* call the common second level interrupt handler */
997 	if (wl->pub->up) {
998 		if (wl->resched) {
999 			unsigned long flags;
1000 
1001 			spin_lock_irqsave(&wl->isr_lock, flags);
1002 			brcms_c_intrsupd(wl->wlc);
1003 			spin_unlock_irqrestore(&wl->isr_lock, flags);
1004 		}
1005 
1006 		wl->resched = brcms_c_dpc(wl->wlc, true);
1007 	}
1008 
1009 	/* brcms_c_dpc() may bring the driver down */
1010 	if (!wl->pub->up)
1011 		goto done;
1012 
1013 	/* re-schedule dpc */
1014 	if (wl->resched)
1015 		tasklet_schedule(&wl->tasklet);
1016 	else
1017 		/* re-enable interrupts */
1018 		brcms_intrson(wl);
1019 
1020  done:
1021 	spin_unlock_bh(&wl->lock);
1022 	wake_up(&wl->tx_flush_wq);
1023 }
1024 
1025 static irqreturn_t brcms_isr(int irq, void *dev_id)
1026 {
1027 	struct brcms_info *wl;
1028 	irqreturn_t ret = IRQ_NONE;
1029 
1030 	wl = (struct brcms_info *) dev_id;
1031 
1032 	spin_lock(&wl->isr_lock);
1033 
1034 	/* call common first level interrupt handler */
1035 	if (brcms_c_isr(wl->wlc)) {
1036 		/* schedule second level handler */
1037 		tasklet_schedule(&wl->tasklet);
1038 		ret = IRQ_HANDLED;
1039 	}
1040 
1041 	spin_unlock(&wl->isr_lock);
1042 
1043 	return ret;
1044 }
1045 
1046 /*
1047  * is called in brcms_pci_probe() context, therefore no locking required.
1048  */
1049 static int ieee_hw_rate_init(struct ieee80211_hw *hw)
1050 {
1051 	struct brcms_info *wl = hw->priv;
1052 	struct brcms_c_info *wlc = wl->wlc;
1053 	struct ieee80211_supported_band *band;
1054 	u16 phy_type;
1055 
1056 	hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
1057 	hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
1058 
1059 	phy_type = brcms_c_get_phy_type(wl->wlc, 0);
1060 	if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
1061 		band = &wlc->bandstate[BAND_2G_INDEX]->band;
1062 		*band = brcms_band_2GHz_nphy_template;
1063 		if (phy_type == PHY_TYPE_LCN) {
1064 			/* Single stream */
1065 			band->ht_cap.mcs.rx_mask[1] = 0;
1066 			band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
1067 		}
1068 		hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
1069 	} else {
1070 		return -EPERM;
1071 	}
1072 
1073 	/* Assume all bands use the same phy.  True for 11n devices. */
1074 	if (wl->pub->_nbands > 1) {
1075 		if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
1076 			band = &wlc->bandstate[BAND_5G_INDEX]->band;
1077 			*band = brcms_band_5GHz_nphy_template;
1078 			hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
1079 		} else {
1080 			return -EPERM;
1081 		}
1082 	}
1083 	return 0;
1084 }
1085 
1086 /*
1087  * is called in brcms_pci_probe() context, therefore no locking required.
1088  */
1089 static int ieee_hw_init(struct ieee80211_hw *hw)
1090 {
1091 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1092 	ieee80211_hw_set(hw, SIGNAL_DBM);
1093 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
1094 	ieee80211_hw_set(hw, MFP_CAPABLE);
1095 
1096 	hw->extra_tx_headroom = brcms_c_get_header_len();
1097 	hw->queues = N_TX_QUEUES;
1098 	hw->max_rates = 2;	/* Primary rate and 1 fallback rate */
1099 
1100 	/* channel change time is dependent on chip and band  */
1101 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1102 				     BIT(NL80211_IFTYPE_AP) |
1103 				     BIT(NL80211_IFTYPE_ADHOC);
1104 
1105 	/*
1106 	 * deactivate sending probe responses by ucude, because this will
1107 	 * cause problems when WPS is used.
1108 	 *
1109 	 * hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1110 	 */
1111 
1112 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1113 
1114 	hw->rate_control_algorithm = "minstrel_ht";
1115 
1116 	hw->sta_data_size = 0;
1117 	return ieee_hw_rate_init(hw);
1118 }
1119 
1120 /*
1121  * attach to the WL device.
1122  *
1123  * Attach to the WL device identified by vendor and device parameters.
1124  * regs is a host accessible memory address pointing to WL device registers.
1125  *
1126  * is called in brcms_bcma_probe() context, therefore no locking required.
1127  */
1128 static struct brcms_info *brcms_attach(struct bcma_device *pdev)
1129 {
1130 	struct brcms_info *wl = NULL;
1131 	int unit, err;
1132 	struct ieee80211_hw *hw;
1133 	u8 perm[ETH_ALEN];
1134 
1135 	unit = n_adapters_found;
1136 	err = 0;
1137 
1138 	if (unit < 0)
1139 		return NULL;
1140 
1141 	/* allocate private info */
1142 	hw = bcma_get_drvdata(pdev);
1143 	if (hw != NULL)
1144 		wl = hw->priv;
1145 	if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
1146 		return NULL;
1147 	wl->wiphy = hw->wiphy;
1148 
1149 	atomic_set(&wl->callbacks, 0);
1150 
1151 	init_waitqueue_head(&wl->tx_flush_wq);
1152 
1153 	/* setup the bottom half handler */
1154 	tasklet_setup(&wl->tasklet, brcms_dpc);
1155 
1156 	spin_lock_init(&wl->lock);
1157 	spin_lock_init(&wl->isr_lock);
1158 
1159 	/* common load-time initialization */
1160 	wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
1161 	if (!wl->wlc) {
1162 		wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
1163 			  KBUILD_MODNAME, err);
1164 		goto fail;
1165 	}
1166 	wl->pub = brcms_c_pub(wl->wlc);
1167 
1168 	wl->pub->ieee_hw = hw;
1169 
1170 	/* register our interrupt handler */
1171 	if (request_irq(pdev->irq, brcms_isr,
1172 			IRQF_SHARED, KBUILD_MODNAME, wl)) {
1173 		wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
1174 		goto fail;
1175 	}
1176 	wl->irq = pdev->irq;
1177 
1178 	/* register module */
1179 	brcms_c_module_register(wl->pub, "linux", wl, NULL);
1180 
1181 	if (ieee_hw_init(hw)) {
1182 		wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
1183 			  __func__);
1184 		goto fail;
1185 	}
1186 
1187 	brcms_c_regd_init(wl->wlc);
1188 
1189 	memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
1190 	if (WARN_ON(!is_valid_ether_addr(perm)))
1191 		goto fail;
1192 	SET_IEEE80211_PERM_ADDR(hw, perm);
1193 
1194 	err = ieee80211_register_hw(hw);
1195 	if (err)
1196 		wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
1197 			  "%d\n", __func__, err);
1198 
1199 	if (wl->pub->srom_ccode[0] &&
1200 	    regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
1201 		wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
1202 
1203 	brcms_debugfs_attach(wl->pub);
1204 	brcms_debugfs_create_files(wl->pub);
1205 	n_adapters_found++;
1206 	return wl;
1207 
1208 fail:
1209 	brcms_free(wl);
1210 	return NULL;
1211 }
1212 
1213 
1214 
1215 /*
1216  * determines if a device is a WL device, and if so, attaches it.
1217  *
1218  * This function determines if a device pointed to by pdev is a WL device,
1219  * and if so, performs a brcms_attach() on it.
1220  *
1221  * Perimeter lock is initialized in the course of this function.
1222  */
1223 static int brcms_bcma_probe(struct bcma_device *pdev)
1224 {
1225 	struct brcms_info *wl;
1226 	struct ieee80211_hw *hw;
1227 	int ret;
1228 
1229 	dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
1230 		 pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
1231 		 pdev->irq);
1232 
1233 	if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
1234 	    (pdev->id.id != BCMA_CORE_80211))
1235 		return -ENODEV;
1236 
1237 	hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
1238 	if (!hw) {
1239 		pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
1240 		return -ENOMEM;
1241 	}
1242 
1243 	SET_IEEE80211_DEV(hw, &pdev->dev);
1244 
1245 	bcma_set_drvdata(pdev, hw);
1246 
1247 	memset(hw->priv, 0, sizeof(*wl));
1248 
1249 	wl = brcms_attach(pdev);
1250 	if (!wl) {
1251 		pr_err("%s: brcms_attach failed!\n", __func__);
1252 		ret = -ENODEV;
1253 		goto err_free_ieee80211;
1254 	}
1255 	brcms_led_register(wl);
1256 
1257 	return 0;
1258 
1259 err_free_ieee80211:
1260 	ieee80211_free_hw(hw);
1261 	return ret;
1262 }
1263 
1264 static int brcms_suspend(struct bcma_device *pdev)
1265 {
1266 	struct brcms_info *wl;
1267 	struct ieee80211_hw *hw;
1268 
1269 	hw = bcma_get_drvdata(pdev);
1270 	wl = hw->priv;
1271 	if (!wl) {
1272 		pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
1273 		       __func__);
1274 		return -ENODEV;
1275 	}
1276 
1277 	/* only need to flag hw is down for proper resume */
1278 	spin_lock_bh(&wl->lock);
1279 	wl->pub->hw_up = false;
1280 	spin_unlock_bh(&wl->lock);
1281 
1282 	brcms_dbg_info(wl->wlc->hw->d11core, "brcms_suspend ok\n");
1283 
1284 	return 0;
1285 }
1286 
1287 static int brcms_resume(struct bcma_device *pdev)
1288 {
1289 	return 0;
1290 }
1291 
1292 static struct bcma_driver brcms_bcma_driver = {
1293 	.name     = KBUILD_MODNAME,
1294 	.probe    = brcms_bcma_probe,
1295 	.suspend  = brcms_suspend,
1296 	.resume   = brcms_resume,
1297 	.remove   = brcms_remove,
1298 	.id_table = brcms_coreid_table,
1299 };
1300 
1301 /*
1302  * This is the main entry point for the brcmsmac driver.
1303  *
1304  * This function is scheduled upon module initialization and
1305  * does the driver registration, which result in brcms_bcma_probe()
1306  * call resulting in the driver bringup.
1307  */
1308 static void brcms_driver_init(struct work_struct *work)
1309 {
1310 	int error;
1311 
1312 	error = bcma_driver_register(&brcms_bcma_driver);
1313 	if (error)
1314 		pr_err("%s: register returned %d\n", __func__, error);
1315 }
1316 
1317 static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
1318 
1319 static int __init brcms_module_init(void)
1320 {
1321 	brcms_debugfs_init();
1322 	if (!schedule_work(&brcms_driver_work))
1323 		return -EBUSY;
1324 
1325 	return 0;
1326 }
1327 
1328 /*
1329  * This function unloads the brcmsmac driver from the system.
1330  *
1331  * This function unconditionally unloads the brcmsmac driver module from the
1332  * system.
1333  *
1334  */
1335 static void __exit brcms_module_exit(void)
1336 {
1337 	cancel_work_sync(&brcms_driver_work);
1338 	bcma_driver_unregister(&brcms_bcma_driver);
1339 	brcms_debugfs_exit();
1340 }
1341 
1342 module_init(brcms_module_init);
1343 module_exit(brcms_module_exit);
1344 
1345 /*
1346  * precondition: perimeter lock has been acquired
1347  */
1348 void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
1349 			 bool state, int prio)
1350 {
1351 	brcms_err(wl->wlc->hw->d11core, "Shouldn't be here %s\n", __func__);
1352 }
1353 
1354 /*
1355  * precondition: perimeter lock has been acquired
1356  */
1357 void brcms_init(struct brcms_info *wl)
1358 {
1359 	brcms_dbg_info(wl->wlc->hw->d11core, "Initializing wl%d\n",
1360 		       wl->pub->unit);
1361 	brcms_reset(wl);
1362 	brcms_c_init(wl->wlc, wl->mute_tx);
1363 }
1364 
1365 /*
1366  * precondition: perimeter lock has been acquired
1367  */
1368 uint brcms_reset(struct brcms_info *wl)
1369 {
1370 	brcms_dbg_info(wl->wlc->hw->d11core, "Resetting wl%d\n", wl->pub->unit);
1371 	brcms_c_reset(wl->wlc);
1372 
1373 	/* dpc will not be rescheduled */
1374 	wl->resched = false;
1375 
1376 	/* inform publicly that interface is down */
1377 	wl->pub->up = false;
1378 
1379 	return 0;
1380 }
1381 
1382 void brcms_fatal_error(struct brcms_info *wl)
1383 {
1384 	brcms_err(wl->wlc->hw->d11core, "wl%d: fatal error, reinitializing\n",
1385 		  wl->wlc->pub->unit);
1386 	brcms_reset(wl);
1387 	ieee80211_restart_hw(wl->pub->ieee_hw);
1388 }
1389 
1390 /*
1391  * These are interrupt on/off entry points. Disable interrupts
1392  * during interrupt state transition.
1393  */
1394 void brcms_intrson(struct brcms_info *wl)
1395 {
1396 	unsigned long flags;
1397 
1398 	spin_lock_irqsave(&wl->isr_lock, flags);
1399 	brcms_c_intrson(wl->wlc);
1400 	spin_unlock_irqrestore(&wl->isr_lock, flags);
1401 }
1402 
1403 u32 brcms_intrsoff(struct brcms_info *wl)
1404 {
1405 	unsigned long flags;
1406 	u32 status;
1407 
1408 	spin_lock_irqsave(&wl->isr_lock, flags);
1409 	status = brcms_c_intrsoff(wl->wlc);
1410 	spin_unlock_irqrestore(&wl->isr_lock, flags);
1411 	return status;
1412 }
1413 
1414 void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
1415 {
1416 	unsigned long flags;
1417 
1418 	spin_lock_irqsave(&wl->isr_lock, flags);
1419 	brcms_c_intrsrestore(wl->wlc, macintmask);
1420 	spin_unlock_irqrestore(&wl->isr_lock, flags);
1421 }
1422 
1423 /*
1424  * precondition: perimeter lock has been acquired
1425  */
1426 int brcms_up(struct brcms_info *wl)
1427 {
1428 	int error = 0;
1429 
1430 	if (wl->pub->up)
1431 		return 0;
1432 
1433 	error = brcms_c_up(wl->wlc);
1434 
1435 	return error;
1436 }
1437 
1438 /*
1439  * precondition: perimeter lock has been acquired
1440  */
1441 void brcms_down(struct brcms_info *wl)
1442 	__must_hold(&wl->lock)
1443 {
1444 	uint callbacks, ret_val = 0;
1445 
1446 	/* call common down function */
1447 	ret_val = brcms_c_down(wl->wlc);
1448 	callbacks = atomic_read(&wl->callbacks) - ret_val;
1449 
1450 	/* wait for down callbacks to complete */
1451 	spin_unlock_bh(&wl->lock);
1452 
1453 	/* For HIGH_only driver, it's important to actually schedule other work,
1454 	 * not just spin wait since everything runs at schedule level
1455 	 */
1456 	SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1457 
1458 	spin_lock_bh(&wl->lock);
1459 }
1460 
1461 /*
1462 * precondition: perimeter lock is not acquired
1463  */
1464 static void _brcms_timer(struct work_struct *work)
1465 {
1466 	struct brcms_timer *t = container_of(work, struct brcms_timer,
1467 					     dly_wrk.work);
1468 
1469 	spin_lock_bh(&t->wl->lock);
1470 
1471 	if (t->set) {
1472 		if (t->periodic) {
1473 			atomic_inc(&t->wl->callbacks);
1474 			ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
1475 						     &t->dly_wrk,
1476 						     msecs_to_jiffies(t->ms));
1477 		} else {
1478 			t->set = false;
1479 		}
1480 
1481 		t->fn(t->arg);
1482 	}
1483 
1484 	atomic_dec(&t->wl->callbacks);
1485 
1486 	spin_unlock_bh(&t->wl->lock);
1487 }
1488 
1489 /*
1490  * Adds a timer to the list. Caller supplies a timer function.
1491  * Is called from wlc.
1492  *
1493  * precondition: perimeter lock has been acquired
1494  */
1495 struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
1496 				     void (*fn) (void *arg),
1497 				     void *arg, const char *name)
1498 {
1499 	struct brcms_timer *t;
1500 
1501 	t = kzalloc(sizeof(*t), GFP_ATOMIC);
1502 	if (!t)
1503 		return NULL;
1504 
1505 	INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
1506 	t->wl = wl;
1507 	t->fn = fn;
1508 	t->arg = arg;
1509 	t->next = wl->timers;
1510 	wl->timers = t;
1511 
1512 #ifdef DEBUG
1513 	t->name = kstrdup(name, GFP_ATOMIC);
1514 #endif
1515 
1516 	return t;
1517 }
1518 
1519 /*
1520  * adds only the kernel timer since it's going to be more accurate
1521  * as well as it's easier to make it periodic
1522  *
1523  * precondition: perimeter lock has been acquired
1524  */
1525 void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
1526 {
1527 	struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
1528 
1529 #ifdef DEBUG
1530 	if (t->set)
1531 		brcms_dbg_info(t->wl->wlc->hw->d11core,
1532 			       "%s: Already set. Name: %s, per %d\n",
1533 			       __func__, t->name, periodic);
1534 #endif
1535 	t->ms = ms;
1536 	t->periodic = (bool) periodic;
1537 	if (!t->set) {
1538 		t->set = true;
1539 		atomic_inc(&t->wl->callbacks);
1540 	}
1541 
1542 	ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
1543 }
1544 
1545 /*
1546  * return true if timer successfully deleted, false if still pending
1547  *
1548  * precondition: perimeter lock has been acquired
1549  */
1550 bool brcms_del_timer(struct brcms_timer *t)
1551 {
1552 	if (t->set) {
1553 		t->set = false;
1554 		if (!cancel_delayed_work(&t->dly_wrk))
1555 			return false;
1556 
1557 		atomic_dec(&t->wl->callbacks);
1558 	}
1559 
1560 	return true;
1561 }
1562 
1563 /*
1564  * precondition: perimeter lock has been acquired
1565  */
1566 void brcms_free_timer(struct brcms_timer *t)
1567 {
1568 	struct brcms_info *wl = t->wl;
1569 	struct brcms_timer *tmp;
1570 
1571 	/* delete the timer in case it is active */
1572 	brcms_del_timer(t);
1573 
1574 	if (wl->timers == t) {
1575 		wl->timers = wl->timers->next;
1576 #ifdef DEBUG
1577 		kfree(t->name);
1578 #endif
1579 		kfree(t);
1580 		return;
1581 
1582 	}
1583 
1584 	tmp = wl->timers;
1585 	while (tmp) {
1586 		if (tmp->next == t) {
1587 			tmp->next = t->next;
1588 #ifdef DEBUG
1589 			kfree(t->name);
1590 #endif
1591 			kfree(t);
1592 			return;
1593 		}
1594 		tmp = tmp->next;
1595 	}
1596 
1597 }
1598 
1599 /*
1600  * precondition: no locking required
1601  */
1602 int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
1603 {
1604 	int i, entry;
1605 	const u8 *pdata;
1606 	struct firmware_hdr *hdr;
1607 	for (i = 0; i < wl->fw.fw_cnt; i++) {
1608 		hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1609 		for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1610 		     entry++, hdr++) {
1611 			u32 len = le32_to_cpu(hdr->len);
1612 			if (le32_to_cpu(hdr->idx) == idx) {
1613 				pdata = wl->fw.fw_bin[i]->data +
1614 					le32_to_cpu(hdr->offset);
1615 				*pbuf = kvmemdup(pdata, len, GFP_KERNEL);
1616 				if (*pbuf == NULL)
1617 					return -ENOMEM;
1618 				return 0;
1619 			}
1620 		}
1621 	}
1622 	brcms_err(wl->wlc->hw->d11core,
1623 		  "ERROR: ucode buf tag:%d can not be found!\n", idx);
1624 	*pbuf = NULL;
1625 	return -ENODATA;
1626 }
1627 
1628 /*
1629  * Precondition: Since this function is called in brcms_bcma_probe() context,
1630  * no locking is required.
1631  */
1632 int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
1633 {
1634 	int i, entry;
1635 	const u8 *pdata;
1636 	struct firmware_hdr *hdr;
1637 	for (i = 0; i < wl->fw.fw_cnt; i++) {
1638 		hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1639 		for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1640 		     entry++, hdr++) {
1641 			if (le32_to_cpu(hdr->idx) == idx) {
1642 				pdata = wl->fw.fw_bin[i]->data +
1643 					le32_to_cpu(hdr->offset);
1644 				if (le32_to_cpu(hdr->len) != 4) {
1645 					brcms_err(wl->wlc->hw->d11core,
1646 						  "ERROR: fw hdr len\n");
1647 					return -ENOMSG;
1648 				}
1649 				*n_bytes = le32_to_cpu(*((__le32 *) pdata));
1650 				return 0;
1651 			}
1652 		}
1653 	}
1654 	brcms_err(wl->wlc->hw->d11core,
1655 		  "ERROR: ucode tag:%d can not be found!\n", idx);
1656 	return -ENOMSG;
1657 }
1658 
1659 /*
1660  * precondition: can both be called locked and unlocked
1661  */
1662 void brcms_ucode_free_buf(void *p)
1663 {
1664 	kvfree(p);
1665 }
1666 
1667 /*
1668  * checks validity of all firmware images loaded from user space
1669  *
1670  * Precondition: Since this function is called in brcms_bcma_probe() context,
1671  * no locking is required.
1672  */
1673 int brcms_check_firmwares(struct brcms_info *wl)
1674 {
1675 	int i;
1676 	int entry;
1677 	int rc = 0;
1678 	const struct firmware *fw;
1679 	const struct firmware *fw_hdr;
1680 	struct firmware_hdr *ucode_hdr;
1681 	for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
1682 		fw =  wl->fw.fw_bin[i];
1683 		fw_hdr = wl->fw.fw_hdr[i];
1684 		if (fw == NULL && fw_hdr == NULL) {
1685 			break;
1686 		} else if (fw == NULL || fw_hdr == NULL) {
1687 			wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
1688 				  __func__);
1689 			rc = -EBADF;
1690 		} else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
1691 			wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
1692 				"size %zu/%zu\n", __func__, fw_hdr->size,
1693 				sizeof(struct firmware_hdr));
1694 			rc = -EBADF;
1695 		} else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
1696 			wiphy_err(wl->wiphy, "%s: out of bounds fw file size %zu\n",
1697 				  __func__, fw->size);
1698 			rc = -EBADF;
1699 		} else {
1700 			/* check if ucode section overruns firmware image */
1701 			ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
1702 			for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
1703 			     !rc; entry++, ucode_hdr++) {
1704 				if (le32_to_cpu(ucode_hdr->offset) +
1705 				    le32_to_cpu(ucode_hdr->len) >
1706 				    fw->size) {
1707 					wiphy_err(wl->wiphy,
1708 						  "%s: conflicting bin/hdr\n",
1709 						  __func__);
1710 					rc = -EBADF;
1711 				}
1712 			}
1713 		}
1714 	}
1715 	if (rc == 0 && wl->fw.fw_cnt != i) {
1716 		wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
1717 			wl->fw.fw_cnt);
1718 		rc = -EBADF;
1719 	}
1720 	return rc;
1721 }
1722 
1723 /*
1724  * precondition: perimeter lock has been acquired
1725  */
1726 bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
1727 	__must_hold(&wl->lock)
1728 {
1729 	bool blocked = brcms_c_check_radio_disabled(wl->wlc);
1730 
1731 	spin_unlock_bh(&wl->lock);
1732 	wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
1733 	if (blocked)
1734 		wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
1735 	spin_lock_bh(&wl->lock);
1736 	return blocked;
1737 }
1738