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