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