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