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