]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/brcm80211/brcmsmac/mac80211_if.c
Merge remote-tracking branches 'regulator/topic/core', 'regulator/topic/bypass',...
[karo-tx-linux.git] / drivers / net / wireless / brcm80211 / brcmsmac / mac80211_if.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/etherdevice.h>
21 #include <linux/sched.h>
22 #include <linux/firmware.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/bcma/bcma.h>
26 #include <net/mac80211.h>
27 #include <defs.h>
28 #include "phy/phy_int.h"
29 #include "d11.h"
30 #include "channel.h"
31 #include "scb.h"
32 #include "pub.h"
33 #include "ucode_loader.h"
34 #include "mac80211_if.h"
35 #include "main.h"
36
37 #define N_TX_QUEUES     4 /* #tx queues on mac80211<->driver interface */
38
39 /* Flags we support */
40 #define MAC_FILTERS (FIF_PROMISC_IN_BSS | \
41         FIF_ALLMULTI | \
42         FIF_FCSFAIL | \
43         FIF_CONTROL | \
44         FIF_OTHER_BSS | \
45         FIF_BCN_PRBRESP_PROMISC | \
46         FIF_PSPOLL)
47
48 #define CHAN2GHZ(channel, freqency, chflags)  { \
49         .band = IEEE80211_BAND_2GHZ, \
50         .center_freq = (freqency), \
51         .hw_value = (channel), \
52         .flags = chflags, \
53         .max_antenna_gain = 0, \
54         .max_power = 19, \
55 }
56
57 #define CHAN5GHZ(channel, chflags)  { \
58         .band = IEEE80211_BAND_5GHZ, \
59         .center_freq = 5000 + 5*(channel), \
60         .hw_value = (channel), \
61         .flags = chflags, \
62         .max_antenna_gain = 0, \
63         .max_power = 21, \
64 }
65
66 #define RATE(rate100m, _flags) { \
67         .bitrate = (rate100m), \
68         .flags = (_flags), \
69         .hw_value = (rate100m / 5), \
70 }
71
72 struct firmware_hdr {
73         __le32 offset;
74         __le32 len;
75         __le32 idx;
76 };
77
78 static const char * const brcms_firmwares[MAX_FW_IMAGES] = {
79         "brcm/bcm43xx",
80         NULL
81 };
82
83 static int n_adapters_found;
84
85 MODULE_AUTHOR("Broadcom Corporation");
86 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver.");
87 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
88 MODULE_LICENSE("Dual BSD/GPL");
89
90
91 /* recognized BCMA Core IDs */
92 static struct bcma_device_id brcms_coreid_table[] = {
93         BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 23, BCMA_ANY_CLASS),
94         BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 24, BCMA_ANY_CLASS),
95         BCMA_CORETABLE_END
96 };
97 MODULE_DEVICE_TABLE(bcma, brcms_coreid_table);
98
99 #ifdef DEBUG
100 static int msglevel = 0xdeadbeef;
101 module_param(msglevel, int, 0);
102 #endif                          /* DEBUG */
103
104 static struct ieee80211_channel brcms_2ghz_chantable[] = {
105         CHAN2GHZ(1, 2412, IEEE80211_CHAN_NO_HT40MINUS),
106         CHAN2GHZ(2, 2417, IEEE80211_CHAN_NO_HT40MINUS),
107         CHAN2GHZ(3, 2422, IEEE80211_CHAN_NO_HT40MINUS),
108         CHAN2GHZ(4, 2427, IEEE80211_CHAN_NO_HT40MINUS),
109         CHAN2GHZ(5, 2432, 0),
110         CHAN2GHZ(6, 2437, 0),
111         CHAN2GHZ(7, 2442, 0),
112         CHAN2GHZ(8, 2447, IEEE80211_CHAN_NO_HT40PLUS),
113         CHAN2GHZ(9, 2452, IEEE80211_CHAN_NO_HT40PLUS),
114         CHAN2GHZ(10, 2457, IEEE80211_CHAN_NO_HT40PLUS),
115         CHAN2GHZ(11, 2462, IEEE80211_CHAN_NO_HT40PLUS),
116         CHAN2GHZ(12, 2467,
117                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
118                  IEEE80211_CHAN_NO_HT40PLUS),
119         CHAN2GHZ(13, 2472,
120                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
121                  IEEE80211_CHAN_NO_HT40PLUS),
122         CHAN2GHZ(14, 2484,
123                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_IBSS |
124                  IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS |
125                  IEEE80211_CHAN_NO_OFDM)
126 };
127
128 static struct ieee80211_channel brcms_5ghz_nphy_chantable[] = {
129         /* UNII-1 */
130         CHAN5GHZ(36, IEEE80211_CHAN_NO_HT40MINUS),
131         CHAN5GHZ(40, IEEE80211_CHAN_NO_HT40PLUS),
132         CHAN5GHZ(44, IEEE80211_CHAN_NO_HT40MINUS),
133         CHAN5GHZ(48, IEEE80211_CHAN_NO_HT40PLUS),
134         /* UNII-2 */
135         CHAN5GHZ(52,
136                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
137                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
138         CHAN5GHZ(56,
139                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
140                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
141         CHAN5GHZ(60,
142                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
143                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
144         CHAN5GHZ(64,
145                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
146                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
147         /* MID */
148         CHAN5GHZ(100,
149                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
150                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
151         CHAN5GHZ(104,
152                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
153                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
154         CHAN5GHZ(108,
155                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
156                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
157         CHAN5GHZ(112,
158                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
159                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
160         CHAN5GHZ(116,
161                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
162                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
163         CHAN5GHZ(120,
164                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
165                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
166         CHAN5GHZ(124,
167                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
168                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
169         CHAN5GHZ(128,
170                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
171                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
172         CHAN5GHZ(132,
173                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
174                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40MINUS),
175         CHAN5GHZ(136,
176                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
177                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS),
178         CHAN5GHZ(140,
179                  IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IBSS |
180                  IEEE80211_CHAN_PASSIVE_SCAN | IEEE80211_CHAN_NO_HT40PLUS |
181                  IEEE80211_CHAN_NO_HT40MINUS),
182         /* UNII-3 */
183         CHAN5GHZ(149, IEEE80211_CHAN_NO_HT40MINUS),
184         CHAN5GHZ(153, IEEE80211_CHAN_NO_HT40PLUS),
185         CHAN5GHZ(157, IEEE80211_CHAN_NO_HT40MINUS),
186         CHAN5GHZ(161, IEEE80211_CHAN_NO_HT40PLUS),
187         CHAN5GHZ(165, IEEE80211_CHAN_NO_HT40PLUS | IEEE80211_CHAN_NO_HT40MINUS)
188 };
189
190 /*
191  * The rate table is used for both 2.4G and 5G rates. The
192  * latter being a subset as it does not support CCK rates.
193  */
194 static struct ieee80211_rate legacy_ratetable[] = {
195         RATE(10, 0),
196         RATE(20, IEEE80211_RATE_SHORT_PREAMBLE),
197         RATE(55, IEEE80211_RATE_SHORT_PREAMBLE),
198         RATE(110, IEEE80211_RATE_SHORT_PREAMBLE),
199         RATE(60, 0),
200         RATE(90, 0),
201         RATE(120, 0),
202         RATE(180, 0),
203         RATE(240, 0),
204         RATE(360, 0),
205         RATE(480, 0),
206         RATE(540, 0),
207 };
208
209 static const struct ieee80211_supported_band brcms_band_2GHz_nphy_template = {
210         .band = IEEE80211_BAND_2GHZ,
211         .channels = brcms_2ghz_chantable,
212         .n_channels = ARRAY_SIZE(brcms_2ghz_chantable),
213         .bitrates = legacy_ratetable,
214         .n_bitrates = ARRAY_SIZE(legacy_ratetable),
215         .ht_cap = {
216                    /* from include/linux/ieee80211.h */
217                    .cap = IEEE80211_HT_CAP_GRN_FLD |
218                           IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40,
219                    .ht_supported = true,
220                    .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
221                    .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
222                    .mcs = {
223                            /* placeholders for now */
224                            .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
225                            .rx_highest = cpu_to_le16(500),
226                            .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
227                    }
228 };
229
230 static const struct ieee80211_supported_band brcms_band_5GHz_nphy_template = {
231         .band = IEEE80211_BAND_5GHZ,
232         .channels = brcms_5ghz_nphy_chantable,
233         .n_channels = ARRAY_SIZE(brcms_5ghz_nphy_chantable),
234         .bitrates = legacy_ratetable + BRCMS_LEGACY_5G_RATE_OFFSET,
235         .n_bitrates = ARRAY_SIZE(legacy_ratetable) -
236                         BRCMS_LEGACY_5G_RATE_OFFSET,
237         .ht_cap = {
238                    .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
239                           IEEE80211_HT_CAP_SGI_40,
240                    .ht_supported = true,
241                    .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K,
242                    .ampdu_density = AMPDU_DEF_MPDU_DENSITY,
243                    .mcs = {
244                            /* placeholders for now */
245                            .rx_mask = {0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0},
246                            .rx_highest = cpu_to_le16(500),
247                            .tx_params = IEEE80211_HT_MCS_TX_DEFINED}
248                    }
249 };
250
251 /* flags the given rate in rateset as requested */
252 static void brcms_set_basic_rate(struct brcm_rateset *rs, u16 rate, bool is_br)
253 {
254         u32 i;
255
256         for (i = 0; i < rs->count; i++) {
257                 if (rate != (rs->rates[i] & 0x7f))
258                         continue;
259
260                 if (is_br)
261                         rs->rates[i] |= BRCMS_RATE_FLAG;
262                 else
263                         rs->rates[i] &= BRCMS_RATE_MASK;
264                 return;
265         }
266 }
267
268 static void brcms_ops_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
269 {
270         struct brcms_info *wl = hw->priv;
271         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
272
273         spin_lock_bh(&wl->lock);
274         if (!wl->pub->up) {
275                 wiphy_err(wl->wiphy, "ops->tx called while down\n");
276                 kfree_skb(skb);
277                 goto done;
278         }
279         brcms_c_sendpkt_mac80211(wl->wlc, skb, hw);
280         tx_info->rate_driver_data[0] = tx_info->control.sta;
281  done:
282         spin_unlock_bh(&wl->lock);
283 }
284
285 static int brcms_ops_start(struct ieee80211_hw *hw)
286 {
287         struct brcms_info *wl = hw->priv;
288         bool blocked;
289         int err;
290
291         ieee80211_wake_queues(hw);
292         spin_lock_bh(&wl->lock);
293         blocked = brcms_rfkill_set_hw_state(wl);
294         spin_unlock_bh(&wl->lock);
295         if (!blocked)
296                 wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
297
298         spin_lock_bh(&wl->lock);
299         /* avoid acknowledging frames before a non-monitor device is added */
300         wl->mute_tx = true;
301
302         if (!wl->pub->up)
303                 err = brcms_up(wl);
304         else
305                 err = -ENODEV;
306         spin_unlock_bh(&wl->lock);
307
308         if (err != 0)
309                 wiphy_err(hw->wiphy, "%s: brcms_up() returned %d\n", __func__,
310                           err);
311         return err;
312 }
313
314 static void brcms_ops_stop(struct ieee80211_hw *hw)
315 {
316         struct brcms_info *wl = hw->priv;
317         int status;
318
319         ieee80211_stop_queues(hw);
320
321         if (wl->wlc == NULL)
322                 return;
323
324         spin_lock_bh(&wl->lock);
325         status = brcms_c_chipmatch(wl->wlc->hw->d11core);
326         spin_unlock_bh(&wl->lock);
327         if (!status) {
328                 wiphy_err(wl->wiphy,
329                           "wl: brcms_ops_stop: chipmatch failed\n");
330                 return;
331         }
332
333         /* put driver in down state */
334         spin_lock_bh(&wl->lock);
335         brcms_down(wl);
336         spin_unlock_bh(&wl->lock);
337 }
338
339 static int
340 brcms_ops_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
341 {
342         struct brcms_info *wl = hw->priv;
343
344         /* Just STA for now */
345         if (vif->type != NL80211_IFTYPE_STATION) {
346                 wiphy_err(hw->wiphy, "%s: Attempt to add type %d, only"
347                           " STA for now\n", __func__, vif->type);
348                 return -EOPNOTSUPP;
349         }
350
351         wl->mute_tx = false;
352         brcms_c_mute(wl->wlc, false);
353
354         return 0;
355 }
356
357 static void
358 brcms_ops_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
359 {
360 }
361
362 static int brcms_ops_config(struct ieee80211_hw *hw, u32 changed)
363 {
364         struct ieee80211_conf *conf = &hw->conf;
365         struct brcms_info *wl = hw->priv;
366         int err = 0;
367         int new_int;
368         struct wiphy *wiphy = hw->wiphy;
369
370         spin_lock_bh(&wl->lock);
371         if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
372                 brcms_c_set_beacon_listen_interval(wl->wlc,
373                                                    conf->listen_interval);
374         }
375         if (changed & IEEE80211_CONF_CHANGE_MONITOR)
376                 wiphy_dbg(wiphy, "%s: change monitor mode: %s\n",
377                           __func__, conf->flags & IEEE80211_CONF_MONITOR ?
378                           "true" : "false");
379         if (changed & IEEE80211_CONF_CHANGE_PS)
380                 wiphy_err(wiphy, "%s: change power-save mode: %s (implement)\n",
381                           __func__, conf->flags & IEEE80211_CONF_PS ?
382                           "true" : "false");
383
384         if (changed & IEEE80211_CONF_CHANGE_POWER) {
385                 err = brcms_c_set_tx_power(wl->wlc, conf->power_level);
386                 if (err < 0) {
387                         wiphy_err(wiphy, "%s: Error setting power_level\n",
388                                   __func__);
389                         goto config_out;
390                 }
391                 new_int = brcms_c_get_tx_power(wl->wlc);
392                 if (new_int != conf->power_level)
393                         wiphy_err(wiphy, "%s: Power level req != actual, %d %d"
394                                   "\n", __func__, conf->power_level,
395                                   new_int);
396         }
397         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
398                 if (conf->channel_type == NL80211_CHAN_HT20 ||
399                     conf->channel_type == NL80211_CHAN_NO_HT)
400                         err = brcms_c_set_channel(wl->wlc,
401                                                   conf->channel->hw_value);
402                 else
403                         err = -ENOTSUPP;
404         }
405         if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
406                 err = brcms_c_set_rate_limit(wl->wlc,
407                                              conf->short_frame_max_tx_count,
408                                              conf->long_frame_max_tx_count);
409
410  config_out:
411         spin_unlock_bh(&wl->lock);
412         return err;
413 }
414
415 static void
416 brcms_ops_bss_info_changed(struct ieee80211_hw *hw,
417                         struct ieee80211_vif *vif,
418                         struct ieee80211_bss_conf *info, u32 changed)
419 {
420         struct brcms_info *wl = hw->priv;
421         struct wiphy *wiphy = hw->wiphy;
422
423         if (changed & BSS_CHANGED_ASSOC) {
424                 /* association status changed (associated/disassociated)
425                  * also implies a change in the AID.
426                  */
427                 wiphy_err(wiphy, "%s: %s: %sassociated\n", KBUILD_MODNAME,
428                           __func__, info->assoc ? "" : "dis");
429                 spin_lock_bh(&wl->lock);
430                 brcms_c_associate_upd(wl->wlc, info->assoc);
431                 spin_unlock_bh(&wl->lock);
432         }
433         if (changed & BSS_CHANGED_ERP_SLOT) {
434                 s8 val;
435
436                 /* slot timing changed */
437                 if (info->use_short_slot)
438                         val = 1;
439                 else
440                         val = 0;
441                 spin_lock_bh(&wl->lock);
442                 brcms_c_set_shortslot_override(wl->wlc, val);
443                 spin_unlock_bh(&wl->lock);
444         }
445
446         if (changed & BSS_CHANGED_HT) {
447                 /* 802.11n parameters changed */
448                 u16 mode = info->ht_operation_mode;
449
450                 spin_lock_bh(&wl->lock);
451                 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_CFG,
452                         mode & IEEE80211_HT_OP_MODE_PROTECTION);
453                 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_NONGF,
454                         mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
455                 brcms_c_protection_upd(wl->wlc, BRCMS_PROT_N_OBSS,
456                         mode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT);
457                 spin_unlock_bh(&wl->lock);
458         }
459         if (changed & BSS_CHANGED_BASIC_RATES) {
460                 struct ieee80211_supported_band *bi;
461                 u32 br_mask, i;
462                 u16 rate;
463                 struct brcm_rateset rs;
464                 int error;
465
466                 /* retrieve the current rates */
467                 spin_lock_bh(&wl->lock);
468                 brcms_c_get_current_rateset(wl->wlc, &rs);
469                 spin_unlock_bh(&wl->lock);
470
471                 br_mask = info->basic_rates;
472                 bi = hw->wiphy->bands[brcms_c_get_curband(wl->wlc)];
473                 for (i = 0; i < bi->n_bitrates; i++) {
474                         /* convert to internal rate value */
475                         rate = (bi->bitrates[i].bitrate << 1) / 10;
476
477                         /* set/clear basic rate flag */
478                         brcms_set_basic_rate(&rs, rate, br_mask & 1);
479                         br_mask >>= 1;
480                 }
481
482                 /* update the rate set */
483                 spin_lock_bh(&wl->lock);
484                 error = brcms_c_set_rateset(wl->wlc, &rs);
485                 spin_unlock_bh(&wl->lock);
486                 if (error)
487                         wiphy_err(wiphy, "changing basic rates failed: %d\n",
488                                   error);
489         }
490         if (changed & BSS_CHANGED_BEACON_INT) {
491                 /* Beacon interval changed */
492                 spin_lock_bh(&wl->lock);
493                 brcms_c_set_beacon_period(wl->wlc, info->beacon_int);
494                 spin_unlock_bh(&wl->lock);
495         }
496         if (changed & BSS_CHANGED_BSSID) {
497                 /* BSSID changed, for whatever reason (IBSS and managed mode) */
498                 spin_lock_bh(&wl->lock);
499                 brcms_c_set_addrmatch(wl->wlc, RCM_BSSID_OFFSET, info->bssid);
500                 spin_unlock_bh(&wl->lock);
501         }
502         if (changed & BSS_CHANGED_BEACON)
503                 /* Beacon data changed, retrieve new beacon (beaconing modes) */
504                 wiphy_err(wiphy, "%s: beacon changed\n", __func__);
505
506         if (changed & BSS_CHANGED_BEACON_ENABLED) {
507                 /* Beaconing should be enabled/disabled (beaconing modes) */
508                 wiphy_err(wiphy, "%s: Beacon enabled: %s\n", __func__,
509                           info->enable_beacon ? "true" : "false");
510         }
511
512         if (changed & BSS_CHANGED_CQM) {
513                 /* Connection quality monitor config changed */
514                 wiphy_err(wiphy, "%s: cqm change: threshold %d, hys %d "
515                           " (implement)\n", __func__, info->cqm_rssi_thold,
516                           info->cqm_rssi_hyst);
517         }
518
519         if (changed & BSS_CHANGED_IBSS) {
520                 /* IBSS join status changed */
521                 wiphy_err(wiphy, "%s: IBSS joined: %s (implement)\n", __func__,
522                           info->ibss_joined ? "true" : "false");
523         }
524
525         if (changed & BSS_CHANGED_ARP_FILTER) {
526                 /* Hardware ARP filter address list or state changed */
527                 wiphy_err(wiphy, "%s: arp filtering: enabled %s, count %d"
528                           " (implement)\n", __func__, info->arp_filter_enabled ?
529                           "true" : "false", info->arp_addr_cnt);
530         }
531
532         if (changed & BSS_CHANGED_QOS) {
533                 /*
534                  * QoS for this association was enabled/disabled.
535                  * Note that it is only ever disabled for station mode.
536                  */
537                 wiphy_err(wiphy, "%s: qos enabled: %s (implement)\n", __func__,
538                           info->qos ? "true" : "false");
539         }
540         return;
541 }
542
543 static void
544 brcms_ops_configure_filter(struct ieee80211_hw *hw,
545                         unsigned int changed_flags,
546                         unsigned int *total_flags, u64 multicast)
547 {
548         struct brcms_info *wl = hw->priv;
549         struct wiphy *wiphy = hw->wiphy;
550
551         changed_flags &= MAC_FILTERS;
552         *total_flags &= MAC_FILTERS;
553
554         if (changed_flags & FIF_PROMISC_IN_BSS)
555                 wiphy_dbg(wiphy, "FIF_PROMISC_IN_BSS\n");
556         if (changed_flags & FIF_ALLMULTI)
557                 wiphy_dbg(wiphy, "FIF_ALLMULTI\n");
558         if (changed_flags & FIF_FCSFAIL)
559                 wiphy_dbg(wiphy, "FIF_FCSFAIL\n");
560         if (changed_flags & FIF_CONTROL)
561                 wiphy_dbg(wiphy, "FIF_CONTROL\n");
562         if (changed_flags & FIF_OTHER_BSS)
563                 wiphy_dbg(wiphy, "FIF_OTHER_BSS\n");
564         if (changed_flags & FIF_PSPOLL)
565                 wiphy_dbg(wiphy, "FIF_PSPOLL\n");
566         if (changed_flags & FIF_BCN_PRBRESP_PROMISC)
567                 wiphy_dbg(wiphy, "FIF_BCN_PRBRESP_PROMISC\n");
568
569         spin_lock_bh(&wl->lock);
570         brcms_c_mac_promisc(wl->wlc, *total_flags);
571         spin_unlock_bh(&wl->lock);
572         return;
573 }
574
575 static void brcms_ops_sw_scan_start(struct ieee80211_hw *hw)
576 {
577         struct brcms_info *wl = hw->priv;
578         spin_lock_bh(&wl->lock);
579         brcms_c_scan_start(wl->wlc);
580         spin_unlock_bh(&wl->lock);
581         return;
582 }
583
584 static void brcms_ops_sw_scan_complete(struct ieee80211_hw *hw)
585 {
586         struct brcms_info *wl = hw->priv;
587         spin_lock_bh(&wl->lock);
588         brcms_c_scan_stop(wl->wlc);
589         spin_unlock_bh(&wl->lock);
590         return;
591 }
592
593 static int
594 brcms_ops_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
595                   const struct ieee80211_tx_queue_params *params)
596 {
597         struct brcms_info *wl = hw->priv;
598
599         spin_lock_bh(&wl->lock);
600         brcms_c_wme_setparams(wl->wlc, queue, params, true);
601         spin_unlock_bh(&wl->lock);
602
603         return 0;
604 }
605
606 static int
607 brcms_ops_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
608                struct ieee80211_sta *sta)
609 {
610         struct brcms_info *wl = hw->priv;
611         struct scb *scb = &wl->wlc->pri_scb;
612
613         brcms_c_init_scb(scb);
614
615         wl->pub->global_ampdu = &(scb->scb_ampdu);
616         wl->pub->global_ampdu->scb = scb;
617         wl->pub->global_ampdu->max_pdu = 16;
618
619         /*
620          * minstrel_ht initiates addBA on our behalf by calling
621          * ieee80211_start_tx_ba_session()
622          */
623         return 0;
624 }
625
626 static int
627 brcms_ops_ampdu_action(struct ieee80211_hw *hw,
628                     struct ieee80211_vif *vif,
629                     enum ieee80211_ampdu_mlme_action action,
630                     struct ieee80211_sta *sta, u16 tid, u16 *ssn,
631                     u8 buf_size)
632 {
633         struct brcms_info *wl = hw->priv;
634         struct scb *scb = &wl->wlc->pri_scb;
635         int status;
636
637         if (WARN_ON(scb->magic != SCB_MAGIC))
638                 return -EIDRM;
639         switch (action) {
640         case IEEE80211_AMPDU_RX_START:
641                 break;
642         case IEEE80211_AMPDU_RX_STOP:
643                 break;
644         case IEEE80211_AMPDU_TX_START:
645                 spin_lock_bh(&wl->lock);
646                 status = brcms_c_aggregatable(wl->wlc, tid);
647                 spin_unlock_bh(&wl->lock);
648                 if (!status) {
649                         wiphy_err(wl->wiphy, "START: tid %d is not agg\'able\n",
650                                   tid);
651                         return -EINVAL;
652                 }
653                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
654                 break;
655
656         case IEEE80211_AMPDU_TX_STOP:
657                 spin_lock_bh(&wl->lock);
658                 brcms_c_ampdu_flush(wl->wlc, sta, tid);
659                 spin_unlock_bh(&wl->lock);
660                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
661                 break;
662         case IEEE80211_AMPDU_TX_OPERATIONAL:
663                 /*
664                  * BA window size from ADDBA response ('buf_size') defines how
665                  * many outstanding MPDUs are allowed for the BA stream by
666                  * recipient and traffic class. 'ampdu_factor' gives maximum
667                  * AMPDU size.
668                  */
669                 spin_lock_bh(&wl->lock);
670                 brcms_c_ampdu_tx_operational(wl->wlc, tid, buf_size,
671                         (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
672                          sta->ht_cap.ampdu_factor)) - 1);
673                 spin_unlock_bh(&wl->lock);
674                 /* Power save wakeup */
675                 break;
676         default:
677                 wiphy_err(wl->wiphy, "%s: Invalid command, ignoring\n",
678                           __func__);
679         }
680
681         return 0;
682 }
683
684 static void brcms_ops_rfkill_poll(struct ieee80211_hw *hw)
685 {
686         struct brcms_info *wl = hw->priv;
687         bool blocked;
688
689         spin_lock_bh(&wl->lock);
690         blocked = brcms_c_check_radio_disabled(wl->wlc);
691         spin_unlock_bh(&wl->lock);
692
693         wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
694 }
695
696 static void brcms_ops_flush(struct ieee80211_hw *hw, bool drop)
697 {
698         struct brcms_info *wl = hw->priv;
699
700         no_printk("%s: drop = %s\n", __func__, drop ? "true" : "false");
701
702         /* wait for packet queue and dma fifos to run empty */
703         spin_lock_bh(&wl->lock);
704         brcms_c_wait_for_tx_completion(wl->wlc, drop);
705         spin_unlock_bh(&wl->lock);
706 }
707
708 static const struct ieee80211_ops brcms_ops = {
709         .tx = brcms_ops_tx,
710         .start = brcms_ops_start,
711         .stop = brcms_ops_stop,
712         .add_interface = brcms_ops_add_interface,
713         .remove_interface = brcms_ops_remove_interface,
714         .config = brcms_ops_config,
715         .bss_info_changed = brcms_ops_bss_info_changed,
716         .configure_filter = brcms_ops_configure_filter,
717         .sw_scan_start = brcms_ops_sw_scan_start,
718         .sw_scan_complete = brcms_ops_sw_scan_complete,
719         .conf_tx = brcms_ops_conf_tx,
720         .sta_add = brcms_ops_sta_add,
721         .ampdu_action = brcms_ops_ampdu_action,
722         .rfkill_poll = brcms_ops_rfkill_poll,
723         .flush = brcms_ops_flush,
724 };
725
726 void brcms_dpc(unsigned long data)
727 {
728         struct brcms_info *wl;
729
730         wl = (struct brcms_info *) data;
731
732         spin_lock_bh(&wl->lock);
733
734         /* call the common second level interrupt handler */
735         if (wl->pub->up) {
736                 if (wl->resched) {
737                         unsigned long flags;
738
739                         spin_lock_irqsave(&wl->isr_lock, flags);
740                         brcms_c_intrsupd(wl->wlc);
741                         spin_unlock_irqrestore(&wl->isr_lock, flags);
742                 }
743
744                 wl->resched = brcms_c_dpc(wl->wlc, true);
745         }
746
747         /* brcms_c_dpc() may bring the driver down */
748         if (!wl->pub->up)
749                 goto done;
750
751         /* re-schedule dpc */
752         if (wl->resched)
753                 tasklet_schedule(&wl->tasklet);
754         else
755                 /* re-enable interrupts */
756                 brcms_intrson(wl);
757
758  done:
759         spin_unlock_bh(&wl->lock);
760 }
761
762 /*
763  * Precondition: Since this function is called in brcms_pci_probe() context,
764  * no locking is required.
765  */
766 static int brcms_request_fw(struct brcms_info *wl, struct bcma_device *pdev)
767 {
768         int status;
769         struct device *device = &pdev->dev;
770         char fw_name[100];
771         int i;
772
773         memset(&wl->fw, 0, sizeof(struct brcms_firmware));
774         for (i = 0; i < MAX_FW_IMAGES; i++) {
775                 if (brcms_firmwares[i] == NULL)
776                         break;
777                 sprintf(fw_name, "%s-%d.fw", brcms_firmwares[i],
778                         UCODE_LOADER_API_VER);
779                 status = request_firmware(&wl->fw.fw_bin[i], fw_name, device);
780                 if (status) {
781                         wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
782                                   KBUILD_MODNAME, fw_name);
783                         return status;
784                 }
785                 sprintf(fw_name, "%s_hdr-%d.fw", brcms_firmwares[i],
786                         UCODE_LOADER_API_VER);
787                 status = request_firmware(&wl->fw.fw_hdr[i], fw_name, device);
788                 if (status) {
789                         wiphy_err(wl->wiphy, "%s: fail to load firmware %s\n",
790                                   KBUILD_MODNAME, fw_name);
791                         return status;
792                 }
793                 wl->fw.hdr_num_entries[i] =
794                     wl->fw.fw_hdr[i]->size / (sizeof(struct firmware_hdr));
795         }
796         wl->fw.fw_cnt = i;
797         return brcms_ucode_data_init(wl, &wl->ucode);
798 }
799
800 /*
801  * Precondition: Since this function is called in brcms_pci_probe() context,
802  * no locking is required.
803  */
804 static void brcms_release_fw(struct brcms_info *wl)
805 {
806         int i;
807         for (i = 0; i < MAX_FW_IMAGES; i++) {
808                 release_firmware(wl->fw.fw_bin[i]);
809                 release_firmware(wl->fw.fw_hdr[i]);
810         }
811 }
812
813 /**
814  * This function frees the WL per-device resources.
815  *
816  * This function frees resources owned by the WL device pointed to
817  * by the wl parameter.
818  *
819  * precondition: can both be called locked and unlocked
820  *
821  */
822 static void brcms_free(struct brcms_info *wl)
823 {
824         struct brcms_timer *t, *next;
825
826         /* free ucode data */
827         if (wl->fw.fw_cnt)
828                 brcms_ucode_data_free(&wl->ucode);
829         if (wl->irq)
830                 free_irq(wl->irq, wl);
831
832         /* kill dpc */
833         tasklet_kill(&wl->tasklet);
834
835         if (wl->pub)
836                 brcms_c_module_unregister(wl->pub, "linux", wl);
837
838         /* free common resources */
839         if (wl->wlc) {
840                 brcms_c_detach(wl->wlc);
841                 wl->wlc = NULL;
842                 wl->pub = NULL;
843         }
844
845         /* virtual interface deletion is deferred so we cannot spinwait */
846
847         /* wait for all pending callbacks to complete */
848         while (atomic_read(&wl->callbacks) > 0)
849                 schedule();
850
851         /* free timers */
852         for (t = wl->timers; t; t = next) {
853                 next = t->next;
854 #ifdef DEBUG
855                 kfree(t->name);
856 #endif
857                 kfree(t);
858         }
859 }
860
861 /*
862 * called from both kernel as from this kernel module (error flow on attach)
863 * precondition: perimeter lock is not acquired.
864 */
865 static void brcms_remove(struct bcma_device *pdev)
866 {
867         struct ieee80211_hw *hw = bcma_get_drvdata(pdev);
868         struct brcms_info *wl = hw->priv;
869
870         if (wl->wlc) {
871                 wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, false);
872                 wiphy_rfkill_stop_polling(wl->pub->ieee_hw->wiphy);
873                 ieee80211_unregister_hw(hw);
874         }
875
876         brcms_free(wl);
877
878         bcma_set_drvdata(pdev, NULL);
879         ieee80211_free_hw(hw);
880 }
881
882 static irqreturn_t brcms_isr(int irq, void *dev_id)
883 {
884         struct brcms_info *wl;
885         bool ours, wantdpc;
886
887         wl = (struct brcms_info *) dev_id;
888
889         spin_lock(&wl->isr_lock);
890
891         /* call common first level interrupt handler */
892         ours = brcms_c_isr(wl->wlc, &wantdpc);
893         if (ours) {
894                 /* if more to do... */
895                 if (wantdpc) {
896
897                         /* ...and call the second level interrupt handler */
898                         /* schedule dpc */
899                         tasklet_schedule(&wl->tasklet);
900                 }
901         }
902
903         spin_unlock(&wl->isr_lock);
904
905         return IRQ_RETVAL(ours);
906 }
907
908 /*
909  * is called in brcms_pci_probe() context, therefore no locking required.
910  */
911 static int ieee_hw_rate_init(struct ieee80211_hw *hw)
912 {
913         struct brcms_info *wl = hw->priv;
914         struct brcms_c_info *wlc = wl->wlc;
915         struct ieee80211_supported_band *band;
916         int has_5g = 0;
917         u16 phy_type;
918
919         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
920         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
921
922         phy_type = brcms_c_get_phy_type(wl->wlc, 0);
923         if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
924                 band = &wlc->bandstate[BAND_2G_INDEX]->band;
925                 *band = brcms_band_2GHz_nphy_template;
926                 if (phy_type == PHY_TYPE_LCN) {
927                         /* Single stream */
928                         band->ht_cap.mcs.rx_mask[1] = 0;
929                         band->ht_cap.mcs.rx_highest = cpu_to_le16(72);
930                 }
931                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
932         } else {
933                 return -EPERM;
934         }
935
936         /* Assume all bands use the same phy.  True for 11n devices. */
937         if (wl->pub->_nbands > 1) {
938                 has_5g++;
939                 if (phy_type == PHY_TYPE_N || phy_type == PHY_TYPE_LCN) {
940                         band = &wlc->bandstate[BAND_5G_INDEX]->band;
941                         *band = brcms_band_5GHz_nphy_template;
942                         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
943                 } else {
944                         return -EPERM;
945                 }
946         }
947         return 0;
948 }
949
950 /*
951  * is called in brcms_pci_probe() context, therefore no locking required.
952  */
953 static int ieee_hw_init(struct ieee80211_hw *hw)
954 {
955         hw->flags = IEEE80211_HW_SIGNAL_DBM
956             /* | IEEE80211_HW_CONNECTION_MONITOR  What is this? */
957             | IEEE80211_HW_REPORTS_TX_ACK_STATUS
958             | IEEE80211_HW_AMPDU_AGGREGATION;
959
960         hw->extra_tx_headroom = brcms_c_get_header_len();
961         hw->queues = N_TX_QUEUES;
962         hw->max_rates = 2;      /* Primary rate and 1 fallback rate */
963
964         /* channel change time is dependent on chip and band  */
965         hw->channel_change_time = 7 * 1000;
966         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
967
968         hw->rate_control_algorithm = "minstrel_ht";
969
970         hw->sta_data_size = 0;
971         return ieee_hw_rate_init(hw);
972 }
973
974 /**
975  * attach to the WL device.
976  *
977  * Attach to the WL device identified by vendor and device parameters.
978  * regs is a host accessible memory address pointing to WL device registers.
979  *
980  * brcms_attach is not defined as static because in the case where no bus
981  * is defined, wl_attach will never be called, and thus, gcc will issue
982  * a warning that this function is defined but not used if we declare
983  * it as static.
984  *
985  *
986  * is called in brcms_bcma_probe() context, therefore no locking required.
987  */
988 static struct brcms_info *brcms_attach(struct bcma_device *pdev)
989 {
990         struct brcms_info *wl = NULL;
991         int unit, err;
992         struct ieee80211_hw *hw;
993         u8 perm[ETH_ALEN];
994
995         unit = n_adapters_found;
996         err = 0;
997
998         if (unit < 0)
999                 return NULL;
1000
1001         /* allocate private info */
1002         hw = bcma_get_drvdata(pdev);
1003         if (hw != NULL)
1004                 wl = hw->priv;
1005         if (WARN_ON(hw == NULL) || WARN_ON(wl == NULL))
1006                 return NULL;
1007         wl->wiphy = hw->wiphy;
1008
1009         atomic_set(&wl->callbacks, 0);
1010
1011         /* setup the bottom half handler */
1012         tasklet_init(&wl->tasklet, brcms_dpc, (unsigned long) wl);
1013
1014         spin_lock_init(&wl->lock);
1015         spin_lock_init(&wl->isr_lock);
1016
1017         /* prepare ucode */
1018         if (brcms_request_fw(wl, pdev) < 0) {
1019                 wiphy_err(wl->wiphy, "%s: Failed to find firmware usually in "
1020                           "%s\n", KBUILD_MODNAME, "/lib/firmware/brcm");
1021                 brcms_release_fw(wl);
1022                 brcms_remove(pdev);
1023                 return NULL;
1024         }
1025
1026         /* common load-time initialization */
1027         wl->wlc = brcms_c_attach((void *)wl, pdev, unit, false, &err);
1028         brcms_release_fw(wl);
1029         if (!wl->wlc) {
1030                 wiphy_err(wl->wiphy, "%s: attach() failed with code %d\n",
1031                           KBUILD_MODNAME, err);
1032                 goto fail;
1033         }
1034         wl->pub = brcms_c_pub(wl->wlc);
1035
1036         wl->pub->ieee_hw = hw;
1037
1038         /* register our interrupt handler */
1039         if (request_irq(pdev->irq, brcms_isr,
1040                         IRQF_SHARED, KBUILD_MODNAME, wl)) {
1041                 wiphy_err(wl->wiphy, "wl%d: request_irq() failed\n", unit);
1042                 goto fail;
1043         }
1044         wl->irq = pdev->irq;
1045
1046         /* register module */
1047         brcms_c_module_register(wl->pub, "linux", wl, NULL);
1048
1049         if (ieee_hw_init(hw)) {
1050                 wiphy_err(wl->wiphy, "wl%d: %s: ieee_hw_init failed!\n", unit,
1051                           __func__);
1052                 goto fail;
1053         }
1054
1055         brcms_c_regd_init(wl->wlc);
1056
1057         memcpy(perm, &wl->pub->cur_etheraddr, ETH_ALEN);
1058         if (WARN_ON(!is_valid_ether_addr(perm)))
1059                 goto fail;
1060         SET_IEEE80211_PERM_ADDR(hw, perm);
1061
1062         err = ieee80211_register_hw(hw);
1063         if (err)
1064                 wiphy_err(wl->wiphy, "%s: ieee80211_register_hw failed, status"
1065                           "%d\n", __func__, err);
1066
1067         if (wl->pub->srom_ccode[0] &&
1068             regulatory_hint(wl->wiphy, wl->pub->srom_ccode))
1069                 wiphy_err(wl->wiphy, "%s: regulatory hint failed\n", __func__);
1070
1071         n_adapters_found++;
1072         return wl;
1073
1074 fail:
1075         brcms_free(wl);
1076         return NULL;
1077 }
1078
1079
1080
1081 /**
1082  * determines if a device is a WL device, and if so, attaches it.
1083  *
1084  * This function determines if a device pointed to by pdev is a WL device,
1085  * and if so, performs a brcms_attach() on it.
1086  *
1087  * Perimeter lock is initialized in the course of this function.
1088  */
1089 static int __devinit brcms_bcma_probe(struct bcma_device *pdev)
1090 {
1091         struct brcms_info *wl;
1092         struct ieee80211_hw *hw;
1093
1094         dev_info(&pdev->dev, "mfg %x core %x rev %d class %d irq %d\n",
1095                  pdev->id.manuf, pdev->id.id, pdev->id.rev, pdev->id.class,
1096                  pdev->irq);
1097
1098         if ((pdev->id.manuf != BCMA_MANUF_BCM) ||
1099             (pdev->id.id != BCMA_CORE_80211))
1100                 return -ENODEV;
1101
1102         hw = ieee80211_alloc_hw(sizeof(struct brcms_info), &brcms_ops);
1103         if (!hw) {
1104                 pr_err("%s: ieee80211_alloc_hw failed\n", __func__);
1105                 return -ENOMEM;
1106         }
1107
1108         SET_IEEE80211_DEV(hw, &pdev->dev);
1109
1110         bcma_set_drvdata(pdev, hw);
1111
1112         memset(hw->priv, 0, sizeof(*wl));
1113
1114         wl = brcms_attach(pdev);
1115         if (!wl) {
1116                 pr_err("%s: brcms_attach failed!\n", __func__);
1117                 return -ENODEV;
1118         }
1119         return 0;
1120 }
1121
1122 static int brcms_suspend(struct bcma_device *pdev)
1123 {
1124         struct brcms_info *wl;
1125         struct ieee80211_hw *hw;
1126
1127         hw = bcma_get_drvdata(pdev);
1128         wl = hw->priv;
1129         if (!wl) {
1130                 pr_err("%s: %s: no driver private struct!\n", KBUILD_MODNAME,
1131                        __func__);
1132                 return -ENODEV;
1133         }
1134
1135         /* only need to flag hw is down for proper resume */
1136         spin_lock_bh(&wl->lock);
1137         wl->pub->hw_up = false;
1138         spin_unlock_bh(&wl->lock);
1139
1140         pr_debug("brcms_suspend ok\n");
1141
1142         return 0;
1143 }
1144
1145 static int brcms_resume(struct bcma_device *pdev)
1146 {
1147         pr_debug("brcms_resume ok\n");
1148         return 0;
1149 }
1150
1151 static struct bcma_driver brcms_bcma_driver = {
1152         .name     = KBUILD_MODNAME,
1153         .probe    = brcms_bcma_probe,
1154         .suspend  = brcms_suspend,
1155         .resume   = brcms_resume,
1156         .remove   = __devexit_p(brcms_remove),
1157         .id_table = brcms_coreid_table,
1158 };
1159
1160 /**
1161  * This is the main entry point for the brcmsmac driver.
1162  *
1163  * This function is scheduled upon module initialization and
1164  * does the driver registration, which result in brcms_bcma_probe()
1165  * call resulting in the driver bringup.
1166  */
1167 static void brcms_driver_init(struct work_struct *work)
1168 {
1169         int error;
1170
1171         error = bcma_driver_register(&brcms_bcma_driver);
1172         if (error)
1173                 pr_err("%s: register returned %d\n", __func__, error);
1174 }
1175
1176 static DECLARE_WORK(brcms_driver_work, brcms_driver_init);
1177
1178 static int __init brcms_module_init(void)
1179 {
1180 #ifdef DEBUG
1181         if (msglevel != 0xdeadbeef)
1182                 brcm_msg_level = msglevel;
1183 #endif
1184         if (!schedule_work(&brcms_driver_work))
1185                 return -EBUSY;
1186
1187         return 0;
1188 }
1189
1190 /**
1191  * This function unloads the brcmsmac driver from the system.
1192  *
1193  * This function unconditionally unloads the brcmsmac driver module from the
1194  * system.
1195  *
1196  */
1197 static void __exit brcms_module_exit(void)
1198 {
1199         cancel_work_sync(&brcms_driver_work);
1200         bcma_driver_unregister(&brcms_bcma_driver);
1201 }
1202
1203 module_init(brcms_module_init);
1204 module_exit(brcms_module_exit);
1205
1206 /*
1207  * precondition: perimeter lock has been acquired
1208  */
1209 void brcms_txflowcontrol(struct brcms_info *wl, struct brcms_if *wlif,
1210                          bool state, int prio)
1211 {
1212         wiphy_err(wl->wiphy, "Shouldn't be here %s\n", __func__);
1213 }
1214
1215 /*
1216  * precondition: perimeter lock has been acquired
1217  */
1218 void brcms_init(struct brcms_info *wl)
1219 {
1220         BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
1221         brcms_reset(wl);
1222         brcms_c_init(wl->wlc, wl->mute_tx);
1223 }
1224
1225 /*
1226  * precondition: perimeter lock has been acquired
1227  */
1228 uint brcms_reset(struct brcms_info *wl)
1229 {
1230         BCMMSG(wl->pub->ieee_hw->wiphy, "wl%d\n", wl->pub->unit);
1231         brcms_c_reset(wl->wlc);
1232
1233         /* dpc will not be rescheduled */
1234         wl->resched = false;
1235
1236         /* inform publicly that interface is down */
1237         wl->pub->up = false;
1238
1239         return 0;
1240 }
1241
1242 void brcms_fatal_error(struct brcms_info *wl)
1243 {
1244         wiphy_err(wl->wlc->wiphy, "wl%d: fatal error, reinitializing\n",
1245                   wl->wlc->pub->unit);
1246         brcms_reset(wl);
1247         ieee80211_restart_hw(wl->pub->ieee_hw);
1248 }
1249
1250 /*
1251  * These are interrupt on/off entry points. Disable interrupts
1252  * during interrupt state transition.
1253  */
1254 void brcms_intrson(struct brcms_info *wl)
1255 {
1256         unsigned long flags;
1257
1258         spin_lock_irqsave(&wl->isr_lock, flags);
1259         brcms_c_intrson(wl->wlc);
1260         spin_unlock_irqrestore(&wl->isr_lock, flags);
1261 }
1262
1263 u32 brcms_intrsoff(struct brcms_info *wl)
1264 {
1265         unsigned long flags;
1266         u32 status;
1267
1268         spin_lock_irqsave(&wl->isr_lock, flags);
1269         status = brcms_c_intrsoff(wl->wlc);
1270         spin_unlock_irqrestore(&wl->isr_lock, flags);
1271         return status;
1272 }
1273
1274 void brcms_intrsrestore(struct brcms_info *wl, u32 macintmask)
1275 {
1276         unsigned long flags;
1277
1278         spin_lock_irqsave(&wl->isr_lock, flags);
1279         brcms_c_intrsrestore(wl->wlc, macintmask);
1280         spin_unlock_irqrestore(&wl->isr_lock, flags);
1281 }
1282
1283 /*
1284  * precondition: perimeter lock has been acquired
1285  */
1286 int brcms_up(struct brcms_info *wl)
1287 {
1288         int error = 0;
1289
1290         if (wl->pub->up)
1291                 return 0;
1292
1293         error = brcms_c_up(wl->wlc);
1294
1295         return error;
1296 }
1297
1298 /*
1299  * precondition: perimeter lock has been acquired
1300  */
1301 void brcms_down(struct brcms_info *wl)
1302 {
1303         uint callbacks, ret_val = 0;
1304
1305         /* call common down function */
1306         ret_val = brcms_c_down(wl->wlc);
1307         callbacks = atomic_read(&wl->callbacks) - ret_val;
1308
1309         /* wait for down callbacks to complete */
1310         spin_unlock_bh(&wl->lock);
1311
1312         /* For HIGH_only driver, it's important to actually schedule other work,
1313          * not just spin wait since everything runs at schedule level
1314          */
1315         SPINWAIT((atomic_read(&wl->callbacks) > callbacks), 100 * 1000);
1316
1317         spin_lock_bh(&wl->lock);
1318 }
1319
1320 /*
1321 * precondition: perimeter lock is not acquired
1322  */
1323 static void _brcms_timer(struct work_struct *work)
1324 {
1325         struct brcms_timer *t = container_of(work, struct brcms_timer,
1326                                              dly_wrk.work);
1327
1328         spin_lock_bh(&t->wl->lock);
1329
1330         if (t->set) {
1331                 if (t->periodic) {
1332                         atomic_inc(&t->wl->callbacks);
1333                         ieee80211_queue_delayed_work(t->wl->pub->ieee_hw,
1334                                                      &t->dly_wrk,
1335                                                      msecs_to_jiffies(t->ms));
1336                 } else {
1337                         t->set = false;
1338                 }
1339
1340                 t->fn(t->arg);
1341         }
1342
1343         atomic_dec(&t->wl->callbacks);
1344
1345         spin_unlock_bh(&t->wl->lock);
1346 }
1347
1348 /*
1349  * Adds a timer to the list. Caller supplies a timer function.
1350  * Is called from wlc.
1351  *
1352  * precondition: perimeter lock has been acquired
1353  */
1354 struct brcms_timer *brcms_init_timer(struct brcms_info *wl,
1355                                      void (*fn) (void *arg),
1356                                      void *arg, const char *name)
1357 {
1358         struct brcms_timer *t;
1359
1360         t = kzalloc(sizeof(struct brcms_timer), GFP_ATOMIC);
1361         if (!t)
1362                 return NULL;
1363
1364         INIT_DELAYED_WORK(&t->dly_wrk, _brcms_timer);
1365         t->wl = wl;
1366         t->fn = fn;
1367         t->arg = arg;
1368         t->next = wl->timers;
1369         wl->timers = t;
1370
1371 #ifdef DEBUG
1372         t->name = kmalloc(strlen(name) + 1, GFP_ATOMIC);
1373         if (t->name)
1374                 strcpy(t->name, name);
1375 #endif
1376
1377         return t;
1378 }
1379
1380 /*
1381  * adds only the kernel timer since it's going to be more accurate
1382  * as well as it's easier to make it periodic
1383  *
1384  * precondition: perimeter lock has been acquired
1385  */
1386 void brcms_add_timer(struct brcms_timer *t, uint ms, int periodic)
1387 {
1388         struct ieee80211_hw *hw = t->wl->pub->ieee_hw;
1389
1390 #ifdef DEBUG
1391         if (t->set)
1392                 wiphy_err(hw->wiphy, "%s: Already set. Name: %s, per %d\n",
1393                           __func__, t->name, periodic);
1394 #endif
1395         t->ms = ms;
1396         t->periodic = (bool) periodic;
1397         t->set = true;
1398
1399         atomic_inc(&t->wl->callbacks);
1400
1401         ieee80211_queue_delayed_work(hw, &t->dly_wrk, msecs_to_jiffies(ms));
1402 }
1403
1404 /*
1405  * return true if timer successfully deleted, false if still pending
1406  *
1407  * precondition: perimeter lock has been acquired
1408  */
1409 bool brcms_del_timer(struct brcms_timer *t)
1410 {
1411         if (t->set) {
1412                 t->set = false;
1413                 if (!cancel_delayed_work(&t->dly_wrk))
1414                         return false;
1415
1416                 atomic_dec(&t->wl->callbacks);
1417         }
1418
1419         return true;
1420 }
1421
1422 /*
1423  * precondition: perimeter lock has been acquired
1424  */
1425 void brcms_free_timer(struct brcms_timer *t)
1426 {
1427         struct brcms_info *wl = t->wl;
1428         struct brcms_timer *tmp;
1429
1430         /* delete the timer in case it is active */
1431         brcms_del_timer(t);
1432
1433         if (wl->timers == t) {
1434                 wl->timers = wl->timers->next;
1435 #ifdef DEBUG
1436                 kfree(t->name);
1437 #endif
1438                 kfree(t);
1439                 return;
1440
1441         }
1442
1443         tmp = wl->timers;
1444         while (tmp) {
1445                 if (tmp->next == t) {
1446                         tmp->next = t->next;
1447 #ifdef DEBUG
1448                         kfree(t->name);
1449 #endif
1450                         kfree(t);
1451                         return;
1452                 }
1453                 tmp = tmp->next;
1454         }
1455
1456 }
1457
1458 /*
1459  * precondition: perimeter lock has been acquired
1460  */
1461 int brcms_ucode_init_buf(struct brcms_info *wl, void **pbuf, u32 idx)
1462 {
1463         int i, entry;
1464         const u8 *pdata;
1465         struct firmware_hdr *hdr;
1466         for (i = 0; i < wl->fw.fw_cnt; i++) {
1467                 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1468                 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1469                      entry++, hdr++) {
1470                         u32 len = le32_to_cpu(hdr->len);
1471                         if (le32_to_cpu(hdr->idx) == idx) {
1472                                 pdata = wl->fw.fw_bin[i]->data +
1473                                         le32_to_cpu(hdr->offset);
1474                                 *pbuf = kmemdup(pdata, len, GFP_ATOMIC);
1475                                 if (*pbuf == NULL)
1476                                         goto fail;
1477
1478                                 return 0;
1479                         }
1480                 }
1481         }
1482         wiphy_err(wl->wiphy, "ERROR: ucode buf tag:%d can not be found!\n",
1483                   idx);
1484         *pbuf = NULL;
1485 fail:
1486         return -ENODATA;
1487 }
1488
1489 /*
1490  * Precondition: Since this function is called in brcms_bcma_probe() context,
1491  * no locking is required.
1492  */
1493 int brcms_ucode_init_uint(struct brcms_info *wl, size_t *n_bytes, u32 idx)
1494 {
1495         int i, entry;
1496         const u8 *pdata;
1497         struct firmware_hdr *hdr;
1498         for (i = 0; i < wl->fw.fw_cnt; i++) {
1499                 hdr = (struct firmware_hdr *)wl->fw.fw_hdr[i]->data;
1500                 for (entry = 0; entry < wl->fw.hdr_num_entries[i];
1501                      entry++, hdr++) {
1502                         if (le32_to_cpu(hdr->idx) == idx) {
1503                                 pdata = wl->fw.fw_bin[i]->data +
1504                                         le32_to_cpu(hdr->offset);
1505                                 if (le32_to_cpu(hdr->len) != 4) {
1506                                         wiphy_err(wl->wiphy,
1507                                                   "ERROR: fw hdr len\n");
1508                                         return -ENOMSG;
1509                                 }
1510                                 *n_bytes = le32_to_cpu(*((__le32 *) pdata));
1511                                 return 0;
1512                         }
1513                 }
1514         }
1515         wiphy_err(wl->wiphy, "ERROR: ucode tag:%d can not be found!\n", idx);
1516         return -ENOMSG;
1517 }
1518
1519 /*
1520  * precondition: can both be called locked and unlocked
1521  */
1522 void brcms_ucode_free_buf(void *p)
1523 {
1524         kfree(p);
1525 }
1526
1527 /*
1528  * checks validity of all firmware images loaded from user space
1529  *
1530  * Precondition: Since this function is called in brcms_bcma_probe() context,
1531  * no locking is required.
1532  */
1533 int brcms_check_firmwares(struct brcms_info *wl)
1534 {
1535         int i;
1536         int entry;
1537         int rc = 0;
1538         const struct firmware *fw;
1539         const struct firmware *fw_hdr;
1540         struct firmware_hdr *ucode_hdr;
1541         for (i = 0; i < MAX_FW_IMAGES && rc == 0; i++) {
1542                 fw =  wl->fw.fw_bin[i];
1543                 fw_hdr = wl->fw.fw_hdr[i];
1544                 if (fw == NULL && fw_hdr == NULL) {
1545                         break;
1546                 } else if (fw == NULL || fw_hdr == NULL) {
1547                         wiphy_err(wl->wiphy, "%s: invalid bin/hdr fw\n",
1548                                   __func__);
1549                         rc = -EBADF;
1550                 } else if (fw_hdr->size % sizeof(struct firmware_hdr)) {
1551                         wiphy_err(wl->wiphy, "%s: non integral fw hdr file "
1552                                 "size %zu/%zu\n", __func__, fw_hdr->size,
1553                                 sizeof(struct firmware_hdr));
1554                         rc = -EBADF;
1555                 } else if (fw->size < MIN_FW_SIZE || fw->size > MAX_FW_SIZE) {
1556                         wiphy_err(wl->wiphy, "%s: out of bounds fw file size "
1557                                   "%zu\n", __func__, fw->size);
1558                         rc = -EBADF;
1559                 } else {
1560                         /* check if ucode section overruns firmware image */
1561                         ucode_hdr = (struct firmware_hdr *)fw_hdr->data;
1562                         for (entry = 0; entry < wl->fw.hdr_num_entries[i] &&
1563                              !rc; entry++, ucode_hdr++) {
1564                                 if (le32_to_cpu(ucode_hdr->offset) +
1565                                     le32_to_cpu(ucode_hdr->len) >
1566                                     fw->size) {
1567                                         wiphy_err(wl->wiphy,
1568                                                   "%s: conflicting bin/hdr\n",
1569                                                   __func__);
1570                                         rc = -EBADF;
1571                                 }
1572                         }
1573                 }
1574         }
1575         if (rc == 0 && wl->fw.fw_cnt != i) {
1576                 wiphy_err(wl->wiphy, "%s: invalid fw_cnt=%d\n", __func__,
1577                         wl->fw.fw_cnt);
1578                 rc = -EBADF;
1579         }
1580         return rc;
1581 }
1582
1583 /*
1584  * precondition: perimeter lock has been acquired
1585  */
1586 bool brcms_rfkill_set_hw_state(struct brcms_info *wl)
1587 {
1588         bool blocked = brcms_c_check_radio_disabled(wl->wlc);
1589
1590         spin_unlock_bh(&wl->lock);
1591         wiphy_rfkill_set_hw_state(wl->pub->ieee_hw->wiphy, blocked);
1592         if (blocked)
1593                 wiphy_rfkill_start_polling(wl->pub->ieee_hw->wiphy);
1594         spin_lock_bh(&wl->lock);
1595         return blocked;
1596 }
1597
1598 /*
1599  * precondition: perimeter lock has been acquired
1600  */
1601 void brcms_msleep(struct brcms_info *wl, uint ms)
1602 {
1603         spin_unlock_bh(&wl->lock);
1604         msleep(ms);
1605         spin_lock_bh(&wl->lock);
1606 }