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