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