blob: b2bc593a6513c1f9b112ced295c820755c84ef15 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio1f21ad22007-11-06 22:49:20 +01006 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Michael Büscheb032b92011-07-04 20:50:05 +02007 Copyright (c) 2005-2009 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -04008 Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
Rafał Miłecki108f4f32011-09-03 21:01:02 +020010 Copyright (c) 2010-2011 Rafał Miłecki <zajec5@gmail.com>
Michael Buesche4d6b792007-09-18 15:39:42 -040011
Albert Herranz3dbba8e2009-09-10 19:34:49 +020012 SDIO support
13 Copyright (c) 2009 Albert Herranz <albert_herranz@yahoo.es>
14
Michael Buesche4d6b792007-09-18 15:39:42 -040015 Some parts of the code in this file are derived from the ipw2200
16 driver Copyright(c) 2003 - 2004 Intel Corporation.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; see the file COPYING. If not, write to
30 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
31 Boston, MA 02110-1301, USA.
32
33*/
34
35#include <linux/delay.h>
36#include <linux/init.h>
Paul Gortmakerac5c24e92011-08-30 14:18:44 -040037#include <linux/module.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040038#include <linux/if_arp.h>
39#include <linux/etherdevice.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040040#include <linux/firmware.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040041#include <linux/workqueue.h>
42#include <linux/skbuff.h>
Andrew Morton96cf49a2008-02-04 22:27:19 -080043#include <linux/io.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040044#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040046#include <asm/unaligned.h>
47
48#include "b43.h"
49#include "main.h"
50#include "debugfs.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020051#include "phy_common.h"
52#include "phy_g.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020053#include "phy_n.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040054#include "dma.h"
Michael Buesch5100d5a2008-03-29 21:01:16 +010055#include "pio.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040056#include "sysfs.h"
57#include "xmit.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040058#include "lo.h"
59#include "pcmcia.h"
Albert Herranz3dbba8e2009-09-10 19:34:49 +020060#include "sdio.h"
61#include <linux/mmc/sdio_func.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040062
63MODULE_DESCRIPTION("Broadcom B43 wireless driver");
64MODULE_AUTHOR("Martin Langer");
65MODULE_AUTHOR("Stefano Brivio");
66MODULE_AUTHOR("Michael Buesch");
Gábor Stefanik0136e512009-08-28 22:32:17 +020067MODULE_AUTHOR("Gábor Stefanik");
Rafał Miłecki108f4f32011-09-03 21:01:02 +020068MODULE_AUTHOR("Rafał Miłecki");
Michael Buesche4d6b792007-09-18 15:39:42 -040069MODULE_LICENSE("GPL");
70
Tim Gardner6021e082010-01-07 11:10:38 -070071MODULE_FIRMWARE("b43/ucode11.fw");
72MODULE_FIRMWARE("b43/ucode13.fw");
73MODULE_FIRMWARE("b43/ucode14.fw");
74MODULE_FIRMWARE("b43/ucode15.fw");
Rafał Miłeckif6158392011-04-19 22:49:29 +020075MODULE_FIRMWARE("b43/ucode16_mimo.fw");
Tim Gardner6021e082010-01-07 11:10:38 -070076MODULE_FIRMWARE("b43/ucode5.fw");
77MODULE_FIRMWARE("b43/ucode9.fw");
Michael Buesche4d6b792007-09-18 15:39:42 -040078
79static int modparam_bad_frames_preempt;
80module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
81MODULE_PARM_DESC(bad_frames_preempt,
82 "enable(1) / disable(0) Bad Frames Preemption");
83
Michael Buesche4d6b792007-09-18 15:39:42 -040084static char modparam_fwpostfix[16];
85module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
86MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
87
Michael Buesche4d6b792007-09-18 15:39:42 -040088static int modparam_hwpctl;
89module_param_named(hwpctl, modparam_hwpctl, int, 0444);
90MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
91
92static int modparam_nohwcrypt;
93module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
94MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
95
gregor kowski035d0242009-08-19 22:35:45 +020096static int modparam_hwtkip;
97module_param_named(hwtkip, modparam_hwtkip, int, 0444);
98MODULE_PARM_DESC(hwtkip, "Enable hardware tkip.");
99
Michael Buesch403a3a12009-06-08 21:04:57 +0200100static int modparam_qos = 1;
101module_param_named(qos, modparam_qos, int, 0444);
Michael Buesche6f5b932008-03-05 21:18:49 +0100102MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
103
Michael Buesch1855ba72008-04-18 20:51:41 +0200104static int modparam_btcoex = 1;
105module_param_named(btcoex, modparam_btcoex, int, 0444);
Gábor Stefanikc71dbd32009-08-28 22:34:21 +0200106MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistence (default on)");
Michael Buesch1855ba72008-04-18 20:51:41 +0200107
Michael Buesch060210f2009-01-25 15:49:59 +0100108int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
109module_param_named(verbose, b43_modparam_verbose, int, 0644);
110MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
111
Rafał Miłeckidf766262011-08-16 12:14:07 +0200112static int b43_modparam_pio = 0;
Linus Torvalds9e3bd912010-02-26 10:34:27 -0800113module_param_named(pio, b43_modparam_pio, int, 0644);
114MODULE_PARM_DESC(pio, "Use PIO accesses by default: 0=DMA, 1=PIO");
Michael Buesche6f5b932008-03-05 21:18:49 +0100115
Rafał Miłecki89604002013-06-26 09:55:54 +0200116static int modparam_allhwsupport = !IS_ENABLED(CONFIG_BRCMSMAC);
117module_param_named(allhwsupport, modparam_allhwsupport, int, 0444);
118MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the brcmsmac driver)");
119
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200120#ifdef CONFIG_B43_BCMA
121static const struct bcma_device_id b43_bcma_tbl[] = {
Hauke Mehrtensc027ed42011-07-23 13:57:34 +0200122 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x11, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200123 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x17, BCMA_ANY_CLASS),
124 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x18, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200125 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1C, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200126 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1D, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200127 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1E, BCMA_ANY_CLASS),
128 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x28, BCMA_ANY_CLASS),
129 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x2A, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200130 BCMA_CORETABLE_END
131};
132MODULE_DEVICE_TABLE(bcma, b43_bcma_tbl);
133#endif
134
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200135#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -0400136static const struct ssb_device_id b43_ssb_tbl[] = {
137 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
138 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
139 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
140 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
141 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
Michael Bueschd5c71e42008-01-04 17:06:29 +0100142 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
Rafał Miłecki003d6d22010-01-15 12:10:53 +0100143 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 12),
Larry Finger013978b2007-11-26 10:29:47 -0600144 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
Michael Buesch6b1c7c62008-12-25 00:39:28 +0100145 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
Johannes Berg92d61282008-12-24 12:44:09 +0100146 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
Michael Buesche4d6b792007-09-18 15:39:42 -0400147 SSB_DEVTABLE_END
148};
Michael Buesche4d6b792007-09-18 15:39:42 -0400149MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200150#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400151
152/* Channel and ratetables are shared for all devices.
153 * They can't be const, because ieee80211 puts some precalculated
154 * data in there. This data is the same for all devices, so we don't
155 * get concurrency issues */
156#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +0100157 { \
158 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
159 .hw_value = (_rateid), \
160 .flags = (_flags), \
Michael Buesche4d6b792007-09-18 15:39:42 -0400161 }
Johannes Berg8318d782008-01-24 19:38:38 +0100162
163/*
164 * NOTE: When changing this, sync with xmit.c's
165 * b43_plcp_get_bitrate_idx_* functions!
166 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400167static struct ieee80211_rate __b43_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100168 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
169 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
170 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
171 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
172 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
173 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
174 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
175 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
176 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
177 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
178 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
179 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400180};
181
182#define b43_a_ratetable (__b43_ratetable + 4)
183#define b43_a_ratetable_size 8
184#define b43_b_ratetable (__b43_ratetable + 0)
185#define b43_b_ratetable_size 4
186#define b43_g_ratetable (__b43_ratetable + 0)
187#define b43_g_ratetable_size 12
188
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200189#define CHAN2G(_channel, _freq, _flags) { \
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100190 .band = IEEE80211_BAND_2GHZ, \
191 .center_freq = (_freq), \
192 .hw_value = (_channel), \
193 .flags = (_flags), \
194 .max_antenna_gain = 0, \
195 .max_power = 30, \
196}
Michael Buesch96c755a2008-01-06 00:09:46 +0100197static struct ieee80211_channel b43_2ghz_chantable[] = {
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200198 CHAN2G(1, 2412, 0),
199 CHAN2G(2, 2417, 0),
200 CHAN2G(3, 2422, 0),
201 CHAN2G(4, 2427, 0),
202 CHAN2G(5, 2432, 0),
203 CHAN2G(6, 2437, 0),
204 CHAN2G(7, 2442, 0),
205 CHAN2G(8, 2447, 0),
206 CHAN2G(9, 2452, 0),
207 CHAN2G(10, 2457, 0),
208 CHAN2G(11, 2462, 0),
209 CHAN2G(12, 2467, 0),
210 CHAN2G(13, 2472, 0),
211 CHAN2G(14, 2484, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100212};
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200213#undef CHAN2G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100214
Rafał Miłecki91211732014-05-21 08:44:20 +0200215#define CHAN4G(_channel, _flags) { \
216 .band = IEEE80211_BAND_5GHZ, \
217 .center_freq = 4000 + (5 * (_channel)), \
218 .hw_value = (_channel), \
219 .flags = (_flags), \
220 .max_antenna_gain = 0, \
221 .max_power = 30, \
222}
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100223#define CHAN5G(_channel, _flags) { \
224 .band = IEEE80211_BAND_5GHZ, \
225 .center_freq = 5000 + (5 * (_channel)), \
226 .hw_value = (_channel), \
227 .flags = (_flags), \
228 .max_antenna_gain = 0, \
229 .max_power = 30, \
230}
231static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
Rafał Miłecki91211732014-05-21 08:44:20 +0200232 CHAN4G(184, 0), CHAN4G(186, 0),
233 CHAN4G(188, 0), CHAN4G(190, 0),
234 CHAN4G(192, 0), CHAN4G(194, 0),
235 CHAN4G(196, 0), CHAN4G(198, 0),
236 CHAN4G(200, 0), CHAN4G(202, 0),
237 CHAN4G(204, 0), CHAN4G(206, 0),
238 CHAN4G(208, 0), CHAN4G(210, 0),
239 CHAN4G(212, 0), CHAN4G(214, 0),
240 CHAN4G(216, 0), CHAN4G(218, 0),
241 CHAN4G(220, 0), CHAN4G(222, 0),
242 CHAN4G(224, 0), CHAN4G(226, 0),
243 CHAN4G(228, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100244 CHAN5G(32, 0), CHAN5G(34, 0),
245 CHAN5G(36, 0), CHAN5G(38, 0),
246 CHAN5G(40, 0), CHAN5G(42, 0),
247 CHAN5G(44, 0), CHAN5G(46, 0),
248 CHAN5G(48, 0), CHAN5G(50, 0),
249 CHAN5G(52, 0), CHAN5G(54, 0),
250 CHAN5G(56, 0), CHAN5G(58, 0),
251 CHAN5G(60, 0), CHAN5G(62, 0),
252 CHAN5G(64, 0), CHAN5G(66, 0),
253 CHAN5G(68, 0), CHAN5G(70, 0),
254 CHAN5G(72, 0), CHAN5G(74, 0),
255 CHAN5G(76, 0), CHAN5G(78, 0),
256 CHAN5G(80, 0), CHAN5G(82, 0),
257 CHAN5G(84, 0), CHAN5G(86, 0),
258 CHAN5G(88, 0), CHAN5G(90, 0),
259 CHAN5G(92, 0), CHAN5G(94, 0),
260 CHAN5G(96, 0), CHAN5G(98, 0),
261 CHAN5G(100, 0), CHAN5G(102, 0),
262 CHAN5G(104, 0), CHAN5G(106, 0),
263 CHAN5G(108, 0), CHAN5G(110, 0),
264 CHAN5G(112, 0), CHAN5G(114, 0),
265 CHAN5G(116, 0), CHAN5G(118, 0),
266 CHAN5G(120, 0), CHAN5G(122, 0),
267 CHAN5G(124, 0), CHAN5G(126, 0),
268 CHAN5G(128, 0), CHAN5G(130, 0),
269 CHAN5G(132, 0), CHAN5G(134, 0),
270 CHAN5G(136, 0), CHAN5G(138, 0),
271 CHAN5G(140, 0), CHAN5G(142, 0),
272 CHAN5G(144, 0), CHAN5G(145, 0),
273 CHAN5G(146, 0), CHAN5G(147, 0),
274 CHAN5G(148, 0), CHAN5G(149, 0),
275 CHAN5G(150, 0), CHAN5G(151, 0),
276 CHAN5G(152, 0), CHAN5G(153, 0),
277 CHAN5G(154, 0), CHAN5G(155, 0),
278 CHAN5G(156, 0), CHAN5G(157, 0),
279 CHAN5G(158, 0), CHAN5G(159, 0),
280 CHAN5G(160, 0), CHAN5G(161, 0),
281 CHAN5G(162, 0), CHAN5G(163, 0),
282 CHAN5G(164, 0), CHAN5G(165, 0),
283 CHAN5G(166, 0), CHAN5G(168, 0),
284 CHAN5G(170, 0), CHAN5G(172, 0),
285 CHAN5G(174, 0), CHAN5G(176, 0),
286 CHAN5G(178, 0), CHAN5G(180, 0),
Rafał Miłecki91211732014-05-21 08:44:20 +0200287 CHAN5G(182, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400288};
289
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100290static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
291 CHAN5G(34, 0), CHAN5G(36, 0),
292 CHAN5G(38, 0), CHAN5G(40, 0),
293 CHAN5G(42, 0), CHAN5G(44, 0),
294 CHAN5G(46, 0), CHAN5G(48, 0),
295 CHAN5G(52, 0), CHAN5G(56, 0),
296 CHAN5G(60, 0), CHAN5G(64, 0),
297 CHAN5G(100, 0), CHAN5G(104, 0),
298 CHAN5G(108, 0), CHAN5G(112, 0),
299 CHAN5G(116, 0), CHAN5G(120, 0),
300 CHAN5G(124, 0), CHAN5G(128, 0),
301 CHAN5G(132, 0), CHAN5G(136, 0),
302 CHAN5G(140, 0), CHAN5G(149, 0),
303 CHAN5G(153, 0), CHAN5G(157, 0),
304 CHAN5G(161, 0), CHAN5G(165, 0),
305 CHAN5G(184, 0), CHAN5G(188, 0),
306 CHAN5G(192, 0), CHAN5G(196, 0),
307 CHAN5G(200, 0), CHAN5G(204, 0),
308 CHAN5G(208, 0), CHAN5G(212, 0),
309 CHAN5G(216, 0),
310};
Rafał Miłecki91211732014-05-21 08:44:20 +0200311#undef CHAN4G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100312#undef CHAN5G
313
314static struct ieee80211_supported_band b43_band_5GHz_nphy = {
315 .band = IEEE80211_BAND_5GHZ,
316 .channels = b43_5ghz_nphy_chantable,
317 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
318 .bitrates = b43_a_ratetable,
319 .n_bitrates = b43_a_ratetable_size,
Michael Buesche4d6b792007-09-18 15:39:42 -0400320};
Johannes Berg8318d782008-01-24 19:38:38 +0100321
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100322static struct ieee80211_supported_band b43_band_5GHz_aphy = {
323 .band = IEEE80211_BAND_5GHZ,
324 .channels = b43_5ghz_aphy_chantable,
325 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
326 .bitrates = b43_a_ratetable,
327 .n_bitrates = b43_a_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100328};
Michael Buesche4d6b792007-09-18 15:39:42 -0400329
Johannes Berg8318d782008-01-24 19:38:38 +0100330static struct ieee80211_supported_band b43_band_2GHz = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100331 .band = IEEE80211_BAND_2GHZ,
332 .channels = b43_2ghz_chantable,
333 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
334 .bitrates = b43_g_ratetable,
335 .n_bitrates = b43_g_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100336};
337
Michael Buesche4d6b792007-09-18 15:39:42 -0400338static void b43_wireless_core_exit(struct b43_wldev *dev);
339static int b43_wireless_core_init(struct b43_wldev *dev);
Michael Buesch36dbd952009-09-04 22:51:29 +0200340static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400341static int b43_wireless_core_start(struct b43_wldev *dev);
Felix Fietkau2a190322011-08-10 13:50:30 -0600342static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
343 struct ieee80211_vif *vif,
344 struct ieee80211_bss_conf *conf,
345 u32 changed);
Michael Buesche4d6b792007-09-18 15:39:42 -0400346
347static int b43_ratelimit(struct b43_wl *wl)
348{
349 if (!wl || !wl->current_dev)
350 return 1;
351 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
352 return 1;
353 /* We are up and running.
354 * Ratelimit the messages to avoid DoS over the net. */
355 return net_ratelimit();
356}
357
358void b43info(struct b43_wl *wl, const char *fmt, ...)
359{
Joe Perches5b736d42010-11-09 16:35:18 -0800360 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400361 va_list args;
362
Michael Buesch060210f2009-01-25 15:49:59 +0100363 if (b43_modparam_verbose < B43_VERBOSITY_INFO)
364 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400365 if (!b43_ratelimit(wl))
366 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800367
Michael Buesche4d6b792007-09-18 15:39:42 -0400368 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800369
370 vaf.fmt = fmt;
371 vaf.va = &args;
372
373 printk(KERN_INFO "b43-%s: %pV",
374 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
375
Michael Buesche4d6b792007-09-18 15:39:42 -0400376 va_end(args);
377}
378
379void b43err(struct b43_wl *wl, const char *fmt, ...)
380{
Joe Perches5b736d42010-11-09 16:35:18 -0800381 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400382 va_list args;
383
Michael Buesch060210f2009-01-25 15:49:59 +0100384 if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
385 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400386 if (!b43_ratelimit(wl))
387 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800388
Michael Buesche4d6b792007-09-18 15:39:42 -0400389 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800390
391 vaf.fmt = fmt;
392 vaf.va = &args;
393
394 printk(KERN_ERR "b43-%s ERROR: %pV",
395 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
396
Michael Buesche4d6b792007-09-18 15:39:42 -0400397 va_end(args);
398}
399
400void b43warn(struct b43_wl *wl, const char *fmt, ...)
401{
Joe Perches5b736d42010-11-09 16:35:18 -0800402 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 va_list args;
404
Michael Buesch060210f2009-01-25 15:49:59 +0100405 if (b43_modparam_verbose < B43_VERBOSITY_WARN)
406 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400407 if (!b43_ratelimit(wl))
408 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800409
Michael Buesche4d6b792007-09-18 15:39:42 -0400410 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800411
412 vaf.fmt = fmt;
413 vaf.va = &args;
414
415 printk(KERN_WARNING "b43-%s warning: %pV",
416 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
417
Michael Buesche4d6b792007-09-18 15:39:42 -0400418 va_end(args);
419}
420
Michael Buesche4d6b792007-09-18 15:39:42 -0400421void b43dbg(struct b43_wl *wl, const char *fmt, ...)
422{
Joe Perches5b736d42010-11-09 16:35:18 -0800423 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400424 va_list args;
425
Michael Buesch060210f2009-01-25 15:49:59 +0100426 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
427 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800428
Michael Buesche4d6b792007-09-18 15:39:42 -0400429 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800430
431 vaf.fmt = fmt;
432 vaf.va = &args;
433
434 printk(KERN_DEBUG "b43-%s debug: %pV",
435 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
436
Michael Buesche4d6b792007-09-18 15:39:42 -0400437 va_end(args);
438}
Michael Buesche4d6b792007-09-18 15:39:42 -0400439
440static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
441{
442 u32 macctl;
443
444 B43_WARN_ON(offset % 4 != 0);
445
446 macctl = b43_read32(dev, B43_MMIO_MACCTL);
447 if (macctl & B43_MACCTL_BE)
448 val = swab32(val);
449
450 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
451 mmiowb();
452 b43_write32(dev, B43_MMIO_RAM_DATA, val);
453}
454
Michael Buesch280d0e12007-12-26 18:26:17 +0100455static inline void b43_shm_control_word(struct b43_wldev *dev,
456 u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400457{
458 u32 control;
459
460 /* "offset" is the WORD offset. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400461 control = routing;
462 control <<= 16;
463 control |= offset;
464 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
465}
466
Michael Buesch69eddc82009-09-04 22:57:26 +0200467u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400468{
469 u32 ret;
470
471 if (routing == B43_SHM_SHARED) {
472 B43_WARN_ON(offset & 0x0001);
473 if (offset & 0x0003) {
474 /* Unaligned access */
475 b43_shm_control_word(dev, routing, offset >> 2);
476 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
Michael Buesche4d6b792007-09-18 15:39:42 -0400477 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200478 ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400479
Michael Buesch280d0e12007-12-26 18:26:17 +0100480 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400481 }
482 offset >>= 2;
483 }
484 b43_shm_control_word(dev, routing, offset);
485 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100486out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200487 return ret;
488}
489
Michael Buesch69eddc82009-09-04 22:57:26 +0200490u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400491{
492 u16 ret;
493
494 if (routing == B43_SHM_SHARED) {
495 B43_WARN_ON(offset & 0x0001);
496 if (offset & 0x0003) {
497 /* Unaligned access */
498 b43_shm_control_word(dev, routing, offset >> 2);
499 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
500
Michael Buesch280d0e12007-12-26 18:26:17 +0100501 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400502 }
503 offset >>= 2;
504 }
505 b43_shm_control_word(dev, routing, offset);
506 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100507out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200508 return ret;
509}
510
Michael Buesch69eddc82009-09-04 22:57:26 +0200511void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400512{
513 if (routing == B43_SHM_SHARED) {
514 B43_WARN_ON(offset & 0x0001);
515 if (offset & 0x0003) {
516 /* Unaligned access */
517 b43_shm_control_word(dev, routing, offset >> 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400518 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200519 value & 0xFFFF);
Michael Buesche4d6b792007-09-18 15:39:42 -0400520 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200521 b43_write16(dev, B43_MMIO_SHM_DATA,
522 (value >> 16) & 0xFFFF);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200523 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400524 }
525 offset >>= 2;
526 }
527 b43_shm_control_word(dev, routing, offset);
Michael Buesche4d6b792007-09-18 15:39:42 -0400528 b43_write32(dev, B43_MMIO_SHM_DATA, value);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200529}
530
Michael Buesch69eddc82009-09-04 22:57:26 +0200531void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
Michael Buesch6bbc3212008-06-19 19:33:51 +0200532{
533 if (routing == B43_SHM_SHARED) {
534 B43_WARN_ON(offset & 0x0001);
535 if (offset & 0x0003) {
536 /* Unaligned access */
537 b43_shm_control_word(dev, routing, offset >> 2);
538 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
539 return;
540 }
541 offset >>= 2;
542 }
543 b43_shm_control_word(dev, routing, offset);
544 b43_write16(dev, B43_MMIO_SHM_DATA, value);
545}
546
Michael Buesche4d6b792007-09-18 15:39:42 -0400547/* Read HostFlags */
John Daiker99da1852009-02-24 02:16:42 -0800548u64 b43_hf_read(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400549{
Michael Buesch35f0d352008-02-13 14:31:08 +0100550 u64 ret;
Michael Buesche4d6b792007-09-18 15:39:42 -0400551
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200552 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400553 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200554 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2);
Michael Buesch35f0d352008-02-13 14:31:08 +0100555 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200556 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400557
558 return ret;
559}
560
561/* Write HostFlags */
Michael Buesch35f0d352008-02-13 14:31:08 +0100562void b43_hf_write(struct b43_wldev *dev, u64 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400563{
Michael Buesch35f0d352008-02-13 14:31:08 +0100564 u16 lo, mi, hi;
565
566 lo = (value & 0x00000000FFFFULL);
567 mi = (value & 0x0000FFFF0000ULL) >> 16;
568 hi = (value & 0xFFFF00000000ULL) >> 32;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200569 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1, lo);
570 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2, mi);
571 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3, hi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400572}
573
Michael Buesch403a3a12009-06-08 21:04:57 +0200574/* Read the firmware capabilities bitmask (Opensource firmware only) */
575static u16 b43_fwcapa_read(struct b43_wldev *dev)
576{
577 B43_WARN_ON(!dev->fw.opensource);
578 return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
579}
580
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100581void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
Michael Buesche4d6b792007-09-18 15:39:42 -0400582{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100583 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400584
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200585 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400586
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100587 /* The hardware guarantees us an atomic read, if we
588 * read the low register first. */
589 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
590 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400591
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100592 *tsf = high;
593 *tsf <<= 32;
594 *tsf |= low;
Michael Buesche4d6b792007-09-18 15:39:42 -0400595}
596
597static void b43_time_lock(struct b43_wldev *dev)
598{
Rafał Miłecki50566352012-01-02 19:31:21 +0100599 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_TBTTHOLD);
Michael Buesche4d6b792007-09-18 15:39:42 -0400600 /* Commit the write */
601 b43_read32(dev, B43_MMIO_MACCTL);
602}
603
604static void b43_time_unlock(struct b43_wldev *dev)
605{
Rafał Miłecki50566352012-01-02 19:31:21 +0100606 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_TBTTHOLD, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400607 /* Commit the write */
608 b43_read32(dev, B43_MMIO_MACCTL);
609}
610
611static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
612{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100613 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400614
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200615 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400616
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100617 low = tsf;
618 high = (tsf >> 32);
619 /* The hardware guarantees us an atomic write, if we
620 * write the low register first. */
621 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
622 mmiowb();
623 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
624 mmiowb();
Michael Buesche4d6b792007-09-18 15:39:42 -0400625}
626
627void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
628{
629 b43_time_lock(dev);
630 b43_tsf_write_locked(dev, tsf);
631 b43_time_unlock(dev);
632}
633
634static
John Daiker99da1852009-02-24 02:16:42 -0800635void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
Michael Buesche4d6b792007-09-18 15:39:42 -0400636{
637 static const u8 zero_addr[ETH_ALEN] = { 0 };
638 u16 data;
639
640 if (!mac)
641 mac = zero_addr;
642
643 offset |= 0x0020;
644 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
645
646 data = mac[0];
647 data |= mac[1] << 8;
648 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
649 data = mac[2];
650 data |= mac[3] << 8;
651 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
652 data = mac[4];
653 data |= mac[5] << 8;
654 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
655}
656
657static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
658{
659 const u8 *mac;
660 const u8 *bssid;
661 u8 mac_bssid[ETH_ALEN * 2];
662 int i;
663 u32 tmp;
664
665 bssid = dev->wl->bssid;
666 mac = dev->wl->mac_addr;
667
668 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
669
670 memcpy(mac_bssid, mac, ETH_ALEN);
671 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
672
673 /* Write our MAC address and BSSID to template ram */
674 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
675 tmp = (u32) (mac_bssid[i + 0]);
676 tmp |= (u32) (mac_bssid[i + 1]) << 8;
677 tmp |= (u32) (mac_bssid[i + 2]) << 16;
678 tmp |= (u32) (mac_bssid[i + 3]) << 24;
679 b43_ram_write(dev, 0x20 + i, tmp);
680 }
681}
682
Johannes Berg4150c572007-09-17 01:29:23 -0400683static void b43_upload_card_macaddress(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400684{
Michael Buesche4d6b792007-09-18 15:39:42 -0400685 b43_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400686 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400687}
688
689static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
690{
691 /* slot_time is in usec. */
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600692 /* This test used to exit for all but a G PHY. */
693 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Michael Buesche4d6b792007-09-18 15:39:42 -0400694 return;
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600695 b43_write16(dev, B43_MMIO_IFSSLOT, 510 + slot_time);
696 /* Shared memory location 0x0010 is the slot time and should be
697 * set to slot_time; however, this register is initially 0 and changing
698 * the value adversely affects the transmit rate for BCM4311
699 * devices. Until this behavior is unterstood, delete this step
700 *
701 * b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
702 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400703}
704
705static void b43_short_slot_timing_enable(struct b43_wldev *dev)
706{
707 b43_set_slot_time(dev, 9);
Michael Buesche4d6b792007-09-18 15:39:42 -0400708}
709
710static void b43_short_slot_timing_disable(struct b43_wldev *dev)
711{
712 b43_set_slot_time(dev, 20);
Michael Buesche4d6b792007-09-18 15:39:42 -0400713}
714
Michael Buesche4d6b792007-09-18 15:39:42 -0400715/* DummyTransmission function, as documented on
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200716 * http://bcm-v4.sipsolutions.net/802.11/DummyTransmission
Michael Buesche4d6b792007-09-18 15:39:42 -0400717 */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200718void b43_dummy_transmission(struct b43_wldev *dev, bool ofdm, bool pa_on)
Michael Buesche4d6b792007-09-18 15:39:42 -0400719{
720 struct b43_phy *phy = &dev->phy;
721 unsigned int i, max_loop;
722 u16 value;
723 u32 buffer[5] = {
724 0x00000000,
725 0x00D40000,
726 0x00000000,
727 0x01000000,
728 0x00000000,
729 };
730
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200731 if (ofdm) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400732 max_loop = 0x1E;
733 buffer[0] = 0x000201CC;
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200734 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -0400735 max_loop = 0xFA;
736 buffer[0] = 0x000B846E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400737 }
738
739 for (i = 0; i < 5; i++)
740 b43_ram_write(dev, i * 4, buffer[i]);
741
Rafał Miłecki7955d872011-09-21 21:44:13 +0200742 b43_write16(dev, B43_MMIO_XMTSEL, 0x0000);
743
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200744 if (dev->dev->core_rev < 11)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200745 b43_write16(dev, B43_MMIO_WEPCTL, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200746 else
Rafał Miłecki7955d872011-09-21 21:44:13 +0200747 b43_write16(dev, B43_MMIO_WEPCTL, 0x0100);
748
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200749 value = (ofdm ? 0x41 : 0x40);
Rafał Miłecki7955d872011-09-21 21:44:13 +0200750 b43_write16(dev, B43_MMIO_TXE0_PHYCTL, value);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200751 if (phy->type == B43_PHYTYPE_N || phy->type == B43_PHYTYPE_LP ||
752 phy->type == B43_PHYTYPE_LCN)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200753 b43_write16(dev, B43_MMIO_TXE0_PHYCTL1, 0x1A02);
754
755 b43_write16(dev, B43_MMIO_TXE0_WM_0, 0x0000);
756 b43_write16(dev, B43_MMIO_TXE0_WM_1, 0x0000);
757
758 b43_write16(dev, B43_MMIO_XMTTPLATETXPTR, 0x0000);
759 b43_write16(dev, B43_MMIO_XMTTXCNT, 0x0014);
760 b43_write16(dev, B43_MMIO_XMTSEL, 0x0826);
761 b43_write16(dev, B43_MMIO_TXE0_CTL, 0x0000);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200762
763 if (!pa_on && phy->type == B43_PHYTYPE_N)
764 ; /*b43_nphy_pa_override(dev, false) */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200765
766 switch (phy->type) {
767 case B43_PHYTYPE_N:
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200768 case B43_PHYTYPE_LCN:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200769 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x00D0);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200770 break;
771 case B43_PHYTYPE_LP:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200772 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0050);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200773 break;
774 default:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200775 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0030);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200776 }
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200777 b43_read16(dev, B43_MMIO_TXE0_AUX);
Michael Buesche4d6b792007-09-18 15:39:42 -0400778
779 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
780 b43_radio_write16(dev, 0x0051, 0x0017);
781 for (i = 0x00; i < max_loop; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200782 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400783 if (value & 0x0080)
784 break;
785 udelay(10);
786 }
787 for (i = 0x00; i < 0x0A; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200788 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400789 if (value & 0x0400)
790 break;
791 udelay(10);
792 }
Larry Finger1d280dd2008-09-29 14:19:29 -0500793 for (i = 0x00; i < 0x19; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200794 value = b43_read16(dev, B43_MMIO_IFSSTAT);
Michael Buesche4d6b792007-09-18 15:39:42 -0400795 if (!(value & 0x0100))
796 break;
797 udelay(10);
798 }
799 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
800 b43_radio_write16(dev, 0x0051, 0x0037);
801}
802
803static void key_write(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800804 u8 index, u8 algorithm, const u8 *key)
Michael Buesche4d6b792007-09-18 15:39:42 -0400805{
806 unsigned int i;
807 u32 offset;
808 u16 value;
809 u16 kidx;
810
811 /* Key index/algo block */
812 kidx = b43_kidx_to_fw(dev, index);
813 value = ((kidx << 4) | algorithm);
814 b43_shm_write16(dev, B43_SHM_SHARED,
815 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
816
817 /* Write the key to the Key Table Pointer offset */
818 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
819 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
820 value = key[i];
821 value |= (u16) (key[i + 1]) << 8;
822 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
823 }
824}
825
John Daiker99da1852009-02-24 02:16:42 -0800826static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400827{
828 u32 addrtmp[2] = { 0, 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200829 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400830
831 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200832 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400833
Michael Buesch66d2d082009-08-06 10:36:50 +0200834 B43_WARN_ON(index < pairwise_keys_start);
835 /* We have four default TX keys and possibly four default RX keys.
Michael Buesche4d6b792007-09-18 15:39:42 -0400836 * Physical mac 0 is mapped to physical key 4 or 8, depending
837 * on the firmware version.
838 * So we must adjust the index here.
839 */
Michael Buesch66d2d082009-08-06 10:36:50 +0200840 index -= pairwise_keys_start;
841 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400842
843 if (addr) {
844 addrtmp[0] = addr[0];
845 addrtmp[0] |= ((u32) (addr[1]) << 8);
846 addrtmp[0] |= ((u32) (addr[2]) << 16);
847 addrtmp[0] |= ((u32) (addr[3]) << 24);
848 addrtmp[1] = addr[4];
849 addrtmp[1] |= ((u32) (addr[5]) << 8);
850 }
851
Michael Buesch66d2d082009-08-06 10:36:50 +0200852 /* Receive match transmitter address (RCMTA) mechanism */
853 b43_shm_write32(dev, B43_SHM_RCMTA,
854 (index * 2) + 0, addrtmp[0]);
855 b43_shm_write16(dev, B43_SHM_RCMTA,
856 (index * 2) + 1, addrtmp[1]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400857}
858
gregor kowski035d0242009-08-19 22:35:45 +0200859/* The ucode will use phase1 key with TEK key to decrypt rx packets.
860 * When a packet is received, the iv32 is checked.
861 * - if it doesn't the packet is returned without modification (and software
862 * decryption can be done). That's what happen when iv16 wrap.
863 * - if it does, the rc4 key is computed, and decryption is tried.
864 * Either it will success and B43_RX_MAC_DEC is returned,
865 * either it fails and B43_RX_MAC_DEC|B43_RX_MAC_DECERR is returned
866 * and the packet is not usable (it got modified by the ucode).
867 * So in order to never have B43_RX_MAC_DECERR, we should provide
868 * a iv32 and phase1key that match. Because we drop packets in case of
869 * B43_RX_MAC_DECERR, if we have a correct iv32 but a wrong phase1key, all
870 * packets will be lost without higher layer knowing (ie no resync possible
871 * until next wrap).
872 *
873 * NOTE : this should support 50 key like RCMTA because
874 * (B43_SHM_SH_KEYIDXBLOCK - B43_SHM_SH_TKIPTSCTTAK)/14 = 50
875 */
876static void rx_tkip_phase1_write(struct b43_wldev *dev, u8 index, u32 iv32,
877 u16 *phase1key)
878{
879 unsigned int i;
880 u32 offset;
881 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
882
883 if (!modparam_hwtkip)
884 return;
885
886 if (b43_new_kidx_api(dev))
887 pairwise_keys_start = B43_NR_GROUP_KEYS;
888
889 B43_WARN_ON(index < pairwise_keys_start);
890 /* We have four default TX keys and possibly four default RX keys.
891 * Physical mac 0 is mapped to physical key 4 or 8, depending
892 * on the firmware version.
893 * So we must adjust the index here.
894 */
895 index -= pairwise_keys_start;
896 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
897
898 if (b43_debug(dev, B43_DBG_KEYS)) {
899 b43dbg(dev->wl, "rx_tkip_phase1_write : idx 0x%x, iv32 0x%x\n",
900 index, iv32);
901 }
902 /* Write the key to the RX tkip shared mem */
903 offset = B43_SHM_SH_TKIPTSCTTAK + index * (10 + 4);
904 for (i = 0; i < 10; i += 2) {
905 b43_shm_write16(dev, B43_SHM_SHARED, offset + i,
906 phase1key ? phase1key[i / 2] : 0);
907 }
908 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, iv32);
909 b43_shm_write16(dev, B43_SHM_SHARED, offset + i + 2, iv32 >> 16);
910}
911
912static void b43_op_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100913 struct ieee80211_vif *vif,
914 struct ieee80211_key_conf *keyconf,
915 struct ieee80211_sta *sta,
916 u32 iv32, u16 *phase1key)
gregor kowski035d0242009-08-19 22:35:45 +0200917{
918 struct b43_wl *wl = hw_to_b43_wl(hw);
919 struct b43_wldev *dev;
920 int index = keyconf->hw_key_idx;
921
922 if (B43_WARN_ON(!modparam_hwtkip))
923 return;
924
Michael Buesch96869a32010-01-24 13:13:32 +0100925 /* This is only called from the RX path through mac80211, where
926 * our mutex is already locked. */
927 B43_WARN_ON(!mutex_is_locked(&wl->mutex));
gregor kowski035d0242009-08-19 22:35:45 +0200928 dev = wl->current_dev;
Michael Buesch96869a32010-01-24 13:13:32 +0100929 B43_WARN_ON(!dev || b43_status(dev) < B43_STAT_INITIALIZED);
gregor kowski035d0242009-08-19 22:35:45 +0200930
931 keymac_write(dev, index, NULL); /* First zero out mac to avoid race */
932
933 rx_tkip_phase1_write(dev, index, iv32, phase1key);
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100934 /* only pairwise TKIP keys are supported right now */
935 if (WARN_ON(!sta))
Michael Buesch96869a32010-01-24 13:13:32 +0100936 return;
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100937 keymac_write(dev, index, sta->addr);
gregor kowski035d0242009-08-19 22:35:45 +0200938}
939
Michael Buesche4d6b792007-09-18 15:39:42 -0400940static void do_key_write(struct b43_wldev *dev,
941 u8 index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800942 const u8 *key, size_t key_len, const u8 *mac_addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400943{
944 u8 buf[B43_SEC_KEYSIZE] = { 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200945 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400946
947 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200948 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400949
Michael Buesch66d2d082009-08-06 10:36:50 +0200950 B43_WARN_ON(index >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400951 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
952
Michael Buesch66d2d082009-08-06 10:36:50 +0200953 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400954 keymac_write(dev, index, NULL); /* First zero out mac. */
gregor kowski035d0242009-08-19 22:35:45 +0200955 if (algorithm == B43_SEC_ALGO_TKIP) {
956 /*
957 * We should provide an initial iv32, phase1key pair.
958 * We could start with iv32=0 and compute the corresponding
959 * phase1key, but this means calling ieee80211_get_tkip_key
960 * with a fake skb (or export other tkip function).
961 * Because we are lazy we hope iv32 won't start with
962 * 0xffffffff and let's b43_op_update_tkip_key provide a
963 * correct pair.
964 */
965 rx_tkip_phase1_write(dev, index, 0xffffffff, (u16*)buf);
966 } else if (index >= pairwise_keys_start) /* clear it */
967 rx_tkip_phase1_write(dev, index, 0, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -0400968 if (key)
969 memcpy(buf, key, key_len);
970 key_write(dev, index, algorithm, buf);
Michael Buesch66d2d082009-08-06 10:36:50 +0200971 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400972 keymac_write(dev, index, mac_addr);
973
974 dev->key[index].algorithm = algorithm;
975}
976
977static int b43_key_write(struct b43_wldev *dev,
978 int index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800979 const u8 *key, size_t key_len,
980 const u8 *mac_addr,
Michael Buesche4d6b792007-09-18 15:39:42 -0400981 struct ieee80211_key_conf *keyconf)
982{
983 int i;
Michael Buesch66d2d082009-08-06 10:36:50 +0200984 int pairwise_keys_start;
Michael Buesche4d6b792007-09-18 15:39:42 -0400985
gregor kowski035d0242009-08-19 22:35:45 +0200986 /* For ALG_TKIP the key is encoded as a 256-bit (32 byte) data block:
987 * - Temporal Encryption Key (128 bits)
988 * - Temporal Authenticator Tx MIC Key (64 bits)
989 * - Temporal Authenticator Rx MIC Key (64 bits)
990 *
991 * Hardware only store TEK
992 */
993 if (algorithm == B43_SEC_ALGO_TKIP && key_len == 32)
994 key_len = 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400995 if (key_len > B43_SEC_KEYSIZE)
996 return -EINVAL;
Michael Buesch66d2d082009-08-06 10:36:50 +0200997 for (i = 0; i < ARRAY_SIZE(dev->key); i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400998 /* Check that we don't already have this key. */
999 B43_WARN_ON(dev->key[i].keyconf == keyconf);
1000 }
1001 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001002 /* Pairwise key. Get an empty slot for the key. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001003 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +02001004 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -04001005 else
Michael Buesch66d2d082009-08-06 10:36:50 +02001006 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1007 for (i = pairwise_keys_start;
1008 i < pairwise_keys_start + B43_NR_PAIRWISE_KEYS;
1009 i++) {
1010 B43_WARN_ON(i >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -04001011 if (!dev->key[i].keyconf) {
1012 /* found empty */
1013 index = i;
1014 break;
1015 }
1016 }
1017 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001018 b43warn(dev->wl, "Out of hardware key memory\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04001019 return -ENOSPC;
1020 }
1021 } else
1022 B43_WARN_ON(index > 3);
1023
1024 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
1025 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1026 /* Default RX key */
1027 B43_WARN_ON(mac_addr);
1028 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
1029 }
1030 keyconf->hw_key_idx = index;
1031 dev->key[index].keyconf = keyconf;
1032
1033 return 0;
1034}
1035
1036static int b43_key_clear(struct b43_wldev *dev, int index)
1037{
Michael Buesch66d2d082009-08-06 10:36:50 +02001038 if (B43_WARN_ON((index < 0) || (index >= ARRAY_SIZE(dev->key))))
Michael Buesche4d6b792007-09-18 15:39:42 -04001039 return -EINVAL;
1040 do_key_write(dev, index, B43_SEC_ALGO_NONE,
1041 NULL, B43_SEC_KEYSIZE, NULL);
1042 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1043 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
1044 NULL, B43_SEC_KEYSIZE, NULL);
1045 }
1046 dev->key[index].keyconf = NULL;
1047
1048 return 0;
1049}
1050
1051static void b43_clear_keys(struct b43_wldev *dev)
1052{
Michael Buesch66d2d082009-08-06 10:36:50 +02001053 int i, count;
Michael Buesche4d6b792007-09-18 15:39:42 -04001054
Michael Buesch66d2d082009-08-06 10:36:50 +02001055 if (b43_new_kidx_api(dev))
1056 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1057 else
1058 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1059 for (i = 0; i < count; i++)
Michael Buesche4d6b792007-09-18 15:39:42 -04001060 b43_key_clear(dev, i);
1061}
1062
Michael Buesch9cf7f242008-12-19 20:24:30 +01001063static void b43_dump_keymemory(struct b43_wldev *dev)
1064{
Michael Buesch66d2d082009-08-06 10:36:50 +02001065 unsigned int i, index, count, offset, pairwise_keys_start;
Michael Buesch9cf7f242008-12-19 20:24:30 +01001066 u8 mac[ETH_ALEN];
1067 u16 algo;
1068 u32 rcmta0;
1069 u16 rcmta1;
1070 u64 hf;
1071 struct b43_key *key;
1072
1073 if (!b43_debug(dev, B43_DBG_KEYS))
1074 return;
1075
1076 hf = b43_hf_read(dev);
1077 b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
1078 !!(hf & B43_HF_USEDEFKEYS));
Michael Buesch66d2d082009-08-06 10:36:50 +02001079 if (b43_new_kidx_api(dev)) {
1080 pairwise_keys_start = B43_NR_GROUP_KEYS;
1081 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1082 } else {
1083 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1084 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1085 }
1086 for (index = 0; index < count; index++) {
Michael Buesch9cf7f242008-12-19 20:24:30 +01001087 key = &(dev->key[index]);
1088 printk(KERN_DEBUG "Key slot %02u: %s",
1089 index, (key->keyconf == NULL) ? " " : "*");
1090 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
1091 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
1092 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1093 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1094 }
1095
1096 algo = b43_shm_read16(dev, B43_SHM_SHARED,
1097 B43_SHM_SH_KEYIDXBLOCK + (index * 2));
1098 printk(" Algo: %04X/%02X", algo, key->algorithm);
1099
Michael Buesch66d2d082009-08-06 10:36:50 +02001100 if (index >= pairwise_keys_start) {
gregor kowski035d0242009-08-19 22:35:45 +02001101 if (key->algorithm == B43_SEC_ALGO_TKIP) {
1102 printk(" TKIP: ");
1103 offset = B43_SHM_SH_TKIPTSCTTAK + (index - 4) * (10 + 4);
1104 for (i = 0; i < 14; i += 2) {
1105 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1106 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1107 }
1108 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01001109 rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001110 ((index - pairwise_keys_start) * 2) + 0);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001111 rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001112 ((index - pairwise_keys_start) * 2) + 1);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001113 *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
1114 *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
Johannes Berge91d8332009-07-15 17:21:41 +02001115 printk(" MAC: %pM", mac);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001116 } else
1117 printk(" DEFAULT KEY");
1118 printk("\n");
1119 }
1120}
1121
Michael Buesche4d6b792007-09-18 15:39:42 -04001122void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1123{
1124 u32 macctl;
1125 u16 ucstat;
1126 bool hwps;
1127 bool awake;
1128 int i;
1129
1130 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1131 (ps_flags & B43_PS_DISABLED));
1132 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1133
1134 if (ps_flags & B43_PS_ENABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001135 hwps = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001136 } else if (ps_flags & B43_PS_DISABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001137 hwps = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001138 } else {
1139 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1140 // and thus is not an AP and we are associated, set bit 25
1141 }
1142 if (ps_flags & B43_PS_AWAKE) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001143 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001144 } else if (ps_flags & B43_PS_ASLEEP) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001145 awake = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001146 } else {
1147 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1148 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1149 // successful, set bit26
1150 }
1151
1152/* FIXME: For now we force awake-on and hwps-off */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001153 hwps = false;
1154 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001155
1156 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1157 if (hwps)
1158 macctl |= B43_MACCTL_HWPS;
1159 else
1160 macctl &= ~B43_MACCTL_HWPS;
1161 if (awake)
1162 macctl |= B43_MACCTL_AWAKE;
1163 else
1164 macctl &= ~B43_MACCTL_AWAKE;
1165 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1166 /* Commit write */
1167 b43_read32(dev, B43_MMIO_MACCTL);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001168 if (awake && dev->dev->core_rev >= 5) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001169 /* Wait for the microcode to wake up. */
1170 for (i = 0; i < 100; i++) {
1171 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1172 B43_SHM_SH_UCODESTAT);
1173 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1174 break;
1175 udelay(10);
1176 }
1177 }
1178}
1179
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001180#ifdef CONFIG_B43_BCMA
Rafał Miłecki49173592011-07-17 01:06:06 +02001181static void b43_bcma_phy_reset(struct b43_wldev *dev)
1182{
1183 u32 flags;
1184
1185 /* Put PHY into reset */
1186 flags = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1187 flags |= B43_BCMA_IOCTL_PHY_RESET;
1188 flags |= B43_BCMA_IOCTL_PHY_BW_20MHZ; /* Make 20 MHz def */
1189 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, flags);
1190 udelay(2);
1191
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001192 b43_phy_take_out_of_reset(dev);
Rafał Miłecki49173592011-07-17 01:06:06 +02001193}
1194
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001195static void b43_bcma_wireless_core_reset(struct b43_wldev *dev, bool gmode)
1196{
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001197 u32 req = B43_BCMA_CLKCTLST_80211_PLL_REQ |
1198 B43_BCMA_CLKCTLST_PHY_PLL_REQ;
1199 u32 status = B43_BCMA_CLKCTLST_80211_PLL_ST |
1200 B43_BCMA_CLKCTLST_PHY_PLL_ST;
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001201 u32 flags;
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001202
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001203 flags = B43_BCMA_IOCTL_PHY_CLKEN;
1204 if (gmode)
1205 flags |= B43_BCMA_IOCTL_GMODE;
1206 b43_device_enable(dev, flags);
1207
Rafał Miłecki49173592011-07-17 01:06:06 +02001208 bcma_core_set_clockmode(dev->dev->bdev, BCMA_CLKMODE_FAST);
1209 b43_bcma_phy_reset(dev);
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001210 bcma_core_pll_ctl(dev->dev->bdev, req, status, true);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001211}
1212#endif
1213
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001214#ifdef CONFIG_B43_SSB
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001215static void b43_ssb_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001216{
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001217 u32 flags = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04001218
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001219 if (gmode)
1220 flags |= B43_TMSLOW_GMODE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001221 flags |= B43_TMSLOW_PHYCLKEN;
1222 flags |= B43_TMSLOW_PHYRESET;
Rafał Miłecki42ab1352010-12-09 20:56:01 +01001223 if (dev->phy.type == B43_PHYTYPE_N)
1224 flags |= B43_TMSLOW_PHY_BANDWIDTH_20MHZ; /* Make 20 MHz def */
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02001225 b43_device_enable(dev, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -04001226 msleep(2); /* Wait for the PLL to turn on. */
1227
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001228 b43_phy_take_out_of_reset(dev);
Rafał Miłecki14952982011-05-17 18:57:28 +02001229}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001230#endif
Rafał Miłecki14952982011-05-17 18:57:28 +02001231
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001232void b43_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Rafał Miłecki14952982011-05-17 18:57:28 +02001233{
1234 u32 macctl;
1235
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001236 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001237#ifdef CONFIG_B43_BCMA
1238 case B43_BUS_BCMA:
1239 b43_bcma_wireless_core_reset(dev, gmode);
1240 break;
1241#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001242#ifdef CONFIG_B43_SSB
1243 case B43_BUS_SSB:
1244 b43_ssb_wireless_core_reset(dev, gmode);
1245 break;
1246#endif
1247 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001248
Michael Bueschfb111372008-09-02 13:00:34 +02001249 /* Turn Analog ON, but only if we already know the PHY-type.
1250 * This protects against very early setup where we don't know the
1251 * PHY-type, yet. wireless_core_reset will be called once again later,
1252 * when we know the PHY-type. */
1253 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001254 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001255
1256 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1257 macctl &= ~B43_MACCTL_GMODE;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001258 if (gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001259 macctl |= B43_MACCTL_GMODE;
1260 macctl |= B43_MACCTL_IHR_ENABLED;
1261 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1262}
1263
1264static void handle_irq_transmit_status(struct b43_wldev *dev)
1265{
1266 u32 v0, v1;
1267 u16 tmp;
1268 struct b43_txstatus stat;
1269
1270 while (1) {
1271 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1272 if (!(v0 & 0x00000001))
1273 break;
1274 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1275
1276 stat.cookie = (v0 >> 16);
1277 stat.seq = (v1 & 0x0000FFFF);
1278 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1279 tmp = (v0 & 0x0000FFFF);
1280 stat.frame_count = ((tmp & 0xF000) >> 12);
1281 stat.rts_count = ((tmp & 0x0F00) >> 8);
1282 stat.supp_reason = ((tmp & 0x001C) >> 2);
1283 stat.pm_indicated = !!(tmp & 0x0080);
1284 stat.intermediate = !!(tmp & 0x0040);
1285 stat.for_ampdu = !!(tmp & 0x0020);
1286 stat.acked = !!(tmp & 0x0002);
1287
1288 b43_handle_txstatus(dev, &stat);
1289 }
1290}
1291
1292static void drain_txstatus_queue(struct b43_wldev *dev)
1293{
1294 u32 dummy;
1295
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001296 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04001297 return;
1298 /* Read all entries from the microcode TXstatus FIFO
1299 * and throw them away.
1300 */
1301 while (1) {
1302 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1303 if (!(dummy & 0x00000001))
1304 break;
1305 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1306 }
1307}
1308
1309static u32 b43_jssi_read(struct b43_wldev *dev)
1310{
1311 u32 val = 0;
1312
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001313 val = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001314 val <<= 16;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001315 val |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001316
1317 return val;
1318}
1319
1320static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1321{
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001322 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0,
1323 (jssi & 0x0000FFFF));
1324 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1,
1325 (jssi & 0xFFFF0000) >> 16);
Michael Buesche4d6b792007-09-18 15:39:42 -04001326}
1327
1328static void b43_generate_noise_sample(struct b43_wldev *dev)
1329{
1330 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001331 b43_write32(dev, B43_MMIO_MACCMD,
1332 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001333}
1334
1335static void b43_calculate_link_quality(struct b43_wldev *dev)
1336{
1337 /* Top half of Link Quality calculation. */
1338
Michael Bueschef1a6282008-08-27 18:53:02 +02001339 if (dev->phy.type != B43_PHYTYPE_G)
1340 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001341 if (dev->noisecalc.calculation_running)
1342 return;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001343 dev->noisecalc.calculation_running = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001344 dev->noisecalc.nr_samples = 0;
1345
1346 b43_generate_noise_sample(dev);
1347}
1348
1349static void handle_irq_noise(struct b43_wldev *dev)
1350{
Michael Bueschef1a6282008-08-27 18:53:02 +02001351 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001352 u16 tmp;
1353 u8 noise[4];
1354 u8 i, j;
1355 s32 average;
1356
1357 /* Bottom half of Link Quality calculation. */
1358
Michael Bueschef1a6282008-08-27 18:53:02 +02001359 if (dev->phy.type != B43_PHYTYPE_G)
1360 return;
1361
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001362 /* Possible race condition: It might be possible that the user
1363 * changed to a different channel in the meantime since we
1364 * started the calculation. We ignore that fact, since it's
1365 * not really that much of a problem. The background noise is
1366 * an estimation only anyway. Slightly wrong results will get damped
1367 * by the averaging of the 8 sample rounds. Additionally the
1368 * value is shortlived. So it will be replaced by the next noise
1369 * calculation round soon. */
1370
Michael Buesche4d6b792007-09-18 15:39:42 -04001371 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001372 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001373 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1374 noise[2] == 0x7F || noise[3] == 0x7F)
1375 goto generate_new;
1376
1377 /* Get the noise samples. */
1378 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1379 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001380 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1381 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1382 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1383 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001384 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1385 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1386 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1387 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1388 dev->noisecalc.nr_samples++;
1389 if (dev->noisecalc.nr_samples == 8) {
1390 /* Calculate the Link Quality by the noise samples. */
1391 average = 0;
1392 for (i = 0; i < 8; i++) {
1393 for (j = 0; j < 4; j++)
1394 average += dev->noisecalc.samples[i][j];
1395 }
1396 average /= (8 * 4);
1397 average *= 125;
1398 average += 64;
1399 average /= 128;
1400 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1401 tmp = (tmp / 128) & 0x1F;
1402 if (tmp >= 8)
1403 average += 2;
1404 else
1405 average -= 25;
1406 if (tmp == 8)
1407 average -= 72;
1408 else
1409 average -= 48;
1410
1411 dev->stats.link_noise = average;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001412 dev->noisecalc.calculation_running = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001413 return;
1414 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001415generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001416 b43_generate_noise_sample(dev);
1417}
1418
1419static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1420{
Johannes Berg05c914f2008-09-11 00:01:58 +02001421 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001422 ///TODO: PS TBTT
1423 } else {
1424 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1425 b43_power_saving_ctl_bits(dev, 0);
1426 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001427 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001428 dev->dfq_valid = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001429}
1430
1431static void handle_irq_atim_end(struct b43_wldev *dev)
1432{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001433 if (dev->dfq_valid) {
1434 b43_write32(dev, B43_MMIO_MACCMD,
1435 b43_read32(dev, B43_MMIO_MACCMD)
1436 | B43_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001437 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001438 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001439}
1440
1441static void handle_irq_pmq(struct b43_wldev *dev)
1442{
1443 u32 tmp;
1444
1445 //TODO: AP mode.
1446
1447 while (1) {
1448 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1449 if (!(tmp & 0x00000008))
1450 break;
1451 }
1452 /* 16bit write is odd, but correct. */
1453 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1454}
1455
1456static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001457 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001458 u16 ram_offset,
1459 u16 shm_size_offset, u8 rate)
1460{
1461 u32 i, tmp;
1462 struct b43_plcp_hdr4 plcp;
1463
1464 plcp.data = 0;
1465 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1466 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1467 ram_offset += sizeof(u32);
1468 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1469 * So leave the first two bytes of the next write blank.
1470 */
1471 tmp = (u32) (data[0]) << 16;
1472 tmp |= (u32) (data[1]) << 24;
1473 b43_ram_write(dev, ram_offset, tmp);
1474 ram_offset += sizeof(u32);
1475 for (i = 2; i < size; i += sizeof(u32)) {
1476 tmp = (u32) (data[i + 0]);
1477 if (i + 1 < size)
1478 tmp |= (u32) (data[i + 1]) << 8;
1479 if (i + 2 < size)
1480 tmp |= (u32) (data[i + 2]) << 16;
1481 if (i + 3 < size)
1482 tmp |= (u32) (data[i + 3]) << 24;
1483 b43_ram_write(dev, ram_offset + i - 2, tmp);
1484 }
1485 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1486 size + sizeof(struct b43_plcp_hdr6));
1487}
1488
Michael Buesch5042c502008-04-05 15:05:00 +02001489/* Check if the use of the antenna that ieee80211 told us to
1490 * use is possible. This will fall back to DEFAULT.
1491 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1492u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1493 u8 antenna_nr)
1494{
1495 u8 antenna_mask;
1496
1497 if (antenna_nr == 0) {
1498 /* Zero means "use default antenna". That's always OK. */
1499 return 0;
1500 }
1501
1502 /* Get the mask of available antennas. */
1503 if (dev->phy.gmode)
Rafał Miłecki05814832011-05-18 02:06:39 +02001504 antenna_mask = dev->dev->bus_sprom->ant_available_bg;
Michael Buesch5042c502008-04-05 15:05:00 +02001505 else
Rafał Miłecki05814832011-05-18 02:06:39 +02001506 antenna_mask = dev->dev->bus_sprom->ant_available_a;
Michael Buesch5042c502008-04-05 15:05:00 +02001507
1508 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1509 /* This antenna is not available. Fall back to default. */
1510 return 0;
1511 }
1512
1513 return antenna_nr;
1514}
1515
Michael Buesch5042c502008-04-05 15:05:00 +02001516/* Convert a b43 antenna number value to the PHY TX control value. */
1517static u16 b43_antenna_to_phyctl(int antenna)
1518{
1519 switch (antenna) {
1520 case B43_ANTENNA0:
1521 return B43_TXH_PHY_ANT0;
1522 case B43_ANTENNA1:
1523 return B43_TXH_PHY_ANT1;
1524 case B43_ANTENNA2:
1525 return B43_TXH_PHY_ANT2;
1526 case B43_ANTENNA3:
1527 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001528 case B43_ANTENNA_AUTO0:
1529 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001530 return B43_TXH_PHY_ANT01AUTO;
1531 }
1532 B43_WARN_ON(1);
1533 return 0;
1534}
1535
Michael Buesche4d6b792007-09-18 15:39:42 -04001536static void b43_write_beacon_template(struct b43_wldev *dev,
1537 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001538 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001539{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001540 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001541 const struct ieee80211_mgmt *bcn;
1542 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001543 bool tim_found = false;
Michael Buesch5042c502008-04-05 15:05:00 +02001544 unsigned int rate;
1545 u16 ctl;
1546 int antenna;
Johannes Berge039fa42008-05-15 12:55:29 +02001547 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001548
Michael Buesche66fee62007-12-26 17:47:10 +01001549 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
Silvan Jegenc8e49552014-02-25 18:12:52 +01001550 len = min_t(size_t, dev->wl->current_beacon->len,
Michael Buesche4d6b792007-09-18 15:39:42 -04001551 0x200 - sizeof(struct b43_plcp_hdr6));
Johannes Berge039fa42008-05-15 12:55:29 +02001552 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Buesche66fee62007-12-26 17:47:10 +01001553
1554 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001555 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001556
Michael Buesch5042c502008-04-05 15:05:00 +02001557 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001558 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001559 antenna = b43_antenna_to_phyctl(antenna);
1560 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1561 /* We can't send beacons with short preamble. Would get PHY errors. */
1562 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1563 ctl &= ~B43_TXH_PHY_ANT;
1564 ctl &= ~B43_TXH_PHY_ENC;
1565 ctl |= antenna;
1566 if (b43_is_cck_rate(rate))
1567 ctl |= B43_TXH_PHY_ENC_CCK;
1568 else
1569 ctl |= B43_TXH_PHY_ENC_OFDM;
1570 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1571
Michael Buesche66fee62007-12-26 17:47:10 +01001572 /* Find the position of the TIM and the DTIM_period value
1573 * and write them to SHM. */
1574 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001575 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1576 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001577 uint8_t ie_id, ie_len;
1578
1579 ie_id = ie[i];
1580 ie_len = ie[i + 1];
1581 if (ie_id == 5) {
1582 u16 tim_position;
1583 u16 dtim_period;
1584 /* This is the TIM Information Element */
1585
1586 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001587 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001588 break;
1589 /* A valid TIM is at least 4 bytes long. */
1590 if (ie_len < 4)
1591 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001592 tim_found = true;
Michael Buesche66fee62007-12-26 17:47:10 +01001593
1594 tim_position = sizeof(struct b43_plcp_hdr6);
1595 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1596 tim_position += i;
1597
1598 dtim_period = ie[i + 3];
1599
1600 b43_shm_write16(dev, B43_SHM_SHARED,
1601 B43_SHM_SH_TIMBPOS, tim_position);
1602 b43_shm_write16(dev, B43_SHM_SHARED,
1603 B43_SHM_SH_DTIMPER, dtim_period);
1604 break;
1605 }
1606 i += ie_len + 2;
1607 }
1608 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001609 /*
1610 * If ucode wants to modify TIM do it behind the beacon, this
1611 * will happen, for example, when doing mesh networking.
1612 */
1613 b43_shm_write16(dev, B43_SHM_SHARED,
1614 B43_SHM_SH_TIMBPOS,
1615 len + sizeof(struct b43_plcp_hdr6));
1616 b43_shm_write16(dev, B43_SHM_SHARED,
1617 B43_SHM_SH_DTIMPER, 0);
1618 }
1619 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Buesche4d6b792007-09-18 15:39:42 -04001620}
1621
Michael Buesch6b4bec012008-05-20 12:16:28 +02001622static void b43_upload_beacon0(struct b43_wldev *dev)
1623{
1624 struct b43_wl *wl = dev->wl;
1625
1626 if (wl->beacon0_uploaded)
1627 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001628 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE0, B43_SHM_SH_BTL0);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001629 wl->beacon0_uploaded = true;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001630}
1631
1632static void b43_upload_beacon1(struct b43_wldev *dev)
1633{
1634 struct b43_wl *wl = dev->wl;
1635
1636 if (wl->beacon1_uploaded)
1637 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001638 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE1, B43_SHM_SH_BTL1);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001639 wl->beacon1_uploaded = true;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001640}
1641
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001642static void handle_irq_beacon(struct b43_wldev *dev)
1643{
1644 struct b43_wl *wl = dev->wl;
1645 u32 cmd, beacon0_valid, beacon1_valid;
1646
Johannes Berg05c914f2008-09-11 00:01:58 +02001647 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
Manual Munz8c235162011-09-18 18:24:03 -05001648 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) &&
1649 !b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001650 return;
1651
1652 /* This is the bottom half of the asynchronous beacon update. */
1653
1654 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001655 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001656
1657 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1658 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1659 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1660
1661 /* Schedule interrupt manually, if busy. */
1662 if (beacon0_valid && beacon1_valid) {
1663 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001664 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001665 return;
1666 }
1667
Michael Buesch6b4bec012008-05-20 12:16:28 +02001668 if (unlikely(wl->beacon_templates_virgin)) {
1669 /* We never uploaded a beacon before.
1670 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001671 wl->beacon_templates_virgin = false;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001672 b43_upload_beacon0(dev);
1673 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001674 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1675 cmd |= B43_MACCMD_BEACON0_VALID;
1676 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec012008-05-20 12:16:28 +02001677 } else {
1678 if (!beacon0_valid) {
1679 b43_upload_beacon0(dev);
1680 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1681 cmd |= B43_MACCMD_BEACON0_VALID;
1682 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1683 } else if (!beacon1_valid) {
1684 b43_upload_beacon1(dev);
1685 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1686 cmd |= B43_MACCMD_BEACON1_VALID;
1687 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001688 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001689 }
1690}
1691
Michael Buesch36dbd952009-09-04 22:51:29 +02001692static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1693{
1694 u32 old_irq_mask = dev->irq_mask;
1695
1696 /* update beacon right away or defer to irq */
1697 handle_irq_beacon(dev);
1698 if (old_irq_mask != dev->irq_mask) {
1699 /* The handler updated the IRQ mask. */
1700 B43_WARN_ON(!dev->irq_mask);
1701 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1702 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1703 } else {
1704 /* Device interrupts are currently disabled. That means
1705 * we just ran the hardirq handler and scheduled the
1706 * IRQ thread. The thread will write the IRQ mask when
1707 * it finished, so there's nothing to do here. Writing
1708 * the mask _here_ would incorrectly re-enable IRQs. */
1709 }
1710 }
1711}
1712
Michael Buescha82d9922008-04-04 21:40:06 +02001713static void b43_beacon_update_trigger_work(struct work_struct *work)
1714{
1715 struct b43_wl *wl = container_of(work, struct b43_wl,
1716 beacon_update_trigger);
1717 struct b43_wldev *dev;
1718
1719 mutex_lock(&wl->mutex);
1720 dev = wl->current_dev;
1721 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Rafał Miłecki505fb012011-05-19 15:11:27 +02001722 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001723 /* wl->mutex is enough. */
1724 b43_do_beacon_update_trigger_work(dev);
1725 mmiowb();
1726 } else {
1727 spin_lock_irq(&wl->hardirq_lock);
1728 b43_do_beacon_update_trigger_work(dev);
1729 mmiowb();
1730 spin_unlock_irq(&wl->hardirq_lock);
1731 }
Michael Buescha82d9922008-04-04 21:40:06 +02001732 }
1733 mutex_unlock(&wl->mutex);
1734}
1735
Michael Bueschd4df6f12007-12-26 18:04:14 +01001736/* Asynchronously update the packet templates in template RAM.
Michael Buesch36dbd952009-09-04 22:51:29 +02001737 * Locking: Requires wl->mutex to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001738static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001739{
Johannes Berg9d139c82008-07-09 14:40:37 +02001740 struct sk_buff *beacon;
1741
Michael Buesche66fee62007-12-26 17:47:10 +01001742 /* This is the top half of the ansynchronous beacon update.
1743 * The bottom half is the beacon IRQ.
1744 * Beacon update must be asynchronous to avoid sending an
1745 * invalid beacon. This can happen for example, if the firmware
1746 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001747
Johannes Berg9d139c82008-07-09 14:40:37 +02001748 /* We could modify the existing beacon and set the aid bit in
1749 * the TIM field, but that would probably require resizing and
1750 * moving of data within the beacon template.
1751 * Simply request a new beacon and let mac80211 do the hard work. */
1752 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1753 if (unlikely(!beacon))
1754 return;
1755
Michael Buesche66fee62007-12-26 17:47:10 +01001756 if (wl->current_beacon)
1757 dev_kfree_skb_any(wl->current_beacon);
1758 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001759 wl->beacon0_uploaded = false;
1760 wl->beacon1_uploaded = false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001761 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Buesche4d6b792007-09-18 15:39:42 -04001762}
1763
Michael Buesche4d6b792007-09-18 15:39:42 -04001764static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1765{
1766 b43_time_lock(dev);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001767 if (dev->dev->core_rev >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001768 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1769 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001770 } else {
1771 b43_write16(dev, 0x606, (beacon_int >> 6));
1772 b43_write16(dev, 0x610, beacon_int);
1773 }
1774 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001775 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001776}
1777
Michael Bueschafa83e22008-05-19 23:51:37 +02001778static void b43_handle_firmware_panic(struct b43_wldev *dev)
1779{
1780 u16 reason;
1781
1782 /* Read the register that contains the reason code for the panic. */
1783 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1784 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1785
1786 switch (reason) {
1787 default:
1788 b43dbg(dev->wl, "The panic reason is unknown.\n");
1789 /* fallthrough */
1790 case B43_FWPANIC_DIE:
1791 /* Do not restart the controller or firmware.
1792 * The device is nonfunctional from now on.
1793 * Restarting would result in this panic to trigger again,
1794 * so we avoid that recursion. */
1795 break;
1796 case B43_FWPANIC_RESTART:
1797 b43_controller_restart(dev, "Microcode panic");
1798 break;
1799 }
1800}
1801
Michael Buesche4d6b792007-09-18 15:39:42 -04001802static void handle_irq_ucode_debug(struct b43_wldev *dev)
1803{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001804 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001805 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001806 __le16 *buf;
1807
1808 /* The proprietary firmware doesn't have this IRQ. */
1809 if (!dev->fw.opensource)
1810 return;
1811
Michael Bueschafa83e22008-05-19 23:51:37 +02001812 /* Read the register that contains the reason code for this IRQ. */
1813 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1814
Michael Buesche48b0ee2008-05-17 22:44:35 +02001815 switch (reason) {
1816 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001817 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001818 break;
1819 case B43_DEBUGIRQ_DUMP_SHM:
1820 if (!B43_DEBUG)
1821 break; /* Only with driver debugging enabled. */
1822 buf = kmalloc(4096, GFP_ATOMIC);
1823 if (!buf) {
1824 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1825 goto out;
1826 }
1827 for (i = 0; i < 4096; i += 2) {
1828 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1829 buf[i / 2] = cpu_to_le16(tmp);
1830 }
1831 b43info(dev->wl, "Shared memory dump:\n");
1832 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1833 16, 2, buf, 4096, 1);
1834 kfree(buf);
1835 break;
1836 case B43_DEBUGIRQ_DUMP_REGS:
1837 if (!B43_DEBUG)
1838 break; /* Only with driver debugging enabled. */
1839 b43info(dev->wl, "Microcode register dump:\n");
1840 for (i = 0, cnt = 0; i < 64; i++) {
1841 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1842 if (cnt == 0)
1843 printk(KERN_INFO);
1844 printk("r%02u: 0x%04X ", i, tmp);
1845 cnt++;
1846 if (cnt == 6) {
1847 printk("\n");
1848 cnt = 0;
1849 }
1850 }
1851 printk("\n");
1852 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001853 case B43_DEBUGIRQ_MARKER:
1854 if (!B43_DEBUG)
1855 break; /* Only with driver debugging enabled. */
1856 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1857 B43_MARKER_ID_REG);
1858 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1859 B43_MARKER_LINE_REG);
1860 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1861 "at line number %u\n",
1862 marker_id, marker_line);
1863 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001864 default:
1865 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1866 reason);
1867 }
1868out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001869 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1870 b43_shm_write16(dev, B43_SHM_SCRATCH,
1871 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001872}
1873
Michael Buesch36dbd952009-09-04 22:51:29 +02001874static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001875{
1876 u32 reason;
1877 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1878 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001879 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001880
Michael Buesch36dbd952009-09-04 22:51:29 +02001881 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1882 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001883
1884 reason = dev->irq_reason;
1885 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1886 dma_reason[i] = dev->dma_reason[i];
1887 merged_dma_reason |= dma_reason[i];
1888 }
1889
1890 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1891 b43err(dev->wl, "MAC transmission error\n");
1892
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001893 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001894 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001895 rmb();
1896 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1897 atomic_set(&dev->phy.txerr_cnt,
1898 B43_PHY_TX_BADNESS_LIMIT);
1899 b43err(dev->wl, "Too many PHY TX errors, "
1900 "restarting the controller\n");
1901 b43_controller_restart(dev, "PHY TX errors");
1902 }
1903 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001904
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001905 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK))) {
1906 b43err(dev->wl,
1907 "Fatal DMA error: 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X\n",
1908 dma_reason[0], dma_reason[1],
1909 dma_reason[2], dma_reason[3],
1910 dma_reason[4], dma_reason[5]);
1911 b43err(dev->wl, "This device does not support DMA "
Larry Fingerbb64d952010-06-19 08:29:08 -05001912 "on your system. It will now be switched to PIO.\n");
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001913 /* Fall back to PIO transfers if we get fatal DMA errors! */
1914 dev->use_pio = true;
1915 b43_controller_restart(dev, "DMA error");
1916 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001917 }
1918
1919 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1920 handle_irq_ucode_debug(dev);
1921 if (reason & B43_IRQ_TBTT_INDI)
1922 handle_irq_tbtt_indication(dev);
1923 if (reason & B43_IRQ_ATIM_END)
1924 handle_irq_atim_end(dev);
1925 if (reason & B43_IRQ_BEACON)
1926 handle_irq_beacon(dev);
1927 if (reason & B43_IRQ_PMQ)
1928 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02001929 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1930 ;/* TODO */
1931 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001932 handle_irq_noise(dev);
1933
1934 /* Check the DMA reason registers for received data. */
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001935 if (dma_reason[0] & B43_DMAIRQ_RDESC_UFLOW) {
1936 if (B43_DEBUG)
1937 b43warn(dev->wl, "RX descriptor underrun\n");
1938 b43_dma_handle_rx_overflow(dev->dma.rx_ring);
1939 }
Michael Buesch5100d5a2008-03-29 21:01:16 +01001940 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1941 if (b43_using_pio_transfers(dev))
1942 b43_pio_rx(dev->pio.rx_queue);
1943 else
1944 b43_dma_rx(dev->dma.rx_ring);
1945 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001946 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
1947 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01001948 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001949 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
1950 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
1951
Michael Buesch21954c32007-09-27 15:31:40 +02001952 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001953 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04001954
Michael Buesch36dbd952009-09-04 22:51:29 +02001955 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02001956 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesch990b86f2009-09-12 00:48:03 +02001957
1958#if B43_DEBUG
1959 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
1960 dev->irq_count++;
1961 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
1962 if (reason & (1 << i))
1963 dev->irq_bit_count[i]++;
1964 }
1965 }
1966#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04001967}
1968
Michael Buesch36dbd952009-09-04 22:51:29 +02001969/* Interrupt thread handler. Handles device interrupts in thread context. */
1970static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04001971{
Michael Buesche4d6b792007-09-18 15:39:42 -04001972 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02001973
1974 mutex_lock(&dev->wl->mutex);
1975 b43_do_interrupt_thread(dev);
1976 mmiowb();
1977 mutex_unlock(&dev->wl->mutex);
1978
1979 return IRQ_HANDLED;
1980}
1981
1982static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
1983{
Michael Buesche4d6b792007-09-18 15:39:42 -04001984 u32 reason;
1985
Michael Buesch36dbd952009-09-04 22:51:29 +02001986 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
1987 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001988
Michael Buesche4d6b792007-09-18 15:39:42 -04001989 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1990 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02001991 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02001992 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04001993 if (!reason)
Sebastian Andrzej Siewiorcae56142011-07-07 21:58:10 +02001994 return IRQ_NONE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001995
1996 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001997 & 0x0001FC00;
Michael Buesche4d6b792007-09-18 15:39:42 -04001998 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
1999 & 0x0000DC00;
2000 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
2001 & 0x0000DC00;
2002 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
2003 & 0x0001DC00;
2004 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
2005 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002006/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04002007 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
2008 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002009*/
Michael Buesche4d6b792007-09-18 15:39:42 -04002010
Michael Buesch36dbd952009-09-04 22:51:29 +02002011 /* ACK the interrupt. */
2012 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
2013 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
2014 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
2015 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
2016 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
2017 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
2018/* Unused ring
2019 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
2020*/
2021
2022 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02002023 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02002024 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002025 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02002026
2027 return IRQ_WAKE_THREAD;
2028}
2029
2030/* Interrupt handler top-half. This runs with interrupts disabled. */
2031static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
2032{
2033 struct b43_wldev *dev = dev_id;
2034 irqreturn_t ret;
2035
2036 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2037 return IRQ_NONE;
2038
2039 spin_lock(&dev->wl->hardirq_lock);
2040 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002041 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02002042 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04002043
2044 return ret;
2045}
2046
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002047/* SDIO interrupt handler. This runs in process context. */
2048static void b43_sdio_interrupt_handler(struct b43_wldev *dev)
2049{
2050 struct b43_wl *wl = dev->wl;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002051 irqreturn_t ret;
2052
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002053 mutex_lock(&wl->mutex);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002054
2055 ret = b43_do_interrupt(dev);
2056 if (ret == IRQ_WAKE_THREAD)
2057 b43_do_interrupt_thread(dev);
2058
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002059 mutex_unlock(&wl->mutex);
2060}
2061
Michael Buesch1a9f5092009-01-23 21:21:51 +01002062void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002063{
2064 release_firmware(fw->data);
2065 fw->data = NULL;
2066 fw->filename = NULL;
2067}
2068
Michael Buesche4d6b792007-09-18 15:39:42 -04002069static void b43_release_firmware(struct b43_wldev *dev)
2070{
Larry Finger0673eff2014-01-12 15:11:38 -06002071 complete(&dev->fw_load_complete);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002072 b43_do_release_fw(&dev->fw.ucode);
2073 b43_do_release_fw(&dev->fw.pcm);
2074 b43_do_release_fw(&dev->fw.initvals);
2075 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04002076}
2077
Michael Buescheb189d8b2008-01-28 14:47:41 -08002078static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04002079{
Hannes Ederfc68ed42009-02-14 11:50:06 +00002080 const char text[] =
2081 "You must go to " \
2082 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
2083 "and download the correct firmware for this driver version. " \
2084 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08002085
Michael Buescheb189d8b2008-01-28 14:47:41 -08002086 if (error)
2087 b43err(wl, text);
2088 else
2089 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04002090}
2091
Larry Finger5e20a4b2012-12-20 15:55:01 -06002092static void b43_fw_cb(const struct firmware *firmware, void *context)
2093{
2094 struct b43_request_fw_context *ctx = context;
2095
2096 ctx->blob = firmware;
Larry Finger0673eff2014-01-12 15:11:38 -06002097 complete(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002098}
2099
Michael Buesch1a9f5092009-01-23 21:21:51 +01002100int b43_do_request_fw(struct b43_request_fw_context *ctx,
2101 const char *name,
Larry Finger5e20a4b2012-12-20 15:55:01 -06002102 struct b43_firmware_file *fw, bool async)
Michael Buesche4d6b792007-09-18 15:39:42 -04002103{
Michael Buesche4d6b792007-09-18 15:39:42 -04002104 struct b43_fw_header *hdr;
2105 u32 size;
2106 int err;
2107
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002108 if (!name) {
2109 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002110 /* FIXME: We should probably keep it anyway, to save some headache
2111 * on suspend/resume with multiband devices. */
2112 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04002113 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002114 }
2115 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002116 if ((fw->type == ctx->req_type) &&
2117 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002118 return 0; /* Already have this fw. */
2119 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002120 /* FIXME: We should probably do this later after we successfully
2121 * got the new fw. This could reduce headache with multiband devices.
2122 * We could also redesign this to cache the firmware for all possible
2123 * bands all the time. */
2124 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002125 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002126
Michael Buesch1a9f5092009-01-23 21:21:51 +01002127 switch (ctx->req_type) {
2128 case B43_FWTYPE_PROPRIETARY:
2129 snprintf(ctx->fwname, sizeof(ctx->fwname),
2130 "b43%s/%s.fw",
2131 modparam_fwpostfix, name);
2132 break;
2133 case B43_FWTYPE_OPENSOURCE:
2134 snprintf(ctx->fwname, sizeof(ctx->fwname),
2135 "b43-open%s/%s.fw",
2136 modparam_fwpostfix, name);
2137 break;
2138 default:
2139 B43_WARN_ON(1);
2140 return -ENOSYS;
2141 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002142 if (async) {
2143 /* do this part asynchronously */
Larry Finger0673eff2014-01-12 15:11:38 -06002144 init_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002145 err = request_firmware_nowait(THIS_MODULE, 1, ctx->fwname,
2146 ctx->dev->dev->dev, GFP_KERNEL,
2147 ctx, b43_fw_cb);
2148 if (err < 0) {
2149 pr_err("Unable to load firmware\n");
2150 return err;
2151 }
Larry Finger0673eff2014-01-12 15:11:38 -06002152 wait_for_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002153 if (ctx->blob)
2154 goto fw_ready;
2155 /* On some ARM systems, the async request will fail, but the next sync
Larry Finger0673eff2014-01-12 15:11:38 -06002156 * request works. For this reason, we fall through here
Larry Finger5e20a4b2012-12-20 15:55:01 -06002157 */
2158 }
2159 err = request_firmware(&ctx->blob, ctx->fwname,
2160 ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002161 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002162 snprintf(ctx->errors[ctx->req_type],
2163 sizeof(ctx->errors[ctx->req_type]),
Larry Finger5e20a4b2012-12-20 15:55:01 -06002164 "Firmware file \"%s\" not found\n",
2165 ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002166 return err;
2167 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002168 snprintf(ctx->errors[ctx->req_type],
2169 sizeof(ctx->errors[ctx->req_type]),
2170 "Firmware file \"%s\" request failed (err=%d)\n",
2171 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002172 return err;
2173 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002174fw_ready:
2175 if (ctx->blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002176 goto err_format;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002177 hdr = (struct b43_fw_header *)(ctx->blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002178 switch (hdr->type) {
2179 case B43_FW_TYPE_UCODE:
2180 case B43_FW_TYPE_PCM:
2181 size = be32_to_cpu(hdr->size);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002182 if (size != ctx->blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002183 goto err_format;
2184 /* fallthrough */
2185 case B43_FW_TYPE_IV:
2186 if (hdr->ver != 1)
2187 goto err_format;
2188 break;
2189 default:
2190 goto err_format;
2191 }
2192
Larry Finger5e20a4b2012-12-20 15:55:01 -06002193 fw->data = ctx->blob;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002194 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002195 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002196
2197 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002198
2199err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002200 snprintf(ctx->errors[ctx->req_type],
2201 sizeof(ctx->errors[ctx->req_type]),
2202 "Firmware file \"%s\" format error.\n", ctx->fwname);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002203 release_firmware(ctx->blob);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002204
Michael Buesche4d6b792007-09-18 15:39:42 -04002205 return -EPROTO;
2206}
2207
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002208/* http://bcm-v4.sipsolutions.net/802.11/Init/Firmware */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002209static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002210{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002211 struct b43_wldev *dev = ctx->dev;
2212 struct b43_firmware *fw = &ctx->dev->fw;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002213 struct b43_phy *phy = &dev->phy;
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002214 const u8 rev = ctx->dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04002215 const char *filename;
Michael Buesche4d6b792007-09-18 15:39:42 -04002216 int err;
2217
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002218 /* Get microcode */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002219 filename = NULL;
2220 switch (rev) {
2221 case 42:
2222 if (phy->type == B43_PHYTYPE_AC)
2223 filename = "ucode42";
2224 break;
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002225 case 40:
2226 if (phy->type == B43_PHYTYPE_AC)
2227 filename = "ucode40";
2228 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002229 case 33:
2230 if (phy->type == B43_PHYTYPE_LCN40)
2231 filename = "ucode33_lcn40";
2232 break;
2233 case 30:
2234 if (phy->type == B43_PHYTYPE_N)
2235 filename = "ucode30_mimo";
2236 break;
2237 case 29:
2238 if (phy->type == B43_PHYTYPE_HT)
2239 filename = "ucode29_mimo";
2240 break;
2241 case 26:
2242 if (phy->type == B43_PHYTYPE_HT)
2243 filename = "ucode26_mimo";
2244 break;
2245 case 28:
2246 case 25:
2247 if (phy->type == B43_PHYTYPE_N)
2248 filename = "ucode25_mimo";
2249 else if (phy->type == B43_PHYTYPE_LCN)
2250 filename = "ucode25_lcn";
2251 break;
2252 case 24:
2253 if (phy->type == B43_PHYTYPE_LCN)
2254 filename = "ucode24_lcn";
2255 break;
2256 case 23:
2257 if (phy->type == B43_PHYTYPE_N)
2258 filename = "ucode16_mimo";
2259 break;
2260 case 16 ... 19:
2261 if (phy->type == B43_PHYTYPE_N)
2262 filename = "ucode16_mimo";
2263 else if (phy->type == B43_PHYTYPE_LP)
2264 filename = "ucode16_lp";
2265 break;
2266 case 15:
Gábor Stefanik759b9732009-08-14 14:39:53 +02002267 filename = "ucode15";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002268 break;
2269 case 14:
2270 filename = "ucode14";
2271 break;
2272 case 13:
2273 filename = "ucode13";
2274 break;
2275 case 11 ... 12:
2276 filename = "ucode11";
2277 break;
2278 case 5 ... 10:
2279 filename = "ucode5";
2280 break;
Rafał Miłecki6ff1e5c2011-07-06 17:41:55 +02002281 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002282 if (!filename)
2283 goto err_no_ucode;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002284 err = b43_do_request_fw(ctx, filename, &fw->ucode, true);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002285 if (err)
2286 goto err_load;
2287
2288 /* Get PCM code */
2289 if ((rev >= 5) && (rev <= 10))
2290 filename = "pcm5";
2291 else if (rev >= 11)
2292 filename = NULL;
2293 else
2294 goto err_no_pcm;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002295 fw->pcm_request_failed = false;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002296 err = b43_do_request_fw(ctx, filename, &fw->pcm, false);
Michael Buesch68217832008-05-17 23:43:57 +02002297 if (err == -ENOENT) {
2298 /* We did not find a PCM file? Not fatal, but
2299 * core rev <= 10 must do without hwcrypto then. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002300 fw->pcm_request_failed = true;
Michael Buesch68217832008-05-17 23:43:57 +02002301 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002302 goto err_load;
2303
2304 /* Get initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002305 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002306 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002307 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002308 if (rev == 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002309 filename = "b0g0initvals13";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002310 else if (rev >= 5 && rev <= 10)
2311 filename = "b0g0initvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002312 break;
2313 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002314 if (rev == 30)
2315 filename = "n16initvals30";
2316 else if (rev == 28 || rev == 25)
2317 filename = "n0initvals25";
2318 else if (rev == 24)
2319 filename = "n0initvals24";
2320 else if (rev == 23)
2321 filename = "n0initvals16"; /* What about n0initvals22? */
2322 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002323 filename = "n0initvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002324 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002325 filename = "n0initvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002326 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002327 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002328 if (rev >= 16 && rev <= 18)
2329 filename = "lp0initvals16";
2330 else if (rev == 15)
2331 filename = "lp0initvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002332 else if (rev == 14)
2333 filename = "lp0initvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002334 else if (rev == 13)
2335 filename = "lp0initvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002336 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002337 case B43_PHYTYPE_HT:
2338 if (rev == 29)
2339 filename = "ht0initvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002340 else if (rev == 26)
2341 filename = "ht0initvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002342 break;
2343 case B43_PHYTYPE_LCN:
2344 if (rev == 24)
2345 filename = "lcn0initvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002346 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002347 case B43_PHYTYPE_LCN40:
2348 if (rev == 33)
2349 filename = "lcn400initvals33";
2350 break;
2351 case B43_PHYTYPE_AC:
2352 if (rev == 42)
2353 filename = "ac1initvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002354 else if (rev == 40)
2355 filename = "ac0initvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002356 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002357 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002358 if (!filename)
2359 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002360 err = b43_do_request_fw(ctx, filename, &fw->initvals, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002361 if (err)
2362 goto err_load;
2363
2364 /* Get bandswitch initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002365 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002366 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002367 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002368 if (rev == 13)
2369 filename = "b0g0bsinitvals13";
2370 else if (rev >= 5 && rev <= 10)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002371 filename = "b0g0bsinitvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002372 break;
2373 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002374 if (rev == 30)
2375 filename = "n16bsinitvals30";
2376 else if (rev == 28 || rev == 25)
2377 filename = "n0bsinitvals25";
2378 else if (rev == 24)
2379 filename = "n0bsinitvals24";
2380 else if (rev == 23)
2381 filename = "n0bsinitvals16"; /* What about n0bsinitvals22? */
2382 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002383 filename = "n0bsinitvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002384 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002385 filename = "n0bsinitvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002386 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002387 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002388 if (rev >= 16 && rev <= 18)
2389 filename = "lp0bsinitvals16";
2390 else if (rev == 15)
2391 filename = "lp0bsinitvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002392 else if (rev == 14)
2393 filename = "lp0bsinitvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002394 else if (rev == 13)
2395 filename = "lp0bsinitvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002396 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002397 case B43_PHYTYPE_HT:
2398 if (rev == 29)
2399 filename = "ht0bsinitvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002400 else if (rev == 26)
2401 filename = "ht0bsinitvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002402 break;
2403 case B43_PHYTYPE_LCN:
2404 if (rev == 24)
2405 filename = "lcn0bsinitvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002406 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002407 case B43_PHYTYPE_LCN40:
2408 if (rev == 33)
2409 filename = "lcn400bsinitvals33";
2410 break;
2411 case B43_PHYTYPE_AC:
2412 if (rev == 42)
2413 filename = "ac1bsinitvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002414 else if (rev == 40)
2415 filename = "ac0bsinitvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002416 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002417 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002418 if (!filename)
2419 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002420 err = b43_do_request_fw(ctx, filename, &fw->initvals_band, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002421 if (err)
2422 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002423
Johannes Berg097b0e12012-07-17 17:12:29 +02002424 fw->opensource = (ctx->req_type == B43_FWTYPE_OPENSOURCE);
2425
Michael Buesche4d6b792007-09-18 15:39:42 -04002426 return 0;
2427
Michael Buesche4d6b792007-09-18 15:39:42 -04002428err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002429 err = ctx->fatal_failure = -EOPNOTSUPP;
2430 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2431 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002432 goto error;
2433
2434err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002435 err = ctx->fatal_failure = -EOPNOTSUPP;
2436 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2437 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002438 goto error;
2439
2440err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002441 err = ctx->fatal_failure = -EOPNOTSUPP;
2442 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2443 "is required for your device (wl-core rev %u)\n", rev);
2444 goto error;
2445
2446err_load:
2447 /* We failed to load this firmware image. The error message
2448 * already is in ctx->errors. Return and let our caller decide
2449 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002450 goto error;
2451
2452error:
2453 b43_release_firmware(dev);
2454 return err;
2455}
2456
Larry Finger6b6fa582012-03-08 22:27:46 -06002457static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl);
2458static void b43_one_core_detach(struct b43_bus_dev *dev);
Larry Finger09164042014-01-12 15:11:37 -06002459static int b43_rng_init(struct b43_wl *wl);
Larry Finger6b6fa582012-03-08 22:27:46 -06002460
2461static void b43_request_firmware(struct work_struct *work)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002462{
Larry Finger6b6fa582012-03-08 22:27:46 -06002463 struct b43_wl *wl = container_of(work,
2464 struct b43_wl, firmware_load);
2465 struct b43_wldev *dev = wl->current_dev;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002466 struct b43_request_fw_context *ctx;
2467 unsigned int i;
2468 int err;
2469 const char *errmsg;
2470
2471 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2472 if (!ctx)
Larry Finger6b6fa582012-03-08 22:27:46 -06002473 return;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002474 ctx->dev = dev;
2475
2476 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2477 err = b43_try_request_fw(ctx);
2478 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002479 goto start_ieee80211; /* Successfully loaded it. */
2480 /* Was fw version known? */
2481 if (ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002482 goto out;
2483
Larry Finger6b6fa582012-03-08 22:27:46 -06002484 /* proprietary fw not found, try open source */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002485 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2486 err = b43_try_request_fw(ctx);
2487 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002488 goto start_ieee80211; /* Successfully loaded it. */
2489 if(ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002490 goto out;
2491
2492 /* Could not find a usable firmware. Print the errors. */
2493 for (i = 0; i < B43_NR_FWTYPES; i++) {
2494 errmsg = ctx->errors[i];
2495 if (strlen(errmsg))
Kees Cooke0e29b62013-05-10 14:48:21 -07002496 b43err(dev->wl, "%s", errmsg);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002497 }
2498 b43_print_fw_helptext(dev->wl, 1);
Larry Finger6b6fa582012-03-08 22:27:46 -06002499 goto out;
2500
2501start_ieee80211:
Johannes Berg097b0e12012-07-17 17:12:29 +02002502 wl->hw->queues = B43_QOS_QUEUE_NUM;
2503 if (!modparam_qos || dev->fw.opensource)
2504 wl->hw->queues = 1;
2505
Larry Finger6b6fa582012-03-08 22:27:46 -06002506 err = ieee80211_register_hw(wl->hw);
2507 if (err)
2508 goto err_one_core_detach;
Oleksij Rempele64add22012-06-05 20:39:32 +02002509 wl->hw_registred = true;
Larry Finger6b6fa582012-03-08 22:27:46 -06002510 b43_leds_register(wl->current_dev);
Larry Finger09164042014-01-12 15:11:37 -06002511
2512 /* Register HW RNG driver */
2513 b43_rng_init(wl);
2514
Larry Finger6b6fa582012-03-08 22:27:46 -06002515 goto out;
2516
2517err_one_core_detach:
2518 b43_one_core_detach(dev->dev);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002519
2520out:
2521 kfree(ctx);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002522}
2523
Michael Buesche4d6b792007-09-18 15:39:42 -04002524static int b43_upload_microcode(struct b43_wldev *dev)
2525{
John W. Linville652caa52010-07-29 13:27:28 -04002526 struct wiphy *wiphy = dev->wl->hw->wiphy;
Michael Buesche4d6b792007-09-18 15:39:42 -04002527 const size_t hdr_len = sizeof(struct b43_fw_header);
2528 const __be32 *data;
2529 unsigned int i, len;
2530 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002531 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002532 int err = 0;
2533
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002534 /* Jump the microcode PSM to offset 0 */
2535 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2536 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2537 macctl |= B43_MACCTL_PSM_JMP0;
2538 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2539 /* Zero out all microcode PSM registers and shared memory. */
2540 for (i = 0; i < 64; i++)
2541 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2542 for (i = 0; i < 4096; i += 2)
2543 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2544
Michael Buesche4d6b792007-09-18 15:39:42 -04002545 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002546 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2547 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002548 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2549 for (i = 0; i < len; i++) {
2550 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2551 udelay(10);
2552 }
2553
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002554 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002555 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002556 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2557 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002558 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2559 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2560 /* No need for autoinc bit in SHM_HW */
2561 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2562 for (i = 0; i < len; i++) {
2563 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2564 udelay(10);
2565 }
2566 }
2567
2568 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002569
2570 /* Start the microcode PSM */
Rafał Miłecki50566352012-01-02 19:31:21 +01002571 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_JMP0,
2572 B43_MACCTL_PSM_RUN);
Michael Buesche4d6b792007-09-18 15:39:42 -04002573
2574 /* Wait for the microcode to load and respond */
2575 i = 0;
2576 while (1) {
2577 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2578 if (tmp == B43_IRQ_MAC_SUSPENDED)
2579 break;
2580 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002581 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002582 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002583 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002584 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002585 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002586 }
Michael Buesche175e992009-09-11 18:31:32 +02002587 msleep(50);
Michael Buesche4d6b792007-09-18 15:39:42 -04002588 }
2589 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2590
2591 /* Get and check the revisions. */
2592 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2593 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2594 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2595 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2596
2597 if (fwrev <= 0x128) {
2598 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2599 "binary drivers older than version 4.x is unsupported. "
2600 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002601 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002602 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002603 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002604 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002605 dev->fw.rev = fwrev;
2606 dev->fw.patch = fwpatch;
Rafał Miłecki5d852902011-08-11 15:07:16 +02002607 if (dev->fw.rev >= 598)
2608 dev->fw.hdr_format = B43_FW_HDR_598;
2609 else if (dev->fw.rev >= 410)
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002610 dev->fw.hdr_format = B43_FW_HDR_410;
2611 else
2612 dev->fw.hdr_format = B43_FW_HDR_351;
Johannes Berg097b0e12012-07-17 17:12:29 +02002613 WARN_ON(dev->fw.opensource != (fwdate == 0xFFFF));
Michael Buesche48b0ee2008-05-17 22:44:35 +02002614
Johannes Berg097b0e12012-07-17 17:12:29 +02002615 dev->qos_enabled = dev->wl->hw->queues > 1;
Michael Buesch403a3a12009-06-08 21:04:57 +02002616 /* Default to firmware/hardware crypto acceleration. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002617 dev->hwcrypto_enabled = true;
Michael Buesch403a3a12009-06-08 21:04:57 +02002618
Michael Buesche48b0ee2008-05-17 22:44:35 +02002619 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002620 u16 fwcapa;
2621
Michael Buesche48b0ee2008-05-17 22:44:35 +02002622 /* Patchlevel info is encoded in the "time" field. */
2623 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002624 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2625 dev->fw.rev, dev->fw.patch);
2626
2627 fwcapa = b43_fwcapa_read(dev);
2628 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2629 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2630 /* Disable hardware crypto and fall back to software crypto. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002631 dev->hwcrypto_enabled = false;
Michael Buesch403a3a12009-06-08 21:04:57 +02002632 }
Johannes Berg097b0e12012-07-17 17:12:29 +02002633 /* adding QoS support should use an offline discovery mechanism */
2634 WARN(fwcapa & B43_FWCAPA_QOS, "QoS in OpenFW not supported\n");
Michael Buesche48b0ee2008-05-17 22:44:35 +02002635 } else {
2636 b43info(dev->wl, "Loading firmware version %u.%u "
2637 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2638 fwrev, fwpatch,
2639 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2640 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002641 if (dev->fw.pcm_request_failed) {
2642 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2643 "Hardware accelerated cryptography is disabled.\n");
2644 b43_print_fw_helptext(dev->wl, 0);
2645 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002646 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002647
John W. Linville652caa52010-07-29 13:27:28 -04002648 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
2649 dev->fw.rev, dev->fw.patch);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002650 wiphy->hw_version = dev->dev->core_id;
John W. Linville652caa52010-07-29 13:27:28 -04002651
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002652 if (dev->fw.hdr_format == B43_FW_HDR_351) {
Michael Bueschc5572892008-12-27 18:26:39 +01002653 /* We're over the deadline, but we keep support for old fw
2654 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002655 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002656 "Support for old firmware will be removed soon "
2657 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002658 b43_print_fw_helptext(dev->wl, 0);
2659 }
2660
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002661 return 0;
2662
2663error:
Rafał Miłecki50566352012-01-02 19:31:21 +01002664 /* Stop the microcode PSM. */
2665 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
2666 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002667
Michael Buesche4d6b792007-09-18 15:39:42 -04002668 return err;
2669}
2670
2671static int b43_write_initvals(struct b43_wldev *dev,
2672 const struct b43_iv *ivals,
2673 size_t count,
2674 size_t array_size)
2675{
2676 const struct b43_iv *iv;
2677 u16 offset;
2678 size_t i;
2679 bool bit32;
2680
2681 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2682 iv = ivals;
2683 for (i = 0; i < count; i++) {
2684 if (array_size < sizeof(iv->offset_size))
2685 goto err_format;
2686 array_size -= sizeof(iv->offset_size);
2687 offset = be16_to_cpu(iv->offset_size);
2688 bit32 = !!(offset & B43_IV_32BIT);
2689 offset &= B43_IV_OFFSET_MASK;
2690 if (offset >= 0x1000)
2691 goto err_format;
2692 if (bit32) {
2693 u32 value;
2694
2695 if (array_size < sizeof(iv->data.d32))
2696 goto err_format;
2697 array_size -= sizeof(iv->data.d32);
2698
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002699 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002700 b43_write32(dev, offset, value);
2701
2702 iv = (const struct b43_iv *)((const uint8_t *)iv +
2703 sizeof(__be16) +
2704 sizeof(__be32));
2705 } else {
2706 u16 value;
2707
2708 if (array_size < sizeof(iv->data.d16))
2709 goto err_format;
2710 array_size -= sizeof(iv->data.d16);
2711
2712 value = be16_to_cpu(iv->data.d16);
2713 b43_write16(dev, offset, value);
2714
2715 iv = (const struct b43_iv *)((const uint8_t *)iv +
2716 sizeof(__be16) +
2717 sizeof(__be16));
2718 }
2719 }
2720 if (array_size)
2721 goto err_format;
2722
2723 return 0;
2724
2725err_format:
2726 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002727 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002728
2729 return -EPROTO;
2730}
2731
2732static int b43_upload_initvals(struct b43_wldev *dev)
2733{
2734 const size_t hdr_len = sizeof(struct b43_fw_header);
2735 const struct b43_fw_header *hdr;
2736 struct b43_firmware *fw = &dev->fw;
2737 const struct b43_iv *ivals;
2738 size_t count;
Michael Buesche4d6b792007-09-18 15:39:42 -04002739
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002740 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2741 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002742 count = be32_to_cpu(hdr->size);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002743 return b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002744 fw->initvals.data->size - hdr_len);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002745}
Michael Buesche4d6b792007-09-18 15:39:42 -04002746
Rafał Miłecki0f684232014-05-17 23:24:53 +02002747static int b43_upload_initvals_band(struct b43_wldev *dev)
2748{
2749 const size_t hdr_len = sizeof(struct b43_fw_header);
2750 const struct b43_fw_header *hdr;
2751 struct b43_firmware *fw = &dev->fw;
2752 const struct b43_iv *ivals;
2753 size_t count;
2754
2755 if (!fw->initvals_band.data)
2756 return 0;
2757
2758 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2759 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2760 count = be32_to_cpu(hdr->size);
2761 return b43_write_initvals(dev, ivals, count,
2762 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002763}
2764
2765/* Initialize the GPIOs
2766 * http://bcm-specs.sipsolutions.net/GPIO
2767 */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002768
2769#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002770static struct ssb_device *b43_ssb_gpio_dev(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002771{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02002772 struct ssb_bus *bus = dev->dev->sdev->bus;
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002773
2774#ifdef CONFIG_SSB_DRIVER_PCICORE
2775 return (bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev);
2776#else
2777 return bus->chipco.dev;
2778#endif
2779}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002780#endif
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002781
Michael Buesche4d6b792007-09-18 15:39:42 -04002782static int b43_gpio_init(struct b43_wldev *dev)
2783{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002784#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002785 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002786#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002787 u32 mask, set;
2788
Rafał Miłecki50566352012-01-02 19:31:21 +01002789 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_GPOUTSMSK, 0);
2790 b43_maskset16(dev, B43_MMIO_GPIO_MASK, ~0, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04002791
2792 mask = 0x0000001F;
2793 set = 0x0000000F;
Rafał Miłeckic244e082011-05-18 02:06:41 +02002794 if (dev->dev->chip_id == 0x4301) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002795 mask |= 0x0060;
2796 set |= 0x0060;
Rafał Miłecki828afd22012-07-23 22:57:01 +02002797 } else if (dev->dev->chip_id == 0x5354) {
2798 /* Don't allow overtaking buttons GPIOs */
2799 set &= 0x2; /* 0x2 is LED GPIO on BCM5354 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002800 }
Rafał Miłecki828afd22012-07-23 22:57:01 +02002801
Michael Buesche4d6b792007-09-18 15:39:42 -04002802 if (0 /* FIXME: conditional unknown */ ) {
2803 b43_write16(dev, B43_MMIO_GPIO_MASK,
2804 b43_read16(dev, B43_MMIO_GPIO_MASK)
2805 | 0x0100);
Rafał Miłecki828afd22012-07-23 22:57:01 +02002806 /* BT Coexistance Input */
2807 mask |= 0x0080;
2808 set |= 0x0080;
2809 /* BT Coexistance Out */
2810 mask |= 0x0100;
2811 set |= 0x0100;
Michael Buesche4d6b792007-09-18 15:39:42 -04002812 }
Rafał Miłecki05814832011-05-18 02:06:39 +02002813 if (dev->dev->bus_sprom->boardflags_lo & B43_BFL_PACTRL) {
Rafał Miłecki828afd22012-07-23 22:57:01 +02002814 /* PA is controlled by gpio 9, let ucode handle it */
Michael Buesche4d6b792007-09-18 15:39:42 -04002815 b43_write16(dev, B43_MMIO_GPIO_MASK,
2816 b43_read16(dev, B43_MMIO_GPIO_MASK)
2817 | 0x0200);
2818 mask |= 0x0200;
2819 set |= 0x0200;
2820 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002821
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002822 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002823#ifdef CONFIG_B43_BCMA
2824 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002825 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, mask, set);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002826 break;
2827#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002828#ifdef CONFIG_B43_SSB
2829 case B43_BUS_SSB:
2830 gpiodev = b43_ssb_gpio_dev(dev);
2831 if (gpiodev)
2832 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2833 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
Rafał Miłecki828afd22012-07-23 22:57:01 +02002834 & ~mask) | set);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002835 break;
2836#endif
2837 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002838
2839 return 0;
2840}
2841
2842/* Turn off all GPIO stuff. Call this on module unload, for example. */
2843static void b43_gpio_cleanup(struct b43_wldev *dev)
2844{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002845#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002846 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002847#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002848
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002849 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002850#ifdef CONFIG_B43_BCMA
2851 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002852 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, ~0, 0);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002853 break;
2854#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002855#ifdef CONFIG_B43_SSB
2856 case B43_BUS_SSB:
2857 gpiodev = b43_ssb_gpio_dev(dev);
2858 if (gpiodev)
2859 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2860 break;
2861#endif
2862 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002863}
2864
2865/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002866void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002867{
Michael Buesch923fd702008-06-20 18:02:08 +02002868 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2869 u16 fwstate;
2870
2871 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2872 B43_SHM_SH_UCODESTAT);
2873 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2874 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2875 b43err(dev->wl, "b43_mac_enable(): The firmware "
2876 "should be suspended, but current state is %u\n",
2877 fwstate);
2878 }
2879 }
2880
Michael Buesche4d6b792007-09-18 15:39:42 -04002881 dev->mac_suspended--;
2882 B43_WARN_ON(dev->mac_suspended < 0);
2883 if (dev->mac_suspended == 0) {
Rafał Miłecki50566352012-01-02 19:31:21 +01002884 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_ENABLED);
Michael Buesche4d6b792007-09-18 15:39:42 -04002885 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2886 B43_IRQ_MAC_SUSPENDED);
2887 /* Commit writes */
2888 b43_read32(dev, B43_MMIO_MACCTL);
2889 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2890 b43_power_saving_ctl_bits(dev, 0);
2891 }
2892}
2893
2894/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002895void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002896{
2897 int i;
2898 u32 tmp;
2899
Michael Buesch05b64b32007-09-28 16:19:03 +02002900 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002901 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002902
Michael Buesche4d6b792007-09-18 15:39:42 -04002903 if (dev->mac_suspended == 0) {
2904 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Rafał Miłecki50566352012-01-02 19:31:21 +01002905 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_ENABLED, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04002906 /* force pci to flush the write */
2907 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02002908 for (i = 35; i; i--) {
2909 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2910 if (tmp & B43_IRQ_MAC_SUSPENDED)
2911 goto out;
2912 udelay(10);
2913 }
2914 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02002915 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002916 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2917 if (tmp & B43_IRQ_MAC_SUSPENDED)
2918 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02002919 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002920 }
2921 b43err(dev->wl, "MAC suspend failed\n");
2922 }
Michael Buesch05b64b32007-09-28 16:19:03 +02002923out:
Michael Buesche4d6b792007-09-18 15:39:42 -04002924 dev->mac_suspended++;
2925}
2926
Rafał Miłecki858a1652011-05-10 16:05:33 +02002927/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MacPhyClkSet */
2928void b43_mac_phy_clock_set(struct b43_wldev *dev, bool on)
2929{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002930 u32 tmp;
2931
2932 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002933#ifdef CONFIG_B43_BCMA
2934 case B43_BUS_BCMA:
Rafał Miłecki36677872011-07-16 18:27:55 +02002935 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002936 if (on)
2937 tmp |= B43_BCMA_IOCTL_MACPHYCLKEN;
2938 else
2939 tmp &= ~B43_BCMA_IOCTL_MACPHYCLKEN;
Rafał Miłecki36677872011-07-16 18:27:55 +02002940 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002941 break;
2942#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002943#ifdef CONFIG_B43_SSB
2944 case B43_BUS_SSB:
2945 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
2946 if (on)
2947 tmp |= B43_TMSLOW_MACPHYCLKEN;
2948 else
2949 tmp &= ~B43_TMSLOW_MACPHYCLKEN;
2950 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
2951 break;
2952#endif
2953 }
Rafał Miłecki858a1652011-05-10 16:05:33 +02002954}
2955
Michael Buesche4d6b792007-09-18 15:39:42 -04002956static void b43_adjust_opmode(struct b43_wldev *dev)
2957{
2958 struct b43_wl *wl = dev->wl;
2959 u32 ctl;
2960 u16 cfp_pretbtt;
2961
2962 ctl = b43_read32(dev, B43_MMIO_MACCTL);
2963 /* Reset status to STA infrastructure mode. */
2964 ctl &= ~B43_MACCTL_AP;
2965 ctl &= ~B43_MACCTL_KEEP_CTL;
2966 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
2967 ctl &= ~B43_MACCTL_KEEP_BAD;
2968 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002969 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04002970 ctl |= B43_MACCTL_INFRA;
2971
Johannes Berg05c914f2008-09-11 00:01:58 +02002972 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
2973 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04002974 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02002975 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04002976 ctl &= ~B43_MACCTL_INFRA;
2977
2978 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04002979 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04002980 if (wl->filter_flags & FIF_FCSFAIL)
2981 ctl |= B43_MACCTL_KEEP_BAD;
2982 if (wl->filter_flags & FIF_PLCPFAIL)
2983 ctl |= B43_MACCTL_KEEP_BADPLCP;
2984 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04002985 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002986 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2987 ctl |= B43_MACCTL_BEACPROMISC;
2988
Michael Buesche4d6b792007-09-18 15:39:42 -04002989 /* Workaround: On old hardware the HW-MAC-address-filter
2990 * doesn't work properly, so always run promisc in filter
2991 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002992 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04002993 ctl |= B43_MACCTL_PROMISC;
2994
2995 b43_write32(dev, B43_MMIO_MACCTL, ctl);
2996
2997 cfp_pretbtt = 2;
2998 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02002999 if (dev->dev->chip_id == 0x4306 &&
3000 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04003001 cfp_pretbtt = 100;
3002 else
3003 cfp_pretbtt = 50;
3004 }
3005 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02003006
3007 /* FIXME: We don't currently implement the PMQ mechanism,
3008 * so always disable it. If we want to implement PMQ,
3009 * we need to enable it here (clear DISCPMQ) in AP mode.
3010 */
Rafał Miłecki50566352012-01-02 19:31:21 +01003011 if (0 /* ctl & B43_MACCTL_AP */)
3012 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3013 else
3014 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003015}
3016
3017static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3018{
3019 u16 offset;
3020
3021 if (is_ofdm) {
3022 offset = 0x480;
3023 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3024 } else {
3025 offset = 0x4C0;
3026 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3027 }
3028 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3029 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3030}
3031
3032static void b43_rate_memory_init(struct b43_wldev *dev)
3033{
3034 switch (dev->phy.type) {
3035 case B43_PHYTYPE_A:
3036 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003037 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003038 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003039 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003040 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003041 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
3042 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3043 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3044 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3045 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3046 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3047 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3048 if (dev->phy.type == B43_PHYTYPE_A)
3049 break;
3050 /* fallthrough */
3051 case B43_PHYTYPE_B:
3052 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3053 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3054 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3055 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3056 break;
3057 default:
3058 B43_WARN_ON(1);
3059 }
3060}
3061
Michael Buesch5042c502008-04-05 15:05:00 +02003062/* Set the default values for the PHY TX Control Words. */
3063static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3064{
3065 u16 ctl = 0;
3066
3067 ctl |= B43_TXH_PHY_ENC_CCK;
3068 ctl |= B43_TXH_PHY_ANT01AUTO;
3069 ctl |= B43_TXH_PHY_TXPWR;
3070
3071 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3072 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3073 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3074}
3075
Michael Buesche4d6b792007-09-18 15:39:42 -04003076/* Set the TX-Antenna for management frames sent by firmware. */
3077static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3078{
Michael Buesch5042c502008-04-05 15:05:00 +02003079 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003080 u16 tmp;
3081
Michael Buesch5042c502008-04-05 15:05:00 +02003082 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003083
Michael Buesche4d6b792007-09-18 15:39:42 -04003084 /* For ACK/CTS */
3085 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003086 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003087 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3088 /* For Probe Resposes */
3089 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003090 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003091 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3092}
3093
3094/* This is the opposite of b43_chip_init() */
3095static void b43_chip_exit(struct b43_wldev *dev)
3096{
Michael Bueschfb111372008-09-02 13:00:34 +02003097 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003098 b43_gpio_cleanup(dev);
3099 /* firmware is released later */
3100}
3101
3102/* Initialize the chip
3103 * http://bcm-specs.sipsolutions.net/ChipInit
3104 */
3105static int b43_chip_init(struct b43_wldev *dev)
3106{
3107 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003108 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003109 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003110 u16 value16;
3111
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003112 /* Initialize the MAC control */
3113 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3114 if (dev->phy.gmode)
3115 macctl |= B43_MACCTL_GMODE;
3116 macctl |= B43_MACCTL_INFRA;
3117 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003118
Michael Buesche4d6b792007-09-18 15:39:42 -04003119 err = b43_upload_microcode(dev);
3120 if (err)
3121 goto out; /* firmware is released later */
3122
3123 err = b43_gpio_init(dev);
3124 if (err)
3125 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003126
Michael Buesche4d6b792007-09-18 15:39:42 -04003127 err = b43_upload_initvals(dev);
3128 if (err)
Larry Finger1a8d1222007-12-14 13:59:11 +01003129 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003130
Rafał Miłecki0f684232014-05-17 23:24:53 +02003131 err = b43_upload_initvals_band(dev);
3132 if (err)
3133 goto err_gpio_clean;
3134
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003135 /* Turn the Analog on and initialize the PHY. */
3136 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003137 err = b43_phy_init(dev);
3138 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003139 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003140
Michael Bueschef1a6282008-08-27 18:53:02 +02003141 /* Disable Interference Mitigation. */
3142 if (phy->ops->interf_mitigation)
3143 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003144
Michael Bueschef1a6282008-08-27 18:53:02 +02003145 /* Select the antennae */
3146 if (phy->ops->set_rx_antenna)
3147 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003148 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3149
3150 if (phy->type == B43_PHYTYPE_B) {
3151 value16 = b43_read16(dev, 0x005E);
3152 value16 |= 0x0004;
3153 b43_write16(dev, 0x005E, value16);
3154 }
3155 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003156 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003157 b43_write32(dev, 0x010C, 0x01000000);
3158
Rafał Miłecki50566352012-01-02 19:31:21 +01003159 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3160 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003161
Michael Buesche4d6b792007-09-18 15:39:42 -04003162 /* Probe Response Timeout value */
3163 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003164 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003165
3166 /* Initially set the wireless operation mode. */
3167 b43_adjust_opmode(dev);
3168
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003169 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003170 b43_write16(dev, 0x060E, 0x0000);
3171 b43_write16(dev, 0x0610, 0x8000);
3172 b43_write16(dev, 0x0604, 0x0000);
3173 b43_write16(dev, 0x0606, 0x0200);
3174 } else {
3175 b43_write32(dev, 0x0188, 0x80000000);
3176 b43_write32(dev, 0x018C, 0x02000000);
3177 }
3178 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003179 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003180 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3181 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3182 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3183 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3184 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3185
Rafał Miłecki858a1652011-05-10 16:05:33 +02003186 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003187
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003188 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003189#ifdef CONFIG_B43_BCMA
3190 case B43_BUS_BCMA:
3191 /* FIXME: 0xE74 is quite common, but should be read from CC */
3192 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3193 break;
3194#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003195#ifdef CONFIG_B43_SSB
3196 case B43_BUS_SSB:
3197 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3198 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3199 break;
3200#endif
3201 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003202
3203 err = 0;
3204 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003205out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003206 return err;
3207
Larry Finger1a8d1222007-12-14 13:59:11 +01003208err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003209 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003210 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003211}
3212
Michael Buesche4d6b792007-09-18 15:39:42 -04003213static void b43_periodic_every60sec(struct b43_wldev *dev)
3214{
Michael Bueschef1a6282008-08-27 18:53:02 +02003215 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003216
Michael Bueschef1a6282008-08-27 18:53:02 +02003217 if (ops->pwork_60sec)
3218 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003219
3220 /* Force check the TX power emission now. */
3221 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003222}
3223
3224static void b43_periodic_every30sec(struct b43_wldev *dev)
3225{
3226 /* Update device statistics. */
3227 b43_calculate_link_quality(dev);
3228}
3229
3230static void b43_periodic_every15sec(struct b43_wldev *dev)
3231{
3232 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003233 u16 wdr;
3234
3235 if (dev->fw.opensource) {
3236 /* Check if the firmware is still alive.
3237 * It will reset the watchdog counter to 0 in its idle loop. */
3238 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3239 if (unlikely(wdr)) {
3240 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3241 b43_controller_restart(dev, "Firmware watchdog");
3242 return;
3243 } else {
3244 b43_shm_write16(dev, B43_SHM_SCRATCH,
3245 B43_WATCHDOG_REG, 1);
3246 }
3247 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003248
Michael Bueschef1a6282008-08-27 18:53:02 +02003249 if (phy->ops->pwork_15sec)
3250 phy->ops->pwork_15sec(dev);
3251
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003252 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3253 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003254
3255#if B43_DEBUG
3256 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3257 unsigned int i;
3258
3259 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3260 dev->irq_count / 15,
3261 dev->tx_count / 15,
3262 dev->rx_count / 15);
3263 dev->irq_count = 0;
3264 dev->tx_count = 0;
3265 dev->rx_count = 0;
3266 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3267 if (dev->irq_bit_count[i]) {
3268 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3269 dev->irq_bit_count[i] / 15, i, (1 << i));
3270 dev->irq_bit_count[i] = 0;
3271 }
3272 }
3273 }
3274#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003275}
3276
Michael Buesche4d6b792007-09-18 15:39:42 -04003277static void do_periodic_work(struct b43_wldev *dev)
3278{
3279 unsigned int state;
3280
3281 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003282 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003283 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003284 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003285 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003286 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003287}
3288
Michael Buesch05b64b32007-09-28 16:19:03 +02003289/* Periodic work locking policy:
3290 * The whole periodic work handler is protected by
3291 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003292 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003293 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003294static void b43_periodic_work_handler(struct work_struct *work)
3295{
Michael Buesch05b64b32007-09-28 16:19:03 +02003296 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3297 periodic_work.work);
3298 struct b43_wl *wl = dev->wl;
3299 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003300
Michael Buesch05b64b32007-09-28 16:19:03 +02003301 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003302
3303 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3304 goto out;
3305 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3306 goto out_requeue;
3307
Michael Buesch05b64b32007-09-28 16:19:03 +02003308 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003309
Michael Buesche4d6b792007-09-18 15:39:42 -04003310 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003311out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003312 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3313 delay = msecs_to_jiffies(50);
3314 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003315 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003316 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003317out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003318 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003319}
3320
3321static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3322{
3323 struct delayed_work *work = &dev->periodic_work;
3324
3325 dev->periodic_state = 0;
3326 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003327 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003328}
3329
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003330/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003331static int b43_validate_chipaccess(struct b43_wldev *dev)
3332{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003333 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003334
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003335 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3336 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003337
3338 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003339 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3340 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3341 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003342 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3343 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003344 goto error;
3345
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003346 /* Check if unaligned 32bit SHM_SHARED access works properly.
3347 * However, don't bail out on failure, because it's noncritical. */
3348 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3349 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3350 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3351 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3352 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3353 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3354 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3355 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3356 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3357 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3358 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3359 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3360
3361 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3362 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003363
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003364 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003365 /* The 32bit register shadows the two 16bit registers
3366 * with update sideeffects. Validate this. */
3367 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3368 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3369 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3370 goto error;
3371 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3372 goto error;
3373 }
3374 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3375
3376 v = b43_read32(dev, B43_MMIO_MACCTL);
3377 v |= B43_MACCTL_GMODE;
3378 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003379 goto error;
3380
3381 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003382error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003383 b43err(dev->wl, "Failed to validate the chipaccess\n");
3384 return -ENODEV;
3385}
3386
3387static void b43_security_init(struct b43_wldev *dev)
3388{
Michael Buesche4d6b792007-09-18 15:39:42 -04003389 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3390 /* KTP is a word address, but we address SHM bytewise.
3391 * So multiply by two.
3392 */
3393 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003394 /* Number of RCMTA address slots */
3395 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3396 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003397 b43_clear_keys(dev);
3398}
3399
Michael Buesch616de352009-03-29 13:19:31 +02003400#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003401static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003402{
3403 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003404 struct b43_wldev *dev;
3405 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003406
Michael Buescha78b3bb2009-09-11 21:44:05 +02003407 mutex_lock(&wl->mutex);
3408 dev = wl->current_dev;
3409 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3410 *data = b43_read16(dev, B43_MMIO_RNG);
3411 count = sizeof(u16);
3412 }
3413 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003414
Michael Buescha78b3bb2009-09-11 21:44:05 +02003415 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003416}
Michael Buesch616de352009-03-29 13:19:31 +02003417#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003418
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003419static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003420{
Michael Buesch616de352009-03-29 13:19:31 +02003421#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003422 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003423 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003424#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003425}
3426
3427static int b43_rng_init(struct b43_wl *wl)
3428{
Michael Buesch616de352009-03-29 13:19:31 +02003429 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003430
Michael Buesch616de352009-03-29 13:19:31 +02003431#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003432 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3433 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3434 wl->rng.name = wl->rng_name;
3435 wl->rng.data_read = b43_rng_read;
3436 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003437 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003438 err = hwrng_register(&wl->rng);
3439 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003440 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003441 b43err(wl, "Failed to register the random "
3442 "number generator (%d)\n", err);
3443 }
Michael Buesch616de352009-03-29 13:19:31 +02003444#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003445
3446 return err;
3447}
3448
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003449static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003450{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003451 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3452 struct b43_wldev *dev;
3453 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003454 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003455 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003456
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003457 mutex_lock(&wl->mutex);
3458 dev = wl->current_dev;
3459 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3460 mutex_unlock(&wl->mutex);
3461 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003462 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003463
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003464 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3465 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3466 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3467 if (b43_using_pio_transfers(dev))
3468 err = b43_pio_tx(dev, skb);
3469 else
3470 err = b43_dma_tx(dev, skb);
3471 if (err == -ENOSPC) {
3472 wl->tx_queue_stopped[queue_num] = 1;
3473 ieee80211_stop_queue(wl->hw, queue_num);
3474 skb_queue_head(&wl->tx_queue[queue_num], skb);
3475 break;
3476 }
3477 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003478 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003479 err = 0;
3480 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003481
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003482 if (!err)
3483 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003484 }
3485
Michael Buesch990b86f2009-09-12 00:48:03 +02003486#if B43_DEBUG
3487 dev->tx_count++;
3488#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003489 mutex_unlock(&wl->mutex);
3490}
Michael Buesch21a75d72008-04-25 19:29:08 +02003491
Johannes Berg7bb45682011-02-24 14:42:06 +01003492static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003493 struct ieee80211_tx_control *control,
3494 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003495{
3496 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003497
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003498 if (unlikely(skb->len < 2 + 2 + 6)) {
3499 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003500 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003501 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003502 }
3503 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3504
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003505 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3506 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3507 ieee80211_queue_work(wl->hw, &wl->tx_work);
3508 } else {
3509 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3510 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003511}
3512
Michael Buesche6f5b932008-03-05 21:18:49 +01003513static void b43_qos_params_upload(struct b43_wldev *dev,
3514 const struct ieee80211_tx_queue_params *p,
3515 u16 shm_offset)
3516{
3517 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003518 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003519 unsigned int i;
3520
Michael Bueschb0544eb2009-09-06 15:42:45 +02003521 if (!dev->qos_enabled)
3522 return;
3523
Johannes Berg0b576642008-07-15 02:08:24 -07003524 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003525
3526 memset(&params, 0, sizeof(params));
3527
3528 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003529 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3530 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3531 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3532 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003533 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003534 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003535
3536 for (i = 0; i < ARRAY_SIZE(params); i++) {
3537 if (i == B43_QOSPARAM_STATUS) {
3538 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3539 shm_offset + (i * 2));
3540 /* Mark the parameters as updated. */
3541 tmp |= 0x100;
3542 b43_shm_write16(dev, B43_SHM_SHARED,
3543 shm_offset + (i * 2),
3544 tmp);
3545 } else {
3546 b43_shm_write16(dev, B43_SHM_SHARED,
3547 shm_offset + (i * 2),
3548 params[i]);
3549 }
3550 }
3551}
3552
Michael Bueschc40c1122008-09-06 16:21:47 +02003553/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3554static const u16 b43_qos_shm_offsets[] = {
3555 /* [mac80211-queue-nr] = SHM_OFFSET, */
3556 [0] = B43_QOS_VOICE,
3557 [1] = B43_QOS_VIDEO,
3558 [2] = B43_QOS_BESTEFFORT,
3559 [3] = B43_QOS_BACKGROUND,
3560};
3561
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003562/* Update all QOS parameters in hardware. */
3563static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003564{
3565 struct b43_wl *wl = dev->wl;
3566 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003567 unsigned int i;
3568
Michael Bueschb0544eb2009-09-06 15:42:45 +02003569 if (!dev->qos_enabled)
3570 return;
3571
Michael Bueschc40c1122008-09-06 16:21:47 +02003572 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3573 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003574
3575 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003576 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3577 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003578 b43_qos_params_upload(dev, &(params->p),
3579 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003580 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003581 b43_mac_enable(dev);
3582}
3583
3584static void b43_qos_clear(struct b43_wl *wl)
3585{
3586 struct b43_qos_params *params;
3587 unsigned int i;
3588
Michael Bueschc40c1122008-09-06 16:21:47 +02003589 /* Initialize QoS parameters to sane defaults. */
3590
3591 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3592 ARRAY_SIZE(wl->qos_params));
3593
Michael Buesche6f5b932008-03-05 21:18:49 +01003594 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3595 params = &(wl->qos_params[i]);
3596
Michael Bueschc40c1122008-09-06 16:21:47 +02003597 switch (b43_qos_shm_offsets[i]) {
3598 case B43_QOS_VOICE:
3599 params->p.txop = 0;
3600 params->p.aifs = 2;
3601 params->p.cw_min = 0x0001;
3602 params->p.cw_max = 0x0001;
3603 break;
3604 case B43_QOS_VIDEO:
3605 params->p.txop = 0;
3606 params->p.aifs = 2;
3607 params->p.cw_min = 0x0001;
3608 params->p.cw_max = 0x0001;
3609 break;
3610 case B43_QOS_BESTEFFORT:
3611 params->p.txop = 0;
3612 params->p.aifs = 3;
3613 params->p.cw_min = 0x0001;
3614 params->p.cw_max = 0x03FF;
3615 break;
3616 case B43_QOS_BACKGROUND:
3617 params->p.txop = 0;
3618 params->p.aifs = 7;
3619 params->p.cw_min = 0x0001;
3620 params->p.cw_max = 0x03FF;
3621 break;
3622 default:
3623 B43_WARN_ON(1);
3624 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003625 }
3626}
3627
3628/* Initialize the core's QOS capabilities */
3629static void b43_qos_init(struct b43_wldev *dev)
3630{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003631 if (!dev->qos_enabled) {
3632 /* Disable QOS support. */
3633 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3634 b43_write16(dev, B43_MMIO_IFSCTL,
3635 b43_read16(dev, B43_MMIO_IFSCTL)
3636 & ~B43_MMIO_IFSCTL_USE_EDCF);
3637 b43dbg(dev->wl, "QoS disabled\n");
3638 return;
3639 }
3640
Michael Buesche6f5b932008-03-05 21:18:49 +01003641 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003642 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003643
3644 /* Enable QOS support. */
3645 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3646 b43_write16(dev, B43_MMIO_IFSCTL,
3647 b43_read16(dev, B43_MMIO_IFSCTL)
3648 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003649 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003650}
3651
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003652static int b43_op_conf_tx(struct ieee80211_hw *hw,
3653 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003654 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003655{
Michael Buesche6f5b932008-03-05 21:18:49 +01003656 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003657 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003658 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003659 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003660
3661 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3662 /* Queue not available or don't support setting
3663 * params on this queue. Return success to not
3664 * confuse mac80211. */
3665 return 0;
3666 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003667 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3668 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003669
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003670 mutex_lock(&wl->mutex);
3671 dev = wl->current_dev;
3672 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3673 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003674
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003675 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3676 b43_mac_suspend(dev);
3677 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3678 b43_qos_shm_offsets[queue]);
3679 b43_mac_enable(dev);
3680 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003681
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003682out_unlock:
3683 mutex_unlock(&wl->mutex);
3684
3685 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003686}
3687
Michael Buesch40faacc2007-10-28 16:29:32 +01003688static int b43_op_get_stats(struct ieee80211_hw *hw,
3689 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003690{
3691 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003692
Michael Buesch36dbd952009-09-04 22:51:29 +02003693 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003694 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003695 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003696
3697 return 0;
3698}
3699
Eliad Peller37a41b42011-09-21 14:06:11 +03003700static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003701{
3702 struct b43_wl *wl = hw_to_b43_wl(hw);
3703 struct b43_wldev *dev;
3704 u64 tsf;
3705
3706 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003707 dev = wl->current_dev;
3708
3709 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3710 b43_tsf_read(dev, &tsf);
3711 else
3712 tsf = 0;
3713
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003714 mutex_unlock(&wl->mutex);
3715
3716 return tsf;
3717}
3718
Eliad Peller37a41b42011-09-21 14:06:11 +03003719static void b43_op_set_tsf(struct ieee80211_hw *hw,
3720 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003721{
3722 struct b43_wl *wl = hw_to_b43_wl(hw);
3723 struct b43_wldev *dev;
3724
3725 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003726 dev = wl->current_dev;
3727
3728 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3729 b43_tsf_write(dev, tsf);
3730
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003731 mutex_unlock(&wl->mutex);
3732}
3733
John Daiker99da1852009-02-24 02:16:42 -08003734static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003735{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003736 switch (band) {
3737 case IEEE80211_BAND_5GHZ:
3738 return "5";
3739 case IEEE80211_BAND_2GHZ:
3740 return "2.4";
3741 default:
3742 break;
3743 }
3744 B43_WARN_ON(1);
3745 return "";
3746}
3747
3748/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003749static int b43_switch_band(struct b43_wldev *dev,
3750 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003751{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003752 struct b43_phy *phy = &dev->phy;
3753 bool gmode;
3754 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003755
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003756 switch (chan->band) {
3757 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003758 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003759 break;
3760 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003761 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003762 break;
3763 default:
3764 B43_WARN_ON(1);
3765 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003766 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003767
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003768 if (!((gmode && phy->supports_2ghz) ||
3769 (!gmode && phy->supports_5ghz))) {
3770 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003771 band_to_string(chan->band));
3772 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003773 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003774
3775 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003776 /* This device is already running. */
3777 return 0;
3778 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003779
3780 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003781 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003782
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003783 /* Some new devices don't need disabling radio for band switching */
3784 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3785 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003786
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003787 phy->gmode = gmode;
3788 b43_phy_put_into_reset(dev);
3789 switch (dev->dev->bus_type) {
3790#ifdef CONFIG_B43_BCMA
3791 case B43_BUS_BCMA:
3792 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3793 if (gmode)
3794 tmp |= B43_BCMA_IOCTL_GMODE;
3795 else
3796 tmp &= ~B43_BCMA_IOCTL_GMODE;
3797 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3798 break;
3799#endif
3800#ifdef CONFIG_B43_SSB
3801 case B43_BUS_SSB:
3802 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3803 if (gmode)
3804 tmp |= B43_TMSLOW_GMODE;
3805 else
3806 tmp &= ~B43_TMSLOW_GMODE;
3807 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3808 break;
3809#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003810 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003811 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003812
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003813 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003814
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003815 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003816
3817 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003818}
3819
Johannes Berg9124b072008-10-14 19:17:54 +02003820/* Write the short and long frame retry limit values. */
3821static void b43_set_retry_limits(struct b43_wldev *dev,
3822 unsigned int short_retry,
3823 unsigned int long_retry)
3824{
3825 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3826 * the chip-internal counter. */
3827 short_retry = min(short_retry, (unsigned int)0xF);
3828 long_retry = min(long_retry, (unsigned int)0xF);
3829
3830 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3831 short_retry);
3832 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3833 long_retry);
3834}
3835
Johannes Berge8975582008-10-09 12:18:51 +02003836static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003837{
3838 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003839 struct b43_wldev *dev = wl->current_dev;
3840 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003841 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003842 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003843 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003844
Michael Buesche4d6b792007-09-18 15:39:42 -04003845 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003846 b43_mac_suspend(dev);
3847
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003848 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02003849 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003850 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06003851
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003852 /* Switch the band (if necessary). */
3853 err = b43_switch_band(dev, conf->chandef.chan);
3854 if (err)
3855 goto out_mac_enable;
3856
3857 /* Switch to the requested channel.
3858 * The firmware takes care of races with the TX handler.
3859 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003860 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003861 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01003862
Johannes Berg9124b072008-10-14 19:17:54 +02003863 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3864 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
3865 conf->long_frame_max_tx_count);
3866 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
3867 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01003868 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04003869
Johannes Berg0869aea2009-10-28 10:03:35 +01003870 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01003871
Michael Buesche4d6b792007-09-18 15:39:42 -04003872 /* Adjust the desired TX power level. */
3873 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02003874 if (conf->power_level != phy->desired_txpower) {
3875 phy->desired_txpower = conf->power_level;
3876 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3877 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04003878 }
3879 }
3880
3881 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02003882 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003883 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02003884 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02003885 if (phy->ops->set_rx_antenna)
3886 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003887
Larry Fingerfd4973c2009-06-20 12:58:11 -05003888 if (wl->radio_enabled != phy->radio_on) {
3889 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003890 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003891 b43info(dev->wl, "Radio turned on by software\n");
3892 if (!dev->radio_hw_enable) {
3893 b43info(dev->wl, "The hardware RF-kill button "
3894 "still turns the radio physically off. "
3895 "Press the button to turn it on.\n");
3896 }
3897 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02003898 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003899 b43info(dev->wl, "Radio turned off by software\n");
3900 }
3901 }
3902
Michael Bueschd10d0e52008-12-18 22:13:39 +01003903out_mac_enable:
3904 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003905 mutex_unlock(&wl->mutex);
3906
3907 return err;
3908}
3909
Johannes Berg881d9482009-01-21 15:13:48 +01003910static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003911{
3912 struct ieee80211_supported_band *sband =
3913 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
3914 struct ieee80211_rate *rate;
3915 int i;
3916 u16 basic, direct, offset, basic_offset, rateptr;
3917
3918 for (i = 0; i < sband->n_bitrates; i++) {
3919 rate = &sband->bitrates[i];
3920
3921 if (b43_is_cck_rate(rate->hw_value)) {
3922 direct = B43_SHM_SH_CCKDIRECT;
3923 basic = B43_SHM_SH_CCKBASIC;
3924 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3925 offset &= 0xF;
3926 } else {
3927 direct = B43_SHM_SH_OFDMDIRECT;
3928 basic = B43_SHM_SH_OFDMBASIC;
3929 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3930 offset &= 0xF;
3931 }
3932
3933 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
3934
3935 if (b43_is_cck_rate(rate->hw_value)) {
3936 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3937 basic_offset &= 0xF;
3938 } else {
3939 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3940 basic_offset &= 0xF;
3941 }
3942
3943 /*
3944 * Get the pointer that we need to point to
3945 * from the direct map
3946 */
3947 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
3948 direct + 2 * basic_offset);
3949 /* and write it to the basic map */
3950 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
3951 rateptr);
3952 }
3953}
3954
3955static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
3956 struct ieee80211_vif *vif,
3957 struct ieee80211_bss_conf *conf,
3958 u32 changed)
3959{
3960 struct b43_wl *wl = hw_to_b43_wl(hw);
3961 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003962
3963 mutex_lock(&wl->mutex);
3964
3965 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01003966 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003967 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003968
3969 B43_WARN_ON(wl->vif != vif);
3970
3971 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003972 if (conf->bssid)
3973 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
3974 else
3975 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003976 }
3977
Johannes Berg3f0d8432009-05-18 10:53:18 +02003978 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
3979 if (changed & BSS_CHANGED_BEACON &&
3980 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3981 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
3982 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
3983 b43_update_templates(wl);
3984
3985 if (changed & BSS_CHANGED_BSSID)
3986 b43_write_mac_bssid_templates(dev);
3987 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02003988
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003989 b43_mac_suspend(dev);
3990
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003991 /* Update templates for AP/mesh mode. */
3992 if (changed & BSS_CHANGED_BEACON_INT &&
3993 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3994 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06003995 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
3996 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003997 b43_set_beacon_int(dev, conf->beacon_int);
3998
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003999 if (changed & BSS_CHANGED_BASIC_RATES)
4000 b43_update_basic_rates(dev, conf->basic_rates);
4001
4002 if (changed & BSS_CHANGED_ERP_SLOT) {
4003 if (conf->use_short_slot)
4004 b43_short_slot_timing_enable(dev);
4005 else
4006 b43_short_slot_timing_disable(dev);
4007 }
4008
4009 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01004010out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004011 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004012}
4013
Michael Buesch40faacc2007-10-28 16:29:32 +01004014static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004015 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4016 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004017{
4018 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004019 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004020 u8 algorithm;
4021 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004022 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004023 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004024
4025 if (modparam_nohwcrypt)
4026 return -ENOSPC; /* User disabled HW-crypto */
4027
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004028 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4029 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4030 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4031 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4032 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4033 /*
4034 * For now, disable hw crypto for the RSN IBSS group keys. This
4035 * could be optimized in the future, but until that gets
4036 * implemented, use of software crypto for group addressed
4037 * frames is a acceptable to allow RSN IBSS to be used.
4038 */
4039 return -EOPNOTSUPP;
4040 }
4041
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004042 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004043
4044 dev = wl->current_dev;
4045 err = -ENODEV;
4046 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4047 goto out_unlock;
4048
Michael Buesch403a3a12009-06-08 21:04:57 +02004049 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004050 /* We don't have firmware for the crypto engine.
4051 * Must use software-crypto. */
4052 err = -EOPNOTSUPP;
4053 goto out_unlock;
4054 }
4055
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004056 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004057 switch (key->cipher) {
4058 case WLAN_CIPHER_SUITE_WEP40:
4059 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004060 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004061 case WLAN_CIPHER_SUITE_WEP104:
4062 algorithm = B43_SEC_ALGO_WEP104;
4063 break;
4064 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004065 algorithm = B43_SEC_ALGO_TKIP;
4066 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004067 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004068 algorithm = B43_SEC_ALGO_AES;
4069 break;
4070 default:
4071 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004072 goto out_unlock;
4073 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004074 index = (u8) (key->keyidx);
4075 if (index > 3)
4076 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004077
4078 switch (cmd) {
4079 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004080 if (algorithm == B43_SEC_ALGO_TKIP &&
4081 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4082 !modparam_hwtkip)) {
4083 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004084 err = -EOPNOTSUPP;
4085 goto out_unlock;
4086 }
4087
Michael Buesche808e582008-12-19 21:30:52 +01004088 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004089 if (WARN_ON(!sta)) {
4090 err = -EOPNOTSUPP;
4091 goto out_unlock;
4092 }
Michael Buesche808e582008-12-19 21:30:52 +01004093 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004094 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004095 key->key, key->keylen,
4096 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004097 } else {
4098 /* Group key */
4099 err = b43_key_write(dev, index, algorithm,
4100 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004101 }
4102 if (err)
4103 goto out_unlock;
4104
4105 if (algorithm == B43_SEC_ALGO_WEP40 ||
4106 algorithm == B43_SEC_ALGO_WEP104) {
4107 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4108 } else {
4109 b43_hf_write(dev,
4110 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4111 }
4112 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004113 if (algorithm == B43_SEC_ALGO_TKIP)
4114 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004115 break;
4116 case DISABLE_KEY: {
4117 err = b43_key_clear(dev, key->hw_key_idx);
4118 if (err)
4119 goto out_unlock;
4120 break;
4121 }
4122 default:
4123 B43_WARN_ON(1);
4124 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004125
Michael Buesche4d6b792007-09-18 15:39:42 -04004126out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004127 if (!err) {
4128 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004129 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004130 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004131 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004132 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004133 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004134 mutex_unlock(&wl->mutex);
4135
Michael Buesche4d6b792007-09-18 15:39:42 -04004136 return err;
4137}
4138
Michael Buesch40faacc2007-10-28 16:29:32 +01004139static void b43_op_configure_filter(struct ieee80211_hw *hw,
4140 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004141 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004142{
4143 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004144 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004145
Michael Buesch36dbd952009-09-04 22:51:29 +02004146 mutex_lock(&wl->mutex);
4147 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004148 if (!dev) {
4149 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004150 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004151 }
Johannes Berg4150c572007-09-17 01:29:23 -04004152
Johannes Berg4150c572007-09-17 01:29:23 -04004153 *fflags &= FIF_PROMISC_IN_BSS |
4154 FIF_ALLMULTI |
4155 FIF_FCSFAIL |
4156 FIF_PLCPFAIL |
4157 FIF_CONTROL |
4158 FIF_OTHER_BSS |
4159 FIF_BCN_PRBRESP_PROMISC;
4160
4161 changed &= FIF_PROMISC_IN_BSS |
4162 FIF_ALLMULTI |
4163 FIF_FCSFAIL |
4164 FIF_PLCPFAIL |
4165 FIF_CONTROL |
4166 FIF_OTHER_BSS |
4167 FIF_BCN_PRBRESP_PROMISC;
4168
4169 wl->filter_flags = *fflags;
4170
4171 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4172 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004173
4174out_unlock:
4175 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004176}
4177
Michael Buesch36dbd952009-09-04 22:51:29 +02004178/* Locking: wl->mutex
4179 * Returns the current dev. This might be different from the passed in dev,
4180 * because the core might be gone away while we unlocked the mutex. */
4181static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004182{
Larry Finger9a53bf52011-08-27 15:53:42 -05004183 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004184 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004185 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004186 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004187
Larry Finger9a53bf52011-08-27 15:53:42 -05004188 if (!dev)
4189 return NULL;
4190 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004191redo:
4192 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4193 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004194
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004195 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004196 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004197 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004198 cancel_work_sync(&wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04004199 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004200 dev = wl->current_dev;
4201 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4202 /* Whoops, aliens ate up the device while we were unlocked. */
4203 return dev;
4204 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004205
Michael Buesch36dbd952009-09-04 22:51:29 +02004206 /* Disable interrupts on the device. */
4207 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004208 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004209 /* wl->mutex is locked. That is enough. */
4210 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4211 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4212 } else {
4213 spin_lock_irq(&wl->hardirq_lock);
4214 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4215 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4216 spin_unlock_irq(&wl->hardirq_lock);
4217 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004218 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004219 orig_dev = dev;
4220 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004221 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004222 b43_sdio_free_irq(dev);
4223 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004224 synchronize_irq(dev->dev->irq);
4225 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004226 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004227 mutex_lock(&wl->mutex);
4228 dev = wl->current_dev;
4229 if (!dev)
4230 return dev;
4231 if (dev != orig_dev) {
4232 if (b43_status(dev) >= B43_STAT_STARTED)
4233 goto redo;
4234 return dev;
4235 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004236 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4237 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004238
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004239 /* Drain all TX queues. */
4240 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004241 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4242 struct sk_buff *skb;
4243
4244 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4245 ieee80211_free_txskb(wl->hw, skb);
4246 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004247 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004248
Michael Buesche4d6b792007-09-18 15:39:42 -04004249 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004250 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004251 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004252
4253 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004254}
4255
4256/* Locking: wl->mutex */
4257static int b43_wireless_core_start(struct b43_wldev *dev)
4258{
4259 int err;
4260
4261 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4262
4263 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004264 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004265 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4266 if (err) {
4267 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4268 goto out;
4269 }
4270 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004271 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004272 b43_interrupt_thread_handler,
4273 IRQF_SHARED, KBUILD_MODNAME, dev);
4274 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004275 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004276 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004277 goto out;
4278 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004279 }
4280
4281 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004282 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004283 b43_set_status(dev, B43_STAT_STARTED);
4284
4285 /* Start data flow (TX/RX). */
4286 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004287 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004288
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004289 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004290 b43_periodic_tasks_setup(dev);
4291
Michael Buescha78b3bb2009-09-11 21:44:05 +02004292 b43_leds_init(dev);
4293
Michael Buesche4d6b792007-09-18 15:39:42 -04004294 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004295out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004296 return err;
4297}
4298
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004299static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4300{
4301 switch (phy_type) {
4302 case B43_PHYTYPE_A:
4303 return "A";
4304 case B43_PHYTYPE_B:
4305 return "B";
4306 case B43_PHYTYPE_G:
4307 return "G";
4308 case B43_PHYTYPE_N:
4309 return "N";
4310 case B43_PHYTYPE_LP:
4311 return "LP";
4312 case B43_PHYTYPE_SSLPN:
4313 return "SSLPN";
4314 case B43_PHYTYPE_HT:
4315 return "HT";
4316 case B43_PHYTYPE_LCN:
4317 return "LCN";
4318 case B43_PHYTYPE_LCNXN:
4319 return "LCNXN";
4320 case B43_PHYTYPE_LCN40:
4321 return "LCN40";
4322 case B43_PHYTYPE_AC:
4323 return "AC";
4324 }
4325 return "UNKNOWN";
4326}
4327
Michael Buesche4d6b792007-09-18 15:39:42 -04004328/* Get PHY and RADIO versioning numbers */
4329static int b43_phy_versioning(struct b43_wldev *dev)
4330{
4331 struct b43_phy *phy = &dev->phy;
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004332 const u8 core_rev = dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004333 u32 tmp;
4334 u8 analog_type;
4335 u8 phy_type;
4336 u8 phy_rev;
4337 u16 radio_manuf;
4338 u16 radio_ver;
4339 u16 radio_rev;
4340 int unsupported = 0;
4341
4342 /* Get PHY versioning */
4343 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4344 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4345 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4346 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004347
4348 /* LCNXN is continuation of N which run out of revisions */
4349 if (phy_type == B43_PHYTYPE_LCNXN) {
4350 phy_type = B43_PHYTYPE_N;
4351 phy_rev += 16;
4352 }
4353
Michael Buesche4d6b792007-09-18 15:39:42 -04004354 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004355#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004356 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004357 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004358 unsupported = 1;
4359 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004360#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004361#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004362 case B43_PHYTYPE_N:
Rafał Miłeckiab72efd2010-12-21 21:29:44 +01004363 if (phy_rev > 9)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004364 unsupported = 1;
4365 break;
4366#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004367#ifdef CONFIG_B43_PHY_LP
4368 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004369 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004370 unsupported = 1;
4371 break;
4372#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004373#ifdef CONFIG_B43_PHY_HT
4374 case B43_PHYTYPE_HT:
4375 if (phy_rev > 1)
4376 unsupported = 1;
4377 break;
4378#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004379#ifdef CONFIG_B43_PHY_LCN
4380 case B43_PHYTYPE_LCN:
4381 if (phy_rev > 1)
4382 unsupported = 1;
4383 break;
4384#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004385 default:
4386 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004387 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004388 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004389 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4390 analog_type, phy_type, b43_phy_name(dev, phy_type),
4391 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004392 return -EOPNOTSUPP;
4393 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004394 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4395 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004396
4397 /* Get RADIO versioning */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004398 if (core_rev == 40 || core_rev == 42) {
4399 radio_manuf = 0x17F;
4400
4401 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, 0);
4402 radio_rev = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4403
4404 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, 1);
4405 radio_ver = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4406 } else if (core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004407 u16 radio24[3];
4408
4409 for (tmp = 0; tmp < 3; tmp++) {
4410 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, tmp);
4411 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4412 }
4413
4414 /* Broadcom uses "id" for our "ver" and has separated "ver" */
4415 /* radio_ver = (radio24[0] & 0xF0) >> 4; */
4416
4417 radio_manuf = 0x17F;
4418 radio_ver = (radio24[2] << 8) | radio24[1];
4419 radio_rev = (radio24[0] & 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004420 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004421 if (dev->dev->chip_id == 0x4317) {
4422 if (dev->dev->chip_rev == 0)
4423 tmp = 0x3205017F;
4424 else if (dev->dev->chip_rev == 1)
4425 tmp = 0x4205017F;
4426 else
4427 tmp = 0x5205017F;
4428 } else {
4429 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4430 B43_RADIOCTL_ID);
4431 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
4432 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4433 B43_RADIOCTL_ID);
4434 tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH)
4435 << 16;
4436 }
4437 radio_manuf = (tmp & 0x00000FFF);
4438 radio_ver = (tmp & 0x0FFFF000) >> 12;
4439 radio_rev = (tmp & 0xF0000000) >> 28;
Michael Buesche4d6b792007-09-18 15:39:42 -04004440 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004441
Michael Buesch96c755a2008-01-06 00:09:46 +01004442 if (radio_manuf != 0x17F /* Broadcom */)
4443 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004444 switch (phy_type) {
4445 case B43_PHYTYPE_A:
4446 if (radio_ver != 0x2060)
4447 unsupported = 1;
4448 if (radio_rev != 1)
4449 unsupported = 1;
4450 if (radio_manuf != 0x17F)
4451 unsupported = 1;
4452 break;
4453 case B43_PHYTYPE_B:
4454 if ((radio_ver & 0xFFF0) != 0x2050)
4455 unsupported = 1;
4456 break;
4457 case B43_PHYTYPE_G:
4458 if (radio_ver != 0x2050)
4459 unsupported = 1;
4460 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004461 case B43_PHYTYPE_N:
Johannes Bergbb519be2008-12-24 15:26:40 +01004462 if (radio_ver != 0x2055 && radio_ver != 0x2056)
Michael Buesch96c755a2008-01-06 00:09:46 +01004463 unsupported = 1;
4464 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004465 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004466 if (radio_ver != 0x2062 && radio_ver != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004467 unsupported = 1;
4468 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004469 case B43_PHYTYPE_HT:
4470 if (radio_ver != 0x2059)
4471 unsupported = 1;
4472 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004473 case B43_PHYTYPE_LCN:
4474 if (radio_ver != 0x2064)
4475 unsupported = 1;
4476 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004477 default:
4478 B43_WARN_ON(1);
4479 }
4480 if (unsupported) {
4481 b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
4482 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
4483 radio_manuf, radio_ver, radio_rev);
4484 return -EOPNOTSUPP;
4485 }
4486 b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
4487 radio_manuf, radio_ver, radio_rev);
4488
4489 phy->radio_manuf = radio_manuf;
4490 phy->radio_ver = radio_ver;
4491 phy->radio_rev = radio_rev;
4492
4493 phy->analog = analog_type;
4494 phy->type = phy_type;
4495 phy->rev = phy_rev;
4496
4497 return 0;
4498}
4499
4500static void setup_struct_phy_for_init(struct b43_wldev *dev,
4501 struct b43_phy *phy)
4502{
Michael Buesche4d6b792007-09-18 15:39:42 -04004503 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004504 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004505 /* PHY TX errors counter. */
4506 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004507
4508#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004509 phy->phy_locked = false;
4510 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004511#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004512}
4513
4514static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4515{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004516 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004517
Michael Buesch6a724d62007-09-20 22:12:58 +02004518 /* Assume the radio is enabled. If it's not enabled, the state will
4519 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004520 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004521
4522 /* Stats */
4523 memset(&dev->stats, 0, sizeof(dev->stats));
4524
4525 setup_struct_phy_for_init(dev, &dev->phy);
4526
4527 /* IRQ related flags */
4528 dev->irq_reason = 0;
4529 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004530 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004531 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004532 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004533
4534 dev->mac_suspended = 1;
4535
4536 /* Noise calculation context */
4537 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4538}
4539
4540static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4541{
Rafał Miłecki05814832011-05-18 02:06:39 +02004542 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004543 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004544
Michael Buesch1855ba72008-04-18 20:51:41 +02004545 if (!modparam_btcoex)
4546 return;
Larry Finger95de2842007-11-09 16:57:18 -06004547 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004548 return;
4549 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4550 return;
4551
4552 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004553 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004554 hf |= B43_HF_BTCOEXALT;
4555 else
4556 hf |= B43_HF_BTCOEX;
4557 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004558}
4559
4560static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004561{
4562 if (!modparam_btcoex)
4563 return;
4564 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004565}
4566
4567static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4568{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004569 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004570 u32 tmp;
4571
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004572#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004573 if (dev->dev->bus_type != B43_BUS_SSB)
4574 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004575#else
4576 return;
4577#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004578
4579 bus = dev->dev->sdev->bus;
4580
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004581 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4582 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004583 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004584 tmp &= ~SSB_IMCFGLO_REQTO;
4585 tmp &= ~SSB_IMCFGLO_SERTO;
4586 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004587 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004588 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004589 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004590}
4591
Michael Bueschd59f7202008-04-03 18:56:19 +02004592static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4593{
4594 u16 pu_delay;
4595
4596 /* The time value is in microseconds. */
4597 if (dev->phy.type == B43_PHYTYPE_A)
4598 pu_delay = 3700;
4599 else
4600 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004601 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004602 pu_delay = 500;
4603 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4604 pu_delay = max(pu_delay, (u16)2400);
4605
4606 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4607}
4608
4609/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4610static void b43_set_pretbtt(struct b43_wldev *dev)
4611{
4612 u16 pretbtt;
4613
4614 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004615 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004616 pretbtt = 2;
4617 } else {
4618 if (dev->phy.type == B43_PHYTYPE_A)
4619 pretbtt = 120;
4620 else
4621 pretbtt = 250;
4622 }
4623 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4624 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4625}
4626
Michael Buesche4d6b792007-09-18 15:39:42 -04004627/* Shutdown a wireless core */
4628/* Locking: wl->mutex */
4629static void b43_wireless_core_exit(struct b43_wldev *dev)
4630{
Michael Buesch36dbd952009-09-04 22:51:29 +02004631 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4632 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004633 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004634
Michael Buesche4d6b792007-09-18 15:39:42 -04004635 b43_set_status(dev, B43_STAT_UNINIT);
4636
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004637 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004638 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4639 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004640
Hauke Mehrtens50023002013-08-24 00:32:34 +02004641 switch (dev->dev->bus_type) {
4642#ifdef CONFIG_B43_BCMA
4643 case B43_BUS_BCMA:
4644 bcma_core_pci_down(dev->dev->bdev->bus);
4645 break;
4646#endif
4647#ifdef CONFIG_B43_SSB
4648 case B43_BUS_SSB:
4649 /* TODO */
4650 break;
4651#endif
4652 }
4653
Michael Buesche4d6b792007-09-18 15:39:42 -04004654 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004655 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004656 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004657 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004658 if (dev->wl->current_beacon) {
4659 dev_kfree_skb_any(dev->wl->current_beacon);
4660 dev->wl->current_beacon = NULL;
4661 }
4662
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004663 b43_device_disable(dev, 0);
4664 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004665}
4666
4667/* Initialize a wireless core */
4668static int b43_wireless_core_init(struct b43_wldev *dev)
4669{
Rafał Miłecki05814832011-05-18 02:06:39 +02004670 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004671 struct b43_phy *phy = &dev->phy;
4672 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004673 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004674
4675 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4676
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004677 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004678 if (err)
4679 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004680 if (!b43_device_is_enabled(dev))
4681 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004682
Michael Bueschfb111372008-09-02 13:00:34 +02004683 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004684 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004685 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004686
4687 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004688 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004689#ifdef CONFIG_B43_BCMA
4690 case B43_BUS_BCMA:
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004691 bcma_core_pci_irq_ctl(&dev->dev->bdev->bus->drv_pci[0],
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004692 dev->dev->bdev, true);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004693 bcma_core_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004694 break;
4695#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004696#ifdef CONFIG_B43_SSB
4697 case B43_BUS_SSB:
4698 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4699 dev->dev->sdev);
4700 break;
4701#endif
4702 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004703
4704 b43_imcfglo_timeouts_workaround(dev);
4705 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004706 if (phy->ops->prepare_hardware) {
4707 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004708 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004709 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004710 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004711 err = b43_chip_init(dev);
4712 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004713 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004714 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004715 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004716 hf = b43_hf_read(dev);
4717 if (phy->type == B43_PHYTYPE_G) {
4718 hf |= B43_HF_SYMW;
4719 if (phy->rev == 1)
4720 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004721 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004722 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004723 }
4724 if (phy->radio_ver == 0x2050) {
4725 if (phy->radio_rev == 6)
4726 hf |= B43_HF_4318TSSI;
4727 if (phy->radio_rev < 6)
4728 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004729 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004730 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4731 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004732#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004733 if (dev->dev->bus_type == B43_BUS_SSB &&
4734 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4735 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004736 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004737#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004738 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004739 b43_hf_write(dev, hf);
4740
Michael Buesch74cfdba2007-10-28 16:19:44 +01004741 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4742 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004743 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4744 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4745
4746 /* Disable sending probe responses from firmware.
4747 * Setting the MaxTime to one usec will always trigger
4748 * a timeout, so we never send any probe resp.
4749 * A timeout of zero is infinite. */
4750 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4751
4752 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004753 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004754
4755 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004756 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004757 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004758 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004759 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004760 /* Maximum Contention Window */
4761 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4762
Rafał Miłecki505fb012011-05-19 15:11:27 +02004763 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004764 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004765 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004766 err = b43_pio_init(dev);
4767 } else if (dev->use_pio) {
4768 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4769 "This should not be needed and will result in lower "
4770 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004771 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004772 err = b43_pio_init(dev);
4773 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004774 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004775 err = b43_dma_init(dev);
4776 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004777 if (err)
4778 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004779 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004780 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004781 b43_bluetooth_coext_enable(dev);
4782
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004783 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004784 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004785 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004786
Michael Buesch5ab95492009-09-10 20:31:46 +02004787 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004788
4789 b43_set_status(dev, B43_STAT_INITIALIZED);
4790
Larry Finger1a8d1222007-12-14 13:59:11 +01004791out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004792 return err;
4793
Michael Bueschef1a6282008-08-27 18:53:02 +02004794err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004795 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004796err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004797 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004798 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4799 return err;
4800}
4801
Michael Buesch40faacc2007-10-28 16:29:32 +01004802static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004803 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004804{
4805 struct b43_wl *wl = hw_to_b43_wl(hw);
4806 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004807 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004808
4809 /* TODO: allow WDS/AP devices to coexist */
4810
Johannes Berg1ed32e42009-12-23 13:15:45 +01004811 if (vif->type != NL80211_IFTYPE_AP &&
4812 vif->type != NL80211_IFTYPE_MESH_POINT &&
4813 vif->type != NL80211_IFTYPE_STATION &&
4814 vif->type != NL80211_IFTYPE_WDS &&
4815 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004816 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004817
4818 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004819 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004820 goto out_mutex_unlock;
4821
Johannes Berg1ed32e42009-12-23 13:15:45 +01004822 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004823
4824 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004825 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01004826 wl->vif = vif;
4827 wl->if_type = vif->type;
4828 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004829
Michael Buesche4d6b792007-09-18 15:39:42 -04004830 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004831 b43_set_pretbtt(dev);
4832 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004833 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004834
4835 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004836 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004837 mutex_unlock(&wl->mutex);
4838
Felix Fietkau2a190322011-08-10 13:50:30 -06004839 if (err == 0)
4840 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
4841
Michael Buesche4d6b792007-09-18 15:39:42 -04004842 return err;
4843}
4844
Michael Buesch40faacc2007-10-28 16:29:32 +01004845static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004846 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004847{
4848 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04004849 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004850
Johannes Berg1ed32e42009-12-23 13:15:45 +01004851 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004852
4853 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004854
4855 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01004856 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01004857 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04004858
Rusty Russell3db1cd52011-12-19 13:56:45 +00004859 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04004860
Johannes Berg4150c572007-09-17 01:29:23 -04004861 b43_adjust_opmode(dev);
4862 memset(wl->mac_addr, 0, ETH_ALEN);
4863 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04004864
4865 mutex_unlock(&wl->mutex);
4866}
4867
Michael Buesch40faacc2007-10-28 16:29:32 +01004868static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004869{
4870 struct b43_wl *wl = hw_to_b43_wl(hw);
4871 struct b43_wldev *dev = wl->current_dev;
4872 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07004873 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004874
Michael Buesch7be1bb62008-01-23 21:10:56 +01004875 /* Kill all old instance specific information to make sure
4876 * the card won't use it in the short timeframe between start
4877 * and mac80211 reconfiguring it. */
4878 memset(wl->bssid, 0, ETH_ALEN);
4879 memset(wl->mac_addr, 0, ETH_ALEN);
4880 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004881 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01004882 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004883 wl->beacon0_uploaded = false;
4884 wl->beacon1_uploaded = false;
4885 wl->beacon_templates_virgin = true;
4886 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01004887
Johannes Berg4150c572007-09-17 01:29:23 -04004888 mutex_lock(&wl->mutex);
4889
4890 if (b43_status(dev) < B43_STAT_INITIALIZED) {
4891 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05004892 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04004893 goto out_mutex_unlock;
4894 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004895 }
4896
Johannes Berg4150c572007-09-17 01:29:23 -04004897 if (b43_status(dev) < B43_STAT_STARTED) {
4898 err = b43_wireless_core_start(dev);
4899 if (err) {
4900 if (did_init)
4901 b43_wireless_core_exit(dev);
4902 goto out_mutex_unlock;
4903 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004904 }
Johannes Berg4150c572007-09-17 01:29:23 -04004905
Johannes Bergf41f3f32009-06-07 12:30:34 -05004906 /* XXX: only do if device doesn't support rfkill irq */
4907 wiphy_rfkill_start_polling(hw->wiphy);
4908
Johannes Berg4150c572007-09-17 01:29:23 -04004909 out_mutex_unlock:
4910 mutex_unlock(&wl->mutex);
4911
Seth Forsheedbdedbd2012-04-25 17:28:00 -05004912 /*
4913 * Configuration may have been overwritten during initialization.
4914 * Reload the configuration, but only if initialization was
4915 * successful. Reloading the configuration after a failed init
4916 * may hang the system.
4917 */
4918 if (!err)
4919 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06004920
Johannes Berg4150c572007-09-17 01:29:23 -04004921 return err;
4922}
4923
Michael Buesch40faacc2007-10-28 16:29:32 +01004924static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004925{
4926 struct b43_wl *wl = hw_to_b43_wl(hw);
4927 struct b43_wldev *dev = wl->current_dev;
4928
Michael Buescha82d9922008-04-04 21:40:06 +02004929 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d1222007-12-14 13:59:11 +01004930
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004931 if (!dev)
4932 goto out;
4933
Johannes Berg4150c572007-09-17 01:29:23 -04004934 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004935 if (b43_status(dev) >= B43_STAT_STARTED) {
4936 dev = b43_wireless_core_stop(dev);
4937 if (!dev)
4938 goto out_unlock;
4939 }
Johannes Berg4150c572007-09-17 01:29:23 -04004940 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004941 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02004942
4943out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004944 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004945out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02004946 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04004947}
4948
Johannes Berg17741cd2008-09-11 00:02:02 +02004949static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
4950 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01004951{
4952 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01004953
Felix Fietkau8f611282009-11-07 18:37:37 +01004954 /* FIXME: add locking */
Johannes Berg9d139c82008-07-09 14:40:37 +02004955 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01004956
4957 return 0;
4958}
4959
Johannes Berg38968d02008-02-25 16:27:50 +01004960static void b43_op_sta_notify(struct ieee80211_hw *hw,
4961 struct ieee80211_vif *vif,
4962 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02004963 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01004964{
4965 struct b43_wl *wl = hw_to_b43_wl(hw);
4966
4967 B43_WARN_ON(!vif || wl->vif != vif);
4968}
4969
Michael Buesch25d3ef52009-02-20 15:39:21 +01004970static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
4971{
4972 struct b43_wl *wl = hw_to_b43_wl(hw);
4973 struct b43_wldev *dev;
4974
4975 mutex_lock(&wl->mutex);
4976 dev = wl->current_dev;
4977 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
4978 /* Disable CFP update during scan on other channels. */
4979 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
4980 }
4981 mutex_unlock(&wl->mutex);
4982}
4983
4984static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
4985{
4986 struct b43_wl *wl = hw_to_b43_wl(hw);
4987 struct b43_wldev *dev;
4988
4989 mutex_lock(&wl->mutex);
4990 dev = wl->current_dev;
4991 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
4992 /* Re-enable CFP update. */
4993 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
4994 }
4995 mutex_unlock(&wl->mutex);
4996}
4997
John W. Linville354b4f02010-04-29 15:56:06 -04004998static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
4999 struct survey_info *survey)
5000{
5001 struct b43_wl *wl = hw_to_b43_wl(hw);
5002 struct b43_wldev *dev = wl->current_dev;
5003 struct ieee80211_conf *conf = &hw->conf;
5004
5005 if (idx != 0)
5006 return -ENOENT;
5007
Karl Beldan675a0b02013-03-25 16:26:57 +01005008 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04005009 survey->filled = SURVEY_INFO_NOISE_DBM;
5010 survey->noise = dev->stats.link_noise;
5011
5012 return 0;
5013}
5014
Michael Buesche4d6b792007-09-18 15:39:42 -04005015static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01005016 .tx = b43_op_tx,
5017 .conf_tx = b43_op_conf_tx,
5018 .add_interface = b43_op_add_interface,
5019 .remove_interface = b43_op_remove_interface,
5020 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005021 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005022 .configure_filter = b43_op_configure_filter,
5023 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005024 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005025 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005026 .get_tsf = b43_op_get_tsf,
5027 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005028 .start = b43_op_start,
5029 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005030 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005031 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005032 .sw_scan_start = b43_op_sw_scan_start_notifier,
5033 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005034 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005035 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005036};
5037
5038/* Hard-reset the chip. Do not call this directly.
5039 * Use b43_controller_restart()
5040 */
5041static void b43_chip_reset(struct work_struct *work)
5042{
5043 struct b43_wldev *dev =
5044 container_of(work, struct b43_wldev, restart_work);
5045 struct b43_wl *wl = dev->wl;
5046 int err = 0;
5047 int prev_status;
5048
5049 mutex_lock(&wl->mutex);
5050
5051 prev_status = b43_status(dev);
5052 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005053 if (prev_status >= B43_STAT_STARTED) {
5054 dev = b43_wireless_core_stop(dev);
5055 if (!dev) {
5056 err = -ENODEV;
5057 goto out;
5058 }
5059 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005060 if (prev_status >= B43_STAT_INITIALIZED)
5061 b43_wireless_core_exit(dev);
5062
5063 /* ...and up again. */
5064 if (prev_status >= B43_STAT_INITIALIZED) {
5065 err = b43_wireless_core_init(dev);
5066 if (err)
5067 goto out;
5068 }
5069 if (prev_status >= B43_STAT_STARTED) {
5070 err = b43_wireless_core_start(dev);
5071 if (err) {
5072 b43_wireless_core_exit(dev);
5073 goto out;
5074 }
5075 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005076out:
5077 if (err)
5078 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005079 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005080
5081 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005082 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005083 return;
5084 }
5085
5086 /* reload configuration */
5087 b43_op_config(wl->hw, ~0);
5088 if (wl->vif)
5089 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5090
5091 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005092}
5093
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005094static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005095 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005096{
5097 struct ieee80211_hw *hw = dev->wl->hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005098
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005099 if (have_2ghz_phy)
5100 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
5101 if (dev->phy.type == B43_PHYTYPE_N) {
5102 if (have_5ghz_phy)
5103 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
5104 } else {
5105 if (have_5ghz_phy)
5106 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5107 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005108
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005109 dev->phy.supports_2ghz = have_2ghz_phy;
5110 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005111
5112 return 0;
5113}
5114
5115static void b43_wireless_core_detach(struct b43_wldev *dev)
5116{
5117 /* We release firmware that late to not be required to re-request
5118 * is all the time when we reinit the core. */
5119 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005120 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005121}
5122
Rafał Miłecki075ca602014-05-19 23:18:54 +02005123static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5124 bool *have_5ghz_phy)
5125{
5126 u16 dev_id = 0;
5127
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005128#ifdef CONFIG_B43_BCMA
5129 if (dev->dev->bus_type == B43_BUS_BCMA &&
5130 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5131 dev_id = dev->dev->bdev->bus->host_pci->device;
5132#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005133#ifdef CONFIG_B43_SSB
5134 if (dev->dev->bus_type == B43_BUS_SSB &&
5135 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5136 dev_id = dev->dev->sdev->bus->host_pci->device;
5137#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005138 /* Override with SPROM value if available */
5139 if (dev->dev->bus_sprom->dev_id)
5140 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005141
5142 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5143 switch (dev_id) {
5144 case 0x4324: /* BCM4306 */
5145 case 0x4312: /* BCM4311 */
5146 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005147 case 0x4328: /* BCM4321 */
5148 case 0x432b: /* BCM4322 */
5149 case 0x4350: /* BCM43222 */
5150 case 0x4353: /* BCM43224 */
5151 case 0x0576: /* BCM43224 */
5152 case 0x435f: /* BCM6362 */
5153 case 0x4331: /* BCM4331 */
5154 case 0x4359: /* BCM43228 */
5155 case 0x43a0: /* BCM4360 */
5156 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005157 /* Dual band devices */
5158 *have_2ghz_phy = true;
5159 *have_5ghz_phy = true;
5160 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005161 case 0x4321: /* BCM4306 */
5162 case 0x4313: /* BCM4311 */
5163 case 0x431a: /* BCM4318 */
5164 case 0x432a: /* BCM4321 */
5165 case 0x432d: /* BCM4322 */
5166 case 0x4352: /* BCM43222 */
5167 case 0x4333: /* BCM4331 */
5168 case 0x43a2: /* BCM4360 */
5169 case 0x43b3: /* BCM4352 */
5170 /* 5 GHz only devices */
5171 *have_2ghz_phy = false;
5172 *have_5ghz_phy = true;
5173 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005174 }
5175
5176 /* As a fallback, try to guess using PHY type */
5177 switch (dev->phy.type) {
5178 case B43_PHYTYPE_A:
5179 *have_2ghz_phy = false;
5180 *have_5ghz_phy = true;
5181 return;
5182 case B43_PHYTYPE_G:
5183 case B43_PHYTYPE_N:
5184 case B43_PHYTYPE_LP:
5185 case B43_PHYTYPE_HT:
5186 case B43_PHYTYPE_LCN:
5187 *have_2ghz_phy = true;
5188 *have_5ghz_phy = false;
5189 return;
5190 }
5191
5192 B43_WARN_ON(1);
5193}
5194
Michael Buesche4d6b792007-09-18 15:39:42 -04005195static int b43_wireless_core_attach(struct b43_wldev *dev)
5196{
5197 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005198 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005199 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005200 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005201 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005202
5203 /* Do NOT do any device initialization here.
5204 * Do it in wireless_core_init() instead.
5205 * This function is for gathering basic information about the HW, only.
5206 * Also some structs may be set up here. But most likely you want to have
5207 * that in core_init(), too.
5208 */
5209
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005210 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005211 if (err) {
5212 b43err(wl, "Bus powerup failed\n");
5213 goto out;
5214 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005215
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005216 phy->do_full_init = true;
5217
Rafał Miłecki075ca602014-05-19 23:18:54 +02005218 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005219 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005220#ifdef CONFIG_B43_BCMA
5221 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005222 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5223 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5224 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005225 break;
5226#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005227#ifdef CONFIG_B43_SSB
5228 case B43_BUS_SSB:
5229 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005230 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5231 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5232 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005233 } else
5234 B43_WARN_ON(1);
5235 break;
5236#endif
5237 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005238
Michael Buesch96c755a2008-01-06 00:09:46 +01005239 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005240 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005241
Rafał Miłecki075ca602014-05-19 23:18:54 +02005242 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005243 err = b43_phy_versioning(dev);
5244 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005245 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005246
5247 /* Get real info about supported bands */
5248 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5249
5250 /* We don't support 5 GHz on some PHYs yet */
5251 switch (dev->phy.type) {
5252 case B43_PHYTYPE_A:
5253 case B43_PHYTYPE_N:
5254 case B43_PHYTYPE_LP:
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005255 case B43_PHYTYPE_HT:
Rafał Miłecki075ca602014-05-19 23:18:54 +02005256 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00005257 have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005258 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005259
5260 if (!have_2ghz_phy && !have_5ghz_phy) {
5261 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005262 err = -EOPNOTSUPP;
5263 goto err_powerdown;
5264 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005265
Michael Bueschfb111372008-09-02 13:00:34 +02005266 err = b43_phy_allocate(dev);
5267 if (err)
5268 goto err_powerdown;
5269
Michael Buesch96c755a2008-01-06 00:09:46 +01005270 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005271 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005272
5273 err = b43_validate_chipaccess(dev);
5274 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005275 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005276 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005277 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005278 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005279
5280 /* Now set some default "current_dev" */
5281 if (!wl->current_dev)
5282 wl->current_dev = dev;
5283 INIT_WORK(&dev->restart_work, b43_chip_reset);
5284
Michael Bueschcb24f572008-09-03 12:12:20 +02005285 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005286 b43_device_disable(dev, 0);
5287 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005288
5289out:
5290 return err;
5291
Michael Bueschfb111372008-09-02 13:00:34 +02005292err_phy_free:
5293 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005294err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005295 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005296 return err;
5297}
5298
Rafał Miłecki482f0532011-05-18 02:06:36 +02005299static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005300{
5301 struct b43_wldev *wldev;
5302 struct b43_wl *wl;
5303
Michael Buesch3bf0a322008-05-22 16:32:16 +02005304 /* Do not cancel ieee80211-workqueue based work here.
5305 * See comment in b43_remove(). */
5306
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005307 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005308 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005309 b43_debugfs_remove_device(wldev);
5310 b43_wireless_core_detach(wldev);
5311 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005312 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005313 kfree(wldev);
5314}
5315
Rafał Miłecki482f0532011-05-18 02:06:36 +02005316static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005317{
5318 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005319 int err = -ENOMEM;
5320
Michael Buesche4d6b792007-09-18 15:39:42 -04005321 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5322 if (!wldev)
5323 goto out;
5324
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005325 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005326 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005327 wldev->wl = wl;
5328 b43_set_status(wldev, B43_STAT_UNINIT);
5329 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005330 INIT_LIST_HEAD(&wldev->list);
5331
5332 err = b43_wireless_core_attach(wldev);
5333 if (err)
5334 goto err_kfree_wldev;
5335
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005336 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005337 b43_debugfs_add_device(wldev);
5338
5339 out:
5340 return err;
5341
5342 err_kfree_wldev:
5343 kfree(wldev);
5344 return err;
5345}
5346
Michael Buesch9fc38452008-04-19 16:53:00 +02005347#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5348 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5349 (pdev->device == _device) && \
5350 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5351 (pdev->subsystem_device == _subdevice) )
5352
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005353#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005354static void b43_sprom_fixup(struct ssb_bus *bus)
5355{
Michael Buesch1855ba72008-04-18 20:51:41 +02005356 struct pci_dev *pdev;
5357
Michael Buesche4d6b792007-09-18 15:39:42 -04005358 /* boardflags workarounds */
5359 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005360 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005361 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005362 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005363 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005364 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005365 if (bus->bustype == SSB_BUSTYPE_PCI) {
5366 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005367 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005368 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005369 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005370 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005371 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005372 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5373 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005374 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5375 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005376}
5377
Rafał Miłecki482f0532011-05-18 02:06:36 +02005378static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005379{
5380 struct ieee80211_hw *hw = wl->hw;
5381
Rafał Miłecki482f0532011-05-18 02:06:36 +02005382 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005383 ieee80211_free_hw(hw);
5384}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005385#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005386
Rafał Miłeckid1507052011-07-05 23:54:07 +02005387static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005388{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005389 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005390 struct ieee80211_hw *hw;
5391 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005392 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005393 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005394
5395 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5396 if (!hw) {
5397 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005398 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005399 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005400 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005401
5402 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005403 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005404 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005405
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005406 hw->wiphy->interface_modes =
5407 BIT(NL80211_IFTYPE_AP) |
5408 BIT(NL80211_IFTYPE_MESH_POINT) |
5409 BIT(NL80211_IFTYPE_STATION) |
5410 BIT(NL80211_IFTYPE_WDS) |
5411 BIT(NL80211_IFTYPE_ADHOC);
5412
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005413 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5414
Oleksij Rempele64add22012-06-05 20:39:32 +02005415 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005416 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005417 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005418 if (is_valid_ether_addr(sprom->et1mac))
5419 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005420 else
Larry Finger95de2842007-11-09 16:57:18 -06005421 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005422
Michael Buesch403a3a12009-06-08 21:04:57 +02005423 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005424 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005425 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005426 spin_lock_init(&wl->hardirq_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005427 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005428 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005429 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005430
5431 /* Initialize queues and flags. */
5432 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5433 skb_queue_head_init(&wl->tx_queue[queue_num]);
5434 wl->tx_queue_stopped[queue_num] = 0;
5435 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005436
Rafał Miłecki2729df22011-07-18 22:45:58 +02005437 snprintf(chip_name, ARRAY_SIZE(chip_name),
5438 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5439 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5440 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005441 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005442}
5443
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005444#ifdef CONFIG_B43_BCMA
5445static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005446{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005447 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005448 struct b43_wl *wl;
5449 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005450
Rafał Miłecki89604002013-06-26 09:55:54 +02005451 if (!modparam_allhwsupport &&
5452 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5453 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5454 return -ENOTSUPP;
5455 }
5456
Rafał Miłecki397915c2011-07-06 19:03:46 +02005457 dev = b43_bus_dev_bcma_init(core);
5458 if (!dev)
5459 return -ENODEV;
5460
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005461 wl = b43_wireless_init(dev);
5462 if (IS_ERR(wl)) {
5463 err = PTR_ERR(wl);
5464 goto bcma_out;
5465 }
5466
5467 err = b43_one_core_attach(dev, wl);
5468 if (err)
5469 goto bcma_err_wireless_exit;
5470
Larry Finger6b6fa582012-03-08 22:27:46 -06005471 /* setup and start work to load firmware */
5472 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5473 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005474
5475bcma_out:
5476 return err;
5477
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005478bcma_err_wireless_exit:
5479 ieee80211_free_hw(wl->hw);
5480 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005481}
5482
5483static void b43_bcma_remove(struct bcma_device *core)
5484{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005485 struct b43_wldev *wldev = bcma_get_drvdata(core);
5486 struct b43_wl *wl = wldev->wl;
5487
5488 /* We must cancel any work here before unregistering from ieee80211,
5489 * as the ieee80211 unreg will destroy the workqueue. */
5490 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005491 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005492
Oleksij Rempele64add22012-06-05 20:39:32 +02005493 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005494 if (!wldev->fw.ucode.data)
5495 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005496 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005497 b43_leds_stop(wldev);
5498 ieee80211_unregister_hw(wl->hw);
5499 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005500
5501 b43_one_core_detach(wldev->dev);
5502
Larry Finger09164042014-01-12 15:11:37 -06005503 /* Unregister HW RNG driver */
5504 b43_rng_exit(wl);
5505
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005506 b43_leds_unregister(wl);
5507
5508 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005509}
5510
5511static struct bcma_driver b43_bcma_driver = {
5512 .name = KBUILD_MODNAME,
5513 .id_table = b43_bcma_tbl,
5514 .probe = b43_bcma_probe,
5515 .remove = b43_bcma_remove,
5516};
5517#endif
5518
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005519#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005520static
5521int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005522{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005523 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005524 struct b43_wl *wl;
5525 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005526
Rafał Miłecki482f0532011-05-18 02:06:36 +02005527 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005528 if (!dev)
5529 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005530
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005531 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005532 if (wl) {
5533 b43err(NULL, "Dual-core devices are not supported\n");
5534 err = -ENOTSUPP;
5535 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005536 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005537
5538 b43_sprom_fixup(sdev->bus);
5539
5540 wl = b43_wireless_init(dev);
5541 if (IS_ERR(wl)) {
5542 err = PTR_ERR(wl);
5543 goto err_ssb_kfree_dev;
5544 }
5545 ssb_set_devtypedata(sdev, wl);
5546 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5547
Michael Buesche4d6b792007-09-18 15:39:42 -04005548 err = b43_one_core_attach(dev, wl);
5549 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005550 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005551
Larry Finger6b6fa582012-03-08 22:27:46 -06005552 /* setup and start work to load firmware */
5553 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5554 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005555
Michael Buesche4d6b792007-09-18 15:39:42 -04005556 return err;
5557
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005558err_ssb_wireless_exit:
5559 b43_wireless_exit(dev, wl);
5560err_ssb_kfree_dev:
5561 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005562 return err;
5563}
5564
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005565static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005566{
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005567 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5568 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005569 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005570
Michael Buesch3bf0a322008-05-22 16:32:16 +02005571 /* We must cancel any work here before unregistering from ieee80211,
5572 * as the ieee80211 unreg will destroy the workqueue. */
5573 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005574 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005575
Michael Buesche4d6b792007-09-18 15:39:42 -04005576 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005577 if (!wldev->fw.ucode.data)
5578 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005579 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005580 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005581 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005582 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005583
Pavel Roskine61b52d2011-07-22 18:07:13 -04005584 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005585
Larry Finger09164042014-01-12 15:11:37 -06005586 /* Unregister HW RNG driver */
5587 b43_rng_exit(wl);
5588
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005589 b43_leds_unregister(wl);
5590 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005591}
5592
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005593static struct ssb_driver b43_ssb_driver = {
5594 .name = KBUILD_MODNAME,
5595 .id_table = b43_ssb_tbl,
5596 .probe = b43_ssb_probe,
5597 .remove = b43_ssb_remove,
5598};
5599#endif /* CONFIG_B43_SSB */
5600
Michael Buesche4d6b792007-09-18 15:39:42 -04005601/* Perform a hardware reset. This can be called from any context. */
5602void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5603{
5604 /* Must avoid requeueing, if we are in shutdown. */
5605 if (b43_status(dev) < B43_STAT_INITIALIZED)
5606 return;
5607 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005608 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005609}
5610
Michael Buesch26bc7832008-02-09 00:18:35 +01005611static void b43_print_driverinfo(void)
5612{
5613 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005614 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005615
5616#ifdef CONFIG_B43_PCI_AUTOSELECT
5617 feat_pci = "P";
5618#endif
5619#ifdef CONFIG_B43_PCMCIA
5620 feat_pcmcia = "M";
5621#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005622#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005623 feat_nphy = "N";
5624#endif
5625#ifdef CONFIG_B43_LEDS
5626 feat_leds = "L";
5627#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005628#ifdef CONFIG_B43_SDIO
5629 feat_sdio = "S";
5630#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005631 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005632 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005633 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005634 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005635}
5636
Michael Buesche4d6b792007-09-18 15:39:42 -04005637static int __init b43_init(void)
5638{
5639 int err;
5640
5641 b43_debugfs_init();
5642 err = b43_pcmcia_init();
5643 if (err)
5644 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005645 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005646 if (err)
5647 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005648#ifdef CONFIG_B43_BCMA
5649 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005650 if (err)
5651 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005652#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005653#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005654 err = ssb_driver_register(&b43_ssb_driver);
5655 if (err)
5656 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005657#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005658 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005659
5660 return err;
5661
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005662#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005663err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005664#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005665#ifdef CONFIG_B43_BCMA
5666 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005667err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005668#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005669 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005670err_pcmcia_exit:
5671 b43_pcmcia_exit();
5672err_dfs_exit:
5673 b43_debugfs_exit();
5674 return err;
5675}
5676
5677static void __exit b43_exit(void)
5678{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005679#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005680 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005681#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005682#ifdef CONFIG_B43_BCMA
5683 bcma_driver_unregister(&b43_bcma_driver);
5684#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005685 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005686 b43_pcmcia_exit();
5687 b43_debugfs_exit();
5688}
5689
5690module_init(b43_init)
5691module_exit(b43_exit)