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