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