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