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