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