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