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