blob: f40992969b4ab0b96d12e6bf8735e269a3d5f341 [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),
Joe Perchesf7219b52015-02-10 12:55:03 -0800130 {},
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200131};
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),
Joe Perches673e2ba2015-02-10 13:19:24 -0800147 {},
Michael Buesche4d6b792007-09-18 15:39:42 -0400148};
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łecki86144b02015-01-25 14:39:34 +01001265 if (dev->phy.type == B43_PHYTYPE_AC) {
1266 u16 tmp;
1267
1268 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1269 tmp &= ~B43_BCMA_IOCTL_DAC;
1270 tmp |= 0x100;
1271 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
1272
1273 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1274 tmp &= ~B43_BCMA_IOCTL_PHY_CLKEN;
1275 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
1276
1277 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1278 tmp |= B43_BCMA_IOCTL_PHY_CLKEN;
1279 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
1280 }
1281
Rafał Miłecki49173592011-07-17 01:06:06 +02001282 bcma_core_set_clockmode(dev->dev->bdev, BCMA_CLKMODE_FAST);
1283 b43_bcma_phy_reset(dev);
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001284 bcma_core_pll_ctl(dev->dev->bdev, req, status, true);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001285}
1286#endif
1287
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001288#ifdef CONFIG_B43_SSB
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001289static void b43_ssb_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001290{
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001291 u32 flags = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04001292
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001293 if (gmode)
1294 flags |= B43_TMSLOW_GMODE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001295 flags |= B43_TMSLOW_PHYCLKEN;
1296 flags |= B43_TMSLOW_PHYRESET;
Rafał Miłecki42ab1352010-12-09 20:56:01 +01001297 if (dev->phy.type == B43_PHYTYPE_N)
1298 flags |= B43_TMSLOW_PHY_BANDWIDTH_20MHZ; /* Make 20 MHz def */
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02001299 b43_device_enable(dev, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -04001300 msleep(2); /* Wait for the PLL to turn on. */
1301
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001302 b43_phy_take_out_of_reset(dev);
Rafał Miłecki14952982011-05-17 18:57:28 +02001303}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001304#endif
Rafał Miłecki14952982011-05-17 18:57:28 +02001305
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001306void b43_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Rafał Miłecki14952982011-05-17 18:57:28 +02001307{
1308 u32 macctl;
1309
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001310 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001311#ifdef CONFIG_B43_BCMA
1312 case B43_BUS_BCMA:
1313 b43_bcma_wireless_core_reset(dev, gmode);
1314 break;
1315#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001316#ifdef CONFIG_B43_SSB
1317 case B43_BUS_SSB:
1318 b43_ssb_wireless_core_reset(dev, gmode);
1319 break;
1320#endif
1321 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001322
Michael Bueschfb111372008-09-02 13:00:34 +02001323 /* Turn Analog ON, but only if we already know the PHY-type.
1324 * This protects against very early setup where we don't know the
1325 * PHY-type, yet. wireless_core_reset will be called once again later,
1326 * when we know the PHY-type. */
1327 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001328 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001329
1330 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1331 macctl &= ~B43_MACCTL_GMODE;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001332 if (gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001333 macctl |= B43_MACCTL_GMODE;
1334 macctl |= B43_MACCTL_IHR_ENABLED;
1335 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1336}
1337
1338static void handle_irq_transmit_status(struct b43_wldev *dev)
1339{
1340 u32 v0, v1;
1341 u16 tmp;
1342 struct b43_txstatus stat;
1343
1344 while (1) {
1345 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1346 if (!(v0 & 0x00000001))
1347 break;
1348 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1349
1350 stat.cookie = (v0 >> 16);
1351 stat.seq = (v1 & 0x0000FFFF);
1352 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1353 tmp = (v0 & 0x0000FFFF);
1354 stat.frame_count = ((tmp & 0xF000) >> 12);
1355 stat.rts_count = ((tmp & 0x0F00) >> 8);
1356 stat.supp_reason = ((tmp & 0x001C) >> 2);
1357 stat.pm_indicated = !!(tmp & 0x0080);
1358 stat.intermediate = !!(tmp & 0x0040);
1359 stat.for_ampdu = !!(tmp & 0x0020);
1360 stat.acked = !!(tmp & 0x0002);
1361
1362 b43_handle_txstatus(dev, &stat);
1363 }
1364}
1365
1366static void drain_txstatus_queue(struct b43_wldev *dev)
1367{
1368 u32 dummy;
1369
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001370 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04001371 return;
1372 /* Read all entries from the microcode TXstatus FIFO
1373 * and throw them away.
1374 */
1375 while (1) {
1376 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1377 if (!(dummy & 0x00000001))
1378 break;
1379 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1380 }
1381}
1382
1383static u32 b43_jssi_read(struct b43_wldev *dev)
1384{
1385 u32 val = 0;
1386
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001387 val = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001388 val <<= 16;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001389 val |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001390
1391 return val;
1392}
1393
1394static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1395{
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001396 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0,
1397 (jssi & 0x0000FFFF));
1398 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1,
1399 (jssi & 0xFFFF0000) >> 16);
Michael Buesche4d6b792007-09-18 15:39:42 -04001400}
1401
1402static void b43_generate_noise_sample(struct b43_wldev *dev)
1403{
1404 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001405 b43_write32(dev, B43_MMIO_MACCMD,
1406 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001407}
1408
1409static void b43_calculate_link_quality(struct b43_wldev *dev)
1410{
1411 /* Top half of Link Quality calculation. */
1412
Michael Bueschef1a6282008-08-27 18:53:02 +02001413 if (dev->phy.type != B43_PHYTYPE_G)
1414 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001415 if (dev->noisecalc.calculation_running)
1416 return;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001417 dev->noisecalc.calculation_running = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001418 dev->noisecalc.nr_samples = 0;
1419
1420 b43_generate_noise_sample(dev);
1421}
1422
1423static void handle_irq_noise(struct b43_wldev *dev)
1424{
Michael Bueschef1a6282008-08-27 18:53:02 +02001425 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001426 u16 tmp;
1427 u8 noise[4];
1428 u8 i, j;
1429 s32 average;
1430
1431 /* Bottom half of Link Quality calculation. */
1432
Michael Bueschef1a6282008-08-27 18:53:02 +02001433 if (dev->phy.type != B43_PHYTYPE_G)
1434 return;
1435
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001436 /* Possible race condition: It might be possible that the user
1437 * changed to a different channel in the meantime since we
1438 * started the calculation. We ignore that fact, since it's
1439 * not really that much of a problem. The background noise is
1440 * an estimation only anyway. Slightly wrong results will get damped
1441 * by the averaging of the 8 sample rounds. Additionally the
1442 * value is shortlived. So it will be replaced by the next noise
1443 * calculation round soon. */
1444
Michael Buesche4d6b792007-09-18 15:39:42 -04001445 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001446 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001447 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1448 noise[2] == 0x7F || noise[3] == 0x7F)
1449 goto generate_new;
1450
1451 /* Get the noise samples. */
1452 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1453 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001454 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1455 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1456 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1457 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001458 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1459 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1460 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1461 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1462 dev->noisecalc.nr_samples++;
1463 if (dev->noisecalc.nr_samples == 8) {
1464 /* Calculate the Link Quality by the noise samples. */
1465 average = 0;
1466 for (i = 0; i < 8; i++) {
1467 for (j = 0; j < 4; j++)
1468 average += dev->noisecalc.samples[i][j];
1469 }
1470 average /= (8 * 4);
1471 average *= 125;
1472 average += 64;
1473 average /= 128;
1474 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1475 tmp = (tmp / 128) & 0x1F;
1476 if (tmp >= 8)
1477 average += 2;
1478 else
1479 average -= 25;
1480 if (tmp == 8)
1481 average -= 72;
1482 else
1483 average -= 48;
1484
1485 dev->stats.link_noise = average;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001486 dev->noisecalc.calculation_running = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001487 return;
1488 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001489generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001490 b43_generate_noise_sample(dev);
1491}
1492
1493static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1494{
Johannes Berg05c914f2008-09-11 00:01:58 +02001495 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001496 ///TODO: PS TBTT
1497 } else {
1498 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1499 b43_power_saving_ctl_bits(dev, 0);
1500 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001501 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001502 dev->dfq_valid = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001503}
1504
1505static void handle_irq_atim_end(struct b43_wldev *dev)
1506{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001507 if (dev->dfq_valid) {
1508 b43_write32(dev, B43_MMIO_MACCMD,
1509 b43_read32(dev, B43_MMIO_MACCMD)
1510 | B43_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001511 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001512 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001513}
1514
1515static void handle_irq_pmq(struct b43_wldev *dev)
1516{
1517 u32 tmp;
1518
1519 //TODO: AP mode.
1520
1521 while (1) {
1522 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1523 if (!(tmp & 0x00000008))
1524 break;
1525 }
1526 /* 16bit write is odd, but correct. */
1527 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1528}
1529
1530static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001531 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001532 u16 ram_offset,
1533 u16 shm_size_offset, u8 rate)
1534{
1535 u32 i, tmp;
1536 struct b43_plcp_hdr4 plcp;
1537
1538 plcp.data = 0;
1539 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1540 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1541 ram_offset += sizeof(u32);
1542 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1543 * So leave the first two bytes of the next write blank.
1544 */
1545 tmp = (u32) (data[0]) << 16;
1546 tmp |= (u32) (data[1]) << 24;
1547 b43_ram_write(dev, ram_offset, tmp);
1548 ram_offset += sizeof(u32);
1549 for (i = 2; i < size; i += sizeof(u32)) {
1550 tmp = (u32) (data[i + 0]);
1551 if (i + 1 < size)
1552 tmp |= (u32) (data[i + 1]) << 8;
1553 if (i + 2 < size)
1554 tmp |= (u32) (data[i + 2]) << 16;
1555 if (i + 3 < size)
1556 tmp |= (u32) (data[i + 3]) << 24;
1557 b43_ram_write(dev, ram_offset + i - 2, tmp);
1558 }
1559 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1560 size + sizeof(struct b43_plcp_hdr6));
1561}
1562
Michael Buesch5042c502008-04-05 15:05:00 +02001563/* Check if the use of the antenna that ieee80211 told us to
1564 * use is possible. This will fall back to DEFAULT.
1565 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1566u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1567 u8 antenna_nr)
1568{
1569 u8 antenna_mask;
1570
1571 if (antenna_nr == 0) {
1572 /* Zero means "use default antenna". That's always OK. */
1573 return 0;
1574 }
1575
1576 /* Get the mask of available antennas. */
1577 if (dev->phy.gmode)
Rafał Miłecki05814832011-05-18 02:06:39 +02001578 antenna_mask = dev->dev->bus_sprom->ant_available_bg;
Michael Buesch5042c502008-04-05 15:05:00 +02001579 else
Rafał Miłecki05814832011-05-18 02:06:39 +02001580 antenna_mask = dev->dev->bus_sprom->ant_available_a;
Michael Buesch5042c502008-04-05 15:05:00 +02001581
1582 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1583 /* This antenna is not available. Fall back to default. */
1584 return 0;
1585 }
1586
1587 return antenna_nr;
1588}
1589
Michael Buesch5042c502008-04-05 15:05:00 +02001590/* Convert a b43 antenna number value to the PHY TX control value. */
1591static u16 b43_antenna_to_phyctl(int antenna)
1592{
1593 switch (antenna) {
1594 case B43_ANTENNA0:
1595 return B43_TXH_PHY_ANT0;
1596 case B43_ANTENNA1:
1597 return B43_TXH_PHY_ANT1;
1598 case B43_ANTENNA2:
1599 return B43_TXH_PHY_ANT2;
1600 case B43_ANTENNA3:
1601 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001602 case B43_ANTENNA_AUTO0:
1603 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001604 return B43_TXH_PHY_ANT01AUTO;
1605 }
1606 B43_WARN_ON(1);
1607 return 0;
1608}
1609
Michael Buesche4d6b792007-09-18 15:39:42 -04001610static void b43_write_beacon_template(struct b43_wldev *dev,
1611 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001612 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001613{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001614 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001615 const struct ieee80211_mgmt *bcn;
1616 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001617 bool tim_found = false;
Michael Buesch5042c502008-04-05 15:05:00 +02001618 unsigned int rate;
1619 u16 ctl;
1620 int antenna;
Michael Büscha75d46a2015-01-26 18:26:17 +01001621 struct ieee80211_tx_info *info;
1622 unsigned long flags;
1623 struct sk_buff *beacon_skb;
Michael Buesche4d6b792007-09-18 15:39:42 -04001624
Michael Büscha75d46a2015-01-26 18:26:17 +01001625 spin_lock_irqsave(&dev->wl->beacon_lock, flags);
1626 info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Johannes Berge039fa42008-05-15 12:55:29 +02001627 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Büscha75d46a2015-01-26 18:26:17 +01001628 /* Clone the beacon, so it cannot go away, while we write it to hw. */
1629 beacon_skb = skb_clone(dev->wl->current_beacon, GFP_ATOMIC);
1630 spin_unlock_irqrestore(&dev->wl->beacon_lock, flags);
1631
1632 if (!beacon_skb) {
1633 b43dbg(dev->wl, "Could not upload beacon. "
1634 "Failed to clone beacon skb.");
1635 return;
1636 }
1637
1638 bcn = (const struct ieee80211_mgmt *)(beacon_skb->data);
1639 len = min_t(size_t, beacon_skb->len,
1640 0x200 - sizeof(struct b43_plcp_hdr6));
Michael Buesche66fee62007-12-26 17:47:10 +01001641
1642 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001643 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001644
Michael Buesch5042c502008-04-05 15:05:00 +02001645 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001646 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001647 antenna = b43_antenna_to_phyctl(antenna);
1648 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1649 /* We can't send beacons with short preamble. Would get PHY errors. */
1650 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1651 ctl &= ~B43_TXH_PHY_ANT;
1652 ctl &= ~B43_TXH_PHY_ENC;
1653 ctl |= antenna;
1654 if (b43_is_cck_rate(rate))
1655 ctl |= B43_TXH_PHY_ENC_CCK;
1656 else
1657 ctl |= B43_TXH_PHY_ENC_OFDM;
1658 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1659
Michael Buesche66fee62007-12-26 17:47:10 +01001660 /* Find the position of the TIM and the DTIM_period value
1661 * and write them to SHM. */
1662 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001663 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1664 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001665 uint8_t ie_id, ie_len;
1666
1667 ie_id = ie[i];
1668 ie_len = ie[i + 1];
1669 if (ie_id == 5) {
1670 u16 tim_position;
1671 u16 dtim_period;
1672 /* This is the TIM Information Element */
1673
1674 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001675 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001676 break;
1677 /* A valid TIM is at least 4 bytes long. */
1678 if (ie_len < 4)
1679 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001680 tim_found = true;
Michael Buesche66fee62007-12-26 17:47:10 +01001681
1682 tim_position = sizeof(struct b43_plcp_hdr6);
1683 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1684 tim_position += i;
1685
1686 dtim_period = ie[i + 3];
1687
1688 b43_shm_write16(dev, B43_SHM_SHARED,
1689 B43_SHM_SH_TIMBPOS, tim_position);
1690 b43_shm_write16(dev, B43_SHM_SHARED,
1691 B43_SHM_SH_DTIMPER, dtim_period);
1692 break;
1693 }
1694 i += ie_len + 2;
1695 }
1696 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001697 /*
1698 * If ucode wants to modify TIM do it behind the beacon, this
1699 * will happen, for example, when doing mesh networking.
1700 */
1701 b43_shm_write16(dev, B43_SHM_SHARED,
1702 B43_SHM_SH_TIMBPOS,
1703 len + sizeof(struct b43_plcp_hdr6));
1704 b43_shm_write16(dev, B43_SHM_SHARED,
1705 B43_SHM_SH_DTIMPER, 0);
1706 }
1707 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Büscha75d46a2015-01-26 18:26:17 +01001708
1709 dev_kfree_skb_any(beacon_skb);
Michael Buesche4d6b792007-09-18 15:39:42 -04001710}
1711
Michael Buesch6b4bec02008-05-20 12:16:28 +02001712static void b43_upload_beacon0(struct b43_wldev *dev)
1713{
1714 struct b43_wl *wl = dev->wl;
1715
1716 if (wl->beacon0_uploaded)
1717 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001718 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE0, B43_SHM_SH_BTL0);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001719 wl->beacon0_uploaded = true;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001720}
1721
1722static void b43_upload_beacon1(struct b43_wldev *dev)
1723{
1724 struct b43_wl *wl = dev->wl;
1725
1726 if (wl->beacon1_uploaded)
1727 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001728 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE1, B43_SHM_SH_BTL1);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001729 wl->beacon1_uploaded = true;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001730}
1731
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001732static void handle_irq_beacon(struct b43_wldev *dev)
1733{
1734 struct b43_wl *wl = dev->wl;
1735 u32 cmd, beacon0_valid, beacon1_valid;
1736
Johannes Berg05c914f2008-09-11 00:01:58 +02001737 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
Manual Munz8c235162011-09-18 18:24:03 -05001738 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) &&
1739 !b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001740 return;
1741
1742 /* This is the bottom half of the asynchronous beacon update. */
1743
1744 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001745 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001746
1747 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1748 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1749 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1750
1751 /* Schedule interrupt manually, if busy. */
1752 if (beacon0_valid && beacon1_valid) {
1753 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001754 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001755 return;
1756 }
1757
Michael Buesch6b4bec02008-05-20 12:16:28 +02001758 if (unlikely(wl->beacon_templates_virgin)) {
1759 /* We never uploaded a beacon before.
1760 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001761 wl->beacon_templates_virgin = false;
Michael Buesch6b4bec02008-05-20 12:16:28 +02001762 b43_upload_beacon0(dev);
1763 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001764 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1765 cmd |= B43_MACCMD_BEACON0_VALID;
1766 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec02008-05-20 12:16:28 +02001767 } else {
1768 if (!beacon0_valid) {
1769 b43_upload_beacon0(dev);
1770 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1771 cmd |= B43_MACCMD_BEACON0_VALID;
1772 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1773 } else if (!beacon1_valid) {
1774 b43_upload_beacon1(dev);
1775 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1776 cmd |= B43_MACCMD_BEACON1_VALID;
1777 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001778 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001779 }
1780}
1781
Michael Buesch36dbd952009-09-04 22:51:29 +02001782static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1783{
1784 u32 old_irq_mask = dev->irq_mask;
1785
1786 /* update beacon right away or defer to irq */
1787 handle_irq_beacon(dev);
1788 if (old_irq_mask != dev->irq_mask) {
1789 /* The handler updated the IRQ mask. */
1790 B43_WARN_ON(!dev->irq_mask);
1791 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1792 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1793 } else {
1794 /* Device interrupts are currently disabled. That means
1795 * we just ran the hardirq handler and scheduled the
1796 * IRQ thread. The thread will write the IRQ mask when
1797 * it finished, so there's nothing to do here. Writing
1798 * the mask _here_ would incorrectly re-enable IRQs. */
1799 }
1800 }
1801}
1802
Michael Buescha82d9922008-04-04 21:40:06 +02001803static void b43_beacon_update_trigger_work(struct work_struct *work)
1804{
1805 struct b43_wl *wl = container_of(work, struct b43_wl,
1806 beacon_update_trigger);
1807 struct b43_wldev *dev;
1808
1809 mutex_lock(&wl->mutex);
1810 dev = wl->current_dev;
1811 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Rafał Miłecki505fb012011-05-19 15:11:27 +02001812 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001813 /* wl->mutex is enough. */
1814 b43_do_beacon_update_trigger_work(dev);
1815 mmiowb();
1816 } else {
1817 spin_lock_irq(&wl->hardirq_lock);
1818 b43_do_beacon_update_trigger_work(dev);
1819 mmiowb();
1820 spin_unlock_irq(&wl->hardirq_lock);
1821 }
Michael Buescha82d9922008-04-04 21:40:06 +02001822 }
1823 mutex_unlock(&wl->mutex);
1824}
1825
Michael Büscha75d46a2015-01-26 18:26:17 +01001826/* Asynchronously update the packet templates in template RAM. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001827static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001828{
Michael Büscha75d46a2015-01-26 18:26:17 +01001829 struct sk_buff *beacon, *old_beacon;
1830 unsigned long flags;
Johannes Berg9d139c82008-07-09 14:40:37 +02001831
Michael Büscha75d46a2015-01-26 18:26:17 +01001832 /* This is the top half of the asynchronous beacon update.
Michael Buesche66fee62007-12-26 17:47:10 +01001833 * The bottom half is the beacon IRQ.
1834 * Beacon update must be asynchronous to avoid sending an
1835 * invalid beacon. This can happen for example, if the firmware
1836 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001837
Johannes Berg9d139c82008-07-09 14:40:37 +02001838 /* We could modify the existing beacon and set the aid bit in
1839 * the TIM field, but that would probably require resizing and
1840 * moving of data within the beacon template.
1841 * Simply request a new beacon and let mac80211 do the hard work. */
1842 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1843 if (unlikely(!beacon))
1844 return;
1845
Michael Büscha75d46a2015-01-26 18:26:17 +01001846 spin_lock_irqsave(&wl->beacon_lock, flags);
1847 old_beacon = wl->current_beacon;
Michael Buesche66fee62007-12-26 17:47:10 +01001848 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001849 wl->beacon0_uploaded = false;
1850 wl->beacon1_uploaded = false;
Michael Büscha75d46a2015-01-26 18:26:17 +01001851 spin_unlock_irqrestore(&wl->beacon_lock, flags);
1852
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001853 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Büscha75d46a2015-01-26 18:26:17 +01001854
1855 if (old_beacon)
1856 dev_kfree_skb_any(old_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001857}
1858
Michael Buesche4d6b792007-09-18 15:39:42 -04001859static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1860{
1861 b43_time_lock(dev);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001862 if (dev->dev->core_rev >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001863 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1864 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001865 } else {
1866 b43_write16(dev, 0x606, (beacon_int >> 6));
1867 b43_write16(dev, 0x610, beacon_int);
1868 }
1869 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001870 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001871}
1872
Michael Bueschafa83e22008-05-19 23:51:37 +02001873static void b43_handle_firmware_panic(struct b43_wldev *dev)
1874{
1875 u16 reason;
1876
1877 /* Read the register that contains the reason code for the panic. */
1878 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1879 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1880
1881 switch (reason) {
1882 default:
1883 b43dbg(dev->wl, "The panic reason is unknown.\n");
1884 /* fallthrough */
1885 case B43_FWPANIC_DIE:
1886 /* Do not restart the controller or firmware.
1887 * The device is nonfunctional from now on.
1888 * Restarting would result in this panic to trigger again,
1889 * so we avoid that recursion. */
1890 break;
1891 case B43_FWPANIC_RESTART:
1892 b43_controller_restart(dev, "Microcode panic");
1893 break;
1894 }
1895}
1896
Michael Buesche4d6b792007-09-18 15:39:42 -04001897static void handle_irq_ucode_debug(struct b43_wldev *dev)
1898{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001899 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001900 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001901 __le16 *buf;
1902
1903 /* The proprietary firmware doesn't have this IRQ. */
1904 if (!dev->fw.opensource)
1905 return;
1906
Michael Bueschafa83e22008-05-19 23:51:37 +02001907 /* Read the register that contains the reason code for this IRQ. */
1908 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1909
Michael Buesche48b0ee2008-05-17 22:44:35 +02001910 switch (reason) {
1911 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001912 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001913 break;
1914 case B43_DEBUGIRQ_DUMP_SHM:
1915 if (!B43_DEBUG)
1916 break; /* Only with driver debugging enabled. */
1917 buf = kmalloc(4096, GFP_ATOMIC);
1918 if (!buf) {
1919 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1920 goto out;
1921 }
1922 for (i = 0; i < 4096; i += 2) {
1923 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1924 buf[i / 2] = cpu_to_le16(tmp);
1925 }
1926 b43info(dev->wl, "Shared memory dump:\n");
1927 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1928 16, 2, buf, 4096, 1);
1929 kfree(buf);
1930 break;
1931 case B43_DEBUGIRQ_DUMP_REGS:
1932 if (!B43_DEBUG)
1933 break; /* Only with driver debugging enabled. */
1934 b43info(dev->wl, "Microcode register dump:\n");
1935 for (i = 0, cnt = 0; i < 64; i++) {
1936 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1937 if (cnt == 0)
1938 printk(KERN_INFO);
1939 printk("r%02u: 0x%04X ", i, tmp);
1940 cnt++;
1941 if (cnt == 6) {
1942 printk("\n");
1943 cnt = 0;
1944 }
1945 }
1946 printk("\n");
1947 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001948 case B43_DEBUGIRQ_MARKER:
1949 if (!B43_DEBUG)
1950 break; /* Only with driver debugging enabled. */
1951 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1952 B43_MARKER_ID_REG);
1953 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1954 B43_MARKER_LINE_REG);
1955 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1956 "at line number %u\n",
1957 marker_id, marker_line);
1958 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001959 default:
1960 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1961 reason);
1962 }
1963out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001964 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1965 b43_shm_write16(dev, B43_SHM_SCRATCH,
1966 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001967}
1968
Michael Buesch36dbd952009-09-04 22:51:29 +02001969static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001970{
1971 u32 reason;
1972 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1973 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001974 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001975
Michael Buesch36dbd952009-09-04 22:51:29 +02001976 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1977 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001978
1979 reason = dev->irq_reason;
1980 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1981 dma_reason[i] = dev->dma_reason[i];
1982 merged_dma_reason |= dma_reason[i];
1983 }
1984
1985 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1986 b43err(dev->wl, "MAC transmission error\n");
1987
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001988 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001989 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001990 rmb();
1991 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1992 atomic_set(&dev->phy.txerr_cnt,
1993 B43_PHY_TX_BADNESS_LIMIT);
1994 b43err(dev->wl, "Too many PHY TX errors, "
1995 "restarting the controller\n");
1996 b43_controller_restart(dev, "PHY TX errors");
1997 }
1998 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001999
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002000 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK))) {
2001 b43err(dev->wl,
2002 "Fatal DMA error: 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X\n",
2003 dma_reason[0], dma_reason[1],
2004 dma_reason[2], dma_reason[3],
2005 dma_reason[4], dma_reason[5]);
2006 b43err(dev->wl, "This device does not support DMA "
Larry Fingerbb64d952010-06-19 08:29:08 -05002007 "on your system. It will now be switched to PIO.\n");
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002008 /* Fall back to PIO transfers if we get fatal DMA errors! */
2009 dev->use_pio = true;
2010 b43_controller_restart(dev, "DMA error");
2011 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04002012 }
2013
2014 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
2015 handle_irq_ucode_debug(dev);
2016 if (reason & B43_IRQ_TBTT_INDI)
2017 handle_irq_tbtt_indication(dev);
2018 if (reason & B43_IRQ_ATIM_END)
2019 handle_irq_atim_end(dev);
2020 if (reason & B43_IRQ_BEACON)
2021 handle_irq_beacon(dev);
2022 if (reason & B43_IRQ_PMQ)
2023 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02002024 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
2025 ;/* TODO */
2026 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04002027 handle_irq_noise(dev);
2028
2029 /* Check the DMA reason registers for received data. */
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002030 if (dma_reason[0] & B43_DMAIRQ_RDESC_UFLOW) {
2031 if (B43_DEBUG)
2032 b43warn(dev->wl, "RX descriptor underrun\n");
2033 b43_dma_handle_rx_overflow(dev->dma.rx_ring);
2034 }
Michael Buesch5100d5a2008-03-29 21:01:16 +01002035 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
2036 if (b43_using_pio_transfers(dev))
2037 b43_pio_rx(dev->pio.rx_queue);
2038 else
2039 b43_dma_rx(dev->dma.rx_ring);
2040 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002041 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
2042 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01002043 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04002044 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
2045 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
2046
Michael Buesch21954c32007-09-27 15:31:40 +02002047 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04002048 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002049
Michael Buesch36dbd952009-09-04 22:51:29 +02002050 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02002051 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesch990b86f2009-09-12 00:48:03 +02002052
2053#if B43_DEBUG
2054 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
2055 dev->irq_count++;
2056 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
2057 if (reason & (1 << i))
2058 dev->irq_bit_count[i]++;
2059 }
2060 }
2061#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002062}
2063
Michael Buesch36dbd952009-09-04 22:51:29 +02002064/* Interrupt thread handler. Handles device interrupts in thread context. */
2065static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04002066{
Michael Buesche4d6b792007-09-18 15:39:42 -04002067 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02002068
2069 mutex_lock(&dev->wl->mutex);
2070 b43_do_interrupt_thread(dev);
2071 mmiowb();
2072 mutex_unlock(&dev->wl->mutex);
2073
2074 return IRQ_HANDLED;
2075}
2076
2077static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
2078{
Michael Buesche4d6b792007-09-18 15:39:42 -04002079 u32 reason;
2080
Michael Buesch36dbd952009-09-04 22:51:29 +02002081 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
2082 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002083
Michael Buesche4d6b792007-09-18 15:39:42 -04002084 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2085 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02002086 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02002087 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04002088 if (!reason)
Sebastian Andrzej Siewiorcae56142011-07-07 21:58:10 +02002089 return IRQ_NONE;
Michael Buesche4d6b792007-09-18 15:39:42 -04002090
2091 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002092 & 0x0001FC00;
Michael Buesche4d6b792007-09-18 15:39:42 -04002093 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
2094 & 0x0000DC00;
2095 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
2096 & 0x0000DC00;
2097 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
2098 & 0x0001DC00;
2099 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
2100 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002101/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04002102 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
2103 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002104*/
Michael Buesche4d6b792007-09-18 15:39:42 -04002105
Michael Buesch36dbd952009-09-04 22:51:29 +02002106 /* ACK the interrupt. */
2107 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
2108 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
2109 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
2110 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
2111 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
2112 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
2113/* Unused ring
2114 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
2115*/
2116
2117 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02002118 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02002119 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002120 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02002121
2122 return IRQ_WAKE_THREAD;
2123}
2124
2125/* Interrupt handler top-half. This runs with interrupts disabled. */
2126static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
2127{
2128 struct b43_wldev *dev = dev_id;
2129 irqreturn_t ret;
2130
2131 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2132 return IRQ_NONE;
2133
2134 spin_lock(&dev->wl->hardirq_lock);
2135 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002136 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02002137 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04002138
2139 return ret;
2140}
2141
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002142/* SDIO interrupt handler. This runs in process context. */
2143static void b43_sdio_interrupt_handler(struct b43_wldev *dev)
2144{
2145 struct b43_wl *wl = dev->wl;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002146 irqreturn_t ret;
2147
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002148 mutex_lock(&wl->mutex);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002149
2150 ret = b43_do_interrupt(dev);
2151 if (ret == IRQ_WAKE_THREAD)
2152 b43_do_interrupt_thread(dev);
2153
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002154 mutex_unlock(&wl->mutex);
2155}
2156
Michael Buesch1a9f5092009-01-23 21:21:51 +01002157void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002158{
2159 release_firmware(fw->data);
2160 fw->data = NULL;
2161 fw->filename = NULL;
2162}
2163
Michael Buesche4d6b792007-09-18 15:39:42 -04002164static void b43_release_firmware(struct b43_wldev *dev)
2165{
Larry Finger0673eff2014-01-12 15:11:38 -06002166 complete(&dev->fw_load_complete);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002167 b43_do_release_fw(&dev->fw.ucode);
2168 b43_do_release_fw(&dev->fw.pcm);
2169 b43_do_release_fw(&dev->fw.initvals);
2170 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04002171}
2172
Michael Buescheb189d8b2008-01-28 14:47:41 -08002173static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04002174{
Hannes Ederfc68ed42009-02-14 11:50:06 +00002175 const char text[] =
2176 "You must go to " \
2177 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
2178 "and download the correct firmware for this driver version. " \
2179 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08002180
Michael Buescheb189d8b2008-01-28 14:47:41 -08002181 if (error)
2182 b43err(wl, text);
2183 else
2184 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04002185}
2186
Larry Finger5e20a4b2012-12-20 15:55:01 -06002187static void b43_fw_cb(const struct firmware *firmware, void *context)
2188{
2189 struct b43_request_fw_context *ctx = context;
2190
2191 ctx->blob = firmware;
Larry Finger0673eff2014-01-12 15:11:38 -06002192 complete(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002193}
2194
Michael Buesch1a9f5092009-01-23 21:21:51 +01002195int b43_do_request_fw(struct b43_request_fw_context *ctx,
2196 const char *name,
Larry Finger5e20a4b2012-12-20 15:55:01 -06002197 struct b43_firmware_file *fw, bool async)
Michael Buesche4d6b792007-09-18 15:39:42 -04002198{
Michael Buesche4d6b792007-09-18 15:39:42 -04002199 struct b43_fw_header *hdr;
2200 u32 size;
2201 int err;
2202
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002203 if (!name) {
2204 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002205 /* FIXME: We should probably keep it anyway, to save some headache
2206 * on suspend/resume with multiband devices. */
2207 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04002208 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002209 }
2210 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002211 if ((fw->type == ctx->req_type) &&
2212 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002213 return 0; /* Already have this fw. */
2214 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002215 /* FIXME: We should probably do this later after we successfully
2216 * got the new fw. This could reduce headache with multiband devices.
2217 * We could also redesign this to cache the firmware for all possible
2218 * bands all the time. */
2219 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002220 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002221
Michael Buesch1a9f5092009-01-23 21:21:51 +01002222 switch (ctx->req_type) {
2223 case B43_FWTYPE_PROPRIETARY:
2224 snprintf(ctx->fwname, sizeof(ctx->fwname),
2225 "b43%s/%s.fw",
2226 modparam_fwpostfix, name);
2227 break;
2228 case B43_FWTYPE_OPENSOURCE:
2229 snprintf(ctx->fwname, sizeof(ctx->fwname),
2230 "b43-open%s/%s.fw",
2231 modparam_fwpostfix, name);
2232 break;
2233 default:
2234 B43_WARN_ON(1);
2235 return -ENOSYS;
2236 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002237 if (async) {
2238 /* do this part asynchronously */
Larry Finger0673eff2014-01-12 15:11:38 -06002239 init_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002240 err = request_firmware_nowait(THIS_MODULE, 1, ctx->fwname,
2241 ctx->dev->dev->dev, GFP_KERNEL,
2242 ctx, b43_fw_cb);
2243 if (err < 0) {
2244 pr_err("Unable to load firmware\n");
2245 return err;
2246 }
Larry Finger0673eff2014-01-12 15:11:38 -06002247 wait_for_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002248 if (ctx->blob)
2249 goto fw_ready;
2250 /* On some ARM systems, the async request will fail, but the next sync
Larry Finger0673eff2014-01-12 15:11:38 -06002251 * request works. For this reason, we fall through here
Larry Finger5e20a4b2012-12-20 15:55:01 -06002252 */
2253 }
2254 err = request_firmware(&ctx->blob, ctx->fwname,
2255 ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002256 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002257 snprintf(ctx->errors[ctx->req_type],
2258 sizeof(ctx->errors[ctx->req_type]),
Larry Finger5e20a4b2012-12-20 15:55:01 -06002259 "Firmware file \"%s\" not found\n",
2260 ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002261 return err;
2262 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002263 snprintf(ctx->errors[ctx->req_type],
2264 sizeof(ctx->errors[ctx->req_type]),
2265 "Firmware file \"%s\" request failed (err=%d)\n",
2266 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002267 return err;
2268 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002269fw_ready:
2270 if (ctx->blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002271 goto err_format;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002272 hdr = (struct b43_fw_header *)(ctx->blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002273 switch (hdr->type) {
2274 case B43_FW_TYPE_UCODE:
2275 case B43_FW_TYPE_PCM:
2276 size = be32_to_cpu(hdr->size);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002277 if (size != ctx->blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002278 goto err_format;
2279 /* fallthrough */
2280 case B43_FW_TYPE_IV:
2281 if (hdr->ver != 1)
2282 goto err_format;
2283 break;
2284 default:
2285 goto err_format;
2286 }
2287
Larry Finger5e20a4b2012-12-20 15:55:01 -06002288 fw->data = ctx->blob;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002289 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002290 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002291
2292 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002293
2294err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002295 snprintf(ctx->errors[ctx->req_type],
2296 sizeof(ctx->errors[ctx->req_type]),
2297 "Firmware file \"%s\" format error.\n", ctx->fwname);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002298 release_firmware(ctx->blob);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002299
Michael Buesche4d6b792007-09-18 15:39:42 -04002300 return -EPROTO;
2301}
2302
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002303/* http://bcm-v4.sipsolutions.net/802.11/Init/Firmware */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002304static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002305{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002306 struct b43_wldev *dev = ctx->dev;
2307 struct b43_firmware *fw = &ctx->dev->fw;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002308 struct b43_phy *phy = &dev->phy;
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002309 const u8 rev = ctx->dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04002310 const char *filename;
Michael Buesche4d6b792007-09-18 15:39:42 -04002311 int err;
2312
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002313 /* Get microcode */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002314 filename = NULL;
2315 switch (rev) {
2316 case 42:
2317 if (phy->type == B43_PHYTYPE_AC)
2318 filename = "ucode42";
2319 break;
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002320 case 40:
2321 if (phy->type == B43_PHYTYPE_AC)
2322 filename = "ucode40";
2323 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002324 case 33:
2325 if (phy->type == B43_PHYTYPE_LCN40)
2326 filename = "ucode33_lcn40";
2327 break;
2328 case 30:
2329 if (phy->type == B43_PHYTYPE_N)
2330 filename = "ucode30_mimo";
2331 break;
2332 case 29:
2333 if (phy->type == B43_PHYTYPE_HT)
2334 filename = "ucode29_mimo";
2335 break;
2336 case 26:
2337 if (phy->type == B43_PHYTYPE_HT)
2338 filename = "ucode26_mimo";
2339 break;
2340 case 28:
2341 case 25:
2342 if (phy->type == B43_PHYTYPE_N)
2343 filename = "ucode25_mimo";
2344 else if (phy->type == B43_PHYTYPE_LCN)
2345 filename = "ucode25_lcn";
2346 break;
2347 case 24:
2348 if (phy->type == B43_PHYTYPE_LCN)
2349 filename = "ucode24_lcn";
2350 break;
2351 case 23:
2352 if (phy->type == B43_PHYTYPE_N)
2353 filename = "ucode16_mimo";
2354 break;
2355 case 16 ... 19:
2356 if (phy->type == B43_PHYTYPE_N)
2357 filename = "ucode16_mimo";
2358 else if (phy->type == B43_PHYTYPE_LP)
2359 filename = "ucode16_lp";
2360 break;
2361 case 15:
Gábor Stefanik759b9732009-08-14 14:39:53 +02002362 filename = "ucode15";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002363 break;
2364 case 14:
2365 filename = "ucode14";
2366 break;
2367 case 13:
2368 filename = "ucode13";
2369 break;
2370 case 11 ... 12:
2371 filename = "ucode11";
2372 break;
2373 case 5 ... 10:
2374 filename = "ucode5";
2375 break;
Rafał Miłecki6ff1e5c2011-07-06 17:41:55 +02002376 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002377 if (!filename)
2378 goto err_no_ucode;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002379 err = b43_do_request_fw(ctx, filename, &fw->ucode, true);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002380 if (err)
2381 goto err_load;
2382
2383 /* Get PCM code */
2384 if ((rev >= 5) && (rev <= 10))
2385 filename = "pcm5";
2386 else if (rev >= 11)
2387 filename = NULL;
2388 else
2389 goto err_no_pcm;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002390 fw->pcm_request_failed = false;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002391 err = b43_do_request_fw(ctx, filename, &fw->pcm, false);
Michael Buesch68217832008-05-17 23:43:57 +02002392 if (err == -ENOENT) {
2393 /* We did not find a PCM file? Not fatal, but
2394 * core rev <= 10 must do without hwcrypto then. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002395 fw->pcm_request_failed = true;
Michael Buesch68217832008-05-17 23:43:57 +02002396 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002397 goto err_load;
2398
2399 /* Get initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002400 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002401 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002402 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002403 if (rev == 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002404 filename = "b0g0initvals13";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002405 else if (rev >= 5 && rev <= 10)
2406 filename = "b0g0initvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002407 break;
2408 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002409 if (rev == 30)
2410 filename = "n16initvals30";
2411 else if (rev == 28 || rev == 25)
2412 filename = "n0initvals25";
2413 else if (rev == 24)
2414 filename = "n0initvals24";
2415 else if (rev == 23)
2416 filename = "n0initvals16"; /* What about n0initvals22? */
2417 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002418 filename = "n0initvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002419 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002420 filename = "n0initvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002421 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002422 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002423 if (rev >= 16 && rev <= 18)
2424 filename = "lp0initvals16";
2425 else if (rev == 15)
2426 filename = "lp0initvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002427 else if (rev == 14)
2428 filename = "lp0initvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002429 else if (rev == 13)
2430 filename = "lp0initvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002431 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002432 case B43_PHYTYPE_HT:
2433 if (rev == 29)
2434 filename = "ht0initvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002435 else if (rev == 26)
2436 filename = "ht0initvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002437 break;
2438 case B43_PHYTYPE_LCN:
2439 if (rev == 24)
2440 filename = "lcn0initvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002441 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002442 case B43_PHYTYPE_LCN40:
2443 if (rev == 33)
2444 filename = "lcn400initvals33";
2445 break;
2446 case B43_PHYTYPE_AC:
2447 if (rev == 42)
2448 filename = "ac1initvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002449 else if (rev == 40)
2450 filename = "ac0initvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002451 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002452 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002453 if (!filename)
2454 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002455 err = b43_do_request_fw(ctx, filename, &fw->initvals, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002456 if (err)
2457 goto err_load;
2458
2459 /* Get bandswitch initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002460 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002461 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002462 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002463 if (rev == 13)
2464 filename = "b0g0bsinitvals13";
2465 else if (rev >= 5 && rev <= 10)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002466 filename = "b0g0bsinitvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002467 break;
2468 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002469 if (rev == 30)
2470 filename = "n16bsinitvals30";
2471 else if (rev == 28 || rev == 25)
2472 filename = "n0bsinitvals25";
2473 else if (rev == 24)
2474 filename = "n0bsinitvals24";
2475 else if (rev == 23)
2476 filename = "n0bsinitvals16"; /* What about n0bsinitvals22? */
2477 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002478 filename = "n0bsinitvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002479 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002480 filename = "n0bsinitvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002481 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002482 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002483 if (rev >= 16 && rev <= 18)
2484 filename = "lp0bsinitvals16";
2485 else if (rev == 15)
2486 filename = "lp0bsinitvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002487 else if (rev == 14)
2488 filename = "lp0bsinitvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002489 else if (rev == 13)
2490 filename = "lp0bsinitvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002491 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002492 case B43_PHYTYPE_HT:
2493 if (rev == 29)
2494 filename = "ht0bsinitvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002495 else if (rev == 26)
2496 filename = "ht0bsinitvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002497 break;
2498 case B43_PHYTYPE_LCN:
2499 if (rev == 24)
2500 filename = "lcn0bsinitvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002501 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002502 case B43_PHYTYPE_LCN40:
2503 if (rev == 33)
2504 filename = "lcn400bsinitvals33";
2505 break;
2506 case B43_PHYTYPE_AC:
2507 if (rev == 42)
2508 filename = "ac1bsinitvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002509 else if (rev == 40)
2510 filename = "ac0bsinitvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002511 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002512 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002513 if (!filename)
2514 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002515 err = b43_do_request_fw(ctx, filename, &fw->initvals_band, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002516 if (err)
2517 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002518
Johannes Berg097b0e12012-07-17 17:12:29 +02002519 fw->opensource = (ctx->req_type == B43_FWTYPE_OPENSOURCE);
2520
Michael Buesche4d6b792007-09-18 15:39:42 -04002521 return 0;
2522
Michael Buesche4d6b792007-09-18 15:39:42 -04002523err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002524 err = ctx->fatal_failure = -EOPNOTSUPP;
2525 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2526 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002527 goto error;
2528
2529err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002530 err = ctx->fatal_failure = -EOPNOTSUPP;
2531 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2532 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002533 goto error;
2534
2535err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002536 err = ctx->fatal_failure = -EOPNOTSUPP;
2537 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2538 "is required for your device (wl-core rev %u)\n", rev);
2539 goto error;
2540
2541err_load:
2542 /* We failed to load this firmware image. The error message
2543 * already is in ctx->errors. Return and let our caller decide
2544 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002545 goto error;
2546
2547error:
2548 b43_release_firmware(dev);
2549 return err;
2550}
2551
Larry Finger6b6fa582012-03-08 22:27:46 -06002552static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl);
2553static void b43_one_core_detach(struct b43_bus_dev *dev);
Larry Finger09164042014-01-12 15:11:37 -06002554static int b43_rng_init(struct b43_wl *wl);
Larry Finger6b6fa582012-03-08 22:27:46 -06002555
2556static void b43_request_firmware(struct work_struct *work)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002557{
Larry Finger6b6fa582012-03-08 22:27:46 -06002558 struct b43_wl *wl = container_of(work,
2559 struct b43_wl, firmware_load);
2560 struct b43_wldev *dev = wl->current_dev;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002561 struct b43_request_fw_context *ctx;
2562 unsigned int i;
2563 int err;
2564 const char *errmsg;
2565
2566 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2567 if (!ctx)
Larry Finger6b6fa582012-03-08 22:27:46 -06002568 return;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002569 ctx->dev = dev;
2570
2571 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2572 err = b43_try_request_fw(ctx);
2573 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002574 goto start_ieee80211; /* Successfully loaded it. */
2575 /* Was fw version known? */
2576 if (ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002577 goto out;
2578
Larry Finger6b6fa582012-03-08 22:27:46 -06002579 /* proprietary fw not found, try open source */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002580 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2581 err = b43_try_request_fw(ctx);
2582 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002583 goto start_ieee80211; /* Successfully loaded it. */
2584 if(ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002585 goto out;
2586
2587 /* Could not find a usable firmware. Print the errors. */
2588 for (i = 0; i < B43_NR_FWTYPES; i++) {
2589 errmsg = ctx->errors[i];
2590 if (strlen(errmsg))
Kees Cooke0e29b62013-05-10 14:48:21 -07002591 b43err(dev->wl, "%s", errmsg);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002592 }
2593 b43_print_fw_helptext(dev->wl, 1);
Larry Finger6b6fa582012-03-08 22:27:46 -06002594 goto out;
2595
2596start_ieee80211:
Johannes Berg097b0e12012-07-17 17:12:29 +02002597 wl->hw->queues = B43_QOS_QUEUE_NUM;
2598 if (!modparam_qos || dev->fw.opensource)
2599 wl->hw->queues = 1;
2600
Larry Finger6b6fa582012-03-08 22:27:46 -06002601 err = ieee80211_register_hw(wl->hw);
2602 if (err)
2603 goto err_one_core_detach;
Oleksij Rempele64add22012-06-05 20:39:32 +02002604 wl->hw_registred = true;
Larry Finger6b6fa582012-03-08 22:27:46 -06002605 b43_leds_register(wl->current_dev);
Larry Finger09164042014-01-12 15:11:37 -06002606
2607 /* Register HW RNG driver */
2608 b43_rng_init(wl);
2609
Larry Finger6b6fa582012-03-08 22:27:46 -06002610 goto out;
2611
2612err_one_core_detach:
2613 b43_one_core_detach(dev->dev);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002614
2615out:
2616 kfree(ctx);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002617}
2618
Michael Buesche4d6b792007-09-18 15:39:42 -04002619static int b43_upload_microcode(struct b43_wldev *dev)
2620{
John W. Linville652caa52010-07-29 13:27:28 -04002621 struct wiphy *wiphy = dev->wl->hw->wiphy;
Michael Buesche4d6b792007-09-18 15:39:42 -04002622 const size_t hdr_len = sizeof(struct b43_fw_header);
2623 const __be32 *data;
2624 unsigned int i, len;
2625 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002626 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002627 int err = 0;
2628
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002629 /* Jump the microcode PSM to offset 0 */
2630 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2631 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2632 macctl |= B43_MACCTL_PSM_JMP0;
2633 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2634 /* Zero out all microcode PSM registers and shared memory. */
2635 for (i = 0; i < 64; i++)
2636 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2637 for (i = 0; i < 4096; i += 2)
2638 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2639
Michael Buesche4d6b792007-09-18 15:39:42 -04002640 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002641 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2642 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002643 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2644 for (i = 0; i < len; i++) {
2645 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2646 udelay(10);
2647 }
2648
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002649 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002650 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002651 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2652 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002653 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2654 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2655 /* No need for autoinc bit in SHM_HW */
2656 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2657 for (i = 0; i < len; i++) {
2658 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2659 udelay(10);
2660 }
2661 }
2662
2663 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002664
2665 /* Start the microcode PSM */
Rafał Miłecki50566352012-01-02 19:31:21 +01002666 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_JMP0,
2667 B43_MACCTL_PSM_RUN);
Michael Buesche4d6b792007-09-18 15:39:42 -04002668
2669 /* Wait for the microcode to load and respond */
2670 i = 0;
2671 while (1) {
2672 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2673 if (tmp == B43_IRQ_MAC_SUSPENDED)
2674 break;
2675 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002676 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002677 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002678 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002679 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002680 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002681 }
Michael Buesche175e992009-09-11 18:31:32 +02002682 msleep(50);
Michael Buesche4d6b792007-09-18 15:39:42 -04002683 }
2684 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2685
2686 /* Get and check the revisions. */
2687 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2688 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2689 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2690 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2691
2692 if (fwrev <= 0x128) {
2693 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2694 "binary drivers older than version 4.x is unsupported. "
2695 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002696 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002697 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002698 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002699 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002700 dev->fw.rev = fwrev;
2701 dev->fw.patch = fwpatch;
Rafał Miłecki5d852902011-08-11 15:07:16 +02002702 if (dev->fw.rev >= 598)
2703 dev->fw.hdr_format = B43_FW_HDR_598;
2704 else if (dev->fw.rev >= 410)
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002705 dev->fw.hdr_format = B43_FW_HDR_410;
2706 else
2707 dev->fw.hdr_format = B43_FW_HDR_351;
Johannes Berg097b0e12012-07-17 17:12:29 +02002708 WARN_ON(dev->fw.opensource != (fwdate == 0xFFFF));
Michael Buesche48b0ee2008-05-17 22:44:35 +02002709
Johannes Berg097b0e12012-07-17 17:12:29 +02002710 dev->qos_enabled = dev->wl->hw->queues > 1;
Michael Buesch403a3a12009-06-08 21:04:57 +02002711 /* Default to firmware/hardware crypto acceleration. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002712 dev->hwcrypto_enabled = true;
Michael Buesch403a3a12009-06-08 21:04:57 +02002713
Michael Buesche48b0ee2008-05-17 22:44:35 +02002714 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002715 u16 fwcapa;
2716
Michael Buesche48b0ee2008-05-17 22:44:35 +02002717 /* Patchlevel info is encoded in the "time" field. */
2718 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002719 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2720 dev->fw.rev, dev->fw.patch);
2721
2722 fwcapa = b43_fwcapa_read(dev);
2723 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2724 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2725 /* Disable hardware crypto and fall back to software crypto. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002726 dev->hwcrypto_enabled = false;
Michael Buesch403a3a12009-06-08 21:04:57 +02002727 }
Johannes Berg097b0e12012-07-17 17:12:29 +02002728 /* adding QoS support should use an offline discovery mechanism */
2729 WARN(fwcapa & B43_FWCAPA_QOS, "QoS in OpenFW not supported\n");
Michael Buesche48b0ee2008-05-17 22:44:35 +02002730 } else {
2731 b43info(dev->wl, "Loading firmware version %u.%u "
2732 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2733 fwrev, fwpatch,
2734 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2735 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002736 if (dev->fw.pcm_request_failed) {
2737 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2738 "Hardware accelerated cryptography is disabled.\n");
2739 b43_print_fw_helptext(dev->wl, 0);
2740 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002741 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002742
John W. Linville652caa52010-07-29 13:27:28 -04002743 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
2744 dev->fw.rev, dev->fw.patch);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002745 wiphy->hw_version = dev->dev->core_id;
John W. Linville652caa52010-07-29 13:27:28 -04002746
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002747 if (dev->fw.hdr_format == B43_FW_HDR_351) {
Michael Bueschc5572892008-12-27 18:26:39 +01002748 /* We're over the deadline, but we keep support for old fw
2749 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002750 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002751 "Support for old firmware will be removed soon "
2752 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002753 b43_print_fw_helptext(dev->wl, 0);
2754 }
2755
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002756 return 0;
2757
2758error:
Rafał Miłecki50566352012-01-02 19:31:21 +01002759 /* Stop the microcode PSM. */
2760 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
2761 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002762
Michael Buesche4d6b792007-09-18 15:39:42 -04002763 return err;
2764}
2765
2766static int b43_write_initvals(struct b43_wldev *dev,
2767 const struct b43_iv *ivals,
2768 size_t count,
2769 size_t array_size)
2770{
2771 const struct b43_iv *iv;
2772 u16 offset;
2773 size_t i;
2774 bool bit32;
2775
2776 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2777 iv = ivals;
2778 for (i = 0; i < count; i++) {
2779 if (array_size < sizeof(iv->offset_size))
2780 goto err_format;
2781 array_size -= sizeof(iv->offset_size);
2782 offset = be16_to_cpu(iv->offset_size);
2783 bit32 = !!(offset & B43_IV_32BIT);
2784 offset &= B43_IV_OFFSET_MASK;
2785 if (offset >= 0x1000)
2786 goto err_format;
2787 if (bit32) {
2788 u32 value;
2789
2790 if (array_size < sizeof(iv->data.d32))
2791 goto err_format;
2792 array_size -= sizeof(iv->data.d32);
2793
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002794 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002795 b43_write32(dev, offset, value);
2796
2797 iv = (const struct b43_iv *)((const uint8_t *)iv +
2798 sizeof(__be16) +
2799 sizeof(__be32));
2800 } else {
2801 u16 value;
2802
2803 if (array_size < sizeof(iv->data.d16))
2804 goto err_format;
2805 array_size -= sizeof(iv->data.d16);
2806
2807 value = be16_to_cpu(iv->data.d16);
2808 b43_write16(dev, offset, value);
2809
2810 iv = (const struct b43_iv *)((const uint8_t *)iv +
2811 sizeof(__be16) +
2812 sizeof(__be16));
2813 }
2814 }
2815 if (array_size)
2816 goto err_format;
2817
2818 return 0;
2819
2820err_format:
2821 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002822 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002823
2824 return -EPROTO;
2825}
2826
2827static int b43_upload_initvals(struct b43_wldev *dev)
2828{
2829 const size_t hdr_len = sizeof(struct b43_fw_header);
2830 const struct b43_fw_header *hdr;
2831 struct b43_firmware *fw = &dev->fw;
2832 const struct b43_iv *ivals;
2833 size_t count;
Michael Buesche4d6b792007-09-18 15:39:42 -04002834
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002835 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2836 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002837 count = be32_to_cpu(hdr->size);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002838 return b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002839 fw->initvals.data->size - hdr_len);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002840}
Michael Buesche4d6b792007-09-18 15:39:42 -04002841
Rafał Miłecki0f684232014-05-17 23:24:53 +02002842static int b43_upload_initvals_band(struct b43_wldev *dev)
2843{
2844 const size_t hdr_len = sizeof(struct b43_fw_header);
2845 const struct b43_fw_header *hdr;
2846 struct b43_firmware *fw = &dev->fw;
2847 const struct b43_iv *ivals;
2848 size_t count;
2849
2850 if (!fw->initvals_band.data)
2851 return 0;
2852
2853 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2854 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2855 count = be32_to_cpu(hdr->size);
2856 return b43_write_initvals(dev, ivals, count,
2857 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002858}
2859
2860/* Initialize the GPIOs
2861 * http://bcm-specs.sipsolutions.net/GPIO
2862 */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002863
2864#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002865static struct ssb_device *b43_ssb_gpio_dev(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002866{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02002867 struct ssb_bus *bus = dev->dev->sdev->bus;
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002868
2869#ifdef CONFIG_SSB_DRIVER_PCICORE
2870 return (bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev);
2871#else
2872 return bus->chipco.dev;
2873#endif
2874}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002875#endif
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002876
Michael Buesche4d6b792007-09-18 15:39:42 -04002877static int b43_gpio_init(struct b43_wldev *dev)
2878{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002879#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002880 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002881#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002882 u32 mask, set;
2883
Rafał Miłecki50566352012-01-02 19:31:21 +01002884 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_GPOUTSMSK, 0);
2885 b43_maskset16(dev, B43_MMIO_GPIO_MASK, ~0, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04002886
2887 mask = 0x0000001F;
2888 set = 0x0000000F;
Rafał Miłeckic244e082011-05-18 02:06:41 +02002889 if (dev->dev->chip_id == 0x4301) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002890 mask |= 0x0060;
2891 set |= 0x0060;
Rafał Miłecki828afd22012-07-23 22:57:01 +02002892 } else if (dev->dev->chip_id == 0x5354) {
2893 /* Don't allow overtaking buttons GPIOs */
2894 set &= 0x2; /* 0x2 is LED GPIO on BCM5354 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002895 }
Rafał Miłecki828afd22012-07-23 22:57:01 +02002896
Michael Buesche4d6b792007-09-18 15:39:42 -04002897 if (0 /* FIXME: conditional unknown */ ) {
2898 b43_write16(dev, B43_MMIO_GPIO_MASK,
2899 b43_read16(dev, B43_MMIO_GPIO_MASK)
2900 | 0x0100);
Rafał Miłecki828afd22012-07-23 22:57:01 +02002901 /* BT Coexistance Input */
2902 mask |= 0x0080;
2903 set |= 0x0080;
2904 /* BT Coexistance Out */
2905 mask |= 0x0100;
2906 set |= 0x0100;
Michael Buesche4d6b792007-09-18 15:39:42 -04002907 }
Rafał Miłecki05814832011-05-18 02:06:39 +02002908 if (dev->dev->bus_sprom->boardflags_lo & B43_BFL_PACTRL) {
Rafał Miłecki828afd22012-07-23 22:57:01 +02002909 /* PA is controlled by gpio 9, let ucode handle it */
Michael Buesche4d6b792007-09-18 15:39:42 -04002910 b43_write16(dev, B43_MMIO_GPIO_MASK,
2911 b43_read16(dev, B43_MMIO_GPIO_MASK)
2912 | 0x0200);
2913 mask |= 0x0200;
2914 set |= 0x0200;
2915 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002916
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002917 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002918#ifdef CONFIG_B43_BCMA
2919 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002920 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, mask, set);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002921 break;
2922#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002923#ifdef CONFIG_B43_SSB
2924 case B43_BUS_SSB:
2925 gpiodev = b43_ssb_gpio_dev(dev);
2926 if (gpiodev)
2927 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2928 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
Rafał Miłecki828afd22012-07-23 22:57:01 +02002929 & ~mask) | set);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002930 break;
2931#endif
2932 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002933
2934 return 0;
2935}
2936
2937/* Turn off all GPIO stuff. Call this on module unload, for example. */
2938static void b43_gpio_cleanup(struct b43_wldev *dev)
2939{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002940#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a0812011-05-17 18:57:27 +02002941 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002942#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002943
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002944 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002945#ifdef CONFIG_B43_BCMA
2946 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002947 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, ~0, 0);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002948 break;
2949#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002950#ifdef CONFIG_B43_SSB
2951 case B43_BUS_SSB:
2952 gpiodev = b43_ssb_gpio_dev(dev);
2953 if (gpiodev)
2954 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2955 break;
2956#endif
2957 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002958}
2959
2960/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002961void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002962{
Michael Buesch923fd702008-06-20 18:02:08 +02002963 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2964 u16 fwstate;
2965
2966 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2967 B43_SHM_SH_UCODESTAT);
2968 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2969 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2970 b43err(dev->wl, "b43_mac_enable(): The firmware "
2971 "should be suspended, but current state is %u\n",
2972 fwstate);
2973 }
2974 }
2975
Michael Buesche4d6b792007-09-18 15:39:42 -04002976 dev->mac_suspended--;
2977 B43_WARN_ON(dev->mac_suspended < 0);
2978 if (dev->mac_suspended == 0) {
Rafał Miłecki50566352012-01-02 19:31:21 +01002979 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_ENABLED);
Michael Buesche4d6b792007-09-18 15:39:42 -04002980 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2981 B43_IRQ_MAC_SUSPENDED);
2982 /* Commit writes */
2983 b43_read32(dev, B43_MMIO_MACCTL);
2984 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2985 b43_power_saving_ctl_bits(dev, 0);
2986 }
2987}
2988
2989/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002990void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002991{
2992 int i;
2993 u32 tmp;
2994
Michael Buesch05b64b32007-09-28 16:19:03 +02002995 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002996 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002997
Michael Buesche4d6b792007-09-18 15:39:42 -04002998 if (dev->mac_suspended == 0) {
2999 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Rafał Miłecki50566352012-01-02 19:31:21 +01003000 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_ENABLED, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003001 /* force pci to flush the write */
3002 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02003003 for (i = 35; i; i--) {
3004 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
3005 if (tmp & B43_IRQ_MAC_SUSPENDED)
3006 goto out;
3007 udelay(10);
3008 }
3009 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02003010 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003011 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
3012 if (tmp & B43_IRQ_MAC_SUSPENDED)
3013 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02003014 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003015 }
3016 b43err(dev->wl, "MAC suspend failed\n");
3017 }
Michael Buesch05b64b32007-09-28 16:19:03 +02003018out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003019 dev->mac_suspended++;
3020}
3021
Rafał Miłecki858a1652011-05-10 16:05:33 +02003022/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MacPhyClkSet */
3023void b43_mac_phy_clock_set(struct b43_wldev *dev, bool on)
3024{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003025 u32 tmp;
3026
3027 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003028#ifdef CONFIG_B43_BCMA
3029 case B43_BUS_BCMA:
Rafał Miłecki36677872011-07-16 18:27:55 +02003030 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003031 if (on)
3032 tmp |= B43_BCMA_IOCTL_MACPHYCLKEN;
3033 else
3034 tmp &= ~B43_BCMA_IOCTL_MACPHYCLKEN;
Rafał Miłecki36677872011-07-16 18:27:55 +02003035 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003036 break;
3037#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003038#ifdef CONFIG_B43_SSB
3039 case B43_BUS_SSB:
3040 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3041 if (on)
3042 tmp |= B43_TMSLOW_MACPHYCLKEN;
3043 else
3044 tmp &= ~B43_TMSLOW_MACPHYCLKEN;
3045 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3046 break;
3047#endif
3048 }
Rafał Miłecki858a1652011-05-10 16:05:33 +02003049}
3050
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02003051/* brcms_b_switch_macfreq */
3052void b43_mac_switch_freq(struct b43_wldev *dev, u8 spurmode)
3053{
3054 u16 chip_id = dev->dev->chip_id;
3055
Rafał Miłeckibc944502014-09-10 09:07:13 +02003056 if (chip_id == BCMA_CHIP_ID_BCM4331) {
3057 switch (spurmode) {
3058 case 2: /* 168 Mhz: 2^26/168 = 0x61862 */
3059 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x1862);
3060 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3061 break;
3062 case 1: /* 164 Mhz: 2^26/164 = 0x63e70 */
3063 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x3e70);
3064 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3065 break;
3066 default: /* 160 Mhz: 2^26/160 = 0x66666 */
3067 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x6666);
3068 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x6);
3069 break;
3070 }
3071 } else if (chip_id == BCMA_CHIP_ID_BCM43131 ||
Rafał Miłeckia67d19d2014-07-24 15:29:18 +02003072 chip_id == BCMA_CHIP_ID_BCM43217 ||
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02003073 chip_id == BCMA_CHIP_ID_BCM43222 ||
3074 chip_id == BCMA_CHIP_ID_BCM43224 ||
3075 chip_id == BCMA_CHIP_ID_BCM43225 ||
3076 chip_id == BCMA_CHIP_ID_BCM43227 ||
3077 chip_id == BCMA_CHIP_ID_BCM43228) {
3078 switch (spurmode) {
3079 case 2: /* 126 Mhz */
3080 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x2082);
3081 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3082 break;
3083 case 1: /* 123 Mhz */
3084 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x5341);
3085 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3086 break;
3087 default: /* 120 Mhz */
3088 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x8889);
3089 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
3090 break;
3091 }
3092 } else if (dev->phy.type == B43_PHYTYPE_LCN) {
3093 switch (spurmode) {
3094 case 1: /* 82 Mhz */
3095 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x7CE0);
3096 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3097 break;
3098 default: /* 80 Mhz */
3099 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0xCCCD);
3100 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3101 break;
3102 }
3103 }
3104}
3105
Michael Buesche4d6b792007-09-18 15:39:42 -04003106static void b43_adjust_opmode(struct b43_wldev *dev)
3107{
3108 struct b43_wl *wl = dev->wl;
3109 u32 ctl;
3110 u16 cfp_pretbtt;
3111
3112 ctl = b43_read32(dev, B43_MMIO_MACCTL);
3113 /* Reset status to STA infrastructure mode. */
3114 ctl &= ~B43_MACCTL_AP;
3115 ctl &= ~B43_MACCTL_KEEP_CTL;
3116 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
3117 ctl &= ~B43_MACCTL_KEEP_BAD;
3118 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003119 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04003120 ctl |= B43_MACCTL_INFRA;
3121
Johannes Berg05c914f2008-09-11 00:01:58 +02003122 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3123 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04003124 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02003125 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04003126 ctl &= ~B43_MACCTL_INFRA;
3127
3128 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04003129 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04003130 if (wl->filter_flags & FIF_FCSFAIL)
3131 ctl |= B43_MACCTL_KEEP_BAD;
3132 if (wl->filter_flags & FIF_PLCPFAIL)
3133 ctl |= B43_MACCTL_KEEP_BADPLCP;
Johannes Berg4150c572007-09-17 01:29:23 -04003134 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
3135 ctl |= B43_MACCTL_BEACPROMISC;
3136
Michael Buesche4d6b792007-09-18 15:39:42 -04003137 /* Workaround: On old hardware the HW-MAC-address-filter
3138 * doesn't work properly, so always run promisc in filter
3139 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003140 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04003141 ctl |= B43_MACCTL_PROMISC;
3142
3143 b43_write32(dev, B43_MMIO_MACCTL, ctl);
3144
3145 cfp_pretbtt = 2;
3146 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02003147 if (dev->dev->chip_id == 0x4306 &&
3148 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04003149 cfp_pretbtt = 100;
3150 else
3151 cfp_pretbtt = 50;
3152 }
3153 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02003154
3155 /* FIXME: We don't currently implement the PMQ mechanism,
3156 * so always disable it. If we want to implement PMQ,
3157 * we need to enable it here (clear DISCPMQ) in AP mode.
3158 */
Rafał Miłecki50566352012-01-02 19:31:21 +01003159 if (0 /* ctl & B43_MACCTL_AP */)
3160 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3161 else
3162 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003163}
3164
3165static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3166{
3167 u16 offset;
3168
3169 if (is_ofdm) {
3170 offset = 0x480;
3171 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3172 } else {
3173 offset = 0x4C0;
3174 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3175 }
3176 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3177 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3178}
3179
3180static void b43_rate_memory_init(struct b43_wldev *dev)
3181{
3182 switch (dev->phy.type) {
3183 case B43_PHYTYPE_A:
3184 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003185 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003186 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003187 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003188 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003189 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
Hauke Mehrtens30adb4d2014-09-14 23:09:10 +02003190 b43_rate_memory_write(dev, B43_OFDM_RATE_9MB, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003191 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3192 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3193 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3194 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3195 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3196 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3197 if (dev->phy.type == B43_PHYTYPE_A)
3198 break;
3199 /* fallthrough */
3200 case B43_PHYTYPE_B:
3201 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3202 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3203 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3204 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3205 break;
3206 default:
3207 B43_WARN_ON(1);
3208 }
3209}
3210
Michael Buesch5042c502008-04-05 15:05:00 +02003211/* Set the default values for the PHY TX Control Words. */
3212static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3213{
3214 u16 ctl = 0;
3215
3216 ctl |= B43_TXH_PHY_ENC_CCK;
3217 ctl |= B43_TXH_PHY_ANT01AUTO;
3218 ctl |= B43_TXH_PHY_TXPWR;
3219
3220 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3221 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3222 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3223}
3224
Michael Buesche4d6b792007-09-18 15:39:42 -04003225/* Set the TX-Antenna for management frames sent by firmware. */
3226static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3227{
Michael Buesch5042c502008-04-05 15:05:00 +02003228 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003229 u16 tmp;
3230
Michael Buesch5042c502008-04-05 15:05:00 +02003231 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003232
Michael Buesche4d6b792007-09-18 15:39:42 -04003233 /* For ACK/CTS */
3234 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003235 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003236 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3237 /* For Probe Resposes */
3238 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003239 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003240 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3241}
3242
3243/* This is the opposite of b43_chip_init() */
3244static void b43_chip_exit(struct b43_wldev *dev)
3245{
Michael Bueschfb111372008-09-02 13:00:34 +02003246 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003247 b43_gpio_cleanup(dev);
3248 /* firmware is released later */
3249}
3250
3251/* Initialize the chip
3252 * http://bcm-specs.sipsolutions.net/ChipInit
3253 */
3254static int b43_chip_init(struct b43_wldev *dev)
3255{
3256 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003257 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003258 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003259 u16 value16;
3260
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003261 /* Initialize the MAC control */
3262 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3263 if (dev->phy.gmode)
3264 macctl |= B43_MACCTL_GMODE;
3265 macctl |= B43_MACCTL_INFRA;
3266 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003267
Michael Buesche4d6b792007-09-18 15:39:42 -04003268 err = b43_upload_microcode(dev);
3269 if (err)
3270 goto out; /* firmware is released later */
3271
3272 err = b43_gpio_init(dev);
3273 if (err)
3274 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003275
Michael Buesche4d6b792007-09-18 15:39:42 -04003276 err = b43_upload_initvals(dev);
3277 if (err)
Larry Finger1a8d12272007-12-14 13:59:11 +01003278 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003279
Rafał Miłecki0f684232014-05-17 23:24:53 +02003280 err = b43_upload_initvals_band(dev);
3281 if (err)
3282 goto err_gpio_clean;
3283
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003284 /* Turn the Analog on and initialize the PHY. */
3285 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003286 err = b43_phy_init(dev);
3287 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003288 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003289
Michael Bueschef1a6282008-08-27 18:53:02 +02003290 /* Disable Interference Mitigation. */
3291 if (phy->ops->interf_mitigation)
3292 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003293
Michael Bueschef1a6282008-08-27 18:53:02 +02003294 /* Select the antennae */
3295 if (phy->ops->set_rx_antenna)
3296 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003297 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3298
3299 if (phy->type == B43_PHYTYPE_B) {
3300 value16 = b43_read16(dev, 0x005E);
3301 value16 |= 0x0004;
3302 b43_write16(dev, 0x005E, value16);
3303 }
3304 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003305 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003306 b43_write32(dev, 0x010C, 0x01000000);
3307
Rafał Miłecki50566352012-01-02 19:31:21 +01003308 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3309 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003310
Michael Buesche4d6b792007-09-18 15:39:42 -04003311 /* Probe Response Timeout value */
3312 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003313 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003314
3315 /* Initially set the wireless operation mode. */
3316 b43_adjust_opmode(dev);
3317
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003318 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003319 b43_write16(dev, 0x060E, 0x0000);
3320 b43_write16(dev, 0x0610, 0x8000);
3321 b43_write16(dev, 0x0604, 0x0000);
3322 b43_write16(dev, 0x0606, 0x0200);
3323 } else {
3324 b43_write32(dev, 0x0188, 0x80000000);
3325 b43_write32(dev, 0x018C, 0x02000000);
3326 }
3327 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003328 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003329 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3330 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3331 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3332 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3333 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3334
Rafał Miłecki858a1652011-05-10 16:05:33 +02003335 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003336
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003337 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003338#ifdef CONFIG_B43_BCMA
3339 case B43_BUS_BCMA:
3340 /* FIXME: 0xE74 is quite common, but should be read from CC */
3341 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3342 break;
3343#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003344#ifdef CONFIG_B43_SSB
3345 case B43_BUS_SSB:
3346 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3347 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3348 break;
3349#endif
3350 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003351
3352 err = 0;
3353 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003354out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003355 return err;
3356
Larry Finger1a8d12272007-12-14 13:59:11 +01003357err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003358 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003359 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003360}
3361
Michael Buesche4d6b792007-09-18 15:39:42 -04003362static void b43_periodic_every60sec(struct b43_wldev *dev)
3363{
Michael Bueschef1a6282008-08-27 18:53:02 +02003364 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003365
Michael Bueschef1a6282008-08-27 18:53:02 +02003366 if (ops->pwork_60sec)
3367 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003368
3369 /* Force check the TX power emission now. */
3370 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003371}
3372
3373static void b43_periodic_every30sec(struct b43_wldev *dev)
3374{
3375 /* Update device statistics. */
3376 b43_calculate_link_quality(dev);
3377}
3378
3379static void b43_periodic_every15sec(struct b43_wldev *dev)
3380{
3381 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003382 u16 wdr;
3383
3384 if (dev->fw.opensource) {
3385 /* Check if the firmware is still alive.
3386 * It will reset the watchdog counter to 0 in its idle loop. */
3387 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3388 if (unlikely(wdr)) {
3389 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3390 b43_controller_restart(dev, "Firmware watchdog");
3391 return;
3392 } else {
3393 b43_shm_write16(dev, B43_SHM_SCRATCH,
3394 B43_WATCHDOG_REG, 1);
3395 }
3396 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003397
Michael Bueschef1a6282008-08-27 18:53:02 +02003398 if (phy->ops->pwork_15sec)
3399 phy->ops->pwork_15sec(dev);
3400
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003401 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3402 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003403
3404#if B43_DEBUG
3405 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3406 unsigned int i;
3407
3408 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3409 dev->irq_count / 15,
3410 dev->tx_count / 15,
3411 dev->rx_count / 15);
3412 dev->irq_count = 0;
3413 dev->tx_count = 0;
3414 dev->rx_count = 0;
3415 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3416 if (dev->irq_bit_count[i]) {
3417 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3418 dev->irq_bit_count[i] / 15, i, (1 << i));
3419 dev->irq_bit_count[i] = 0;
3420 }
3421 }
3422 }
3423#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003424}
3425
Michael Buesche4d6b792007-09-18 15:39:42 -04003426static void do_periodic_work(struct b43_wldev *dev)
3427{
3428 unsigned int state;
3429
3430 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003431 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003432 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003433 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003434 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003435 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003436}
3437
Michael Buesch05b64b32007-09-28 16:19:03 +02003438/* Periodic work locking policy:
3439 * The whole periodic work handler is protected by
3440 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003441 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003442 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003443static void b43_periodic_work_handler(struct work_struct *work)
3444{
Michael Buesch05b64b32007-09-28 16:19:03 +02003445 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3446 periodic_work.work);
3447 struct b43_wl *wl = dev->wl;
3448 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003449
Michael Buesch05b64b32007-09-28 16:19:03 +02003450 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003451
3452 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3453 goto out;
3454 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3455 goto out_requeue;
3456
Michael Buesch05b64b32007-09-28 16:19:03 +02003457 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003458
Michael Buesche4d6b792007-09-18 15:39:42 -04003459 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003460out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003461 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3462 delay = msecs_to_jiffies(50);
3463 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003464 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003465 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003466out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003467 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003468}
3469
3470static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3471{
3472 struct delayed_work *work = &dev->periodic_work;
3473
3474 dev->periodic_state = 0;
3475 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003476 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003477}
3478
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003479/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003480static int b43_validate_chipaccess(struct b43_wldev *dev)
3481{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003482 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003483
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003484 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3485 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003486
3487 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003488 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3489 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3490 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003491 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3492 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003493 goto error;
3494
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003495 /* Check if unaligned 32bit SHM_SHARED access works properly.
3496 * However, don't bail out on failure, because it's noncritical. */
3497 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3498 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3499 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3500 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3501 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3502 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3503 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3504 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3505 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3506 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3507 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3508 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3509
3510 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3511 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003512
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003513 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003514 /* The 32bit register shadows the two 16bit registers
3515 * with update sideeffects. Validate this. */
3516 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3517 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3518 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3519 goto error;
3520 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3521 goto error;
3522 }
3523 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3524
3525 v = b43_read32(dev, B43_MMIO_MACCTL);
3526 v |= B43_MACCTL_GMODE;
3527 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003528 goto error;
3529
3530 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003531error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003532 b43err(dev->wl, "Failed to validate the chipaccess\n");
3533 return -ENODEV;
3534}
3535
3536static void b43_security_init(struct b43_wldev *dev)
3537{
Michael Buesche4d6b792007-09-18 15:39:42 -04003538 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3539 /* KTP is a word address, but we address SHM bytewise.
3540 * So multiply by two.
3541 */
3542 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003543 /* Number of RCMTA address slots */
3544 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3545 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003546 b43_clear_keys(dev);
3547}
3548
Michael Buesch616de352009-03-29 13:19:31 +02003549#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003550static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003551{
3552 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003553 struct b43_wldev *dev;
3554 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003555
Michael Buescha78b3bb2009-09-11 21:44:05 +02003556 mutex_lock(&wl->mutex);
3557 dev = wl->current_dev;
3558 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3559 *data = b43_read16(dev, B43_MMIO_RNG);
3560 count = sizeof(u16);
3561 }
3562 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003563
Michael Buescha78b3bb2009-09-11 21:44:05 +02003564 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003565}
Michael Buesch616de352009-03-29 13:19:31 +02003566#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003567
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003568static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003569{
Michael Buesch616de352009-03-29 13:19:31 +02003570#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003571 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003572 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003573#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003574}
3575
3576static int b43_rng_init(struct b43_wl *wl)
3577{
Michael Buesch616de352009-03-29 13:19:31 +02003578 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003579
Michael Buesch616de352009-03-29 13:19:31 +02003580#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003581 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3582 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3583 wl->rng.name = wl->rng_name;
3584 wl->rng.data_read = b43_rng_read;
3585 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003586 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003587 err = hwrng_register(&wl->rng);
3588 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003589 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003590 b43err(wl, "Failed to register the random "
3591 "number generator (%d)\n", err);
3592 }
Michael Buesch616de352009-03-29 13:19:31 +02003593#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003594
3595 return err;
3596}
3597
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003598static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003599{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003600 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3601 struct b43_wldev *dev;
3602 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003603 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003604 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003605
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003606 mutex_lock(&wl->mutex);
3607 dev = wl->current_dev;
3608 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3609 mutex_unlock(&wl->mutex);
3610 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003611 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003612
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003613 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3614 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3615 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3616 if (b43_using_pio_transfers(dev))
3617 err = b43_pio_tx(dev, skb);
3618 else
3619 err = b43_dma_tx(dev, skb);
3620 if (err == -ENOSPC) {
3621 wl->tx_queue_stopped[queue_num] = 1;
3622 ieee80211_stop_queue(wl->hw, queue_num);
3623 skb_queue_head(&wl->tx_queue[queue_num], skb);
3624 break;
3625 }
3626 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003627 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003628 err = 0;
3629 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003630
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003631 if (!err)
3632 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003633 }
3634
Michael Buesch990b86f2009-09-12 00:48:03 +02003635#if B43_DEBUG
3636 dev->tx_count++;
3637#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003638 mutex_unlock(&wl->mutex);
3639}
Michael Buesch21a75d72008-04-25 19:29:08 +02003640
Johannes Berg7bb45682011-02-24 14:42:06 +01003641static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003642 struct ieee80211_tx_control *control,
3643 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003644{
3645 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003646
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003647 if (unlikely(skb->len < 2 + 2 + 6)) {
3648 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003649 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003650 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003651 }
3652 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3653
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003654 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3655 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3656 ieee80211_queue_work(wl->hw, &wl->tx_work);
3657 } else {
3658 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3659 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003660}
3661
Michael Buesche6f5b932008-03-05 21:18:49 +01003662static void b43_qos_params_upload(struct b43_wldev *dev,
3663 const struct ieee80211_tx_queue_params *p,
3664 u16 shm_offset)
3665{
3666 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003667 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003668 unsigned int i;
3669
Michael Bueschb0544eb2009-09-06 15:42:45 +02003670 if (!dev->qos_enabled)
3671 return;
3672
Johannes Berg0b576642008-07-15 02:08:24 -07003673 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003674
3675 memset(&params, 0, sizeof(params));
3676
3677 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003678 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3679 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3680 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3681 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003682 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003683 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003684
3685 for (i = 0; i < ARRAY_SIZE(params); i++) {
3686 if (i == B43_QOSPARAM_STATUS) {
3687 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3688 shm_offset + (i * 2));
3689 /* Mark the parameters as updated. */
3690 tmp |= 0x100;
3691 b43_shm_write16(dev, B43_SHM_SHARED,
3692 shm_offset + (i * 2),
3693 tmp);
3694 } else {
3695 b43_shm_write16(dev, B43_SHM_SHARED,
3696 shm_offset + (i * 2),
3697 params[i]);
3698 }
3699 }
3700}
3701
Michael Bueschc40c1122008-09-06 16:21:47 +02003702/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3703static const u16 b43_qos_shm_offsets[] = {
3704 /* [mac80211-queue-nr] = SHM_OFFSET, */
3705 [0] = B43_QOS_VOICE,
3706 [1] = B43_QOS_VIDEO,
3707 [2] = B43_QOS_BESTEFFORT,
3708 [3] = B43_QOS_BACKGROUND,
3709};
3710
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003711/* Update all QOS parameters in hardware. */
3712static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003713{
3714 struct b43_wl *wl = dev->wl;
3715 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003716 unsigned int i;
3717
Michael Bueschb0544eb2009-09-06 15:42:45 +02003718 if (!dev->qos_enabled)
3719 return;
3720
Michael Bueschc40c1122008-09-06 16:21:47 +02003721 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3722 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003723
3724 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003725 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3726 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003727 b43_qos_params_upload(dev, &(params->p),
3728 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003729 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003730 b43_mac_enable(dev);
3731}
3732
3733static void b43_qos_clear(struct b43_wl *wl)
3734{
3735 struct b43_qos_params *params;
3736 unsigned int i;
3737
Michael Bueschc40c1122008-09-06 16:21:47 +02003738 /* Initialize QoS parameters to sane defaults. */
3739
3740 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3741 ARRAY_SIZE(wl->qos_params));
3742
Michael Buesche6f5b932008-03-05 21:18:49 +01003743 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3744 params = &(wl->qos_params[i]);
3745
Michael Bueschc40c1122008-09-06 16:21:47 +02003746 switch (b43_qos_shm_offsets[i]) {
3747 case B43_QOS_VOICE:
3748 params->p.txop = 0;
3749 params->p.aifs = 2;
3750 params->p.cw_min = 0x0001;
3751 params->p.cw_max = 0x0001;
3752 break;
3753 case B43_QOS_VIDEO:
3754 params->p.txop = 0;
3755 params->p.aifs = 2;
3756 params->p.cw_min = 0x0001;
3757 params->p.cw_max = 0x0001;
3758 break;
3759 case B43_QOS_BESTEFFORT:
3760 params->p.txop = 0;
3761 params->p.aifs = 3;
3762 params->p.cw_min = 0x0001;
3763 params->p.cw_max = 0x03FF;
3764 break;
3765 case B43_QOS_BACKGROUND:
3766 params->p.txop = 0;
3767 params->p.aifs = 7;
3768 params->p.cw_min = 0x0001;
3769 params->p.cw_max = 0x03FF;
3770 break;
3771 default:
3772 B43_WARN_ON(1);
3773 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003774 }
3775}
3776
3777/* Initialize the core's QOS capabilities */
3778static void b43_qos_init(struct b43_wldev *dev)
3779{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003780 if (!dev->qos_enabled) {
3781 /* Disable QOS support. */
3782 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3783 b43_write16(dev, B43_MMIO_IFSCTL,
3784 b43_read16(dev, B43_MMIO_IFSCTL)
3785 & ~B43_MMIO_IFSCTL_USE_EDCF);
3786 b43dbg(dev->wl, "QoS disabled\n");
3787 return;
3788 }
3789
Michael Buesche6f5b932008-03-05 21:18:49 +01003790 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003791 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003792
3793 /* Enable QOS support. */
3794 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3795 b43_write16(dev, B43_MMIO_IFSCTL,
3796 b43_read16(dev, B43_MMIO_IFSCTL)
3797 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003798 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003799}
3800
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003801static int b43_op_conf_tx(struct ieee80211_hw *hw,
3802 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003803 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003804{
Michael Buesche6f5b932008-03-05 21:18:49 +01003805 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003806 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003807 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003808 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003809
3810 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3811 /* Queue not available or don't support setting
3812 * params on this queue. Return success to not
3813 * confuse mac80211. */
3814 return 0;
3815 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003816 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3817 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003818
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003819 mutex_lock(&wl->mutex);
3820 dev = wl->current_dev;
3821 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3822 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003823
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003824 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3825 b43_mac_suspend(dev);
3826 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3827 b43_qos_shm_offsets[queue]);
3828 b43_mac_enable(dev);
3829 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003830
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003831out_unlock:
3832 mutex_unlock(&wl->mutex);
3833
3834 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003835}
3836
Michael Buesch40faacc2007-10-28 16:29:32 +01003837static int b43_op_get_stats(struct ieee80211_hw *hw,
3838 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003839{
3840 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003841
Michael Buesch36dbd952009-09-04 22:51:29 +02003842 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003843 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003844 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003845
3846 return 0;
3847}
3848
Eliad Peller37a41b42011-09-21 14:06:11 +03003849static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003850{
3851 struct b43_wl *wl = hw_to_b43_wl(hw);
3852 struct b43_wldev *dev;
3853 u64 tsf;
3854
3855 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003856 dev = wl->current_dev;
3857
3858 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3859 b43_tsf_read(dev, &tsf);
3860 else
3861 tsf = 0;
3862
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003863 mutex_unlock(&wl->mutex);
3864
3865 return tsf;
3866}
3867
Eliad Peller37a41b42011-09-21 14:06:11 +03003868static void b43_op_set_tsf(struct ieee80211_hw *hw,
3869 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003870{
3871 struct b43_wl *wl = hw_to_b43_wl(hw);
3872 struct b43_wldev *dev;
3873
3874 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003875 dev = wl->current_dev;
3876
3877 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3878 b43_tsf_write(dev, tsf);
3879
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003880 mutex_unlock(&wl->mutex);
3881}
3882
John Daiker99da1852009-02-24 02:16:42 -08003883static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003884{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003885 switch (band) {
3886 case IEEE80211_BAND_5GHZ:
3887 return "5";
3888 case IEEE80211_BAND_2GHZ:
3889 return "2.4";
3890 default:
3891 break;
3892 }
3893 B43_WARN_ON(1);
3894 return "";
3895}
3896
3897/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003898static int b43_switch_band(struct b43_wldev *dev,
3899 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003900{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003901 struct b43_phy *phy = &dev->phy;
3902 bool gmode;
3903 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003904
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003905 switch (chan->band) {
3906 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003907 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003908 break;
3909 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003910 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003911 break;
3912 default:
3913 B43_WARN_ON(1);
3914 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003915 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003916
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003917 if (!((gmode && phy->supports_2ghz) ||
3918 (!gmode && phy->supports_5ghz))) {
3919 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003920 band_to_string(chan->band));
3921 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003922 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003923
3924 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003925 /* This device is already running. */
3926 return 0;
3927 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003928
3929 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003930 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003931
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003932 /* Some new devices don't need disabling radio for band switching */
3933 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3934 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003935
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003936 phy->gmode = gmode;
3937 b43_phy_put_into_reset(dev);
3938 switch (dev->dev->bus_type) {
3939#ifdef CONFIG_B43_BCMA
3940 case B43_BUS_BCMA:
3941 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3942 if (gmode)
3943 tmp |= B43_BCMA_IOCTL_GMODE;
3944 else
3945 tmp &= ~B43_BCMA_IOCTL_GMODE;
3946 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3947 break;
3948#endif
3949#ifdef CONFIG_B43_SSB
3950 case B43_BUS_SSB:
3951 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3952 if (gmode)
3953 tmp |= B43_TMSLOW_GMODE;
3954 else
3955 tmp &= ~B43_TMSLOW_GMODE;
3956 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3957 break;
3958#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003959 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003960 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003961
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003962 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003963
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003964 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003965
3966 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003967}
3968
Hauke Mehrtens42148522014-09-14 23:09:12 +02003969static void b43_set_beacon_listen_interval(struct b43_wldev *dev, u16 interval)
3970{
3971 interval = min_t(u16, interval, (u16)0xFF);
3972 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BCN_LI, interval);
3973}
3974
Johannes Berg9124b072008-10-14 19:17:54 +02003975/* Write the short and long frame retry limit values. */
3976static void b43_set_retry_limits(struct b43_wldev *dev,
3977 unsigned int short_retry,
3978 unsigned int long_retry)
3979{
3980 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3981 * the chip-internal counter. */
3982 short_retry = min(short_retry, (unsigned int)0xF);
3983 long_retry = min(long_retry, (unsigned int)0xF);
3984
3985 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3986 short_retry);
3987 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3988 long_retry);
3989}
3990
Johannes Berge8975582008-10-09 12:18:51 +02003991static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003992{
3993 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003994 struct b43_wldev *dev = wl->current_dev;
3995 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003996 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003997 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003998 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003999
Michael Buesche4d6b792007-09-18 15:39:42 -04004000 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02004001 b43_mac_suspend(dev);
4002
Hauke Mehrtens42148522014-09-14 23:09:12 +02004003 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL)
4004 b43_set_beacon_listen_interval(dev, conf->listen_interval);
4005
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02004006 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02004007 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02004008 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06004009
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02004010 /* Switch the band (if necessary). */
4011 err = b43_switch_band(dev, conf->chandef.chan);
4012 if (err)
4013 goto out_mac_enable;
4014
4015 /* Switch to the requested channel.
4016 * The firmware takes care of races with the TX handler.
4017 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02004018 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02004019 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01004020
Johannes Berg9124b072008-10-14 19:17:54 +02004021 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
4022 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
4023 conf->long_frame_max_tx_count);
4024 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
4025 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01004026 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04004027
Johannes Berg0869aea02009-10-28 10:03:35 +01004028 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01004029
Michael Buesche4d6b792007-09-18 15:39:42 -04004030 /* Adjust the desired TX power level. */
4031 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02004032 if (conf->power_level != phy->desired_txpower) {
4033 phy->desired_txpower = conf->power_level;
4034 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
4035 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04004036 }
4037 }
4038
4039 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02004040 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01004041 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02004042 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02004043 if (phy->ops->set_rx_antenna)
4044 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04004045
Larry Fingerfd4973c2009-06-20 12:58:11 -05004046 if (wl->radio_enabled != phy->radio_on) {
4047 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02004048 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02004049 b43info(dev->wl, "Radio turned on by software\n");
4050 if (!dev->radio_hw_enable) {
4051 b43info(dev->wl, "The hardware RF-kill button "
4052 "still turns the radio physically off. "
4053 "Press the button to turn it on.\n");
4054 }
4055 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02004056 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02004057 b43info(dev->wl, "Radio turned off by software\n");
4058 }
4059 }
4060
Michael Bueschd10d0e52008-12-18 22:13:39 +01004061out_mac_enable:
4062 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004063 mutex_unlock(&wl->mutex);
4064
4065 return err;
4066}
4067
Johannes Berg881d9482009-01-21 15:13:48 +01004068static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004069{
4070 struct ieee80211_supported_band *sband =
4071 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
4072 struct ieee80211_rate *rate;
4073 int i;
4074 u16 basic, direct, offset, basic_offset, rateptr;
4075
4076 for (i = 0; i < sband->n_bitrates; i++) {
4077 rate = &sband->bitrates[i];
4078
4079 if (b43_is_cck_rate(rate->hw_value)) {
4080 direct = B43_SHM_SH_CCKDIRECT;
4081 basic = B43_SHM_SH_CCKBASIC;
4082 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
4083 offset &= 0xF;
4084 } else {
4085 direct = B43_SHM_SH_OFDMDIRECT;
4086 basic = B43_SHM_SH_OFDMBASIC;
4087 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
4088 offset &= 0xF;
4089 }
4090
4091 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
4092
4093 if (b43_is_cck_rate(rate->hw_value)) {
4094 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
4095 basic_offset &= 0xF;
4096 } else {
4097 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
4098 basic_offset &= 0xF;
4099 }
4100
4101 /*
4102 * Get the pointer that we need to point to
4103 * from the direct map
4104 */
4105 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
4106 direct + 2 * basic_offset);
4107 /* and write it to the basic map */
4108 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
4109 rateptr);
4110 }
4111}
4112
4113static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
4114 struct ieee80211_vif *vif,
4115 struct ieee80211_bss_conf *conf,
4116 u32 changed)
4117{
4118 struct b43_wl *wl = hw_to_b43_wl(hw);
4119 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004120
4121 mutex_lock(&wl->mutex);
4122
4123 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01004124 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004125 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004126
4127 B43_WARN_ON(wl->vif != vif);
4128
4129 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004130 if (conf->bssid)
4131 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
4132 else
Joe Perches93803b32015-03-02 19:54:49 -08004133 eth_zero_addr(wl->bssid);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004134 }
4135
Johannes Berg3f0d8432009-05-18 10:53:18 +02004136 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
4137 if (changed & BSS_CHANGED_BEACON &&
4138 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4139 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
4140 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
4141 b43_update_templates(wl);
4142
4143 if (changed & BSS_CHANGED_BSSID)
4144 b43_write_mac_bssid_templates(dev);
4145 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02004146
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004147 b43_mac_suspend(dev);
4148
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004149 /* Update templates for AP/mesh mode. */
4150 if (changed & BSS_CHANGED_BEACON_INT &&
4151 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4152 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06004153 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
4154 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004155 b43_set_beacon_int(dev, conf->beacon_int);
4156
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004157 if (changed & BSS_CHANGED_BASIC_RATES)
4158 b43_update_basic_rates(dev, conf->basic_rates);
4159
4160 if (changed & BSS_CHANGED_ERP_SLOT) {
4161 if (conf->use_short_slot)
4162 b43_short_slot_timing_enable(dev);
4163 else
4164 b43_short_slot_timing_disable(dev);
4165 }
4166
4167 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01004168out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004169 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004170}
4171
Michael Buesch40faacc2007-10-28 16:29:32 +01004172static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004173 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4174 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004175{
4176 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004177 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004178 u8 algorithm;
4179 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004180 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004181 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004182
4183 if (modparam_nohwcrypt)
4184 return -ENOSPC; /* User disabled HW-crypto */
4185
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004186 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4187 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4188 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4189 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4190 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4191 /*
4192 * For now, disable hw crypto for the RSN IBSS group keys. This
4193 * could be optimized in the future, but until that gets
4194 * implemented, use of software crypto for group addressed
4195 * frames is a acceptable to allow RSN IBSS to be used.
4196 */
4197 return -EOPNOTSUPP;
4198 }
4199
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004200 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004201
4202 dev = wl->current_dev;
4203 err = -ENODEV;
4204 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4205 goto out_unlock;
4206
Michael Buesch403a3a12009-06-08 21:04:57 +02004207 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004208 /* We don't have firmware for the crypto engine.
4209 * Must use software-crypto. */
4210 err = -EOPNOTSUPP;
4211 goto out_unlock;
4212 }
4213
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004214 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004215 switch (key->cipher) {
4216 case WLAN_CIPHER_SUITE_WEP40:
4217 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004218 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004219 case WLAN_CIPHER_SUITE_WEP104:
4220 algorithm = B43_SEC_ALGO_WEP104;
4221 break;
4222 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004223 algorithm = B43_SEC_ALGO_TKIP;
4224 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004225 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004226 algorithm = B43_SEC_ALGO_AES;
4227 break;
4228 default:
4229 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004230 goto out_unlock;
4231 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004232 index = (u8) (key->keyidx);
4233 if (index > 3)
4234 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004235
4236 switch (cmd) {
4237 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004238 if (algorithm == B43_SEC_ALGO_TKIP &&
4239 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4240 !modparam_hwtkip)) {
4241 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004242 err = -EOPNOTSUPP;
4243 goto out_unlock;
4244 }
4245
Michael Buesche808e582008-12-19 21:30:52 +01004246 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004247 if (WARN_ON(!sta)) {
4248 err = -EOPNOTSUPP;
4249 goto out_unlock;
4250 }
Michael Buesche808e582008-12-19 21:30:52 +01004251 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004252 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004253 key->key, key->keylen,
4254 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004255 } else {
4256 /* Group key */
4257 err = b43_key_write(dev, index, algorithm,
4258 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004259 }
4260 if (err)
4261 goto out_unlock;
4262
4263 if (algorithm == B43_SEC_ALGO_WEP40 ||
4264 algorithm == B43_SEC_ALGO_WEP104) {
4265 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4266 } else {
4267 b43_hf_write(dev,
4268 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4269 }
4270 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004271 if (algorithm == B43_SEC_ALGO_TKIP)
4272 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004273 break;
4274 case DISABLE_KEY: {
4275 err = b43_key_clear(dev, key->hw_key_idx);
4276 if (err)
4277 goto out_unlock;
4278 break;
4279 }
4280 default:
4281 B43_WARN_ON(1);
4282 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004283
Michael Buesche4d6b792007-09-18 15:39:42 -04004284out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004285 if (!err) {
4286 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004287 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004288 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004289 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004290 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004291 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004292 mutex_unlock(&wl->mutex);
4293
Michael Buesche4d6b792007-09-18 15:39:42 -04004294 return err;
4295}
4296
Michael Buesch40faacc2007-10-28 16:29:32 +01004297static void b43_op_configure_filter(struct ieee80211_hw *hw,
4298 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004299 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004300{
4301 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004302 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004303
Michael Buesch36dbd952009-09-04 22:51:29 +02004304 mutex_lock(&wl->mutex);
4305 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004306 if (!dev) {
4307 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004308 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004309 }
Johannes Berg4150c572007-09-17 01:29:23 -04004310
Johannes Bergdf140462015-04-22 14:40:58 +02004311 *fflags &= FIF_ALLMULTI |
Johannes Berg4150c572007-09-17 01:29:23 -04004312 FIF_FCSFAIL |
4313 FIF_PLCPFAIL |
4314 FIF_CONTROL |
4315 FIF_OTHER_BSS |
4316 FIF_BCN_PRBRESP_PROMISC;
4317
Johannes Bergdf140462015-04-22 14:40:58 +02004318 changed &= FIF_ALLMULTI |
Johannes Berg4150c572007-09-17 01:29:23 -04004319 FIF_FCSFAIL |
4320 FIF_PLCPFAIL |
4321 FIF_CONTROL |
4322 FIF_OTHER_BSS |
4323 FIF_BCN_PRBRESP_PROMISC;
4324
4325 wl->filter_flags = *fflags;
4326
4327 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4328 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004329
4330out_unlock:
4331 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004332}
4333
Michael Buesch36dbd952009-09-04 22:51:29 +02004334/* Locking: wl->mutex
4335 * Returns the current dev. This might be different from the passed in dev,
4336 * because the core might be gone away while we unlocked the mutex. */
4337static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004338{
Larry Finger9a53bf52011-08-27 15:53:42 -05004339 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004340 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004341 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004342 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004343
Larry Finger9a53bf52011-08-27 15:53:42 -05004344 if (!dev)
4345 return NULL;
4346 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004347redo:
4348 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4349 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004350
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004351 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004352 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004353 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004354 cancel_work_sync(&wl->tx_work);
Sabrina Dubroca6b47aac2015-01-19 15:34:32 +01004355 b43_leds_stop(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004356 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004357 dev = wl->current_dev;
4358 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4359 /* Whoops, aliens ate up the device while we were unlocked. */
4360 return dev;
4361 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004362
Michael Buesch36dbd952009-09-04 22:51:29 +02004363 /* Disable interrupts on the device. */
4364 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004365 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004366 /* wl->mutex is locked. That is enough. */
4367 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4368 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4369 } else {
4370 spin_lock_irq(&wl->hardirq_lock);
4371 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4372 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4373 spin_unlock_irq(&wl->hardirq_lock);
4374 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004375 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004376 orig_dev = dev;
4377 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004378 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004379 b43_sdio_free_irq(dev);
4380 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004381 synchronize_irq(dev->dev->irq);
4382 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004383 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004384 mutex_lock(&wl->mutex);
4385 dev = wl->current_dev;
4386 if (!dev)
4387 return dev;
4388 if (dev != orig_dev) {
4389 if (b43_status(dev) >= B43_STAT_STARTED)
4390 goto redo;
4391 return dev;
4392 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004393 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4394 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004395
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004396 /* Drain all TX queues. */
4397 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004398 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4399 struct sk_buff *skb;
4400
4401 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4402 ieee80211_free_txskb(wl->hw, skb);
4403 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004404 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004405
Michael Buesche4d6b792007-09-18 15:39:42 -04004406 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004407 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004408 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004409
4410 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004411}
4412
4413/* Locking: wl->mutex */
4414static int b43_wireless_core_start(struct b43_wldev *dev)
4415{
4416 int err;
4417
4418 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4419
4420 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004421 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004422 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4423 if (err) {
4424 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4425 goto out;
4426 }
4427 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004428 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004429 b43_interrupt_thread_handler,
4430 IRQF_SHARED, KBUILD_MODNAME, dev);
4431 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004432 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004433 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004434 goto out;
4435 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004436 }
4437
4438 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004439 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004440 b43_set_status(dev, B43_STAT_STARTED);
4441
4442 /* Start data flow (TX/RX). */
4443 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004444 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004445
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004446 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004447 b43_periodic_tasks_setup(dev);
4448
Michael Buescha78b3bb2009-09-11 21:44:05 +02004449 b43_leds_init(dev);
4450
Michael Buesche4d6b792007-09-18 15:39:42 -04004451 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004452out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004453 return err;
4454}
4455
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004456static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4457{
4458 switch (phy_type) {
4459 case B43_PHYTYPE_A:
4460 return "A";
4461 case B43_PHYTYPE_B:
4462 return "B";
4463 case B43_PHYTYPE_G:
4464 return "G";
4465 case B43_PHYTYPE_N:
4466 return "N";
4467 case B43_PHYTYPE_LP:
4468 return "LP";
4469 case B43_PHYTYPE_SSLPN:
4470 return "SSLPN";
4471 case B43_PHYTYPE_HT:
4472 return "HT";
4473 case B43_PHYTYPE_LCN:
4474 return "LCN";
4475 case B43_PHYTYPE_LCNXN:
4476 return "LCNXN";
4477 case B43_PHYTYPE_LCN40:
4478 return "LCN40";
4479 case B43_PHYTYPE_AC:
4480 return "AC";
4481 }
4482 return "UNKNOWN";
4483}
4484
Michael Buesche4d6b792007-09-18 15:39:42 -04004485/* Get PHY and RADIO versioning numbers */
4486static int b43_phy_versioning(struct b43_wldev *dev)
4487{
4488 struct b43_phy *phy = &dev->phy;
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004489 const u8 core_rev = dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004490 u32 tmp;
4491 u8 analog_type;
4492 u8 phy_type;
4493 u8 phy_rev;
4494 u16 radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004495 u16 radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004496 u16 radio_rev;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004497 u8 radio_ver;
Michael Buesche4d6b792007-09-18 15:39:42 -04004498 int unsupported = 0;
4499
4500 /* Get PHY versioning */
4501 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4502 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4503 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4504 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004505
4506 /* LCNXN is continuation of N which run out of revisions */
4507 if (phy_type == B43_PHYTYPE_LCNXN) {
4508 phy_type = B43_PHYTYPE_N;
4509 phy_rev += 16;
4510 }
4511
Michael Buesche4d6b792007-09-18 15:39:42 -04004512 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004513#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004514 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004515 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004516 unsupported = 1;
4517 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004518#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004519#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004520 case B43_PHYTYPE_N:
Rafał Miłecki40c68f22014-07-08 15:11:07 +02004521 if (phy_rev >= 19)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004522 unsupported = 1;
4523 break;
4524#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004525#ifdef CONFIG_B43_PHY_LP
4526 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004527 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004528 unsupported = 1;
4529 break;
4530#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004531#ifdef CONFIG_B43_PHY_HT
4532 case B43_PHYTYPE_HT:
4533 if (phy_rev > 1)
4534 unsupported = 1;
4535 break;
4536#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004537#ifdef CONFIG_B43_PHY_LCN
4538 case B43_PHYTYPE_LCN:
4539 if (phy_rev > 1)
4540 unsupported = 1;
4541 break;
4542#endif
Rafał Miłecki3f7bb3f2015-01-25 18:01:37 +01004543#ifdef CONFIG_B43_PHY_AC
4544 case B43_PHYTYPE_AC:
4545 if (phy_rev > 1)
4546 unsupported = 1;
4547 break;
4548#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004549 default:
4550 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004551 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004552 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004553 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4554 analog_type, phy_type, b43_phy_name(dev, phy_type),
4555 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004556 return -EOPNOTSUPP;
4557 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004558 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4559 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004560
4561 /* Get RADIO versioning */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004562 if (core_rev == 40 || core_rev == 42) {
4563 radio_manuf = 0x17F;
4564
Rafał Miłecki25c15562014-08-07 07:45:37 +02004565 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, 0);
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004566 radio_rev = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4567
Rafał Miłecki25c15562014-08-07 07:45:37 +02004568 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, 1);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004569 radio_id = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4570
4571 radio_ver = 0; /* Is there version somewhere? */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004572 } else if (core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004573 u16 radio24[3];
4574
4575 for (tmp = 0; tmp < 3; tmp++) {
Rafał Miłecki25c15562014-08-07 07:45:37 +02004576 b43_write16f(dev, B43_MMIO_RADIO24_CONTROL, tmp);
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004577 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4578 }
4579
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004580 radio_manuf = 0x17F;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004581 radio_id = (radio24[2] << 8) | radio24[1];
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004582 radio_rev = (radio24[0] & 0xF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004583 radio_ver = (radio24[0] & 0xF0) >> 4;
Michael Buesche4d6b792007-09-18 15:39:42 -04004584 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004585 if (dev->dev->chip_id == 0x4317) {
4586 if (dev->dev->chip_rev == 0)
4587 tmp = 0x3205017F;
4588 else if (dev->dev->chip_rev == 1)
4589 tmp = 0x4205017F;
4590 else
4591 tmp = 0x5205017F;
4592 } else {
Rafał Miłecki25c15562014-08-07 07:45:37 +02004593 b43_write16f(dev, B43_MMIO_RADIO_CONTROL,
4594 B43_RADIOCTL_ID);
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004595 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
Rafał Miłecki25c15562014-08-07 07:45:37 +02004596 b43_write16f(dev, B43_MMIO_RADIO_CONTROL,
4597 B43_RADIOCTL_ID);
4598 tmp |= b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004599 }
4600 radio_manuf = (tmp & 0x00000FFF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004601 radio_id = (tmp & 0x0FFFF000) >> 12;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004602 radio_rev = (tmp & 0xF0000000) >> 28;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004603 radio_ver = 0; /* Probably not available on old hw */
Michael Buesche4d6b792007-09-18 15:39:42 -04004604 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004605
Michael Buesch96c755a2008-01-06 00:09:46 +01004606 if (radio_manuf != 0x17F /* Broadcom */)
4607 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004608 switch (phy_type) {
4609 case B43_PHYTYPE_A:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004610 if (radio_id != 0x2060)
Michael Buesche4d6b792007-09-18 15:39:42 -04004611 unsupported = 1;
4612 if (radio_rev != 1)
4613 unsupported = 1;
4614 if (radio_manuf != 0x17F)
4615 unsupported = 1;
4616 break;
4617 case B43_PHYTYPE_B:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004618 if ((radio_id & 0xFFF0) != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004619 unsupported = 1;
4620 break;
4621 case B43_PHYTYPE_G:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004622 if (radio_id != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004623 unsupported = 1;
4624 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004625 case B43_PHYTYPE_N:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004626 if (radio_id != 0x2055 && radio_id != 0x2056 &&
4627 radio_id != 0x2057)
Rafał Miłecki3695b932014-07-08 15:11:10 +02004628 unsupported = 1;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004629 if (radio_id == 0x2057 &&
Rafał Miłeckic11082f2014-07-19 12:52:47 +02004630 !(radio_rev == 9 || radio_rev == 14))
Michael Buesch96c755a2008-01-06 00:09:46 +01004631 unsupported = 1;
4632 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004633 case B43_PHYTYPE_LP:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004634 if (radio_id != 0x2062 && radio_id != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004635 unsupported = 1;
4636 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004637 case B43_PHYTYPE_HT:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004638 if (radio_id != 0x2059)
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004639 unsupported = 1;
4640 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004641 case B43_PHYTYPE_LCN:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004642 if (radio_id != 0x2064)
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004643 unsupported = 1;
4644 break;
Rafał Miłecki3f7bb3f2015-01-25 18:01:37 +01004645 case B43_PHYTYPE_AC:
4646 if (radio_id != 0x2069)
4647 unsupported = 1;
4648 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004649 default:
4650 B43_WARN_ON(1);
4651 }
4652 if (unsupported) {
Rafał Miłecki88d825b2014-07-02 19:07:43 +02004653 b43err(dev->wl,
Rafał Miłecki16e75452014-07-20 12:57:45 +02004654 "FOUND UNSUPPORTED RADIO (Manuf 0x%X, ID 0x%X, Revision %u, Version %u)\n",
4655 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004656 return -EOPNOTSUPP;
4657 }
Rafał Miłecki16e75452014-07-20 12:57:45 +02004658 b43info(dev->wl,
4659 "Found Radio: Manuf 0x%X, ID 0x%X, Revision %u, Version %u\n",
4660 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004661
Rafał Miłecki16e75452014-07-20 12:57:45 +02004662 /* FIXME: b43 treats "id" as "ver" and ignores the real "ver" */
Michael Buesche4d6b792007-09-18 15:39:42 -04004663 phy->radio_manuf = radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004664 phy->radio_ver = radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004665 phy->radio_rev = radio_rev;
4666
4667 phy->analog = analog_type;
4668 phy->type = phy_type;
4669 phy->rev = phy_rev;
4670
4671 return 0;
4672}
4673
4674static void setup_struct_phy_for_init(struct b43_wldev *dev,
4675 struct b43_phy *phy)
4676{
Michael Buesche4d6b792007-09-18 15:39:42 -04004677 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004678 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004679 /* PHY TX errors counter. */
4680 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004681
4682#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004683 phy->phy_locked = false;
4684 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004685#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004686}
4687
4688static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4689{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004690 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004691
Michael Buesch6a724d62007-09-20 22:12:58 +02004692 /* Assume the radio is enabled. If it's not enabled, the state will
4693 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004694 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004695
4696 /* Stats */
4697 memset(&dev->stats, 0, sizeof(dev->stats));
4698
4699 setup_struct_phy_for_init(dev, &dev->phy);
4700
4701 /* IRQ related flags */
4702 dev->irq_reason = 0;
4703 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004704 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004705 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004706 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004707
4708 dev->mac_suspended = 1;
4709
4710 /* Noise calculation context */
4711 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4712}
4713
4714static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4715{
Rafał Miłecki05814832011-05-18 02:06:39 +02004716 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004717 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004718
Michael Buesch1855ba72008-04-18 20:51:41 +02004719 if (!modparam_btcoex)
4720 return;
Larry Finger95de2842007-11-09 16:57:18 -06004721 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004722 return;
4723 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4724 return;
4725
4726 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004727 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004728 hf |= B43_HF_BTCOEXALT;
4729 else
4730 hf |= B43_HF_BTCOEX;
4731 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004732}
4733
4734static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004735{
4736 if (!modparam_btcoex)
4737 return;
4738 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004739}
4740
4741static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4742{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004743 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004744 u32 tmp;
4745
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004746#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004747 if (dev->dev->bus_type != B43_BUS_SSB)
4748 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004749#else
4750 return;
4751#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004752
4753 bus = dev->dev->sdev->bus;
4754
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004755 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4756 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004757 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004758 tmp &= ~SSB_IMCFGLO_REQTO;
4759 tmp &= ~SSB_IMCFGLO_SERTO;
4760 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004761 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004762 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004763 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004764}
4765
Michael Bueschd59f7202008-04-03 18:56:19 +02004766static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4767{
4768 u16 pu_delay;
4769
4770 /* The time value is in microseconds. */
4771 if (dev->phy.type == B43_PHYTYPE_A)
4772 pu_delay = 3700;
4773 else
4774 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004775 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004776 pu_delay = 500;
4777 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4778 pu_delay = max(pu_delay, (u16)2400);
4779
4780 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4781}
4782
4783/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4784static void b43_set_pretbtt(struct b43_wldev *dev)
4785{
4786 u16 pretbtt;
4787
4788 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004789 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004790 pretbtt = 2;
4791 } else {
4792 if (dev->phy.type == B43_PHYTYPE_A)
4793 pretbtt = 120;
4794 else
4795 pretbtt = 250;
4796 }
4797 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4798 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4799}
4800
Michael Buesche4d6b792007-09-18 15:39:42 -04004801/* Shutdown a wireless core */
4802/* Locking: wl->mutex */
4803static void b43_wireless_core_exit(struct b43_wldev *dev)
4804{
Michael Buesch36dbd952009-09-04 22:51:29 +02004805 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4806 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004807 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004808
Michael Buesche4d6b792007-09-18 15:39:42 -04004809 b43_set_status(dev, B43_STAT_UNINIT);
4810
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004811 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004812 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4813 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004814
Hauke Mehrtens50023002013-08-24 00:32:34 +02004815 switch (dev->dev->bus_type) {
4816#ifdef CONFIG_B43_BCMA
4817 case B43_BUS_BCMA:
Rafał Miłecki41867212015-02-08 17:11:47 +01004818 bcma_host_pci_down(dev->dev->bdev->bus);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004819 break;
4820#endif
4821#ifdef CONFIG_B43_SSB
4822 case B43_BUS_SSB:
4823 /* TODO */
4824 break;
4825#endif
4826 }
4827
Michael Buesche4d6b792007-09-18 15:39:42 -04004828 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004829 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004830 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004831 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004832 if (dev->wl->current_beacon) {
4833 dev_kfree_skb_any(dev->wl->current_beacon);
4834 dev->wl->current_beacon = NULL;
4835 }
4836
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004837 b43_device_disable(dev, 0);
4838 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004839}
4840
4841/* Initialize a wireless core */
4842static int b43_wireless_core_init(struct b43_wldev *dev)
4843{
Rafał Miłecki05814832011-05-18 02:06:39 +02004844 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004845 struct b43_phy *phy = &dev->phy;
4846 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004847 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004848
4849 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4850
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004851 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004852 if (err)
4853 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004854 if (!b43_device_is_enabled(dev))
4855 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004856
Michael Bueschfb111372008-09-02 13:00:34 +02004857 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004858 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004859 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004860
4861 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004862 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004863#ifdef CONFIG_B43_BCMA
4864 case B43_BUS_BCMA:
Rafał Miłecki702131e2015-03-05 18:25:10 +01004865 bcma_host_pci_irq_ctl(dev->dev->bdev->bus,
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004866 dev->dev->bdev, true);
Rafał Miłecki41867212015-02-08 17:11:47 +01004867 bcma_host_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004868 break;
4869#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004870#ifdef CONFIG_B43_SSB
4871 case B43_BUS_SSB:
4872 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4873 dev->dev->sdev);
4874 break;
4875#endif
4876 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004877
4878 b43_imcfglo_timeouts_workaround(dev);
4879 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004880 if (phy->ops->prepare_hardware) {
4881 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004882 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004883 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004884 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004885 err = b43_chip_init(dev);
4886 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004887 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004888 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004889 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004890 hf = b43_hf_read(dev);
4891 if (phy->type == B43_PHYTYPE_G) {
4892 hf |= B43_HF_SYMW;
4893 if (phy->rev == 1)
4894 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004895 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004896 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004897 }
4898 if (phy->radio_ver == 0x2050) {
4899 if (phy->radio_rev == 6)
4900 hf |= B43_HF_4318TSSI;
4901 if (phy->radio_rev < 6)
4902 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004903 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004904 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4905 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004906#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004907 if (dev->dev->bus_type == B43_BUS_SSB &&
4908 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4909 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004910 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004911#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004912 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004913 b43_hf_write(dev, hf);
4914
Hauke Mehrtens5eb36452014-09-14 23:09:08 +02004915 /* tell the ucode MAC capabilities */
4916 if (dev->dev->core_rev >= 13) {
4917 u32 mac_hw_cap = b43_read32(dev, B43_MMIO_MAC_HW_CAP);
4918
4919 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_MACHW_L,
4920 mac_hw_cap & 0xffff);
4921 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_MACHW_H,
4922 (mac_hw_cap >> 16) & 0xffff);
4923 }
4924
Michael Buesch74cfdba2007-10-28 16:19:44 +01004925 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4926 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004927 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4928 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4929
4930 /* Disable sending probe responses from firmware.
4931 * Setting the MaxTime to one usec will always trigger
4932 * a timeout, so we never send any probe resp.
4933 * A timeout of zero is infinite. */
4934 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4935
4936 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004937 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004938
4939 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004940 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004941 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004942 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004943 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004944 /* Maximum Contention Window */
4945 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4946
Hauke Mehrtens261b7582014-09-14 23:09:09 +02004947 /* write phytype and phyvers */
4948 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PHYTYPE, phy->type);
4949 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PHYVER, phy->rev);
4950
Rafał Miłecki505fb012011-05-19 15:11:27 +02004951 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004952 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004953 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004954 err = b43_pio_init(dev);
4955 } else if (dev->use_pio) {
4956 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4957 "This should not be needed and will result in lower "
4958 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004959 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004960 err = b43_pio_init(dev);
4961 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004962 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004963 err = b43_dma_init(dev);
4964 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004965 if (err)
4966 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004967 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004968 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004969 b43_bluetooth_coext_enable(dev);
4970
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004971 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004972 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004973 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004974
Michael Buesch5ab95492009-09-10 20:31:46 +02004975 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004976
4977 b43_set_status(dev, B43_STAT_INITIALIZED);
4978
Larry Finger1a8d12272007-12-14 13:59:11 +01004979out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004980 return err;
4981
Michael Bueschef1a6282008-08-27 18:53:02 +02004982err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004983 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004984err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004985 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004986 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4987 return err;
4988}
4989
Michael Buesch40faacc2007-10-28 16:29:32 +01004990static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004991 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004992{
4993 struct b43_wl *wl = hw_to_b43_wl(hw);
4994 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004995 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004996
4997 /* TODO: allow WDS/AP devices to coexist */
4998
Johannes Berg1ed32e42009-12-23 13:15:45 +01004999 if (vif->type != NL80211_IFTYPE_AP &&
5000 vif->type != NL80211_IFTYPE_MESH_POINT &&
5001 vif->type != NL80211_IFTYPE_STATION &&
5002 vif->type != NL80211_IFTYPE_WDS &&
5003 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04005004 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04005005
5006 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04005007 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04005008 goto out_mutex_unlock;
5009
Johannes Berg1ed32e42009-12-23 13:15:45 +01005010 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04005011
5012 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005013 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01005014 wl->vif = vif;
5015 wl->if_type = vif->type;
5016 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04005017
Michael Buesche4d6b792007-09-18 15:39:42 -04005018 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02005019 b43_set_pretbtt(dev);
5020 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04005021 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005022
5023 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04005024 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04005025 mutex_unlock(&wl->mutex);
5026
Felix Fietkau2a190322011-08-10 13:50:30 -06005027 if (err == 0)
5028 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
5029
Michael Buesche4d6b792007-09-18 15:39:42 -04005030 return err;
5031}
5032
Michael Buesch40faacc2007-10-28 16:29:32 +01005033static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01005034 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04005035{
5036 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04005037 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005038
Johannes Berg1ed32e42009-12-23 13:15:45 +01005039 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04005040
5041 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04005042
5043 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01005044 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01005045 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04005046
Rusty Russell3db1cd52011-12-19 13:56:45 +00005047 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04005048
Johannes Berg4150c572007-09-17 01:29:23 -04005049 b43_adjust_opmode(dev);
Joe Perches93803b32015-03-02 19:54:49 -08005050 eth_zero_addr(wl->mac_addr);
Johannes Berg4150c572007-09-17 01:29:23 -04005051 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04005052
5053 mutex_unlock(&wl->mutex);
5054}
5055
Michael Buesch40faacc2007-10-28 16:29:32 +01005056static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04005057{
5058 struct b43_wl *wl = hw_to_b43_wl(hw);
5059 struct b43_wldev *dev = wl->current_dev;
5060 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07005061 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04005062
Michael Buesch7be1bb62008-01-23 21:10:56 +01005063 /* Kill all old instance specific information to make sure
5064 * the card won't use it in the short timeframe between start
5065 * and mac80211 reconfiguring it. */
Joe Perches93803b32015-03-02 19:54:49 -08005066 eth_zero_addr(wl->bssid);
5067 eth_zero_addr(wl->mac_addr);
Michael Buesch7be1bb62008-01-23 21:10:56 +01005068 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005069 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01005070 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00005071 wl->beacon0_uploaded = false;
5072 wl->beacon1_uploaded = false;
5073 wl->beacon_templates_virgin = true;
5074 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01005075
Johannes Berg4150c572007-09-17 01:29:23 -04005076 mutex_lock(&wl->mutex);
5077
5078 if (b43_status(dev) < B43_STAT_INITIALIZED) {
5079 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05005080 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04005081 goto out_mutex_unlock;
5082 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04005083 }
5084
Johannes Berg4150c572007-09-17 01:29:23 -04005085 if (b43_status(dev) < B43_STAT_STARTED) {
5086 err = b43_wireless_core_start(dev);
5087 if (err) {
5088 if (did_init)
5089 b43_wireless_core_exit(dev);
5090 goto out_mutex_unlock;
5091 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005092 }
Johannes Berg4150c572007-09-17 01:29:23 -04005093
Johannes Bergf41f3f32009-06-07 12:30:34 -05005094 /* XXX: only do if device doesn't support rfkill irq */
5095 wiphy_rfkill_start_polling(hw->wiphy);
5096
Johannes Berg4150c572007-09-17 01:29:23 -04005097 out_mutex_unlock:
5098 mutex_unlock(&wl->mutex);
5099
Seth Forsheedbdedbd2012-04-25 17:28:00 -05005100 /*
5101 * Configuration may have been overwritten during initialization.
5102 * Reload the configuration, but only if initialization was
5103 * successful. Reloading the configuration after a failed init
5104 * may hang the system.
5105 */
5106 if (!err)
5107 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06005108
Johannes Berg4150c572007-09-17 01:29:23 -04005109 return err;
5110}
5111
Michael Buesch40faacc2007-10-28 16:29:32 +01005112static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04005113{
5114 struct b43_wl *wl = hw_to_b43_wl(hw);
5115 struct b43_wldev *dev = wl->current_dev;
5116
Michael Buescha82d9922008-04-04 21:40:06 +02005117 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d12272007-12-14 13:59:11 +01005118
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005119 if (!dev)
5120 goto out;
5121
Johannes Berg4150c572007-09-17 01:29:23 -04005122 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005123 if (b43_status(dev) >= B43_STAT_STARTED) {
5124 dev = b43_wireless_core_stop(dev);
5125 if (!dev)
5126 goto out_unlock;
5127 }
Johannes Berg4150c572007-09-17 01:29:23 -04005128 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00005129 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02005130
5131out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04005132 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005133out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02005134 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04005135}
5136
Johannes Berg17741cd2008-09-11 00:02:02 +02005137static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
5138 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01005139{
5140 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01005141
Johannes Berg9d139c82008-07-09 14:40:37 +02005142 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01005143
5144 return 0;
5145}
5146
Johannes Berg38968d02008-02-25 16:27:50 +01005147static void b43_op_sta_notify(struct ieee80211_hw *hw,
5148 struct ieee80211_vif *vif,
5149 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02005150 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01005151{
5152 struct b43_wl *wl = hw_to_b43_wl(hw);
5153
5154 B43_WARN_ON(!vif || wl->vif != vif);
5155}
5156
Johannes Berga344d672014-06-12 22:24:31 +02005157static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw,
5158 struct ieee80211_vif *vif,
5159 const u8 *mac_addr)
Michael Buesch25d3ef52009-02-20 15:39:21 +01005160{
5161 struct b43_wl *wl = hw_to_b43_wl(hw);
5162 struct b43_wldev *dev;
5163
5164 mutex_lock(&wl->mutex);
5165 dev = wl->current_dev;
5166 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5167 /* Disable CFP update during scan on other channels. */
5168 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
5169 }
5170 mutex_unlock(&wl->mutex);
5171}
5172
Johannes Berga344d672014-06-12 22:24:31 +02005173static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw,
5174 struct ieee80211_vif *vif)
Michael Buesch25d3ef52009-02-20 15:39:21 +01005175{
5176 struct b43_wl *wl = hw_to_b43_wl(hw);
5177 struct b43_wldev *dev;
5178
5179 mutex_lock(&wl->mutex);
5180 dev = wl->current_dev;
5181 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5182 /* Re-enable CFP update. */
5183 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
5184 }
5185 mutex_unlock(&wl->mutex);
5186}
5187
John W. Linville354b4f02010-04-29 15:56:06 -04005188static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
5189 struct survey_info *survey)
5190{
5191 struct b43_wl *wl = hw_to_b43_wl(hw);
5192 struct b43_wldev *dev = wl->current_dev;
5193 struct ieee80211_conf *conf = &hw->conf;
5194
5195 if (idx != 0)
5196 return -ENOENT;
5197
Karl Beldan675a0b02013-03-25 16:26:57 +01005198 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04005199 survey->filled = SURVEY_INFO_NOISE_DBM;
5200 survey->noise = dev->stats.link_noise;
5201
5202 return 0;
5203}
5204
Michael Buesche4d6b792007-09-18 15:39:42 -04005205static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01005206 .tx = b43_op_tx,
5207 .conf_tx = b43_op_conf_tx,
5208 .add_interface = b43_op_add_interface,
5209 .remove_interface = b43_op_remove_interface,
5210 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005211 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005212 .configure_filter = b43_op_configure_filter,
5213 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005214 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005215 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005216 .get_tsf = b43_op_get_tsf,
5217 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005218 .start = b43_op_start,
5219 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005220 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005221 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005222 .sw_scan_start = b43_op_sw_scan_start_notifier,
5223 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005224 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005225 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005226};
5227
5228/* Hard-reset the chip. Do not call this directly.
5229 * Use b43_controller_restart()
5230 */
5231static void b43_chip_reset(struct work_struct *work)
5232{
5233 struct b43_wldev *dev =
5234 container_of(work, struct b43_wldev, restart_work);
5235 struct b43_wl *wl = dev->wl;
5236 int err = 0;
5237 int prev_status;
5238
5239 mutex_lock(&wl->mutex);
5240
5241 prev_status = b43_status(dev);
5242 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005243 if (prev_status >= B43_STAT_STARTED) {
5244 dev = b43_wireless_core_stop(dev);
5245 if (!dev) {
5246 err = -ENODEV;
5247 goto out;
5248 }
5249 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005250 if (prev_status >= B43_STAT_INITIALIZED)
5251 b43_wireless_core_exit(dev);
5252
5253 /* ...and up again. */
5254 if (prev_status >= B43_STAT_INITIALIZED) {
5255 err = b43_wireless_core_init(dev);
5256 if (err)
5257 goto out;
5258 }
5259 if (prev_status >= B43_STAT_STARTED) {
5260 err = b43_wireless_core_start(dev);
5261 if (err) {
5262 b43_wireless_core_exit(dev);
5263 goto out;
5264 }
5265 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005266out:
5267 if (err)
5268 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005269 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005270
5271 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005272 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005273 return;
5274 }
5275
5276 /* reload configuration */
5277 b43_op_config(wl->hw, ~0);
5278 if (wl->vif)
5279 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5280
5281 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005282}
5283
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005284static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005285 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005286{
5287 struct ieee80211_hw *hw = dev->wl->hw;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005288 struct b43_phy *phy = &dev->phy;
5289 bool limited_2g;
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005290 bool limited_5g;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005291
5292 /* We don't support all 2 GHz channels on some devices */
Rafał Miłeckic11082f2014-07-19 12:52:47 +02005293 limited_2g = phy->radio_ver == 0x2057 &&
5294 (phy->radio_rev == 9 || phy->radio_rev == 14);
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005295 limited_5g = phy->radio_ver == 0x2057 &&
5296 phy->radio_rev == 9;
Michael Buesche4d6b792007-09-18 15:39:42 -04005297
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005298 if (have_2ghz_phy)
Rafał Miłecki3695b932014-07-08 15:11:10 +02005299 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = limited_2g ?
5300 &b43_band_2ghz_limited : &b43_band_2GHz;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005301 if (dev->phy.type == B43_PHYTYPE_N) {
5302 if (have_5ghz_phy)
Rafał Miłeckib453fda62014-07-23 18:54:49 +02005303 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = limited_5g ?
5304 &b43_band_5GHz_nphy_limited :
5305 &b43_band_5GHz_nphy;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005306 } else {
5307 if (have_5ghz_phy)
5308 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5309 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005310
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005311 dev->phy.supports_2ghz = have_2ghz_phy;
5312 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005313
5314 return 0;
5315}
5316
5317static void b43_wireless_core_detach(struct b43_wldev *dev)
5318{
5319 /* We release firmware that late to not be required to re-request
5320 * is all the time when we reinit the core. */
5321 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005322 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005323}
5324
Rafał Miłecki075ca602014-05-19 23:18:54 +02005325static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5326 bool *have_5ghz_phy)
5327{
5328 u16 dev_id = 0;
5329
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005330#ifdef CONFIG_B43_BCMA
5331 if (dev->dev->bus_type == B43_BUS_BCMA &&
5332 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5333 dev_id = dev->dev->bdev->bus->host_pci->device;
5334#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005335#ifdef CONFIG_B43_SSB
5336 if (dev->dev->bus_type == B43_BUS_SSB &&
5337 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5338 dev_id = dev->dev->sdev->bus->host_pci->device;
5339#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005340 /* Override with SPROM value if available */
5341 if (dev->dev->bus_sprom->dev_id)
5342 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005343
5344 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5345 switch (dev_id) {
5346 case 0x4324: /* BCM4306 */
5347 case 0x4312: /* BCM4311 */
5348 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005349 case 0x4328: /* BCM4321 */
5350 case 0x432b: /* BCM4322 */
5351 case 0x4350: /* BCM43222 */
5352 case 0x4353: /* BCM43224 */
5353 case 0x0576: /* BCM43224 */
5354 case 0x435f: /* BCM6362 */
5355 case 0x4331: /* BCM4331 */
5356 case 0x4359: /* BCM43228 */
5357 case 0x43a0: /* BCM4360 */
5358 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005359 /* Dual band devices */
5360 *have_2ghz_phy = true;
5361 *have_5ghz_phy = true;
5362 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005363 case 0x4321: /* BCM4306 */
5364 case 0x4313: /* BCM4311 */
5365 case 0x431a: /* BCM4318 */
5366 case 0x432a: /* BCM4321 */
5367 case 0x432d: /* BCM4322 */
5368 case 0x4352: /* BCM43222 */
Rafał Miłecki0ff66cf2015-03-02 17:18:55 +01005369 case 0x435a: /* BCM43228 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005370 case 0x4333: /* BCM4331 */
5371 case 0x43a2: /* BCM4360 */
5372 case 0x43b3: /* BCM4352 */
5373 /* 5 GHz only devices */
5374 *have_2ghz_phy = false;
5375 *have_5ghz_phy = true;
5376 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005377 }
5378
5379 /* As a fallback, try to guess using PHY type */
5380 switch (dev->phy.type) {
5381 case B43_PHYTYPE_A:
5382 *have_2ghz_phy = false;
5383 *have_5ghz_phy = true;
5384 return;
5385 case B43_PHYTYPE_G:
5386 case B43_PHYTYPE_N:
5387 case B43_PHYTYPE_LP:
5388 case B43_PHYTYPE_HT:
5389 case B43_PHYTYPE_LCN:
5390 *have_2ghz_phy = true;
5391 *have_5ghz_phy = false;
5392 return;
5393 }
5394
5395 B43_WARN_ON(1);
5396}
5397
Michael Buesche4d6b792007-09-18 15:39:42 -04005398static int b43_wireless_core_attach(struct b43_wldev *dev)
5399{
5400 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005401 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005402 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005403 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005404 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005405
5406 /* Do NOT do any device initialization here.
5407 * Do it in wireless_core_init() instead.
5408 * This function is for gathering basic information about the HW, only.
5409 * Also some structs may be set up here. But most likely you want to have
5410 * that in core_init(), too.
5411 */
5412
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005413 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005414 if (err) {
5415 b43err(wl, "Bus powerup failed\n");
5416 goto out;
5417 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005418
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005419 phy->do_full_init = true;
5420
Rafał Miłecki075ca602014-05-19 23:18:54 +02005421 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005422 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005423#ifdef CONFIG_B43_BCMA
5424 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005425 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5426 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5427 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005428 break;
5429#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005430#ifdef CONFIG_B43_SSB
5431 case B43_BUS_SSB:
5432 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005433 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5434 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5435 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005436 } else
5437 B43_WARN_ON(1);
5438 break;
5439#endif
5440 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005441
Michael Buesch96c755a2008-01-06 00:09:46 +01005442 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005443 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005444
Rafał Miłecki075ca602014-05-19 23:18:54 +02005445 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005446 err = b43_phy_versioning(dev);
5447 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005448 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005449
5450 /* Get real info about supported bands */
5451 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5452
5453 /* We don't support 5 GHz on some PHYs yet */
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005454 if (have_5ghz_phy) {
5455 switch (dev->phy.type) {
5456 case B43_PHYTYPE_A:
5457 case B43_PHYTYPE_G:
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005458 case B43_PHYTYPE_LP:
5459 case B43_PHYTYPE_HT:
5460 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
5461 have_5ghz_phy = false;
5462 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005463 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005464
5465 if (!have_2ghz_phy && !have_5ghz_phy) {
5466 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005467 err = -EOPNOTSUPP;
5468 goto err_powerdown;
5469 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005470
Michael Bueschfb111372008-09-02 13:00:34 +02005471 err = b43_phy_allocate(dev);
5472 if (err)
5473 goto err_powerdown;
5474
Michael Buesch96c755a2008-01-06 00:09:46 +01005475 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005476 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005477
5478 err = b43_validate_chipaccess(dev);
5479 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005480 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005481 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005482 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005483 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005484
5485 /* Now set some default "current_dev" */
5486 if (!wl->current_dev)
5487 wl->current_dev = dev;
5488 INIT_WORK(&dev->restart_work, b43_chip_reset);
5489
Michael Bueschcb24f572008-09-03 12:12:20 +02005490 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005491 b43_device_disable(dev, 0);
5492 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005493
5494out:
5495 return err;
5496
Michael Bueschfb111372008-09-02 13:00:34 +02005497err_phy_free:
5498 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005499err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005500 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005501 return err;
5502}
5503
Rafał Miłecki482f0532011-05-18 02:06:36 +02005504static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005505{
5506 struct b43_wldev *wldev;
5507 struct b43_wl *wl;
5508
Michael Buesch3bf0a322008-05-22 16:32:16 +02005509 /* Do not cancel ieee80211-workqueue based work here.
5510 * See comment in b43_remove(). */
5511
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005512 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005513 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005514 b43_debugfs_remove_device(wldev);
5515 b43_wireless_core_detach(wldev);
5516 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005517 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005518 kfree(wldev);
5519}
5520
Rafał Miłecki482f0532011-05-18 02:06:36 +02005521static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005522{
5523 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005524 int err = -ENOMEM;
5525
Michael Buesche4d6b792007-09-18 15:39:42 -04005526 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5527 if (!wldev)
5528 goto out;
5529
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005530 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005531 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005532 wldev->wl = wl;
5533 b43_set_status(wldev, B43_STAT_UNINIT);
5534 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005535 INIT_LIST_HEAD(&wldev->list);
5536
5537 err = b43_wireless_core_attach(wldev);
5538 if (err)
5539 goto err_kfree_wldev;
5540
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005541 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005542 b43_debugfs_add_device(wldev);
5543
5544 out:
5545 return err;
5546
5547 err_kfree_wldev:
5548 kfree(wldev);
5549 return err;
5550}
5551
Michael Buesch9fc38452008-04-19 16:53:00 +02005552#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5553 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5554 (pdev->device == _device) && \
5555 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5556 (pdev->subsystem_device == _subdevice) )
5557
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005558#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005559static void b43_sprom_fixup(struct ssb_bus *bus)
5560{
Michael Buesch1855ba72008-04-18 20:51:41 +02005561 struct pci_dev *pdev;
5562
Michael Buesche4d6b792007-09-18 15:39:42 -04005563 /* boardflags workarounds */
5564 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005565 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005566 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005567 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005568 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005569 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005570 if (bus->bustype == SSB_BUSTYPE_PCI) {
5571 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005572 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005573 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005574 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005575 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005576 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005577 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5578 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005579 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5580 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005581}
5582
Rafał Miłecki482f0532011-05-18 02:06:36 +02005583static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005584{
5585 struct ieee80211_hw *hw = wl->hw;
5586
Rafał Miłecki482f0532011-05-18 02:06:36 +02005587 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005588 ieee80211_free_hw(hw);
5589}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005590#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005591
Rafał Miłeckid1507052011-07-05 23:54:07 +02005592static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005593{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005594 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005595 struct ieee80211_hw *hw;
5596 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005597 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005598 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005599
5600 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5601 if (!hw) {
5602 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005603 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005604 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005605 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005606
5607 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005608 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005609 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005610
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005611 hw->wiphy->interface_modes =
5612 BIT(NL80211_IFTYPE_AP) |
5613 BIT(NL80211_IFTYPE_MESH_POINT) |
5614 BIT(NL80211_IFTYPE_STATION) |
5615 BIT(NL80211_IFTYPE_WDS) |
5616 BIT(NL80211_IFTYPE_ADHOC);
5617
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005618 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5619
Oleksij Rempele64add22012-06-05 20:39:32 +02005620 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005621 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005622 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005623 if (is_valid_ether_addr(sprom->et1mac))
5624 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005625 else
Larry Finger95de2842007-11-09 16:57:18 -06005626 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005627
Michael Buesch403a3a12009-06-08 21:04:57 +02005628 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005629 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005630 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005631 spin_lock_init(&wl->hardirq_lock);
Michael Büscha75d46a2015-01-26 18:26:17 +01005632 spin_lock_init(&wl->beacon_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005633 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005634 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005635 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005636
5637 /* Initialize queues and flags. */
5638 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5639 skb_queue_head_init(&wl->tx_queue[queue_num]);
5640 wl->tx_queue_stopped[queue_num] = 0;
5641 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005642
Rafał Miłecki2729df22011-07-18 22:45:58 +02005643 snprintf(chip_name, ARRAY_SIZE(chip_name),
5644 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5645 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5646 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005647 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005648}
5649
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005650#ifdef CONFIG_B43_BCMA
5651static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005652{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005653 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005654 struct b43_wl *wl;
5655 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005656
Rafał Miłecki89604002013-06-26 09:55:54 +02005657 if (!modparam_allhwsupport &&
5658 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5659 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5660 return -ENOTSUPP;
5661 }
5662
Rafał Miłecki397915c2011-07-06 19:03:46 +02005663 dev = b43_bus_dev_bcma_init(core);
5664 if (!dev)
5665 return -ENODEV;
5666
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005667 wl = b43_wireless_init(dev);
5668 if (IS_ERR(wl)) {
5669 err = PTR_ERR(wl);
5670 goto bcma_out;
5671 }
5672
5673 err = b43_one_core_attach(dev, wl);
5674 if (err)
5675 goto bcma_err_wireless_exit;
5676
Larry Finger6b6fa582012-03-08 22:27:46 -06005677 /* setup and start work to load firmware */
5678 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5679 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005680
5681bcma_out:
5682 return err;
5683
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005684bcma_err_wireless_exit:
5685 ieee80211_free_hw(wl->hw);
5686 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005687}
5688
5689static void b43_bcma_remove(struct bcma_device *core)
5690{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005691 struct b43_wldev *wldev = bcma_get_drvdata(core);
5692 struct b43_wl *wl = wldev->wl;
5693
5694 /* We must cancel any work here before unregistering from ieee80211,
5695 * as the ieee80211 unreg will destroy the workqueue. */
5696 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005697 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005698
Oleksij Rempele64add22012-06-05 20:39:32 +02005699 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005700 if (!wldev->fw.ucode.data)
5701 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005702 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005703 b43_leds_stop(wldev);
5704 ieee80211_unregister_hw(wl->hw);
5705 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005706
5707 b43_one_core_detach(wldev->dev);
5708
Larry Finger09164042014-01-12 15:11:37 -06005709 /* Unregister HW RNG driver */
5710 b43_rng_exit(wl);
5711
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005712 b43_leds_unregister(wl);
5713
5714 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005715}
5716
5717static struct bcma_driver b43_bcma_driver = {
5718 .name = KBUILD_MODNAME,
5719 .id_table = b43_bcma_tbl,
5720 .probe = b43_bcma_probe,
5721 .remove = b43_bcma_remove,
5722};
5723#endif
5724
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005725#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005726static
5727int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005728{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005729 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005730 struct b43_wl *wl;
5731 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005732
Rafał Miłecki482f0532011-05-18 02:06:36 +02005733 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005734 if (!dev)
5735 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005736
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005737 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005738 if (wl) {
5739 b43err(NULL, "Dual-core devices are not supported\n");
5740 err = -ENOTSUPP;
5741 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005742 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005743
5744 b43_sprom_fixup(sdev->bus);
5745
5746 wl = b43_wireless_init(dev);
5747 if (IS_ERR(wl)) {
5748 err = PTR_ERR(wl);
5749 goto err_ssb_kfree_dev;
5750 }
5751 ssb_set_devtypedata(sdev, wl);
5752 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5753
Michael Buesche4d6b792007-09-18 15:39:42 -04005754 err = b43_one_core_attach(dev, wl);
5755 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005756 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005757
Larry Finger6b6fa582012-03-08 22:27:46 -06005758 /* setup and start work to load firmware */
5759 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5760 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005761
Michael Buesche4d6b792007-09-18 15:39:42 -04005762 return err;
5763
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005764err_ssb_wireless_exit:
5765 b43_wireless_exit(dev, wl);
5766err_ssb_kfree_dev:
5767 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005768 return err;
5769}
5770
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005771static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005772{
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005773 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5774 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005775 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005776
Michael Buesch3bf0a322008-05-22 16:32:16 +02005777 /* We must cancel any work here before unregistering from ieee80211,
5778 * as the ieee80211 unreg will destroy the workqueue. */
5779 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005780 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005781
Michael Buesche4d6b792007-09-18 15:39:42 -04005782 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005783 if (!wldev->fw.ucode.data)
5784 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005785 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005786 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005787 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005788 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005789
Pavel Roskine61b52d2011-07-22 18:07:13 -04005790 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005791
Larry Finger09164042014-01-12 15:11:37 -06005792 /* Unregister HW RNG driver */
5793 b43_rng_exit(wl);
5794
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005795 b43_leds_unregister(wl);
5796 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005797}
5798
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005799static struct ssb_driver b43_ssb_driver = {
5800 .name = KBUILD_MODNAME,
5801 .id_table = b43_ssb_tbl,
5802 .probe = b43_ssb_probe,
5803 .remove = b43_ssb_remove,
5804};
5805#endif /* CONFIG_B43_SSB */
5806
Michael Buesche4d6b792007-09-18 15:39:42 -04005807/* Perform a hardware reset. This can be called from any context. */
5808void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5809{
5810 /* Must avoid requeueing, if we are in shutdown. */
5811 if (b43_status(dev) < B43_STAT_INITIALIZED)
5812 return;
5813 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005814 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005815}
5816
Michael Buesch26bc7832008-02-09 00:18:35 +01005817static void b43_print_driverinfo(void)
5818{
5819 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005820 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005821
5822#ifdef CONFIG_B43_PCI_AUTOSELECT
5823 feat_pci = "P";
5824#endif
5825#ifdef CONFIG_B43_PCMCIA
5826 feat_pcmcia = "M";
5827#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005828#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005829 feat_nphy = "N";
5830#endif
5831#ifdef CONFIG_B43_LEDS
5832 feat_leds = "L";
5833#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005834#ifdef CONFIG_B43_SDIO
5835 feat_sdio = "S";
5836#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005837 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005838 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005839 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005840 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005841}
5842
Michael Buesche4d6b792007-09-18 15:39:42 -04005843static int __init b43_init(void)
5844{
5845 int err;
5846
5847 b43_debugfs_init();
5848 err = b43_pcmcia_init();
5849 if (err)
5850 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005851 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005852 if (err)
5853 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005854#ifdef CONFIG_B43_BCMA
5855 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005856 if (err)
5857 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005858#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005859#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005860 err = ssb_driver_register(&b43_ssb_driver);
5861 if (err)
5862 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005863#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005864 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005865
5866 return err;
5867
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005868#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005869err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005870#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005871#ifdef CONFIG_B43_BCMA
5872 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005873err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005874#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005875 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005876err_pcmcia_exit:
5877 b43_pcmcia_exit();
5878err_dfs_exit:
5879 b43_debugfs_exit();
5880 return err;
5881}
5882
5883static void __exit b43_exit(void)
5884{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005885#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005886 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005887#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005888#ifdef CONFIG_B43_BCMA
5889 bcma_driver_unregister(&b43_bcma_driver);
5890#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005891 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005892 b43_pcmcia_exit();
5893 b43_debugfs_exit();
5894}
5895
5896module_init(b43_init)
5897module_exit(b43_exit)