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