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