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