staging: brcm80211: renamed files to get rid of wl_ file name prefix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / brcmsmac / brcms_mac80211.c
blob8eadb4365fa23bd82d37e3ea5af845e0e2f7275f
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
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.
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.
17 #define __UNDEF_NO_VERSION__
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 <bcmdefs.h>
29 #include <bcmwifi.h>
30 #include <bcmutils.h>
31 #include <bcmnvram.h>
32 #include <nicpci.h>
33 #include "bcmdma.h"
35 #include "phy/wlc_phy_int.h"
36 #include "d11.h"
37 #include "wlc_types.h"
38 #include "wlc_cfg.h"
39 #include "wlc_key.h"
40 #include "wlc_channel.h"
41 #include "wlc_scb.h"
42 #include "wlc_pub.h"
43 #include "wl_dbg.h"
44 #include "wl_export.h"
45 #include "ucode_loader.h"
46 #include "brcms_mac80211.h"
48 #define N_TX_QUEUES 4 /* #tx queues on mac80211<->driver interface */
50 #define LOCK(wl) spin_lock_bh(&(wl)->lock)
51 #define UNLOCK(wl) spin_unlock_bh(&(wl)->lock)
53 /* locking from inside brcms_isr */
54 #define ISR_LOCK(wl, flags)\
55 do {\
56 spin_lock(&(wl)->isr_lock);\
57 (void)(flags); } \
58 while (0)
60 #define ISR_UNLOCK(wl, flags)\
61 do {\
62 spin_unlock(&(wl)->isr_lock);\
63 (void)(flags); } \
64 while (0)
66 /* locking under LOCK() to synchronize with brcms_isr */
67 #define INT_LOCK(wl, flags) spin_lock_irqsave(&(wl)->isr_lock, flags)
68 #define INT_UNLOCK(wl, flags) spin_unlock_irqrestore(&(wl)->isr_lock, flags)
70 static void brcms_timer(unsigned long data);
71 static void _brcms_timer(struct brcms_timer *t);
74 static int ieee_hw_init(struct ieee80211_hw *hw);
75 static int ieee_hw_rate_init(struct ieee80211_hw *hw);
77 static int wl_linux_watchdog(void *ctx);
79 /* Flags we support */
80 #define MAC_FILTERS (FIF_PROMISC_IN_BSS | \
81 FIF_ALLMULTI | \
82 FIF_FCSFAIL | \
83 FIF_PLCPFAIL | \
84 FIF_CONTROL | \
85 FIF_OTHER_BSS | \
86 FIF_BCN_PRBRESP_PROMISC)
88 static int n_adapters_found;
90 static int brcms_request_fw(struct brcms_info *wl, struct pci_dev *pdev);
91 static void brcms_release_fw(struct brcms_info *wl);
93 /* local prototypes */
94 static void brcms_dpc(unsigned long data);
95 static irqreturn_t brcms_isr(int irq, void *dev_id);
97 static int __devinit brcms_pci_probe(struct pci_dev *pdev,
98 const struct pci_device_id *ent);
99 static void brcms_remove(struct pci_dev *pdev);
100 static void brcms_free(struct brcms_info *wl);
101 static void brcms_set_basic_rate(struct wl_rateset *rs, u16 rate, bool is_br);
103 MODULE_AUTHOR("Broadcom Corporation");
104 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
105 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
106 MODULE_LICENSE("Dual BSD/GPL");
108 /* recognized PCI IDs */
109 static DEFINE_PCI_DEVICE_TABLE(brcms_pci_id_table) = {
110 {PCI_VENDOR_ID_BROADCOM, 0x4357, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* 43225 2G */
111 {PCI_VENDOR_ID_BROADCOM, 0x4353, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* 43224 DUAL */
112 {PCI_VENDOR_ID_BROADCOM, 0x4727, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* 4313 DUAL */
113 /* 43224 Ven */
114 {PCI_VENDOR_ID_BROADCOM, 0x0576, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
118 MODULE_DEVICE_TABLE(pci, brcms_pci_id_table);
120 #ifdef BCMDBG
121 static int msglevel = 0xdeadbeef;
122 module_param(msglevel, int, 0);
123 static int phymsglevel = 0xdeadbeef;
124 module_param(phymsglevel, int, 0);
125 #endif /* BCMDBG */
127 #define HW_TO_WL(hw) (hw->priv)
128 #define WL_TO_HW(wl) (wl->pub->ieee_hw)
130 /* MAC80211 callback functions */
131 static int brcms_ops_start(struct ieee80211_hw *hw);
132 static void brcms_ops_stop(struct ieee80211_hw *hw);
133 static int brcms_ops_add_interface(struct ieee80211_hw *hw,
134 struct ieee80211_vif *vif);
135 static void brcms_ops_remove_interface(struct ieee80211_hw *hw,
136 struct ieee80211_vif *vif);
137 static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed);
138 static void brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
139 struct ieee80211_vif *vif,
140 struct ieee80211_bss_conf *info,
141 u32 changed);
142 static void brcms_ops_configure_filter(struct ieee80211_hw *hw,
143 unsigned int changed_flags,
144 unsigned int *total_flags, u64 multicast);
145 static int brcms_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
146 bool set);
147 static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw);
148 static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw);
149 static void brcms_ops_set_tsf(struct ieee80211_hw *hw, u64 tsf);
150 static int brcms_ops_get_stats(struct ieee80211_hw *hw,
151 struct ieee80211_low_level_stats *stats);
152 static void brcms_ops_sta_notify(struct ieee80211_hw *hw,
153 struct ieee80211_vif *vif,
154 enum sta_notify_cmd cmd,
155 struct ieee80211_sta *sta);
156 static int brcms_ops_conf_tx(struct ieee80211_hw *hw, u16 queue,
157 const struct ieee80211_tx_queue_params *params);
158 static u64 brcms_ops_get_tsf(struct ieee80211_hw *hw);
159 static int brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
160 struct ieee80211_sta *sta);
161 static int brcms_ops_sta_remove(struct ieee80211_hw *hw,
162 struct ieee80211_vif *vif,
163 struct ieee80211_sta *sta);
164 static int brcms_ops_ampdu_action(struct ieee80211_hw *hw,
165 struct ieee80211_vif *vif,
166 enum ieee80211_ampdu_mlme_action action,
167 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
168 u8 buf_size);
169 static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw);
170 static void brcms_ops_flush(struct ieee80211_hw *hw, bool drop);
172 static void brcms_ops_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
174 struct brcms_info *wl = hw->priv;
176 LOCK(wl);
177 if (!wl->pub->up) {
178 wiphy_err(wl->wiphy, "ops->tx called while down\n");
179 kfree_skb(skb);
180 goto done;
182 wlc_sendpkt_mac80211(wl->wlc, skb, hw);
183 done:
184 UNLOCK(wl);
187 static int brcms_ops_start(struct ieee80211_hw *hw)
189 struct brcms_info *wl = hw->priv;
190 bool blocked;
192 struct ieee80211_channel *curchan = hw->conf.channel;
195 ieee80211_wake_queues(hw);
196 LOCK(wl);
197 blocked = brcms_rfkill_set_hw_state(wl);
198 UNLOCK(wl);
199 if (!blocked)
200 wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
202 return 0;
205 static void brcms_ops_stop(struct ieee80211_hw *hw)
207 ieee80211_stop_queues(hw);
210 static int
211 brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
213 struct brcms_info *wl;
214 int err;
216 /* Just STA for now */
217 if (vif->type != NL80211_IFTYPE_AP &&
218 vif->type != NL80211_IFTYPE_MESH_POINT &&
219 vif->type != NL80211_IFTYPE_STATION &&
220 vif->type != NL80211_IFTYPE_WDS &&
221 vif->type != NL80211_IFTYPE_ADHOC) {
222 wiphy_err(hw->wiphy, "%s: Attempt to add type %d, only"
223 " STA for now\n", __func__, vif->type);
224 return -EOPNOTSUPP;
227 wl = HW_TO_WL(hw);
228 LOCK(wl);
229 err = brcms_up(wl);
230 UNLOCK(wl);
232 if (err != 0) {
233 wiphy_err(hw->wiphy, "%s: brcms_up() returned %d\n", __func__,
234 err);
236 return err;
239 static void
240 brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
242 struct brcms_info *wl;
244 wl = HW_TO_WL(hw);
246 /* put driver in down state */
247 LOCK(wl);
248 brcms_down(wl);
249 UNLOCK(wl);
253 * precondition: perimeter lock has been acquired
255 static int
256 ieee_set_channel(struct ieee80211_hw *hw, struct ieee80211_channel *chan,
257 enum nl80211_channel_type type)
259 struct brcms_info *wl = HW_TO_WL(hw);
260 int err = 0;
262 switch (type) {
263 case NL80211_CHAN_HT20:
264 case NL80211_CHAN_NO_HT:
265 err = wlc_set(wl->wlc, WLC_SET_CHANNEL, chan->hw_value);
266 break;
267 case NL80211_CHAN_HT40MINUS:
268 case NL80211_CHAN_HT40PLUS:
269 wiphy_err(hw->wiphy,
270 "%s: Need to implement 40 Mhz Channels!\n", __func__);
271 err = 1;
272 break;
275 if (err)
276 return -EIO;
277 return err;
280 static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
282 struct ieee80211_conf *conf = &hw->conf;
283 struct brcms_info *wl = HW_TO_WL(hw);
284 int err = 0;
285 int new_int;
286 struct wiphy *wiphy = hw->wiphy;
288 LOCK(wl);
289 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
290 if (wlc_set_par(wl->wlc, IOV_BCN_LI_BCN, conf->listen_interval)
291 < 0) {
292 wiphy_err(wiphy, "%s: Error setting listen_interval\n",
293 __func__);
294 err = -EIO;
295 goto config_out;
297 wlc_get_par(wl->wlc, IOV_BCN_LI_BCN, &new_int);
299 if (changed & IEEE80211_CONF_CHANGE_MONITOR)
300 wiphy_err(wiphy, "%s: change monitor mode: %s (implement)\n",
301 __func__, conf->flags & IEEE80211_CONF_MONITOR ?
302 "true" : "false");
303 if (changed & IEEE80211_CONF_CHANGE_PS)
304 wiphy_err(wiphy, "%s: change power-save mode: %s (implement)\n",
305 __func__, conf->flags & IEEE80211_CONF_PS ?
306 "true" : "false");
308 if (changed & IEEE80211_CONF_CHANGE_POWER) {
309 if (wlc_set_par(wl->wlc, IOV_QTXPOWER, conf->power_level * 4)
310 < 0) {
311 wiphy_err(wiphy, "%s: Error setting power_level\n",
312 __func__);
313 err = -EIO;
314 goto config_out;
316 wlc_get_par(wl->wlc, IOV_QTXPOWER, &new_int);
317 if (new_int != (conf->power_level * 4))
318 wiphy_err(wiphy, "%s: Power level req != actual, %d %d"
319 "\n", __func__, conf->power_level * 4,
320 new_int);
322 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
323 err = ieee_set_channel(hw, conf->channel, conf->channel_type);
325 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
326 if (wlc_set
327 (wl->wlc, WLC_SET_SRL,
328 conf->short_frame_max_tx_count) < 0) {
329 wiphy_err(wiphy, "%s: Error setting srl\n", __func__);
330 err = -EIO;
331 goto config_out;
333 if (wlc_set(wl->wlc, WLC_SET_LRL, conf->long_frame_max_tx_count)
334 < 0) {
335 wiphy_err(wiphy, "%s: Error setting lrl\n", __func__);
336 err = -EIO;
337 goto config_out;
341 config_out:
342 UNLOCK(wl);
343 return err;
346 static void
347 brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
348 struct ieee80211_vif *vif,
349 struct ieee80211_bss_conf *info, u32 changed)
351 struct brcms_info *wl = HW_TO_WL(hw);
352 struct wiphy *wiphy = hw->wiphy;
353 int val;
355 if (changed & BSS_CHANGED_ASSOC) {
356 /* association status changed (associated/disassociated)
357 * also implies a change in the AID.
359 wiphy_err(wiphy, "%s: %s: %sassociated\n", KBUILD_MODNAME,
360 __func__, info->assoc ? "" : "dis");
361 LOCK(wl);
362 wlc_associate_upd(wl->wlc, info->assoc);
363 UNLOCK(wl);
365 if (changed & BSS_CHANGED_ERP_SLOT) {
366 /* slot timing changed */
367 if (info->use_short_slot)
368 val = 1;
369 else
370 val = 0;
371 LOCK(wl);
372 wlc_set(wl->wlc, WLC_SET_SHORTSLOT_OVERRIDE, val);
373 UNLOCK(wl);
376 if (changed & BSS_CHANGED_HT) {
377 /* 802.11n parameters changed */
378 u16 mode = info->ht_operation_mode;
380 LOCK(wl);
381 wlc_protection_upd(wl->wlc, WLC_PROT_N_CFG,
382 mode & IEEE80211_HT_OP_MODE_PROTECTION);
383 wlc_protection_upd(wl->wlc, WLC_PROT_N_NONGF,
384 mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
385 wlc_protection_upd(wl->wlc, WLC_PROT_N_OBSS,
386 mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
387 UNLOCK(wl);
389 if (changed & BSS_CHANGED_BASIC_RATES) {
390 struct ieee80211_supported_band *bi;
391 u32 br_mask, i;
392 u16 rate;
393 struct wl_rateset rs;
394 int error;
396 /* retrieve the current rates */
397 LOCK(wl);
398 error = wlc_ioctl(wl->wlc, WLC_GET_CURR_RATESET,
399 &rs, sizeof(rs), NULL);
400 UNLOCK(wl);
401 if (error) {
402 wiphy_err(wiphy, "%s: retrieve rateset failed: %d\n",
403 __func__, error);
404 return;
406 br_mask = info->basic_rates;
407 bi = hw->wiphy->bands[wlc_get_curband(wl->wlc)];
408 for (i = 0; i < bi->n_bitrates; i++) {
409 /* convert to internal rate value */
410 rate = (bi->bitrates[i].bitrate << 1) / 10;
412 /* set/clear basic rate flag */
413 brcms_set_basic_rate(&rs, rate, br_mask & 1);
414 br_mask >>= 1;
417 /* update the rate set */
418 LOCK(wl);
419 wlc_ioctl(wl->wlc, WLC_SET_RATESET, &rs, sizeof(rs), NULL);
420 UNLOCK(wl);
422 if (changed & BSS_CHANGED_BEACON_INT) {
423 /* Beacon interval changed */
424 LOCK(wl);
425 wlc_set(wl->wlc, WLC_SET_BCNPRD, info->beacon_int);
426 UNLOCK(wl);
428 if (changed & BSS_CHANGED_BSSID) {
429 /* BSSID changed, for whatever reason (IBSS and managed mode) */
430 LOCK(wl);
431 wlc_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET,
432 info->bssid);
433 UNLOCK(wl);
435 if (changed & BSS_CHANGED_BEACON) {
436 /* Beacon data changed, retrieve new beacon (beaconing modes) */
437 wiphy_err(wiphy, "%s: beacon changed\n", __func__);
439 if (changed & BSS_CHANGED_BEACON_ENABLED) {
440 /* Beaconing should be enabled/disabled (beaconing modes) */
441 wiphy_err(wiphy, "%s: Beacon enabled: %s\n", __func__,
442 info->enable_beacon ? "true" : "false");
444 if (changed & BSS_CHANGED_CQM) {
445 /* Connection quality monitor config changed */
446 wiphy_err(wiphy, "%s: cqm change: threshold %d, hys %d "
447 " (implement)\n", __func__, info->cqm_rssi_thold,
448 info->cqm_rssi_hyst);
450 if (changed & BSS_CHANGED_IBSS) {
451 /* IBSS join status changed */
452 wiphy_err(wiphy, "%s: IBSS joined: %s (implement)\n", __func__,
453 info->ibss_joined ? "true" : "false");
455 if (changed & BSS_CHANGED_ARP_FILTER) {
456 /* Hardware ARP filter address list or state changed */
457 wiphy_err(wiphy, "%s: arp filtering: enabled %s, count %d"
458 " (implement)\n", __func__, info->arp_filter_enabled ?
459 "true" : "false", info->arp_addr_cnt);
461 if (changed & BSS_CHANGED_QOS) {
463 * QoS for this association was enabled/disabled.
464 * Note that it is only ever disabled for station mode.
466 wiphy_err(wiphy, "%s: qos enabled: %s (implement)\n", __func__,
467 info->qos ? "true" : "false");
469 if (changed & BSS_CHANGED_IDLE) {
470 /* Idle changed for this BSS/interface */
471 wiphy_err(wiphy, "%s: BSS idle: %s (implement)\n", __func__,
472 info->idle ? "true" : "false");
474 return;
477 static void
478 brcms_ops_configure_filter(struct ieee80211_hw *hw,
479 unsigned int changed_flags,
480 unsigned int *total_flags, u64 multicast)
482 struct brcms_info *wl = hw->priv;
483 struct wiphy *wiphy = hw->wiphy;
485 changed_flags &= MAC_FILTERS;
486 *total_flags &= MAC_FILTERS;
487 if (changed_flags & FIF_PROMISC_IN_BSS)
488 wiphy_err(wiphy, "FIF_PROMISC_IN_BSS\n");
489 if (changed_flags & FIF_ALLMULTI)
490 wiphy_err(wiphy, "FIF_ALLMULTI\n");
491 if (changed_flags & FIF_FCSFAIL)
492 wiphy_err(wiphy, "FIF_FCSFAIL\n");
493 if (changed_flags & FIF_PLCPFAIL)
494 wiphy_err(wiphy, "FIF_PLCPFAIL\n");
495 if (changed_flags & FIF_CONTROL)
496 wiphy_err(wiphy, "FIF_CONTROL\n");
497 if (changed_flags & FIF_OTHER_BSS)
498 wiphy_err(wiphy, "FIF_OTHER_BSS\n");
499 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
500 LOCK(wl);
501 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
502 wl->pub->mac80211_state |= MAC80211_PROMISC_BCNS;
503 wlc_mac_bcn_promisc_change(wl->wlc, 1);
504 } else {
505 wlc_mac_bcn_promisc_change(wl->wlc, 0);
506 wl->pub->mac80211_state &= ~MAC80211_PROMISC_BCNS;
508 UNLOCK(wl);
510 return;
513 static int
514 brcms_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
516 return 0;
519 static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw)
521 struct brcms_info *wl = hw->priv;
522 LOCK(wl);
523 wlc_scan_start(wl->wlc);
524 UNLOCK(wl);
525 return;
528 static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw)
530 struct brcms_info *wl = hw->priv;
531 LOCK(wl);
532 wlc_scan_stop(wl->wlc);
533 UNLOCK(wl);
534 return;
537 static void brcms_ops_set_tsf(struct ieee80211_hw *hw, u64 tsf)
539 wiphy_err(hw->wiphy, "%s: Enter\n", __func__);
540 return;
543 static int
544 brcms_ops_get_stats(struct ieee80211_hw *hw,
545 struct ieee80211_low_level_stats *stats)
547 struct brcms_info *wl = hw->priv;
548 struct wl_cnt *cnt;
550 LOCK(wl);
551 cnt = wl->pub->_cnt;
552 stats->dot11ACKFailureCount = 0;
553 stats->dot11RTSFailureCount = 0;
554 stats->dot11FCSErrorCount = 0;
555 stats->dot11RTSSuccessCount = 0;
556 UNLOCK(wl);
557 return 0;
560 static void
561 brcms_ops_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
562 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
564 switch (cmd) {
565 default:
566 wiphy_err(hw->wiphy, "%s: Unknown cmd = %d\n", __func__,
567 cmd);
568 break;
570 return;
573 static int
574 brcms_ops_conf_tx(struct ieee80211_hw *hw, u16 queue,
575 const struct ieee80211_tx_queue_params *params)
577 struct brcms_info *wl = hw->priv;
579 LOCK(wl);
580 wlc_wme_setparams(wl->wlc, queue, params, true);
581 UNLOCK(wl);
583 return 0;
586 static u64 brcms_ops_get_tsf(struct ieee80211_hw *hw)
588 wiphy_err(hw->wiphy, "%s: Enter\n", __func__);
589 return 0;
592 static int
593 brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
594 struct ieee80211_sta *sta)
596 struct scb *scb;
598 int i;
599 struct brcms_info *wl = hw->priv;
601 /* Init the scb */
602 scb = (struct scb *)sta->drv_priv;
603 memset(scb, 0, sizeof(struct scb));
604 for (i = 0; i < NUMPRIO; i++)
605 scb->seqctl[i] = 0xFFFF;
606 scb->seqctl_nonqos = 0xFFFF;
607 scb->magic = SCB_MAGIC;
609 wl->pub->global_scb = scb;
610 wl->pub->global_ampdu = &(scb->scb_ampdu);
611 wl->pub->global_ampdu->scb = scb;
612 wl->pub->global_ampdu->max_pdu = 16;
613 bcm_pktq_init(&scb->scb_ampdu.txq, AMPDU_MAX_SCB_TID,
614 AMPDU_MAX_SCB_TID * PKTQ_LEN_DEFAULT);
616 sta->ht_cap.ht_supported = true;
617 sta->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
618 sta->ht_cap.ampdu_density = AMPDU_DEF_MPDU_DENSITY;
619 sta->ht_cap.cap = IEEE80211_HT_CAP_GRN_FLD |
620 IEEE80211_HT_CAP_SGI_20 |
621 IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_40MHZ_INTOLERANT;
623 /* minstrel_ht initiates addBA on our behalf by calling ieee80211_start_tx_ba_session() */
624 return 0;
627 static int
628 brcms_ops_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
629 struct ieee80211_sta *sta)
631 return 0;
634 static int
635 brcms_ops_ampdu_action(struct ieee80211_hw *hw,
636 struct ieee80211_vif *vif,
637 enum ieee80211_ampdu_mlme_action action,
638 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
639 u8 buf_size)
641 struct scb *scb = (struct scb *)sta->drv_priv;
642 struct brcms_info *wl = hw->priv;
643 int status;
645 if (WARN_ON(scb->magic != SCB_MAGIC))
646 return -EIDRM;
647 switch (action) {
648 case IEEE80211_AMPDU_RX_START:
649 break;
650 case IEEE80211_AMPDU_RX_STOP:
651 break;
652 case IEEE80211_AMPDU_TX_START:
653 LOCK(wl);
654 status = wlc_aggregatable(wl->wlc, tid);
655 UNLOCK(wl);
656 if (!status) {
657 wiphy_err(wl->wiphy, "START: tid %d is not agg\'able\n",
658 tid);
659 return -EINVAL;
661 /* XXX: Use the starting sequence number provided ... */
662 *ssn = 0;
663 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
664 break;
666 case IEEE80211_AMPDU_TX_STOP:
667 LOCK(wl);
668 wlc_ampdu_flush(wl->wlc, sta, tid);
669 UNLOCK(wl);
670 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
671 break;
672 case IEEE80211_AMPDU_TX_OPERATIONAL:
673 /* Not sure what to do here */
674 /* Power save wakeup */
675 break;
676 default:
677 wiphy_err(wl->wiphy, "%s: Invalid command, ignoring\n",
678 __func__);
681 return 0;
684 static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
686 struct brcms_info *wl = HW_TO_WL(hw);
687 bool blocked;
689 LOCK(wl);
690 blocked = wlc_check_radio_disabled(wl->wlc);
691 UNLOCK(wl);
693 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
696 static void brcms_ops_flush(struct ieee80211_hw *hw, bool drop)
698 struct brcms_info *wl = HW_TO_WL(hw);
700 no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
702 /* wait for packet queue and dma fifos to run empty */
703 LOCK(wl);
704 wlc_wait_for_tx_completion(wl->wlc, drop);
705 UNLOCK(wl);
708 static const struct ieee80211_ops brcms_ops = {
709 .tx = brcms_ops_tx,
710 .start = brcms_ops_start,
711 .stop = brcms_ops_stop,
712 .add_interface = brcms_ops_add_interface,
713 .remove_interface = brcms_ops_remove_interface,
714 .config = brcms_ops_config,
715 .bss_info_changed = brcms_ops_bss_info_changed,
716 .configure_filter = brcms_ops_configure_filter,
717 .set_tim = brcms_ops_set_tim,
718 .sw_scan_start = brcms_ops_sw_scan_start,
719 .sw_scan_complete = brcms_ops_sw_scan_complete,
720 .set_tsf = brcms_ops_set_tsf,
721 .get_stats = brcms_ops_get_stats,
722 .sta_notify = brcms_ops_sta_notify,
723 .conf_tx = brcms_ops_conf_tx,
724 .get_tsf = brcms_ops_get_tsf,
725 .sta_add = brcms_ops_sta_add,
726 .sta_remove = brcms_ops_sta_remove,
727 .ampdu_action = brcms_ops_ampdu_action,
728 .rfkill_poll = brcms_ops_rfkill_poll,
729 .flush = brcms_ops_flush,
733 * is called in brcms_pci_probe() context, therefore no locking required.
735 static int brcms_set_hint(struct brcms_info *wl, char *abbrev)
737 return regulatory_hint(wl->pub->ieee_hw->wiphy, abbrev);
741 * attach to the WL device.
743 * Attach to the WL device identified by vendor and device parameters.
744 * regs is a host accessible memory address pointing to WL device registers.
746 * brcms_attach is not defined as static because in the case where no bus
747 * is defined, wl_attach will never be called, and thus, gcc will issue
748 * a warning that this function is defined but not used if we declare
749 * it as static.
752 * is called in brcms_pci_probe() context, therefore no locking required.
754 static struct brcms_info *brcms_attach(u16 vendor, u16 device,
755 unsigned long regs,
756 uint bustype, void *btparam, uint irq)
758 struct brcms_info *wl = NULL;
759 int unit, err;
760 unsigned long base_addr;
761 struct ieee80211_hw *hw;
762 u8 perm[ETH_ALEN];
764 unit = n_adapters_found;
765 err = 0;
767 if (unit < 0) {
768 return NULL;
771 /* allocate private info */
772 hw = pci_get_drvdata(btparam); /* btparam == pdev */
773 if (hw != NULL)
774 wl = hw->priv;
775 if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
776 return NULL;
777 wl->wiphy = hw->wiphy;
779 atomic_set(&wl->callbacks, 0);
781 /* setup the bottom half handler */
782 tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
786 base_addr = regs;
788 if (bustype == PCI_BUS || bustype == RPC_BUS) {
789 /* Do nothing */
790 } else {
791 bustype = PCI_BUS;
792 BCMMSG(wl->wiphy, "force to PCI\n");
794 wl->bcm_bustype = bustype;
796 wl->regsva = ioremap_nocache(base_addr, PCI_BAR0_WINSZ);
797 if (wl->regsva == NULL) {
798 wiphy_err(wl->wiphy, "wl%d: ioremap() failed\n", unit);
799 goto fail;
801 spin_lock_init(&wl->lock);
802 spin_lock_init(&wl->isr_lock);
804 /* prepare ucode */
805 if (brcms_request_fw(wl, (struct pci_dev *)btparam) < 0) {
806 wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
807 "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
808 brcms_release_fw(wl);
809 brcms_remove((struct pci_dev *)btparam);
810 return NULL;
813 /* common load-time initialization */
814 wl->wlc = wlc_attach((void *)wl, vendor, device, unit, false,
815 wl->regsva, wl->bcm_bustype, btparam, &err);
816 brcms_release_fw(wl);
817 if (!wl->wlc) {
818 wiphy_err(wl->wiphy, "%s: wlc_attach() failed with code %d\n",
819 KBUILD_MODNAME, err);
820 goto fail;
822 wl->pub = wlc_pub(wl->wlc);
824 wl->pub->ieee_hw = hw;
826 if (wlc_set_par(wl->wlc, IOV_MPC, 0) < 0) {
827 wiphy_err(wl->wiphy, "wl%d: Error setting MPC variable to 0\n",
828 unit);
831 /* register our interrupt handler */
832 if (request_irq(irq, brcms_isr, IRQF_SHARED, KBUILD_MODNAME, wl)) {
833 wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
834 goto fail;
836 wl->irq = irq;
838 /* register module */
839 wlc_module_register(wl->pub, "linux", wl, wl_linux_watchdog, NULL);
841 if (ieee_hw_init(hw)) {
842 wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
843 __func__);
844 goto fail;
847 memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
848 if (WARN_ON(!is_valid_ether_addr(perm)))
849 goto fail;
850 SET_IEEE80211_PERM_ADDR(hw, perm);
852 err = ieee80211_register_hw(hw);
853 if (err) {
854 wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
855 "%d\n", __func__, err);
858 if (wl->pub->srom_ccode[0])
859 err = brcms_set_hint(wl, wl->pub->srom_ccode);
860 else
861 err = brcms_set_hint(wl, "US");
862 if (err) {
863 wiphy_err(wl->wiphy, "%s: regulatory_hint failed, status %d\n",
864 __func__, err);
867 n_adapters_found++;
868 return wl;
870 fail:
871 brcms_free(wl);
872 return NULL;
877 #define CHAN2GHZ(channel, freqency, chflags) { \
878 .band = IEEE80211_BAND_2GHZ, \
879 .center_freq = (freqency), \
880 .hw_value = (channel), \
881 .flags = chflags, \
882 .max_antenna_gain = 0, \
883 .max_power = 19, \
886 static struct ieee80211_channel brcms_2ghz_chantable[] = {
887 CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
888 CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
889 CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
890 CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
891 CHAN2GHZ(5, 2432, 0),
892 CHAN2GHZ(6, 2437, 0),
893 CHAN2GHZ(7, 2442, 0),
894 CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
895 CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
896 CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
897 CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
898 CHAN2GHZ(12, 2467,
899 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
900 IEEE80211_CHAN_NO_HT40PLUS),
901 CHAN2GHZ(13, 2472,
902 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
903 IEEE80211_CHAN_NO_HT40PLUS),
904 CHAN2GHZ(14, 2484,
905 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
906 IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
909 #define CHAN5GHZ(channel, chflags) { \
910 .band = IEEE80211_BAND_5GHZ, \
911 .center_freq = 5000 + 5*(channel), \
912 .hw_value = (channel), \
913 .flags = chflags, \
914 .max_antenna_gain = 0, \
915 .max_power = 21, \
918 static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
919 /* UNII-1 */
920 CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
921 CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
922 CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
923 CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
924 /* UNII-2 */
925 CHAN5GHZ(52,
926 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
927 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
928 CHAN5GHZ(56,
929 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
930 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
931 CHAN5GHZ(60,
932 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
933 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
934 CHAN5GHZ(64,
935 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
936 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
937 /* MID */
938 CHAN5GHZ(100,
939 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
940 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
941 CHAN5GHZ(104,
942 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
943 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
944 CHAN5GHZ(108,
945 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
946 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
947 CHAN5GHZ(112,
948 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
949 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
950 CHAN5GHZ(116,
951 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
952 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
953 CHAN5GHZ(120,
954 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
955 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
956 CHAN5GHZ(124,
957 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
958 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
959 CHAN5GHZ(128,
960 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
961 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
962 CHAN5GHZ(132,
963 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
964 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
965 CHAN5GHZ(136,
966 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
967 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
968 CHAN5GHZ(140,
969 IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
970 IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS |
971 IEEE80211_CHAN_NO_HT40MINUS),
972 /* UNII-3 */
973 CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
974 CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
975 CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
976 CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
977 CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
980 #define RATE(rate100m, _flags) { \
981 .bitrate = (rate100m), \
982 .flags = (_flags), \
983 .hw_value = (rate100m / 5), \
986 static struct ieee80211_rate legacy_ratetable[] = {
987 RATE(10, 0),
988 RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
989 RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
990 RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
991 RATE(60, 0),
992 RATE(90, 0),
993 RATE(120, 0),
994 RATE(180, 0),
995 RATE(240, 0),
996 RATE(360, 0),
997 RATE(480, 0),
998 RATE(540, 0),
1001 static struct ieee80211_supported_band brcms_band_2GHz_nphy = {
1002 .band = IEEE80211_BAND_2GHZ,
1003 .channels = brcms_2ghz_chantable,
1004 .n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
1005 .bitrates = legacy_ratetable,
1006 .n_bitrates = ARRAY_SIZE(legacy_ratetable),
1007 .ht_cap = {
1008 /* from include/linux/ieee80211.h */
1009 .cap = IEEE80211_HT_CAP_GRN_FLD |
1010 IEEE80211_HT_CAP_SGI_20 |
1011 IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_40MHZ_INTOLERANT,
1012 .ht_supported = true,
1013 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
1014 .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
1015 .mcs = {
1016 /* placeholders for now */
1017 .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
1018 .rx_highest = 500,
1019 .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
1023 static struct ieee80211_supported_band brcms_band_5GHz_nphy = {
1024 .band = IEEE80211_BAND_5GHZ,
1025 .channels = brcms_5ghz_nphy_chantable,
1026 .n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
1027 .bitrates = legacy_ratetable + 4,
1028 .n_bitrates = ARRAY_SIZE(legacy_ratetable) - 4,
1029 .ht_cap = {
1030 /* use IEEE80211_HT_CAP_* from include/linux/ieee80211.h */
1031 .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 */
1032 .ht_supported = true,
1033 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
1034 .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
1035 .mcs = {
1036 /* placeholders for now */
1037 .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
1038 .rx_highest = 500,
1039 .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
1044 * is called in brcms_pci_probe() context, therefore no locking required.
1046 static int ieee_hw_rate_init(struct ieee80211_hw *hw)
1048 struct brcms_info *wl = HW_TO_WL(hw);
1049 int has_5g;
1050 char phy_list[4];
1052 has_5g = 0;
1054 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
1055 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
1057 if (wlc_get(wl->wlc, WLC_GET_PHYLIST, (int *)&phy_list) < 0) {
1058 wiphy_err(hw->wiphy, "Phy list failed\n");
1061 if (phy_list[0] == 'n' || phy_list[0] == 'c') {
1062 if (phy_list[0] == 'c') {
1063 /* Single stream */
1064 brcms_band_2GHz_nphy.ht_cap.mcs.rx_mask[1] = 0;
1065 brcms_band_2GHz_nphy.ht_cap.mcs.rx_highest = 72;
1067 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &brcms_band_2GHz_nphy;
1068 } else {
1069 return -EPERM;
1072 /* Assume all bands use the same phy. True for 11n devices. */
1073 if (NBANDS_PUB(wl->pub) > 1) {
1074 has_5g++;
1075 if (phy_list[0] == 'n' || phy_list[0] == 'c') {
1076 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1077 &brcms_band_5GHz_nphy;
1078 } else {
1079 return -EPERM;
1082 return 0;
1086 * is called in brcms_pci_probe() context, therefore no locking required.
1088 static int ieee_hw_init(struct ieee80211_hw *hw)
1090 hw->flags = IEEE80211_HW_SIGNAL_DBM
1091 /* | IEEE80211_HW_CONNECTION_MONITOR What is this? */
1092 | IEEE80211_HW_REPORTS_TX_ACK_STATUS
1093 | IEEE80211_HW_AMPDU_AGGREGATION;
1095 hw->extra_tx_headroom = wlc_get_header_len();
1096 hw->queues = N_TX_QUEUES;
1097 /* FIXME: this doesn't seem to be used properly in minstrel_ht.
1098 * mac80211/status.c:ieee80211_tx_status() checks this value,
1099 * but mac80211/rc80211_minstrel_ht.c:minstrel_ht_get_rate()
1100 * appears to always set 3 rates
1102 hw->max_rates = 2; /* Primary rate and 1 fallback rate */
1104 hw->channel_change_time = 7 * 1000; /* channel change time is dependent on chip and band */
1105 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1107 hw->rate_control_algorithm = "minstrel_ht";
1109 hw->sta_data_size = sizeof(struct scb);
1110 return ieee_hw_rate_init(hw);
1114 * determines if a device is a WL device, and if so, attaches it.
1116 * This function determines if a device pointed to by pdev is a WL device,
1117 * and if so, performs a brcms_attach() on it.
1119 * Perimeter lock is initialized in the course of this function.
1121 static int __devinit
1122 brcms_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1124 int rc;
1125 struct brcms_info *wl;
1126 struct ieee80211_hw *hw;
1127 u32 val;
1129 dev_info(&pdev->dev, "bus %d slot %d func %d irq %d\n",
1130 pdev->bus->number, PCI_SLOT(pdev->devfn),
1131 PCI_FUNC(pdev->devfn), pdev->irq);
1133 if ((pdev->vendor != PCI_VENDOR_ID_BROADCOM) ||
1134 ((pdev->device != 0x0576) &&
1135 ((pdev->device & 0xff00) != 0x4300) &&
1136 ((pdev->device & 0xff00) != 0x4700) &&
1137 ((pdev->device < 43000) || (pdev->device > 43999))))
1138 return -ENODEV;
1140 rc = pci_enable_device(pdev);
1141 if (rc) {
1142 pr_err("%s: Cannot enable device %d-%d_%d\n",
1143 __func__, pdev->bus->number, PCI_SLOT(pdev->devfn),
1144 PCI_FUNC(pdev->devfn));
1145 return -ENODEV;
1147 pci_set_master(pdev);
1149 pci_read_config_dword(pdev, 0x40, &val);
1150 if ((val & 0x0000ff00) != 0)
1151 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1153 hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
1154 if (!hw) {
1155 pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
1156 return -ENOMEM;
1159 SET_IEEE80211_DEV(hw, &pdev->dev);
1161 pci_set_drvdata(pdev, hw);
1163 memset(hw->priv, 0, sizeof(*wl));
1165 wl = brcms_attach(pdev->vendor, pdev->device,
1166 pci_resource_start(pdev, 0), PCI_BUS, pdev,
1167 pdev->irq);
1169 if (!wl) {
1170 pr_err("%s: %s: brcms_attach failed!\n", KBUILD_MODNAME,
1171 __func__);
1172 return -ENODEV;
1174 return 0;
1177 static int brcms_suspend(struct pci_dev *pdev, pm_message_t state)
1179 struct brcms_info *wl;
1180 struct ieee80211_hw *hw;
1182 hw = pci_get_drvdata(pdev);
1183 wl = HW_TO_WL(hw);
1184 if (!wl) {
1185 wiphy_err(wl->wiphy,
1186 "brcms_suspend: pci_get_drvdata failed\n");
1187 return -ENODEV;
1190 /* only need to flag hw is down for proper resume */
1191 LOCK(wl);
1192 wl->pub->hw_up = false;
1193 UNLOCK(wl);
1195 pci_save_state(pdev);
1196 pci_disable_device(pdev);
1197 return pci_set_power_state(pdev, PCI_D3hot);
1200 static int brcms_resume(struct pci_dev *pdev)
1202 struct brcms_info *wl;
1203 struct ieee80211_hw *hw;
1204 int err = 0;
1205 u32 val;
1207 hw = pci_get_drvdata(pdev);
1208 wl = HW_TO_WL(hw);
1209 if (!wl) {
1210 wiphy_err(wl->wiphy,
1211 "wl: brcms_resume: pci_get_drvdata failed\n");
1212 return -ENODEV;
1215 err = pci_set_power_state(pdev, PCI_D0);
1216 if (err)
1217 return err;
1219 pci_restore_state(pdev);
1221 err = pci_enable_device(pdev);
1222 if (err)
1223 return err;
1225 pci_set_master(pdev);
1227 pci_read_config_dword(pdev, 0x40, &val);
1228 if ((val & 0x0000ff00) != 0)
1229 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1232 * done. driver will be put in up state
1233 * in brcms_ops_add_interface() call.
1235 return err;
1239 * called from both kernel as from this kernel module.
1240 * precondition: perimeter lock is not acquired.
1242 static void brcms_remove(struct pci_dev *pdev)
1244 struct brcms_info *wl;
1245 struct ieee80211_hw *hw;
1246 int status;
1248 hw = pci_get_drvdata(pdev);
1249 wl = HW_TO_WL(hw);
1250 if (!wl) {
1251 pr_err("wl: brcms_remove: pci_get_drvdata failed\n");
1252 return;
1255 LOCK(wl);
1256 status = wlc_chipmatch(pdev->vendor, pdev->device);
1257 UNLOCK(wl);
1258 if (!status) {
1259 wiphy_err(wl->wiphy, "wl: brcms_remove: wlc_chipmatch "
1260 "failed\n");
1261 return;
1263 if (wl->wlc) {
1264 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
1265 wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
1266 ieee80211_unregister_hw(hw);
1267 LOCK(wl);
1268 brcms_down(wl);
1269 UNLOCK(wl);
1271 pci_disable_device(pdev);
1273 brcms_free(wl);
1275 pci_set_drvdata(pdev, NULL);
1276 ieee80211_free_hw(hw);
1279 static struct pci_driver brcms_pci_driver = {
1280 .name = KBUILD_MODNAME,
1281 .probe = brcms_pci_probe,
1282 .suspend = brcms_suspend,
1283 .resume = brcms_resume,
1284 .remove = __devexit_p(brcms_remove),
1285 .id_table = brcms_pci_id_table,
1289 * This is the main entry point for the WL driver.
1291 * This function determines if a device pointed to by pdev is a WL device,
1292 * and if so, performs a brcms_attach() on it.
1295 static int __init brcms_module_init(void)
1297 int error = -ENODEV;
1299 #ifdef BCMDBG
1300 if (msglevel != 0xdeadbeef)
1301 brcm_msg_level = msglevel;
1302 else {
1303 char *var = getvar(NULL, "wl_msglevel");
1304 if (var) {
1305 unsigned long value;
1307 (void)strict_strtoul(var, 0, &value);
1308 brcm_msg_level = value;
1311 if (phymsglevel != 0xdeadbeef)
1312 phyhal_msg_level = phymsglevel;
1313 else {
1314 char *var = getvar(NULL, "phy_msglevel");
1315 if (var) {
1316 unsigned long value;
1318 (void)strict_strtoul(var, 0, &value);
1319 phyhal_msg_level = value;
1322 #endif /* BCMDBG */
1324 error = pci_register_driver(&brcms_pci_driver);
1325 if (!error)
1326 return 0;
1330 return error;
1334 * This function unloads the WL driver from the system.
1336 * This function unconditionally unloads the WL driver module from the
1337 * system.
1340 static void __exit brcms_module_exit(void)
1342 pci_unregister_driver(&brcms_pci_driver);
1346 module_init(brcms_module_init);
1347 module_exit(brcms_module_exit);
1350 * This function frees the WL per-device resources.
1352 * This function frees resources owned by the WL device pointed to
1353 * by the wl parameter.
1355 * precondition: can both be called locked and unlocked
1358 static void brcms_free(struct brcms_info *wl)
1360 struct brcms_timer *t, *next;
1362 /* free ucode data */
1363 if (wl->fw.fw_cnt)
1364 brcms_ucode_data_free();
1365 if (wl->irq)
1366 free_irq(wl->irq, wl);
1368 /* kill dpc */
1369 tasklet_kill(&wl->tasklet);
1371 if (wl->pub) {
1372 wlc_module_unregister(wl->pub, "linux", wl);
1375 /* free common resources */
1376 if (wl->wlc) {
1377 wlc_detach(wl->wlc);
1378 wl->wlc = NULL;
1379 wl->pub = NULL;
1382 /* virtual interface deletion is deferred so we cannot spinwait */
1384 /* wait for all pending callbacks to complete */
1385 while (atomic_read(&wl->callbacks) > 0)
1386 schedule();
1388 /* free timers */
1389 for (t = wl->timers; t; t = next) {
1390 next = t->next;
1391 #ifdef BCMDBG
1392 kfree(t->name);
1393 #endif
1394 kfree(t);
1398 * unregister_netdev() calls get_stats() which may read chip registers
1399 * so we cannot unmap the chip registers until after calling unregister_netdev() .
1401 if (wl->regsva && wl->bcm_bustype != SDIO_BUS &&
1402 wl->bcm_bustype != JTAG_BUS) {
1403 iounmap((void *)wl->regsva);
1405 wl->regsva = NULL;
1408 /* flags the given rate in rateset as requested */
1409 static void brcms_set_basic_rate(struct wl_rateset *rs, u16 rate, bool is_br)
1411 u32 i;
1413 for (i = 0; i < rs->count; i++) {
1414 if (rate != (rs->rates[i] & 0x7f))
1415 continue;
1417 if (is_br)
1418 rs->rates[i] |= WLC_RATE_FLAG;
1419 else
1420 rs->rates[i] &= WLC_RATE_MASK;
1421 return;
1426 * precondition: perimeter lock has been acquired
1428 void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
1429 bool state, int prio)
1431 wiphy_err(wl->wiphy, "Shouldn't be here %s\n", __func__);
1435 * precondition: perimeter lock has been acquired
1437 void brcms_init(struct brcms_info *wl)
1439 BCMMSG(WL_TO_HW(wl)->wiphy, "wl%d\n", wl->pub->unit);
1440 brcms_reset(wl);
1442 wlc_init(wl->wlc);
1446 * precondition: perimeter lock has been acquired
1448 uint brcms_reset(struct brcms_info *wl)
1450 BCMMSG(WL_TO_HW(wl)->wiphy, "wl%d\n", wl->pub->unit);
1451 wlc_reset(wl->wlc);
1453 /* dpc will not be rescheduled */
1454 wl->resched = 0;
1456 return 0;
1460 * These are interrupt on/off entry points. Disable interrupts
1461 * during interrupt state transition.
1463 void brcms_intrson(struct brcms_info *wl)
1465 unsigned long flags;
1467 INT_LOCK(wl, flags);
1468 wlc_intrson(wl->wlc);
1469 INT_UNLOCK(wl, flags);
1473 * precondition: perimeter lock has been acquired
1475 bool wl_alloc_dma_resources(struct brcms_info *wl, uint addrwidth)
1477 return true;
1480 u32 brcms_intrsoff(struct brcms_info *wl)
1482 unsigned long flags;
1483 u32 status;
1485 INT_LOCK(wl, flags);
1486 status = wlc_intrsoff(wl->wlc);
1487 INT_UNLOCK(wl, flags);
1488 return status;
1491 void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
1493 unsigned long flags;
1495 INT_LOCK(wl, flags);
1496 wlc_intrsrestore(wl->wlc, macintmask);
1497 INT_UNLOCK(wl, flags);
1501 * precondition: perimeter lock has been acquired
1503 int brcms_up(struct brcms_info *wl)
1505 int error = 0;
1507 if (wl->pub->up)
1508 return 0;
1510 error = wlc_up(wl->wlc);
1512 return error;
1516 * precondition: perimeter lock has been acquired
1518 void brcms_down(struct brcms_info *wl)
1520 uint callbacks, ret_val = 0;
1522 /* call common down function */
1523 ret_val = wlc_down(wl->wlc);
1524 callbacks = atomic_read(&wl->callbacks) - ret_val;
1526 /* wait for down callbacks to complete */
1527 UNLOCK(wl);
1529 /* For HIGH_only driver, it's important to actually schedule other work,
1530 * not just spin wait since everything runs at schedule level
1532 SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1534 LOCK(wl);
1537 static irqreturn_t brcms_isr(int irq, void *dev_id)
1539 struct brcms_info *wl;
1540 bool ours, wantdpc;
1541 unsigned long flags;
1543 wl = (struct brcms_info *) dev_id;
1545 ISR_LOCK(wl, flags);
1547 /* call common first level interrupt handler */
1548 ours = wlc_isr(wl->wlc, &wantdpc);
1549 if (ours) {
1550 /* if more to do... */
1551 if (wantdpc) {
1553 /* ...and call the second level interrupt handler */
1554 /* schedule dpc */
1555 tasklet_schedule(&wl->tasklet);
1559 ISR_UNLOCK(wl, flags);
1561 return IRQ_RETVAL(ours);
1564 static void brcms_dpc(unsigned long data)
1566 struct brcms_info *wl;
1568 wl = (struct brcms_info *) data;
1570 LOCK(wl);
1572 /* call the common second level interrupt handler */
1573 if (wl->pub->up) {
1574 if (wl->resched) {
1575 unsigned long flags;
1577 INT_LOCK(wl, flags);
1578 wlc_intrsupd(wl->wlc);
1579 INT_UNLOCK(wl, flags);
1582 wl->resched = wlc_dpc(wl->wlc, true);
1585 /* wlc_dpc() may bring the driver down */
1586 if (!wl->pub->up)
1587 goto done;
1589 /* re-schedule dpc */
1590 if (wl->resched)
1591 tasklet_schedule(&wl->tasklet);
1592 else {
1593 /* re-enable interrupts */
1594 brcms_intrson(wl);
1597 done:
1598 UNLOCK(wl);
1602 * is called by the kernel from software irq context
1604 static void brcms_timer(unsigned long data)
1606 _brcms_timer((struct brcms_timer *) data);
1610 * precondition: perimeter lock is not acquired
1612 static void _brcms_timer(struct brcms_timer *t)
1614 LOCK(t->wl);
1616 if (t->set) {
1617 if (t->periodic) {
1618 t->timer.expires = jiffies + t->ms * HZ / 1000;
1619 atomic_inc(&t->wl->callbacks);
1620 add_timer(&t->timer);
1621 t->set = true;
1622 } else
1623 t->set = false;
1625 t->fn(t->arg);
1628 atomic_dec(&t->wl->callbacks);
1630 UNLOCK(t->wl);
1634 * Adds a timer to the list. Caller supplies a timer function.
1635 * Is called from wlc.
1637 * precondition: perimeter lock has been acquired
1639 struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
1640 void (*fn) (void *arg),
1641 void *arg, const char *name)
1643 struct brcms_timer *t;
1645 t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
1646 if (!t) {
1647 wiphy_err(wl->wiphy, "wl%d: brcms_init_timer: out of memory\n",
1648 wl->pub->unit);
1649 return 0;
1652 init_timer(&t->timer);
1653 t->timer.data = (unsigned long) t;
1654 t->timer.function = brcms_timer;
1655 t->wl = wl;
1656 t->fn = fn;
1657 t->arg = arg;
1658 t->next = wl->timers;
1659 wl->timers = t;
1661 #ifdef BCMDBG
1662 t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
1663 if (t->name)
1664 strcpy(t->name, name);
1665 #endif
1667 return t;
1670 /* BMAC_NOTE: Add timer adds only the kernel timer since it's going to be more accurate
1671 * as well as it's easier to make it periodic
1673 * precondition: perimeter lock has been acquired
1675 void brcms_add_timer(struct brcms_info *wl, struct brcms_timer *t, uint ms,
1676 int periodic)
1678 #ifdef BCMDBG
1679 if (t->set) {
1680 wiphy_err(wl->wiphy, "%s: Already set. Name: %s, per %d\n",
1681 __func__, t->name, periodic);
1683 #endif
1684 t->ms = ms;
1685 t->periodic = (bool) periodic;
1686 t->set = true;
1687 t->timer.expires = jiffies + ms * HZ / 1000;
1689 atomic_inc(&wl->callbacks);
1690 add_timer(&t->timer);
1694 * return true if timer successfully deleted, false if still pending
1696 * precondition: perimeter lock has been acquired
1698 bool brcms_del_timer(struct brcms_info *wl, struct brcms_timer *t)
1700 if (t->set) {
1701 t->set = false;
1702 if (!del_timer(&t->timer)) {
1703 return false;
1705 atomic_dec(&wl->callbacks);
1708 return true;
1712 * precondition: perimeter lock has been acquired
1714 void brcms_free_timer(struct brcms_info *wl, struct brcms_timer *t)
1716 struct brcms_timer *tmp;
1718 /* delete the timer in case it is active */
1719 brcms_del_timer(wl, t);
1721 if (wl->timers == t) {
1722 wl->timers = wl->timers->next;
1723 #ifdef BCMDBG
1724 kfree(t->name);
1725 #endif
1726 kfree(t);
1727 return;
1731 tmp = wl->timers;
1732 while (tmp) {
1733 if (tmp->next == t) {
1734 tmp->next = t->next;
1735 #ifdef BCMDBG
1736 kfree(t->name);
1737 #endif
1738 kfree(t);
1739 return;
1741 tmp = tmp->next;
1747 * runs in software irq context
1749 * precondition: perimeter lock is not acquired
1751 static int wl_linux_watchdog(void *ctx)
1753 return 0;
1756 struct firmware_hdr {
1757 u32 offset;
1758 u32 len;
1759 u32 idx;
1762 char *brcms_firmwares[MAX_FW_IMAGES] = {
1763 "brcm/bcm43xx",
1764 NULL
1768 * precondition: perimeter lock has been acquired
1770 int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
1772 int i, entry;
1773 const u8 *pdata;
1774 struct firmware_hdr *hdr;
1775 for (i = 0; i < wl->fw.fw_cnt; i++) {
1776 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1777 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1778 entry++, hdr++) {
1779 if (hdr->idx == idx) {
1780 pdata = wl->fw.fw_bin[i]->data + hdr->offset;
1781 *pbuf = kmalloc(hdr->len, GFP_ATOMIC);
1782 if (*pbuf == NULL) {
1783 wiphy_err(wl->wiphy, "fail to alloc %d"
1784 " bytes\n", hdr->len);
1785 goto fail;
1787 memcpy(*pbuf, pdata, hdr->len);
1788 return 0;
1792 wiphy_err(wl->wiphy, "ERROR: ucode buf tag:%d can not be found!\n",
1793 idx);
1794 *pbuf = NULL;
1795 fail:
1796 return -ENODATA;
1800 * Precondition: Since this function is called in brcms_pci_probe() context,
1801 * no locking is required.
1803 int brcms_ucode_init_uint(struct brcms_info *wl, u32 *data, u32 idx)
1805 int i, entry;
1806 const u8 *pdata;
1807 struct firmware_hdr *hdr;
1808 for (i = 0; i < wl->fw.fw_cnt; i++) {
1809 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1810 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1811 entry++, hdr++) {
1812 if (hdr->idx == idx) {
1813 pdata = wl->fw.fw_bin[i]->data + hdr->offset;
1814 if (hdr->len != 4) {
1815 wiphy_err(wl->wiphy,
1816 "ERROR: fw hdr len\n");
1817 return -ENOMSG;
1819 *data = *((u32 *) pdata);
1820 return 0;
1824 wiphy_err(wl->wiphy, "ERROR: ucode tag:%d can not be found!\n", idx);
1825 return -ENOMSG;
1829 * Precondition: Since this function is called in brcms_pci_probe() context,
1830 * no locking is required.
1832 static int brcms_request_fw(struct brcms_info *wl, struct pci_dev *pdev)
1834 int status;
1835 struct device *device = &pdev->dev;
1836 char fw_name[100];
1837 int i;
1839 memset((void *)&wl->fw, 0, sizeof(struct brcms_firmware));
1840 for (i = 0; i < MAX_FW_IMAGES; i++) {
1841 if (brcms_firmwares[i] == NULL)
1842 break;
1843 sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
1844 UCODE_LOADER_API_VER);
1845 status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
1846 if (status) {
1847 wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
1848 KBUILD_MODNAME, fw_name);
1849 return status;
1851 sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
1852 UCODE_LOADER_API_VER);
1853 status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
1854 if (status) {
1855 wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
1856 KBUILD_MODNAME, fw_name);
1857 return status;
1859 wl->fw.hdr_num_entries[i] =
1860 wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
1862 wl->fw.fw_cnt = i;
1863 return brcms_ucode_data_init(wl);
1867 * precondition: can both be called locked and unlocked
1869 void brcms_ucode_free_buf(void *p)
1871 kfree(p);
1875 * Precondition: Since this function is called in brcms_pci_probe() context,
1876 * no locking is required.
1878 static void brcms_release_fw(struct brcms_info *wl)
1880 int i;
1881 for (i = 0; i < MAX_FW_IMAGES; i++) {
1882 release_firmware(wl->fw.fw_bin[i]);
1883 release_firmware(wl->fw.fw_hdr[i]);
1889 * checks validity of all firmware images loaded from user space
1891 * Precondition: Since this function is called in brcms_pci_probe() context,
1892 * no locking is required.
1894 int brcms_check_firmwares(struct brcms_info *wl)
1896 int i;
1897 int entry;
1898 int rc = 0;
1899 const struct firmware *fw;
1900 const struct firmware *fw_hdr;
1901 struct firmware_hdr *ucode_hdr;
1902 for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
1903 fw = wl->fw.fw_bin[i];
1904 fw_hdr = wl->fw.fw_hdr[i];
1905 if (fw == NULL && fw_hdr == NULL) {
1906 break;
1907 } else if (fw == NULL || fw_hdr == NULL) {
1908 wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
1909 __func__);
1910 rc = -EBADF;
1911 } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
1912 wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
1913 "size %zu/%zu\n", __func__, fw_hdr->size,
1914 sizeof(struct firmware_hdr));
1915 rc = -EBADF;
1916 } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
1917 wiphy_err(wl->wiphy, "%s: out of bounds fw file size "
1918 "%zu\n", __func__, fw->size);
1919 rc = -EBADF;
1920 } else {
1921 /* check if ucode section overruns firmware image */
1922 ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
1923 for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
1924 !rc; entry++, ucode_hdr++) {
1925 if (ucode_hdr->offset + ucode_hdr->len >
1926 fw->size) {
1927 wiphy_err(wl->wiphy,
1928 "%s: conflicting bin/hdr\n",
1929 __func__);
1930 rc = -EBADF;
1935 if (rc == 0 && wl->fw.fw_cnt != i) {
1936 wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
1937 wl->fw.fw_cnt);
1938 rc = -EBADF;
1940 return rc;
1944 * precondition: perimeter lock has been acquired
1946 bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
1948 bool blocked = wlc_check_radio_disabled(wl->wlc);
1950 UNLOCK(wl);
1951 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
1952 if (blocked)
1953 wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
1954 LOCK(wl);
1955 return blocked;
1959 * precondition: perimeter lock has been acquired
1961 void brcms_msleep(struct brcms_info *wl, uint ms)
1963 UNLOCK(wl);
1964 msleep(ms);
1965 LOCK(wl);