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