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