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