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