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