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