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