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