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