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