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