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