Staging: Add initial release of brcm80211 - Broadcom 802.11n wireless LAN driver.
[cascardo/linux.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 <typedefs.h>
20 #include <linuxver.h>
21 #include <osl.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/delay.h>
32 #include <linux/string.h>
33 #include <linux/ethtool.h>
34 #include <linux/completion.h>
35 #include <linux/pci_ids.h>
36 #define WLC_MAXBSSCFG           1       /* single BSS configs */
37
38 #include <asm/system.h>
39 #include <asm/io.h>
40 #include <asm/irq.h>
41 #include <asm/pgtable.h>
42 #include <asm/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <wlc_cfg.h>
45 #include <net/mac80211.h>
46 #include <epivers.h>
47 #ifndef WLC_HIGH_ONLY
48 #include <phy_version.h>
49 #endif
50 #include <bcmendian.h>
51 #include <bcmutils.h>
52 #include <pcicfg.h>
53 #include <wlioctl.h>
54 #include <wlc_key.h>
55 #include <proto/802.1d.h>       /* NUMPRIO & BE */
56 #include <linux/proc_fs.h>
57 #include <linux/vmalloc.h>
58 #include <wlc_channel.h>
59 #include <wlc_pub.h>
60 #include <wlc_scb.h>
61 #include <wl_dbg.h>
62 #ifdef BCMSDIO
63 #include <bcmsdh.h>
64 #endif
65 #include <wl_export.h>
66 #ifdef WLC_HIGH_ONLY
67 #include "dbus.h"
68 #include "bcm_rpc_tp.h"
69 #include "bcm_rpc.h"
70 #include "bcm_xdr.h"
71 #include "wlc_rpc.h"
72 #endif
73
74 #include <wl_mac80211.h>
75 #include <linux/firmware.h>
76 #ifndef WLC_HIGH_ONLY
77 #include <wl_ucode.h>
78 #include <d11ucode_ext.h>
79 #endif
80
81 #ifdef BCMSDIO
82 extern struct device *sdiommc_dev;
83 #endif
84
85 extern void wlc_wme_setparams(wlc_info_t * wlc, u16 aci, void *arg,
86                               bool suspend);
87 bool wlc_sendpkt_mac80211(wlc_info_t * wlc, void *sdu, struct ieee80211_hw *hw);
88 void wlc_mac_bcn_promisc_change(wlc_info_t * wlc, bool promisc);
89 void wlc_set_addrmatch(wlc_info_t * wlc, int match_reg_offset,
90                        const struct ether_addr *addr);
91
92 static void wl_timer(ulong data);
93 static void _wl_timer(wl_timer_t * t);
94
95 /* proc fs */
96 static int wl_proc_read(char *buffer, char **start, off_t offset, int length,
97                         int *eof, void *data);
98 static int wl_proc_write(struct file *filp, const char __user * buff,
99                          unsigned long len, void *data);
100
101 #ifdef WLC_HIGH_ONLY
102 #define RPCQ_LOCK(_wl, _flags) spin_lock_irqsave(&(_wl)->rpcq_lock, (_flags))
103 #define RPCQ_UNLOCK(_wl, _flags)  spin_unlock_irqrestore(&(_wl)->rpcq_lock, (_flags))
104 #define TXQ_LOCK(_wl, _flags) spin_lock_irqsave(&(_wl)->txq_lock, (_flags))
105 #define TXQ_UNLOCK(_wl, _flags)  spin_unlock_irqrestore(&(_wl)->txq_lock, (_flags))
106 static void wl_rpc_down(void *wlh);
107 static void wl_rpcq_free(wl_info_t * wl);
108 static void wl_rpcq_dispatch(struct wl_task *task);
109 static void wl_rpc_dispatch_schedule(void *ctx, struct rpc_buf *buf);
110 static void wl_start_txqwork(struct wl_task *task);
111 static void wl_txq_free(wl_info_t * wl);
112 static void wl_timer_task(wl_task_t * task);
113 static int wl_schedule_task(wl_info_t * wl, void (*fn) (struct wl_task *),
114                             void *context);
115 #endif                          /* WLC_HIGH_ONLY */
116
117 static int ieee_hw_init(struct ieee80211_hw *hw);
118 static int ieee_hw_rate_init(struct ieee80211_hw *hw);
119
120 static int wl_linux_watchdog(void *ctx);
121
122 /* Flags we support */
123 #define MAC_FILTERS (FIF_PROMISC_IN_BSS | \
124         FIF_ALLMULTI | \
125         FIF_FCSFAIL | \
126         FIF_PLCPFAIL | \
127         FIF_CONTROL | \
128         FIF_OTHER_BSS | \
129         FIF_BCN_PRBRESP_PROMISC)
130
131 static int wl_found = 0;
132
133 struct ieee80211_tkip_data {
134 #define TKIP_KEY_LEN 32
135         u8 key[TKIP_KEY_LEN];
136         int key_set;
137
138         u32 tx_iv32;
139         u16 tx_iv16;
140         u16 tx_ttak[5];
141         int tx_phase1_done;
142
143         u32 rx_iv32;
144         u16 rx_iv16;
145         u16 rx_ttak[5];
146         int rx_phase1_done;
147         u32 rx_iv32_new;
148         u16 rx_iv16_new;
149
150         u32 dot11RSNAStatsTKIPReplays;
151         u32 dot11RSNAStatsTKIPICVErrors;
152         u32 dot11RSNAStatsTKIPLocalMICFailures;
153
154         int key_idx;
155
156         struct crypto_tfm *tfm_arc4;
157         struct crypto_tfm *tfm_michael;
158
159         /* scratch buffers for virt_to_page() (crypto API) */
160         u8 rx_hdr[16], tx_hdr[16];
161 };
162
163 #ifndef WLC_HIGH_ONLY
164 #define WL_INFO(dev)            ((wl_info_t*)(WL_DEV_IF(dev)->wl))      /* points to wl */
165 static int wl_request_fw(wl_info_t * wl, struct pci_dev *pdev);
166 static void wl_release_fw(wl_info_t * wl);
167 #endif
168
169 /* local prototypes */
170 static int wl_start(struct sk_buff *skb, wl_info_t * wl);
171 static int wl_start_int(wl_info_t * wl, struct ieee80211_hw *hw,
172                         struct sk_buff *skb);
173 static void wl_dpc(ulong data);
174
175 MODULE_AUTHOR("Broadcom Corporation");
176 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
177 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
178 MODULE_LICENSE("Dual BSD/GPL");
179
180 #ifndef BCMSDIO
181 /* recognized PCI IDs */
182 static struct pci_device_id wl_id_table[] = {
183         {PCI_VENDOR_ID_BROADCOM, 0x4357, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},      /* 43225 2G */
184         {PCI_VENDOR_ID_BROADCOM, 0x4353, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},      /* 43224 DUAL */
185         {PCI_VENDOR_ID_BROADCOM, 0x4727, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},      /* 4313 DUAL */
186         {0}
187 };
188
189 MODULE_DEVICE_TABLE(pci, wl_id_table);
190 #endif                          /* !BCMSDIO */
191
192 #ifdef BCMSDIO
193 static uint sd_drivestrength = 6;
194 module_param(sd_drivestrength, uint, 0);
195 #endif
196
197 #ifdef BCMDBG
198 static int msglevel = 0xdeadbeef;
199 module_param(msglevel, int, 0);
200 #ifndef WLC_HIGH_ONLY
201 static int phymsglevel = 0xdeadbeef;
202 module_param(phymsglevel, int, 0);
203 #endif                          /* WLC_HIGH_ONLY */
204 #endif                          /* BCMDBG */
205
206 static int oneonly = 0;
207 module_param(oneonly, int, 0);
208
209 static int piomode = 0;
210 module_param(piomode, int, 0);
211
212 static int instance_base = 0;   /* Starting instance number */
213 module_param(instance_base, int, 0);
214
215 #if defined(BCMDBG)
216 static char *macaddr = NULL;
217 module_param(macaddr, charp, S_IRUGO);
218 #endif
219
220 static int nompc = 1;
221 module_param(nompc, int, 0);
222
223 static char name[IFNAMSIZ] = "eth%d";
224 module_param_string(name, name, IFNAMSIZ, 0);
225
226 #ifndef SRCBASE
227 #define SRCBASE "."
228 #endif
229
230 #define WL_MAGIC        0xdeadbeef
231
232 #define HW_TO_WL(hw)     hw->priv
233 #define WL_TO_HW(wl)      wl->pub->ieee_hw
234 #ifdef WLC_HIGH_ONLY
235 static int wl_ops_tx_nl(struct ieee80211_hw *hw, struct sk_buff *skb);
236 #else
237 static int wl_ops_tx(struct ieee80211_hw *hw, struct sk_buff *skb);
238 #endif
239 static int wl_ops_start(struct ieee80211_hw *hw);
240 static void wl_ops_stop(struct ieee80211_hw *hw);
241 static int wl_ops_add_interface(struct ieee80211_hw *hw,
242                                 struct ieee80211_vif *vif);
243 static void wl_ops_remove_interface(struct ieee80211_hw *hw,
244                                     struct ieee80211_vif *vif);
245 static int wl_ops_config(struct ieee80211_hw *hw, u32 changed);
246 static void wl_ops_bss_info_changed(struct ieee80211_hw *hw,
247                                     struct ieee80211_vif *vif,
248                                     struct ieee80211_bss_conf *info,
249                                     u32 changed);
250 static void wl_ops_configure_filter(struct ieee80211_hw *hw,
251                                     unsigned int changed_flags,
252                                     unsigned int *total_flags, u64 multicast);
253 static int wl_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
254                           bool set);
255 static void wl_ops_sw_scan_start(struct ieee80211_hw *hw);
256 static void wl_ops_sw_scan_complete(struct ieee80211_hw *hw);
257 static void wl_ops_set_tsf(struct ieee80211_hw *hw, u64 tsf);
258 static int wl_ops_get_stats(struct ieee80211_hw *hw,
259                             struct ieee80211_low_level_stats *stats);
260 static int wl_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value);
261 static void wl_ops_sta_notify(struct ieee80211_hw *hw,
262                               struct ieee80211_vif *vif,
263                               enum sta_notify_cmd cmd,
264                               struct ieee80211_sta *sta);
265 static int wl_ops_conf_tx(struct ieee80211_hw *hw, u16 queue,
266                           const struct ieee80211_tx_queue_params *params);
267 static u64 wl_ops_get_tsf(struct ieee80211_hw *hw);
268 static int wl_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
269                       struct ieee80211_sta *sta);
270 static int wl_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
271                          struct ieee80211_sta *sta);
272 static int wl_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
273                            enum ieee80211_ampdu_mlme_action action,
274                            struct ieee80211_sta *sta, u16 tid, u16 * ssn);
275
276 #ifdef WLC_HIGH_ONLY
277 static int wl_ops_tx_nl(struct ieee80211_hw *hw, struct sk_buff *skb)
278 {
279         int status;
280         wl_info_t *wl = hw->priv;
281         if (!wl->pub->up) {
282                 WL_ERROR(("ops->tx called while down\n"));
283                 status = -ENETDOWN;
284                 goto done;
285         }
286         status = wl_start(skb, wl);
287  done:
288         return status;
289 }
290 #else
291 static int wl_ops_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
292 {
293         int status;
294         wl_info_t *wl = hw->priv;
295         WL_LOCK(wl);
296         if (!wl->pub->up) {
297                 WL_ERROR(("ops->tx called while down\n"));
298                 status = -ENETDOWN;
299                 goto done;
300         }
301         status = wl_start(skb, wl);
302  done:
303         WL_UNLOCK(wl);
304         return status;
305 }
306 #endif                          /* WLC_HIGH_ONLY */
307
308 static int wl_ops_start(struct ieee80211_hw *hw)
309 {
310         wl_info_t *wl = hw->priv;
311         /* struct ieee80211_channel *curchan = hw->conf.channel; */
312         WL_NONE(("%s : Initial channel: %d\n", __func__, curchan->hw_value));
313
314         WL_LOCK(wl);
315         ieee80211_wake_queues(hw);
316         WL_UNLOCK(wl);
317
318         return 0;
319 }
320
321 static void wl_ops_stop(struct ieee80211_hw *hw)
322 {
323         wl_info_t *wl = hw->priv;
324         ASSERT(wl);
325         WL_LOCK(wl);
326         wl_down(wl);
327         ieee80211_stop_queues(hw);
328         WL_UNLOCK(wl);
329
330         return;
331 }
332
333 static int
334 wl_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
335 {
336         wl_info_t *wl;
337         int err;
338
339         /* Just STA for now */
340         if (vif->type != NL80211_IFTYPE_AP &&
341             vif->type != NL80211_IFTYPE_MESH_POINT &&
342             vif->type != NL80211_IFTYPE_STATION &&
343             vif->type != NL80211_IFTYPE_WDS &&
344             vif->type != NL80211_IFTYPE_ADHOC) {
345                 WL_ERROR(("%s: Attempt to add type %d, only STA for now\n",
346                           __func__, vif->type));
347                 return -EOPNOTSUPP;
348         }
349
350         wl = HW_TO_WL(hw);
351         WL_LOCK(wl);
352         err = wl_up(wl);
353         WL_UNLOCK(wl);
354
355         if (err != 0)
356                 WL_ERROR(("%s: wl_up() returned %d\n", __func__, err));
357         return err;
358 }
359
360 static void
361 wl_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
362 {
363         return;
364 }
365
366 static int
367 ieee_set_channel(struct ieee80211_hw *hw, struct ieee80211_channel *chan,
368                  enum nl80211_channel_type type)
369 {
370         wl_info_t *wl = HW_TO_WL(hw);
371         int err = 0;
372
373         switch (type) {
374         case NL80211_CHAN_HT20:
375         case NL80211_CHAN_NO_HT:
376                 WL_LOCK(wl);
377                 err = wlc_set(wl->wlc, WLC_SET_CHANNEL, chan->hw_value);
378                 WL_UNLOCK(wl);
379                 break;
380         case NL80211_CHAN_HT40MINUS:
381         case NL80211_CHAN_HT40PLUS:
382                 WL_ERROR(("%s: Need to implement 40 Mhz Channels!\n",
383                           __func__));
384                 break;
385         }
386
387         if (err)
388                 return -EIO;
389         return err;
390 }
391
392 static int wl_ops_config(struct ieee80211_hw *hw, u32 changed)
393 {
394         struct ieee80211_conf *conf = &hw->conf;
395         wl_info_t *wl = HW_TO_WL(hw);
396         int err = 0;
397         int new_int;
398
399         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
400                 WL_NONE(("%s: Setting listen interval to %d\n",
401                          __func__, conf->listen_interval));
402                 if (wlc_iovar_setint
403                     (wl->wlc, "bcn_li_bcn", conf->listen_interval)) {
404                         WL_ERROR(("%s: Error setting listen_interval\n",
405                                   __func__));
406                         err = -EIO;
407                         goto config_out;
408                 }
409                 wlc_iovar_getint(wl->wlc, "bcn_li_bcn", &new_int);
410                 ASSERT(new_int == conf->listen_interval);
411         }
412         if (changed & IEEE80211_CONF_CHANGE_MONITOR)
413                 WL_NONE(("Need to set monitor mode\n"));
414         if (changed & IEEE80211_CONF_CHANGE_PS)
415                 WL_NONE(("Need to set Power-save mode\n"));
416
417         if (changed & IEEE80211_CONF_CHANGE_POWER) {
418                 WL_NONE(("%s: Setting tx power to %d dbm\n", __func__,
419                          conf->power_level));
420                 if (wlc_iovar_setint
421                     (wl->wlc, "qtxpower", conf->power_level * 4)) {
422                         WL_ERROR(("%s: Error setting power_level\n", __func__));
423                         err = -EIO;
424                         goto config_out;
425                 }
426                 wlc_iovar_getint(wl->wlc, "qtxpower", &new_int);
427                 if (new_int != (conf->power_level * 4))
428                         WL_ERROR(("%s: Power level req != actual, %d %d\n",
429                                   __func__, conf->power_level * 4, new_int));
430         }
431         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
432                 err = ieee_set_channel(hw, conf->channel, conf->channel_type);
433         }
434         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
435                 WL_NONE(("%s: srl %d, lrl %d\n", __func__,
436                          conf->short_frame_max_tx_count,
437                          conf->long_frame_max_tx_count));
438                 if (wlc_set
439                     (wl->wlc, WLC_SET_SRL,
440                      conf->short_frame_max_tx_count) < 0) {
441                         WL_ERROR(("%s: Error setting srl\n", __func__));
442                         err = -EIO;
443                         goto config_out;
444                 }
445                 if (wlc_set(wl->wlc, WLC_SET_LRL, conf->long_frame_max_tx_count)
446                     < 0) {
447                         WL_ERROR(("%s: Error setting lrl\n", __func__));
448                         err = -EIO;
449                         goto config_out;
450                 }
451         }
452
453  config_out:
454         return err;
455 }
456
457 static void
458 wl_ops_bss_info_changed(struct ieee80211_hw *hw,
459                         struct ieee80211_vif *vif,
460                         struct ieee80211_bss_conf *info, u32 changed)
461 {
462         wl_info_t *wl = HW_TO_WL(hw);
463         int val;
464
465 #ifdef WLC_HIGH_ONLY
466         WL_LOCK(wl);
467 #endif
468
469         if (changed & BSS_CHANGED_ASSOC) {
470                 WL_ERROR(("Associated:\t%s\n", info->assoc ? "True" : "False"));
471                 /* association status changed (associated/disassociated)
472                  * also implies a change in the AID.
473                  */
474         }
475         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
476                 WL_NONE(("Use_cts_prot:\t%s Implement me\n",
477                          info->use_cts_prot ? "True" : "False"));
478                 /* CTS protection changed */
479         }
480         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
481                 WL_NONE(("Short preamble:\t%s Implement me\n",
482                          info->use_short_preamble ? "True" : "False"));
483                 /* preamble changed */
484         }
485         if (changed & BSS_CHANGED_ERP_SLOT) {
486                 WL_NONE(("Changing short slot:\t%s\n",
487                          info->use_short_slot ? "True" : "False"));
488                 if (info->use_short_slot)
489                         val = 1;
490                 else
491                         val = 0;
492                 wlc_set(wl->wlc, WLC_SET_SHORTSLOT_OVERRIDE, val);
493                 /* slot timing changed */
494         }
495
496         if (changed & BSS_CHANGED_HT) {
497                 WL_NONE(("%s: HT mode - Implement me\n", __func__));
498                 /* 802.11n parameters changed */
499         }
500         if (changed & BSS_CHANGED_BASIC_RATES) {
501                 WL_NONE(("Need to change Basic Rates:\t0x%x! Implement me\n",
502                          (uint32) info->basic_rates));
503                 /* Basic rateset changed */
504         }
505         if (changed & BSS_CHANGED_BEACON_INT) {
506                 WL_NONE(("Beacon Interval:\t%d Implement me\n",
507                          info->beacon_int));
508                 /* Beacon interval changed */
509         }
510         if (changed & BSS_CHANGED_BSSID) {
511                 /* char eabuf[ETHER_ADDR_STR_LEN]; */
512                 WL_NONE(("new BSSID:\taid %d  bss:%s\n",
513                          info->aid,
514                          bcm_ether_ntoa((struct ether_addr *)info->bssid,
515                                         eabuf)));
516                 /* BSSID changed, for whatever reason (IBSS and managed mode) */
517                 /* FIXME: need to store bssid in bsscfg */
518                 wlc_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET,
519                                   (struct ether_addr *)info->bssid);
520         }
521         if (changed & BSS_CHANGED_BEACON) {
522                 WL_ERROR(("BSS_CHANGED_BEACON\n"));
523                 /* Beacon data changed, retrieve new beacon (beaconing modes) */
524         }
525         if (changed & BSS_CHANGED_BEACON_ENABLED) {
526                 WL_ERROR(("Beacon enabled:\t%s\n",
527                           info->enable_beacon ? "True" : "False"));
528                 /* Beaconing should be enabled/disabled (beaconing modes) */
529         }
530 #ifdef WLC_HIGH_ONLY
531         WL_UNLOCK(wl);
532 #endif
533         return;
534 }
535
536 static void
537 wl_ops_configure_filter(struct ieee80211_hw *hw,
538                         unsigned int changed_flags,
539                         unsigned int *total_flags, u64 multicast)
540 {
541 #ifndef WLC_HIGH_ONLY
542         wl_info_t *wl = hw->priv;
543 #endif
544
545         changed_flags &= MAC_FILTERS;
546         *total_flags &= MAC_FILTERS;
547         if (changed_flags & FIF_PROMISC_IN_BSS)
548                 WL_ERROR(("FIF_PROMISC_IN_BSS\n"));
549         if (changed_flags & FIF_ALLMULTI)
550                 WL_ERROR(("FIF_ALLMULTI\n"));
551         if (changed_flags & FIF_FCSFAIL)
552                 WL_ERROR(("FIF_FCSFAIL\n"));
553         if (changed_flags & FIF_PLCPFAIL)
554                 WL_ERROR(("FIF_PLCPFAIL\n"));
555         if (changed_flags & FIF_CONTROL)
556                 WL_ERROR(("FIF_CONTROL\n"));
557         if (changed_flags & FIF_OTHER_BSS)
558                 WL_ERROR(("FIF_OTHER_BSS\n"));
559         if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
560                 WL_NONE(("FIF_BCN_PRBRESP_PROMISC\n"));
561 #ifndef WLC_HIGH_ONLY
562                 WL_LOCK(wl);
563                 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
564                         wl->pub->mac80211_state |= MAC80211_PROMISC_BCNS;
565                         wlc_mac_bcn_promisc_change(wl->wlc, 1);
566                 } else {
567                         wlc_mac_bcn_promisc_change(wl->wlc, 0);
568                         wl->pub->mac80211_state &= ~MAC80211_PROMISC_BCNS;
569                 }
570                 WL_UNLOCK(wl);
571 #endif
572         }
573         return;
574 }
575
576 static int
577 wl_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
578 {
579         WL_ERROR(("%s: Enter\n", __func__));
580         return 0;
581 }
582
583 static void wl_ops_sw_scan_start(struct ieee80211_hw *hw)
584 {
585         WL_NONE(("Scan Start\n"));
586         return;
587 }
588
589 static void wl_ops_sw_scan_complete(struct ieee80211_hw *hw)
590 {
591         WL_NONE(("Scan Complete\n"));
592         return;
593 }
594
595 static void wl_ops_set_tsf(struct ieee80211_hw *hw, u64 tsf)
596 {
597         WL_ERROR(("%s: Enter\n", __func__));
598         return;
599 }
600
601 static int
602 wl_ops_get_stats(struct ieee80211_hw *hw,
603                  struct ieee80211_low_level_stats *stats)
604 {
605         WL_ERROR(("%s: Enter\n", __func__));
606         return 0;
607 }
608
609 static int wl_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
610 {
611         WL_ERROR(("%s: Enter\n", __func__));
612         return 0;
613 }
614
615 static void
616 wl_ops_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
617                   enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
618 {
619         WL_NONE(("%s: Enter\n", __func__));
620         switch (cmd) {
621         default:
622                 WL_ERROR(("%s: Uknown cmd = %d\n", __func__, cmd));
623                 break;
624         }
625         return;
626 }
627
628 static int
629 wl_ops_conf_tx(struct ieee80211_hw *hw, u16 queue,
630                const struct ieee80211_tx_queue_params *params)
631 {
632         wl_info_t *wl = hw->priv;
633
634         WL_NONE(("%s: Enter (WME config)\n", __func__));
635         WL_NONE(("queue %d, txop %d, cwmin %d, cwmax %d, aifs %d\n", queue,
636                  params->txop, params->cw_min, params->cw_max, params->aifs));
637
638         WL_LOCK(wl);
639         wlc_wme_setparams(wl->wlc, queue, (void *)params, TRUE);
640         WL_UNLOCK(wl);
641
642         return 0;
643 }
644
645 static u64 wl_ops_get_tsf(struct ieee80211_hw *hw)
646 {
647         WL_ERROR(("%s: Enter\n", __func__));
648         return 0;
649 }
650
651 static int
652 wl_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
653            struct ieee80211_sta *sta)
654 {
655         struct scb *scb;
656
657         int i;
658         wl_info_t *wl = hw->priv;
659
660         /* Init the scb */
661         scb = (struct scb *)sta->drv_priv;
662         bzero(scb, sizeof(struct scb));
663         for (i = 0; i < NUMPRIO; i++)
664                 scb->seqctl[i] = 0xFFFF;
665         scb->seqctl_nonqos = 0xFFFF;
666         scb->magic = SCB_MAGIC;
667
668         wl->pub->global_scb = scb;
669         wl->pub->global_ampdu = &(scb->scb_ampdu);
670         wl->pub->global_ampdu->scb = scb;
671 #ifdef WLC_HIGH_ONLY
672         wl->pub->global_ampdu->max_pdu = AMPDU_NUM_MPDU;
673 #else
674         wl->pub->global_ampdu->max_pdu = 16;
675 #endif
676         pktq_init(&scb->scb_ampdu.txq, AMPDU_MAX_SCB_TID,
677                   AMPDU_MAX_SCB_TID * PKTQ_LEN_DEFAULT);
678
679         sta->ht_cap.ht_supported = TRUE;
680 #ifdef WLC_HIGH_ONLY
681         sta->ht_cap.ampdu_factor = AMPDU_RX_FACTOR_16K;
682 #else
683         sta->ht_cap.ampdu_factor = AMPDU_RX_FACTOR_64K;
684 #endif
685         sta->ht_cap.ampdu_density = AMPDU_DEF_MPDU_DENSITY;
686         sta->ht_cap.cap = IEEE80211_HT_CAP_GRN_FLD |
687             IEEE80211_HT_CAP_SGI_20 |
688             IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_40MHZ_INTOLERANT;
689
690         /* minstrel_ht initiates addBA on our behalf by calling ieee80211_start_tx_ba_session() */
691         return 0;
692 }
693
694 static int
695 wl_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
696               struct ieee80211_sta *sta)
697 {
698         WL_NONE(("%s: Enter\n", __func__));
699         return 0;
700 }
701
702 static int
703 wl_ampdu_action(struct ieee80211_hw *hw,
704                 struct ieee80211_vif *vif,
705                 enum ieee80211_ampdu_mlme_action action,
706                 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
707 {
708 #if defined(BCMDBG)
709         struct scb *scb = (struct scb *)sta->drv_priv;
710 #endif
711         wl_info_t *wl = hw->priv;
712
713         ASSERT(scb->magic == SCB_MAGIC);
714         switch (action) {
715         case IEEE80211_AMPDU_RX_START:
716                 WL_NONE(("%s: action = IEEE80211_AMPDU_RX_START\n", __func__));
717                 break;
718         case IEEE80211_AMPDU_RX_STOP:
719                 WL_NONE(("%s: action = IEEE80211_AMPDU_RX_STOP\n", __func__));
720                 break;
721         case IEEE80211_AMPDU_TX_START:
722                 if (!wlc_aggregatable(wl->wlc, tid)) {
723                         /* WL_ERROR(("START: tid %d is not agg' able, return FAILURE to stack\n", tid)); */
724                         return -1;
725                 }
726                 /* XXX: Use the starting sequence number provided ... */
727                 *ssn = 0;
728                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
729                 break;
730
731         case IEEE80211_AMPDU_TX_STOP:
732                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
733                 break;
734         case IEEE80211_AMPDU_TX_OPERATIONAL:
735                 /* Not sure what to do here */
736                 /* Power save wakeup */
737                 WL_NONE(("%s: action = IEEE80211_AMPDU_TX_OPERATIONAL\n",
738                          __func__));
739                 break;
740         default:
741                 WL_ERROR(("%s: Invalid command, ignoring\n", __func__));
742         }
743
744         return 0;
745 }
746
747 static const struct ieee80211_ops wl_ops = {
748 #ifdef WLC_HIGH_ONLY
749         .tx = wl_ops_tx_nl,
750 #else
751         .tx = wl_ops_tx,
752 #endif
753         .start = wl_ops_start,
754         .stop = wl_ops_stop,
755         .add_interface = wl_ops_add_interface,
756         .remove_interface = wl_ops_remove_interface,
757         .config = wl_ops_config,
758         .bss_info_changed = wl_ops_bss_info_changed,
759         .configure_filter = wl_ops_configure_filter,
760         .set_tim = wl_ops_set_tim,
761         .sw_scan_start = wl_ops_sw_scan_start,
762         .sw_scan_complete = wl_ops_sw_scan_complete,
763         .set_tsf = wl_ops_set_tsf,
764         .get_stats = wl_ops_get_stats,
765         .set_rts_threshold = wl_ops_set_rts_threshold,
766         .sta_notify = wl_ops_sta_notify,
767         .conf_tx = wl_ops_conf_tx,
768         .get_tsf = wl_ops_get_tsf,
769         .sta_add = wl_sta_add,
770         .sta_remove = wl_sta_remove,
771         .ampdu_action = wl_ampdu_action,
772 };
773
774 static int wl_set_hint(wl_info_t * wl, char *abbrev)
775 {
776         WL_ERROR(("%s: Sending country code %c%c to MAC80211\n", __func__,
777                   abbrev[0], abbrev[1]));
778         return (regulatory_hint(wl->pub->ieee_hw->wiphy, abbrev));
779 }
780
781 /**
782  * attach to the WL device.
783  *
784  * Attach to the WL device identified by vendor and device parameters.
785  * regs is a host accessible memory address pointing to WL device registers.
786  *
787  * wl_attach is not defined as static because in the case where no bus
788  * is defined, wl_attach will never be called, and thus, gcc will issue
789  * a warning that this function is defined but not used if we declare
790  * it as static.
791  */
792 static wl_info_t *wl_attach(uint16 vendor, uint16 device, ulong regs,
793                             uint bustype, void *btparam, uint irq)
794 {
795         wl_info_t *wl;
796         osl_t *osh;
797         int unit, err;
798
799         ulong base_addr;
800         struct ieee80211_hw *hw;
801         uint8 perm[ETH_ALEN];
802
803         unit = wl_found + instance_base;
804         err = 0;
805
806         if (unit < 0) {
807                 WL_ERROR(("wl%d: unit number overflow, exiting\n", unit));
808                 return NULL;
809         }
810
811         if (oneonly && (unit != instance_base)) {
812                 WL_ERROR(("wl%d: wl_attach: oneonly is set, exiting\n", unit));
813                 return NULL;
814         }
815
816         /* Requires pkttag feature */
817         osh = osl_attach(btparam, bustype, TRUE);
818         ASSERT(osh);
819
820 #ifdef WLC_HIGH_ONLY
821         hw = ieee80211_alloc_hw(sizeof(wl_info_t), &wl_ops);
822         if (!hw) {
823                 WL_ERROR(("%s: ieee80211_alloc_hw failed\n", __func__));
824                 ASSERT(0);
825         }
826
827         bzero(hw->priv, sizeof(*wl));
828         wl = hw->priv;
829 #else
830         /* allocate private info */
831         hw = pci_get_drvdata(btparam);  /* btparam == pdev */
832         wl = hw->priv;
833 #endif
834         ASSERT(wl);
835
836         wl->magic = WL_MAGIC;
837         wl->osh = osh;
838         atomic_set(&wl->callbacks, 0);
839
840 #ifdef WLC_HIGH_ONLY
841         wl->rpc_th = bcm_rpc_tp_attach(osh, NULL);
842         if (wl->rpc_th == NULL) {
843                 WL_ERROR(("wl%d: %s: bcm_rpc_tp_attach failed!\n", unit,
844                           __func__));
845                 goto fail;
846         }
847
848         wl->rpc = bcm_rpc_attach(NULL, osh, wl->rpc_th);
849         if (wl->rpc == NULL) {
850                 WL_ERROR(("wl%d: %s: bcm_rpc_attach failed!\n", unit,
851                           __func__));
852                 goto fail;
853         }
854
855         /* init tx work queue for wl_start/send pkt; no need to destroy workitem  */
856         MY_INIT_WORK(&wl->txq_task.work, (work_func_t) wl_start_txqwork);
857         wl->txq_task.context = wl;
858 #endif                          /* WLC_HIGH_ONLY */
859
860 #ifdef BCMSDIO
861         SET_IEEE80211_DEV(hw, sdiommc_dev);
862 #endif
863
864         base_addr = regs;
865
866         if (bustype == PCI_BUS) {
867                 /* piomode can be overwritten by command argument */
868                 wl->piomode = piomode;
869                 WL_TRACE(("PCI/%s\n", wl->piomode ? "PIO" : "DMA"));
870         } else if (bustype == RPC_BUS) {
871                 /* Do nothing */
872         } else {
873                 bustype = PCI_BUS;
874                 WL_TRACE(("force to PCI\n"));
875         }
876         wl->bcm_bustype = bustype;
877
878 #ifdef WLC_HIGH_ONLY
879         if (wl->bcm_bustype == RPC_BUS) {
880                 wl->regsva = (void *)0;
881                 btparam = wl->rpc;
882         } else
883 #endif
884         if ((wl->regsva = ioremap_nocache(base_addr, PCI_BAR0_WINSZ)) == NULL) {
885                 WL_ERROR(("wl%d: ioremap() failed\n", unit));
886                 goto fail;
887         }
888 #ifdef WLC_HIGH_ONLY
889         spin_lock_init(&wl->rpcq_lock);
890         spin_lock_init(&wl->txq_lock);
891
892         init_MUTEX(&wl->sem);
893 #else
894         spin_lock_init(&wl->lock);
895         spin_lock_init(&wl->isr_lock);
896 #endif
897
898 #ifndef WLC_HIGH_ONLY
899         /* prepare ucode */
900         if (wl_request_fw(wl, (struct pci_dev *)btparam)) {
901                 printf("%s: %s driver failed\n", KBUILD_MODNAME,
902                        EPI_VERSION_STR);
903                 goto fail;
904         }
905 #endif
906
907         /* common load-time initialization */
908         if (!
909             (wl->wlc =
910              wlc_attach((void *)wl, vendor, device, unit, wl->piomode, osh,
911                         wl->regsva, wl->bcm_bustype, btparam, &err))) {
912                 printf("%s: %s driver failed with code %d\n", KBUILD_MODNAME,
913                        EPI_VERSION_STR, err);
914                 goto fail;
915         }
916 #ifndef WLC_HIGH_ONLY
917         wl_release_fw(wl);
918 #endif
919         wl->pub = wlc_pub(wl->wlc);
920
921         wl->pub->ieee_hw = hw;
922         ASSERT(wl->pub->ieee_hw);
923         ASSERT(wl->pub->ieee_hw->priv == wl);
924
925 #ifdef WLC_HIGH_ONLY
926         REGOPSSET(osh, (osl_rreg_fn_t) wlc_reg_read,
927                   (osl_wreg_fn_t) wlc_reg_write, wl->wlc);
928         wl->rpc_dispatch_ctx.rpc = wl->rpc;
929         wl->rpc_dispatch_ctx.wlc = wl->wlc;
930         bcm_rpc_rxcb_init(wl->rpc, wl, wl_rpc_dispatch_schedule, wl,
931                           wl_rpc_down, NULL, NULL);
932 #endif                          /* WLC_HIGH_ONLY */
933
934         if (nompc) {
935                 if (wlc_iovar_setint(wl->wlc, "mpc", 0)) {
936                         WL_ERROR(("wl%d: Error setting MPC variable to 0\n",
937                                   unit));
938                 }
939         }
940 #ifdef BCMSDIO
941         /* Set SDIO drive strength */
942         wlc_iovar_setint(wl->wlc, "sd_drivestrength", sd_drivestrength);
943 #endif
944
945         /* setup the bottom half handler */
946         tasklet_init(&wl->tasklet, wl_dpc, (ulong) wl);
947
948 #ifdef WLC_LOW
949         /* register our interrupt handler */
950         if (request_irq(irq, wl_isr, IRQF_SHARED, KBUILD_MODNAME, wl)) {
951                 WL_ERROR(("wl%d: request_irq() failed\n", unit));
952                 goto fail;
953         }
954         wl->irq = irq;
955 #endif                          /* WLC_LOW */
956
957         /* register module */
958         wlc_module_register(wl->pub, NULL, "linux", wl, NULL, wl_linux_watchdog,
959                             NULL);
960
961         if (ieee_hw_init(hw)) {
962                 WL_ERROR(("wl%d: %s: ieee_hw_init failed!\n", unit, __func__));
963                 goto fail;
964         }
965
966         bcopy(&wl->pub->cur_etheraddr, perm, ETHER_ADDR_LEN);
967         ASSERT(is_valid_ether_addr(perm));
968         SET_IEEE80211_PERM_ADDR(hw, perm);
969
970         err = ieee80211_register_hw(hw);
971         if (err) {
972                 WL_ERROR(("%s: ieee80211_register_hw failed, status %d\n",
973                           __func__, err));
974         }
975
976         if (wl->pub->srom_ccode[0])
977                 err = wl_set_hint(wl, wl->pub->srom_ccode);
978         else
979                 err = wl_set_hint(wl, "US");
980         if (err) {
981                 WL_ERROR(("%s: regulatory_hint failed, status %d\n", __func__,
982                           err));
983         }
984 #ifndef WLC_HIGH_ONLY
985         WL_ERROR(("wl%d: Broadcom BCM43xx 802.11 MAC80211 Driver "
986                   EPI_VERSION_STR " (" PHY_VERSION_STR ")", unit));
987 #else
988         WL_ERROR(("wl%d: Broadcom BCM43xx 802.11 MAC80211 Driver "
989                   EPI_VERSION_STR, unit));
990 #endif
991
992 #ifdef BCMDBG
993         printf(" (Compiled in " SRCBASE " at " __TIME__ " on " __DATE__ ")");
994 #endif                          /* BCMDBG */
995         printf("\n");
996
997         if ((wl->proc_entry =
998              create_proc_entry(PROC_ENTRY_NAME, 0644, NULL)) == NULL) {
999                 WL_ERROR(("create_proc_entry failed *******\n"));
1000                 ASSERT(0);
1001         } else {
1002                 wl->proc_entry->read_proc = wl_proc_read;
1003                 wl->proc_entry->write_proc = wl_proc_write;
1004                 wl->proc_entry->data = wl;
1005                 /* wl->proc_entry->owner = THIS_MODULE; */
1006
1007                 if ((wl->ioctlbuf = (char *)vmalloc(PAGE_SIZE)) == NULL) {
1008                         WL_ERROR(("%s: Vmalloc failed\n", __func__));
1009                 }
1010                 wl->ioctlbuf_sz = PAGE_SIZE;
1011                 memset(wl->ioctlbuf, 0, PAGE_SIZE);
1012                 wl->ioctlbuf[0] = '!';
1013         }
1014
1015         wl_found++;
1016         return wl;
1017
1018  fail:
1019         wl_free(wl);
1020         return NULL;
1021 }
1022
1023 #define PROC_MESSAGE  "Broadcom driver debugger access only.  Requires matching 'wl' app\n"
1024
1025 /* OS Entry point when app attempts to read */
1026 static int
1027 wl_proc_read(char *buffer, char **start, off_t offset, int length, int *eof,
1028              void *data)
1029 {
1030         wl_info_t *wl = (wl_info_t *) data;
1031
1032         switch (wl->proc_state) {
1033
1034         case WL_PROC_IDLE:
1035                 return 0;
1036
1037         case WL_PROC_HAVE_IOC:
1038                 /* Give the processed buffer back to userland */
1039                 if (!wl->ioctl_in_progress) {
1040                         WL_ERROR(("%s: No ioctl in progress nothing to read, 2\n", __func__));
1041                         return 0;
1042                 }
1043
1044                 if (wl->ioc.len > wl->ioctlbuf_sz) {
1045                 }
1046                 bcopy(wl->ioctlbuf, buffer + offset, wl->ioc.len);
1047                 wl->proc_state--;
1048                 wl->ioctl_in_progress = 0;
1049                 return wl->ioc.len + offset;
1050
1051         case WL_PROC_HAVE_BUF:
1052                 /* Give the processed IOC back to userland */
1053                 if (!wl->ioctl_in_progress) {
1054                         WL_ERROR(("%s: No ioctl in progress nothing to read, 1\n", __func__));
1055                         return 0;
1056                 }
1057                 if (length != sizeof(wl_ioctl_t)) {
1058                         WL_ERROR(("%s: Reading ioc but len != sizeof(wl_ioctl_t)\n", __func__));
1059                         return 0;
1060                 }
1061                 bcopy(&wl->ioc, buffer + offset, length);
1062                 wl->proc_state--;
1063                 return length + offset;
1064
1065         default:
1066                 WL_ERROR(("%s: Proc read out of sync. proc_state %d, ioctl_in_progress %d\n", __func__, wl->proc_state, wl->ioctl_in_progress));
1067         }
1068
1069         WL_ERROR(("%s: Invalid ioctl!!!\n", __func__));
1070         return 0;
1071 }
1072
1073 /* OS Entry point when app attempts to write */
1074 static int
1075 wl_proc_write(struct file *filp, const char __user * buff, unsigned long length,
1076               void *data)
1077 {
1078         wl_info_t *wl = (wl_info_t *) data;
1079         int bcmerror;
1080
1081         switch (wl->proc_state) {
1082
1083         case WL_PROC_IDLE:
1084                 if (wl->ioctl_in_progress) {
1085                         WL_ERROR(("%s: ioctl still in progress\n", __func__));
1086                         return -EIO;
1087                 }
1088                 if (length != sizeof(wl_ioctl_t)) {
1089                         WL_ERROR(("%s: Expecting ioctl sized buf\n", __func__));
1090                         return -EIO;
1091                 }
1092                 if (copy_from_user(&wl->ioc, buff, sizeof(wl_ioctl_t))) {
1093                         WL_ERROR(("%s: copy from user failed\n", __func__));
1094                         return -EIO;
1095                 }
1096                 wl->proc_state++;
1097                 wl->ioctl_in_progress++;
1098                 return sizeof(wl_ioctl_t);
1099
1100         case WL_PROC_HAVE_IOC:
1101                 if (!wl->ioctl_in_progress) {
1102                         WL_ERROR(("%s: Ioctl not ready yet 1\n", __func__));
1103                         return -EIO;
1104                 }
1105                 if (wl->ioctlbuf_sz < length) {
1106                         WL_ERROR(("%s: Buf write, ioctl buf %d not big enough too hold buffer %d\n", __func__, (int)sizeof(wl->ioctlbuf), (int)length));
1107                         WL_ERROR(("Shortening input\n"));
1108                         length = wl->ioctlbuf_sz;
1109                 }
1110                 if (length != wl->ioc.len) {
1111                         WL_ERROR(("%s: ioc.len %d != length param %d\n",
1112                                   __func__, wl->ioc.len, (int)length));
1113                         return -EIO;
1114                 }
1115                 if (copy_from_user(wl->ioctlbuf, buff, length)) {
1116                         WL_ERROR(("%s: copy from user of %d bytes failed\n",
1117                                   __func__, (int)length));
1118                         return -EIO;
1119                 }
1120                 wl->proc_state++;
1121
1122                 WL_LOCK(wl);
1123                 bcmerror =
1124                     wlc_ioctl(wl->wlc, wl->ioc.cmd, wl->ioctlbuf, wl->ioc.len,
1125                               NULL);
1126                 WL_UNLOCK(wl);
1127
1128                 if (bcmerror < 0)
1129                         return bcmerror;
1130
1131                 return length;
1132
1133         case WL_PROC_HAVE_BUF:
1134                 WL_ERROR(("%s: Illegal write.  Rejecting.\n", __func__));
1135                 return 0;
1136         default:
1137                 WL_ERROR(("%s: Proc write out of sync. proc_state %d, ioctl_in_progress %d\n", __func__, wl->proc_state, wl->ioctl_in_progress));
1138         }
1139         return 0;
1140 }
1141
1142 #ifdef WLC_HIGH_ONLY
1143 static void *wl_dbus_probe_cb(void *arg, const char *desc, uint32 bustype,
1144                               uint32 hdrlen)
1145 {
1146         wl_info_t *wl;
1147         WL_ERROR(("%s:\n", __func__));
1148
1149         if (!
1150             (wl =
1151              wl_attach(BCM_DNGL_VID, BCM_DNGL_BDC_PID, (ulong) NULL, RPC_BUS,
1152                        NULL, 0))) {
1153                 WL_ERROR(("%s: wl_attach failed\n", __func__));
1154         }
1155
1156         /* This is later passed to wl_dbus_disconnect_cb */
1157         return wl;
1158 }
1159
1160 static void wl_dbus_disconnect_cb(void *arg)
1161 {
1162         wl_info_t *wl = arg;
1163
1164         WL_ERROR(("%s:\n", __func__));
1165
1166         if (wl) {
1167 #ifdef WLC_HIGH_ONLY
1168                 if (wl->pub->ieee_hw) {
1169                         ieee80211_unregister_hw(wl->pub->ieee_hw);
1170                         WL_ERROR(("%s: Back from down\n", __func__));
1171                 }
1172                 wlc_device_removed(wl->wlc);
1173                 wlc_bmac_dngl_reboot(wl->rpc);
1174                 bcm_rpc_down(wl->rpc);
1175 #endif
1176                 WL_LOCK(wl);
1177                 wl_down(wl);
1178                 WL_UNLOCK(wl);
1179 #ifdef WLC_HIGH_ONLY
1180                 if (wl->pub->ieee_hw) {
1181                         ieee80211_free_hw(wl->pub->ieee_hw);
1182                         WL_ERROR(("%s: Back from ieee80211_free_hw\n",
1183                                   __func__));
1184                         wl->pub->ieee_hw = NULL;
1185                 }
1186 #endif
1187                 wl_free(wl);
1188         }
1189 }
1190 #endif                          /* WLC_HIGH_ONLY */
1191
1192 #ifndef BCMSDIO
1193 static void __devexit wl_remove(struct pci_dev *pdev);
1194 #endif
1195
1196 #define CHAN2GHZ(channel, freqency, chflags)  { \
1197         .band = IEEE80211_BAND_2GHZ, \
1198         .center_freq = (freqency), \
1199         .hw_value = (channel), \
1200         .flags = chflags, \
1201         .max_antenna_gain = 0, \
1202         .max_power = 19, \
1203 }
1204
1205 static struct ieee80211_channel wl_2ghz_chantable[] = {
1206         CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
1207         CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
1208         CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
1209         CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
1210         CHAN2GHZ(5, 2432, 0),
1211         CHAN2GHZ(6, 2437, 0),
1212         CHAN2GHZ(7, 2442, 0),
1213         CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
1214         CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
1215         CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
1216         CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
1217         CHAN2GHZ(12, 2467,
1218                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
1219                  IEEE80211_CHAN_NO_HT40PLUS),
1220         CHAN2GHZ(13, 2472,
1221                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
1222                  IEEE80211_CHAN_NO_HT40PLUS),
1223         CHAN2GHZ(14, 2484,
1224                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
1225                  IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
1226 };
1227
1228 #define CHAN5GHZ(channel, chflags)  { \
1229         .band = IEEE80211_BAND_5GHZ, \
1230         .center_freq = 5000 + 5*(channel), \
1231         .hw_value = (channel), \
1232         .flags = chflags, \
1233         .max_antenna_gain = 0, \
1234         .max_power = 21, \
1235 }
1236
1237 static struct ieee80211_channel wl_5ghz_nphy_chantable[] = {
1238         /* UNII-1 */
1239         CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
1240         CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
1241         CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
1242         CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
1243         /* UNII-2 */
1244         CHAN5GHZ(52,
1245                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1246                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1247         CHAN5GHZ(56,
1248                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1249                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1250         CHAN5GHZ(60,
1251                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1252                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1253         CHAN5GHZ(64,
1254                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1255                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1256         /* MID */
1257         CHAN5GHZ(100,
1258                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1259                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1260         CHAN5GHZ(104,
1261                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1262                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1263         CHAN5GHZ(108,
1264                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1265                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1266         CHAN5GHZ(112,
1267                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1268                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1269         CHAN5GHZ(116,
1270                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1271                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1272         CHAN5GHZ(120,
1273                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1274                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1275         CHAN5GHZ(124,
1276                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1277                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1278         CHAN5GHZ(128,
1279                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1280                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1281         CHAN5GHZ(132,
1282                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1283                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
1284         CHAN5GHZ(136,
1285                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1286                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
1287         CHAN5GHZ(140,
1288                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
1289                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS |
1290                  IEEE80211_CHAN_NO_HT40MINUS),
1291         /* UNII-3 */
1292         CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
1293         CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
1294         CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
1295         CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
1296         CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
1297 };
1298
1299 #define RATE(rate100m, _flags) { \
1300         .bitrate = (rate100m), \
1301         .flags = (_flags), \
1302         .hw_value = (rate100m / 5), \
1303 }
1304
1305 static struct ieee80211_rate wl_legacy_ratetable[] = {
1306         RATE(10, 0),
1307         RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
1308         RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
1309         RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
1310         RATE(60, 0),
1311         RATE(90, 0),
1312         RATE(120, 0),
1313         RATE(180, 0),
1314         RATE(240, 0),
1315         RATE(360, 0),
1316         RATE(480, 0),
1317         RATE(540, 0),
1318 };
1319
1320 static struct ieee80211_supported_band wl_band_2GHz_nphy = {
1321         .band = IEEE80211_BAND_2GHZ,
1322         .channels = wl_2ghz_chantable,
1323         .n_channels = ARRAY_SIZE(wl_2ghz_chantable),
1324         .bitrates = wl_legacy_ratetable,
1325         .n_bitrates = ARRAY_SIZE(wl_legacy_ratetable),
1326         .ht_cap = {
1327                    /* from include/linux/ieee80211.h */
1328                    .cap = IEEE80211_HT_CAP_GRN_FLD |
1329                    IEEE80211_HT_CAP_SGI_20 |
1330                    IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_40MHZ_INTOLERANT,
1331 #ifdef WLC_HIGH_ONLY
1332                    .ht_supported = true,
1333                    .ampdu_factor = AMPDU_RX_FACTOR_16K,
1334 #else
1335                    .ht_supported = true,
1336                    .ampdu_factor = AMPDU_RX_FACTOR_64K,
1337 #endif
1338                    .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
1339                    .mcs = {
1340                            /* placeholders for now */
1341 #ifdef WLC_HIGH_ONLY
1342                            /*
1343                             * rx_mask[0] = 0xff by default
1344                             * rx_mask[1] = 0xff if number of rx chain >=2
1345                             * rx_mask[2] = 0xff if number of rx chain >=3
1346                             * rx_mask[4] = 1 if 40Mhz is supported
1347                             */
1348                            .rx_mask = {0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1349                            .rx_highest = 72,    /* max rate of single stream */
1350 #else
1351                            .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
1352                            .rx_highest = 500,
1353 #endif
1354                            .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
1355                    }
1356 };
1357
1358 static struct ieee80211_supported_band wl_band_5GHz_nphy = {
1359         .band = IEEE80211_BAND_5GHZ,
1360         .channels = wl_5ghz_nphy_chantable,
1361         .n_channels = ARRAY_SIZE(wl_5ghz_nphy_chantable),
1362         .bitrates = wl_legacy_ratetable + 4,
1363         .n_bitrates = ARRAY_SIZE(wl_legacy_ratetable) - 4,
1364         .ht_cap = {
1365                    /* use IEEE80211_HT_CAP_* from include/linux/ieee80211.h */
1366                    .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 */
1367                    .ht_supported = true,
1368                    .ampdu_factor = AMPDU_RX_FACTOR_64K,
1369                    .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
1370                    .mcs = {
1371                            /* placeholders for now */
1372                            .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
1373                            .rx_highest = 500,
1374                            .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
1375                    }
1376 };
1377
1378 static int ieee_hw_rate_init(struct ieee80211_hw *hw)
1379 {
1380         wl_info_t *wl = HW_TO_WL(hw);
1381         int has_5g;
1382         char phy_list[4];
1383
1384         has_5g = 0;
1385
1386         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
1387         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
1388
1389         if (wlc_get(wl->wlc, WLC_GET_PHYLIST, (int *)&phy_list) < 0) {
1390                 WL_ERROR(("Phy list failed\n"));
1391         }
1392         WL_NONE(("%s: phylist = %c\n", __func__, phy_list[0]));
1393
1394 #ifndef WLC_HIGH_ONLY
1395         if (phy_list[0] == 'n' || phy_list[0] == 'c') {
1396                 if (phy_list[0] == 'c') {
1397                         /* Single stream */
1398                         wl_band_2GHz_nphy.ht_cap.mcs.rx_mask[1] = 0;
1399                         wl_band_2GHz_nphy.ht_cap.mcs.rx_highest = 72;
1400                 }
1401 #else
1402         if (phy_list[0] == 's') {
1403 #endif
1404                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl_band_2GHz_nphy;
1405         } else {
1406                 BUG();
1407                 return (-1);
1408         }
1409
1410         /* Assume all bands use the same phy.  True for 11n devices. */
1411         if (NBANDS_PUB(wl->pub) > 1) {
1412                 has_5g++;
1413 #ifndef WLC_HIGH_ONLY
1414                 if (phy_list[0] == 'n' || phy_list[0] == 'c') {
1415 #else
1416                 if (phy_list[0] == 's') {
1417 #endif
1418                         hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1419                             &wl_band_5GHz_nphy;
1420                 } else {
1421                         return (-1);
1422                 }
1423         }
1424
1425         WL_NONE(("%s: 2ghz = %d, 5ghz = %d\n", __func__, 1, has_5g));
1426
1427         return (0);
1428 }
1429
1430 static int ieee_hw_init(struct ieee80211_hw *hw)
1431 {
1432         hw->flags = IEEE80211_HW_SIGNAL_DBM
1433             /* | IEEE80211_HW_CONNECTION_MONITOR  What is this? */
1434             | IEEE80211_HW_REPORTS_TX_ACK_STATUS
1435             | IEEE80211_HW_AMPDU_AGGREGATION;
1436
1437         hw->extra_tx_headroom = wlc_get_header_len();
1438         /* FIXME: should get this from wlc->machwcap */
1439         hw->queues = 4;
1440         /* FIXME: this doesn't seem to be used properly in minstrel_ht.
1441          * mac80211/status.c:ieee80211_tx_status() checks this value,
1442          * but mac80211/rc80211_minstrel_ht.c:minstrel_ht_get_rate()
1443          * appears to always set 3 rates
1444          */
1445         hw->max_rates = 2;      /* Primary rate and 1 fallback rate */
1446
1447         hw->channel_change_time = 7 * 1000;     /* channel change time is dependant on chip and band  */
1448         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1449
1450         hw->rate_control_algorithm = "minstrel_ht";
1451
1452         hw->sta_data_size = sizeof(struct scb);
1453         return (ieee_hw_rate_init(hw));
1454 }
1455
1456 #ifndef BCMSDIO
1457 /**
1458  * determines if a device is a WL device, and if so, attaches it.
1459  *
1460  * This function determines if a device pointed to by pdev is a WL device,
1461  * and if so, performs a wl_attach() on it.
1462  *
1463  */
1464 int __devinit
1465 wl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1466 {
1467         int rc;
1468         wl_info_t *wl;
1469         struct ieee80211_hw *hw;
1470         uint32 val;
1471
1472         ASSERT(pdev);
1473
1474         WL_TRACE(("%s: bus %d slot %d func %d irq %d\n", __func__,
1475                   pdev->bus->number, PCI_SLOT(pdev->devfn),
1476                   PCI_FUNC(pdev->devfn), pdev->irq));
1477
1478         if ((pdev->vendor != PCI_VENDOR_ID_BROADCOM) ||
1479             (((pdev->device & 0xff00) != 0x4300) &&
1480              ((pdev->device & 0xff00) != 0x4700) &&
1481              ((pdev->device < 43000) || (pdev->device > 43999))))
1482                 return (-ENODEV);
1483
1484         rc = pci_enable_device(pdev);
1485         if (rc) {
1486                 WL_ERROR(("%s: Cannot enable device %d-%d_%d\n", __func__,
1487                           pdev->bus->number, PCI_SLOT(pdev->devfn),
1488                           PCI_FUNC(pdev->devfn)));
1489                 return (-ENODEV);
1490         }
1491         pci_set_master(pdev);
1492
1493         pci_read_config_dword(pdev, 0x40, &val);
1494         if ((val & 0x0000ff00) != 0)
1495                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1496
1497         hw = ieee80211_alloc_hw(sizeof(wl_info_t), &wl_ops);
1498         if (!hw) {
1499                 WL_ERROR(("%s: ieee80211_alloc_hw failed\n", __func__));
1500                 rc = -ENOMEM;
1501                 goto err_1;
1502         }
1503
1504         SET_IEEE80211_DEV(hw, &pdev->dev);
1505
1506         pci_set_drvdata(pdev, hw);
1507
1508         bzero(hw->priv, sizeof(*wl));
1509
1510         wl = wl_attach(pdev->vendor, pdev->device, pci_resource_start(pdev, 0),
1511                        PCI_BUS, pdev, pdev->irq);
1512
1513         return 0;
1514  err_1:
1515         WL_ERROR(("%s: err_1: Major hoarkage\n", __func__));
1516         return 0;
1517 }
1518
1519 #ifdef LINUXSTA_PS
1520 static int wl_suspend(struct pci_dev *pdev, DRV_SUSPEND_STATE_TYPE state)
1521 {
1522         wl_info_t *wl;
1523         struct ieee80211_hw *hw;
1524
1525         WL_TRACE(("wl: wl_suspend\n"));
1526
1527         hw = pci_get_drvdata(pdev);
1528         wl = HW_TO_WL(hw);
1529         if (!wl) {
1530                 WL_ERROR(("wl: wl_suspend: pci_get_drvdata failed\n"));
1531                 return -ENODEV;
1532         }
1533
1534         WL_LOCK(wl);
1535         wl_down(wl);
1536         wl->pub->hw_up = FALSE;
1537         WL_UNLOCK(wl);
1538         PCI_SAVE_STATE(pdev, wl->pci_psstate);
1539         pci_disable_device(pdev);
1540         return pci_set_power_state(pdev, PCI_D3hot);
1541 }
1542
1543 static int wl_resume(struct pci_dev *pdev)
1544 {
1545         wl_info_t *wl;
1546         struct ieee80211_hw *hw;
1547         int err = 0;
1548         uint32 val;
1549
1550         WL_TRACE(("wl: wl_resume\n"));
1551         hw = pci_get_drvdata(pdev);
1552         wl = HW_TO_WL(hw);
1553         if (!wl) {
1554                 WL_ERROR(("wl: wl_resume: pci_get_drvdata failed\n"));
1555                 return -ENODEV;
1556         }
1557
1558         err = pci_set_power_state(pdev, PCI_D0);
1559         if (err)
1560                 return err;
1561
1562         PCI_RESTORE_STATE(pdev, wl->pci_psstate);
1563
1564         err = pci_enable_device(pdev);
1565         if (err)
1566                 return err;
1567
1568         pci_set_master(pdev);
1569
1570         pci_read_config_dword(pdev, 0x40, &val);
1571         if ((val & 0x0000ff00) != 0)
1572                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1573
1574         WL_LOCK(wl);
1575         err = wl_up(wl);
1576         WL_UNLOCK(wl);
1577
1578         return (err);
1579 }
1580 #endif                          /* LINUXSTA_PS */
1581
1582 static void __devexit wl_remove(struct pci_dev *pdev)
1583 {
1584         wl_info_t *wl;
1585         struct ieee80211_hw *hw;
1586
1587         hw = pci_get_drvdata(pdev);
1588         wl = HW_TO_WL(hw);
1589         if (!wl) {
1590                 WL_ERROR(("wl: wl_remove: pci_get_drvdata failed\n"));
1591                 return;
1592         }
1593         if (!wlc_chipmatch(pdev->vendor, pdev->device)) {
1594                 WL_ERROR(("wl: wl_remove: wlc_chipmatch failed\n"));
1595                 return;
1596         }
1597
1598         ieee80211_unregister_hw(hw);
1599
1600         WL_LOCK(wl);
1601         wl_down(wl);
1602         WL_UNLOCK(wl);
1603         WL_NONE(("%s: Down\n", __func__));
1604
1605         pci_disable_device(pdev);
1606
1607         wl_free(wl);
1608
1609         pci_set_drvdata(pdev, NULL);
1610         ieee80211_free_hw(hw);
1611 }
1612
1613 static struct pci_driver wl_pci_driver = {
1614  name:  "brcm80211",
1615  probe:wl_pci_probe,
1616 #ifdef LINUXSTA_PS
1617  suspend:wl_suspend,
1618  resume:wl_resume,
1619 #endif                          /* LINUXSTA_PS */
1620  remove:__devexit_p(wl_remove),
1621  id_table:wl_id_table,
1622 };
1623 #endif                          /* !BCMSDIO */
1624
1625 /**
1626  * This is the main entry point for the WL driver.
1627  *
1628  * This function determines if a device pointed to by pdev is a WL device,
1629  * and if so, performs a wl_attach() on it.
1630  *
1631  */
1632 static int __init wl_module_init(void)
1633 {
1634         int error = -ENODEV;
1635
1636 #ifdef BCMDBG
1637         if (msglevel != 0xdeadbeef)
1638                 wl_msg_level = msglevel;
1639         else {
1640                 char *var = getvar(NULL, "wl_msglevel");
1641                 if (var)
1642                         wl_msg_level = bcm_strtoul(var, NULL, 0);
1643         }
1644 #ifndef WLC_HIGH_ONLY
1645         {
1646                 extern uint32 phyhal_msg_level;
1647
1648                 if (phymsglevel != 0xdeadbeef)
1649                         phyhal_msg_level = phymsglevel;
1650                 else {
1651                         char *var = getvar(NULL, "phy_msglevel");
1652                         if (var)
1653                                 phyhal_msg_level = bcm_strtoul(var, NULL, 0);
1654                 }
1655         }
1656 #endif                          /* WLC_HIGH_ONLY */
1657 #endif                          /* BCMDBG */
1658
1659 #ifndef BCMSDIO
1660         if (!(error = pci_module_init(&wl_pci_driver)))
1661                 return (0);
1662
1663 #endif                          /* !BCMSDIO */
1664
1665 #ifdef WLC_HIGH_ONLY
1666         /* BMAC_NOTE: define hardcode number, why NODEVICE is ok ? */
1667         error =
1668             dbus_register(BCM_DNGL_VID, 0, wl_dbus_probe_cb,
1669                           wl_dbus_disconnect_cb, NULL, NULL, NULL);
1670         if (error == DBUS_ERR_NODEVICE) {
1671                 error = DBUS_OK;
1672         }
1673 #endif                          /* WLC_HIGH_ONLY */
1674
1675         return (error);
1676 }
1677
1678 /**
1679  * This function unloads the WL driver from the system.
1680  *
1681  * This function unconditionally unloads the WL driver module from the
1682  * system.
1683  *
1684  */
1685 static void __exit wl_module_exit(void)
1686 {
1687 #ifndef BCMSDIO
1688         pci_unregister_driver(&wl_pci_driver);
1689 #endif                          /* !BCMSDIO */
1690
1691 #ifdef WLC_HIGH_ONLY
1692         dbus_deregister();
1693 #endif                          /* WLC_HIGH_ONLY */
1694 }
1695
1696 module_init(wl_module_init);
1697 module_exit(wl_module_exit);
1698
1699 /**
1700  * This function frees the WL per-device resources.
1701  *
1702  * This function frees resources owned by the WL device pointed to
1703  * by the wl parameter.
1704  *
1705  */
1706 void wl_free(wl_info_t * wl)
1707 {
1708         wl_timer_t *t, *next;
1709         osl_t *osh;
1710
1711         ASSERT(wl);
1712 #ifndef WLC_HIGH_ONLY
1713         ASSERT(wl->irq);        /* bmac does not use direct interrupt */
1714         /* free ucode data */
1715         if (wl->fw.fw_cnt)
1716                 wl_ucode_data_free();
1717         ASSERT(wl->wlc);
1718         if (wl->irq)
1719                 free_irq(wl->irq, wl);
1720 #endif
1721
1722         /* kill dpc */
1723         tasklet_kill(&wl->tasklet);
1724
1725         if (wl->pub) {
1726                 wlc_module_unregister(wl->pub, "linux", wl);
1727         }
1728
1729         /* free common resources */
1730         if (wl->wlc) {
1731                 wlc_detach(wl->wlc);
1732                 wl->wlc = NULL;
1733                 wl->pub = NULL;
1734         }
1735
1736         /* virtual interface deletion is deferred so we cannot spinwait */
1737
1738         /* wait for all pending callbacks to complete */
1739         while (atomic_read(&wl->callbacks) > 0)
1740                 schedule();
1741
1742         /* free timers */
1743         for (t = wl->timers; t; t = next) {
1744                 next = t->next;
1745 #ifdef BCMDBG
1746                 if (t->name)
1747                         MFREE(wl->osh, t->name, strlen(t->name) + 1);
1748 #endif
1749                 MFREE(wl->osh, t, sizeof(wl_timer_t));
1750         }
1751
1752         if (wl->ioctlbuf_sz) {
1753                 remove_proc_entry(PROC_ENTRY_NAME, NULL);
1754                 vfree(wl->ioctlbuf);
1755                 wl->ioctlbuf_sz = 0;
1756         }
1757
1758         osh = wl->osh;
1759
1760         /*
1761          * unregister_netdev() calls get_stats() which may read chip registers
1762          * so we cannot unmap the chip registers until after calling unregister_netdev() .
1763          */
1764         if (wl->regsva && BUSTYPE(wl->bcm_bustype) != SDIO_BUS &&
1765             BUSTYPE(wl->bcm_bustype) != JTAG_BUS) {
1766                 iounmap((void *)wl->regsva);
1767         }
1768         wl->regsva = NULL;
1769
1770 #ifdef WLC_HIGH_ONLY
1771         wl_rpcq_free(wl);
1772
1773         wl_txq_free(wl);
1774
1775         if (wl->rpc) {
1776                 bcm_rpc_detach(wl->rpc);
1777                 wl->rpc = NULL;
1778         }
1779
1780         if (wl->rpc_th) {
1781                 bcm_rpc_tp_detach(wl->rpc_th);
1782                 wl->rpc_th = NULL;
1783         }
1784 #endif                          /* WLC_HIGH_ONLY */
1785
1786         if (osl_malloced(osh)) {
1787                 printf("****   Memory leak of bytes %d\n", osl_malloced(osh));
1788                 ASSERT(0 && "Memory Leak");
1789         }
1790
1791         osl_detach(osh);
1792 }
1793
1794 #ifdef WLC_LOW
1795 /* transmit a packet */
1796 static int BCMFASTPATH wl_start(struct sk_buff *skb, wl_info_t * wl)
1797 {
1798         if (!wl)
1799                 return -ENETDOWN;
1800
1801         return wl_start_int(wl, WL_TO_HW(wl), skb);
1802 }
1803 #endif                          /* WLC_LOW */
1804
1805 static int BCMFASTPATH
1806 wl_start_int(wl_info_t * wl, struct ieee80211_hw *hw, struct sk_buff *skb)
1807 {
1808 #ifdef WLC_HIGH_ONLY
1809         WL_LOCK(wl);
1810 #endif
1811         wlc_sendpkt_mac80211(wl->wlc, skb, hw);
1812 #ifdef WLC_HIGH_ONLY
1813         WL_UNLOCK(wl);
1814 #endif
1815         return (NETDEV_TX_OK);
1816 }
1817
1818 void wl_txflowcontrol(wl_info_t * wl, struct wl_if *wlif, bool state, int prio)
1819 {
1820         WL_ERROR(("Shouldn't be here %s\n", __func__));
1821 }
1822
1823 #if defined(WLC_HIGH_ONLY)
1824 /* Schedule a completion handler to run at safe time */
1825 static int
1826 wl_schedule_task(wl_info_t * wl, void (*fn) (struct wl_task * task),
1827                  void *context)
1828 {
1829         wl_task_t *task;
1830
1831         WL_TRACE(("wl%d: wl_schedule_task\n", wl->pub->unit));
1832
1833         if (!(task = osl_malloc(wl->osh, sizeof(wl_task_t)))) {
1834                 WL_ERROR(("wl%d: wl_schedule_task: out of memory, malloced %d bytes\n", wl->pub->unit, osl_malloced(wl->osh)));
1835                 return -ENOMEM;
1836         }
1837
1838         MY_INIT_WORK(&task->work, (work_func_t) fn);
1839         task->context = context;
1840
1841         if (!schedule_work(&task->work)) {
1842                 WL_ERROR(("wl%d: schedule_work() failed\n", wl->pub->unit));
1843                 MFREE(wl->osh, task, sizeof(wl_task_t));
1844                 return -ENOMEM;
1845         }
1846
1847         atomic_inc(&wl->callbacks);
1848
1849         return 0;
1850 }
1851 #endif                          /* defined(WLC_HIGH_ONLY) */
1852
1853 void wl_init(wl_info_t * wl)
1854 {
1855         WL_TRACE(("wl%d: wl_init\n", wl->pub->unit));
1856
1857         wl_reset(wl);
1858
1859         wlc_init(wl->wlc);
1860 }
1861
1862 uint wl_reset(wl_info_t * wl)
1863 {
1864         WL_TRACE(("wl%d: wl_reset\n", wl->pub->unit));
1865
1866         wlc_reset(wl->wlc);
1867
1868         /* dpc will not be rescheduled */
1869         wl->resched = 0;
1870
1871         return (0);
1872 }
1873
1874 /*
1875  * These are interrupt on/off entry points. Disable interrupts
1876  * during interrupt state transition.
1877  */
1878 void BCMFASTPATH wl_intrson(wl_info_t * wl)
1879 {
1880 #if defined(WLC_LOW)
1881         unsigned long flags;
1882
1883         INT_LOCK(wl, flags);
1884         wlc_intrson(wl->wlc);
1885         INT_UNLOCK(wl, flags);
1886 #endif                          /* WLC_LOW */
1887 }
1888
1889 bool wl_alloc_dma_resources(wl_info_t * wl, uint addrwidth)
1890 {
1891         return TRUE;
1892 }
1893
1894 uint32 BCMFASTPATH wl_intrsoff(wl_info_t * wl)
1895 {
1896 #if defined(WLC_LOW)
1897         unsigned long flags;
1898         uint32 status;
1899
1900         INT_LOCK(wl, flags);
1901         status = wlc_intrsoff(wl->wlc);
1902         INT_UNLOCK(wl, flags);
1903         return status;
1904 #else
1905         return 0;
1906 #endif                          /* WLC_LOW */
1907 }
1908
1909 void wl_intrsrestore(wl_info_t * wl, uint32 macintmask)
1910 {
1911 #if defined(WLC_LOW)
1912         unsigned long flags;
1913
1914         INT_LOCK(wl, flags);
1915         wlc_intrsrestore(wl->wlc, macintmask);
1916         INT_UNLOCK(wl, flags);
1917 #endif                          /* WLC_LOW */
1918 }
1919
1920 int wl_up(wl_info_t * wl)
1921 {
1922         int error = 0;
1923
1924         if (wl->pub->up)
1925                 return (0);
1926
1927         error = wlc_up(wl->wlc);
1928
1929         return (error);
1930 }
1931
1932 void wl_down(wl_info_t * wl)
1933 {
1934         uint callbacks, ret_val = 0;
1935
1936         /* call common down function */
1937         ret_val = wlc_down(wl->wlc);
1938         callbacks = atomic_read(&wl->callbacks) - ret_val;
1939
1940         /* wait for down callbacks to complete */
1941         WL_UNLOCK(wl);
1942
1943 #ifndef WLC_HIGH_ONLY
1944         /* For HIGH_only driver, it's important to actually schedule other work,
1945          * not just spin wait since everything runs at schedule level
1946          */
1947         SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1948 #endif                          /* WLC_HIGH_ONLY */
1949
1950         WL_LOCK(wl);
1951 }
1952
1953 irqreturn_t BCMFASTPATH wl_isr(int irq, void *dev_id)
1954 {
1955 #if defined(WLC_LOW)
1956         wl_info_t *wl;
1957         bool ours, wantdpc;
1958         unsigned long flags;
1959
1960         wl = (wl_info_t *) dev_id;
1961
1962         WL_ISRLOCK(wl, flags);
1963
1964         /* call common first level interrupt handler */
1965         if ((ours = wlc_isr(wl->wlc, &wantdpc))) {
1966                 /* if more to do... */
1967                 if (wantdpc) {
1968
1969                         /* ...and call the second level interrupt handler */
1970                         /* schedule dpc */
1971                         ASSERT(wl->resched == FALSE);
1972                         tasklet_schedule(&wl->tasklet);
1973                 }
1974         }
1975
1976         WL_ISRUNLOCK(wl, flags);
1977
1978         return IRQ_RETVAL(ours);
1979 #else
1980         return IRQ_RETVAL(0);
1981 #endif                          /* WLC_LOW */
1982 }
1983
1984 static void BCMFASTPATH wl_dpc(ulong data)
1985 {
1986 #ifdef WLC_LOW
1987         wl_info_t *wl;
1988
1989         wl = (wl_info_t *) data;
1990
1991         WL_LOCK(wl);
1992
1993         /* call the common second level interrupt handler */
1994         if (wl->pub->up) {
1995                 if (wl->resched) {
1996                         unsigned long flags;
1997
1998                         INT_LOCK(wl, flags);
1999                         wlc_intrsupd(wl->wlc);
2000                         INT_UNLOCK(wl, flags);
2001                 }
2002
2003                 wl->resched = wlc_dpc(wl->wlc, TRUE);
2004         }
2005
2006         /* wlc_dpc() may bring the driver down */
2007         if (!wl->pub->up)
2008                 goto done;
2009
2010         /* re-schedule dpc */
2011         if (wl->resched)
2012                 tasklet_schedule(&wl->tasklet);
2013         else {
2014                 /* re-enable interrupts */
2015                 wl_intrson(wl);
2016         }
2017
2018  done:
2019         WL_UNLOCK(wl);
2020 #endif                          /* WLC_LOW */
2021 }
2022
2023 static void wl_link_up(wl_info_t * wl, char *ifname)
2024 {
2025         WL_ERROR(("wl%d: link up (%s)\n", wl->pub->unit, ifname));
2026 }
2027
2028 static void wl_link_down(wl_info_t * wl, char *ifname)
2029 {
2030         WL_ERROR(("wl%d: link down (%s)\n", wl->pub->unit, ifname));
2031 }
2032
2033 void wl_event(wl_info_t * wl, char *ifname, wlc_event_t * e)
2034 {
2035
2036         switch (e->event.event_type) {
2037         case WLC_E_LINK:
2038         case WLC_E_NDIS_LINK:
2039                 if (e->event.flags & WLC_EVENT_MSG_LINK)
2040                         wl_link_up(wl, ifname);
2041                 else
2042                         wl_link_down(wl, ifname);
2043                 break;
2044         case WLC_E_RADIO:
2045                 break;
2046         }
2047 }
2048
2049 static void wl_timer(ulong data)
2050 {
2051 #ifndef WLC_HIGH_ONLY
2052         _wl_timer((wl_timer_t *) data);
2053 #else
2054         wl_timer_t *t = (wl_timer_t *) data;
2055         wl_schedule_task(t->wl, wl_timer_task, t);
2056 #endif                          /* WLC_HIGH_ONLY */
2057 }
2058
2059 static void _wl_timer(wl_timer_t * t)
2060 {
2061         WL_LOCK(t->wl);
2062
2063         if (t->set) {
2064                 if (t->periodic) {
2065                         t->timer.expires = jiffies + t->ms * HZ / 1000;
2066                         atomic_inc(&t->wl->callbacks);
2067                         add_timer(&t->timer);
2068                         t->set = TRUE;
2069                 } else
2070                         t->set = FALSE;
2071
2072                 t->fn(t->arg);
2073         }
2074
2075         atomic_dec(&t->wl->callbacks);
2076
2077         WL_UNLOCK(t->wl);
2078 }
2079
2080 wl_timer_t *wl_init_timer(wl_info_t * wl, void (*fn) (void *arg), void *arg,
2081                           const char *name)
2082 {
2083         wl_timer_t *t;
2084
2085         if (!(t = osl_malloc(wl->osh, sizeof(wl_timer_t)))) {
2086                 WL_ERROR(("wl%d: wl_init_timer: out of memory, malloced %d bytes\n", wl->pub->unit, osl_malloced(wl->osh)));
2087                 return 0;
2088         }
2089
2090         bzero(t, sizeof(wl_timer_t));
2091
2092         init_timer(&t->timer);
2093         t->timer.data = (ulong) t;
2094         t->timer.function = wl_timer;
2095         t->wl = wl;
2096         t->fn = fn;
2097         t->arg = arg;
2098         t->next = wl->timers;
2099         wl->timers = t;
2100
2101 #ifdef BCMDBG
2102         if ((t->name = osl_malloc(wl->osh, strlen(name) + 1)))
2103                 strcpy(t->name, name);
2104 #endif
2105
2106         return t;
2107 }
2108
2109 /* BMAC_NOTE: Add timer adds only the kernel timer since it's going to be more accurate
2110  * as well as it's easier to make it periodic
2111  */
2112 void wl_add_timer(wl_info_t * wl, wl_timer_t * t, uint ms, int periodic)
2113 {
2114 #ifdef BCMDBG
2115         if (t->set) {
2116                 WL_ERROR(("%s: Already set. Name: %s, per %d\n",
2117                           __func__, t->name, periodic));
2118         }
2119 #endif
2120         ASSERT(!t->set);
2121
2122         t->ms = ms;
2123         t->periodic = (bool) periodic;
2124         t->set = TRUE;
2125         t->timer.expires = jiffies + ms * HZ / 1000;
2126
2127         atomic_inc(&wl->callbacks);
2128         add_timer(&t->timer);
2129 }
2130
2131 /* return TRUE if timer successfully deleted, FALSE if still pending */
2132 bool wl_del_timer(wl_info_t * wl, wl_timer_t * t)
2133 {
2134         if (t->set) {
2135                 t->set = FALSE;
2136                 if (!del_timer(&t->timer)) {
2137                         return FALSE;
2138                 }
2139                 atomic_dec(&wl->callbacks);
2140         }
2141
2142         return TRUE;
2143 }
2144
2145 void wl_free_timer(wl_info_t * wl, wl_timer_t * t)
2146 {
2147         wl_timer_t *tmp;
2148
2149         /* delete the timer in case it is active */
2150         wl_del_timer(wl, t);
2151
2152         if (wl->timers == t) {
2153                 wl->timers = wl->timers->next;
2154 #ifdef BCMDBG
2155                 if (t->name)
2156                         MFREE(wl->osh, t->name, strlen(t->name) + 1);
2157 #endif
2158                 MFREE(wl->osh, t, sizeof(wl_timer_t));
2159                 return;
2160
2161         }
2162
2163         tmp = wl->timers;
2164         while (tmp) {
2165                 if (tmp->next == t) {
2166                         tmp->next = t->next;
2167 #ifdef BCMDBG
2168                         if (t->name)
2169                                 MFREE(wl->osh, t->name, strlen(t->name) + 1);
2170 #endif
2171                         MFREE(wl->osh, t, sizeof(wl_timer_t));
2172                         return;
2173                 }
2174                 tmp = tmp->next;
2175         }
2176
2177 }
2178
2179 static int wl_linux_watchdog(void *ctx)
2180 {
2181         wl_info_t *wl = (wl_info_t *) ctx;
2182         struct net_device_stats *stats = NULL;
2183         uint id;
2184         /* refresh stats */
2185         if (wl->pub->up) {
2186                 ASSERT(wl->stats_id < 2);
2187
2188                 id = 1 - wl->stats_id;
2189
2190                 stats = &wl->stats_watchdog[id];
2191                 stats->rx_packets = WLCNTVAL(wl->pub->_cnt->rxframe);
2192                 stats->tx_packets = WLCNTVAL(wl->pub->_cnt->txframe);
2193                 stats->rx_bytes = WLCNTVAL(wl->pub->_cnt->rxbyte);
2194                 stats->tx_bytes = WLCNTVAL(wl->pub->_cnt->txbyte);
2195                 stats->rx_errors = WLCNTVAL(wl->pub->_cnt->rxerror);
2196                 stats->tx_errors = WLCNTVAL(wl->pub->_cnt->txerror);
2197                 stats->collisions = 0;
2198
2199                 stats->rx_length_errors = 0;
2200                 stats->rx_over_errors = WLCNTVAL(wl->pub->_cnt->rxoflo);
2201                 stats->rx_crc_errors = WLCNTVAL(wl->pub->_cnt->rxcrc);
2202                 stats->rx_frame_errors = 0;
2203                 stats->rx_fifo_errors = WLCNTVAL(wl->pub->_cnt->rxoflo);
2204                 stats->rx_missed_errors = 0;
2205
2206                 stats->tx_fifo_errors = WLCNTVAL(wl->pub->_cnt->txuflo);
2207
2208                 wl->stats_id = id;
2209
2210         }
2211
2212         return 0;
2213 }
2214
2215 struct wl_fw_hdr {
2216         uint32 offset;
2217         uint32 len;
2218         uint32 idx;
2219 };
2220
2221 #ifdef WLC_HIGH_ONLY
2222 static void wl_rpc_down(void *wlh)
2223 {
2224         wl_info_t *wl = (wl_info_t *) (wlh);
2225
2226         wlc_device_removed(wl->wlc);
2227
2228         wl_rpcq_free(wl);
2229 }
2230
2231 static int BCMFASTPATH wl_start(struct sk_buff *skb, wl_info_t * wl)
2232 {
2233
2234         ulong flags;
2235
2236         skb->prev = NULL;
2237
2238         /* Lock the queue as tasklet could be running at this time */
2239         TXQ_LOCK(wl, flags);
2240         if (wl->txq_head == NULL)
2241                 wl->txq_head = skb;
2242         else {
2243                 wl->txq_tail->prev = skb;
2244         }
2245         wl->txq_tail = skb;
2246
2247         if (wl->txq_dispatched == FALSE) {
2248                 wl->txq_dispatched = TRUE;
2249
2250                 if (schedule_work(&wl->txq_task.work)) {
2251                         atomic_inc(&wl->callbacks);
2252                 } else {
2253                         WL_ERROR(("wl%d: wl_start/schedule_work failed\n",
2254                                   wl->pub->unit));
2255                 }
2256         }
2257
2258         TXQ_UNLOCK(wl, flags);
2259
2260         return (0);
2261
2262 }
2263
2264 static void wl_start_txqwork(struct wl_task *task)
2265 {
2266         wl_info_t *wl = (wl_info_t *) task->context;
2267         struct sk_buff *skb;
2268         ulong flags;
2269         uint count = 0;
2270
2271         WL_TRACE(("wl%d: wl_start_txqwork\n", wl->pub->unit));
2272
2273         /* First remove an entry then go for execution */
2274         TXQ_LOCK(wl, flags);
2275         while (wl->txq_head) {
2276                 skb = wl->txq_head;
2277                 wl->txq_head = skb->prev;
2278                 skb->prev = NULL;
2279                 if (wl->txq_head == NULL)
2280                         wl->txq_tail = NULL;
2281                 TXQ_UNLOCK(wl, flags);
2282
2283                 /* it has WL_LOCK/WL_UNLOCK inside */
2284                 wl_start_int(wl, WL_TO_HW(wl), skb);
2285
2286                 /* bounded our execution, reshedule ourself next */
2287                 if (++count >= 10)
2288                         break;
2289
2290                 TXQ_LOCK(wl, flags);
2291         }
2292
2293         if (count >= 10) {
2294                 if (!schedule_work(&wl->txq_task.work)) {
2295                         WL_ERROR(("wl%d: wl_start/schedule_work failed\n",
2296                                   wl->pub->unit));
2297                         atomic_dec(&wl->callbacks);
2298                 }
2299         } else {
2300                 wl->txq_dispatched = FALSE;
2301                 TXQ_UNLOCK(wl, flags);
2302                 atomic_dec(&wl->callbacks);
2303         }
2304
2305         return;
2306 }
2307
2308 static void wl_txq_free(wl_info_t * wl)
2309 {
2310         struct sk_buff *skb;
2311
2312         if (wl->txq_head == NULL) {
2313                 ASSERT(wl->txq_tail == NULL);
2314                 return;
2315         }
2316
2317         while (wl->txq_head) {
2318                 skb = wl->txq_head;
2319                 wl->txq_head = skb->prev;
2320                 PKTFREE(wl->osh, skb, TRUE);
2321         }
2322
2323         wl->txq_tail = NULL;
2324 }
2325
2326 static void wl_rpcq_free(wl_info_t * wl)
2327 {
2328         rpc_buf_t *buf;
2329
2330         if (wl->rpcq_head == NULL) {
2331                 ASSERT(wl->rpcq_tail == NULL);
2332                 return;
2333         }
2334
2335         while (wl->rpcq_head) {
2336                 buf = wl->rpcq_head;
2337                 wl->rpcq_head = bcm_rpc_buf_next_get(wl->rpc_th, buf);
2338                 bcm_rpc_buf_free(wl->rpc_dispatch_ctx.rpc, buf);
2339         }
2340
2341         wl->rpcq_tail = NULL;
2342 }
2343
2344 static void wl_rpcq_dispatch(struct wl_task *task)
2345 {
2346         wl_info_t *wl = (wl_info_t *) task->context;
2347         rpc_buf_t *buf;
2348         ulong flags;
2349
2350         /* First remove an entry then go for execution */
2351         RPCQ_LOCK(wl, flags);
2352         while (wl->rpcq_head) {
2353                 buf = wl->rpcq_head;
2354                 wl->rpcq_head = bcm_rpc_buf_next_get(wl->rpc_th, buf);
2355
2356                 if (wl->rpcq_head == NULL)
2357                         wl->rpcq_tail = NULL;
2358                 RPCQ_UNLOCK(wl, flags);
2359
2360                 WL_LOCK(wl);
2361                 wlc_rpc_high_dispatch(&wl->rpc_dispatch_ctx, buf);
2362                 WL_UNLOCK(wl);
2363
2364                 RPCQ_LOCK(wl, flags);
2365         }
2366
2367         wl->rpcq_dispatched = FALSE;
2368
2369         RPCQ_UNLOCK(wl, flags);
2370
2371         MFREE(wl->osh, task, sizeof(wl_task_t));
2372         atomic_dec(&wl->callbacks);
2373 }
2374
2375 static void wl_rpcq_add(wl_info_t * wl, rpc_buf_t * buf)
2376 {
2377         ulong flags;
2378
2379         bcm_rpc_buf_next_set(wl->rpc_th, buf, NULL);
2380
2381         /* Lock the queue as tasklet could be running at this time */
2382         RPCQ_LOCK(wl, flags);
2383         if (wl->rpcq_head == NULL)
2384                 wl->rpcq_head = buf;
2385         else
2386                 bcm_rpc_buf_next_set(wl->rpc_th, wl->rpcq_tail, buf);
2387
2388         wl->rpcq_tail = buf;
2389
2390         if (wl->rpcq_dispatched == FALSE) {
2391                 wl->rpcq_dispatched = TRUE;
2392                 wl_schedule_task(wl, wl_rpcq_dispatch, wl);
2393         }
2394
2395         RPCQ_UNLOCK(wl, flags);
2396 }
2397
2398 #if defined(BCMDBG)
2399 static const struct name_entry rpc_name_tbl[] = RPC_ID_TABLE;
2400 #endif                          /* BCMDBG */
2401
2402 /* dongle-side rpc dispatch routine */
2403 static void wl_rpc_dispatch_schedule(void *ctx, struct rpc_buf *buf)
2404 {
2405         bcm_xdr_buf_t b;
2406         wl_info_t *wl = (wl_info_t *) ctx;
2407         wlc_rpc_id_t rpc_id;
2408         int err;
2409
2410         bcm_xdr_buf_init(&b, bcm_rpc_buf_data(wl->rpc_th, buf),
2411                          bcm_rpc_buf_len_get(wl->rpc_th, buf));
2412
2413         err = bcm_xdr_unpack_uint32(&b, &rpc_id);
2414         ASSERT(!err);
2415         WL_TRACE(("%s: Dispatch id %s\n", __func__,
2416                   WLC_RPC_ID_LOOKUP(rpc_name_tbl, rpc_id)));
2417
2418         /* Handle few emergency ones */
2419         switch (rpc_id) {
2420         default:
2421                 wl_rpcq_add(wl, buf);
2422                 break;
2423         }
2424 }
2425
2426 static void wl_timer_task(wl_task_t * task)
2427 {
2428         wl_timer_t *t = (wl_timer_t *) task->context;
2429
2430         _wl_timer(t);
2431         MFREE(t->wl->osh, task, sizeof(wl_task_t));
2432
2433         /* This dec is for the task_schedule. The timer related
2434          * callback is decremented in _wl_timer
2435          */
2436         atomic_dec(&t->wl->callbacks);
2437 }
2438 #endif                          /* WLC_HIGH_ONLY */
2439
2440 #ifndef WLC_HIGH_ONLY
2441 char *wl_firmwares[WL_MAX_FW] = {
2442         "brcm/bcm43xx",
2443         NULL
2444 };
2445
2446 #ifdef WLC_LOW
2447 int wl_ucode_init_buf(wl_info_t * wl, void **pbuf, uint32 idx)
2448 {
2449         int i, entry;
2450         const uint8 *pdata;
2451         struct wl_fw_hdr *hdr;
2452         for (i = 0; i < wl->fw.fw_cnt; i++) {
2453                 hdr = (struct wl_fw_hdr *)wl->fw.fw_hdr[i]->data;
2454                 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
2455                      entry++, hdr++) {
2456                         if (hdr->idx == idx) {
2457                                 pdata = wl->fw.fw_bin[i]->data + hdr->offset;
2458                                 *pbuf = kmalloc(hdr->len, GFP_ATOMIC);
2459                                 if (*pbuf == NULL) {
2460                                         printf("fail to alloc %d bytes\n",
2461                                                hdr->len);
2462                                 }
2463                                 bcopy(pdata, *pbuf, hdr->len);
2464                                 return 0;
2465                         }
2466                 }
2467         }
2468         printf("ERROR: ucode buf tag:%d can not be found!\n", idx);
2469         *pbuf = NULL;
2470         return -1;
2471 }
2472
2473 int wl_ucode_init_uint(wl_info_t * wl, uint32 * data, uint32 idx)
2474 {
2475         int i, entry;
2476         const uint8 *pdata;
2477         struct wl_fw_hdr *hdr;
2478         for (i = 0; i < wl->fw.fw_cnt; i++) {
2479                 hdr = (struct wl_fw_hdr *)wl->fw.fw_hdr[i]->data;
2480                 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
2481                      entry++, hdr++) {
2482                         if (hdr->idx == idx) {
2483                                 pdata = wl->fw.fw_bin[i]->data + hdr->offset;
2484                                 ASSERT(hdr->len == 4);
2485                                 *data = *((uint32 *) pdata);
2486                                 return 0;
2487                         }
2488                 }
2489         }
2490         printf("ERROR: ucode tag:%d can not be found!\n", idx);
2491         return -1;
2492 }
2493 #endif                          /* WLC_LOW */
2494
2495 static int wl_request_fw(wl_info_t * wl, struct pci_dev *pdev)
2496 {
2497         int status;
2498         struct device *device = &pdev->dev;
2499         char fw_name[100];
2500         int i;
2501
2502         bzero((void *)&wl->fw, sizeof(struct wl_firmware));
2503         for (i = 0; i < WL_MAX_FW; i++) {
2504                 if (wl_firmwares[i] == NULL)
2505                         break;
2506                 sprintf(fw_name, "%s-%d.fw", wl_firmwares[i],
2507                         UCODE_LOADER_API_VER);
2508                 WL_NONE(("request fw %s\n", fw_name));
2509                 status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
2510                 if (status) {
2511                         printf("fail to request firmware %s\n", fw_name);
2512                         return status;
2513                 }
2514                 WL_NONE(("request fw %s\n", fw_name));
2515                 sprintf(fw_name, "%s_hdr-%d.fw", wl_firmwares[i],
2516                         UCODE_LOADER_API_VER);
2517                 status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
2518                 if (status) {
2519                         printf("fail to request firmware %s\n", fw_name);
2520                         return status;
2521                 }
2522                 wl->fw.hdr_num_entries[i] =
2523                     wl->fw.fw_hdr[i]->size / (sizeof(struct wl_fw_hdr));
2524                 WL_NONE(("request fw %s find: %d entries\n", fw_name,
2525                          wl->fw.hdr_num_entries[i]));
2526         }
2527         wl->fw.fw_cnt = i;
2528         wl_ucode_data_init(wl);
2529         return 0;
2530 }
2531
2532 #ifdef WLC_LOW
2533 void wl_ucode_free_buf(void *p)
2534 {
2535         kfree(p);
2536 }
2537 #endif                          /* WLC_LOW */
2538
2539 static void wl_release_fw(wl_info_t * wl)
2540 {
2541         int i;
2542         for (i = 0; i < wl->fw.fw_cnt; i++) {
2543                 release_firmware(wl->fw.fw_bin[i]);
2544                 release_firmware(wl->fw.fw_hdr[i]);
2545         }
2546 }
2547 #endif                          /* WLC_HIGH_ONLY */