blob: d1c397162a6ac9dfb3e4e0c66b0f50912f89319e [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio1f21ad22007-11-06 22:49:20 +01006 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Michael Büscheb032b92011-07-04 20:50:05 +02007 Copyright (c) 2005-2009 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -04008 Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
Rafał Miłecki108f4f32011-09-03 21:01:02 +020010 Copyright (c) 2010-2011 Rafał Miłecki <zajec5@gmail.com>
Michael Buesche4d6b792007-09-18 15:39:42 -040011
Albert Herranz3dbba8e2009-09-10 19:34:49 +020012 SDIO support
13 Copyright (c) 2009 Albert Herranz <albert_herranz@yahoo.es>
14
Michael Buesche4d6b792007-09-18 15:39:42 -040015 Some parts of the code in this file are derived from the ipw2200
16 driver Copyright(c) 2003 - 2004 Intel Corporation.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; see the file COPYING. If not, write to
30 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
31 Boston, MA 02110-1301, USA.
32
33*/
34
35#include <linux/delay.h>
36#include <linux/init.h>
Paul Gortmakerac5c24e92011-08-30 14:18:44 -040037#include <linux/module.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040038#include <linux/if_arp.h>
39#include <linux/etherdevice.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040040#include <linux/firmware.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040041#include <linux/workqueue.h>
42#include <linux/skbuff.h>
Andrew Morton96cf49a2008-02-04 22:27:19 -080043#include <linux/io.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040044#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040046#include <asm/unaligned.h>
47
48#include "b43.h"
49#include "main.h"
50#include "debugfs.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020051#include "phy_common.h"
52#include "phy_g.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020053#include "phy_n.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040054#include "dma.h"
Michael Buesch5100d5a2008-03-29 21:01:16 +010055#include "pio.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040056#include "sysfs.h"
57#include "xmit.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040058#include "lo.h"
59#include "pcmcia.h"
Albert Herranz3dbba8e2009-09-10 19:34:49 +020060#include "sdio.h"
61#include <linux/mmc/sdio_func.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040062
63MODULE_DESCRIPTION("Broadcom B43 wireless driver");
64MODULE_AUTHOR("Martin Langer");
65MODULE_AUTHOR("Stefano Brivio");
66MODULE_AUTHOR("Michael Buesch");
Gábor Stefanik0136e512009-08-28 22:32:17 +020067MODULE_AUTHOR("Gábor Stefanik");
Rafał Miłecki108f4f32011-09-03 21:01:02 +020068MODULE_AUTHOR("Rafał Miłecki");
Michael Buesche4d6b792007-09-18 15:39:42 -040069MODULE_LICENSE("GPL");
70
Tim Gardner6021e082010-01-07 11:10:38 -070071MODULE_FIRMWARE("b43/ucode11.fw");
72MODULE_FIRMWARE("b43/ucode13.fw");
73MODULE_FIRMWARE("b43/ucode14.fw");
74MODULE_FIRMWARE("b43/ucode15.fw");
Rafał Miłeckif6158392011-04-19 22:49:29 +020075MODULE_FIRMWARE("b43/ucode16_mimo.fw");
Tim Gardner6021e082010-01-07 11:10:38 -070076MODULE_FIRMWARE("b43/ucode5.fw");
77MODULE_FIRMWARE("b43/ucode9.fw");
Michael Buesche4d6b792007-09-18 15:39:42 -040078
79static int modparam_bad_frames_preempt;
80module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
81MODULE_PARM_DESC(bad_frames_preempt,
82 "enable(1) / disable(0) Bad Frames Preemption");
83
Michael Buesche4d6b792007-09-18 15:39:42 -040084static char modparam_fwpostfix[16];
85module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
86MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
87
Michael Buesche4d6b792007-09-18 15:39:42 -040088static int modparam_hwpctl;
89module_param_named(hwpctl, modparam_hwpctl, int, 0444);
90MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
91
92static int modparam_nohwcrypt;
93module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
94MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
95
gregor kowski035d0242009-08-19 22:35:45 +020096static int modparam_hwtkip;
97module_param_named(hwtkip, modparam_hwtkip, int, 0444);
98MODULE_PARM_DESC(hwtkip, "Enable hardware tkip.");
99
Michael Buesch403a3a12009-06-08 21:04:57 +0200100static int modparam_qos = 1;
101module_param_named(qos, modparam_qos, int, 0444);
Michael Buesche6f5b932008-03-05 21:18:49 +0100102MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
103
Michael Buesch1855ba72008-04-18 20:51:41 +0200104static int modparam_btcoex = 1;
105module_param_named(btcoex, modparam_btcoex, int, 0444);
Gábor Stefanikc71dbd32009-08-28 22:34:21 +0200106MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistence (default on)");
Michael Buesch1855ba72008-04-18 20:51:41 +0200107
Michael Buesch060210f2009-01-25 15:49:59 +0100108int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
109module_param_named(verbose, b43_modparam_verbose, int, 0644);
110MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
111
Rafał Miłeckidf766262011-08-16 12:14:07 +0200112static int b43_modparam_pio = 0;
Linus Torvalds9e3bd912010-02-26 10:34:27 -0800113module_param_named(pio, b43_modparam_pio, int, 0644);
114MODULE_PARM_DESC(pio, "Use PIO accesses by default: 0=DMA, 1=PIO");
Michael Buesche6f5b932008-03-05 21:18:49 +0100115
Rafał Miłecki89604002013-06-26 09:55:54 +0200116static int modparam_allhwsupport = !IS_ENABLED(CONFIG_BRCMSMAC);
117module_param_named(allhwsupport, modparam_allhwsupport, int, 0444);
118MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the brcmsmac driver)");
119
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200120#ifdef CONFIG_B43_BCMA
121static const struct bcma_device_id b43_bcma_tbl[] = {
Hauke Mehrtensc027ed42011-07-23 13:57:34 +0200122 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x11, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200123 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x17, BCMA_ANY_CLASS),
124 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x18, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200125 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1C, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200126 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1D, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200127 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1E, BCMA_ANY_CLASS),
128 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x28, BCMA_ANY_CLASS),
129 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x2A, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200130 BCMA_CORETABLE_END
131};
132MODULE_DEVICE_TABLE(bcma, b43_bcma_tbl);
133#endif
134
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200135#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -0400136static const struct ssb_device_id b43_ssb_tbl[] = {
137 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
138 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
139 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
140 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
141 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
Michael Bueschd5c71e42008-01-04 17:06:29 +0100142 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
Rafał Miłecki003d6d22010-01-15 12:10:53 +0100143 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 12),
Larry Finger013978b2007-11-26 10:29:47 -0600144 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
Michael Buesch6b1c7c62008-12-25 00:39:28 +0100145 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
Johannes Berg92d61282008-12-24 12:44:09 +0100146 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
Michael Buesche4d6b792007-09-18 15:39:42 -0400147 SSB_DEVTABLE_END
148};
Michael Buesche4d6b792007-09-18 15:39:42 -0400149MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200150#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400151
152/* Channel and ratetables are shared for all devices.
153 * They can't be const, because ieee80211 puts some precalculated
154 * data in there. This data is the same for all devices, so we don't
155 * get concurrency issues */
156#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +0100157 { \
158 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
159 .hw_value = (_rateid), \
160 .flags = (_flags), \
Michael Buesche4d6b792007-09-18 15:39:42 -0400161 }
Johannes Berg8318d782008-01-24 19:38:38 +0100162
163/*
164 * NOTE: When changing this, sync with xmit.c's
165 * b43_plcp_get_bitrate_idx_* functions!
166 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400167static struct ieee80211_rate __b43_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100168 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
169 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
170 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
171 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
172 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
173 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
174 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
175 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
176 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
177 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
178 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
179 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400180};
181
182#define b43_a_ratetable (__b43_ratetable + 4)
183#define b43_a_ratetable_size 8
184#define b43_b_ratetable (__b43_ratetable + 0)
185#define b43_b_ratetable_size 4
186#define b43_g_ratetable (__b43_ratetable + 0)
187#define b43_g_ratetable_size 12
188
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200189#define CHAN2G(_channel, _freq, _flags) { \
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100190 .band = IEEE80211_BAND_2GHZ, \
191 .center_freq = (_freq), \
192 .hw_value = (_channel), \
193 .flags = (_flags), \
194 .max_antenna_gain = 0, \
195 .max_power = 30, \
196}
Michael Buesch96c755a2008-01-06 00:09:46 +0100197static struct ieee80211_channel b43_2ghz_chantable[] = {
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200198 CHAN2G(1, 2412, 0),
199 CHAN2G(2, 2417, 0),
200 CHAN2G(3, 2422, 0),
201 CHAN2G(4, 2427, 0),
202 CHAN2G(5, 2432, 0),
203 CHAN2G(6, 2437, 0),
204 CHAN2G(7, 2442, 0),
205 CHAN2G(8, 2447, 0),
206 CHAN2G(9, 2452, 0),
207 CHAN2G(10, 2457, 0),
208 CHAN2G(11, 2462, 0),
209 CHAN2G(12, 2467, 0),
210 CHAN2G(13, 2472, 0),
211 CHAN2G(14, 2484, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100212};
Rafał Miłecki3695b932014-07-08 15:11:10 +0200213
214/* No support for the last 3 channels (12, 13, 14) */
215#define b43_2ghz_chantable_limited_size 11
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200216#undef CHAN2G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100217
Rafał Miłecki91211732014-05-21 08:44:20 +0200218#define CHAN4G(_channel, _flags) { \
219 .band = IEEE80211_BAND_5GHZ, \
220 .center_freq = 4000 + (5 * (_channel)), \
221 .hw_value = (_channel), \
222 .flags = (_flags), \
223 .max_antenna_gain = 0, \
224 .max_power = 30, \
225}
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100226#define CHAN5G(_channel, _flags) { \
227 .band = IEEE80211_BAND_5GHZ, \
228 .center_freq = 5000 + (5 * (_channel)), \
229 .hw_value = (_channel), \
230 .flags = (_flags), \
231 .max_antenna_gain = 0, \
232 .max_power = 30, \
233}
234static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
Rafał Miłecki91211732014-05-21 08:44:20 +0200235 CHAN4G(184, 0), CHAN4G(186, 0),
236 CHAN4G(188, 0), CHAN4G(190, 0),
237 CHAN4G(192, 0), CHAN4G(194, 0),
238 CHAN4G(196, 0), CHAN4G(198, 0),
239 CHAN4G(200, 0), CHAN4G(202, 0),
240 CHAN4G(204, 0), CHAN4G(206, 0),
241 CHAN4G(208, 0), CHAN4G(210, 0),
242 CHAN4G(212, 0), CHAN4G(214, 0),
243 CHAN4G(216, 0), CHAN4G(218, 0),
244 CHAN4G(220, 0), CHAN4G(222, 0),
245 CHAN4G(224, 0), CHAN4G(226, 0),
246 CHAN4G(228, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100247 CHAN5G(32, 0), CHAN5G(34, 0),
248 CHAN5G(36, 0), CHAN5G(38, 0),
249 CHAN5G(40, 0), CHAN5G(42, 0),
250 CHAN5G(44, 0), CHAN5G(46, 0),
251 CHAN5G(48, 0), CHAN5G(50, 0),
252 CHAN5G(52, 0), CHAN5G(54, 0),
253 CHAN5G(56, 0), CHAN5G(58, 0),
254 CHAN5G(60, 0), CHAN5G(62, 0),
255 CHAN5G(64, 0), CHAN5G(66, 0),
256 CHAN5G(68, 0), CHAN5G(70, 0),
257 CHAN5G(72, 0), CHAN5G(74, 0),
258 CHAN5G(76, 0), CHAN5G(78, 0),
259 CHAN5G(80, 0), CHAN5G(82, 0),
260 CHAN5G(84, 0), CHAN5G(86, 0),
261 CHAN5G(88, 0), CHAN5G(90, 0),
262 CHAN5G(92, 0), CHAN5G(94, 0),
263 CHAN5G(96, 0), CHAN5G(98, 0),
264 CHAN5G(100, 0), CHAN5G(102, 0),
265 CHAN5G(104, 0), CHAN5G(106, 0),
266 CHAN5G(108, 0), CHAN5G(110, 0),
267 CHAN5G(112, 0), CHAN5G(114, 0),
268 CHAN5G(116, 0), CHAN5G(118, 0),
269 CHAN5G(120, 0), CHAN5G(122, 0),
270 CHAN5G(124, 0), CHAN5G(126, 0),
271 CHAN5G(128, 0), CHAN5G(130, 0),
272 CHAN5G(132, 0), CHAN5G(134, 0),
273 CHAN5G(136, 0), CHAN5G(138, 0),
274 CHAN5G(140, 0), CHAN5G(142, 0),
275 CHAN5G(144, 0), CHAN5G(145, 0),
276 CHAN5G(146, 0), CHAN5G(147, 0),
277 CHAN5G(148, 0), CHAN5G(149, 0),
278 CHAN5G(150, 0), CHAN5G(151, 0),
279 CHAN5G(152, 0), CHAN5G(153, 0),
280 CHAN5G(154, 0), CHAN5G(155, 0),
281 CHAN5G(156, 0), CHAN5G(157, 0),
282 CHAN5G(158, 0), CHAN5G(159, 0),
283 CHAN5G(160, 0), CHAN5G(161, 0),
284 CHAN5G(162, 0), CHAN5G(163, 0),
285 CHAN5G(164, 0), CHAN5G(165, 0),
286 CHAN5G(166, 0), CHAN5G(168, 0),
287 CHAN5G(170, 0), CHAN5G(172, 0),
288 CHAN5G(174, 0), CHAN5G(176, 0),
289 CHAN5G(178, 0), CHAN5G(180, 0),
Rafał Miłecki91211732014-05-21 08:44:20 +0200290 CHAN5G(182, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400291};
292
Rafał Miłeckib453fda62014-07-23 18:54:49 +0200293static struct ieee80211_channel b43_5ghz_nphy_chantable_limited[] = {
294 CHAN5G(36, 0), CHAN5G(40, 0),
295 CHAN5G(44, 0), CHAN5G(48, 0),
296 CHAN5G(149, 0), CHAN5G(153, 0),
297 CHAN5G(157, 0), CHAN5G(161, 0),
298 CHAN5G(165, 0),
299};
300
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100301static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
302 CHAN5G(34, 0), CHAN5G(36, 0),
303 CHAN5G(38, 0), CHAN5G(40, 0),
304 CHAN5G(42, 0), CHAN5G(44, 0),
305 CHAN5G(46, 0), CHAN5G(48, 0),
306 CHAN5G(52, 0), CHAN5G(56, 0),
307 CHAN5G(60, 0), CHAN5G(64, 0),
308 CHAN5G(100, 0), CHAN5G(104, 0),
309 CHAN5G(108, 0), CHAN5G(112, 0),
310 CHAN5G(116, 0), CHAN5G(120, 0),
311 CHAN5G(124, 0), CHAN5G(128, 0),
312 CHAN5G(132, 0), CHAN5G(136, 0),
313 CHAN5G(140, 0), CHAN5G(149, 0),
314 CHAN5G(153, 0), CHAN5G(157, 0),
315 CHAN5G(161, 0), CHAN5G(165, 0),
316 CHAN5G(184, 0), CHAN5G(188, 0),
317 CHAN5G(192, 0), CHAN5G(196, 0),
318 CHAN5G(200, 0), CHAN5G(204, 0),
319 CHAN5G(208, 0), CHAN5G(212, 0),
320 CHAN5G(216, 0),
321};
Rafał Miłecki91211732014-05-21 08:44:20 +0200322#undef CHAN4G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100323#undef CHAN5G
324
325static struct ieee80211_supported_band b43_band_5GHz_nphy = {
326 .band = IEEE80211_BAND_5GHZ,
327 .channels = b43_5ghz_nphy_chantable,
328 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
329 .bitrates = b43_a_ratetable,
330 .n_bitrates = b43_a_ratetable_size,
Michael Buesche4d6b792007-09-18 15:39:42 -0400331};
Johannes Berg8318d782008-01-24 19:38:38 +0100332
Rafał Miłeckib453fda62014-07-23 18:54:49 +0200333static struct ieee80211_supported_band b43_band_5GHz_nphy_limited = {
334 .band = IEEE80211_BAND_5GHZ,
335 .channels = b43_5ghz_nphy_chantable_limited,
336 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable_limited),
337 .bitrates = b43_a_ratetable,
338 .n_bitrates = b43_a_ratetable_size,
339};
340
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100341static struct ieee80211_supported_band b43_band_5GHz_aphy = {
342 .band = IEEE80211_BAND_5GHZ,
343 .channels = b43_5ghz_aphy_chantable,
344 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
345 .bitrates = b43_a_ratetable,
346 .n_bitrates = b43_a_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100347};
Michael Buesche4d6b792007-09-18 15:39:42 -0400348
Johannes Berg8318d782008-01-24 19:38:38 +0100349static struct ieee80211_supported_band b43_band_2GHz = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100350 .band = IEEE80211_BAND_2GHZ,
351 .channels = b43_2ghz_chantable,
352 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
353 .bitrates = b43_g_ratetable,
354 .n_bitrates = b43_g_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100355};
356
Rafał Miłecki3695b932014-07-08 15:11:10 +0200357static struct ieee80211_supported_band b43_band_2ghz_limited = {
358 .band = IEEE80211_BAND_2GHZ,
359 .channels = b43_2ghz_chantable,
360 .n_channels = b43_2ghz_chantable_limited_size,
361 .bitrates = b43_g_ratetable,
362 .n_bitrates = b43_g_ratetable_size,
363};
364
Michael Buesche4d6b792007-09-18 15:39:42 -0400365static void b43_wireless_core_exit(struct b43_wldev *dev);
366static int b43_wireless_core_init(struct b43_wldev *dev);
Michael Buesch36dbd952009-09-04 22:51:29 +0200367static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400368static int b43_wireless_core_start(struct b43_wldev *dev);
Felix Fietkau2a190322011-08-10 13:50:30 -0600369static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
370 struct ieee80211_vif *vif,
371 struct ieee80211_bss_conf *conf,
372 u32 changed);
Michael Buesche4d6b792007-09-18 15:39:42 -0400373
374static int b43_ratelimit(struct b43_wl *wl)
375{
376 if (!wl || !wl->current_dev)
377 return 1;
378 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
379 return 1;
380 /* We are up and running.
381 * Ratelimit the messages to avoid DoS over the net. */
382 return net_ratelimit();
383}
384
385void b43info(struct b43_wl *wl, const char *fmt, ...)
386{
Joe Perches5b736d42010-11-09 16:35:18 -0800387 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400388 va_list args;
389
Michael Buesch060210f2009-01-25 15:49:59 +0100390 if (b43_modparam_verbose < B43_VERBOSITY_INFO)
391 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400392 if (!b43_ratelimit(wl))
393 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800394
Michael Buesche4d6b792007-09-18 15:39:42 -0400395 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800396
397 vaf.fmt = fmt;
398 vaf.va = &args;
399
400 printk(KERN_INFO "b43-%s: %pV",
401 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
402
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 va_end(args);
404}
405
406void b43err(struct b43_wl *wl, const char *fmt, ...)
407{
Joe Perches5b736d42010-11-09 16:35:18 -0800408 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400409 va_list args;
410
Michael Buesch060210f2009-01-25 15:49:59 +0100411 if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
412 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400413 if (!b43_ratelimit(wl))
414 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800415
Michael Buesche4d6b792007-09-18 15:39:42 -0400416 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800417
418 vaf.fmt = fmt;
419 vaf.va = &args;
420
421 printk(KERN_ERR "b43-%s ERROR: %pV",
422 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
423
Michael Buesche4d6b792007-09-18 15:39:42 -0400424 va_end(args);
425}
426
427void b43warn(struct b43_wl *wl, const char *fmt, ...)
428{
Joe Perches5b736d42010-11-09 16:35:18 -0800429 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400430 va_list args;
431
Michael Buesch060210f2009-01-25 15:49:59 +0100432 if (b43_modparam_verbose < B43_VERBOSITY_WARN)
433 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400434 if (!b43_ratelimit(wl))
435 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800436
Michael Buesche4d6b792007-09-18 15:39:42 -0400437 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800438
439 vaf.fmt = fmt;
440 vaf.va = &args;
441
442 printk(KERN_WARNING "b43-%s warning: %pV",
443 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
444
Michael Buesche4d6b792007-09-18 15:39:42 -0400445 va_end(args);
446}
447
Michael Buesche4d6b792007-09-18 15:39:42 -0400448void b43dbg(struct b43_wl *wl, const char *fmt, ...)
449{
Joe Perches5b736d42010-11-09 16:35:18 -0800450 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400451 va_list args;
452
Michael Buesch060210f2009-01-25 15:49:59 +0100453 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
454 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800455
Michael Buesche4d6b792007-09-18 15:39:42 -0400456 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800457
458 vaf.fmt = fmt;
459 vaf.va = &args;
460
461 printk(KERN_DEBUG "b43-%s debug: %pV",
462 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
463
Michael Buesche4d6b792007-09-18 15:39:42 -0400464 va_end(args);
465}
Michael Buesche4d6b792007-09-18 15:39:42 -0400466
467static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
468{
469 u32 macctl;
470
471 B43_WARN_ON(offset % 4 != 0);
472
473 macctl = b43_read32(dev, B43_MMIO_MACCTL);
474 if (macctl & B43_MACCTL_BE)
475 val = swab32(val);
476
477 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
478 mmiowb();
479 b43_write32(dev, B43_MMIO_RAM_DATA, val);
480}
481
Michael Buesch280d0e12007-12-26 18:26:17 +0100482static inline void b43_shm_control_word(struct b43_wldev *dev,
483 u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400484{
485 u32 control;
486
487 /* "offset" is the WORD offset. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400488 control = routing;
489 control <<= 16;
490 control |= offset;
491 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
492}
493
Michael Buesch69eddc82009-09-04 22:57:26 +0200494u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400495{
496 u32 ret;
497
498 if (routing == B43_SHM_SHARED) {
499 B43_WARN_ON(offset & 0x0001);
500 if (offset & 0x0003) {
501 /* Unaligned access */
502 b43_shm_control_word(dev, routing, offset >> 2);
503 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
Michael Buesche4d6b792007-09-18 15:39:42 -0400504 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200505 ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400506
Michael Buesch280d0e12007-12-26 18:26:17 +0100507 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400508 }
509 offset >>= 2;
510 }
511 b43_shm_control_word(dev, routing, offset);
512 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100513out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200514 return ret;
515}
516
Michael Buesch69eddc82009-09-04 22:57:26 +0200517u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400518{
519 u16 ret;
520
521 if (routing == B43_SHM_SHARED) {
522 B43_WARN_ON(offset & 0x0001);
523 if (offset & 0x0003) {
524 /* Unaligned access */
525 b43_shm_control_word(dev, routing, offset >> 2);
526 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
527
Michael Buesch280d0e12007-12-26 18:26:17 +0100528 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400529 }
530 offset >>= 2;
531 }
532 b43_shm_control_word(dev, routing, offset);
533 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100534out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200535 return ret;
536}
537
Michael Buesch69eddc82009-09-04 22:57:26 +0200538void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400539{
540 if (routing == B43_SHM_SHARED) {
541 B43_WARN_ON(offset & 0x0001);
542 if (offset & 0x0003) {
543 /* Unaligned access */
544 b43_shm_control_word(dev, routing, offset >> 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400545 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200546 value & 0xFFFF);
Michael Buesche4d6b792007-09-18 15:39:42 -0400547 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200548 b43_write16(dev, B43_MMIO_SHM_DATA,
549 (value >> 16) & 0xFFFF);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200550 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400551 }
552 offset >>= 2;
553 }
554 b43_shm_control_word(dev, routing, offset);
Michael Buesche4d6b792007-09-18 15:39:42 -0400555 b43_write32(dev, B43_MMIO_SHM_DATA, value);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200556}
557
Michael Buesch69eddc82009-09-04 22:57:26 +0200558void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
Michael Buesch6bbc3212008-06-19 19:33:51 +0200559{
560 if (routing == B43_SHM_SHARED) {
561 B43_WARN_ON(offset & 0x0001);
562 if (offset & 0x0003) {
563 /* Unaligned access */
564 b43_shm_control_word(dev, routing, offset >> 2);
565 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
566 return;
567 }
568 offset >>= 2;
569 }
570 b43_shm_control_word(dev, routing, offset);
571 b43_write16(dev, B43_MMIO_SHM_DATA, value);
572}
573
Michael Buesche4d6b792007-09-18 15:39:42 -0400574/* Read HostFlags */
John Daiker99da1852009-02-24 02:16:42 -0800575u64 b43_hf_read(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400576{
Michael Buesch35f0d352008-02-13 14:31:08 +0100577 u64 ret;
Michael Buesche4d6b792007-09-18 15:39:42 -0400578
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200579 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400580 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200581 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2);
Michael Buesch35f0d352008-02-13 14:31:08 +0100582 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200583 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400584
585 return ret;
586}
587
588/* Write HostFlags */
Michael Buesch35f0d352008-02-13 14:31:08 +0100589void b43_hf_write(struct b43_wldev *dev, u64 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400590{
Michael Buesch35f0d352008-02-13 14:31:08 +0100591 u16 lo, mi, hi;
592
593 lo = (value & 0x00000000FFFFULL);
594 mi = (value & 0x0000FFFF0000ULL) >> 16;
595 hi = (value & 0xFFFF00000000ULL) >> 32;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200596 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1, lo);
597 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2, mi);
598 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3, hi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400599}
600
Michael Buesch403a3a12009-06-08 21:04:57 +0200601/* Read the firmware capabilities bitmask (Opensource firmware only) */
602static u16 b43_fwcapa_read(struct b43_wldev *dev)
603{
604 B43_WARN_ON(!dev->fw.opensource);
605 return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
606}
607
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100608void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
Michael Buesche4d6b792007-09-18 15:39:42 -0400609{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100610 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400611
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200612 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400613
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100614 /* The hardware guarantees us an atomic read, if we
615 * read the low register first. */
616 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
617 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400618
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100619 *tsf = high;
620 *tsf <<= 32;
621 *tsf |= low;
Michael Buesche4d6b792007-09-18 15:39:42 -0400622}
623
624static void b43_time_lock(struct b43_wldev *dev)
625{
Rafał Miłecki50566352012-01-02 19:31:21 +0100626 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_TBTTHOLD);
Michael Buesche4d6b792007-09-18 15:39:42 -0400627 /* Commit the write */
628 b43_read32(dev, B43_MMIO_MACCTL);
629}
630
631static void b43_time_unlock(struct b43_wldev *dev)
632{
Rafał Miłecki50566352012-01-02 19:31:21 +0100633 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_TBTTHOLD, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400634 /* Commit the write */
635 b43_read32(dev, B43_MMIO_MACCTL);
636}
637
638static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
639{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100640 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400641
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200642 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400643
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100644 low = tsf;
645 high = (tsf >> 32);
646 /* The hardware guarantees us an atomic write, if we
647 * write the low register first. */
648 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
649 mmiowb();
650 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
651 mmiowb();
Michael Buesche4d6b792007-09-18 15:39:42 -0400652}
653
654void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
655{
656 b43_time_lock(dev);
657 b43_tsf_write_locked(dev, tsf);
658 b43_time_unlock(dev);
659}
660
661static
John Daiker99da1852009-02-24 02:16:42 -0800662void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
Michael Buesche4d6b792007-09-18 15:39:42 -0400663{
664 static const u8 zero_addr[ETH_ALEN] = { 0 };
665 u16 data;
666
667 if (!mac)
668 mac = zero_addr;
669
670 offset |= 0x0020;
671 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
672
673 data = mac[0];
674 data |= mac[1] << 8;
675 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
676 data = mac[2];
677 data |= mac[3] << 8;
678 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
679 data = mac[4];
680 data |= mac[5] << 8;
681 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
682}
683
684static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
685{
686 const u8 *mac;
687 const u8 *bssid;
688 u8 mac_bssid[ETH_ALEN * 2];
689 int i;
690 u32 tmp;
691
692 bssid = dev->wl->bssid;
693 mac = dev->wl->mac_addr;
694
695 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
696
697 memcpy(mac_bssid, mac, ETH_ALEN);
698 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
699
700 /* Write our MAC address and BSSID to template ram */
701 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
702 tmp = (u32) (mac_bssid[i + 0]);
703 tmp |= (u32) (mac_bssid[i + 1]) << 8;
704 tmp |= (u32) (mac_bssid[i + 2]) << 16;
705 tmp |= (u32) (mac_bssid[i + 3]) << 24;
706 b43_ram_write(dev, 0x20 + i, tmp);
707 }
708}
709
Johannes Berg4150c572007-09-17 01:29:23 -0400710static void b43_upload_card_macaddress(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400711{
Michael Buesche4d6b792007-09-18 15:39:42 -0400712 b43_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400713 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400714}
715
716static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
717{
718 /* slot_time is in usec. */
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600719 /* This test used to exit for all but a G PHY. */
720 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Michael Buesche4d6b792007-09-18 15:39:42 -0400721 return;
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600722 b43_write16(dev, B43_MMIO_IFSSLOT, 510 + slot_time);
723 /* Shared memory location 0x0010 is the slot time and should be
724 * set to slot_time; however, this register is initially 0 and changing
725 * the value adversely affects the transmit rate for BCM4311
726 * devices. Until this behavior is unterstood, delete this step
727 *
728 * b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
729 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400730}
731
732static void b43_short_slot_timing_enable(struct b43_wldev *dev)
733{
734 b43_set_slot_time(dev, 9);
Michael Buesche4d6b792007-09-18 15:39:42 -0400735}
736
737static void b43_short_slot_timing_disable(struct b43_wldev *dev)
738{
739 b43_set_slot_time(dev, 20);
Michael Buesche4d6b792007-09-18 15:39:42 -0400740}
741
Michael Buesche4d6b792007-09-18 15:39:42 -0400742/* DummyTransmission function, as documented on
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200743 * http://bcm-v4.sipsolutions.net/802.11/DummyTransmission
Michael Buesche4d6b792007-09-18 15:39:42 -0400744 */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200745void b43_dummy_transmission(struct b43_wldev *dev, bool ofdm, bool pa_on)
Michael Buesche4d6b792007-09-18 15:39:42 -0400746{
747 struct b43_phy *phy = &dev->phy;
748 unsigned int i, max_loop;
749 u16 value;
750 u32 buffer[5] = {
751 0x00000000,
752 0x00D40000,
753 0x00000000,
754 0x01000000,
755 0x00000000,
756 };
757
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200758 if (ofdm) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400759 max_loop = 0x1E;
760 buffer[0] = 0x000201CC;
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200761 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -0400762 max_loop = 0xFA;
763 buffer[0] = 0x000B846E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400764 }
765
766 for (i = 0; i < 5; i++)
767 b43_ram_write(dev, i * 4, buffer[i]);
768
Rafał Miłecki7955d872011-09-21 21:44:13 +0200769 b43_write16(dev, B43_MMIO_XMTSEL, 0x0000);
770
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200771 if (dev->dev->core_rev < 11)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200772 b43_write16(dev, B43_MMIO_WEPCTL, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200773 else
Rafał Miłecki7955d872011-09-21 21:44:13 +0200774 b43_write16(dev, B43_MMIO_WEPCTL, 0x0100);
775
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200776 value = (ofdm ? 0x41 : 0x40);
Rafał Miłecki7955d872011-09-21 21:44:13 +0200777 b43_write16(dev, B43_MMIO_TXE0_PHYCTL, value);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200778 if (phy->type == B43_PHYTYPE_N || phy->type == B43_PHYTYPE_LP ||
779 phy->type == B43_PHYTYPE_LCN)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200780 b43_write16(dev, B43_MMIO_TXE0_PHYCTL1, 0x1A02);
781
782 b43_write16(dev, B43_MMIO_TXE0_WM_0, 0x0000);
783 b43_write16(dev, B43_MMIO_TXE0_WM_1, 0x0000);
784
785 b43_write16(dev, B43_MMIO_XMTTPLATETXPTR, 0x0000);
786 b43_write16(dev, B43_MMIO_XMTTXCNT, 0x0014);
787 b43_write16(dev, B43_MMIO_XMTSEL, 0x0826);
788 b43_write16(dev, B43_MMIO_TXE0_CTL, 0x0000);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200789
790 if (!pa_on && phy->type == B43_PHYTYPE_N)
791 ; /*b43_nphy_pa_override(dev, false) */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200792
793 switch (phy->type) {
794 case B43_PHYTYPE_N:
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200795 case B43_PHYTYPE_LCN:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200796 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x00D0);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200797 break;
798 case B43_PHYTYPE_LP:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200799 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0050);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200800 break;
801 default:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200802 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0030);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200803 }
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200804 b43_read16(dev, B43_MMIO_TXE0_AUX);
Michael Buesche4d6b792007-09-18 15:39:42 -0400805
806 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
807 b43_radio_write16(dev, 0x0051, 0x0017);
808 for (i = 0x00; i < max_loop; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200809 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400810 if (value & 0x0080)
811 break;
812 udelay(10);
813 }
814 for (i = 0x00; i < 0x0A; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200815 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400816 if (value & 0x0400)
817 break;
818 udelay(10);
819 }
Larry Finger1d280dd2008-09-29 14:19:29 -0500820 for (i = 0x00; i < 0x19; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200821 value = b43_read16(dev, B43_MMIO_IFSSTAT);
Michael Buesche4d6b792007-09-18 15:39:42 -0400822 if (!(value & 0x0100))
823 break;
824 udelay(10);
825 }
826 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
827 b43_radio_write16(dev, 0x0051, 0x0037);
828}
829
830static void key_write(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800831 u8 index, u8 algorithm, const u8 *key)
Michael Buesche4d6b792007-09-18 15:39:42 -0400832{
833 unsigned int i;
834 u32 offset;
835 u16 value;
836 u16 kidx;
837
838 /* Key index/algo block */
839 kidx = b43_kidx_to_fw(dev, index);
840 value = ((kidx << 4) | algorithm);
841 b43_shm_write16(dev, B43_SHM_SHARED,
842 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
843
844 /* Write the key to the Key Table Pointer offset */
845 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
846 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
847 value = key[i];
848 value |= (u16) (key[i + 1]) << 8;
849 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
850 }
851}
852
John Daiker99da1852009-02-24 02:16:42 -0800853static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400854{
855 u32 addrtmp[2] = { 0, 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200856 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400857
858 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200859 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400860
Michael Buesch66d2d082009-08-06 10:36:50 +0200861 B43_WARN_ON(index < pairwise_keys_start);
862 /* We have four default TX keys and possibly four default RX keys.
Michael Buesche4d6b792007-09-18 15:39:42 -0400863 * Physical mac 0 is mapped to physical key 4 or 8, depending
864 * on the firmware version.
865 * So we must adjust the index here.
866 */
Michael Buesch66d2d082009-08-06 10:36:50 +0200867 index -= pairwise_keys_start;
868 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400869
870 if (addr) {
871 addrtmp[0] = addr[0];
872 addrtmp[0] |= ((u32) (addr[1]) << 8);
873 addrtmp[0] |= ((u32) (addr[2]) << 16);
874 addrtmp[0] |= ((u32) (addr[3]) << 24);
875 addrtmp[1] = addr[4];
876 addrtmp[1] |= ((u32) (addr[5]) << 8);
877 }
878
Michael Buesch66d2d082009-08-06 10:36:50 +0200879 /* Receive match transmitter address (RCMTA) mechanism */
880 b43_shm_write32(dev, B43_SHM_RCMTA,
881 (index * 2) + 0, addrtmp[0]);
882 b43_shm_write16(dev, B43_SHM_RCMTA,
883 (index * 2) + 1, addrtmp[1]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400884}
885
gregor kowski035d0242009-08-19 22:35:45 +0200886/* The ucode will use phase1 key with TEK key to decrypt rx packets.
887 * When a packet is received, the iv32 is checked.
888 * - if it doesn't the packet is returned without modification (and software
889 * decryption can be done). That's what happen when iv16 wrap.
890 * - if it does, the rc4 key is computed, and decryption is tried.
891 * Either it will success and B43_RX_MAC_DEC is returned,
892 * either it fails and B43_RX_MAC_DEC|B43_RX_MAC_DECERR is returned
893 * and the packet is not usable (it got modified by the ucode).
894 * So in order to never have B43_RX_MAC_DECERR, we should provide
895 * a iv32 and phase1key that match. Because we drop packets in case of
896 * B43_RX_MAC_DECERR, if we have a correct iv32 but a wrong phase1key, all
897 * packets will be lost without higher layer knowing (ie no resync possible
898 * until next wrap).
899 *
900 * NOTE : this should support 50 key like RCMTA because
901 * (B43_SHM_SH_KEYIDXBLOCK - B43_SHM_SH_TKIPTSCTTAK)/14 = 50
902 */
903static void rx_tkip_phase1_write(struct b43_wldev *dev, u8 index, u32 iv32,
904 u16 *phase1key)
905{
906 unsigned int i;
907 u32 offset;
908 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
909
910 if (!modparam_hwtkip)
911 return;
912
913 if (b43_new_kidx_api(dev))
914 pairwise_keys_start = B43_NR_GROUP_KEYS;
915
916 B43_WARN_ON(index < pairwise_keys_start);
917 /* We have four default TX keys and possibly four default RX keys.
918 * Physical mac 0 is mapped to physical key 4 or 8, depending
919 * on the firmware version.
920 * So we must adjust the index here.
921 */
922 index -= pairwise_keys_start;
923 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
924
925 if (b43_debug(dev, B43_DBG_KEYS)) {
926 b43dbg(dev->wl, "rx_tkip_phase1_write : idx 0x%x, iv32 0x%x\n",
927 index, iv32);
928 }
929 /* Write the key to the RX tkip shared mem */
930 offset = B43_SHM_SH_TKIPTSCTTAK + index * (10 + 4);
931 for (i = 0; i < 10; i += 2) {
932 b43_shm_write16(dev, B43_SHM_SHARED, offset + i,
933 phase1key ? phase1key[i / 2] : 0);
934 }
935 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, iv32);
936 b43_shm_write16(dev, B43_SHM_SHARED, offset + i + 2, iv32 >> 16);
937}
938
939static void b43_op_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100940 struct ieee80211_vif *vif,
941 struct ieee80211_key_conf *keyconf,
942 struct ieee80211_sta *sta,
943 u32 iv32, u16 *phase1key)
gregor kowski035d0242009-08-19 22:35:45 +0200944{
945 struct b43_wl *wl = hw_to_b43_wl(hw);
946 struct b43_wldev *dev;
947 int index = keyconf->hw_key_idx;
948
949 if (B43_WARN_ON(!modparam_hwtkip))
950 return;
951
Michael Buesch96869a32010-01-24 13:13:32 +0100952 /* This is only called from the RX path through mac80211, where
953 * our mutex is already locked. */
954 B43_WARN_ON(!mutex_is_locked(&wl->mutex));
gregor kowski035d0242009-08-19 22:35:45 +0200955 dev = wl->current_dev;
Michael Buesch96869a32010-01-24 13:13:32 +0100956 B43_WARN_ON(!dev || b43_status(dev) < B43_STAT_INITIALIZED);
gregor kowski035d0242009-08-19 22:35:45 +0200957
958 keymac_write(dev, index, NULL); /* First zero out mac to avoid race */
959
960 rx_tkip_phase1_write(dev, index, iv32, phase1key);
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100961 /* only pairwise TKIP keys are supported right now */
962 if (WARN_ON(!sta))
Michael Buesch96869a32010-01-24 13:13:32 +0100963 return;
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100964 keymac_write(dev, index, sta->addr);
gregor kowski035d0242009-08-19 22:35:45 +0200965}
966
Michael Buesche4d6b792007-09-18 15:39:42 -0400967static void do_key_write(struct b43_wldev *dev,
968 u8 index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800969 const u8 *key, size_t key_len, const u8 *mac_addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400970{
971 u8 buf[B43_SEC_KEYSIZE] = { 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200972 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400973
974 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200975 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400976
Michael Buesch66d2d082009-08-06 10:36:50 +0200977 B43_WARN_ON(index >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400978 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
979
Michael Buesch66d2d082009-08-06 10:36:50 +0200980 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400981 keymac_write(dev, index, NULL); /* First zero out mac. */
gregor kowski035d0242009-08-19 22:35:45 +0200982 if (algorithm == B43_SEC_ALGO_TKIP) {
983 /*
984 * We should provide an initial iv32, phase1key pair.
985 * We could start with iv32=0 and compute the corresponding
986 * phase1key, but this means calling ieee80211_get_tkip_key
987 * with a fake skb (or export other tkip function).
988 * Because we are lazy we hope iv32 won't start with
989 * 0xffffffff and let's b43_op_update_tkip_key provide a
990 * correct pair.
991 */
992 rx_tkip_phase1_write(dev, index, 0xffffffff, (u16*)buf);
993 } else if (index >= pairwise_keys_start) /* clear it */
994 rx_tkip_phase1_write(dev, index, 0, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -0400995 if (key)
996 memcpy(buf, key, key_len);
997 key_write(dev, index, algorithm, buf);
Michael Buesch66d2d082009-08-06 10:36:50 +0200998 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400999 keymac_write(dev, index, mac_addr);
1000
1001 dev->key[index].algorithm = algorithm;
1002}
1003
1004static int b43_key_write(struct b43_wldev *dev,
1005 int index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -08001006 const u8 *key, size_t key_len,
1007 const u8 *mac_addr,
Michael Buesche4d6b792007-09-18 15:39:42 -04001008 struct ieee80211_key_conf *keyconf)
1009{
1010 int i;
Michael Buesch66d2d082009-08-06 10:36:50 +02001011 int pairwise_keys_start;
Michael Buesche4d6b792007-09-18 15:39:42 -04001012
gregor kowski035d0242009-08-19 22:35:45 +02001013 /* For ALG_TKIP the key is encoded as a 256-bit (32 byte) data block:
1014 * - Temporal Encryption Key (128 bits)
1015 * - Temporal Authenticator Tx MIC Key (64 bits)
1016 * - Temporal Authenticator Rx MIC Key (64 bits)
1017 *
1018 * Hardware only store TEK
1019 */
1020 if (algorithm == B43_SEC_ALGO_TKIP && key_len == 32)
1021 key_len = 16;
Michael Buesche4d6b792007-09-18 15:39:42 -04001022 if (key_len > B43_SEC_KEYSIZE)
1023 return -EINVAL;
Michael Buesch66d2d082009-08-06 10:36:50 +02001024 for (i = 0; i < ARRAY_SIZE(dev->key); i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001025 /* Check that we don't already have this key. */
1026 B43_WARN_ON(dev->key[i].keyconf == keyconf);
1027 }
1028 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001029 /* Pairwise key. Get an empty slot for the key. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001030 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +02001031 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -04001032 else
Michael Buesch66d2d082009-08-06 10:36:50 +02001033 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1034 for (i = pairwise_keys_start;
1035 i < pairwise_keys_start + B43_NR_PAIRWISE_KEYS;
1036 i++) {
1037 B43_WARN_ON(i >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -04001038 if (!dev->key[i].keyconf) {
1039 /* found empty */
1040 index = i;
1041 break;
1042 }
1043 }
1044 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001045 b43warn(dev->wl, "Out of hardware key memory\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04001046 return -ENOSPC;
1047 }
1048 } else
1049 B43_WARN_ON(index > 3);
1050
1051 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
1052 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1053 /* Default RX key */
1054 B43_WARN_ON(mac_addr);
1055 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
1056 }
1057 keyconf->hw_key_idx = index;
1058 dev->key[index].keyconf = keyconf;
1059
1060 return 0;
1061}
1062
1063static int b43_key_clear(struct b43_wldev *dev, int index)
1064{
Michael Buesch66d2d082009-08-06 10:36:50 +02001065 if (B43_WARN_ON((index < 0) || (index >= ARRAY_SIZE(dev->key))))
Michael Buesche4d6b792007-09-18 15:39:42 -04001066 return -EINVAL;
1067 do_key_write(dev, index, B43_SEC_ALGO_NONE,
1068 NULL, B43_SEC_KEYSIZE, NULL);
1069 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1070 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
1071 NULL, B43_SEC_KEYSIZE, NULL);
1072 }
1073 dev->key[index].keyconf = NULL;
1074
1075 return 0;
1076}
1077
1078static void b43_clear_keys(struct b43_wldev *dev)
1079{
Michael Buesch66d2d082009-08-06 10:36:50 +02001080 int i, count;
Michael Buesche4d6b792007-09-18 15:39:42 -04001081
Michael Buesch66d2d082009-08-06 10:36:50 +02001082 if (b43_new_kidx_api(dev))
1083 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1084 else
1085 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1086 for (i = 0; i < count; i++)
Michael Buesche4d6b792007-09-18 15:39:42 -04001087 b43_key_clear(dev, i);
1088}
1089
Michael Buesch9cf7f242008-12-19 20:24:30 +01001090static void b43_dump_keymemory(struct b43_wldev *dev)
1091{
Michael Buesch66d2d082009-08-06 10:36:50 +02001092 unsigned int i, index, count, offset, pairwise_keys_start;
Michael Buesch9cf7f242008-12-19 20:24:30 +01001093 u8 mac[ETH_ALEN];
1094 u16 algo;
1095 u32 rcmta0;
1096 u16 rcmta1;
1097 u64 hf;
1098 struct b43_key *key;
1099
1100 if (!b43_debug(dev, B43_DBG_KEYS))
1101 return;
1102
1103 hf = b43_hf_read(dev);
1104 b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
1105 !!(hf & B43_HF_USEDEFKEYS));
Michael Buesch66d2d082009-08-06 10:36:50 +02001106 if (b43_new_kidx_api(dev)) {
1107 pairwise_keys_start = B43_NR_GROUP_KEYS;
1108 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1109 } else {
1110 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1111 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1112 }
1113 for (index = 0; index < count; index++) {
Michael Buesch9cf7f242008-12-19 20:24:30 +01001114 key = &(dev->key[index]);
1115 printk(KERN_DEBUG "Key slot %02u: %s",
1116 index, (key->keyconf == NULL) ? " " : "*");
1117 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
1118 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
1119 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1120 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1121 }
1122
1123 algo = b43_shm_read16(dev, B43_SHM_SHARED,
1124 B43_SHM_SH_KEYIDXBLOCK + (index * 2));
1125 printk(" Algo: %04X/%02X", algo, key->algorithm);
1126
Michael Buesch66d2d082009-08-06 10:36:50 +02001127 if (index >= pairwise_keys_start) {
gregor kowski035d0242009-08-19 22:35:45 +02001128 if (key->algorithm == B43_SEC_ALGO_TKIP) {
1129 printk(" TKIP: ");
1130 offset = B43_SHM_SH_TKIPTSCTTAK + (index - 4) * (10 + 4);
1131 for (i = 0; i < 14; i += 2) {
1132 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1133 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1134 }
1135 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01001136 rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001137 ((index - pairwise_keys_start) * 2) + 0);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001138 rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001139 ((index - pairwise_keys_start) * 2) + 1);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001140 *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
1141 *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
Johannes Berge91d8332009-07-15 17:21:41 +02001142 printk(" MAC: %pM", mac);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001143 } else
1144 printk(" DEFAULT KEY");
1145 printk("\n");
1146 }
1147}
1148
Michael Buesche4d6b792007-09-18 15:39:42 -04001149void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1150{
1151 u32 macctl;
1152 u16 ucstat;
1153 bool hwps;
1154 bool awake;
1155 int i;
1156
1157 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1158 (ps_flags & B43_PS_DISABLED));
1159 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1160
1161 if (ps_flags & B43_PS_ENABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001162 hwps = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001163 } else if (ps_flags & B43_PS_DISABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001164 hwps = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001165 } else {
1166 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1167 // and thus is not an AP and we are associated, set bit 25
1168 }
1169 if (ps_flags & B43_PS_AWAKE) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001170 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001171 } else if (ps_flags & B43_PS_ASLEEP) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001172 awake = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001173 } else {
1174 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1175 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1176 // successful, set bit26
1177 }
1178
1179/* FIXME: For now we force awake-on and hwps-off */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001180 hwps = false;
1181 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001182
1183 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1184 if (hwps)
1185 macctl |= B43_MACCTL_HWPS;
1186 else
1187 macctl &= ~B43_MACCTL_HWPS;
1188 if (awake)
1189 macctl |= B43_MACCTL_AWAKE;
1190 else
1191 macctl &= ~B43_MACCTL_AWAKE;
1192 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1193 /* Commit write */
1194 b43_read32(dev, B43_MMIO_MACCTL);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001195 if (awake && dev->dev->core_rev >= 5) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001196 /* Wait for the microcode to wake up. */
1197 for (i = 0; i < 100; i++) {
1198 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1199 B43_SHM_SH_UCODESTAT);
1200 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1201 break;
1202 udelay(10);
1203 }
1204 }
1205}
1206
Rafał Miłecki737f6572014-09-12 18:37:26 +02001207/* http://bcm-v4.sipsolutions.net/802.11/PHY/BmacCorePllReset */
1208void b43_wireless_core_phy_pll_reset(struct b43_wldev *dev)
1209{
1210 struct bcma_drv_cc *bcma_cc __maybe_unused;
1211 struct ssb_chipcommon *ssb_cc __maybe_unused;
1212
1213 switch (dev->dev->bus_type) {
1214#ifdef CONFIG_B43_BCMA
1215 case B43_BUS_BCMA:
1216 bcma_cc = &dev->dev->bdev->bus->drv_cc;
1217
1218 bcma_cc_write32(bcma_cc, BCMA_CC_CHIPCTL_ADDR, 0);
1219 bcma_cc_mask32(bcma_cc, BCMA_CC_CHIPCTL_DATA, ~0x4);
1220 bcma_cc_set32(bcma_cc, BCMA_CC_CHIPCTL_DATA, 0x4);
1221 bcma_cc_mask32(bcma_cc, BCMA_CC_CHIPCTL_DATA, ~0x4);
1222 break;
1223#endif
1224#ifdef CONFIG_B43_SSB
1225 case B43_BUS_SSB:
1226 ssb_cc = &dev->dev->sdev->bus->chipco;
1227
1228 chipco_write32(ssb_cc, SSB_CHIPCO_CHIPCTL_ADDR, 0);
1229 chipco_mask32(ssb_cc, SSB_CHIPCO_CHIPCTL_DATA, ~0x4);
1230 chipco_set32(ssb_cc, SSB_CHIPCO_CHIPCTL_DATA, 0x4);
1231 chipco_mask32(ssb_cc, SSB_CHIPCO_CHIPCTL_DATA, ~0x4);
1232 break;
1233#endif
1234 }
1235}
1236
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001237#ifdef CONFIG_B43_BCMA
Rafał Miłecki49173592011-07-17 01:06:06 +02001238static void b43_bcma_phy_reset(struct b43_wldev *dev)
1239{
1240 u32 flags;
1241
1242 /* Put PHY into reset */
1243 flags = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1244 flags |= B43_BCMA_IOCTL_PHY_RESET;
1245 flags |= B43_BCMA_IOCTL_PHY_BW_20MHZ; /* Make 20 MHz def */
1246 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, flags);
1247 udelay(2);
1248
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001249 b43_phy_take_out_of_reset(dev);
Rafał Miłecki49173592011-07-17 01:06:06 +02001250}
1251
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001252static void b43_bcma_wireless_core_reset(struct b43_wldev *dev, bool gmode)
1253{
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001254 u32 req = B43_BCMA_CLKCTLST_80211_PLL_REQ |
1255 B43_BCMA_CLKCTLST_PHY_PLL_REQ;
1256 u32 status = B43_BCMA_CLKCTLST_80211_PLL_ST |
1257 B43_BCMA_CLKCTLST_PHY_PLL_ST;
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001258 u32 flags;
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001259
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001260 flags = B43_BCMA_IOCTL_PHY_CLKEN;
1261 if (gmode)
1262 flags |= B43_BCMA_IOCTL_GMODE;
1263 b43_device_enable(dev, flags);
1264
Rafał Miłecki49173592011-07-17 01:06:06 +02001265 bcma_core_set_clockmode(dev->dev->bdev, BCMA_CLKMODE_FAST);
1266 b43_bcma_phy_reset(dev);
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001267 bcma_core_pll_ctl(dev->dev->bdev, req, status, true);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001268}
1269#endif
1270
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001271#ifdef CONFIG_B43_SSB
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001272static void b43_ssb_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001273{
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001274 u32 flags = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04001275
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001276 if (gmode)
1277 flags |= B43_TMSLOW_GMODE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001278 flags |= B43_TMSLOW_PHYCLKEN;
1279 flags |= B43_TMSLOW_PHYRESET;
Rafał Miłecki42ab1352010-12-09 20:56:01 +01001280 if (dev->phy.type == B43_PHYTYPE_N)
1281 flags |= B43_TMSLOW_PHY_BANDWIDTH_20MHZ; /* Make 20 MHz def */
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02001282 b43_device_enable(dev, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -04001283 msleep(2); /* Wait for the PLL to turn on. */
1284
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001285 b43_phy_take_out_of_reset(dev);
Rafał Miłecki14952982011-05-17 18:57:28 +02001286}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001287#endif
Rafał Miłecki14952982011-05-17 18:57:28 +02001288
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001289void b43_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Rafał Miłecki14952982011-05-17 18:57:28 +02001290{
1291 u32 macctl;
1292
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001293 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001294#ifdef CONFIG_B43_BCMA
1295 case B43_BUS_BCMA:
1296 b43_bcma_wireless_core_reset(dev, gmode);
1297 break;
1298#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001299#ifdef CONFIG_B43_SSB
1300 case B43_BUS_SSB:
1301 b43_ssb_wireless_core_reset(dev, gmode);
1302 break;
1303#endif
1304 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001305
Michael Bueschfb111372008-09-02 13:00:34 +02001306 /* Turn Analog ON, but only if we already know the PHY-type.
1307 * This protects against very early setup where we don't know the
1308 * PHY-type, yet. wireless_core_reset will be called once again later,
1309 * when we know the PHY-type. */
1310 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001311 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001312
1313 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1314 macctl &= ~B43_MACCTL_GMODE;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001315 if (gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001316 macctl |= B43_MACCTL_GMODE;
1317 macctl |= B43_MACCTL_IHR_ENABLED;
1318 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1319}
1320
1321static void handle_irq_transmit_status(struct b43_wldev *dev)
1322{
1323 u32 v0, v1;
1324 u16 tmp;
1325 struct b43_txstatus stat;
1326
1327 while (1) {
1328 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1329 if (!(v0 & 0x00000001))
1330 break;
1331 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1332
1333 stat.cookie = (v0 >> 16);
1334 stat.seq = (v1 & 0x0000FFFF);
1335 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1336 tmp = (v0 & 0x0000FFFF);
1337 stat.frame_count = ((tmp & 0xF000) >> 12);
1338 stat.rts_count = ((tmp & 0x0F00) >> 8);
1339 stat.supp_reason = ((tmp & 0x001C) >> 2);
1340 stat.pm_indicated = !!(tmp & 0x0080);
1341 stat.intermediate = !!(tmp & 0x0040);
1342 stat.for_ampdu = !!(tmp & 0x0020);
1343 stat.acked = !!(tmp & 0x0002);
1344
1345 b43_handle_txstatus(dev, &stat);
1346 }
1347}
1348
1349static void drain_txstatus_queue(struct b43_wldev *dev)
1350{
1351 u32 dummy;
1352
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001353 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04001354 return;
1355 /* Read all entries from the microcode TXstatus FIFO
1356 * and throw them away.
1357 */
1358 while (1) {
1359 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1360 if (!(dummy & 0x00000001))
1361 break;
1362 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1363 }
1364}
1365
1366static u32 b43_jssi_read(struct b43_wldev *dev)
1367{
1368 u32 val = 0;
1369
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001370 val = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001371 val <<= 16;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001372 val |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001373
1374 return val;
1375}
1376
1377static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1378{
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001379 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0,
1380 (jssi & 0x0000FFFF));
1381 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1,
1382 (jssi & 0xFFFF0000) >> 16);
Michael Buesche4d6b792007-09-18 15:39:42 -04001383}
1384
1385static void b43_generate_noise_sample(struct b43_wldev *dev)
1386{
1387 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001388 b43_write32(dev, B43_MMIO_MACCMD,
1389 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001390}
1391
1392static void b43_calculate_link_quality(struct b43_wldev *dev)
1393{
1394 /* Top half of Link Quality calculation. */
1395
Michael Bueschef1a6282008-08-27 18:53:02 +02001396 if (dev->phy.type != B43_PHYTYPE_G)
1397 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001398 if (dev->noisecalc.calculation_running)
1399 return;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001400 dev->noisecalc.calculation_running = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001401 dev->noisecalc.nr_samples = 0;
1402
1403 b43_generate_noise_sample(dev);
1404}
1405
1406static void handle_irq_noise(struct b43_wldev *dev)
1407{
Michael Bueschef1a6282008-08-27 18:53:02 +02001408 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001409 u16 tmp;
1410 u8 noise[4];
1411 u8 i, j;
1412 s32 average;
1413
1414 /* Bottom half of Link Quality calculation. */
1415
Michael Bueschef1a6282008-08-27 18:53:02 +02001416 if (dev->phy.type != B43_PHYTYPE_G)
1417 return;
1418
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001419 /* Possible race condition: It might be possible that the user
1420 * changed to a different channel in the meantime since we
1421 * started the calculation. We ignore that fact, since it's
1422 * not really that much of a problem. The background noise is
1423 * an estimation only anyway. Slightly wrong results will get damped
1424 * by the averaging of the 8 sample rounds. Additionally the
1425 * value is shortlived. So it will be replaced by the next noise
1426 * calculation round soon. */
1427
Michael Buesche4d6b792007-09-18 15:39:42 -04001428 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001429 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001430 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1431 noise[2] == 0x7F || noise[3] == 0x7F)
1432 goto generate_new;
1433
1434 /* Get the noise samples. */
1435 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1436 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001437 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1438 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1439 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1440 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001441 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1442 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1443 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1444 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1445 dev->noisecalc.nr_samples++;
1446 if (dev->noisecalc.nr_samples == 8) {
1447 /* Calculate the Link Quality by the noise samples. */
1448 average = 0;
1449 for (i = 0; i < 8; i++) {
1450 for (j = 0; j < 4; j++)
1451 average += dev->noisecalc.samples[i][j];
1452 }
1453 average /= (8 * 4);
1454 average *= 125;
1455 average += 64;
1456 average /= 128;
1457 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1458 tmp = (tmp / 128) & 0x1F;
1459 if (tmp >= 8)
1460 average += 2;
1461 else
1462 average -= 25;
1463 if (tmp == 8)
1464 average -= 72;
1465 else
1466 average -= 48;
1467
1468 dev->stats.link_noise = average;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001469 dev->noisecalc.calculation_running = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001470 return;
1471 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001472generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001473 b43_generate_noise_sample(dev);
1474}
1475
1476static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1477{
Johannes Berg05c914f2008-09-11 00:01:58 +02001478 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001479 ///TODO: PS TBTT
1480 } else {
1481 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1482 b43_power_saving_ctl_bits(dev, 0);
1483 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001484 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001485 dev->dfq_valid = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001486}
1487
1488static void handle_irq_atim_end(struct b43_wldev *dev)
1489{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001490 if (dev->dfq_valid) {
1491 b43_write32(dev, B43_MMIO_MACCMD,
1492 b43_read32(dev, B43_MMIO_MACCMD)
1493 | B43_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001494 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001495 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001496}
1497
1498static void handle_irq_pmq(struct b43_wldev *dev)
1499{
1500 u32 tmp;
1501
1502 //TODO: AP mode.
1503
1504 while (1) {
1505 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1506 if (!(tmp & 0x00000008))
1507 break;
1508 }
1509 /* 16bit write is odd, but correct. */
1510 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1511}
1512
1513static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001514 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001515 u16 ram_offset,
1516 u16 shm_size_offset, u8 rate)
1517{
1518 u32 i, tmp;
1519 struct b43_plcp_hdr4 plcp;
1520
1521 plcp.data = 0;
1522 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1523 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1524 ram_offset += sizeof(u32);
1525 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1526 * So leave the first two bytes of the next write blank.
1527 */
1528 tmp = (u32) (data[0]) << 16;
1529 tmp |= (u32) (data[1]) << 24;
1530 b43_ram_write(dev, ram_offset, tmp);
1531 ram_offset += sizeof(u32);
1532 for (i = 2; i < size; i += sizeof(u32)) {
1533 tmp = (u32) (data[i + 0]);
1534 if (i + 1 < size)
1535 tmp |= (u32) (data[i + 1]) << 8;
1536 if (i + 2 < size)
1537 tmp |= (u32) (data[i + 2]) << 16;
1538 if (i + 3 < size)
1539 tmp |= (u32) (data[i + 3]) << 24;
1540 b43_ram_write(dev, ram_offset + i - 2, tmp);
1541 }
1542 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1543 size + sizeof(struct b43_plcp_hdr6));
1544}
1545
Michael Buesch5042c502008-04-05 15:05:00 +02001546/* Check if the use of the antenna that ieee80211 told us to
1547 * use is possible. This will fall back to DEFAULT.
1548 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1549u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1550 u8 antenna_nr)
1551{
1552 u8 antenna_mask;
1553
1554 if (antenna_nr == 0) {
1555 /* Zero means "use default antenna". That's always OK. */
1556 return 0;
1557 }
1558
1559 /* Get the mask of available antennas. */
1560 if (dev->phy.gmode)
Rafał Miłecki05814832011-05-18 02:06:39 +02001561 antenna_mask = dev->dev->bus_sprom->ant_available_bg;
Michael Buesch5042c502008-04-05 15:05:00 +02001562 else
Rafał Miłecki05814832011-05-18 02:06:39 +02001563 antenna_mask = dev->dev->bus_sprom->ant_available_a;
Michael Buesch5042c502008-04-05 15:05:00 +02001564
1565 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1566 /* This antenna is not available. Fall back to default. */
1567 return 0;
1568 }
1569
1570 return antenna_nr;
1571}
1572
Michael Buesch5042c502008-04-05 15:05:00 +02001573/* Convert a b43 antenna number value to the PHY TX control value. */
1574static u16 b43_antenna_to_phyctl(int antenna)
1575{
1576 switch (antenna) {
1577 case B43_ANTENNA0:
1578 return B43_TXH_PHY_ANT0;
1579 case B43_ANTENNA1:
1580 return B43_TXH_PHY_ANT1;
1581 case B43_ANTENNA2:
1582 return B43_TXH_PHY_ANT2;
1583 case B43_ANTENNA3:
1584 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001585 case B43_ANTENNA_AUTO0:
1586 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001587 return B43_TXH_PHY_ANT01AUTO;
1588 }
1589 B43_WARN_ON(1);
1590 return 0;
1591}
1592
Michael Buesche4d6b792007-09-18 15:39:42 -04001593static void b43_write_beacon_template(struct b43_wldev *dev,
1594 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001595 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001596{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001597 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001598 const struct ieee80211_mgmt *bcn;
1599 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001600 bool tim_found = false;
Michael Buesch5042c502008-04-05 15:05:00 +02001601 unsigned int rate;
1602 u16 ctl;
1603 int antenna;
Michael Büscha75d46a2015-01-26 18:26:17 +01001604 struct ieee80211_tx_info *info;
1605 unsigned long flags;
1606 struct sk_buff *beacon_skb;
Michael Buesche4d6b792007-09-18 15:39:42 -04001607
Michael Büscha75d46a2015-01-26 18:26:17 +01001608 spin_lock_irqsave(&dev->wl->beacon_lock, flags);
1609 info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Johannes Berge039fa42008-05-15 12:55:29 +02001610 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Büscha75d46a2015-01-26 18:26:17 +01001611 /* Clone the beacon, so it cannot go away, while we write it to hw. */
1612 beacon_skb = skb_clone(dev->wl->current_beacon, GFP_ATOMIC);
1613 spin_unlock_irqrestore(&dev->wl->beacon_lock, flags);
1614
1615 if (!beacon_skb) {
1616 b43dbg(dev->wl, "Could not upload beacon. "
1617 "Failed to clone beacon skb.");
1618 return;
1619 }
1620
1621 bcn = (const struct ieee80211_mgmt *)(beacon_skb->data);
1622 len = min_t(size_t, beacon_skb->len,
1623 0x200 - sizeof(struct b43_plcp_hdr6));
Michael Buesche66fee62007-12-26 17:47:10 +01001624
1625 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001626 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001627
Michael Buesch5042c502008-04-05 15:05:00 +02001628 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001629 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001630 antenna = b43_antenna_to_phyctl(antenna);
1631 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1632 /* We can't send beacons with short preamble. Would get PHY errors. */
1633 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1634 ctl &= ~B43_TXH_PHY_ANT;
1635 ctl &= ~B43_TXH_PHY_ENC;
1636 ctl |= antenna;
1637 if (b43_is_cck_rate(rate))
1638 ctl |= B43_TXH_PHY_ENC_CCK;
1639 else
1640 ctl |= B43_TXH_PHY_ENC_OFDM;
1641 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1642
Michael Buesche66fee62007-12-26 17:47:10 +01001643 /* Find the position of the TIM and the DTIM_period value
1644 * and write them to SHM. */
1645 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001646 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1647 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001648 uint8_t ie_id, ie_len;
1649
1650 ie_id = ie[i];
1651 ie_len = ie[i + 1];
1652 if (ie_id == 5) {
1653 u16 tim_position;
1654 u16 dtim_period;
1655 /* This is the TIM Information Element */
1656
1657 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001658 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001659 break;
1660 /* A valid TIM is at least 4 bytes long. */
1661 if (ie_len < 4)
1662 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001663 tim_found = true;
Michael Buesche66fee62007-12-26 17:47:10 +01001664
1665 tim_position = sizeof(struct b43_plcp_hdr6);
1666 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1667 tim_position += i;
1668
1669 dtim_period = ie[i + 3];
1670
1671 b43_shm_write16(dev, B43_SHM_SHARED,
1672 B43_SHM_SH_TIMBPOS, tim_position);
1673 b43_shm_write16(dev, B43_SHM_SHARED,
1674 B43_SHM_SH_DTIMPER, dtim_period);
1675 break;
1676 }
1677 i += ie_len + 2;
1678 }
1679 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001680 /*
1681 * If ucode wants to modify TIM do it behind the beacon, this
1682 * will happen, for example, when doing mesh networking.
1683 */
1684 b43_shm_write16(dev, B43_SHM_SHARED,
1685 B43_SHM_SH_TIMBPOS,
1686 len + sizeof(struct b43_plcp_hdr6));
1687 b43_shm_write16(dev, B43_SHM_SHARED,
1688 B43_SHM_SH_DTIMPER, 0);
1689 }
1690 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Büscha75d46a2015-01-26 18:26:17 +01001691
1692 dev_kfree_skb_any(beacon_skb);
Michael Buesche4d6b792007-09-18 15:39:42 -04001693}
1694
Michael Buesch6b4bec02008-05-20 12:16:28 +02001695static void b43_upload_beacon0(struct b43_wldev *dev)
1696{
1697 struct b43_wl *wl = dev->wl;
1698
1699 if (wl->beacon0_uploaded)
1700 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001701 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE0, B43_SHM_SH_BTL0);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001702 wl->beacon0_uploaded = true;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001703}
1704
1705static void b43_upload_beacon1(struct b43_wldev *dev)
1706{
1707 struct b43_wl *wl = dev->wl;
1708
1709 if (wl->beacon1_uploaded)
1710 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001711 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE1, B43_SHM_SH_BTL1);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001712 wl->beacon1_uploaded = true;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001713}
1714
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001715static void handle_irq_beacon(struct b43_wldev *dev)
1716{
1717 struct b43_wl *wl = dev->wl;
1718 u32 cmd, beacon0_valid, beacon1_valid;
1719
Johannes Berg05c914f2008-09-11 00:01:58 +02001720 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
Manual Munz8c235162011-09-18 18:24:03 -05001721 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) &&
1722 !b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001723 return;
1724
1725 /* This is the bottom half of the asynchronous beacon update. */
1726
1727 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001728 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001729
1730 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1731 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1732 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1733
1734 /* Schedule interrupt manually, if busy. */
1735 if (beacon0_valid && beacon1_valid) {
1736 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001737 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001738 return;
1739 }
1740
Michael Buesch6b4bec02008-05-20 12:16:28 +02001741 if (unlikely(wl->beacon_templates_virgin)) {
1742 /* We never uploaded a beacon before.
1743 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001744 wl->beacon_templates_virgin = false;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001745 b43_upload_beacon0(dev);
1746 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001747 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1748 cmd |= B43_MACCMD_BEACON0_VALID;
1749 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec02008-05-20 12:16:28 +02001750 } else {
1751 if (!beacon0_valid) {
1752 b43_upload_beacon0(dev);
1753 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1754 cmd |= B43_MACCMD_BEACON0_VALID;
1755 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1756 } else if (!beacon1_valid) {
1757 b43_upload_beacon1(dev);
1758 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1759 cmd |= B43_MACCMD_BEACON1_VALID;
1760 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001761 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001762 }
1763}
1764
Michael Buesch36dbd952009-09-04 22:51:29 +02001765static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1766{
1767 u32 old_irq_mask = dev->irq_mask;
1768
1769 /* update beacon right away or defer to irq */
1770 handle_irq_beacon(dev);
1771 if (old_irq_mask != dev->irq_mask) {
1772 /* The handler updated the IRQ mask. */
1773 B43_WARN_ON(!dev->irq_mask);
1774 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1775 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1776 } else {
1777 /* Device interrupts are currently disabled. That means
1778 * we just ran the hardirq handler and scheduled the
1779 * IRQ thread. The thread will write the IRQ mask when
1780 * it finished, so there's nothing to do here. Writing
1781 * the mask _here_ would incorrectly re-enable IRQs. */
1782 }
1783 }
1784}
1785
Michael Buescha82d9922008-04-04 21:40:06 +02001786static void b43_beacon_update_trigger_work(struct work_struct *work)
1787{
1788 struct b43_wl *wl = container_of(work, struct b43_wl,
1789 beacon_update_trigger);
1790 struct b43_wldev *dev;
1791
1792 mutex_lock(&wl->mutex);
1793 dev = wl->current_dev;
1794 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Rafał Miłecki505fb012011-05-19 15:11:27 +02001795 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001796 /* wl->mutex is enough. */
1797 b43_do_beacon_update_trigger_work(dev);
1798 mmiowb();
1799 } else {
1800 spin_lock_irq(&wl->hardirq_lock);
1801 b43_do_beacon_update_trigger_work(dev);
1802 mmiowb();
1803 spin_unlock_irq(&wl->hardirq_lock);
1804 }
Michael Buescha82d9922008-04-04 21:40:06 +02001805 }
1806 mutex_unlock(&wl->mutex);
1807}
1808
Michael Büscha75d46a2015-01-26 18:26:17 +01001809/* Asynchronously update the packet templates in template RAM. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001810static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001811{
Michael Büscha75d46a2015-01-26 18:26:17 +01001812 struct sk_buff *beacon, *old_beacon;
1813 unsigned long flags;
Johannes Berg9d139c82008-07-09 14:40:37 +02001814
Michael Büscha75d46a2015-01-26 18:26:17 +01001815 /* This is the top half of the asynchronous beacon update.
Michael Buesche66fee62007-12-26 17:47:10 +01001816 * The bottom half is the beacon IRQ.
1817 * Beacon update must be asynchronous to avoid sending an
1818 * invalid beacon. This can happen for example, if the firmware
1819 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001820
Johannes Berg9d139c82008-07-09 14:40:37 +02001821 /* We could modify the existing beacon and set the aid bit in
1822 * the TIM field, but that would probably require resizing and
1823 * moving of data within the beacon template.
1824 * Simply request a new beacon and let mac80211 do the hard work. */
1825 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1826 if (unlikely(!beacon))
1827 return;
1828
Michael Büscha75d46a2015-01-26 18:26:17 +01001829 spin_lock_irqsave(&wl->beacon_lock, flags);
1830 old_beacon = wl->current_beacon;
Michael Buesche66fee62007-12-26 17:47:10 +01001831 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001832 wl->beacon0_uploaded = false;
1833 wl->beacon1_uploaded = false;
Michael Büscha75d46a2015-01-26 18:26:17 +01001834 spin_unlock_irqrestore(&wl->beacon_lock, flags);
1835
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001836 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Büscha75d46a2015-01-26 18:26:17 +01001837
1838 if (old_beacon)
1839 dev_kfree_skb_any(old_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001840}
1841
Michael Buesche4d6b792007-09-18 15:39:42 -04001842static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1843{
1844 b43_time_lock(dev);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001845 if (dev->dev->core_rev >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001846 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1847 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001848 } else {
1849 b43_write16(dev, 0x606, (beacon_int >> 6));
1850 b43_write16(dev, 0x610, beacon_int);
1851 }
1852 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001853 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001854}
1855
Michael Bueschafa83e22008-05-19 23:51:37 +02001856static void b43_handle_firmware_panic(struct b43_wldev *dev)
1857{
1858 u16 reason;
1859
1860 /* Read the register that contains the reason code for the panic. */
1861 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1862 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1863
1864 switch (reason) {
1865 default:
1866 b43dbg(dev->wl, "The panic reason is unknown.\n");
1867 /* fallthrough */
1868 case B43_FWPANIC_DIE:
1869 /* Do not restart the controller or firmware.
1870 * The device is nonfunctional from now on.
1871 * Restarting would result in this panic to trigger again,
1872 * so we avoid that recursion. */
1873 break;
1874 case B43_FWPANIC_RESTART:
1875 b43_controller_restart(dev, "Microcode panic");
1876 break;
1877 }
1878}
1879
Michael Buesche4d6b792007-09-18 15:39:42 -04001880static void handle_irq_ucode_debug(struct b43_wldev *dev)
1881{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001882 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001883 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001884 __le16 *buf;
1885
1886 /* The proprietary firmware doesn't have this IRQ. */
1887 if (!dev->fw.opensource)
1888 return;
1889
Michael Bueschafa83e22008-05-19 23:51:37 +02001890 /* Read the register that contains the reason code for this IRQ. */
1891 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1892
Michael Buesche48b0ee2008-05-17 22:44:35 +02001893 switch (reason) {
1894 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001895 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001896 break;
1897 case B43_DEBUGIRQ_DUMP_SHM:
1898 if (!B43_DEBUG)
1899 break; /* Only with driver debugging enabled. */
1900 buf = kmalloc(4096, GFP_ATOMIC);
1901 if (!buf) {
1902 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1903 goto out;
1904 }
1905 for (i = 0; i < 4096; i += 2) {
1906 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1907 buf[i / 2] = cpu_to_le16(tmp);
1908 }
1909 b43info(dev->wl, "Shared memory dump:\n");
1910 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1911 16, 2, buf, 4096, 1);
1912 kfree(buf);
1913 break;
1914 case B43_DEBUGIRQ_DUMP_REGS:
1915 if (!B43_DEBUG)
1916 break; /* Only with driver debugging enabled. */
1917 b43info(dev->wl, "Microcode register dump:\n");
1918 for (i = 0, cnt = 0; i < 64; i++) {
1919 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1920 if (cnt == 0)
1921 printk(KERN_INFO);
1922 printk("r%02u: 0x%04X ", i, tmp);
1923 cnt++;
1924 if (cnt == 6) {
1925 printk("\n");
1926 cnt = 0;
1927 }
1928 }
1929 printk("\n");
1930 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001931 case B43_DEBUGIRQ_MARKER:
1932 if (!B43_DEBUG)
1933 break; /* Only with driver debugging enabled. */
1934 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1935 B43_MARKER_ID_REG);
1936 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1937 B43_MARKER_LINE_REG);
1938 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1939 "at line number %u\n",
1940 marker_id, marker_line);
1941 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001942 default:
1943 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1944 reason);
1945 }
1946out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001947 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1948 b43_shm_write16(dev, B43_SHM_SCRATCH,
1949 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001950}
1951
Michael Buesch36dbd952009-09-04 22:51:29 +02001952static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001953{
1954 u32 reason;
1955 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1956 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001957 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001958
Michael Buesch36dbd952009-09-04 22:51:29 +02001959 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1960 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001961
1962 reason = dev->irq_reason;
1963 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1964 dma_reason[i] = dev->dma_reason[i];
1965 merged_dma_reason |= dma_reason[i];
1966 }
1967
1968 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1969 b43err(dev->wl, "MAC transmission error\n");
1970
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001971 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001972 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001973 rmb();
1974 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1975 atomic_set(&dev->phy.txerr_cnt,
1976 B43_PHY_TX_BADNESS_LIMIT);
1977 b43err(dev->wl, "Too many PHY TX errors, "
1978 "restarting the controller\n");
1979 b43_controller_restart(dev, "PHY TX errors");
1980 }
1981 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001982
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001983 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK))) {
1984 b43err(dev->wl,
1985 "Fatal DMA error: 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X\n",
1986 dma_reason[0], dma_reason[1],
1987 dma_reason[2], dma_reason[3],
1988 dma_reason[4], dma_reason[5]);
1989 b43err(dev->wl, "This device does not support DMA "
Larry Fingerbb64d952010-06-19 08:29:08 -05001990 "on your system. It will now be switched to PIO.\n");
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001991 /* Fall back to PIO transfers if we get fatal DMA errors! */
1992 dev->use_pio = true;
1993 b43_controller_restart(dev, "DMA error");
1994 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001995 }
1996
1997 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1998 handle_irq_ucode_debug(dev);
1999 if (reason & B43_IRQ_TBTT_INDI)
2000 handle_irq_tbtt_indication(dev);
2001 if (reason & B43_IRQ_ATIM_END)
2002 handle_irq_atim_end(dev);
2003 if (reason & B43_IRQ_BEACON)
2004 handle_irq_beacon(dev);
2005 if (reason & B43_IRQ_PMQ)
2006 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02002007 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
2008 ;/* TODO */
2009 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04002010 handle_irq_noise(dev);
2011
2012 /* Check the DMA reason registers for received data. */
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002013 if (dma_reason[0] & B43_DMAIRQ_RDESC_UFLOW) {
2014 if (B43_DEBUG)
2015 b43warn(dev->wl, "RX descriptor underrun\n");
2016 b43_dma_handle_rx_overflow(dev->dma.rx_ring);
2017 }
Michael Buesch5100d5a2008-03-29 21:01:16 +01002018 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
2019 if (b43_using_pio_transfers(dev))
2020 b43_pio_rx(dev->pio.rx_queue);
2021 else
2022 b43_dma_rx(dev->dma.rx_ring);
2023 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002024 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
2025 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01002026 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04002027 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
2028 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
2029
Michael Buesch21954c32007-09-27 15:31:40 +02002030 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04002031 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002032
Michael Buesch36dbd952009-09-04 22:51:29 +02002033 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02002034 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesch990b86f2009-09-12 00:48:03 +02002035
2036#if B43_DEBUG
2037 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
2038 dev->irq_count++;
2039 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
2040 if (reason & (1 << i))
2041 dev->irq_bit_count[i]++;
2042 }
2043 }
2044#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002045}
2046
Michael Buesch36dbd952009-09-04 22:51:29 +02002047/* Interrupt thread handler. Handles device interrupts in thread context. */
2048static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04002049{
Michael Buesche4d6b792007-09-18 15:39:42 -04002050 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02002051
2052 mutex_lock(&dev->wl->mutex);
2053 b43_do_interrupt_thread(dev);
2054 mmiowb();
2055 mutex_unlock(&dev->wl->mutex);
2056
2057 return IRQ_HANDLED;
2058}
2059
2060static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
2061{
Michael Buesche4d6b792007-09-18 15:39:42 -04002062 u32 reason;
2063
Michael Buesch36dbd952009-09-04 22:51:29 +02002064 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
2065 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002066
Michael Buesche4d6b792007-09-18 15:39:42 -04002067 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2068 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02002069 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02002070 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04002071 if (!reason)
Sebastian Andrzej Siewiorcae56142011-07-07 21:58:10 +02002072 return IRQ_NONE;
Michael Buesche4d6b792007-09-18 15:39:42 -04002073
2074 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002075 & 0x0001FC00;
Michael Buesche4d6b792007-09-18 15:39:42 -04002076 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
2077 & 0x0000DC00;
2078 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
2079 & 0x0000DC00;
2080 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
2081 & 0x0001DC00;
2082 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
2083 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002084/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04002085 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
2086 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002087*/
Michael Buesche4d6b792007-09-18 15:39:42 -04002088
Michael Buesch36dbd952009-09-04 22:51:29 +02002089 /* ACK the interrupt. */
2090 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
2091 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
2092 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
2093 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
2094 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
2095 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
2096/* Unused ring
2097 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
2098*/
2099
2100 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02002101 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02002102 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002103 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02002104
2105 return IRQ_WAKE_THREAD;
2106}
2107
2108/* Interrupt handler top-half. This runs with interrupts disabled. */
2109static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
2110{
2111 struct b43_wldev *dev = dev_id;
2112 irqreturn_t ret;
2113
2114 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2115 return IRQ_NONE;
2116
2117 spin_lock(&dev->wl->hardirq_lock);
2118 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002119 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02002120 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04002121
2122 return ret;
2123}
2124
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002125/* SDIO interrupt handler. This runs in process context. */
2126static void b43_sdio_interrupt_handler(struct b43_wldev *dev)
2127{
2128 struct b43_wl *wl = dev->wl;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002129 irqreturn_t ret;
2130
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002131 mutex_lock(&wl->mutex);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002132
2133 ret = b43_do_interrupt(dev);
2134 if (ret == IRQ_WAKE_THREAD)
2135 b43_do_interrupt_thread(dev);
2136
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002137 mutex_unlock(&wl->mutex);
2138}
2139
Michael Buesch1a9f5092009-01-23 21:21:51 +01002140void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002141{
2142 release_firmware(fw->data);
2143 fw->data = NULL;
2144 fw->filename = NULL;
2145}
2146
Michael Buesche4d6b792007-09-18 15:39:42 -04002147static void b43_release_firmware(struct b43_wldev *dev)
2148{
Larry Finger0673eff2014-01-12 15:11:38 -06002149 complete(&dev->fw_load_complete);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002150 b43_do_release_fw(&dev->fw.ucode);
2151 b43_do_release_fw(&dev->fw.pcm);
2152 b43_do_release_fw(&dev->fw.initvals);
2153 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04002154}
2155
Michael Buescheb189d8b2008-01-28 14:47:41 -08002156static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04002157{
Hannes Ederfc68ed42009-02-14 11:50:06 +00002158 const char text[] =
2159 "You must go to " \
2160 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
2161 "and download the correct firmware for this driver version. " \
2162 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08002163
Michael Buescheb189d8b2008-01-28 14:47:41 -08002164 if (error)
2165 b43err(wl, text);
2166 else
2167 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04002168}
2169
Larry Finger5e20a4b2012-12-20 15:55:01 -06002170static void b43_fw_cb(const struct firmware *firmware, void *context)
2171{
2172 struct b43_request_fw_context *ctx = context;
2173
2174 ctx->blob = firmware;
Larry Finger0673eff2014-01-12 15:11:38 -06002175 complete(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002176}
2177
Michael Buesch1a9f5092009-01-23 21:21:51 +01002178int b43_do_request_fw(struct b43_request_fw_context *ctx,
2179 const char *name,
Larry Finger5e20a4b2012-12-20 15:55:01 -06002180 struct b43_firmware_file *fw, bool async)
Michael Buesche4d6b792007-09-18 15:39:42 -04002181{
Michael Buesche4d6b792007-09-18 15:39:42 -04002182 struct b43_fw_header *hdr;
2183 u32 size;
2184 int err;
2185
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002186 if (!name) {
2187 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002188 /* FIXME: We should probably keep it anyway, to save some headache
2189 * on suspend/resume with multiband devices. */
2190 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04002191 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002192 }
2193 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002194 if ((fw->type == ctx->req_type) &&
2195 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002196 return 0; /* Already have this fw. */
2197 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002198 /* FIXME: We should probably do this later after we successfully
2199 * got the new fw. This could reduce headache with multiband devices.
2200 * We could also redesign this to cache the firmware for all possible
2201 * bands all the time. */
2202 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002203 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002204
Michael Buesch1a9f5092009-01-23 21:21:51 +01002205 switch (ctx->req_type) {
2206 case B43_FWTYPE_PROPRIETARY:
2207 snprintf(ctx->fwname, sizeof(ctx->fwname),
2208 "b43%s/%s.fw",
2209 modparam_fwpostfix, name);
2210 break;
2211 case B43_FWTYPE_OPENSOURCE:
2212 snprintf(ctx->fwname, sizeof(ctx->fwname),
2213 "b43-open%s/%s.fw",
2214 modparam_fwpostfix, name);
2215 break;
2216 default:
2217 B43_WARN_ON(1);
2218 return -ENOSYS;
2219 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002220 if (async) {
2221 /* do this part asynchronously */
Larry Finger0673eff2014-01-12 15:11:38 -06002222 init_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002223 err = request_firmware_nowait(THIS_MODULE, 1, ctx->fwname,
2224 ctx->dev->dev->dev, GFP_KERNEL,
2225 ctx, b43_fw_cb);
2226 if (err < 0) {
2227 pr_err("Unable to load firmware\n");
2228 return err;
2229 }
Larry Finger0673eff2014-01-12 15:11:38 -06002230 wait_for_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002231 if (ctx->blob)
2232 goto fw_ready;
2233 /* On some ARM systems, the async request will fail, but the next sync
Larry Finger0673eff2014-01-12 15:11:38 -06002234 * request works. For this reason, we fall through here
Larry Finger5e20a4b2012-12-20 15:55:01 -06002235 */
2236 }
2237 err = request_firmware(&ctx->blob, ctx->fwname,
2238 ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002239 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002240 snprintf(ctx->errors[ctx->req_type],
2241 sizeof(ctx->errors[ctx->req_type]),
Larry Finger5e20a4b2012-12-20 15:55:01 -06002242 "Firmware file \"%s\" not found\n",
2243 ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002244 return err;
2245 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002246 snprintf(ctx->errors[ctx->req_type],
2247 sizeof(ctx->errors[ctx->req_type]),
2248 "Firmware file \"%s\" request failed (err=%d)\n",
2249 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002250 return err;
2251 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002252fw_ready:
2253 if (ctx->blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002254 goto err_format;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002255 hdr = (struct b43_fw_header *)(ctx->blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002256 switch (hdr->type) {
2257 case B43_FW_TYPE_UCODE:
2258 case B43_FW_TYPE_PCM:
2259 size = be32_to_cpu(hdr->size);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002260 if (size != ctx->blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002261 goto err_format;
2262 /* fallthrough */
2263 case B43_FW_TYPE_IV:
2264 if (hdr->ver != 1)
2265 goto err_format;
2266 break;
2267 default:
2268 goto err_format;
2269 }
2270
Larry Finger5e20a4b2012-12-20 15:55:01 -06002271 fw->data = ctx->blob;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002272 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002273 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002274
2275 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002276
2277err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002278 snprintf(ctx->errors[ctx->req_type],
2279 sizeof(ctx->errors[ctx->req_type]),
2280 "Firmware file \"%s\" format error.\n", ctx->fwname);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002281 release_firmware(ctx->blob);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002282
Michael Buesche4d6b792007-09-18 15:39:42 -04002283 return -EPROTO;
2284}
2285
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002286/* http://bcm-v4.sipsolutions.net/802.11/Init/Firmware */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002287static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002288{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002289 struct b43_wldev *dev = ctx->dev;
2290 struct b43_firmware *fw = &ctx->dev->fw;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002291 struct b43_phy *phy = &dev->phy;
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002292 const u8 rev = ctx->dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04002293 const char *filename;
Michael Buesche4d6b792007-09-18 15:39:42 -04002294 int err;
2295
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002296 /* Get microcode */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002297 filename = NULL;
2298 switch (rev) {
2299 case 42:
2300 if (phy->type == B43_PHYTYPE_AC)
2301 filename = "ucode42";
2302 break;
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002303 case 40:
2304 if (phy->type == B43_PHYTYPE_AC)
2305 filename = "ucode40";
2306 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002307 case 33:
2308 if (phy->type == B43_PHYTYPE_LCN40)
2309 filename = "ucode33_lcn40";
2310 break;
2311 case 30:
2312 if (phy->type == B43_PHYTYPE_N)
2313 filename = "ucode30_mimo";
2314 break;
2315 case 29:
2316 if (phy->type == B43_PHYTYPE_HT)
2317 filename = "ucode29_mimo";
2318 break;
2319 case 26:
2320 if (phy->type == B43_PHYTYPE_HT)
2321 filename = "ucode26_mimo";
2322 break;
2323 case 28:
2324 case 25:
2325 if (phy->type == B43_PHYTYPE_N)
2326 filename = "ucode25_mimo";
2327 else if (phy->type == B43_PHYTYPE_LCN)
2328 filename = "ucode25_lcn";
2329 break;
2330 case 24:
2331 if (phy->type == B43_PHYTYPE_LCN)
2332 filename = "ucode24_lcn";
2333 break;
2334 case 23:
2335 if (phy->type == B43_PHYTYPE_N)
2336 filename = "ucode16_mimo";
2337 break;
2338 case 16 ... 19:
2339 if (phy->type == B43_PHYTYPE_N)
2340 filename = "ucode16_mimo";
2341 else if (phy->type == B43_PHYTYPE_LP)
2342 filename = "ucode16_lp";
2343 break;
2344 case 15:
Gábor Stefanik759b9732009-08-14 14:39:53 +02002345 filename = "ucode15";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002346 break;
2347 case 14:
2348 filename = "ucode14";
2349 break;
2350 case 13:
2351 filename = "ucode13";
2352 break;
2353 case 11 ... 12:
2354 filename = "ucode11";
2355 break;
2356 case 5 ... 10:
2357 filename = "ucode5";
2358 break;
Rafał Miłecki6ff1e5c2011-07-06 17:41:55 +02002359 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002360 if (!filename)
2361 goto err_no_ucode;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002362 err = b43_do_request_fw(ctx, filename, &fw->ucode, true);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002363 if (err)
2364 goto err_load;
2365
2366 /* Get PCM code */
2367 if ((rev >= 5) && (rev <= 10))
2368 filename = "pcm5";
2369 else if (rev >= 11)
2370 filename = NULL;
2371 else
2372 goto err_no_pcm;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002373 fw->pcm_request_failed = false;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002374 err = b43_do_request_fw(ctx, filename, &fw->pcm, false);
Michael Buesch68217832008-05-17 23:43:57 +02002375 if (err == -ENOENT) {
2376 /* We did not find a PCM file? Not fatal, but
2377 * core rev <= 10 must do without hwcrypto then. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002378 fw->pcm_request_failed = true;
Michael Buesch68217832008-05-17 23:43:57 +02002379 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002380 goto err_load;
2381
2382 /* Get initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002383 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002384 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002385 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002386 if (rev == 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002387 filename = "b0g0initvals13";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002388 else if (rev >= 5 && rev <= 10)
2389 filename = "b0g0initvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002390 break;
2391 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002392 if (rev == 30)
2393 filename = "n16initvals30";
2394 else if (rev == 28 || rev == 25)
2395 filename = "n0initvals25";
2396 else if (rev == 24)
2397 filename = "n0initvals24";
2398 else if (rev == 23)
2399 filename = "n0initvals16"; /* What about n0initvals22? */
2400 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002401 filename = "n0initvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002402 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002403 filename = "n0initvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002404 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002405 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002406 if (rev >= 16 && rev <= 18)
2407 filename = "lp0initvals16";
2408 else if (rev == 15)
2409 filename = "lp0initvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002410 else if (rev == 14)
2411 filename = "lp0initvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002412 else if (rev == 13)
2413 filename = "lp0initvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002414 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002415 case B43_PHYTYPE_HT:
2416 if (rev == 29)
2417 filename = "ht0initvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002418 else if (rev == 26)
2419 filename = "ht0initvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002420 break;
2421 case B43_PHYTYPE_LCN:
2422 if (rev == 24)
2423 filename = "lcn0initvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002424 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002425 case B43_PHYTYPE_LCN40:
2426 if (rev == 33)
2427 filename = "lcn400initvals33";
2428 break;
2429 case B43_PHYTYPE_AC:
2430 if (rev == 42)
2431 filename = "ac1initvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002432 else if (rev == 40)
2433 filename = "ac0initvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002434 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002435 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002436 if (!filename)
2437 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002438 err = b43_do_request_fw(ctx, filename, &fw->initvals, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002439 if (err)
2440 goto err_load;
2441
2442 /* Get bandswitch initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002443 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002444 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002445 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002446 if (rev == 13)
2447 filename = "b0g0bsinitvals13";
2448 else if (rev >= 5 && rev <= 10)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002449 filename = "b0g0bsinitvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002450 break;
2451 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002452 if (rev == 30)
2453 filename = "n16bsinitvals30";
2454 else if (rev == 28 || rev == 25)
2455 filename = "n0bsinitvals25";
2456 else if (rev == 24)
2457 filename = "n0bsinitvals24";
2458 else if (rev == 23)
2459 filename = "n0bsinitvals16"; /* What about n0bsinitvals22? */
2460 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002461 filename = "n0bsinitvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002462 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002463 filename = "n0bsinitvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002464 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002465 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002466 if (rev >= 16 && rev <= 18)
2467 filename = "lp0bsinitvals16";
2468 else if (rev == 15)
2469 filename = "lp0bsinitvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002470 else if (rev == 14)
2471 filename = "lp0bsinitvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002472 else if (rev == 13)
2473 filename = "lp0bsinitvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002474 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002475 case B43_PHYTYPE_HT:
2476 if (rev == 29)
2477 filename = "ht0bsinitvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002478 else if (rev == 26)
2479 filename = "ht0bsinitvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002480 break;
2481 case B43_PHYTYPE_LCN:
2482 if (rev == 24)
2483 filename = "lcn0bsinitvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002484 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002485 case B43_PHYTYPE_LCN40:
2486 if (rev == 33)
2487 filename = "lcn400bsinitvals33";
2488 break;
2489 case B43_PHYTYPE_AC:
2490 if (rev == 42)
2491 filename = "ac1bsinitvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002492 else if (rev == 40)
2493 filename = "ac0bsinitvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002494 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002495 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002496 if (!filename)
2497 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002498 err = b43_do_request_fw(ctx, filename, &fw->initvals_band, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002499 if (err)
2500 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002501
Johannes Berg097b0e12012-07-17 17:12:29 +02002502 fw->opensource = (ctx->req_type == B43_FWTYPE_OPENSOURCE);
2503
Michael Buesche4d6b792007-09-18 15:39:42 -04002504 return 0;
2505
Michael Buesche4d6b792007-09-18 15:39:42 -04002506err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002507 err = ctx->fatal_failure = -EOPNOTSUPP;
2508 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2509 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002510 goto error;
2511
2512err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002513 err = ctx->fatal_failure = -EOPNOTSUPP;
2514 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2515 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002516 goto error;
2517
2518err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002519 err = ctx->fatal_failure = -EOPNOTSUPP;
2520 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2521 "is required for your device (wl-core rev %u)\n", rev);
2522 goto error;
2523
2524err_load:
2525 /* We failed to load this firmware image. The error message
2526 * already is in ctx->errors. Return and let our caller decide
2527 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002528 goto error;
2529
2530error:
2531 b43_release_firmware(dev);
2532 return err;
2533}
2534
Larry Finger6b6fa582012-03-08 22:27:46 -06002535static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl);
2536static void b43_one_core_detach(struct b43_bus_dev *dev);
Larry Finger09164042014-01-12 15:11:37 -06002537static int b43_rng_init(struct b43_wl *wl);
Larry Finger6b6fa582012-03-08 22:27:46 -06002538
2539static void b43_request_firmware(struct work_struct *work)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002540{
Larry Finger6b6fa582012-03-08 22:27:46 -06002541 struct b43_wl *wl = container_of(work,
2542 struct b43_wl, firmware_load);
2543 struct b43_wldev *dev = wl->current_dev;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002544 struct b43_request_fw_context *ctx;
2545 unsigned int i;
2546 int err;
2547 const char *errmsg;
2548
2549 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2550 if (!ctx)
Larry Finger6b6fa582012-03-08 22:27:46 -06002551 return;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002552 ctx->dev = dev;
2553
2554 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2555 err = b43_try_request_fw(ctx);
2556 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002557 goto start_ieee80211; /* Successfully loaded it. */
2558 /* Was fw version known? */
2559 if (ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002560 goto out;
2561
Larry Finger6b6fa582012-03-08 22:27:46 -06002562 /* proprietary fw not found, try open source */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002563 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2564 err = b43_try_request_fw(ctx);
2565 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002566 goto start_ieee80211; /* Successfully loaded it. */
2567 if(ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002568 goto out;
2569
2570 /* Could not find a usable firmware. Print the errors. */
2571 for (i = 0; i < B43_NR_FWTYPES; i++) {
2572 errmsg = ctx->errors[i];
2573 if (strlen(errmsg))
Kees Cooke0e29b62013-05-10 14:48:21 -07002574 b43err(dev->wl, "%s", errmsg);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002575 }
2576 b43_print_fw_helptext(dev->wl, 1);
Larry Finger6b6fa582012-03-08 22:27:46 -06002577 goto out;
2578
2579start_ieee80211:
Johannes Berg097b0e12012-07-17 17:12:29 +02002580 wl->hw->queues = B43_QOS_QUEUE_NUM;
2581 if (!modparam_qos || dev->fw.opensource)
2582 wl->hw->queues = 1;
2583
Larry Finger6b6fa582012-03-08 22:27:46 -06002584 err = ieee80211_register_hw(wl->hw);
2585 if (err)
2586 goto err_one_core_detach;
Oleksij Rempele64add22012-06-05 20:39:32 +02002587 wl->hw_registred = true;
Larry Finger6b6fa582012-03-08 22:27:46 -06002588 b43_leds_register(wl->current_dev);
Larry Finger09164042014-01-12 15:11:37 -06002589
2590 /* Register HW RNG driver */
2591 b43_rng_init(wl);
2592
Larry Finger6b6fa582012-03-08 22:27:46 -06002593 goto out;
2594
2595err_one_core_detach:
2596 b43_one_core_detach(dev->dev);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002597
2598out:
2599 kfree(ctx);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002600}
2601
Michael Buesche4d6b792007-09-18 15:39:42 -04002602static int b43_upload_microcode(struct b43_wldev *dev)
2603{
John W. Linville652caa52010-07-29 13:27:28 -04002604 struct wiphy *wiphy = dev->wl->hw->wiphy;
Michael Buesche4d6b792007-09-18 15:39:42 -04002605 const size_t hdr_len = sizeof(struct b43_fw_header);
2606 const __be32 *data;
2607 unsigned int i, len;
2608 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002609 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002610 int err = 0;
2611
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002612 /* Jump the microcode PSM to offset 0 */
2613 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2614 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2615 macctl |= B43_MACCTL_PSM_JMP0;
2616 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2617 /* Zero out all microcode PSM registers and shared memory. */
2618 for (i = 0; i < 64; i++)
2619 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2620 for (i = 0; i < 4096; i += 2)
2621 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2622
Michael Buesche4d6b792007-09-18 15:39:42 -04002623 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002624 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2625 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002626 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2627 for (i = 0; i < len; i++) {
2628 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2629 udelay(10);
2630 }
2631
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002632 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002633 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002634 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2635 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002636 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2637 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2638 /* No need for autoinc bit in SHM_HW */
2639 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2640 for (i = 0; i < len; i++) {
2641 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2642 udelay(10);
2643 }
2644 }
2645
2646 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002647
2648 /* Start the microcode PSM */
Rafał Miłecki50566352012-01-02 19:31:21 +01002649 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_JMP0,
2650 B43_MACCTL_PSM_RUN);
Michael Buesche4d6b792007-09-18 15:39:42 -04002651
2652 /* Wait for the microcode to load and respond */
2653 i = 0;
2654 while (1) {
2655 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2656 if (tmp == B43_IRQ_MAC_SUSPENDED)
2657 break;
2658 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002659 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002660 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002661 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002662 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002663 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002664 }
Michael Buesche175e992009-09-11 18:31:32 +02002665 msleep(50);
Michael Buesche4d6b792007-09-18 15:39:42 -04002666 }
2667 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2668
2669 /* Get and check the revisions. */
2670 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2671 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2672 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2673 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2674
2675 if (fwrev <= 0x128) {
2676 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2677 "binary drivers older than version 4.x is unsupported. "
2678 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002679 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002680 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002681 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002682 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002683 dev->fw.rev = fwrev;
2684 dev->fw.patch = fwpatch;
Rafał Miłecki5d852902011-08-11 15:07:16 +02002685 if (dev->fw.rev >= 598)
2686 dev->fw.hdr_format = B43_FW_HDR_598;
2687 else if (dev->fw.rev >= 410)
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002688 dev->fw.hdr_format = B43_FW_HDR_410;
2689 else
2690 dev->fw.hdr_format = B43_FW_HDR_351;
Johannes Berg097b0e12012-07-17 17:12:29 +02002691 WARN_ON(dev->fw.opensource != (fwdate == 0xFFFF));
Michael Buesche48b0ee2008-05-17 22:44:35 +02002692
Johannes Berg097b0e12012-07-17 17:12:29 +02002693 dev->qos_enabled = dev->wl->hw->queues > 1;
Michael Buesch403a3a12009-06-08 21:04:57 +02002694 /* Default to firmware/hardware crypto acceleration. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002695 dev->hwcrypto_enabled = true;
Michael Buesch403a3a12009-06-08 21:04:57 +02002696
Michael Buesche48b0ee2008-05-17 22:44:35 +02002697 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002698 u16 fwcapa;
2699
Michael Buesche48b0ee2008-05-17 22:44:35 +02002700 /* Patchlevel info is encoded in the "time" field. */
2701 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002702 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2703 dev->fw.rev, dev->fw.patch);
2704
2705 fwcapa = b43_fwcapa_read(dev);
2706 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2707 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2708 /* Disable hardware crypto and fall back to software crypto. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002709 dev->hwcrypto_enabled = false;
Michael Buesch403a3a12009-06-08 21:04:57 +02002710 }
Johannes Berg097b0e12012-07-17 17:12:29 +02002711 /* adding QoS support should use an offline discovery mechanism */
2712 WARN(fwcapa & B43_FWCAPA_QOS, "QoS in OpenFW not supported\n");
Michael Buesche48b0ee2008-05-17 22:44:35 +02002713 } else {
2714 b43info(dev->wl, "Loading firmware version %u.%u "
2715 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2716 fwrev, fwpatch,
2717 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2718 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002719 if (dev->fw.pcm_request_failed) {
2720 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2721 "Hardware accelerated cryptography is disabled.\n");
2722 b43_print_fw_helptext(dev->wl, 0);
2723 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002724 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002725
John W. Linville652caa52010-07-29 13:27:28 -04002726 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
2727 dev->fw.rev, dev->fw.patch);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002728 wiphy->hw_version = dev->dev->core_id;
John W. Linville652caa52010-07-29 13:27:28 -04002729
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002730 if (dev->fw.hdr_format == B43_FW_HDR_351) {
Michael Bueschc5572892008-12-27 18:26:39 +01002731 /* We're over the deadline, but we keep support for old fw
2732 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002733 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002734 "Support for old firmware will be removed soon "
2735 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002736 b43_print_fw_helptext(dev->wl, 0);
2737 }
2738
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002739 return 0;
2740
2741error:
Rafał Miłecki50566352012-01-02 19:31:21 +01002742 /* Stop the microcode PSM. */
2743 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
2744 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002745
Michael Buesche4d6b792007-09-18 15:39:42 -04002746 return err;
2747}
2748
2749static int b43_write_initvals(struct b43_wldev *dev,
2750 const struct b43_iv *ivals,
2751 size_t count,
2752 size_t array_size)
2753{
2754 const struct b43_iv *iv;
2755 u16 offset;
2756 size_t i;
2757 bool bit32;
2758
2759 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2760 iv = ivals;
2761 for (i = 0; i < count; i++) {
2762 if (array_size < sizeof(iv->offset_size))
2763 goto err_format;
2764 array_size -= sizeof(iv->offset_size);
2765 offset = be16_to_cpu(iv->offset_size);
2766 bit32 = !!(offset & B43_IV_32BIT);
2767 offset &= B43_IV_OFFSET_MASK;
2768 if (offset >= 0x1000)
2769 goto err_format;
2770 if (bit32) {
2771 u32 value;
2772
2773 if (array_size < sizeof(iv->data.d32))
2774 goto err_format;
2775 array_size -= sizeof(iv->data.d32);
2776
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002777 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002778 b43_write32(dev, offset, value);
2779
2780 iv = (const struct b43_iv *)((const uint8_t *)iv +
2781 sizeof(__be16) +
2782 sizeof(__be32));
2783 } else {
2784 u16 value;
2785
2786 if (array_size < sizeof(iv->data.d16))
2787 goto err_format;
2788 array_size -= sizeof(iv->data.d16);
2789
2790 value = be16_to_cpu(iv->data.d16);
2791 b43_write16(dev, offset, value);
2792
2793 iv = (const struct b43_iv *)((const uint8_t *)iv +
2794 sizeof(__be16) +
2795 sizeof(__be16));
2796 }
2797 }
2798 if (array_size)
2799 goto err_format;
2800
2801 return 0;
2802
2803err_format:
2804 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002805 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002806
2807 return -EPROTO;
2808}
2809
2810static int b43_upload_initvals(struct b43_wldev *dev)
2811{
2812 const size_t hdr_len = sizeof(struct b43_fw_header);
2813 const struct b43_fw_header *hdr;
2814 struct b43_firmware *fw = &dev->fw;
2815 const struct b43_iv *ivals;
2816 size_t count;
Michael Buesche4d6b792007-09-18 15:39:42 -04002817
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002818 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2819 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002820 count = be32_to_cpu(hdr->size);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002821 return b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002822 fw->initvals.data->size - hdr_len);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002823}
Michael Buesche4d6b792007-09-18 15:39:42 -04002824
Rafał Miłecki0f684232014-05-17 23:24:53 +02002825static int b43_upload_initvals_band(struct b43_wldev *dev)
2826{
2827 const size_t hdr_len = sizeof(struct b43_fw_header);
2828 const struct b43_fw_header *hdr;
2829 struct b43_firmware *fw = &dev->fw;
2830 const struct b43_iv *ivals;
2831 size_t count;
2832
2833 if (!fw->initvals_band.data)
2834 return 0;
2835
2836 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2837 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2838 count = be32_to_cpu(hdr->size);
2839 return b43_write_initvals(dev, ivals, count,
2840 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002841}
2842
2843/* Initialize the GPIOs
2844 * http://bcm-specs.sipsolutions.net/GPIO
2845 */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002846
2847#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002848static struct ssb_device *b43_ssb_gpio_dev(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002849{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02002850 struct ssb_bus *bus = dev->dev->sdev->bus;
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002851
2852#ifdef CONFIG_SSB_DRIVER_PCICORE
2853 return (bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev);
2854#else
2855 return bus->chipco.dev;
2856#endif
2857}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002858#endif
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002859
Michael Buesche4d6b792007-09-18 15:39:42 -04002860static int b43_gpio_init(struct b43_wldev *dev)
2861{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002862#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002863 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002864#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002865 u32 mask, set;
2866
Rafał Miłecki50566352012-01-02 19:31:21 +01002867 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_GPOUTSMSK, 0);
2868 b43_maskset16(dev, B43_MMIO_GPIO_MASK, ~0, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04002869
2870 mask = 0x0000001F;
2871 set = 0x0000000F;
Rafał Miłeckic244e082011-05-18 02:06:41 +02002872 if (dev->dev->chip_id == 0x4301) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002873 mask |= 0x0060;
2874 set |= 0x0060;
Rafał Miłecki828afd22012-07-23 22:57:01 +02002875 } else if (dev->dev->chip_id == 0x5354) {
2876 /* Don't allow overtaking buttons GPIOs */
2877 set &= 0x2; /* 0x2 is LED GPIO on BCM5354 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002878 }
Rafał Miłecki828afd22012-07-23 22:57:01 +02002879
Michael Buesche4d6b792007-09-18 15:39:42 -04002880 if (0 /* FIXME: conditional unknown */ ) {
2881 b43_write16(dev, B43_MMIO_GPIO_MASK,
2882 b43_read16(dev, B43_MMIO_GPIO_MASK)
2883 | 0x0100);
Rafał Miłecki828afd22012-07-23 22:57:01 +02002884 /* BT Coexistance Input */
2885 mask |= 0x0080;
2886 set |= 0x0080;
2887 /* BT Coexistance Out */
2888 mask |= 0x0100;
2889 set |= 0x0100;
Michael Buesche4d6b792007-09-18 15:39:42 -04002890 }
Rafał Miłecki05814832011-05-18 02:06:39 +02002891 if (dev->dev->bus_sprom->boardflags_lo & B43_BFL_PACTRL) {
Rafał Miłecki828afd22012-07-23 22:57:01 +02002892 /* PA is controlled by gpio 9, let ucode handle it */
Michael Buesche4d6b792007-09-18 15:39:42 -04002893 b43_write16(dev, B43_MMIO_GPIO_MASK,
2894 b43_read16(dev, B43_MMIO_GPIO_MASK)
2895 | 0x0200);
2896 mask |= 0x0200;
2897 set |= 0x0200;
2898 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002899
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002900 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002901#ifdef CONFIG_B43_BCMA
2902 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002903 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, mask, set);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002904 break;
2905#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002906#ifdef CONFIG_B43_SSB
2907 case B43_BUS_SSB:
2908 gpiodev = b43_ssb_gpio_dev(dev);
2909 if (gpiodev)
2910 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2911 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
Rafał Miłecki828afd22012-07-23 22:57:01 +02002912 & ~mask) | set);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002913 break;
2914#endif
2915 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002916
2917 return 0;
2918}
2919
2920/* Turn off all GPIO stuff. Call this on module unload, for example. */
2921static void b43_gpio_cleanup(struct b43_wldev *dev)
2922{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002923#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002924 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002925#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002926
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002927 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002928#ifdef CONFIG_B43_BCMA
2929 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002930 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, ~0, 0);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002931 break;
2932#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002933#ifdef CONFIG_B43_SSB
2934 case B43_BUS_SSB:
2935 gpiodev = b43_ssb_gpio_dev(dev);
2936 if (gpiodev)
2937 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2938 break;
2939#endif
2940 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002941}
2942
2943/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002944void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002945{
Michael Buesch923fd702008-06-20 18:02:08 +02002946 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2947 u16 fwstate;
2948
2949 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2950 B43_SHM_SH_UCODESTAT);
2951 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2952 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2953 b43err(dev->wl, "b43_mac_enable(): The firmware "
2954 "should be suspended, but current state is %u\n",
2955 fwstate);
2956 }
2957 }
2958
Michael Buesche4d6b792007-09-18 15:39:42 -04002959 dev->mac_suspended--;
2960 B43_WARN_ON(dev->mac_suspended < 0);
2961 if (dev->mac_suspended == 0) {
Rafał Miłecki50566352012-01-02 19:31:21 +01002962 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_ENABLED);
Michael Buesche4d6b792007-09-18 15:39:42 -04002963 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2964 B43_IRQ_MAC_SUSPENDED);
2965 /* Commit writes */
2966 b43_read32(dev, B43_MMIO_MACCTL);
2967 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2968 b43_power_saving_ctl_bits(dev, 0);
2969 }
2970}
2971
2972/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002973void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002974{
2975 int i;
2976 u32 tmp;
2977
Michael Buesch05b64b32007-09-28 16:19:03 +02002978 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002979 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002980
Michael Buesche4d6b792007-09-18 15:39:42 -04002981 if (dev->mac_suspended == 0) {
2982 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Rafał Miłecki50566352012-01-02 19:31:21 +01002983 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_ENABLED, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04002984 /* force pci to flush the write */
2985 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02002986 for (i = 35; i; i--) {
2987 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2988 if (tmp & B43_IRQ_MAC_SUSPENDED)
2989 goto out;
2990 udelay(10);
2991 }
2992 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02002993 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002994 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2995 if (tmp & B43_IRQ_MAC_SUSPENDED)
2996 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02002997 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002998 }
2999 b43err(dev->wl, "MAC suspend failed\n");
3000 }
Michael Buesch05b64b32007-09-28 16:19:03 +02003001out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003002 dev->mac_suspended++;
3003}
3004
Rafał Miłecki858a1652011-05-10 16:05:33 +02003005/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MacPhyClkSet */
3006void b43_mac_phy_clock_set(struct b43_wldev *dev, bool on)
3007{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003008 u32 tmp;
3009
3010 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003011#ifdef CONFIG_B43_BCMA
3012 case B43_BUS_BCMA:
Rafał Miłecki36677872011-07-16 18:27:55 +02003013 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003014 if (on)
3015 tmp |= B43_BCMA_IOCTL_MACPHYCLKEN;
3016 else
3017 tmp &= ~B43_BCMA_IOCTL_MACPHYCLKEN;
Rafał Miłecki36677872011-07-16 18:27:55 +02003018 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003019 break;
3020#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003021#ifdef CONFIG_B43_SSB
3022 case B43_BUS_SSB:
3023 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3024 if (on)
3025 tmp |= B43_TMSLOW_MACPHYCLKEN;
3026 else
3027 tmp &= ~B43_TMSLOW_MACPHYCLKEN;
3028 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3029 break;
3030#endif
3031 }
Rafał Miłecki858a1652011-05-10 16:05:33 +02003032}
3033
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02003034/* brcms_b_switch_macfreq */
3035void b43_mac_switch_freq(struct b43_wldev *dev, u8 spurmode)
3036{
3037 u16 chip_id = dev->dev->chip_id;
3038
Rafał Miłeckibc944502014-09-10 09:07:13 +02003039 if (chip_id == BCMA_CHIP_ID_BCM4331) {
3040 switch (spurmode) {
3041 case 2: /* 168 Mhz: 2^26/168 = 0x61862 */
3042 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x1862);
3043 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3044 break;
3045 case 1: /* 164 Mhz: 2^26/164 = 0x63e70 */
3046 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x3e70);
3047 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3048 break;
3049 default: /* 160 Mhz: 2^26/160 = 0x66666 */
3050 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x6666);
3051 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3052 break;
3053 }
3054 } else if (chip_id == BCMA_CHIP_ID_BCM43131 ||
Rafał Miłeckia67d19d2014-07-24 15:29:18 +02003055 chip_id == BCMA_CHIP_ID_BCM43217 ||
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02003056 chip_id == BCMA_CHIP_ID_BCM43222 ||
3057 chip_id == BCMA_CHIP_ID_BCM43224 ||
3058 chip_id == BCMA_CHIP_ID_BCM43225 ||
3059 chip_id == BCMA_CHIP_ID_BCM43227 ||
3060 chip_id == BCMA_CHIP_ID_BCM43228) {
3061 switch (spurmode) {
3062 case 2: /* 126 Mhz */
3063 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x2082);
3064 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3065 break;
3066 case 1: /* 123 Mhz */
3067 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x5341);
3068 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3069 break;
3070 default: /* 120 Mhz */
3071 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x8889);
3072 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3073 break;
3074 }
3075 } else if (dev->phy.type == B43_PHYTYPE_LCN) {
3076 switch (spurmode) {
3077 case 1: /* 82 Mhz */
3078 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x7CE0);
3079 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3080 break;
3081 default: /* 80 Mhz */
3082 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0xCCCD);
3083 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3084 break;
3085 }
3086 }
3087}
3088
Michael Buesche4d6b792007-09-18 15:39:42 -04003089static void b43_adjust_opmode(struct b43_wldev *dev)
3090{
3091 struct b43_wl *wl = dev->wl;
3092 u32 ctl;
3093 u16 cfp_pretbtt;
3094
3095 ctl = b43_read32(dev, B43_MMIO_MACCTL);
3096 /* Reset status to STA infrastructure mode. */
3097 ctl &= ~B43_MACCTL_AP;
3098 ctl &= ~B43_MACCTL_KEEP_CTL;
3099 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
3100 ctl &= ~B43_MACCTL_KEEP_BAD;
3101 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003102 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04003103 ctl |= B43_MACCTL_INFRA;
3104
Johannes Berg05c914f2008-09-11 00:01:58 +02003105 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3106 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04003107 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02003108 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04003109 ctl &= ~B43_MACCTL_INFRA;
3110
3111 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04003112 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04003113 if (wl->filter_flags & FIF_FCSFAIL)
3114 ctl |= B43_MACCTL_KEEP_BAD;
3115 if (wl->filter_flags & FIF_PLCPFAIL)
3116 ctl |= B43_MACCTL_KEEP_BADPLCP;
3117 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04003118 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003119 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
3120 ctl |= B43_MACCTL_BEACPROMISC;
3121
Michael Buesche4d6b792007-09-18 15:39:42 -04003122 /* Workaround: On old hardware the HW-MAC-address-filter
3123 * doesn't work properly, so always run promisc in filter
3124 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003125 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04003126 ctl |= B43_MACCTL_PROMISC;
3127
3128 b43_write32(dev, B43_MMIO_MACCTL, ctl);
3129
3130 cfp_pretbtt = 2;
3131 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02003132 if (dev->dev->chip_id == 0x4306 &&
3133 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04003134 cfp_pretbtt = 100;
3135 else
3136 cfp_pretbtt = 50;
3137 }
3138 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02003139
3140 /* FIXME: We don't currently implement the PMQ mechanism,
3141 * so always disable it. If we want to implement PMQ,
3142 * we need to enable it here (clear DISCPMQ) in AP mode.
3143 */
Rafał Miłecki50566352012-01-02 19:31:21 +01003144 if (0 /* ctl & B43_MACCTL_AP */)
3145 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3146 else
3147 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003148}
3149
3150static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3151{
3152 u16 offset;
3153
3154 if (is_ofdm) {
3155 offset = 0x480;
3156 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3157 } else {
3158 offset = 0x4C0;
3159 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3160 }
3161 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3162 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3163}
3164
3165static void b43_rate_memory_init(struct b43_wldev *dev)
3166{
3167 switch (dev->phy.type) {
3168 case B43_PHYTYPE_A:
3169 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003170 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003171 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003172 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003173 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003174 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
Hauke Mehrtens30adb4d2014-09-14 23:09:10 +02003175 b43_rate_memory_write(dev, B43_OFDM_RATE_9MB, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003176 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3177 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3178 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3179 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3180 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3181 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3182 if (dev->phy.type == B43_PHYTYPE_A)
3183 break;
3184 /* fallthrough */
3185 case B43_PHYTYPE_B:
3186 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3187 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3188 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3189 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3190 break;
3191 default:
3192 B43_WARN_ON(1);
3193 }
3194}
3195
Michael Buesch5042c502008-04-05 15:05:00 +02003196/* Set the default values for the PHY TX Control Words. */
3197static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3198{
3199 u16 ctl = 0;
3200
3201 ctl |= B43_TXH_PHY_ENC_CCK;
3202 ctl |= B43_TXH_PHY_ANT01AUTO;
3203 ctl |= B43_TXH_PHY_TXPWR;
3204
3205 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3206 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3207 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3208}
3209
Michael Buesche4d6b792007-09-18 15:39:42 -04003210/* Set the TX-Antenna for management frames sent by firmware. */
3211static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3212{
Michael Buesch5042c502008-04-05 15:05:00 +02003213 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003214 u16 tmp;
3215
Michael Buesch5042c502008-04-05 15:05:00 +02003216 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003217
Michael Buesche4d6b792007-09-18 15:39:42 -04003218 /* For ACK/CTS */
3219 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003220 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003221 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3222 /* For Probe Resposes */
3223 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003224 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003225 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3226}
3227
3228/* This is the opposite of b43_chip_init() */
3229static void b43_chip_exit(struct b43_wldev *dev)
3230{
Michael Bueschfb111372008-09-02 13:00:34 +02003231 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003232 b43_gpio_cleanup(dev);
3233 /* firmware is released later */
3234}
3235
3236/* Initialize the chip
3237 * http://bcm-specs.sipsolutions.net/ChipInit
3238 */
3239static int b43_chip_init(struct b43_wldev *dev)
3240{
3241 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003242 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003243 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003244 u16 value16;
3245
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003246 /* Initialize the MAC control */
3247 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3248 if (dev->phy.gmode)
3249 macctl |= B43_MACCTL_GMODE;
3250 macctl |= B43_MACCTL_INFRA;
3251 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003252
Michael Buesche4d6b792007-09-18 15:39:42 -04003253 err = b43_upload_microcode(dev);
3254 if (err)
3255 goto out; /* firmware is released later */
3256
3257 err = b43_gpio_init(dev);
3258 if (err)
3259 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003260
Michael Buesche4d6b792007-09-18 15:39:42 -04003261 err = b43_upload_initvals(dev);
3262 if (err)
Larry Finger1a8d12272007-12-14 13:59:11 +01003263 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003264
Rafał Miłecki0f684232014-05-17 23:24:53 +02003265 err = b43_upload_initvals_band(dev);
3266 if (err)
3267 goto err_gpio_clean;
3268
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003269 /* Turn the Analog on and initialize the PHY. */
3270 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003271 err = b43_phy_init(dev);
3272 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003273 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003274
Michael Bueschef1a6282008-08-27 18:53:02 +02003275 /* Disable Interference Mitigation. */
3276 if (phy->ops->interf_mitigation)
3277 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003278
Michael Bueschef1a6282008-08-27 18:53:02 +02003279 /* Select the antennae */
3280 if (phy->ops->set_rx_antenna)
3281 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003282 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3283
3284 if (phy->type == B43_PHYTYPE_B) {
3285 value16 = b43_read16(dev, 0x005E);
3286 value16 |= 0x0004;
3287 b43_write16(dev, 0x005E, value16);
3288 }
3289 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003290 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003291 b43_write32(dev, 0x010C, 0x01000000);
3292
Rafał Miłecki50566352012-01-02 19:31:21 +01003293 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3294 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003295
Michael Buesche4d6b792007-09-18 15:39:42 -04003296 /* Probe Response Timeout value */
3297 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003298 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003299
3300 /* Initially set the wireless operation mode. */
3301 b43_adjust_opmode(dev);
3302
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003303 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003304 b43_write16(dev, 0x060E, 0x0000);
3305 b43_write16(dev, 0x0610, 0x8000);
3306 b43_write16(dev, 0x0604, 0x0000);
3307 b43_write16(dev, 0x0606, 0x0200);
3308 } else {
3309 b43_write32(dev, 0x0188, 0x80000000);
3310 b43_write32(dev, 0x018C, 0x02000000);
3311 }
3312 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003313 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003314 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3315 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3316 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3317 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3318 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3319
Rafał Miłecki858a1652011-05-10 16:05:33 +02003320 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003321
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003322 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003323#ifdef CONFIG_B43_BCMA
3324 case B43_BUS_BCMA:
3325 /* FIXME: 0xE74 is quite common, but should be read from CC */
3326 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3327 break;
3328#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003329#ifdef CONFIG_B43_SSB
3330 case B43_BUS_SSB:
3331 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3332 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3333 break;
3334#endif
3335 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003336
3337 err = 0;
3338 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003339out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003340 return err;
3341
Larry Finger1a8d12272007-12-14 13:59:11 +01003342err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003343 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003344 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003345}
3346
Michael Buesche4d6b792007-09-18 15:39:42 -04003347static void b43_periodic_every60sec(struct b43_wldev *dev)
3348{
Michael Bueschef1a6282008-08-27 18:53:02 +02003349 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003350
Michael Bueschef1a6282008-08-27 18:53:02 +02003351 if (ops->pwork_60sec)
3352 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003353
3354 /* Force check the TX power emission now. */
3355 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003356}
3357
3358static void b43_periodic_every30sec(struct b43_wldev *dev)
3359{
3360 /* Update device statistics. */
3361 b43_calculate_link_quality(dev);
3362}
3363
3364static void b43_periodic_every15sec(struct b43_wldev *dev)
3365{
3366 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003367 u16 wdr;
3368
3369 if (dev->fw.opensource) {
3370 /* Check if the firmware is still alive.
3371 * It will reset the watchdog counter to 0 in its idle loop. */
3372 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3373 if (unlikely(wdr)) {
3374 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3375 b43_controller_restart(dev, "Firmware watchdog");
3376 return;
3377 } else {
3378 b43_shm_write16(dev, B43_SHM_SCRATCH,
3379 B43_WATCHDOG_REG, 1);
3380 }
3381 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003382
Michael Bueschef1a6282008-08-27 18:53:02 +02003383 if (phy->ops->pwork_15sec)
3384 phy->ops->pwork_15sec(dev);
3385
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003386 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3387 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003388
3389#if B43_DEBUG
3390 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3391 unsigned int i;
3392
3393 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3394 dev->irq_count / 15,
3395 dev->tx_count / 15,
3396 dev->rx_count / 15);
3397 dev->irq_count = 0;
3398 dev->tx_count = 0;
3399 dev->rx_count = 0;
3400 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3401 if (dev->irq_bit_count[i]) {
3402 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3403 dev->irq_bit_count[i] / 15, i, (1 << i));
3404 dev->irq_bit_count[i] = 0;
3405 }
3406 }
3407 }
3408#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003409}
3410
Michael Buesche4d6b792007-09-18 15:39:42 -04003411static void do_periodic_work(struct b43_wldev *dev)
3412{
3413 unsigned int state;
3414
3415 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003416 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003417 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003418 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003419 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003420 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003421}
3422
Michael Buesch05b64b32007-09-28 16:19:03 +02003423/* Periodic work locking policy:
3424 * The whole periodic work handler is protected by
3425 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003426 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003427 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003428static void b43_periodic_work_handler(struct work_struct *work)
3429{
Michael Buesch05b64b32007-09-28 16:19:03 +02003430 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3431 periodic_work.work);
3432 struct b43_wl *wl = dev->wl;
3433 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003434
Michael Buesch05b64b32007-09-28 16:19:03 +02003435 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003436
3437 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3438 goto out;
3439 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3440 goto out_requeue;
3441
Michael Buesch05b64b32007-09-28 16:19:03 +02003442 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003443
Michael Buesche4d6b792007-09-18 15:39:42 -04003444 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003445out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003446 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3447 delay = msecs_to_jiffies(50);
3448 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003449 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003450 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003451out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003452 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003453}
3454
3455static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3456{
3457 struct delayed_work *work = &dev->periodic_work;
3458
3459 dev->periodic_state = 0;
3460 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003461 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003462}
3463
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003464/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003465static int b43_validate_chipaccess(struct b43_wldev *dev)
3466{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003467 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003468
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003469 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3470 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003471
3472 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003473 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3474 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3475 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003476 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3477 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003478 goto error;
3479
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003480 /* Check if unaligned 32bit SHM_SHARED access works properly.
3481 * However, don't bail out on failure, because it's noncritical. */
3482 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3483 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3484 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3485 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3486 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3487 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3488 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3489 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3490 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3491 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3492 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3493 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3494
3495 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3496 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003497
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003498 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003499 /* The 32bit register shadows the two 16bit registers
3500 * with update sideeffects. Validate this. */
3501 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3502 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3503 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3504 goto error;
3505 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3506 goto error;
3507 }
3508 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3509
3510 v = b43_read32(dev, B43_MMIO_MACCTL);
3511 v |= B43_MACCTL_GMODE;
3512 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003513 goto error;
3514
3515 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003516error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003517 b43err(dev->wl, "Failed to validate the chipaccess\n");
3518 return -ENODEV;
3519}
3520
3521static void b43_security_init(struct b43_wldev *dev)
3522{
Michael Buesche4d6b792007-09-18 15:39:42 -04003523 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3524 /* KTP is a word address, but we address SHM bytewise.
3525 * So multiply by two.
3526 */
3527 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003528 /* Number of RCMTA address slots */
3529 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3530 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003531 b43_clear_keys(dev);
3532}
3533
Michael Buesch616de352009-03-29 13:19:31 +02003534#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003535static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003536{
3537 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003538 struct b43_wldev *dev;
3539 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003540
Michael Buescha78b3bb2009-09-11 21:44:05 +02003541 mutex_lock(&wl->mutex);
3542 dev = wl->current_dev;
3543 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3544 *data = b43_read16(dev, B43_MMIO_RNG);
3545 count = sizeof(u16);
3546 }
3547 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003548
Michael Buescha78b3bb2009-09-11 21:44:05 +02003549 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003550}
Michael Buesch616de352009-03-29 13:19:31 +02003551#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003552
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003553static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003554{
Michael Buesch616de352009-03-29 13:19:31 +02003555#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003556 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003557 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003558#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003559}
3560
3561static int b43_rng_init(struct b43_wl *wl)
3562{
Michael Buesch616de352009-03-29 13:19:31 +02003563 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003564
Michael Buesch616de352009-03-29 13:19:31 +02003565#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003566 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3567 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3568 wl->rng.name = wl->rng_name;
3569 wl->rng.data_read = b43_rng_read;
3570 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003571 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003572 err = hwrng_register(&wl->rng);
3573 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003574 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003575 b43err(wl, "Failed to register the random "
3576 "number generator (%d)\n", err);
3577 }
Michael Buesch616de352009-03-29 13:19:31 +02003578#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003579
3580 return err;
3581}
3582
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003583static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003584{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003585 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3586 struct b43_wldev *dev;
3587 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003588 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003589 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003590
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003591 mutex_lock(&wl->mutex);
3592 dev = wl->current_dev;
3593 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3594 mutex_unlock(&wl->mutex);
3595 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003596 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003597
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003598 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3599 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3600 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3601 if (b43_using_pio_transfers(dev))
3602 err = b43_pio_tx(dev, skb);
3603 else
3604 err = b43_dma_tx(dev, skb);
3605 if (err == -ENOSPC) {
3606 wl->tx_queue_stopped[queue_num] = 1;
3607 ieee80211_stop_queue(wl->hw, queue_num);
3608 skb_queue_head(&wl->tx_queue[queue_num], skb);
3609 break;
3610 }
3611 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003612 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003613 err = 0;
3614 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003615
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003616 if (!err)
3617 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003618 }
3619
Michael Buesch990b86f2009-09-12 00:48:03 +02003620#if B43_DEBUG
3621 dev->tx_count++;
3622#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003623 mutex_unlock(&wl->mutex);
3624}
Michael Buesch21a75d72008-04-25 19:29:08 +02003625
Johannes Berg7bb45682011-02-24 14:42:06 +01003626static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003627 struct ieee80211_tx_control *control,
3628 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003629{
3630 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003631
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003632 if (unlikely(skb->len < 2 + 2 + 6)) {
3633 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003634 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003635 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003636 }
3637 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3638
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003639 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3640 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3641 ieee80211_queue_work(wl->hw, &wl->tx_work);
3642 } else {
3643 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3644 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003645}
3646
Michael Buesche6f5b932008-03-05 21:18:49 +01003647static void b43_qos_params_upload(struct b43_wldev *dev,
3648 const struct ieee80211_tx_queue_params *p,
3649 u16 shm_offset)
3650{
3651 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003652 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003653 unsigned int i;
3654
Michael Bueschb0544eb2009-09-06 15:42:45 +02003655 if (!dev->qos_enabled)
3656 return;
3657
Johannes Berg0b576642008-07-15 02:08:24 -07003658 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003659
3660 memset(&params, 0, sizeof(params));
3661
3662 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003663 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3664 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3665 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3666 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003667 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003668 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003669
3670 for (i = 0; i < ARRAY_SIZE(params); i++) {
3671 if (i == B43_QOSPARAM_STATUS) {
3672 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3673 shm_offset + (i * 2));
3674 /* Mark the parameters as updated. */
3675 tmp |= 0x100;
3676 b43_shm_write16(dev, B43_SHM_SHARED,
3677 shm_offset + (i * 2),
3678 tmp);
3679 } else {
3680 b43_shm_write16(dev, B43_SHM_SHARED,
3681 shm_offset + (i * 2),
3682 params[i]);
3683 }
3684 }
3685}
3686
Michael Bueschc40c1122008-09-06 16:21:47 +02003687/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3688static const u16 b43_qos_shm_offsets[] = {
3689 /* [mac80211-queue-nr] = SHM_OFFSET, */
3690 [0] = B43_QOS_VOICE,
3691 [1] = B43_QOS_VIDEO,
3692 [2] = B43_QOS_BESTEFFORT,
3693 [3] = B43_QOS_BACKGROUND,
3694};
3695
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003696/* Update all QOS parameters in hardware. */
3697static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003698{
3699 struct b43_wl *wl = dev->wl;
3700 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003701 unsigned int i;
3702
Michael Bueschb0544eb2009-09-06 15:42:45 +02003703 if (!dev->qos_enabled)
3704 return;
3705
Michael Bueschc40c1122008-09-06 16:21:47 +02003706 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3707 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003708
3709 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003710 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3711 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003712 b43_qos_params_upload(dev, &(params->p),
3713 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003714 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003715 b43_mac_enable(dev);
3716}
3717
3718static void b43_qos_clear(struct b43_wl *wl)
3719{
3720 struct b43_qos_params *params;
3721 unsigned int i;
3722
Michael Bueschc40c1122008-09-06 16:21:47 +02003723 /* Initialize QoS parameters to sane defaults. */
3724
3725 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3726 ARRAY_SIZE(wl->qos_params));
3727
Michael Buesche6f5b932008-03-05 21:18:49 +01003728 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3729 params = &(wl->qos_params[i]);
3730
Michael Bueschc40c1122008-09-06 16:21:47 +02003731 switch (b43_qos_shm_offsets[i]) {
3732 case B43_QOS_VOICE:
3733 params->p.txop = 0;
3734 params->p.aifs = 2;
3735 params->p.cw_min = 0x0001;
3736 params->p.cw_max = 0x0001;
3737 break;
3738 case B43_QOS_VIDEO:
3739 params->p.txop = 0;
3740 params->p.aifs = 2;
3741 params->p.cw_min = 0x0001;
3742 params->p.cw_max = 0x0001;
3743 break;
3744 case B43_QOS_BESTEFFORT:
3745 params->p.txop = 0;
3746 params->p.aifs = 3;
3747 params->p.cw_min = 0x0001;
3748 params->p.cw_max = 0x03FF;
3749 break;
3750 case B43_QOS_BACKGROUND:
3751 params->p.txop = 0;
3752 params->p.aifs = 7;
3753 params->p.cw_min = 0x0001;
3754 params->p.cw_max = 0x03FF;
3755 break;
3756 default:
3757 B43_WARN_ON(1);
3758 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003759 }
3760}
3761
3762/* Initialize the core's QOS capabilities */
3763static void b43_qos_init(struct b43_wldev *dev)
3764{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003765 if (!dev->qos_enabled) {
3766 /* Disable QOS support. */
3767 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3768 b43_write16(dev, B43_MMIO_IFSCTL,
3769 b43_read16(dev, B43_MMIO_IFSCTL)
3770 & ~B43_MMIO_IFSCTL_USE_EDCF);
3771 b43dbg(dev->wl, "QoS disabled\n");
3772 return;
3773 }
3774
Michael Buesche6f5b932008-03-05 21:18:49 +01003775 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003776 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003777
3778 /* Enable QOS support. */
3779 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3780 b43_write16(dev, B43_MMIO_IFSCTL,
3781 b43_read16(dev, B43_MMIO_IFSCTL)
3782 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003783 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003784}
3785
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003786static int b43_op_conf_tx(struct ieee80211_hw *hw,
3787 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003788 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003789{
Michael Buesche6f5b932008-03-05 21:18:49 +01003790 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003791 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003792 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003793 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003794
3795 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3796 /* Queue not available or don't support setting
3797 * params on this queue. Return success to not
3798 * confuse mac80211. */
3799 return 0;
3800 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003801 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3802 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003803
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003804 mutex_lock(&wl->mutex);
3805 dev = wl->current_dev;
3806 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3807 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003808
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003809 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3810 b43_mac_suspend(dev);
3811 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3812 b43_qos_shm_offsets[queue]);
3813 b43_mac_enable(dev);
3814 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003815
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003816out_unlock:
3817 mutex_unlock(&wl->mutex);
3818
3819 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003820}
3821
Michael Buesch40faacc2007-10-28 16:29:32 +01003822static int b43_op_get_stats(struct ieee80211_hw *hw,
3823 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003824{
3825 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003826
Michael Buesch36dbd952009-09-04 22:51:29 +02003827 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003828 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003829 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003830
3831 return 0;
3832}
3833
Eliad Peller37a41b42011-09-21 14:06:11 +03003834static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003835{
3836 struct b43_wl *wl = hw_to_b43_wl(hw);
3837 struct b43_wldev *dev;
3838 u64 tsf;
3839
3840 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003841 dev = wl->current_dev;
3842
3843 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3844 b43_tsf_read(dev, &tsf);
3845 else
3846 tsf = 0;
3847
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003848 mutex_unlock(&wl->mutex);
3849
3850 return tsf;
3851}
3852
Eliad Peller37a41b42011-09-21 14:06:11 +03003853static void b43_op_set_tsf(struct ieee80211_hw *hw,
3854 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003855{
3856 struct b43_wl *wl = hw_to_b43_wl(hw);
3857 struct b43_wldev *dev;
3858
3859 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003860 dev = wl->current_dev;
3861
3862 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3863 b43_tsf_write(dev, tsf);
3864
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003865 mutex_unlock(&wl->mutex);
3866}
3867
John Daiker99da1852009-02-24 02:16:42 -08003868static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003869{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003870 switch (band) {
3871 case IEEE80211_BAND_5GHZ:
3872 return "5";
3873 case IEEE80211_BAND_2GHZ:
3874 return "2.4";
3875 default:
3876 break;
3877 }
3878 B43_WARN_ON(1);
3879 return "";
3880}
3881
3882/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003883static int b43_switch_band(struct b43_wldev *dev,
3884 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003885{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003886 struct b43_phy *phy = &dev->phy;
3887 bool gmode;
3888 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003889
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003890 switch (chan->band) {
3891 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003892 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003893 break;
3894 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003895 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003896 break;
3897 default:
3898 B43_WARN_ON(1);
3899 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003900 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003901
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003902 if (!((gmode && phy->supports_2ghz) ||
3903 (!gmode && phy->supports_5ghz))) {
3904 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003905 band_to_string(chan->band));
3906 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003907 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003908
3909 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003910 /* This device is already running. */
3911 return 0;
3912 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003913
3914 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003915 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003916
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003917 /* Some new devices don't need disabling radio for band switching */
3918 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3919 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003920
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003921 phy->gmode = gmode;
3922 b43_phy_put_into_reset(dev);
3923 switch (dev->dev->bus_type) {
3924#ifdef CONFIG_B43_BCMA
3925 case B43_BUS_BCMA:
3926 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3927 if (gmode)
3928 tmp |= B43_BCMA_IOCTL_GMODE;
3929 else
3930 tmp &= ~B43_BCMA_IOCTL_GMODE;
3931 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3932 break;
3933#endif
3934#ifdef CONFIG_B43_SSB
3935 case B43_BUS_SSB:
3936 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3937 if (gmode)
3938 tmp |= B43_TMSLOW_GMODE;
3939 else
3940 tmp &= ~B43_TMSLOW_GMODE;
3941 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3942 break;
3943#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003944 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003945 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003946
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003947 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003948
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003949 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003950
3951 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003952}
3953
Hauke Mehrtens42148522014-09-14 23:09:12 +02003954static void b43_set_beacon_listen_interval(struct b43_wldev *dev, u16 interval)
3955{
3956 interval = min_t(u16, interval, (u16)0xFF);
3957 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BCN_LI, interval);
3958}
3959
Johannes Berg9124b072008-10-14 19:17:54 +02003960/* Write the short and long frame retry limit values. */
3961static void b43_set_retry_limits(struct b43_wldev *dev,
3962 unsigned int short_retry,
3963 unsigned int long_retry)
3964{
3965 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3966 * the chip-internal counter. */
3967 short_retry = min(short_retry, (unsigned int)0xF);
3968 long_retry = min(long_retry, (unsigned int)0xF);
3969
3970 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3971 short_retry);
3972 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3973 long_retry);
3974}
3975
Johannes Berge8975582008-10-09 12:18:51 +02003976static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003977{
3978 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003979 struct b43_wldev *dev = wl->current_dev;
3980 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003981 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003982 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003983 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003984
Michael Buesche4d6b792007-09-18 15:39:42 -04003985 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003986 b43_mac_suspend(dev);
3987
Hauke Mehrtens42148522014-09-14 23:09:12 +02003988 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL)
3989 b43_set_beacon_listen_interval(dev, conf->listen_interval);
3990
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003991 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02003992 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003993 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06003994
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003995 /* Switch the band (if necessary). */
3996 err = b43_switch_band(dev, conf->chandef.chan);
3997 if (err)
3998 goto out_mac_enable;
3999
4000 /* Switch to the requested channel.
4001 * The firmware takes care of races with the TX handler.
4002 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02004003 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02004004 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01004005
Johannes Berg9124b072008-10-14 19:17:54 +02004006 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
4007 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
4008 conf->long_frame_max_tx_count);
4009 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
4010 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01004011 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04004012
Johannes Berg0869aea02009-10-28 10:03:35 +01004013 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01004014
Michael Buesche4d6b792007-09-18 15:39:42 -04004015 /* Adjust the desired TX power level. */
4016 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02004017 if (conf->power_level != phy->desired_txpower) {
4018 phy->desired_txpower = conf->power_level;
4019 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
4020 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04004021 }
4022 }
4023
4024 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02004025 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01004026 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02004027 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02004028 if (phy->ops->set_rx_antenna)
4029 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04004030
Larry Fingerfd4973c2009-06-20 12:58:11 -05004031 if (wl->radio_enabled != phy->radio_on) {
4032 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004033 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02004034 b43info(dev->wl, "Radio turned on by software\n");
4035 if (!dev->radio_hw_enable) {
4036 b43info(dev->wl, "The hardware RF-kill button "
4037 "still turns the radio physically off. "
4038 "Press the button to turn it on.\n");
4039 }
4040 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02004041 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02004042 b43info(dev->wl, "Radio turned off by software\n");
4043 }
4044 }
4045
Michael Bueschd10d0e52008-12-18 22:13:39 +01004046out_mac_enable:
4047 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004048 mutex_unlock(&wl->mutex);
4049
4050 return err;
4051}
4052
Johannes Berg881d9482009-01-21 15:13:48 +01004053static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004054{
4055 struct ieee80211_supported_band *sband =
4056 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
4057 struct ieee80211_rate *rate;
4058 int i;
4059 u16 basic, direct, offset, basic_offset, rateptr;
4060
4061 for (i = 0; i < sband->n_bitrates; i++) {
4062 rate = &sband->bitrates[i];
4063
4064 if (b43_is_cck_rate(rate->hw_value)) {
4065 direct = B43_SHM_SH_CCKDIRECT;
4066 basic = B43_SHM_SH_CCKBASIC;
4067 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
4068 offset &= 0xF;
4069 } else {
4070 direct = B43_SHM_SH_OFDMDIRECT;
4071 basic = B43_SHM_SH_OFDMBASIC;
4072 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
4073 offset &= 0xF;
4074 }
4075
4076 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
4077
4078 if (b43_is_cck_rate(rate->hw_value)) {
4079 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
4080 basic_offset &= 0xF;
4081 } else {
4082 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
4083 basic_offset &= 0xF;
4084 }
4085
4086 /*
4087 * Get the pointer that we need to point to
4088 * from the direct map
4089 */
4090 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
4091 direct + 2 * basic_offset);
4092 /* and write it to the basic map */
4093 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
4094 rateptr);
4095 }
4096}
4097
4098static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
4099 struct ieee80211_vif *vif,
4100 struct ieee80211_bss_conf *conf,
4101 u32 changed)
4102{
4103 struct b43_wl *wl = hw_to_b43_wl(hw);
4104 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004105
4106 mutex_lock(&wl->mutex);
4107
4108 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01004109 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004110 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004111
4112 B43_WARN_ON(wl->vif != vif);
4113
4114 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004115 if (conf->bssid)
4116 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
4117 else
4118 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004119 }
4120
Johannes Berg3f0d8432009-05-18 10:53:18 +02004121 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
4122 if (changed & BSS_CHANGED_BEACON &&
4123 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4124 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
4125 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
4126 b43_update_templates(wl);
4127
4128 if (changed & BSS_CHANGED_BSSID)
4129 b43_write_mac_bssid_templates(dev);
4130 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02004131
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004132 b43_mac_suspend(dev);
4133
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004134 /* Update templates for AP/mesh mode. */
4135 if (changed & BSS_CHANGED_BEACON_INT &&
4136 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4137 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06004138 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
4139 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004140 b43_set_beacon_int(dev, conf->beacon_int);
4141
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004142 if (changed & BSS_CHANGED_BASIC_RATES)
4143 b43_update_basic_rates(dev, conf->basic_rates);
4144
4145 if (changed & BSS_CHANGED_ERP_SLOT) {
4146 if (conf->use_short_slot)
4147 b43_short_slot_timing_enable(dev);
4148 else
4149 b43_short_slot_timing_disable(dev);
4150 }
4151
4152 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01004153out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004154 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004155}
4156
Michael Buesch40faacc2007-10-28 16:29:32 +01004157static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004158 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4159 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004160{
4161 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004162 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004163 u8 algorithm;
4164 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004165 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004166 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004167
4168 if (modparam_nohwcrypt)
4169 return -ENOSPC; /* User disabled HW-crypto */
4170
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004171 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4172 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4173 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4174 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4175 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4176 /*
4177 * For now, disable hw crypto for the RSN IBSS group keys. This
4178 * could be optimized in the future, but until that gets
4179 * implemented, use of software crypto for group addressed
4180 * frames is a acceptable to allow RSN IBSS to be used.
4181 */
4182 return -EOPNOTSUPP;
4183 }
4184
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004185 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004186
4187 dev = wl->current_dev;
4188 err = -ENODEV;
4189 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4190 goto out_unlock;
4191
Michael Buesch403a3a12009-06-08 21:04:57 +02004192 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004193 /* We don't have firmware for the crypto engine.
4194 * Must use software-crypto. */
4195 err = -EOPNOTSUPP;
4196 goto out_unlock;
4197 }
4198
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004199 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004200 switch (key->cipher) {
4201 case WLAN_CIPHER_SUITE_WEP40:
4202 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004203 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004204 case WLAN_CIPHER_SUITE_WEP104:
4205 algorithm = B43_SEC_ALGO_WEP104;
4206 break;
4207 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004208 algorithm = B43_SEC_ALGO_TKIP;
4209 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004210 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004211 algorithm = B43_SEC_ALGO_AES;
4212 break;
4213 default:
4214 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004215 goto out_unlock;
4216 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004217 index = (u8) (key->keyidx);
4218 if (index > 3)
4219 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004220
4221 switch (cmd) {
4222 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004223 if (algorithm == B43_SEC_ALGO_TKIP &&
4224 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4225 !modparam_hwtkip)) {
4226 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004227 err = -EOPNOTSUPP;
4228 goto out_unlock;
4229 }
4230
Michael Buesche808e582008-12-19 21:30:52 +01004231 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004232 if (WARN_ON(!sta)) {
4233 err = -EOPNOTSUPP;
4234 goto out_unlock;
4235 }
Michael Buesche808e582008-12-19 21:30:52 +01004236 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004237 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004238 key->key, key->keylen,
4239 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004240 } else {
4241 /* Group key */
4242 err = b43_key_write(dev, index, algorithm,
4243 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004244 }
4245 if (err)
4246 goto out_unlock;
4247
4248 if (algorithm == B43_SEC_ALGO_WEP40 ||
4249 algorithm == B43_SEC_ALGO_WEP104) {
4250 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4251 } else {
4252 b43_hf_write(dev,
4253 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4254 }
4255 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004256 if (algorithm == B43_SEC_ALGO_TKIP)
4257 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004258 break;
4259 case DISABLE_KEY: {
4260 err = b43_key_clear(dev, key->hw_key_idx);
4261 if (err)
4262 goto out_unlock;
4263 break;
4264 }
4265 default:
4266 B43_WARN_ON(1);
4267 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004268
Michael Buesche4d6b792007-09-18 15:39:42 -04004269out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004270 if (!err) {
4271 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004272 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004273 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004274 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004275 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004276 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004277 mutex_unlock(&wl->mutex);
4278
Michael Buesche4d6b792007-09-18 15:39:42 -04004279 return err;
4280}
4281
Michael Buesch40faacc2007-10-28 16:29:32 +01004282static void b43_op_configure_filter(struct ieee80211_hw *hw,
4283 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004284 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004285{
4286 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004287 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004288
Michael Buesch36dbd952009-09-04 22:51:29 +02004289 mutex_lock(&wl->mutex);
4290 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004291 if (!dev) {
4292 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004293 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004294 }
Johannes Berg4150c572007-09-17 01:29:23 -04004295
Johannes Berg4150c572007-09-17 01:29:23 -04004296 *fflags &= FIF_PROMISC_IN_BSS |
4297 FIF_ALLMULTI |
4298 FIF_FCSFAIL |
4299 FIF_PLCPFAIL |
4300 FIF_CONTROL |
4301 FIF_OTHER_BSS |
4302 FIF_BCN_PRBRESP_PROMISC;
4303
4304 changed &= FIF_PROMISC_IN_BSS |
4305 FIF_ALLMULTI |
4306 FIF_FCSFAIL |
4307 FIF_PLCPFAIL |
4308 FIF_CONTROL |
4309 FIF_OTHER_BSS |
4310 FIF_BCN_PRBRESP_PROMISC;
4311
4312 wl->filter_flags = *fflags;
4313
4314 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4315 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004316
4317out_unlock:
4318 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004319}
4320
Michael Buesch36dbd952009-09-04 22:51:29 +02004321/* Locking: wl->mutex
4322 * Returns the current dev. This might be different from the passed in dev,
4323 * because the core might be gone away while we unlocked the mutex. */
4324static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004325{
Larry Finger9a53bf52011-08-27 15:53:42 -05004326 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004327 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004328 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004329 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004330
Larry Finger9a53bf52011-08-27 15:53:42 -05004331 if (!dev)
4332 return NULL;
4333 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004334redo:
4335 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4336 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004337
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004338 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004339 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004340 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004341 cancel_work_sync(&wl->tx_work);
Sabrina Dubroca6b47aac2015-01-19 15:34:32 +01004342 b43_leds_stop(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004343 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004344 dev = wl->current_dev;
4345 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4346 /* Whoops, aliens ate up the device while we were unlocked. */
4347 return dev;
4348 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004349
Michael Buesch36dbd952009-09-04 22:51:29 +02004350 /* Disable interrupts on the device. */
4351 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004352 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004353 /* wl->mutex is locked. That is enough. */
4354 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4355 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4356 } else {
4357 spin_lock_irq(&wl->hardirq_lock);
4358 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4359 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4360 spin_unlock_irq(&wl->hardirq_lock);
4361 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004362 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004363 orig_dev = dev;
4364 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004365 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004366 b43_sdio_free_irq(dev);
4367 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004368 synchronize_irq(dev->dev->irq);
4369 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004370 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004371 mutex_lock(&wl->mutex);
4372 dev = wl->current_dev;
4373 if (!dev)
4374 return dev;
4375 if (dev != orig_dev) {
4376 if (b43_status(dev) >= B43_STAT_STARTED)
4377 goto redo;
4378 return dev;
4379 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004380 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4381 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004382
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004383 /* Drain all TX queues. */
4384 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004385 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4386 struct sk_buff *skb;
4387
4388 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4389 ieee80211_free_txskb(wl->hw, skb);
4390 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004391 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004392
Michael Buesche4d6b792007-09-18 15:39:42 -04004393 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004394 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004395 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004396
4397 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004398}
4399
4400/* Locking: wl->mutex */
4401static int b43_wireless_core_start(struct b43_wldev *dev)
4402{
4403 int err;
4404
4405 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4406
4407 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004408 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004409 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4410 if (err) {
4411 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4412 goto out;
4413 }
4414 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004415 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004416 b43_interrupt_thread_handler,
4417 IRQF_SHARED, KBUILD_MODNAME, dev);
4418 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004419 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004420 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004421 goto out;
4422 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004423 }
4424
4425 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004426 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004427 b43_set_status(dev, B43_STAT_STARTED);
4428
4429 /* Start data flow (TX/RX). */
4430 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004431 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004432
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004433 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004434 b43_periodic_tasks_setup(dev);
4435
Michael Buescha78b3bb2009-09-11 21:44:05 +02004436 b43_leds_init(dev);
4437
Michael Buesche4d6b792007-09-18 15:39:42 -04004438 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004439out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004440 return err;
4441}
4442
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004443static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4444{
4445 switch (phy_type) {
4446 case B43_PHYTYPE_A:
4447 return "A";
4448 case B43_PHYTYPE_B:
4449 return "B";
4450 case B43_PHYTYPE_G:
4451 return "G";
4452 case B43_PHYTYPE_N:
4453 return "N";
4454 case B43_PHYTYPE_LP:
4455 return "LP";
4456 case B43_PHYTYPE_SSLPN:
4457 return "SSLPN";
4458 case B43_PHYTYPE_HT:
4459 return "HT";
4460 case B43_PHYTYPE_LCN:
4461 return "LCN";
4462 case B43_PHYTYPE_LCNXN:
4463 return "LCNXN";
4464 case B43_PHYTYPE_LCN40:
4465 return "LCN40";
4466 case B43_PHYTYPE_AC:
4467 return "AC";
4468 }
4469 return "UNKNOWN";
4470}
4471
Michael Buesche4d6b792007-09-18 15:39:42 -04004472/* Get PHY and RADIO versioning numbers */
4473static int b43_phy_versioning(struct b43_wldev *dev)
4474{
4475 struct b43_phy *phy = &dev->phy;
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004476 const u8 core_rev = dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004477 u32 tmp;
4478 u8 analog_type;
4479 u8 phy_type;
4480 u8 phy_rev;
4481 u16 radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004482 u16 radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004483 u16 radio_rev;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004484 u8 radio_ver;
Michael Buesche4d6b792007-09-18 15:39:42 -04004485 int unsupported = 0;
4486
4487 /* Get PHY versioning */
4488 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4489 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4490 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4491 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004492
4493 /* LCNXN is continuation of N which run out of revisions */
4494 if (phy_type == B43_PHYTYPE_LCNXN) {
4495 phy_type = B43_PHYTYPE_N;
4496 phy_rev += 16;
4497 }
4498
Michael Buesche4d6b792007-09-18 15:39:42 -04004499 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004500#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004501 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004502 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004503 unsupported = 1;
4504 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004505#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004506#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004507 case B43_PHYTYPE_N:
Rafał Miłecki40c68f22014-07-08 15:11:07 +02004508 if (phy_rev >= 19)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004509 unsupported = 1;
4510 break;
4511#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004512#ifdef CONFIG_B43_PHY_LP
4513 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004514 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004515 unsupported = 1;
4516 break;
4517#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004518#ifdef CONFIG_B43_PHY_HT
4519 case B43_PHYTYPE_HT:
4520 if (phy_rev > 1)
4521 unsupported = 1;
4522 break;
4523#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004524#ifdef CONFIG_B43_PHY_LCN
4525 case B43_PHYTYPE_LCN:
4526 if (phy_rev > 1)
4527 unsupported = 1;
4528 break;
4529#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004530 default:
4531 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004532 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004533 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004534 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4535 analog_type, phy_type, b43_phy_name(dev, phy_type),
4536 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004537 return -EOPNOTSUPP;
4538 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004539 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4540 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004541
4542 /* Get RADIO versioning */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004543 if (core_rev == 40 || core_rev == 42) {
4544 radio_manuf = 0x17F;
4545
Rafał Miłecki25c15562014-08-07 07:45:37 +02004546 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, 0);
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004547 radio_rev = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4548
Rafał Miłecki25c15562014-08-07 07:45:37 +02004549 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, 1);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004550 radio_id = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4551
4552 radio_ver = 0; /* Is there version somewhere? */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004553 } else if (core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004554 u16 radio24[3];
4555
4556 for (tmp = 0; tmp < 3; tmp++) {
Rafał Miłecki25c15562014-08-07 07:45:37 +02004557 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, tmp);
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004558 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4559 }
4560
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004561 radio_manuf = 0x17F;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004562 radio_id = (radio24[2] << 8) | radio24[1];
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004563 radio_rev = (radio24[0] & 0xF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004564 radio_ver = (radio24[0] & 0xF0) >> 4;
Michael Buesche4d6b792007-09-18 15:39:42 -04004565 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004566 if (dev->dev->chip_id == 0x4317) {
4567 if (dev->dev->chip_rev == 0)
4568 tmp = 0x3205017F;
4569 else if (dev->dev->chip_rev == 1)
4570 tmp = 0x4205017F;
4571 else
4572 tmp = 0x5205017F;
4573 } else {
Rafał Miłecki25c15562014-08-07 07:45:37 +02004574 b43_write16f(dev, B43_MMIO_RADIO_CONTROL,
4575 B43_RADIOCTL_ID);
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004576 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
Rafał Miłecki25c15562014-08-07 07:45:37 +02004577 b43_write16f(dev, B43_MMIO_RADIO_CONTROL,
4578 B43_RADIOCTL_ID);
4579 tmp |= b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004580 }
4581 radio_manuf = (tmp & 0x00000FFF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004582 radio_id = (tmp & 0x0FFFF000) >> 12;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004583 radio_rev = (tmp & 0xF0000000) >> 28;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004584 radio_ver = 0; /* Probably not available on old hw */
Michael Buesche4d6b792007-09-18 15:39:42 -04004585 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004586
Michael Buesch96c755a2008-01-06 00:09:46 +01004587 if (radio_manuf != 0x17F /* Broadcom */)
4588 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004589 switch (phy_type) {
4590 case B43_PHYTYPE_A:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004591 if (radio_id != 0x2060)
Michael Buesche4d6b792007-09-18 15:39:42 -04004592 unsupported = 1;
4593 if (radio_rev != 1)
4594 unsupported = 1;
4595 if (radio_manuf != 0x17F)
4596 unsupported = 1;
4597 break;
4598 case B43_PHYTYPE_B:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004599 if ((radio_id & 0xFFF0) != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004600 unsupported = 1;
4601 break;
4602 case B43_PHYTYPE_G:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004603 if (radio_id != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004604 unsupported = 1;
4605 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004606 case B43_PHYTYPE_N:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004607 if (radio_id != 0x2055 && radio_id != 0x2056 &&
4608 radio_id != 0x2057)
Rafał Miłecki3695b932014-07-08 15:11:10 +02004609 unsupported = 1;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004610 if (radio_id == 0x2057 &&
Rafał Miłeckic11082f2014-07-19 12:52:47 +02004611 !(radio_rev == 9 || radio_rev == 14))
Michael Buesch96c755a2008-01-06 00:09:46 +01004612 unsupported = 1;
4613 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004614 case B43_PHYTYPE_LP:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004615 if (radio_id != 0x2062 && radio_id != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004616 unsupported = 1;
4617 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004618 case B43_PHYTYPE_HT:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004619 if (radio_id != 0x2059)
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004620 unsupported = 1;
4621 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004622 case B43_PHYTYPE_LCN:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004623 if (radio_id != 0x2064)
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004624 unsupported = 1;
4625 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004626 default:
4627 B43_WARN_ON(1);
4628 }
4629 if (unsupported) {
Rafał Miłecki88d825b2014-07-02 19:07:43 +02004630 b43err(dev->wl,
Rafał Miłecki16e75452014-07-20 12:57:45 +02004631 "FOUND UNSUPPORTED RADIO (Manuf 0x%X, ID 0x%X, Revision %u, Version %u)\n",
4632 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004633 return -EOPNOTSUPP;
4634 }
Rafał Miłecki16e75452014-07-20 12:57:45 +02004635 b43info(dev->wl,
4636 "Found Radio: Manuf 0x%X, ID 0x%X, Revision %u, Version %u\n",
4637 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004638
Rafał Miłecki16e75452014-07-20 12:57:45 +02004639 /* FIXME: b43 treats "id" as "ver" and ignores the real "ver" */
Michael Buesche4d6b792007-09-18 15:39:42 -04004640 phy->radio_manuf = radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004641 phy->radio_ver = radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004642 phy->radio_rev = radio_rev;
4643
4644 phy->analog = analog_type;
4645 phy->type = phy_type;
4646 phy->rev = phy_rev;
4647
4648 return 0;
4649}
4650
4651static void setup_struct_phy_for_init(struct b43_wldev *dev,
4652 struct b43_phy *phy)
4653{
Michael Buesche4d6b792007-09-18 15:39:42 -04004654 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004655 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004656 /* PHY TX errors counter. */
4657 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004658
4659#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004660 phy->phy_locked = false;
4661 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004662#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004663}
4664
4665static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4666{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004667 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004668
Michael Buesch6a724d62007-09-20 22:12:58 +02004669 /* Assume the radio is enabled. If it's not enabled, the state will
4670 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004671 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004672
4673 /* Stats */
4674 memset(&dev->stats, 0, sizeof(dev->stats));
4675
4676 setup_struct_phy_for_init(dev, &dev->phy);
4677
4678 /* IRQ related flags */
4679 dev->irq_reason = 0;
4680 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004681 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004682 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004683 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004684
4685 dev->mac_suspended = 1;
4686
4687 /* Noise calculation context */
4688 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4689}
4690
4691static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4692{
Rafał Miłecki05814832011-05-18 02:06:39 +02004693 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004694 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004695
Michael Buesch1855ba72008-04-18 20:51:41 +02004696 if (!modparam_btcoex)
4697 return;
Larry Finger95de2842007-11-09 16:57:18 -06004698 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004699 return;
4700 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4701 return;
4702
4703 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004704 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004705 hf |= B43_HF_BTCOEXALT;
4706 else
4707 hf |= B43_HF_BTCOEX;
4708 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004709}
4710
4711static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004712{
4713 if (!modparam_btcoex)
4714 return;
4715 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004716}
4717
4718static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4719{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004720 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004721 u32 tmp;
4722
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004723#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004724 if (dev->dev->bus_type != B43_BUS_SSB)
4725 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004726#else
4727 return;
4728#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004729
4730 bus = dev->dev->sdev->bus;
4731
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004732 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4733 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004734 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004735 tmp &= ~SSB_IMCFGLO_REQTO;
4736 tmp &= ~SSB_IMCFGLO_SERTO;
4737 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004738 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004739 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004740 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004741}
4742
Michael Bueschd59f7202008-04-03 18:56:19 +02004743static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4744{
4745 u16 pu_delay;
4746
4747 /* The time value is in microseconds. */
4748 if (dev->phy.type == B43_PHYTYPE_A)
4749 pu_delay = 3700;
4750 else
4751 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004752 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004753 pu_delay = 500;
4754 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4755 pu_delay = max(pu_delay, (u16)2400);
4756
4757 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4758}
4759
4760/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4761static void b43_set_pretbtt(struct b43_wldev *dev)
4762{
4763 u16 pretbtt;
4764
4765 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004766 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004767 pretbtt = 2;
4768 } else {
4769 if (dev->phy.type == B43_PHYTYPE_A)
4770 pretbtt = 120;
4771 else
4772 pretbtt = 250;
4773 }
4774 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4775 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4776}
4777
Michael Buesche4d6b792007-09-18 15:39:42 -04004778/* Shutdown a wireless core */
4779/* Locking: wl->mutex */
4780static void b43_wireless_core_exit(struct b43_wldev *dev)
4781{
Michael Buesch36dbd952009-09-04 22:51:29 +02004782 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4783 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004784 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004785
Michael Buesche4d6b792007-09-18 15:39:42 -04004786 b43_set_status(dev, B43_STAT_UNINIT);
4787
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004788 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004789 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4790 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004791
Hauke Mehrtens50023002013-08-24 00:32:34 +02004792 switch (dev->dev->bus_type) {
4793#ifdef CONFIG_B43_BCMA
4794 case B43_BUS_BCMA:
4795 bcma_core_pci_down(dev->dev->bdev->bus);
4796 break;
4797#endif
4798#ifdef CONFIG_B43_SSB
4799 case B43_BUS_SSB:
4800 /* TODO */
4801 break;
4802#endif
4803 }
4804
Michael Buesche4d6b792007-09-18 15:39:42 -04004805 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004806 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004807 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004808 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004809 if (dev->wl->current_beacon) {
4810 dev_kfree_skb_any(dev->wl->current_beacon);
4811 dev->wl->current_beacon = NULL;
4812 }
4813
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004814 b43_device_disable(dev, 0);
4815 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004816}
4817
4818/* Initialize a wireless core */
4819static int b43_wireless_core_init(struct b43_wldev *dev)
4820{
Rafał Miłecki05814832011-05-18 02:06:39 +02004821 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004822 struct b43_phy *phy = &dev->phy;
4823 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004824 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004825
4826 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4827
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004828 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004829 if (err)
4830 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004831 if (!b43_device_is_enabled(dev))
4832 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004833
Michael Bueschfb111372008-09-02 13:00:34 +02004834 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004835 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004836 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004837
4838 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004839 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004840#ifdef CONFIG_B43_BCMA
4841 case B43_BUS_BCMA:
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004842 bcma_core_pci_irq_ctl(&dev->dev->bdev->bus->drv_pci[0],
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004843 dev->dev->bdev, true);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004844 bcma_core_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004845 break;
4846#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004847#ifdef CONFIG_B43_SSB
4848 case B43_BUS_SSB:
4849 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4850 dev->dev->sdev);
4851 break;
4852#endif
4853 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004854
4855 b43_imcfglo_timeouts_workaround(dev);
4856 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004857 if (phy->ops->prepare_hardware) {
4858 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004859 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004860 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004861 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004862 err = b43_chip_init(dev);
4863 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004864 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004865 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004866 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004867 hf = b43_hf_read(dev);
4868 if (phy->type == B43_PHYTYPE_G) {
4869 hf |= B43_HF_SYMW;
4870 if (phy->rev == 1)
4871 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004872 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004873 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004874 }
4875 if (phy->radio_ver == 0x2050) {
4876 if (phy->radio_rev == 6)
4877 hf |= B43_HF_4318TSSI;
4878 if (phy->radio_rev < 6)
4879 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004880 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004881 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4882 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004883#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004884 if (dev->dev->bus_type == B43_BUS_SSB &&
4885 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4886 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004887 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004888#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004889 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004890 b43_hf_write(dev, hf);
4891
Hauke Mehrtens5eb36452014-09-14 23:09:08 +02004892 /* tell the ucode MAC capabilities */
4893 if (dev->dev->core_rev >= 13) {
4894 u32 mac_hw_cap = b43_read32(dev, B43_MMIO_MAC_HW_CAP);
4895
4896 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_MACHW_L,
4897 mac_hw_cap & 0xffff);
4898 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_MACHW_H,
4899 (mac_hw_cap >> 16) & 0xffff);
4900 }
4901
Michael Buesch74cfdba2007-10-28 16:19:44 +01004902 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4903 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004904 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4905 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4906
4907 /* Disable sending probe responses from firmware.
4908 * Setting the MaxTime to one usec will always trigger
4909 * a timeout, so we never send any probe resp.
4910 * A timeout of zero is infinite. */
4911 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4912
4913 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004914 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004915
4916 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004917 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004918 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004919 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004920 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004921 /* Maximum Contention Window */
4922 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4923
Hauke Mehrtens261b7582014-09-14 23:09:09 +02004924 /* write phytype and phyvers */
4925 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PHYTYPE, phy->type);
4926 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PHYVER, phy->rev);
4927
Rafał Miłecki505fb012011-05-19 15:11:27 +02004928 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004929 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004930 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004931 err = b43_pio_init(dev);
4932 } else if (dev->use_pio) {
4933 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4934 "This should not be needed and will result in lower "
4935 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004936 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004937 err = b43_pio_init(dev);
4938 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004939 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004940 err = b43_dma_init(dev);
4941 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004942 if (err)
4943 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004944 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004945 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004946 b43_bluetooth_coext_enable(dev);
4947
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004948 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004949 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004950 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004951
Michael Buesch5ab95492009-09-10 20:31:46 +02004952 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004953
4954 b43_set_status(dev, B43_STAT_INITIALIZED);
4955
Larry Finger1a8d12272007-12-14 13:59:11 +01004956out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004957 return err;
4958
Michael Bueschef1a6282008-08-27 18:53:02 +02004959err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004960 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004961err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004962 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004963 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4964 return err;
4965}
4966
Michael Buesch40faacc2007-10-28 16:29:32 +01004967static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004968 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004969{
4970 struct b43_wl *wl = hw_to_b43_wl(hw);
4971 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004972 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004973
4974 /* TODO: allow WDS/AP devices to coexist */
4975
Johannes Berg1ed32e42009-12-23 13:15:45 +01004976 if (vif->type != NL80211_IFTYPE_AP &&
4977 vif->type != NL80211_IFTYPE_MESH_POINT &&
4978 vif->type != NL80211_IFTYPE_STATION &&
4979 vif->type != NL80211_IFTYPE_WDS &&
4980 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004981 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004982
4983 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004984 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004985 goto out_mutex_unlock;
4986
Johannes Berg1ed32e42009-12-23 13:15:45 +01004987 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004988
4989 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004990 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01004991 wl->vif = vif;
4992 wl->if_type = vif->type;
4993 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004994
Michael Buesche4d6b792007-09-18 15:39:42 -04004995 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004996 b43_set_pretbtt(dev);
4997 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004998 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004999
5000 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04005001 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04005002 mutex_unlock(&wl->mutex);
5003
Felix Fietkau2a190322011-08-10 13:50:30 -06005004 if (err == 0)
5005 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
5006
Michael Buesche4d6b792007-09-18 15:39:42 -04005007 return err;
5008}
5009
Michael Buesch40faacc2007-10-28 16:29:32 +01005010static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01005011 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04005012{
5013 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04005014 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005015
Johannes Berg1ed32e42009-12-23 13:15:45 +01005016 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04005017
5018 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04005019
5020 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01005021 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01005022 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04005023
Rusty Russell3db1cd52011-12-19 13:56:45 +00005024 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04005025
Johannes Berg4150c572007-09-17 01:29:23 -04005026 b43_adjust_opmode(dev);
5027 memset(wl->mac_addr, 0, ETH_ALEN);
5028 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04005029
5030 mutex_unlock(&wl->mutex);
5031}
5032
Michael Buesch40faacc2007-10-28 16:29:32 +01005033static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04005034{
5035 struct b43_wl *wl = hw_to_b43_wl(hw);
5036 struct b43_wldev *dev = wl->current_dev;
5037 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07005038 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04005039
Michael Buesch7be1bb62008-01-23 21:10:56 +01005040 /* Kill all old instance specific information to make sure
5041 * the card won't use it in the short timeframe between start
5042 * and mac80211 reconfiguring it. */
5043 memset(wl->bssid, 0, ETH_ALEN);
5044 memset(wl->mac_addr, 0, ETH_ALEN);
5045 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005046 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01005047 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00005048 wl->beacon0_uploaded = false;
5049 wl->beacon1_uploaded = false;
5050 wl->beacon_templates_virgin = true;
5051 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01005052
Johannes Berg4150c572007-09-17 01:29:23 -04005053 mutex_lock(&wl->mutex);
5054
5055 if (b43_status(dev) < B43_STAT_INITIALIZED) {
5056 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05005057 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04005058 goto out_mutex_unlock;
5059 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04005060 }
5061
Johannes Berg4150c572007-09-17 01:29:23 -04005062 if (b43_status(dev) < B43_STAT_STARTED) {
5063 err = b43_wireless_core_start(dev);
5064 if (err) {
5065 if (did_init)
5066 b43_wireless_core_exit(dev);
5067 goto out_mutex_unlock;
5068 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005069 }
Johannes Berg4150c572007-09-17 01:29:23 -04005070
Johannes Bergf41f3f32009-06-07 12:30:34 -05005071 /* XXX: only do if device doesn't support rfkill irq */
5072 wiphy_rfkill_start_polling(hw->wiphy);
5073
Johannes Berg4150c572007-09-17 01:29:23 -04005074 out_mutex_unlock:
5075 mutex_unlock(&wl->mutex);
5076
Seth Forsheedbdedbd2012-04-25 17:28:00 -05005077 /*
5078 * Configuration may have been overwritten during initialization.
5079 * Reload the configuration, but only if initialization was
5080 * successful. Reloading the configuration after a failed init
5081 * may hang the system.
5082 */
5083 if (!err)
5084 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06005085
Johannes Berg4150c572007-09-17 01:29:23 -04005086 return err;
5087}
5088
Michael Buesch40faacc2007-10-28 16:29:32 +01005089static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04005090{
5091 struct b43_wl *wl = hw_to_b43_wl(hw);
5092 struct b43_wldev *dev = wl->current_dev;
5093
Michael Buescha82d9922008-04-04 21:40:06 +02005094 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d12272007-12-14 13:59:11 +01005095
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005096 if (!dev)
5097 goto out;
5098
Johannes Berg4150c572007-09-17 01:29:23 -04005099 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005100 if (b43_status(dev) >= B43_STAT_STARTED) {
5101 dev = b43_wireless_core_stop(dev);
5102 if (!dev)
5103 goto out_unlock;
5104 }
Johannes Berg4150c572007-09-17 01:29:23 -04005105 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00005106 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02005107
5108out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04005109 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005110out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02005111 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04005112}
5113
Johannes Berg17741cd2008-09-11 00:02:02 +02005114static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
5115 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01005116{
5117 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01005118
Johannes Berg9d139c82008-07-09 14:40:37 +02005119 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01005120
5121 return 0;
5122}
5123
Johannes Berg38968d02008-02-25 16:27:50 +01005124static void b43_op_sta_notify(struct ieee80211_hw *hw,
5125 struct ieee80211_vif *vif,
5126 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02005127 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01005128{
5129 struct b43_wl *wl = hw_to_b43_wl(hw);
5130
5131 B43_WARN_ON(!vif || wl->vif != vif);
5132}
5133
Johannes Berga344d672014-06-12 22:24:31 +02005134static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw,
5135 struct ieee80211_vif *vif,
5136 const u8 *mac_addr)
Michael Buesch25d3ef52009-02-20 15:39:21 +01005137{
5138 struct b43_wl *wl = hw_to_b43_wl(hw);
5139 struct b43_wldev *dev;
5140
5141 mutex_lock(&wl->mutex);
5142 dev = wl->current_dev;
5143 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5144 /* Disable CFP update during scan on other channels. */
5145 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
5146 }
5147 mutex_unlock(&wl->mutex);
5148}
5149
Johannes Berga344d672014-06-12 22:24:31 +02005150static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw,
5151 struct ieee80211_vif *vif)
Michael Buesch25d3ef52009-02-20 15:39:21 +01005152{
5153 struct b43_wl *wl = hw_to_b43_wl(hw);
5154 struct b43_wldev *dev;
5155
5156 mutex_lock(&wl->mutex);
5157 dev = wl->current_dev;
5158 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5159 /* Re-enable CFP update. */
5160 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
5161 }
5162 mutex_unlock(&wl->mutex);
5163}
5164
John W. Linville354b4f02010-04-29 15:56:06 -04005165static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
5166 struct survey_info *survey)
5167{
5168 struct b43_wl *wl = hw_to_b43_wl(hw);
5169 struct b43_wldev *dev = wl->current_dev;
5170 struct ieee80211_conf *conf = &hw->conf;
5171
5172 if (idx != 0)
5173 return -ENOENT;
5174
Karl Beldan675a0b02013-03-25 16:26:57 +01005175 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04005176 survey->filled = SURVEY_INFO_NOISE_DBM;
5177 survey->noise = dev->stats.link_noise;
5178
5179 return 0;
5180}
5181
Michael Buesche4d6b792007-09-18 15:39:42 -04005182static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01005183 .tx = b43_op_tx,
5184 .conf_tx = b43_op_conf_tx,
5185 .add_interface = b43_op_add_interface,
5186 .remove_interface = b43_op_remove_interface,
5187 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005188 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005189 .configure_filter = b43_op_configure_filter,
5190 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005191 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005192 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005193 .get_tsf = b43_op_get_tsf,
5194 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005195 .start = b43_op_start,
5196 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005197 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005198 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005199 .sw_scan_start = b43_op_sw_scan_start_notifier,
5200 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005201 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005202 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005203};
5204
5205/* Hard-reset the chip. Do not call this directly.
5206 * Use b43_controller_restart()
5207 */
5208static void b43_chip_reset(struct work_struct *work)
5209{
5210 struct b43_wldev *dev =
5211 container_of(work, struct b43_wldev, restart_work);
5212 struct b43_wl *wl = dev->wl;
5213 int err = 0;
5214 int prev_status;
5215
5216 mutex_lock(&wl->mutex);
5217
5218 prev_status = b43_status(dev);
5219 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005220 if (prev_status >= B43_STAT_STARTED) {
5221 dev = b43_wireless_core_stop(dev);
5222 if (!dev) {
5223 err = -ENODEV;
5224 goto out;
5225 }
5226 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005227 if (prev_status >= B43_STAT_INITIALIZED)
5228 b43_wireless_core_exit(dev);
5229
5230 /* ...and up again. */
5231 if (prev_status >= B43_STAT_INITIALIZED) {
5232 err = b43_wireless_core_init(dev);
5233 if (err)
5234 goto out;
5235 }
5236 if (prev_status >= B43_STAT_STARTED) {
5237 err = b43_wireless_core_start(dev);
5238 if (err) {
5239 b43_wireless_core_exit(dev);
5240 goto out;
5241 }
5242 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005243out:
5244 if (err)
5245 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005246 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005247
5248 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005249 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005250 return;
5251 }
5252
5253 /* reload configuration */
5254 b43_op_config(wl->hw, ~0);
5255 if (wl->vif)
5256 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5257
5258 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005259}
5260
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005261static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005262 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005263{
5264 struct ieee80211_hw *hw = dev->wl->hw;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005265 struct b43_phy *phy = &dev->phy;
5266 bool limited_2g;
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005267 bool limited_5g;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005268
5269 /* We don't support all 2 GHz channels on some devices */
Rafał Miłeckic11082f2014-07-19 12:52:47 +02005270 limited_2g = phy->radio_ver == 0x2057 &&
5271 (phy->radio_rev == 9 || phy->radio_rev == 14);
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005272 limited_5g = phy->radio_ver == 0x2057 &&
5273 phy->radio_rev == 9;
Michael Buesche4d6b792007-09-18 15:39:42 -04005274
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005275 if (have_2ghz_phy)
Rafał Miłecki3695b932014-07-08 15:11:10 +02005276 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = limited_2g ?
5277 &b43_band_2ghz_limited : &b43_band_2GHz;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005278 if (dev->phy.type == B43_PHYTYPE_N) {
5279 if (have_5ghz_phy)
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005280 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = limited_5g ?
5281 &b43_band_5GHz_nphy_limited :
5282 &b43_band_5GHz_nphy;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005283 } else {
5284 if (have_5ghz_phy)
5285 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5286 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005287
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005288 dev->phy.supports_2ghz = have_2ghz_phy;
5289 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005290
5291 return 0;
5292}
5293
5294static void b43_wireless_core_detach(struct b43_wldev *dev)
5295{
5296 /* We release firmware that late to not be required to re-request
5297 * is all the time when we reinit the core. */
5298 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005299 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005300}
5301
Rafał Miłecki075ca602014-05-19 23:18:54 +02005302static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5303 bool *have_5ghz_phy)
5304{
5305 u16 dev_id = 0;
5306
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005307#ifdef CONFIG_B43_BCMA
5308 if (dev->dev->bus_type == B43_BUS_BCMA &&
5309 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5310 dev_id = dev->dev->bdev->bus->host_pci->device;
5311#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005312#ifdef CONFIG_B43_SSB
5313 if (dev->dev->bus_type == B43_BUS_SSB &&
5314 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5315 dev_id = dev->dev->sdev->bus->host_pci->device;
5316#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005317 /* Override with SPROM value if available */
5318 if (dev->dev->bus_sprom->dev_id)
5319 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005320
5321 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5322 switch (dev_id) {
5323 case 0x4324: /* BCM4306 */
5324 case 0x4312: /* BCM4311 */
5325 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005326 case 0x4328: /* BCM4321 */
5327 case 0x432b: /* BCM4322 */
5328 case 0x4350: /* BCM43222 */
5329 case 0x4353: /* BCM43224 */
5330 case 0x0576: /* BCM43224 */
5331 case 0x435f: /* BCM6362 */
5332 case 0x4331: /* BCM4331 */
5333 case 0x4359: /* BCM43228 */
5334 case 0x43a0: /* BCM4360 */
5335 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005336 /* Dual band devices */
5337 *have_2ghz_phy = true;
5338 *have_5ghz_phy = true;
5339 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005340 case 0x4321: /* BCM4306 */
5341 case 0x4313: /* BCM4311 */
5342 case 0x431a: /* BCM4318 */
5343 case 0x432a: /* BCM4321 */
5344 case 0x432d: /* BCM4322 */
5345 case 0x4352: /* BCM43222 */
5346 case 0x4333: /* BCM4331 */
5347 case 0x43a2: /* BCM4360 */
5348 case 0x43b3: /* BCM4352 */
5349 /* 5 GHz only devices */
5350 *have_2ghz_phy = false;
5351 *have_5ghz_phy = true;
5352 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005353 }
5354
5355 /* As a fallback, try to guess using PHY type */
5356 switch (dev->phy.type) {
5357 case B43_PHYTYPE_A:
5358 *have_2ghz_phy = false;
5359 *have_5ghz_phy = true;
5360 return;
5361 case B43_PHYTYPE_G:
5362 case B43_PHYTYPE_N:
5363 case B43_PHYTYPE_LP:
5364 case B43_PHYTYPE_HT:
5365 case B43_PHYTYPE_LCN:
5366 *have_2ghz_phy = true;
5367 *have_5ghz_phy = false;
5368 return;
5369 }
5370
5371 B43_WARN_ON(1);
5372}
5373
Michael Buesche4d6b792007-09-18 15:39:42 -04005374static int b43_wireless_core_attach(struct b43_wldev *dev)
5375{
5376 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005377 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005378 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005379 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005380 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005381
5382 /* Do NOT do any device initialization here.
5383 * Do it in wireless_core_init() instead.
5384 * This function is for gathering basic information about the HW, only.
5385 * Also some structs may be set up here. But most likely you want to have
5386 * that in core_init(), too.
5387 */
5388
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005389 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005390 if (err) {
5391 b43err(wl, "Bus powerup failed\n");
5392 goto out;
5393 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005394
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005395 phy->do_full_init = true;
5396
Rafał Miłecki075ca602014-05-19 23:18:54 +02005397 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005398 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005399#ifdef CONFIG_B43_BCMA
5400 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005401 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5402 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5403 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005404 break;
5405#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005406#ifdef CONFIG_B43_SSB
5407 case B43_BUS_SSB:
5408 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005409 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5410 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5411 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005412 } else
5413 B43_WARN_ON(1);
5414 break;
5415#endif
5416 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005417
Michael Buesch96c755a2008-01-06 00:09:46 +01005418 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005419 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005420
Rafał Miłecki075ca602014-05-19 23:18:54 +02005421 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005422 err = b43_phy_versioning(dev);
5423 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005424 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005425
5426 /* Get real info about supported bands */
5427 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5428
5429 /* We don't support 5 GHz on some PHYs yet */
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005430 if (have_5ghz_phy) {
5431 switch (dev->phy.type) {
5432 case B43_PHYTYPE_A:
5433 case B43_PHYTYPE_G:
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005434 case B43_PHYTYPE_LP:
5435 case B43_PHYTYPE_HT:
5436 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
5437 have_5ghz_phy = false;
5438 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005439 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005440
5441 if (!have_2ghz_phy && !have_5ghz_phy) {
5442 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005443 err = -EOPNOTSUPP;
5444 goto err_powerdown;
5445 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005446
Michael Bueschfb111372008-09-02 13:00:34 +02005447 err = b43_phy_allocate(dev);
5448 if (err)
5449 goto err_powerdown;
5450
Michael Buesch96c755a2008-01-06 00:09:46 +01005451 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005452 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005453
5454 err = b43_validate_chipaccess(dev);
5455 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005456 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005457 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005458 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005459 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005460
5461 /* Now set some default "current_dev" */
5462 if (!wl->current_dev)
5463 wl->current_dev = dev;
5464 INIT_WORK(&dev->restart_work, b43_chip_reset);
5465
Michael Bueschcb24f572008-09-03 12:12:20 +02005466 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005467 b43_device_disable(dev, 0);
5468 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005469
5470out:
5471 return err;
5472
Michael Bueschfb111372008-09-02 13:00:34 +02005473err_phy_free:
5474 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005475err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005476 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005477 return err;
5478}
5479
Rafał Miłecki482f0532011-05-18 02:06:36 +02005480static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005481{
5482 struct b43_wldev *wldev;
5483 struct b43_wl *wl;
5484
Michael Buesch3bf0a322008-05-22 16:32:16 +02005485 /* Do not cancel ieee80211-workqueue based work here.
5486 * See comment in b43_remove(). */
5487
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005488 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005489 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005490 b43_debugfs_remove_device(wldev);
5491 b43_wireless_core_detach(wldev);
5492 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005493 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005494 kfree(wldev);
5495}
5496
Rafał Miłecki482f0532011-05-18 02:06:36 +02005497static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005498{
5499 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005500 int err = -ENOMEM;
5501
Michael Buesche4d6b792007-09-18 15:39:42 -04005502 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5503 if (!wldev)
5504 goto out;
5505
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005506 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005507 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005508 wldev->wl = wl;
5509 b43_set_status(wldev, B43_STAT_UNINIT);
5510 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005511 INIT_LIST_HEAD(&wldev->list);
5512
5513 err = b43_wireless_core_attach(wldev);
5514 if (err)
5515 goto err_kfree_wldev;
5516
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005517 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005518 b43_debugfs_add_device(wldev);
5519
5520 out:
5521 return err;
5522
5523 err_kfree_wldev:
5524 kfree(wldev);
5525 return err;
5526}
5527
Michael Buesch9fc38452008-04-19 16:53:00 +02005528#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5529 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5530 (pdev->device == _device) && \
5531 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5532 (pdev->subsystem_device == _subdevice) )
5533
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005534#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005535static void b43_sprom_fixup(struct ssb_bus *bus)
5536{
Michael Buesch1855ba72008-04-18 20:51:41 +02005537 struct pci_dev *pdev;
5538
Michael Buesche4d6b792007-09-18 15:39:42 -04005539 /* boardflags workarounds */
5540 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005541 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005542 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005543 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005544 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005545 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005546 if (bus->bustype == SSB_BUSTYPE_PCI) {
5547 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005548 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005549 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005550 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005551 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005552 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005553 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5554 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005555 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5556 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005557}
5558
Rafał Miłecki482f0532011-05-18 02:06:36 +02005559static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005560{
5561 struct ieee80211_hw *hw = wl->hw;
5562
Rafał Miłecki482f0532011-05-18 02:06:36 +02005563 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005564 ieee80211_free_hw(hw);
5565}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005566#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005567
Rafał Miłeckid1507052011-07-05 23:54:07 +02005568static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005569{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005570 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005571 struct ieee80211_hw *hw;
5572 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005573 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005574 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005575
5576 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5577 if (!hw) {
5578 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005579 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005580 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005581 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005582
5583 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005584 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005585 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005586
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005587 hw->wiphy->interface_modes =
5588 BIT(NL80211_IFTYPE_AP) |
5589 BIT(NL80211_IFTYPE_MESH_POINT) |
5590 BIT(NL80211_IFTYPE_STATION) |
5591 BIT(NL80211_IFTYPE_WDS) |
5592 BIT(NL80211_IFTYPE_ADHOC);
5593
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005594 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5595
Oleksij Rempele64add22012-06-05 20:39:32 +02005596 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005597 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005598 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005599 if (is_valid_ether_addr(sprom->et1mac))
5600 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005601 else
Larry Finger95de2842007-11-09 16:57:18 -06005602 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005603
Michael Buesch403a3a12009-06-08 21:04:57 +02005604 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005605 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005606 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005607 spin_lock_init(&wl->hardirq_lock);
Michael Büscha75d46a2015-01-26 18:26:17 +01005608 spin_lock_init(&wl->beacon_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005609 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005610 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005611 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005612
5613 /* Initialize queues and flags. */
5614 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5615 skb_queue_head_init(&wl->tx_queue[queue_num]);
5616 wl->tx_queue_stopped[queue_num] = 0;
5617 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005618
Rafał Miłecki2729df22011-07-18 22:45:58 +02005619 snprintf(chip_name, ARRAY_SIZE(chip_name),
5620 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5621 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5622 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005623 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005624}
5625
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005626#ifdef CONFIG_B43_BCMA
5627static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005628{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005629 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005630 struct b43_wl *wl;
5631 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005632
Rafał Miłecki89604002013-06-26 09:55:54 +02005633 if (!modparam_allhwsupport &&
5634 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5635 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5636 return -ENOTSUPP;
5637 }
5638
Rafał Miłecki397915c2011-07-06 19:03:46 +02005639 dev = b43_bus_dev_bcma_init(core);
5640 if (!dev)
5641 return -ENODEV;
5642
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005643 wl = b43_wireless_init(dev);
5644 if (IS_ERR(wl)) {
5645 err = PTR_ERR(wl);
5646 goto bcma_out;
5647 }
5648
5649 err = b43_one_core_attach(dev, wl);
5650 if (err)
5651 goto bcma_err_wireless_exit;
5652
Larry Finger6b6fa582012-03-08 22:27:46 -06005653 /* setup and start work to load firmware */
5654 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5655 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005656
5657bcma_out:
5658 return err;
5659
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005660bcma_err_wireless_exit:
5661 ieee80211_free_hw(wl->hw);
5662 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005663}
5664
5665static void b43_bcma_remove(struct bcma_device *core)
5666{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005667 struct b43_wldev *wldev = bcma_get_drvdata(core);
5668 struct b43_wl *wl = wldev->wl;
5669
5670 /* We must cancel any work here before unregistering from ieee80211,
5671 * as the ieee80211 unreg will destroy the workqueue. */
5672 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005673 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005674
Oleksij Rempele64add22012-06-05 20:39:32 +02005675 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005676 if (!wldev->fw.ucode.data)
5677 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005678 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005679 b43_leds_stop(wldev);
5680 ieee80211_unregister_hw(wl->hw);
5681 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005682
5683 b43_one_core_detach(wldev->dev);
5684
Larry Finger09164042014-01-12 15:11:37 -06005685 /* Unregister HW RNG driver */
5686 b43_rng_exit(wl);
5687
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005688 b43_leds_unregister(wl);
5689
5690 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005691}
5692
5693static struct bcma_driver b43_bcma_driver = {
5694 .name = KBUILD_MODNAME,
5695 .id_table = b43_bcma_tbl,
5696 .probe = b43_bcma_probe,
5697 .remove = b43_bcma_remove,
5698};
5699#endif
5700
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005701#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005702static
5703int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005704{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005705 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005706 struct b43_wl *wl;
5707 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005708
Rafał Miłecki482f0532011-05-18 02:06:36 +02005709 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005710 if (!dev)
5711 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005712
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005713 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005714 if (wl) {
5715 b43err(NULL, "Dual-core devices are not supported\n");
5716 err = -ENOTSUPP;
5717 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005718 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005719
5720 b43_sprom_fixup(sdev->bus);
5721
5722 wl = b43_wireless_init(dev);
5723 if (IS_ERR(wl)) {
5724 err = PTR_ERR(wl);
5725 goto err_ssb_kfree_dev;
5726 }
5727 ssb_set_devtypedata(sdev, wl);
5728 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5729
Michael Buesche4d6b792007-09-18 15:39:42 -04005730 err = b43_one_core_attach(dev, wl);
5731 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005732 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005733
Larry Finger6b6fa582012-03-08 22:27:46 -06005734 /* setup and start work to load firmware */
5735 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5736 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005737
Michael Buesche4d6b792007-09-18 15:39:42 -04005738 return err;
5739
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005740err_ssb_wireless_exit:
5741 b43_wireless_exit(dev, wl);
5742err_ssb_kfree_dev:
5743 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005744 return err;
5745}
5746
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005747static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005748{
Rafał Miłeckiaa63418a2011-05-18 02:06:35 +02005749 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5750 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005751 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005752
Michael Buesch3bf0a322008-05-22 16:32:16 +02005753 /* We must cancel any work here before unregistering from ieee80211,
5754 * as the ieee80211 unreg will destroy the workqueue. */
5755 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005756 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005757
Michael Buesche4d6b792007-09-18 15:39:42 -04005758 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005759 if (!wldev->fw.ucode.data)
5760 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005761 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005762 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005763 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005764 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005765
Pavel Roskine61b52d2011-07-22 18:07:13 -04005766 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005767
Larry Finger09164042014-01-12 15:11:37 -06005768 /* Unregister HW RNG driver */
5769 b43_rng_exit(wl);
5770
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005771 b43_leds_unregister(wl);
5772 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005773}
5774
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005775static struct ssb_driver b43_ssb_driver = {
5776 .name = KBUILD_MODNAME,
5777 .id_table = b43_ssb_tbl,
5778 .probe = b43_ssb_probe,
5779 .remove = b43_ssb_remove,
5780};
5781#endif /* CONFIG_B43_SSB */
5782
Michael Buesche4d6b792007-09-18 15:39:42 -04005783/* Perform a hardware reset. This can be called from any context. */
5784void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5785{
5786 /* Must avoid requeueing, if we are in shutdown. */
5787 if (b43_status(dev) < B43_STAT_INITIALIZED)
5788 return;
5789 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005790 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005791}
5792
Michael Buesch26bc7832008-02-09 00:18:35 +01005793static void b43_print_driverinfo(void)
5794{
5795 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005796 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005797
5798#ifdef CONFIG_B43_PCI_AUTOSELECT
5799 feat_pci = "P";
5800#endif
5801#ifdef CONFIG_B43_PCMCIA
5802 feat_pcmcia = "M";
5803#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005804#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005805 feat_nphy = "N";
5806#endif
5807#ifdef CONFIG_B43_LEDS
5808 feat_leds = "L";
5809#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005810#ifdef CONFIG_B43_SDIO
5811 feat_sdio = "S";
5812#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005813 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005814 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005815 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005816 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005817}
5818
Michael Buesche4d6b792007-09-18 15:39:42 -04005819static int __init b43_init(void)
5820{
5821 int err;
5822
5823 b43_debugfs_init();
5824 err = b43_pcmcia_init();
5825 if (err)
5826 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005827 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005828 if (err)
5829 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005830#ifdef CONFIG_B43_BCMA
5831 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005832 if (err)
5833 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005834#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005835#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005836 err = ssb_driver_register(&b43_ssb_driver);
5837 if (err)
5838 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005839#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005840 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005841
5842 return err;
5843
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005844#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005845err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005846#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005847#ifdef CONFIG_B43_BCMA
5848 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005849err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005850#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005851 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005852err_pcmcia_exit:
5853 b43_pcmcia_exit();
5854err_dfs_exit:
5855 b43_debugfs_exit();
5856 return err;
5857}
5858
5859static void __exit b43_exit(void)
5860{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005861#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005862 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005863#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005864#ifdef CONFIG_B43_BCMA
5865 bcma_driver_unregister(&b43_bcma_driver);
5866#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005867 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005868 b43_pcmcia_exit();
5869 b43_debugfs_exit();
5870}
5871
5872module_init(b43_init)
5873module_exit(b43_exit)