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