GNU Linux-libre 4.9-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->mute_tx = false;
505         brcms_c_mute(wl->wlc, false);
506         if (vif->type == NL80211_IFTYPE_STATION)
507                 brcms_c_start_station(wl->wlc, vif->addr);
508         else if (vif->type == NL80211_IFTYPE_AP)
509                 brcms_c_start_ap(wl->wlc, vif->addr, vif->bss_conf.bssid,
510                                  vif->bss_conf.ssid, vif->bss_conf.ssid_len);
511         else if (vif->type == NL80211_IFTYPE_ADHOC)
512                 brcms_c_start_adhoc(wl->wlc, vif->addr);
513         spin_unlock_bh(&wl->lock);
514
515         return 0;
516 }
517
518 static void
519 brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
520 {
521 }
522
523 static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
524 {
525         struct ieee80211_conf *conf = &hw->conf;
526         struct brcms_info *wl = hw->priv;
527         struct bcma_device *core = wl->wlc->hw->d11core;
528         int err = 0;
529         int new_int;
530
531         spin_lock_bh(&wl->lock);
532         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
533                 brcms_c_set_beacon_listen_interval(wl->wlc,
534                                                    conf->listen_interval);
535         }
536         if (changed & IEEE80211_CONF_CHANGE_MONITOR)
537                 brcms_dbg_info(core, "%s: change monitor mode: %s\n",
538                                __func__, conf->flags & IEEE80211_CONF_MONITOR ?
539                                "true" : "false");
540         if (changed & IEEE80211_CONF_CHANGE_PS)
541                 brcms_err(core, "%s: change power-save mode: %s (implement)\n",
542                           __func__, conf->flags & IEEE80211_CONF_PS ?
543                           "true" : "false");
544
545         if (changed & IEEE80211_CONF_CHANGE_POWER) {
546                 err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
547                 if (err < 0) {
548                         brcms_err(core, "%s: Error setting power_level\n",
549                                   __func__);
550                         goto config_out;
551                 }
552                 new_int = brcms_c_get_tx_power(wl->wlc);
553                 if (new_int != conf->power_level)
554                         brcms_err(core,
555                                   "%s: Power level req != actual, %d %d\n",
556                                   __func__, conf->power_level,
557                                   new_int);
558         }
559         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
560                 if (conf->chandef.width == NL80211_CHAN_WIDTH_20 ||
561                     conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
562                         err = brcms_c_set_channel(wl->wlc,
563                                                   conf->chandef.chan->hw_value);
564                 else
565                         err = -ENOTSUPP;
566         }
567         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
568                 err = brcms_c_set_rate_limit(wl->wlc,
569                                              conf->short_frame_max_tx_count,
570                                              conf->long_frame_max_tx_count);
571
572  config_out:
573         spin_unlock_bh(&wl->lock);
574         return err;
575 }
576
577 static void
578 brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
579                         struct ieee80211_vif *vif,
580                         struct ieee80211_bss_conf *info, u32 changed)
581 {
582         struct brcms_info *wl = hw->priv;
583         struct bcma_device *core = wl->wlc->hw->d11core;
584
585         if (changed & BSS_CHANGED_ASSOC) {
586                 /* association status changed (associated/disassociated)
587                  * also implies a change in the AID.
588                  */
589                 brcms_err(core, "%s: %s: %sassociated\n", KBUILD_MODNAME,
590                           __func__, info->assoc ? "" : "dis");
591                 spin_lock_bh(&wl->lock);
592                 brcms_c_associate_upd(wl->wlc, info->assoc);
593                 spin_unlock_bh(&wl->lock);
594         }
595         if (changed & BSS_CHANGED_ERP_SLOT) {
596                 s8 val;
597
598                 /* slot timing changed */
599                 if (info->use_short_slot)
600                         val = 1;
601                 else
602                         val = 0;
603                 spin_lock_bh(&wl->lock);
604                 brcms_c_set_shortslot_override(wl->wlc, val);
605                 spin_unlock_bh(&wl->lock);
606         }
607
608         if (changed & BSS_CHANGED_HT) {
609                 /* 802.11n parameters changed */
610                 u16 mode = info->ht_operation_mode;
611
612                 spin_lock_bh(&wl->lock);
613                 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
614                         mode & IEEE80211_HT_OP_MODE_PROTECTION);
615                 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
616                         mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
617                 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
618                         mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
619                 spin_unlock_bh(&wl->lock);
620         }
621         if (changed & BSS_CHANGED_BASIC_RATES) {
622                 struct ieee80211_supported_band *bi;
623                 u32 br_mask, i;
624                 u16 rate;
625                 struct brcm_rateset rs;
626                 int error;
627
628                 /* retrieve the current rates */
629                 spin_lock_bh(&wl->lock);
630                 brcms_c_get_current_rateset(wl->wlc, &rs);
631                 spin_unlock_bh(&wl->lock);
632
633                 br_mask = info->basic_rates;
634                 bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
635                 for (i = 0; i < bi->n_bitrates; i++) {
636                         /* convert to internal rate value */
637                         rate = (bi->bitrates[i].bitrate << 1) / 10;
638
639                         /* set/clear basic rate flag */
640                         brcms_set_basic_rate(&rs, rate, br_mask & 1);
641                         br_mask >>= 1;
642                 }
643
644                 /* update the rate set */
645                 spin_lock_bh(&wl->lock);
646                 error = brcms_c_set_rateset(wl->wlc, &rs);
647                 spin_unlock_bh(&wl->lock);
648                 if (error)
649                         brcms_err(core, "changing basic rates failed: %d\n",
650                                   error);
651         }
652         if (changed & BSS_CHANGED_BEACON_INT) {
653                 /* Beacon interval changed */
654                 spin_lock_bh(&wl->lock);
655                 brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
656                 spin_unlock_bh(&wl->lock);
657         }
658         if (changed & BSS_CHANGED_BSSID) {
659                 /* BSSID changed, for whatever reason (IBSS and managed mode) */
660                 spin_lock_bh(&wl->lock);
661                 brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
662                 spin_unlock_bh(&wl->lock);
663         }
664         if (changed & BSS_CHANGED_SSID) {
665                 /* BSSID changed, for whatever reason (IBSS and managed mode) */
666                 spin_lock_bh(&wl->lock);
667                 brcms_c_set_ssid(wl->wlc, info->ssid, info->ssid_len);
668                 spin_unlock_bh(&wl->lock);
669         }
670         if (changed & BSS_CHANGED_BEACON) {
671                 /* Beacon data changed, retrieve new beacon (beaconing modes) */
672                 struct sk_buff *beacon;
673                 u16 tim_offset = 0;
674
675                 spin_lock_bh(&wl->lock);
676                 beacon = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL);
677                 brcms_c_set_new_beacon(wl->wlc, beacon, tim_offset,
678                                        info->dtim_period);
679                 spin_unlock_bh(&wl->lock);
680         }
681
682         if (changed & BSS_CHANGED_AP_PROBE_RESP) {
683                 struct sk_buff *probe_resp;
684
685                 spin_lock_bh(&wl->lock);
686                 probe_resp = ieee80211_proberesp_get(hw, vif);
687                 brcms_c_set_new_probe_resp(wl->wlc, probe_resp);
688                 spin_unlock_bh(&wl->lock);
689         }
690
691         if (changed & BSS_CHANGED_BEACON_ENABLED) {
692                 /* Beaconing should be enabled/disabled (beaconing modes) */
693                 brcms_err(core, "%s: Beacon enabled: %s\n", __func__,
694                           info->enable_beacon ? "true" : "false");
695                 if (info->enable_beacon &&
696                     hw->wiphy->flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) {
697                         brcms_c_enable_probe_resp(wl->wlc, true);
698                 } else {
699                         brcms_c_enable_probe_resp(wl->wlc, false);
700                 }
701         }
702
703         if (changed & BSS_CHANGED_CQM) {
704                 /* Connection quality monitor config changed */
705                 brcms_err(core, "%s: cqm change: threshold %d, hys %d "
706                           " (implement)\n", __func__, info->cqm_rssi_thold,
707                           info->cqm_rssi_hyst);
708         }
709
710         if (changed & BSS_CHANGED_IBSS) {
711                 /* IBSS join status changed */
712                 brcms_err(core, "%s: IBSS joined: %s (implement)\n",
713                           __func__, info->ibss_joined ? "true" : "false");
714         }
715
716         if (changed & BSS_CHANGED_ARP_FILTER) {
717                 /* Hardware ARP filter address list or state changed */
718                 brcms_err(core, "%s: arp filtering: %d addresses"
719                           " (implement)\n", __func__, info->arp_addr_cnt);
720         }
721
722         if (changed & BSS_CHANGED_QOS) {
723                 /*
724                  * QoS for this association was enabled/disabled.
725                  * Note that it is only ever disabled for station mode.
726                  */
727                 brcms_err(core, "%s: qos enabled: %s (implement)\n",
728                           __func__, info->qos ? "true" : "false");
729         }
730         return;
731 }
732
733 static void
734 brcms_ops_configure_filter(struct ieee80211_hw *hw,
735                         unsigned int changed_flags,
736                         unsigned int *total_flags, u64 multicast)
737 {
738         struct brcms_info *wl = hw->priv;
739         struct bcma_device *core = wl->wlc->hw->d11core;
740
741         changed_flags &= MAC_FILTERS;
742         *total_flags &= MAC_FILTERS;
743
744         if (changed_flags & FIF_ALLMULTI)
745                 brcms_dbg_info(core, "FIF_ALLMULTI\n");
746         if (changed_flags & FIF_FCSFAIL)
747                 brcms_dbg_info(core, "FIF_FCSFAIL\n");
748         if (changed_flags & FIF_CONTROL)
749                 brcms_dbg_info(core, "FIF_CONTROL\n");
750         if (changed_flags & FIF_OTHER_BSS)
751                 brcms_dbg_info(core, "FIF_OTHER_BSS\n");
752         if (changed_flags & FIF_PSPOLL)
753                 brcms_dbg_info(core, "FIF_PSPOLL\n");
754         if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
755                 brcms_dbg_info(core, "FIF_BCN_PRBRESP_PROMISC\n");
756
757         spin_lock_bh(&wl->lock);
758         brcms_c_mac_promisc(wl->wlc, *total_flags);
759         spin_unlock_bh(&wl->lock);
760         return;
761 }
762
763 static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw,
764                                     struct ieee80211_vif *vif,
765                                     const u8 *mac_addr)
766 {
767         struct brcms_info *wl = hw->priv;
768         spin_lock_bh(&wl->lock);
769         brcms_c_scan_start(wl->wlc);
770         spin_unlock_bh(&wl->lock);
771         return;
772 }
773
774 static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw,
775                                        struct ieee80211_vif *vif)
776 {
777         struct brcms_info *wl = hw->priv;
778         spin_lock_bh(&wl->lock);
779         brcms_c_scan_stop(wl->wlc);
780         spin_unlock_bh(&wl->lock);
781         return;
782 }
783
784 static int
785 brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
786                   const struct ieee80211_tx_queue_params *params)
787 {
788         struct brcms_info *wl = hw->priv;
789
790         spin_lock_bh(&wl->lock);
791         brcms_c_wme_setparams(wl->wlc, queue, params, true);
792         spin_unlock_bh(&wl->lock);
793
794         return 0;
795 }
796
797 static int
798 brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
799                struct ieee80211_sta *sta)
800 {
801         struct brcms_info *wl = hw->priv;
802         struct scb *scb = &wl->wlc->pri_scb;
803
804         brcms_c_init_scb(scb);
805
806         wl->pub->global_ampdu = &(scb->scb_ampdu);
807         wl->pub->global_ampdu->scb = scb;
808         wl->pub->global_ampdu->max_pdu = 16;
809
810         /*
811          * minstrel_ht initiates addBA on our behalf by calling
812          * ieee80211_start_tx_ba_session()
813          */
814         return 0;
815 }
816
817 static int
818 brcms_ops_ampdu_action(struct ieee80211_hw *hw,
819                     struct ieee80211_vif *vif,
820                     struct ieee80211_ampdu_params *params)
821 {
822         struct brcms_info *wl = hw->priv;
823         struct scb *scb = &wl->wlc->pri_scb;
824         int status;
825         struct ieee80211_sta *sta = params->sta;
826         enum ieee80211_ampdu_mlme_action action = params->action;
827         u16 tid = params->tid;
828         u8 buf_size = params->buf_size;
829
830         if (WARN_ON(scb->magic != SCB_MAGIC))
831                 return -EIDRM;
832         switch (action) {
833         case IEEE80211_AMPDU_RX_START:
834                 break;
835         case IEEE80211_AMPDU_RX_STOP:
836                 break;
837         case IEEE80211_AMPDU_TX_START:
838                 spin_lock_bh(&wl->lock);
839                 status = brcms_c_aggregatable(wl->wlc, tid);
840                 spin_unlock_bh(&wl->lock);
841                 if (!status) {
842                         brcms_err(wl->wlc->hw->d11core,
843                                   "START: tid %d is not agg\'able\n", tid);
844                         return -EINVAL;
845                 }
846                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
847                 break;
848
849         case IEEE80211_AMPDU_TX_STOP_CONT:
850         case IEEE80211_AMPDU_TX_STOP_FLUSH:
851         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
852                 spin_lock_bh(&wl->lock);
853                 brcms_c_ampdu_flush(wl->wlc, sta, tid);
854                 spin_unlock_bh(&wl->lock);
855                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
856                 break;
857         case IEEE80211_AMPDU_TX_OPERATIONAL:
858                 /*
859                  * BA window size from ADDBA response ('buf_size') defines how
860                  * many outstanding MPDUs are allowed for the BA stream by
861                  * recipient and traffic class. 'ampdu_factor' gives maximum
862                  * AMPDU size.
863                  */
864                 spin_lock_bh(&wl->lock);
865                 brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
866                         (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
867                          sta->ht_cap.ampdu_factor)) - 1);
868                 spin_unlock_bh(&wl->lock);
869                 /* Power save wakeup */
870                 break;
871         default:
872                 brcms_err(wl->wlc->hw->d11core,
873                           "%s: Invalid command, ignoring\n", __func__);
874         }
875
876         return 0;
877 }
878
879 static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
880 {
881         struct brcms_info *wl = hw->priv;
882         bool blocked;
883
884         spin_lock_bh(&wl->lock);
885         blocked = brcms_c_check_radio_disabled(wl->wlc);
886         spin_unlock_bh(&wl->lock);
887
888         wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
889 }
890
891 static bool brcms_tx_flush_completed(struct brcms_info *wl)
892 {
893         bool result;
894
895         spin_lock_bh(&wl->lock);
896         result = brcms_c_tx_flush_completed(wl->wlc);
897         spin_unlock_bh(&wl->lock);
898         return result;
899 }
900
901 static void brcms_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
902                             u32 queues, bool drop)
903 {
904         struct brcms_info *wl = hw->priv;
905         int ret;
906
907         no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
908
909         ret = wait_event_timeout(wl->tx_flush_wq,
910                                  brcms_tx_flush_completed(wl),
911                                  msecs_to_jiffies(BRCMS_FLUSH_TIMEOUT));
912
913         brcms_dbg_mac80211(wl->wlc->hw->d11core,
914                            "ret=%d\n", jiffies_to_msecs(ret));
915 }
916
917 static u64 brcms_ops_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
918 {
919         struct brcms_info *wl = hw->priv;
920         u64 tsf;
921
922         spin_lock_bh(&wl->lock);
923         tsf = brcms_c_tsf_get(wl->wlc);
924         spin_unlock_bh(&wl->lock);
925
926         return tsf;
927 }
928
929 static void brcms_ops_set_tsf(struct ieee80211_hw *hw,
930                            struct ieee80211_vif *vif, u64 tsf)
931 {
932         struct brcms_info *wl = hw->priv;
933
934         spin_lock_bh(&wl->lock);
935         brcms_c_tsf_set(wl->wlc, tsf);
936         spin_unlock_bh(&wl->lock);
937 }
938
939 static const struct ieee80211_ops brcms_ops = {
940         .tx = brcms_ops_tx,
941         .start = brcms_ops_start,
942         .stop = brcms_ops_stop,
943         .add_interface = brcms_ops_add_interface,
944         .remove_interface = brcms_ops_remove_interface,
945         .config = brcms_ops_config,
946         .bss_info_changed = brcms_ops_bss_info_changed,
947         .configure_filter = brcms_ops_configure_filter,
948         .sw_scan_start = brcms_ops_sw_scan_start,
949         .sw_scan_complete = brcms_ops_sw_scan_complete,
950         .conf_tx = brcms_ops_conf_tx,
951         .sta_add = brcms_ops_sta_add,
952         .ampdu_action = brcms_ops_ampdu_action,
953         .rfkill_poll = brcms_ops_rfkill_poll,
954         .flush = brcms_ops_flush,
955         .get_tsf = brcms_ops_get_tsf,
956         .set_tsf = brcms_ops_set_tsf,
957 };
958
959 void brcms_dpc(unsigned long data)
960 {
961         struct brcms_info *wl;
962
963         wl = (struct brcms_info *) data;
964
965         spin_lock_bh(&wl->lock);
966
967         /* call the common second level interrupt handler */
968         if (wl->pub->up) {
969                 if (wl->resched) {
970                         unsigned long flags;
971
972                         spin_lock_irqsave(&wl->isr_lock, flags);
973                         brcms_c_intrsupd(wl->wlc);
974                         spin_unlock_irqrestore(&wl->isr_lock, flags);
975                 }
976
977                 wl->resched = brcms_c_dpc(wl->wlc, true);
978         }
979
980         /* brcms_c_dpc() may bring the driver down */
981         if (!wl->pub->up)
982                 goto done;
983
984         /* re-schedule dpc */
985         if (wl->resched)
986                 tasklet_schedule(&wl->tasklet);
987         else
988                 /* re-enable interrupts */
989                 brcms_intrson(wl);
990
991  done:
992         spin_unlock_bh(&wl->lock);
993         wake_up(&wl->tx_flush_wq);
994 }
995
996 static irqreturn_t brcms_isr(int irq, void *dev_id)
997 {
998         struct brcms_info *wl;
999         irqreturn_t ret = IRQ_NONE;
1000
1001         wl = (struct brcms_info *) dev_id;
1002
1003         spin_lock(&wl->isr_lock);
1004
1005         /* call common first level interrupt handler */
1006         if (brcms_c_isr(wl->wlc)) {
1007                 /* schedule second level handler */
1008                 tasklet_schedule(&wl->tasklet);
1009                 ret = IRQ_HANDLED;
1010         }
1011
1012         spin_unlock(&wl->isr_lock);
1013
1014         return ret;
1015 }
1016
1017 /*
1018  * is called in brcms_pci_probe() context, therefore no locking required.
1019  */
1020 static int ieee_hw_rate_init(struct ieee80211_hw *hw)
1021 {
1022         struct brcms_info *wl = hw->priv;
1023         struct brcms_c_info *wlc = wl->wlc;
1024         struct ieee80211_supported_band *band;
1025         int has_5g = 0;
1026         u16 phy_type;
1027
1028         hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
1029         hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
1030
1031         phy_type = brcms_c_get_phy_type(wl->wlc, 0);
1032         if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
1033                 band = &wlc->bandstate[BAND_2G_INDEX]->band;
1034                 *band = brcms_band_2GHz_nphy_template;
1035                 if (phy_type == PHY_TYPE_LCN) {
1036                         /* Single stream */
1037                         band->ht_cap.mcs.rx_mask[1] = 0;
1038                         band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
1039                 }
1040                 hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
1041         } else {
1042                 return -EPERM;
1043         }
1044
1045         /* Assume all bands use the same phy.  True for 11n devices. */
1046         if (wl->pub->_nbands > 1) {
1047                 has_5g++;
1048                 if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
1049                         band = &wlc->bandstate[BAND_5G_INDEX]->band;
1050                         *band = brcms_band_5GHz_nphy_template;
1051                         hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
1052                 } else {
1053                         return -EPERM;
1054                 }
1055         }
1056         return 0;
1057 }
1058
1059 /*
1060  * is called in brcms_pci_probe() context, therefore no locking required.
1061  */
1062 static int ieee_hw_init(struct ieee80211_hw *hw)
1063 {
1064         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1065         ieee80211_hw_set(hw, SIGNAL_DBM);
1066         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
1067
1068         hw->extra_tx_headroom = brcms_c_get_header_len();
1069         hw->queues = N_TX_QUEUES;
1070         hw->max_rates = 2;      /* Primary rate and 1 fallback rate */
1071
1072         /* channel change time is dependent on chip and band  */
1073         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1074                                      BIT(NL80211_IFTYPE_AP) |
1075                                      BIT(NL80211_IFTYPE_ADHOC);
1076
1077         /*
1078          * deactivate sending probe responses by ucude, because this will
1079          * cause problems when WPS is used.
1080          *
1081          * hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1082          */
1083
1084         hw->rate_control_algorithm = "minstrel_ht";
1085
1086         hw->sta_data_size = 0;
1087         return ieee_hw_rate_init(hw);
1088 }
1089
1090 /**
1091  * attach to the WL device.
1092  *
1093  * Attach to the WL device identified by vendor and device parameters.
1094  * regs is a host accessible memory address pointing to WL device registers.
1095  *
1096  * is called in brcms_bcma_probe() context, therefore no locking required.
1097  */
1098 static struct brcms_info *brcms_attach(struct bcma_device *pdev)
1099 {
1100         struct brcms_info *wl = NULL;
1101         int unit, err;
1102         struct ieee80211_hw *hw;
1103         u8 perm[ETH_ALEN];
1104
1105         unit = n_adapters_found;
1106         err = 0;
1107
1108         if (unit < 0)
1109                 return NULL;
1110
1111         /* allocate private info */
1112         hw = bcma_get_drvdata(pdev);
1113         if (hw != NULL)
1114                 wl = hw->priv;
1115         if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
1116                 return NULL;
1117         wl->wiphy = hw->wiphy;
1118
1119         atomic_set(&wl->callbacks, 0);
1120
1121         init_waitqueue_head(&wl->tx_flush_wq);
1122
1123         /* setup the bottom half handler */
1124         tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
1125
1126         spin_lock_init(&wl->lock);
1127         spin_lock_init(&wl->isr_lock);
1128
1129         /* common load-time initialization */
1130         wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
1131         if (!wl->wlc) {
1132                 wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
1133                           KBUILD_MODNAME, err);
1134                 goto fail;
1135         }
1136         wl->pub = brcms_c_pub(wl->wlc);
1137
1138         wl->pub->ieee_hw = hw;
1139
1140         /* register our interrupt handler */
1141         if (request_irq(pdev->irq, brcms_isr,
1142                         IRQF_SHARED, KBUILD_MODNAME, wl)) {
1143                 wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
1144                 goto fail;
1145         }
1146         wl->irq = pdev->irq;
1147
1148         /* register module */
1149         brcms_c_module_register(wl->pub, "linux", wl, NULL);
1150
1151         if (ieee_hw_init(hw)) {
1152                 wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
1153                           __func__);
1154                 goto fail;
1155         }
1156
1157         brcms_c_regd_init(wl->wlc);
1158
1159         memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
1160         if (WARN_ON(!is_valid_ether_addr(perm)))
1161                 goto fail;
1162         SET_IEEE80211_PERM_ADDR(hw, perm);
1163
1164         err = ieee80211_register_hw(hw);
1165         if (err)
1166                 wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
1167                           "%d\n", __func__, err);
1168
1169         if (wl->pub->srom_ccode[0] &&
1170             regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
1171                 wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
1172
1173         brcms_debugfs_attach(wl->pub);
1174         brcms_debugfs_create_files(wl->pub);
1175         n_adapters_found++;
1176         return wl;
1177
1178 fail:
1179         brcms_free(wl);
1180         return NULL;
1181 }
1182
1183
1184
1185 /**
1186  * determines if a device is a WL device, and if so, attaches it.
1187  *
1188  * This function determines if a device pointed to by pdev is a WL device,
1189  * and if so, performs a brcms_attach() on it.
1190  *
1191  * Perimeter lock is initialized in the course of this function.
1192  */
1193 static int brcms_bcma_probe(struct bcma_device *pdev)
1194 {
1195         struct brcms_info *wl;
1196         struct ieee80211_hw *hw;
1197
1198         dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
1199                  pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
1200                  pdev->irq);
1201
1202         if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
1203             (pdev->id.id != BCMA_CORE_80211))
1204                 return -ENODEV;
1205
1206         hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
1207         if (!hw) {
1208                 pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
1209                 return -ENOMEM;
1210         }
1211
1212         SET_IEEE80211_DEV(hw, &pdev->dev);
1213
1214         bcma_set_drvdata(pdev, hw);
1215
1216         memset(hw->priv, 0, sizeof(*wl));
1217
1218         wl = brcms_attach(pdev);
1219         if (!wl) {
1220                 pr_err("%s: brcms_attach failed!\n", __func__);
1221                 return -ENODEV;
1222         }
1223         brcms_led_register(wl);
1224
1225         return 0;
1226 }
1227
1228 static int brcms_suspend(struct bcma_device *pdev)
1229 {
1230         struct brcms_info *wl;
1231         struct ieee80211_hw *hw;
1232
1233         hw = bcma_get_drvdata(pdev);
1234         wl = hw->priv;
1235         if (!wl) {
1236                 pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
1237                        __func__);
1238                 return -ENODEV;
1239         }
1240
1241         /* only need to flag hw is down for proper resume */
1242         spin_lock_bh(&wl->lock);
1243         wl->pub->hw_up = false;
1244         spin_unlock_bh(&wl->lock);
1245
1246         brcms_dbg_info(wl->wlc->hw->d11core, "brcms_suspend ok\n");
1247
1248         return 0;
1249 }
1250
1251 static int brcms_resume(struct bcma_device *pdev)
1252 {
1253         return 0;
1254 }
1255
1256 static struct bcma_driver brcms_bcma_driver = {
1257         .name     = KBUILD_MODNAME,
1258         .probe    = brcms_bcma_probe,
1259         .suspend  = brcms_suspend,
1260         .resume   = brcms_resume,
1261         .remove   = brcms_remove,
1262         .id_table = brcms_coreid_table,
1263 };
1264
1265 /**
1266  * This is the main entry point for the brcmsmac driver.
1267  *
1268  * This function is scheduled upon module initialization and
1269  * does the driver registration, which result in brcms_bcma_probe()
1270  * call resulting in the driver bringup.
1271  */
1272 static void brcms_driver_init(struct work_struct *work)
1273 {
1274         int error;
1275
1276         error = bcma_driver_register(&brcms_bcma_driver);
1277         if (error)
1278                 pr_err("%s: register returned %d\n", __func__, error);
1279 }
1280
1281 static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
1282
1283 static int __init brcms_module_init(void)
1284 {
1285         brcms_debugfs_init();
1286         if (!schedule_work(&brcms_driver_work))
1287                 return -EBUSY;
1288
1289         return 0;
1290 }
1291
1292 /**
1293  * This function unloads the brcmsmac driver from the system.
1294  *
1295  * This function unconditionally unloads the brcmsmac driver module from the
1296  * system.
1297  *
1298  */
1299 static void __exit brcms_module_exit(void)
1300 {
1301         cancel_work_sync(&brcms_driver_work);
1302         bcma_driver_unregister(&brcms_bcma_driver);
1303         brcms_debugfs_exit();
1304 }
1305
1306 module_init(brcms_module_init);
1307 module_exit(brcms_module_exit);
1308
1309 /*
1310  * precondition: perimeter lock has been acquired
1311  */
1312 void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
1313                          bool state, int prio)
1314 {
1315         brcms_err(wl->wlc->hw->d11core, "Shouldn't be here %s\n", __func__);
1316 }
1317
1318 /*
1319  * precondition: perimeter lock has been acquired
1320  */
1321 void brcms_init(struct brcms_info *wl)
1322 {
1323         brcms_dbg_info(wl->wlc->hw->d11core, "Initializing wl%d\n",
1324                        wl->pub->unit);
1325         brcms_reset(wl);
1326         brcms_c_init(wl->wlc, wl->mute_tx);
1327 }
1328
1329 /*
1330  * precondition: perimeter lock has been acquired
1331  */
1332 uint brcms_reset(struct brcms_info *wl)
1333 {
1334         brcms_dbg_info(wl->wlc->hw->d11core, "Resetting wl%d\n", wl->pub->unit);
1335         brcms_c_reset(wl->wlc);
1336
1337         /* dpc will not be rescheduled */
1338         wl->resched = false;
1339
1340         /* inform publicly that interface is down */
1341         wl->pub->up = false;
1342
1343         return 0;
1344 }
1345
1346 void brcms_fatal_error(struct brcms_info *wl)
1347 {
1348         brcms_err(wl->wlc->hw->d11core, "wl%d: fatal error, reinitializing\n",
1349                   wl->wlc->pub->unit);
1350         brcms_reset(wl);
1351         ieee80211_restart_hw(wl->pub->ieee_hw);
1352 }
1353
1354 /*
1355  * These are interrupt on/off entry points. Disable interrupts
1356  * during interrupt state transition.
1357  */
1358 void brcms_intrson(struct brcms_info *wl)
1359 {
1360         unsigned long flags;
1361
1362         spin_lock_irqsave(&wl->isr_lock, flags);
1363         brcms_c_intrson(wl->wlc);
1364         spin_unlock_irqrestore(&wl->isr_lock, flags);
1365 }
1366
1367 u32 brcms_intrsoff(struct brcms_info *wl)
1368 {
1369         unsigned long flags;
1370         u32 status;
1371
1372         spin_lock_irqsave(&wl->isr_lock, flags);
1373         status = brcms_c_intrsoff(wl->wlc);
1374         spin_unlock_irqrestore(&wl->isr_lock, flags);
1375         return status;
1376 }
1377
1378 void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
1379 {
1380         unsigned long flags;
1381
1382         spin_lock_irqsave(&wl->isr_lock, flags);
1383         brcms_c_intrsrestore(wl->wlc, macintmask);
1384         spin_unlock_irqrestore(&wl->isr_lock, flags);
1385 }
1386
1387 /*
1388  * precondition: perimeter lock has been acquired
1389  */
1390 int brcms_up(struct brcms_info *wl)
1391 {
1392         int error = 0;
1393
1394         if (wl->pub->up)
1395                 return 0;
1396
1397         error = brcms_c_up(wl->wlc);
1398
1399         return error;
1400 }
1401
1402 /*
1403  * precondition: perimeter lock has been acquired
1404  */
1405 void brcms_down(struct brcms_info *wl)
1406 {
1407         uint callbacks, ret_val = 0;
1408
1409         /* call common down function */
1410         ret_val = brcms_c_down(wl->wlc);
1411         callbacks = atomic_read(&wl->callbacks) - ret_val;
1412
1413         /* wait for down callbacks to complete */
1414         spin_unlock_bh(&wl->lock);
1415
1416         /* For HIGH_only driver, it's important to actually schedule other work,
1417          * not just spin wait since everything runs at schedule level
1418          */
1419         SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1420
1421         spin_lock_bh(&wl->lock);
1422 }
1423
1424 /*
1425 * precondition: perimeter lock is not acquired
1426  */
1427 static void _brcms_timer(struct work_struct *work)
1428 {
1429         struct brcms_timer *t = container_of(work, struct brcms_timer,
1430                                              dly_wrk.work);
1431
1432         spin_lock_bh(&t->wl->lock);
1433
1434         if (t->set) {
1435                 if (t->periodic) {
1436                         atomic_inc(&t->wl->callbacks);
1437                         ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
1438                                                      &t->dly_wrk,
1439                                                      msecs_to_jiffies(t->ms));
1440                 } else {
1441                         t->set = false;
1442                 }
1443
1444                 t->fn(t->arg);
1445         }
1446
1447         atomic_dec(&t->wl->callbacks);
1448
1449         spin_unlock_bh(&t->wl->lock);
1450 }
1451
1452 /*
1453  * Adds a timer to the list. Caller supplies a timer function.
1454  * Is called from wlc.
1455  *
1456  * precondition: perimeter lock has been acquired
1457  */
1458 struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
1459                                      void (*fn) (void *arg),
1460                                      void *arg, const char *name)
1461 {
1462         struct brcms_timer *t;
1463
1464         t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
1465         if (!t)
1466                 return NULL;
1467
1468         INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
1469         t->wl = wl;
1470         t->fn = fn;
1471         t->arg = arg;
1472         t->next = wl->timers;
1473         wl->timers = t;
1474
1475 #ifdef DEBUG
1476         t->name = kstrdup(name, GFP_ATOMIC);
1477 #endif
1478
1479         return t;
1480 }
1481
1482 /*
1483  * adds only the kernel timer since it's going to be more accurate
1484  * as well as it's easier to make it periodic
1485  *
1486  * precondition: perimeter lock has been acquired
1487  */
1488 void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
1489 {
1490         struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
1491
1492 #ifdef DEBUG
1493         if (t->set)
1494                 brcms_dbg_info(t->wl->wlc->hw->d11core,
1495                                "%s: Already set. Name: %s, per %d\n",
1496                                __func__, t->name, periodic);
1497 #endif
1498         t->ms = ms;
1499         t->periodic = (bool) periodic;
1500         if (!t->set) {
1501                 t->set = true;
1502                 atomic_inc(&t->wl->callbacks);
1503         }
1504
1505         ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
1506 }
1507
1508 /*
1509  * return true if timer successfully deleted, false if still pending
1510  *
1511  * precondition: perimeter lock has been acquired
1512  */
1513 bool brcms_del_timer(struct brcms_timer *t)
1514 {
1515         if (t->set) {
1516                 t->set = false;
1517                 if (!cancel_delayed_work(&t->dly_wrk))
1518                         return false;
1519
1520                 atomic_dec(&t->wl->callbacks);
1521         }
1522
1523         return true;
1524 }
1525
1526 /*
1527  * precondition: perimeter lock has been acquired
1528  */
1529 void brcms_free_timer(struct brcms_timer *t)
1530 {
1531         struct brcms_info *wl = t->wl;
1532         struct brcms_timer *tmp;
1533
1534         /* delete the timer in case it is active */
1535         brcms_del_timer(t);
1536
1537         if (wl->timers == t) {
1538                 wl->timers = wl->timers->next;
1539 #ifdef DEBUG
1540                 kfree(t->name);
1541 #endif
1542                 kfree(t);
1543                 return;
1544
1545         }
1546
1547         tmp = wl->timers;
1548         while (tmp) {
1549                 if (tmp->next == t) {
1550                         tmp->next = t->next;
1551 #ifdef DEBUG
1552                         kfree(t->name);
1553 #endif
1554                         kfree(t);
1555                         return;
1556                 }
1557                 tmp = tmp->next;
1558         }
1559
1560 }
1561
1562 /*
1563  * precondition: perimeter lock has been acquired
1564  */
1565 int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
1566 {
1567         int i, entry;
1568         const u8 *pdata;
1569         struct firmware_hdr *hdr;
1570         for (i = 0; i < wl->fw.fw_cnt; i++) {
1571                 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1572                 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1573                      entry++, hdr++) {
1574                         u32 len = le32_to_cpu(hdr->len);
1575                         if (le32_to_cpu(hdr->idx) == idx) {
1576                                 pdata = wl->fw.fw_bin[i]->data +
1577                                         le32_to_cpu(hdr->offset);
1578                                 *pbuf = kmemdup(pdata, len, GFP_ATOMIC);
1579                                 if (*pbuf == NULL)
1580                                         goto fail;
1581
1582                                 return 0;
1583                         }
1584                 }
1585         }
1586         brcms_err(wl->wlc->hw->d11core,
1587                   "ERROR: ucode buf tag:%d can not be found!\n", idx);
1588         *pbuf = NULL;
1589 fail:
1590         return -ENODATA;
1591 }
1592
1593 /*
1594  * Precondition: Since this function is called in brcms_bcma_probe() context,
1595  * no locking is required.
1596  */
1597 int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, 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                         if (le32_to_cpu(hdr->idx) == idx) {
1607                                 pdata = wl->fw.fw_bin[i]->data +
1608                                         le32_to_cpu(hdr->offset);
1609                                 if (le32_to_cpu(hdr->len) != 4) {
1610                                         brcms_err(wl->wlc->hw->d11core,
1611                                                   "ERROR: fw hdr len\n");
1612                                         return -ENOMSG;
1613                                 }
1614                                 *n_bytes = le32_to_cpu(*((__le32 *) pdata));
1615                                 return 0;
1616                         }
1617                 }
1618         }
1619         brcms_err(wl->wlc->hw->d11core,
1620                   "ERROR: ucode tag:%d can not be found!\n", idx);
1621         return -ENOMSG;
1622 }
1623
1624 /*
1625  * precondition: can both be called locked and unlocked
1626  */
1627 void brcms_ucode_free_buf(void *p)
1628 {
1629         kfree(p);
1630 }
1631
1632 /*
1633  * checks validity of all firmware images loaded from user space
1634  *
1635  * Precondition: Since this function is called in brcms_bcma_probe() context,
1636  * no locking is required.
1637  */
1638 int brcms_check_firmwares(struct brcms_info *wl)
1639 {
1640         int i;
1641         int entry;
1642         int rc = 0;
1643         const struct firmware *fw;
1644         const struct firmware *fw_hdr;
1645         struct firmware_hdr *ucode_hdr;
1646         for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
1647                 fw =  wl->fw.fw_bin[i];
1648                 fw_hdr = wl->fw.fw_hdr[i];
1649                 if (fw == NULL && fw_hdr == NULL) {
1650                         break;
1651                 } else if (fw == NULL || fw_hdr == NULL) {
1652                         wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
1653                                   __func__);
1654                         rc = -EBADF;
1655                 } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
1656                         wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
1657                                 "size %zu/%zu\n", __func__, fw_hdr->size,
1658                                 sizeof(struct firmware_hdr));
1659                         rc = -EBADF;
1660                 } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
1661                         wiphy_err(wl->wiphy, "%s: out of bounds fw file size %zu\n",
1662                                   __func__, fw->size);
1663                         rc = -EBADF;
1664                 } else {
1665                         /* check if ucode section overruns firmware image */
1666                         ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
1667                         for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
1668                              !rc; entry++, ucode_hdr++) {
1669                                 if (le32_to_cpu(ucode_hdr->offset) +
1670                                     le32_to_cpu(ucode_hdr->len) >
1671                                     fw->size) {
1672                                         wiphy_err(wl->wiphy,
1673                                                   "%s: conflicting bin/hdr\n",
1674                                                   __func__);
1675                                         rc = -EBADF;
1676                                 }
1677                         }
1678                 }
1679         }
1680         if (rc == 0 && wl->fw.fw_cnt != i) {
1681                 wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
1682                         wl->fw.fw_cnt);
1683                 rc = -EBADF;
1684         }
1685         return rc;
1686 }
1687
1688 /*
1689  * precondition: perimeter lock has been acquired
1690  */
1691 bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
1692 {
1693         bool blocked = brcms_c_check_radio_disabled(wl->wlc);
1694
1695         spin_unlock_bh(&wl->lock);
1696         wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
1697         if (blocked)
1698                 wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
1699         spin_lock_bh(&wl->lock);
1700         return blocked;
1701 }