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