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