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