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