blob: 7fc49ca3f59793a7d5aa9d900266c288ab8c0c59 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
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__
Joe Perches02f77192012-01-15 00:38:44 -080018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Arend van Spriel5b435de2011-10-05 13:19:03 +020019
20#include <linux/etherdevice.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020021#include <linux/sched.h>
22#include <linux/firmware.h>
23#include <linux/interrupt.h>
Paul Gortmaker45296232011-08-30 17:50:46 -040024#include <linux/module.h>
Arend van Spriel2e7565602011-12-08 15:06:46 -080025#include <linux/bcma/bcma.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020026#include <net/mac80211.h>
27#include <defs.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020028#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"
Seth Forsheeb353dda2012-11-15 08:08:03 -060036#include "debug.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020037
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 | \
Arend van Spriel5b435de2011-10-05 13:19:03 +020044 FIF_CONTROL | \
45 FIF_OTHER_BSS | \
Alwin Beukersbe667662011-11-22 17:21:43 -080046 FIF_BCN_PRBRESP_PROMISC | \
47 FIF_PSPOLL)
Arend van Spriel5b435de2011-10-05 13:19:03 +020048
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
73struct firmware_hdr {
74 __le32 offset;
75 __le32 len;
76 __le32 idx;
77};
78
79static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
80 "brcm/bcm43xx",
81 NULL
82};
83
84static int n_adapters_found;
85
86MODULE_AUTHOR("Broadcom Corporation");
87MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
88MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
89MODULE_LICENSE("Dual BSD/GPL");
Jeff Mahoneyfaa97bd4a2012-08-06 15:17:26 -040090/* This needs to be adjusted when brcms_firmwares changes */
91MODULE_FIRMWARE("brcm/bcm43xx-0.fw");
92MODULE_FIRMWARE("brcm/bcm43xx_hdr-0.fw");
Arend van Spriel5b435de2011-10-05 13:19:03 +020093
Arend van Spriel2e7565602011-12-08 15:06:46 -080094/* recognized BCMA Core IDs */
95static struct bcma_device_id brcms_coreid_table[] = {
Hauke Mehrtens6f80f012012-12-07 00:35:53 +010096 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 17, BCMA_ANY_CLASS),
Arend van Sprieleb032f02011-12-12 15:15:12 -080097 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 23, BCMA_ANY_CLASS),
98 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 24, BCMA_ANY_CLASS),
Arend van Spriel2e7565602011-12-08 15:06:46 -080099 BCMA_CORETABLE_END
100};
101MODULE_DEVICE_TABLE(bcma, brcms_coreid_table);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200102
Seth Forsheeb0341742012-11-15 08:08:01 -0600103#if defined(CONFIG_BRCMDBG)
104/*
105 * Module parameter for setting the debug message level. Available
106 * flags are specified by the BRCM_DL_* macros in
107 * drivers/net/wireless/brcm80211/include/defs.h.
108 */
109module_param_named(debug, brcm_msg_level, uint, S_IRUGO | S_IWUSR);
110#endif
Arend van Spriel5b435de2011-10-05 13:19:03 +0200111
112static struct ieee80211_channel brcms_2ghz_chantable[] = {
113 CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
114 CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
115 CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
116 CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
117 CHAN2GHZ(5, 2432, 0),
118 CHAN2GHZ(6, 2437, 0),
119 CHAN2GHZ(7, 2442, 0),
120 CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
121 CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
122 CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
123 CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
124 CHAN2GHZ(12, 2467,
125 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
126 IEEE80211_CHAN_NO_HT40PLUS),
127 CHAN2GHZ(13, 2472,
128 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
129 IEEE80211_CHAN_NO_HT40PLUS),
130 CHAN2GHZ(14, 2484,
131 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
Seth Forshee7f38e5b2012-08-01 15:58:43 -0500132 IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS |
133 IEEE80211_CHAN_NO_OFDM)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200134};
135
136static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
137 /* UNII-1 */
138 CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
139 CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
140 CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
141 CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
142 /* UNII-2 */
143 CHAN5GHZ(52,
144 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
145 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
146 CHAN5GHZ(56,
147 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
148 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
149 CHAN5GHZ(60,
150 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
151 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
152 CHAN5GHZ(64,
153 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
154 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
155 /* MID */
156 CHAN5GHZ(100,
157 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
158 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
159 CHAN5GHZ(104,
160 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
161 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
162 CHAN5GHZ(108,
163 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
164 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
165 CHAN5GHZ(112,
166 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
167 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
168 CHAN5GHZ(116,
169 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
170 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
171 CHAN5GHZ(120,
172 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
173 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
174 CHAN5GHZ(124,
175 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
176 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
177 CHAN5GHZ(128,
178 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
179 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
180 CHAN5GHZ(132,
181 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
182 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
183 CHAN5GHZ(136,
184 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
185 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
186 CHAN5GHZ(140,
187 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
188 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS |
189 IEEE80211_CHAN_NO_HT40MINUS),
190 /* UNII-3 */
191 CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
192 CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
193 CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
194 CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
195 CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
196};
197
198/*
199 * The rate table is used for both 2.4G and 5G rates. The
200 * latter being a subset as it does not support CCK rates.
201 */
202static struct ieee80211_rate legacy_ratetable[] = {
203 RATE(10, 0),
204 RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
205 RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
206 RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
207 RATE(60, 0),
208 RATE(90, 0),
209 RATE(120, 0),
210 RATE(180, 0),
211 RATE(240, 0),
212 RATE(360, 0),
213 RATE(480, 0),
214 RATE(540, 0),
215};
216
217static const struct ieee80211_supported_band brcms_band_2GHz_nphy_template = {
218 .band = IEEE80211_BAND_2GHZ,
219 .channels = brcms_2ghz_chantable,
220 .n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
221 .bitrates = legacy_ratetable,
222 .n_bitrates = ARRAY_SIZE(legacy_ratetable),
223 .ht_cap = {
224 /* from include/linux/ieee80211.h */
225 .cap = IEEE80211_HT_CAP_GRN_FLD |
Arend van Spriel6b1a89a2011-10-18 14:02:59 +0200226 IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200227 .ht_supported = true,
228 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
229 .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
230 .mcs = {
231 /* placeholders for now */
232 .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
233 .rx_highest = cpu_to_le16(500),
234 .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
235 }
236};
237
238static const struct ieee80211_supported_band brcms_band_5GHz_nphy_template = {
239 .band = IEEE80211_BAND_5GHZ,
240 .channels = brcms_5ghz_nphy_chantable,
241 .n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
242 .bitrates = legacy_ratetable + BRCMS_LEGACY_5G_RATE_OFFSET,
243 .n_bitrates = ARRAY_SIZE(legacy_ratetable) -
244 BRCMS_LEGACY_5G_RATE_OFFSET,
245 .ht_cap = {
246 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
Arend van Spriel6b1a89a2011-10-18 14:02:59 +0200247 IEEE80211_HT_CAP_SGI_40,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200248 .ht_supported = true,
249 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
250 .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
251 .mcs = {
252 /* placeholders for now */
253 .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
254 .rx_highest = cpu_to_le16(500),
255 .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
256 }
257};
258
259/* flags the given rate in rateset as requested */
260static void brcms_set_basic_rate(struct brcm_rateset *rs, u16 rate, bool is_br)
261{
262 u32 i;
263
264 for (i = 0; i < rs->count; i++) {
265 if (rate != (rs->rates[i] & 0x7f))
266 continue;
267
268 if (is_br)
269 rs->rates[i] |= BRCMS_RATE_FLAG;
270 else
271 rs->rates[i] &= BRCMS_RATE_MASK;
272 return;
273 }
274}
275
Thomas Huehn36323f82012-07-23 21:33:42 +0200276static void brcms_ops_tx(struct ieee80211_hw *hw,
277 struct ieee80211_tx_control *control,
278 struct sk_buff *skb)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200279{
280 struct brcms_info *wl = hw->priv;
Thomas Huehn644e8c02012-07-10 14:01:37 +0200281 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200282
283 spin_lock_bh(&wl->lock);
284 if (!wl->pub->up) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600285 brcms_err(wl->wlc->hw->d11core, "ops->tx called while down\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200286 kfree_skb(skb);
287 goto done;
288 }
Piotr Haberc4dea352012-11-28 21:44:05 +0100289 if (brcms_c_sendpkt_mac80211(wl->wlc, skb, hw))
290 tx_info->rate_driver_data[0] = control->sta;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200291 done:
292 spin_unlock_bh(&wl->lock);
293}
294
295static int brcms_ops_start(struct ieee80211_hw *hw)
296{
297 struct brcms_info *wl = hw->priv;
298 bool blocked;
Roland Vossen2646c462011-10-21 16:16:27 +0200299 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200300
301 ieee80211_wake_queues(hw);
302 spin_lock_bh(&wl->lock);
303 blocked = brcms_rfkill_set_hw_state(wl);
304 spin_unlock_bh(&wl->lock);
305 if (!blocked)
306 wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
307
Roland Vossen2646c462011-10-21 16:16:27 +0200308 spin_lock_bh(&wl->lock);
Roland Vossendc460122011-10-21 16:16:28 +0200309 /* avoid acknowledging frames before a non-monitor device is added */
310 wl->mute_tx = true;
311
Roland Vossen2646c462011-10-21 16:16:27 +0200312 if (!wl->pub->up)
Piotr Haber82d8eba2012-09-19 22:21:15 +0200313 if (!blocked)
314 err = brcms_up(wl);
315 else
316 err = -ERFKILL;
Roland Vossen2646c462011-10-21 16:16:27 +0200317 else
318 err = -ENODEV;
319 spin_unlock_bh(&wl->lock);
320
321 if (err != 0)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600322 brcms_err(wl->wlc->hw->d11core, "%s: brcms_up() returned %d\n",
323 __func__, err);
Roland Vossen2646c462011-10-21 16:16:27 +0200324 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200325}
326
327static void brcms_ops_stop(struct ieee80211_hw *hw)
328{
Roland Vossen2646c462011-10-21 16:16:27 +0200329 struct brcms_info *wl = hw->priv;
330 int status;
331
Arend van Spriel5b435de2011-10-05 13:19:03 +0200332 ieee80211_stop_queues(hw);
Roland Vossen2646c462011-10-21 16:16:27 +0200333
334 if (wl->wlc == NULL)
335 return;
336
337 spin_lock_bh(&wl->lock);
Hauke Mehrtenscacaa642012-06-30 15:16:19 +0200338 status = brcms_c_chipmatch(wl->wlc->hw->d11core);
Roland Vossen2646c462011-10-21 16:16:27 +0200339 spin_unlock_bh(&wl->lock);
340 if (!status) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600341 brcms_err(wl->wlc->hw->d11core,
Roland Vossen2646c462011-10-21 16:16:27 +0200342 "wl: brcms_ops_stop: chipmatch failed\n");
343 return;
344 }
345
346 /* put driver in down state */
347 spin_lock_bh(&wl->lock);
348 brcms_down(wl);
349 spin_unlock_bh(&wl->lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200350}
351
352static int
353brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
354{
Roland Vossendc460122011-10-21 16:16:28 +0200355 struct brcms_info *wl = hw->priv;
356
Arend van Spriel5b435de2011-10-05 13:19:03 +0200357 /* Just STA for now */
Roland Vossen15256622011-10-21 16:16:29 +0200358 if (vif->type != NL80211_IFTYPE_STATION) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600359 brcms_err(wl->wlc->hw->d11core,
360 "%s: Attempt to add type %d, only STA for now\n",
361 __func__, vif->type);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200362 return -EOPNOTSUPP;
363 }
364
Arend van Spriel66578c02013-01-02 15:22:35 +0100365 spin_lock_bh(&wl->lock);
Arend van Sprielb180b102013-01-02 15:22:36 +0100366 memcpy(wl->pub->cur_etheraddr, vif->addr, sizeof(vif->addr));
Roland Vossendc460122011-10-21 16:16:28 +0200367 wl->mute_tx = false;
368 brcms_c_mute(wl->wlc, false);
Arend van Spriel66578c02013-01-02 15:22:35 +0100369 spin_unlock_bh(&wl->lock);
Roland Vossendc460122011-10-21 16:16:28 +0200370
Roland Vossen2646c462011-10-21 16:16:27 +0200371 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200372}
373
374static void
375brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
376{
Arend van Spriel5b435de2011-10-05 13:19:03 +0200377}
378
379static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
380{
381 struct ieee80211_conf *conf = &hw->conf;
382 struct brcms_info *wl = hw->priv;
Seth Forsheeb353dda2012-11-15 08:08:03 -0600383 struct bcma_device *core = wl->wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200384 int err = 0;
385 int new_int;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200386
387 spin_lock_bh(&wl->lock);
388 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
389 brcms_c_set_beacon_listen_interval(wl->wlc,
390 conf->listen_interval);
391 }
392 if (changed & IEEE80211_CONF_CHANGE_MONITOR)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600393 brcms_dbg_info(core, "%s: change monitor mode: %s\n",
394 __func__, conf->flags & IEEE80211_CONF_MONITOR ?
395 "true" : "false");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200396 if (changed & IEEE80211_CONF_CHANGE_PS)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600397 brcms_err(core, "%s: change power-save mode: %s (implement)\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200398 __func__, conf->flags & IEEE80211_CONF_PS ?
399 "true" : "false");
400
401 if (changed & IEEE80211_CONF_CHANGE_POWER) {
402 err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
403 if (err < 0) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600404 brcms_err(core, "%s: Error setting power_level\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200405 __func__);
406 goto config_out;
407 }
408 new_int = brcms_c_get_tx_power(wl->wlc);
409 if (new_int != conf->power_level)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600410 brcms_err(core,
411 "%s: Power level req != actual, %d %d\n",
412 __func__, conf->power_level,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200413 new_int);
414 }
415 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
416 if (conf->channel_type == NL80211_CHAN_HT20 ||
417 conf->channel_type == NL80211_CHAN_NO_HT)
418 err = brcms_c_set_channel(wl->wlc,
419 conf->channel->hw_value);
420 else
421 err = -ENOTSUPP;
422 }
423 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
424 err = brcms_c_set_rate_limit(wl->wlc,
425 conf->short_frame_max_tx_count,
426 conf->long_frame_max_tx_count);
427
428 config_out:
429 spin_unlock_bh(&wl->lock);
430 return err;
431}
432
433static void
434brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
435 struct ieee80211_vif *vif,
436 struct ieee80211_bss_conf *info, u32 changed)
437{
438 struct brcms_info *wl = hw->priv;
Seth Forsheeb353dda2012-11-15 08:08:03 -0600439 struct bcma_device *core = wl->wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200440
441 if (changed & BSS_CHANGED_ASSOC) {
442 /* association status changed (associated/disassociated)
443 * also implies a change in the AID.
444 */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600445 brcms_err(core, "%s: %s: %sassociated\n", KBUILD_MODNAME,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200446 __func__, info->assoc ? "" : "dis");
447 spin_lock_bh(&wl->lock);
448 brcms_c_associate_upd(wl->wlc, info->assoc);
449 spin_unlock_bh(&wl->lock);
450 }
451 if (changed & BSS_CHANGED_ERP_SLOT) {
452 s8 val;
453
454 /* slot timing changed */
455 if (info->use_short_slot)
456 val = 1;
457 else
458 val = 0;
459 spin_lock_bh(&wl->lock);
460 brcms_c_set_shortslot_override(wl->wlc, val);
461 spin_unlock_bh(&wl->lock);
462 }
463
464 if (changed & BSS_CHANGED_HT) {
465 /* 802.11n parameters changed */
466 u16 mode = info->ht_operation_mode;
467
468 spin_lock_bh(&wl->lock);
469 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
470 mode & IEEE80211_HT_OP_MODE_PROTECTION);
471 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
472 mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
473 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
474 mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
475 spin_unlock_bh(&wl->lock);
476 }
477 if (changed & BSS_CHANGED_BASIC_RATES) {
478 struct ieee80211_supported_band *bi;
479 u32 br_mask, i;
480 u16 rate;
481 struct brcm_rateset rs;
482 int error;
483
484 /* retrieve the current rates */
485 spin_lock_bh(&wl->lock);
486 brcms_c_get_current_rateset(wl->wlc, &rs);
487 spin_unlock_bh(&wl->lock);
488
489 br_mask = info->basic_rates;
490 bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
491 for (i = 0; i < bi->n_bitrates; i++) {
492 /* convert to internal rate value */
493 rate = (bi->bitrates[i].bitrate << 1) / 10;
494
495 /* set/clear basic rate flag */
496 brcms_set_basic_rate(&rs, rate, br_mask & 1);
497 br_mask >>= 1;
498 }
499
500 /* update the rate set */
501 spin_lock_bh(&wl->lock);
502 error = brcms_c_set_rateset(wl->wlc, &rs);
503 spin_unlock_bh(&wl->lock);
504 if (error)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600505 brcms_err(core, "changing basic rates failed: %d\n",
Arend van Spriel5b435de2011-10-05 13:19:03 +0200506 error);
507 }
508 if (changed & BSS_CHANGED_BEACON_INT) {
509 /* Beacon interval changed */
510 spin_lock_bh(&wl->lock);
511 brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
512 spin_unlock_bh(&wl->lock);
513 }
514 if (changed & BSS_CHANGED_BSSID) {
515 /* BSSID changed, for whatever reason (IBSS and managed mode) */
516 spin_lock_bh(&wl->lock);
517 brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
518 spin_unlock_bh(&wl->lock);
519 }
520 if (changed & BSS_CHANGED_BEACON)
521 /* Beacon data changed, retrieve new beacon (beaconing modes) */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600522 brcms_err(core, "%s: beacon changed\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200523
524 if (changed & BSS_CHANGED_BEACON_ENABLED) {
525 /* Beaconing should be enabled/disabled (beaconing modes) */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600526 brcms_err(core, "%s: Beacon enabled: %s\n", __func__,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200527 info->enable_beacon ? "true" : "false");
528 }
529
530 if (changed & BSS_CHANGED_CQM) {
531 /* Connection quality monitor config changed */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600532 brcms_err(core, "%s: cqm change: threshold %d, hys %d "
Arend van Spriel5b435de2011-10-05 13:19:03 +0200533 " (implement)\n", __func__, info->cqm_rssi_thold,
534 info->cqm_rssi_hyst);
535 }
536
537 if (changed & BSS_CHANGED_IBSS) {
538 /* IBSS join status changed */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600539 brcms_err(core, "%s: IBSS joined: %s (implement)\n",
540 __func__, info->ibss_joined ? "true" : "false");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200541 }
542
543 if (changed & BSS_CHANGED_ARP_FILTER) {
544 /* Hardware ARP filter address list or state changed */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600545 brcms_err(core, "%s: arp filtering: enabled %s, count %d"
Arend van Spriel5b435de2011-10-05 13:19:03 +0200546 " (implement)\n", __func__, info->arp_filter_enabled ?
547 "true" : "false", info->arp_addr_cnt);
548 }
549
550 if (changed & BSS_CHANGED_QOS) {
551 /*
552 * QoS for this association was enabled/disabled.
553 * Note that it is only ever disabled for station mode.
554 */
Seth Forsheeb353dda2012-11-15 08:08:03 -0600555 brcms_err(core, "%s: qos enabled: %s (implement)\n",
556 __func__, info->qos ? "true" : "false");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200557 }
558 return;
559}
560
561static void
562brcms_ops_configure_filter(struct ieee80211_hw *hw,
563 unsigned int changed_flags,
564 unsigned int *total_flags, u64 multicast)
565{
566 struct brcms_info *wl = hw->priv;
Seth Forsheeb353dda2012-11-15 08:08:03 -0600567 struct bcma_device *core = wl->wlc->hw->d11core;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200568
569 changed_flags &= MAC_FILTERS;
570 *total_flags &= MAC_FILTERS;
Alwin Beukersbe667662011-11-22 17:21:43 -0800571
Arend van Spriel5b435de2011-10-05 13:19:03 +0200572 if (changed_flags & FIF_PROMISC_IN_BSS)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600573 brcms_dbg_info(core, "FIF_PROMISC_IN_BSS\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200574 if (changed_flags & FIF_ALLMULTI)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600575 brcms_dbg_info(core, "FIF_ALLMULTI\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200576 if (changed_flags & FIF_FCSFAIL)
Seth Forsheead667862012-11-21 09:24:31 -0600577 brcms_dbg_info(core, "FIF_FCSFAIL\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200578 if (changed_flags & FIF_CONTROL)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600579 brcms_dbg_info(core, "FIF_CONTROL\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200580 if (changed_flags & FIF_OTHER_BSS)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600581 brcms_dbg_info(core, "FIF_OTHER_BSS\n");
Alwin Beukersbe667662011-11-22 17:21:43 -0800582 if (changed_flags & FIF_PSPOLL)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600583 brcms_dbg_info(core, "FIF_PSPOLL\n");
Alwin Beukersbe667662011-11-22 17:21:43 -0800584 if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
Seth Forsheeb353dda2012-11-15 08:08:03 -0600585 brcms_dbg_info(core, "FIF_BCN_PRBRESP_PROMISC\n");
Alwin Beukersbe667662011-11-22 17:21:43 -0800586
587 spin_lock_bh(&wl->lock);
588 brcms_c_mac_promisc(wl->wlc, *total_flags);
589 spin_unlock_bh(&wl->lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200590 return;
591}
592
593static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw)
594{
595 struct brcms_info *wl = hw->priv;
596 spin_lock_bh(&wl->lock);
597 brcms_c_scan_start(wl->wlc);
598 spin_unlock_bh(&wl->lock);
599 return;
600}
601
602static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw)
603{
604 struct brcms_info *wl = hw->priv;
605 spin_lock_bh(&wl->lock);
606 brcms_c_scan_stop(wl->wlc);
607 spin_unlock_bh(&wl->lock);
608 return;
609}
610
611static int
612brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
613 const struct ieee80211_tx_queue_params *params)
614{
615 struct brcms_info *wl = hw->priv;
616
617 spin_lock_bh(&wl->lock);
618 brcms_c_wme_setparams(wl->wlc, queue, params, true);
619 spin_unlock_bh(&wl->lock);
620
621 return 0;
622}
623
624static int
625brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
626 struct ieee80211_sta *sta)
627{
628 struct brcms_info *wl = hw->priv;
629 struct scb *scb = &wl->wlc->pri_scb;
630
631 brcms_c_init_scb(scb);
632
633 wl->pub->global_ampdu = &(scb->scb_ampdu);
634 wl->pub->global_ampdu->scb = scb;
635 wl->pub->global_ampdu->max_pdu = 16;
636
Arend van Spriel5b435de2011-10-05 13:19:03 +0200637 /*
638 * minstrel_ht initiates addBA on our behalf by calling
639 * ieee80211_start_tx_ba_session()
640 */
641 return 0;
642}
643
644static int
645brcms_ops_ampdu_action(struct ieee80211_hw *hw,
646 struct ieee80211_vif *vif,
647 enum ieee80211_ampdu_mlme_action action,
648 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
649 u8 buf_size)
650{
651 struct brcms_info *wl = hw->priv;
652 struct scb *scb = &wl->wlc->pri_scb;
653 int status;
654
655 if (WARN_ON(scb->magic != SCB_MAGIC))
656 return -EIDRM;
657 switch (action) {
658 case IEEE80211_AMPDU_RX_START:
659 break;
660 case IEEE80211_AMPDU_RX_STOP:
661 break;
662 case IEEE80211_AMPDU_TX_START:
663 spin_lock_bh(&wl->lock);
664 status = brcms_c_aggregatable(wl->wlc, tid);
665 spin_unlock_bh(&wl->lock);
666 if (!status) {
Seth Forsheeb353dda2012-11-15 08:08:03 -0600667 brcms_err(wl->wlc->hw->d11core,
668 "START: tid %d is not agg\'able\n", tid);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200669 return -EINVAL;
670 }
671 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
672 break;
673
Johannes Berg18b559d2012-07-18 13:51:25 +0200674 case IEEE80211_AMPDU_TX_STOP_CONT:
675 case IEEE80211_AMPDU_TX_STOP_FLUSH:
676 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Arend van Spriel5b435de2011-10-05 13:19:03 +0200677 spin_lock_bh(&wl->lock);
678 brcms_c_ampdu_flush(wl->wlc, sta, tid);
679 spin_unlock_bh(&wl->lock);
680 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
681 break;
682 case IEEE80211_AMPDU_TX_OPERATIONAL:
683 /*
684 * BA window size from ADDBA response ('buf_size') defines how
685 * many outstanding MPDUs are allowed for the BA stream by
686 * recipient and traffic class. 'ampdu_factor' gives maximum
687 * AMPDU size.
688 */
689 spin_lock_bh(&wl->lock);
690 brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
691 (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
692 sta->ht_cap.ampdu_factor)) - 1);
693 spin_unlock_bh(&wl->lock);
694 /* Power save wakeup */
695 break;
696 default:
Seth Forsheeb353dda2012-11-15 08:08:03 -0600697 brcms_err(wl->wlc->hw->d11core,
698 "%s: Invalid command, ignoring\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200699 }
700
701 return 0;
702}
703
704static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
705{
706 struct brcms_info *wl = hw->priv;
707 bool blocked;
708
709 spin_lock_bh(&wl->lock);
710 blocked = brcms_c_check_radio_disabled(wl->wlc);
711 spin_unlock_bh(&wl->lock);
712
713 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
714}
715
716static void brcms_ops_flush(struct ieee80211_hw *hw, bool drop)
717{
718 struct brcms_info *wl = hw->priv;
719
720 no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
721
722 /* wait for packet queue and dma fifos to run empty */
723 spin_lock_bh(&wl->lock);
724 brcms_c_wait_for_tx_completion(wl->wlc, drop);
725 spin_unlock_bh(&wl->lock);
726}
727
728static const struct ieee80211_ops brcms_ops = {
729 .tx = brcms_ops_tx,
730 .start = brcms_ops_start,
731 .stop = brcms_ops_stop,
732 .add_interface = brcms_ops_add_interface,
733 .remove_interface = brcms_ops_remove_interface,
734 .config = brcms_ops_config,
735 .bss_info_changed = brcms_ops_bss_info_changed,
736 .configure_filter = brcms_ops_configure_filter,
737 .sw_scan_start = brcms_ops_sw_scan_start,
738 .sw_scan_complete = brcms_ops_sw_scan_complete,
739 .conf_tx = brcms_ops_conf_tx,
740 .sta_add = brcms_ops_sta_add,
741 .ampdu_action = brcms_ops_ampdu_action,
742 .rfkill_poll = brcms_ops_rfkill_poll,
743 .flush = brcms_ops_flush,
744};
745
Arend van Spriel5b435de2011-10-05 13:19:03 +0200746void brcms_dpc(unsigned long data)
747{
748 struct brcms_info *wl;
749
750 wl = (struct brcms_info *) data;
751
752 spin_lock_bh(&wl->lock);
753
754 /* call the common second level interrupt handler */
755 if (wl->pub->up) {
756 if (wl->resched) {
757 unsigned long flags;
758
759 spin_lock_irqsave(&wl->isr_lock, flags);
760 brcms_c_intrsupd(wl->wlc);
761 spin_unlock_irqrestore(&wl->isr_lock, flags);
762 }
763
764 wl->resched = brcms_c_dpc(wl->wlc, true);
765 }
766
767 /* brcms_c_dpc() may bring the driver down */
768 if (!wl->pub->up)
769 goto done;
770
771 /* re-schedule dpc */
772 if (wl->resched)
773 tasklet_schedule(&wl->tasklet);
774 else
775 /* re-enable interrupts */
776 brcms_intrson(wl);
777
778 done:
779 spin_unlock_bh(&wl->lock);
780}
781
782/*
783 * Precondition: Since this function is called in brcms_pci_probe() context,
784 * no locking is required.
785 */
Hauke Mehrtens00bcda42012-04-29 02:50:41 +0200786static int brcms_request_fw(struct brcms_info *wl, struct bcma_device *pdev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200787{
788 int status;
789 struct device *device = &pdev->dev;
790 char fw_name[100];
791 int i;
792
793 memset(&wl->fw, 0, sizeof(struct brcms_firmware));
794 for (i = 0; i < MAX_FW_IMAGES; i++) {
795 if (brcms_firmwares[i] == NULL)
796 break;
797 sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
798 UCODE_LOADER_API_VER);
799 status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
800 if (status) {
801 wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
802 KBUILD_MODNAME, fw_name);
803 return status;
804 }
805 sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
806 UCODE_LOADER_API_VER);
807 status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
808 if (status) {
809 wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
810 KBUILD_MODNAME, fw_name);
811 return status;
812 }
813 wl->fw.hdr_num_entries[i] =
814 wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
815 }
816 wl->fw.fw_cnt = i;
817 return brcms_ucode_data_init(wl, &wl->ucode);
818}
819
820/*
821 * Precondition: Since this function is called in brcms_pci_probe() context,
822 * no locking is required.
823 */
824static void brcms_release_fw(struct brcms_info *wl)
825{
826 int i;
827 for (i = 0; i < MAX_FW_IMAGES; i++) {
828 release_firmware(wl->fw.fw_bin[i]);
829 release_firmware(wl->fw.fw_hdr[i]);
830 }
831}
832
833/**
834 * This function frees the WL per-device resources.
835 *
836 * This function frees resources owned by the WL device pointed to
837 * by the wl parameter.
838 *
839 * precondition: can both be called locked and unlocked
840 *
841 */
842static void brcms_free(struct brcms_info *wl)
843{
844 struct brcms_timer *t, *next;
845
846 /* free ucode data */
847 if (wl->fw.fw_cnt)
848 brcms_ucode_data_free(&wl->ucode);
849 if (wl->irq)
850 free_irq(wl->irq, wl);
851
852 /* kill dpc */
853 tasklet_kill(&wl->tasklet);
854
Piotr Haber8e21df22012-11-28 21:44:08 +0100855 if (wl->pub) {
856 brcms_debugfs_detach(wl->pub);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200857 brcms_c_module_unregister(wl->pub, "linux", wl);
Piotr Haber8e21df22012-11-28 21:44:08 +0100858 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200859
860 /* free common resources */
861 if (wl->wlc) {
862 brcms_c_detach(wl->wlc);
863 wl->wlc = NULL;
864 wl->pub = NULL;
865 }
866
867 /* virtual interface deletion is deferred so we cannot spinwait */
868
869 /* wait for all pending callbacks to complete */
870 while (atomic_read(&wl->callbacks) > 0)
871 schedule();
872
873 /* free timers */
874 for (t = wl->timers; t; t = next) {
875 next = t->next;
Joe Perches8ae74652012-01-15 00:38:38 -0800876#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200877 kfree(t->name);
878#endif
879 kfree(t);
880 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200881}
882
883/*
Roland Vossen2646c462011-10-21 16:16:27 +0200884* called from both kernel as from this kernel module (error flow on attach)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200885* precondition: perimeter lock is not acquired.
886*/
Arend van Spriel2e7565602011-12-08 15:06:46 -0800887static void brcms_remove(struct bcma_device *pdev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200888{
Arend van Spriel2e7565602011-12-08 15:06:46 -0800889 struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
Roland Vossen2646c462011-10-21 16:16:27 +0200890 struct brcms_info *wl = hw->priv;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200891
Arend van Spriel5b435de2011-10-05 13:19:03 +0200892 if (wl->wlc) {
893 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
894 wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
895 ieee80211_unregister_hw(hw);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200896 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200897
898 brcms_free(wl);
899
Arend van Spriel2e7565602011-12-08 15:06:46 -0800900 bcma_set_drvdata(pdev, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200901 ieee80211_free_hw(hw);
902}
903
904static irqreturn_t brcms_isr(int irq, void *dev_id)
905{
906 struct brcms_info *wl;
Piotr Haber94d99022012-11-28 21:44:06 +0100907 irqreturn_t ret = IRQ_NONE;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200908
909 wl = (struct brcms_info *) dev_id;
910
911 spin_lock(&wl->isr_lock);
912
913 /* call common first level interrupt handler */
Piotr Haber94d99022012-11-28 21:44:06 +0100914 if (brcms_c_isr(wl->wlc)) {
915 /* schedule second level handler */
916 tasklet_schedule(&wl->tasklet);
917 ret = IRQ_HANDLED;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200918 }
919
920 spin_unlock(&wl->isr_lock);
921
Piotr Haber94d99022012-11-28 21:44:06 +0100922 return ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200923}
924
925/*
926 * is called in brcms_pci_probe() context, therefore no locking required.
927 */
928static int ieee_hw_rate_init(struct ieee80211_hw *hw)
929{
930 struct brcms_info *wl = hw->priv;
931 struct brcms_c_info *wlc = wl->wlc;
932 struct ieee80211_supported_band *band;
933 int has_5g = 0;
934 u16 phy_type;
935
936 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
937 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
938
939 phy_type = brcms_c_get_phy_type(wl->wlc, 0);
940 if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
941 band = &wlc->bandstate[BAND_2G_INDEX]->band;
942 *band = brcms_band_2GHz_nphy_template;
943 if (phy_type == PHY_TYPE_LCN) {
944 /* Single stream */
945 band->ht_cap.mcs.rx_mask[1] = 0;
Arend van Sprieldf4492f2011-10-12 20:51:15 +0200946 band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200947 }
948 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
949 } else {
950 return -EPERM;
951 }
952
953 /* Assume all bands use the same phy. True for 11n devices. */
954 if (wl->pub->_nbands > 1) {
955 has_5g++;
956 if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
957 band = &wlc->bandstate[BAND_5G_INDEX]->band;
958 *band = brcms_band_5GHz_nphy_template;
959 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
960 } else {
961 return -EPERM;
962 }
963 }
964 return 0;
965}
966
967/*
968 * is called in brcms_pci_probe() context, therefore no locking required.
969 */
970static int ieee_hw_init(struct ieee80211_hw *hw)
971{
972 hw->flags = IEEE80211_HW_SIGNAL_DBM
973 /* | IEEE80211_HW_CONNECTION_MONITOR What is this? */
974 | IEEE80211_HW_REPORTS_TX_ACK_STATUS
975 | IEEE80211_HW_AMPDU_AGGREGATION;
976
977 hw->extra_tx_headroom = brcms_c_get_header_len();
978 hw->queues = N_TX_QUEUES;
979 hw->max_rates = 2; /* Primary rate and 1 fallback rate */
980
981 /* channel change time is dependent on chip and band */
982 hw->channel_change_time = 7 * 1000;
983 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
984
985 hw->rate_control_algorithm = "minstrel_ht";
986
987 hw->sta_data_size = 0;
988 return ieee_hw_rate_init(hw);
989}
990
991/**
992 * attach to the WL device.
993 *
994 * Attach to the WL device identified by vendor and device parameters.
995 * regs is a host accessible memory address pointing to WL device registers.
996 *
997 * brcms_attach is not defined as static because in the case where no bus
998 * is defined, wl_attach will never be called, and thus, gcc will issue
999 * a warning that this function is defined but not used if we declare
1000 * it as static.
1001 *
1002 *
Arend van Spriel2e7565602011-12-08 15:06:46 -08001003 * is called in brcms_bcma_probe() context, therefore no locking required.
Arend van Spriel5b435de2011-10-05 13:19:03 +02001004 */
Arend van Spriel2e7565602011-12-08 15:06:46 -08001005static struct brcms_info *brcms_attach(struct bcma_device *pdev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001006{
1007 struct brcms_info *wl = NULL;
1008 int unit, err;
1009 struct ieee80211_hw *hw;
1010 u8 perm[ETH_ALEN];
1011
1012 unit = n_adapters_found;
1013 err = 0;
1014
1015 if (unit < 0)
1016 return NULL;
1017
1018 /* allocate private info */
Arend van Spriel2e7565602011-12-08 15:06:46 -08001019 hw = bcma_get_drvdata(pdev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001020 if (hw != NULL)
1021 wl = hw->priv;
1022 if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
1023 return NULL;
1024 wl->wiphy = hw->wiphy;
1025
1026 atomic_set(&wl->callbacks, 0);
1027
1028 /* setup the bottom half handler */
1029 tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
1030
Arend van Spriel5b435de2011-10-05 13:19:03 +02001031 spin_lock_init(&wl->lock);
1032 spin_lock_init(&wl->isr_lock);
1033
1034 /* prepare ucode */
Hauke Mehrtens00bcda42012-04-29 02:50:41 +02001035 if (brcms_request_fw(wl, pdev) < 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001036 wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
1037 "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
1038 brcms_release_fw(wl);
Arend van Spriel2e7565602011-12-08 15:06:46 -08001039 brcms_remove(pdev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001040 return NULL;
1041 }
1042
1043 /* common load-time initialization */
Arend van Sprielb63337a2011-12-08 15:06:47 -08001044 wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001045 brcms_release_fw(wl);
1046 if (!wl->wlc) {
1047 wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
1048 KBUILD_MODNAME, err);
1049 goto fail;
1050 }
1051 wl->pub = brcms_c_pub(wl->wlc);
1052
1053 wl->pub->ieee_hw = hw;
1054
Arend van Spriel5b435de2011-10-05 13:19:03 +02001055 /* register our interrupt handler */
Hauke Mehrtens00bcda42012-04-29 02:50:41 +02001056 if (request_irq(pdev->irq, brcms_isr,
Arend van Spriel2e7565602011-12-08 15:06:46 -08001057 IRQF_SHARED, KBUILD_MODNAME, wl)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001058 wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
1059 goto fail;
1060 }
Hauke Mehrtens00bcda42012-04-29 02:50:41 +02001061 wl->irq = pdev->irq;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001062
1063 /* register module */
1064 brcms_c_module_register(wl->pub, "linux", wl, NULL);
1065
1066 if (ieee_hw_init(hw)) {
1067 wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
1068 __func__);
1069 goto fail;
1070 }
1071
Seth Forsheecf03c5d2012-06-16 07:47:52 -05001072 brcms_c_regd_init(wl->wlc);
1073
Arend van Spriel5b435de2011-10-05 13:19:03 +02001074 memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
1075 if (WARN_ON(!is_valid_ether_addr(perm)))
1076 goto fail;
1077 SET_IEEE80211_PERM_ADDR(hw, perm);
1078
1079 err = ieee80211_register_hw(hw);
1080 if (err)
1081 wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
1082 "%d\n", __func__, err);
1083
Arend van Sprield597ee72012-06-09 22:51:41 +02001084 if (wl->pub->srom_ccode[0] &&
1085 regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
1086 wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001087
Piotr Haber8e21df22012-11-28 21:44:08 +01001088 brcms_debugfs_attach(wl->pub);
1089 brcms_debugfs_create_files(wl->pub);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001090 n_adapters_found++;
1091 return wl;
1092
1093fail:
1094 brcms_free(wl);
1095 return NULL;
1096}
1097
1098
1099
1100/**
1101 * determines if a device is a WL device, and if so, attaches it.
1102 *
1103 * This function determines if a device pointed to by pdev is a WL device,
1104 * and if so, performs a brcms_attach() on it.
1105 *
1106 * Perimeter lock is initialized in the course of this function.
1107 */
Bill Pembertonfcff0c02012-12-03 09:56:31 -05001108static int brcms_bcma_probe(struct bcma_device *pdev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001109{
Arend van Spriel5b435de2011-10-05 13:19:03 +02001110 struct brcms_info *wl;
1111 struct ieee80211_hw *hw;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001112
Arend van Spriel2e7565602011-12-08 15:06:46 -08001113 dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
1114 pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
Hauke Mehrtens00bcda42012-04-29 02:50:41 +02001115 pdev->irq);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001116
Arend van Spriel2e7565602011-12-08 15:06:46 -08001117 if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
1118 (pdev->id.id != BCMA_CORE_80211))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001119 return -ENODEV;
1120
Arend van Spriel5b435de2011-10-05 13:19:03 +02001121 hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
1122 if (!hw) {
1123 pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
1124 return -ENOMEM;
1125 }
1126
1127 SET_IEEE80211_DEV(hw, &pdev->dev);
1128
Arend van Spriel2e7565602011-12-08 15:06:46 -08001129 bcma_set_drvdata(pdev, hw);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001130
1131 memset(hw->priv, 0, sizeof(*wl));
1132
Arend van Spriel2e7565602011-12-08 15:06:46 -08001133 wl = brcms_attach(pdev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001134 if (!wl) {
Joe Perches02f77192012-01-15 00:38:44 -08001135 pr_err("%s: brcms_attach failed!\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001136 return -ENODEV;
1137 }
1138 return 0;
1139}
1140
Linus Torvalds7d5869e2012-01-13 23:58:41 +01001141static int brcms_suspend(struct bcma_device *pdev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001142{
1143 struct brcms_info *wl;
1144 struct ieee80211_hw *hw;
1145
Arend van Spriel2e7565602011-12-08 15:06:46 -08001146 hw = bcma_get_drvdata(pdev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001147 wl = hw->priv;
1148 if (!wl) {
Arend van Spriel137dabe2012-02-09 21:08:59 +01001149 pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
1150 __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001151 return -ENODEV;
1152 }
1153
1154 /* only need to flag hw is down for proper resume */
1155 spin_lock_bh(&wl->lock);
1156 wl->pub->hw_up = false;
1157 spin_unlock_bh(&wl->lock);
1158
Seth Forsheeb353dda2012-11-15 08:08:03 -06001159 brcms_dbg_info(wl->wlc->hw->d11core, "brcms_suspend ok\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001160
Arend van Spriel2e7565602011-12-08 15:06:46 -08001161 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001162}
1163
Arend van Spriel2e7565602011-12-08 15:06:46 -08001164static int brcms_resume(struct bcma_device *pdev)
1165{
Linus Torvalds7e9e7fa2012-01-13 23:58:42 +01001166 return 0;
Arend van Spriel2e7565602011-12-08 15:06:46 -08001167}
1168
1169static struct bcma_driver brcms_bcma_driver = {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001170 .name = KBUILD_MODNAME,
Arend van Spriel2e7565602011-12-08 15:06:46 -08001171 .probe = brcms_bcma_probe,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001172 .suspend = brcms_suspend,
1173 .resume = brcms_resume,
Bill Pembertonfcff0c02012-12-03 09:56:31 -05001174 .remove = brcms_remove,
Arend van Spriel2e7565602011-12-08 15:06:46 -08001175 .id_table = brcms_coreid_table,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001176};
1177
1178/**
Arend van Spriel2e7565602011-12-08 15:06:46 -08001179 * This is the main entry point for the brcmsmac driver.
Arend van Spriel5b435de2011-10-05 13:19:03 +02001180 *
Arend van Sprielb0c359b2012-03-02 22:55:50 +01001181 * This function is scheduled upon module initialization and
1182 * does the driver registration, which result in brcms_bcma_probe()
1183 * call resulting in the driver bringup.
Arend van Spriel5b435de2011-10-05 13:19:03 +02001184 */
Arend van Sprielb0c359b2012-03-02 22:55:50 +01001185static void brcms_driver_init(struct work_struct *work)
1186{
1187 int error;
1188
1189 error = bcma_driver_register(&brcms_bcma_driver);
1190 if (error)
1191 pr_err("%s: register returned %d\n", __func__, error);
1192}
1193
1194static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
1195
Arend van Spriel5b435de2011-10-05 13:19:03 +02001196static int __init brcms_module_init(void)
1197{
Piotr Haber8e21df22012-11-28 21:44:08 +01001198 brcms_debugfs_init();
Arend van Sprielb0c359b2012-03-02 22:55:50 +01001199 if (!schedule_work(&brcms_driver_work))
1200 return -EBUSY;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001201
Arend van Sprielb0c359b2012-03-02 22:55:50 +01001202 return 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001203}
1204
1205/**
Arend van Spriel2e7565602011-12-08 15:06:46 -08001206 * This function unloads the brcmsmac driver from the system.
Arend van Spriel5b435de2011-10-05 13:19:03 +02001207 *
Arend van Spriel2e7565602011-12-08 15:06:46 -08001208 * This function unconditionally unloads the brcmsmac driver module from the
Arend van Spriel5b435de2011-10-05 13:19:03 +02001209 * system.
1210 *
1211 */
1212static void __exit brcms_module_exit(void)
1213{
Arend van Sprielb0c359b2012-03-02 22:55:50 +01001214 cancel_work_sync(&brcms_driver_work);
Arend van Spriel2e7565602011-12-08 15:06:46 -08001215 bcma_driver_unregister(&brcms_bcma_driver);
Piotr Haber8e21df22012-11-28 21:44:08 +01001216 brcms_debugfs_exit();
Arend van Spriel5b435de2011-10-05 13:19:03 +02001217}
1218
1219module_init(brcms_module_init);
1220module_exit(brcms_module_exit);
1221
1222/*
1223 * precondition: perimeter lock has been acquired
1224 */
1225void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
1226 bool state, int prio)
1227{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001228 brcms_err(wl->wlc->hw->d11core, "Shouldn't be here %s\n", __func__);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001229}
1230
1231/*
1232 * precondition: perimeter lock has been acquired
1233 */
1234void brcms_init(struct brcms_info *wl)
1235{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001236 brcms_dbg_info(wl->wlc->hw->d11core, "Initializing wl%d\n",
1237 wl->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001238 brcms_reset(wl);
Roland Vossendc460122011-10-21 16:16:28 +02001239 brcms_c_init(wl->wlc, wl->mute_tx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001240}
1241
1242/*
1243 * precondition: perimeter lock has been acquired
1244 */
1245uint brcms_reset(struct brcms_info *wl)
1246{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001247 brcms_dbg_info(wl->wlc->hw->d11core, "Resetting wl%d\n", wl->pub->unit);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001248 brcms_c_reset(wl->wlc);
1249
1250 /* dpc will not be rescheduled */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001251 wl->resched = false;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001252
Vladimir Zapolskiyea2d2182012-08-05 00:29:07 +03001253 /* inform publicly that interface is down */
1254 wl->pub->up = false;
1255
Arend van Spriel5b435de2011-10-05 13:19:03 +02001256 return 0;
1257}
1258
Roland Vossenc261bdf2011-10-18 14:03:04 +02001259void brcms_fatal_error(struct brcms_info *wl)
1260{
Seth Forsheeb353dda2012-11-15 08:08:03 -06001261 brcms_err(wl->wlc->hw->d11core, "wl%d: fatal error, reinitializing\n",
Roland Vossenc261bdf2011-10-18 14:03:04 +02001262 wl->wlc->pub->unit);
1263 brcms_reset(wl);
1264 ieee80211_restart_hw(wl->pub->ieee_hw);
1265}
1266
Arend van Spriel5b435de2011-10-05 13:19:03 +02001267/*
1268 * These are interrupt on/off entry points. Disable interrupts
1269 * during interrupt state transition.
1270 */
1271void brcms_intrson(struct brcms_info *wl)
1272{
1273 unsigned long flags;
1274
1275 spin_lock_irqsave(&wl->isr_lock, flags);
1276 brcms_c_intrson(wl->wlc);
1277 spin_unlock_irqrestore(&wl->isr_lock, flags);
1278}
1279
1280u32 brcms_intrsoff(struct brcms_info *wl)
1281{
1282 unsigned long flags;
1283 u32 status;
1284
1285 spin_lock_irqsave(&wl->isr_lock, flags);
1286 status = brcms_c_intrsoff(wl->wlc);
1287 spin_unlock_irqrestore(&wl->isr_lock, flags);
1288 return status;
1289}
1290
1291void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
1292{
1293 unsigned long flags;
1294
1295 spin_lock_irqsave(&wl->isr_lock, flags);
1296 brcms_c_intrsrestore(wl->wlc, macintmask);
1297 spin_unlock_irqrestore(&wl->isr_lock, flags);
1298}
1299
1300/*
1301 * precondition: perimeter lock has been acquired
1302 */
1303int brcms_up(struct brcms_info *wl)
1304{
1305 int error = 0;
1306
1307 if (wl->pub->up)
1308 return 0;
1309
1310 error = brcms_c_up(wl->wlc);
1311
1312 return error;
1313}
1314
1315/*
1316 * precondition: perimeter lock has been acquired
1317 */
1318void brcms_down(struct brcms_info *wl)
1319{
1320 uint callbacks, ret_val = 0;
1321
1322 /* call common down function */
1323 ret_val = brcms_c_down(wl->wlc);
1324 callbacks = atomic_read(&wl->callbacks) - ret_val;
1325
1326 /* wait for down callbacks to complete */
1327 spin_unlock_bh(&wl->lock);
1328
1329 /* For HIGH_only driver, it's important to actually schedule other work,
1330 * not just spin wait since everything runs at schedule level
1331 */
1332 SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1333
1334 spin_lock_bh(&wl->lock);
1335}
1336
1337/*
1338* precondition: perimeter lock is not acquired
1339 */
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001340static void _brcms_timer(struct work_struct *work)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001341{
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001342 struct brcms_timer *t = container_of(work, struct brcms_timer,
1343 dly_wrk.work);
1344
Arend van Spriel5b435de2011-10-05 13:19:03 +02001345 spin_lock_bh(&t->wl->lock);
1346
1347 if (t->set) {
1348 if (t->periodic) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001349 atomic_inc(&t->wl->callbacks);
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001350 ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
1351 &t->dly_wrk,
1352 msecs_to_jiffies(t->ms));
1353 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001354 t->set = false;
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001355 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001356
1357 t->fn(t->arg);
1358 }
1359
1360 atomic_dec(&t->wl->callbacks);
1361
1362 spin_unlock_bh(&t->wl->lock);
1363}
1364
1365/*
Arend van Spriel5b435de2011-10-05 13:19:03 +02001366 * Adds a timer to the list. Caller supplies a timer function.
1367 * Is called from wlc.
1368 *
1369 * precondition: perimeter lock has been acquired
1370 */
1371struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
1372 void (*fn) (void *arg),
1373 void *arg, const char *name)
1374{
1375 struct brcms_timer *t;
1376
1377 t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
1378 if (!t)
1379 return NULL;
1380
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001381 INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001382 t->wl = wl;
1383 t->fn = fn;
1384 t->arg = arg;
1385 t->next = wl->timers;
1386 wl->timers = t;
1387
Joe Perches8ae74652012-01-15 00:38:38 -08001388#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02001389 t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
1390 if (t->name)
1391 strcpy(t->name, name);
1392#endif
1393
1394 return t;
1395}
1396
1397/*
1398 * adds only the kernel timer since it's going to be more accurate
1399 * as well as it's easier to make it periodic
1400 *
1401 * precondition: perimeter lock has been acquired
1402 */
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001403void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001404{
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001405 struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
1406
Joe Perches8ae74652012-01-15 00:38:38 -08001407#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02001408 if (t->set)
Seth Forsheeb353dda2012-11-15 08:08:03 -06001409 brcms_dbg_info(t->wl->wlc->hw->d11core,
1410 "%s: Already set. Name: %s, per %d\n",
1411 __func__, t->name, periodic);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001412#endif
1413 t->ms = ms;
1414 t->periodic = (bool) periodic;
Piotr Haber01486c52013-01-02 15:22:34 +01001415 if (!t->set) {
1416 t->set = true;
1417 atomic_inc(&t->wl->callbacks);
1418 }
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001419
1420 ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001421}
1422
1423/*
1424 * return true if timer successfully deleted, false if still pending
1425 *
1426 * precondition: perimeter lock has been acquired
1427 */
Roland Vossenbe69c4e2011-10-12 20:51:11 +02001428bool brcms_del_timer(struct brcms_timer *t)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001429{
1430 if (t->set) {
1431 t->set = false;
Roland Vossen2a7fc5b2011-10-12 20:51:12 +02001432 if (!cancel_delayed_work(&t->dly_wrk))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001433 return false;
1434
Roland Vossenbe69c4e2011-10-12 20:51:11 +02001435 atomic_dec(&t->wl->callbacks);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001436 }
1437
1438 return true;
1439}
1440
1441/*
1442 * precondition: perimeter lock has been acquired
1443 */
Roland Vossenbe69c4e2011-10-12 20:51:11 +02001444void brcms_free_timer(struct brcms_timer *t)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001445{
Roland Vossenbe69c4e2011-10-12 20:51:11 +02001446 struct brcms_info *wl = t->wl;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001447 struct brcms_timer *tmp;
1448
1449 /* delete the timer in case it is active */
Roland Vossenbe69c4e2011-10-12 20:51:11 +02001450 brcms_del_timer(t);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001451
1452 if (wl->timers == t) {
1453 wl->timers = wl->timers->next;
Joe Perches8ae74652012-01-15 00:38:38 -08001454#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02001455 kfree(t->name);
1456#endif
1457 kfree(t);
1458 return;
1459
1460 }
1461
1462 tmp = wl->timers;
1463 while (tmp) {
1464 if (tmp->next == t) {
1465 tmp->next = t->next;
Joe Perches8ae74652012-01-15 00:38:38 -08001466#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02001467 kfree(t->name);
1468#endif
1469 kfree(t);
1470 return;
1471 }
1472 tmp = tmp->next;
1473 }
1474
1475}
1476
1477/*
1478 * precondition: perimeter lock has been acquired
1479 */
1480int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
1481{
1482 int i, entry;
1483 const u8 *pdata;
1484 struct firmware_hdr *hdr;
1485 for (i = 0; i < wl->fw.fw_cnt; i++) {
1486 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1487 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1488 entry++, hdr++) {
1489 u32 len = le32_to_cpu(hdr->len);
1490 if (le32_to_cpu(hdr->idx) == idx) {
1491 pdata = wl->fw.fw_bin[i]->data +
1492 le32_to_cpu(hdr->offset);
Thomas Meyer1f1d52892011-11-17 23:43:40 +01001493 *pbuf = kmemdup(pdata, len, GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001494 if (*pbuf == NULL)
1495 goto fail;
1496
Arend van Spriel5b435de2011-10-05 13:19:03 +02001497 return 0;
1498 }
1499 }
1500 }
Seth Forsheeb353dda2012-11-15 08:08:03 -06001501 brcms_err(wl->wlc->hw->d11core,
1502 "ERROR: ucode buf tag:%d can not be found!\n", idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001503 *pbuf = NULL;
1504fail:
1505 return -ENODATA;
1506}
1507
1508/*
Arend van Spriel2e7565602011-12-08 15:06:46 -08001509 * Precondition: Since this function is called in brcms_bcma_probe() context,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001510 * no locking is required.
1511 */
1512int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
1513{
1514 int i, entry;
1515 const u8 *pdata;
1516 struct firmware_hdr *hdr;
1517 for (i = 0; i < wl->fw.fw_cnt; i++) {
1518 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1519 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1520 entry++, hdr++) {
1521 if (le32_to_cpu(hdr->idx) == idx) {
1522 pdata = wl->fw.fw_bin[i]->data +
1523 le32_to_cpu(hdr->offset);
1524 if (le32_to_cpu(hdr->len) != 4) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001525 brcms_err(wl->wlc->hw->d11core,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001526 "ERROR: fw hdr len\n");
1527 return -ENOMSG;
1528 }
1529 *n_bytes = le32_to_cpu(*((__le32 *) pdata));
1530 return 0;
1531 }
1532 }
1533 }
Seth Forsheeb353dda2012-11-15 08:08:03 -06001534 brcms_err(wl->wlc->hw->d11core,
1535 "ERROR: ucode tag:%d can not be found!\n", idx);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001536 return -ENOMSG;
1537}
1538
1539/*
1540 * precondition: can both be called locked and unlocked
1541 */
1542void brcms_ucode_free_buf(void *p)
1543{
1544 kfree(p);
1545}
1546
1547/*
1548 * checks validity of all firmware images loaded from user space
1549 *
Arend van Spriel2e7565602011-12-08 15:06:46 -08001550 * Precondition: Since this function is called in brcms_bcma_probe() context,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001551 * no locking is required.
1552 */
1553int brcms_check_firmwares(struct brcms_info *wl)
1554{
1555 int i;
1556 int entry;
1557 int rc = 0;
1558 const struct firmware *fw;
1559 const struct firmware *fw_hdr;
1560 struct firmware_hdr *ucode_hdr;
1561 for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
1562 fw = wl->fw.fw_bin[i];
1563 fw_hdr = wl->fw.fw_hdr[i];
1564 if (fw == NULL && fw_hdr == NULL) {
1565 break;
1566 } else if (fw == NULL || fw_hdr == NULL) {
1567 wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
1568 __func__);
1569 rc = -EBADF;
1570 } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
1571 wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
1572 "size %zu/%zu\n", __func__, fw_hdr->size,
1573 sizeof(struct firmware_hdr));
1574 rc = -EBADF;
1575 } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
Seth Forsheeb353dda2012-11-15 08:08:03 -06001576 wiphy_err(wl->wiphy, "%s: out of bounds fw file size %zu\n",
1577 __func__, fw->size);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001578 rc = -EBADF;
1579 } else {
1580 /* check if ucode section overruns firmware image */
1581 ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
1582 for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
1583 !rc; entry++, ucode_hdr++) {
1584 if (le32_to_cpu(ucode_hdr->offset) +
1585 le32_to_cpu(ucode_hdr->len) >
1586 fw->size) {
1587 wiphy_err(wl->wiphy,
1588 "%s: conflicting bin/hdr\n",
1589 __func__);
1590 rc = -EBADF;
1591 }
1592 }
1593 }
1594 }
1595 if (rc == 0 && wl->fw.fw_cnt != i) {
1596 wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
1597 wl->fw.fw_cnt);
1598 rc = -EBADF;
1599 }
1600 return rc;
1601}
1602
1603/*
1604 * precondition: perimeter lock has been acquired
1605 */
1606bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
1607{
1608 bool blocked = brcms_c_check_radio_disabled(wl->wlc);
1609
1610 spin_unlock_bh(&wl->lock);
1611 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
1612 if (blocked)
1613 wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
1614 spin_lock_bh(&wl->lock);
1615 return blocked;
1616}
1617
1618/*
1619 * precondition: perimeter lock has been acquired
1620 */
1621void brcms_msleep(struct brcms_info *wl, uint ms)
1622{
1623 spin_unlock_bh(&wl->lock);
1624 msleep(ms);
1625 spin_lock_bh(&wl->lock);
1626}