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