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