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