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