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