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