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