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