blob: 3dcd3aa38608ff0daf41b084bfa2e6038ab449b2 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio1f21ad22007-11-06 22:49:20 +01006 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Michael Büscheb032b92011-07-04 20:50:05 +02007 Copyright (c) 2005-2009 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -04008 Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
Rafał Miłecki108f4f32011-09-03 21:01:02 +020010 Copyright (c) 2010-2011 Rafał Miłecki <zajec5@gmail.com>
Michael Buesche4d6b792007-09-18 15:39:42 -040011
Albert Herranz3dbba8e2009-09-10 19:34:49 +020012 SDIO support
13 Copyright (c) 2009 Albert Herranz <albert_herranz@yahoo.es>
14
Michael Buesche4d6b792007-09-18 15:39:42 -040015 Some parts of the code in this file are derived from the ipw2200
16 driver Copyright(c) 2003 - 2004 Intel Corporation.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; see the file COPYING. If not, write to
30 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
31 Boston, MA 02110-1301, USA.
32
33*/
34
35#include <linux/delay.h>
36#include <linux/init.h>
Paul Gortmakerac5c24e92011-08-30 14:18:44 -040037#include <linux/module.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040038#include <linux/if_arp.h>
39#include <linux/etherdevice.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040040#include <linux/firmware.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040041#include <linux/workqueue.h>
42#include <linux/skbuff.h>
Andrew Morton96cf49a2008-02-04 22:27:19 -080043#include <linux/io.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040044#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040046#include <asm/unaligned.h>
47
48#include "b43.h"
49#include "main.h"
50#include "debugfs.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020051#include "phy_common.h"
52#include "phy_g.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020053#include "phy_n.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040054#include "dma.h"
Michael Buesch5100d5a2008-03-29 21:01:16 +010055#include "pio.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040056#include "sysfs.h"
57#include "xmit.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040058#include "lo.h"
59#include "pcmcia.h"
Albert Herranz3dbba8e2009-09-10 19:34:49 +020060#include "sdio.h"
61#include <linux/mmc/sdio_func.h>
Michael Buesche4d6b792007-09-18 15:39:42 -040062
63MODULE_DESCRIPTION("Broadcom B43 wireless driver");
64MODULE_AUTHOR("Martin Langer");
65MODULE_AUTHOR("Stefano Brivio");
66MODULE_AUTHOR("Michael Buesch");
Gábor Stefanik0136e512009-08-28 22:32:17 +020067MODULE_AUTHOR("Gábor Stefanik");
Rafał Miłecki108f4f32011-09-03 21:01:02 +020068MODULE_AUTHOR("Rafał Miłecki");
Michael Buesche4d6b792007-09-18 15:39:42 -040069MODULE_LICENSE("GPL");
70
Tim Gardner6021e082010-01-07 11:10:38 -070071MODULE_FIRMWARE("b43/ucode11.fw");
72MODULE_FIRMWARE("b43/ucode13.fw");
73MODULE_FIRMWARE("b43/ucode14.fw");
74MODULE_FIRMWARE("b43/ucode15.fw");
Rafał Miłeckif6158392011-04-19 22:49:29 +020075MODULE_FIRMWARE("b43/ucode16_mimo.fw");
Tim Gardner6021e082010-01-07 11:10:38 -070076MODULE_FIRMWARE("b43/ucode5.fw");
77MODULE_FIRMWARE("b43/ucode9.fw");
Michael Buesche4d6b792007-09-18 15:39:42 -040078
79static int modparam_bad_frames_preempt;
80module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
81MODULE_PARM_DESC(bad_frames_preempt,
82 "enable(1) / disable(0) Bad Frames Preemption");
83
Michael Buesche4d6b792007-09-18 15:39:42 -040084static char modparam_fwpostfix[16];
85module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
86MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
87
Michael Buesche4d6b792007-09-18 15:39:42 -040088static int modparam_hwpctl;
89module_param_named(hwpctl, modparam_hwpctl, int, 0444);
90MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
91
92static int modparam_nohwcrypt;
93module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
94MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
95
gregor kowski035d0242009-08-19 22:35:45 +020096static int modparam_hwtkip;
97module_param_named(hwtkip, modparam_hwtkip, int, 0444);
98MODULE_PARM_DESC(hwtkip, "Enable hardware tkip.");
99
Michael Buesch403a3a12009-06-08 21:04:57 +0200100static int modparam_qos = 1;
101module_param_named(qos, modparam_qos, int, 0444);
Michael Buesche6f5b932008-03-05 21:18:49 +0100102MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
103
Michael Buesch1855ba72008-04-18 20:51:41 +0200104static int modparam_btcoex = 1;
105module_param_named(btcoex, modparam_btcoex, int, 0444);
Gábor Stefanikc71dbd32009-08-28 22:34:21 +0200106MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistence (default on)");
Michael Buesch1855ba72008-04-18 20:51:41 +0200107
Michael Buesch060210f2009-01-25 15:49:59 +0100108int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
109module_param_named(verbose, b43_modparam_verbose, int, 0644);
110MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
111
Rafał Miłeckidf766262011-08-16 12:14:07 +0200112static int b43_modparam_pio = 0;
Linus Torvalds9e3bd912010-02-26 10:34:27 -0800113module_param_named(pio, b43_modparam_pio, int, 0644);
114MODULE_PARM_DESC(pio, "Use PIO accesses by default: 0=DMA, 1=PIO");
Michael Buesche6f5b932008-03-05 21:18:49 +0100115
Rafał Miłecki89604002013-06-26 09:55:54 +0200116static int modparam_allhwsupport = !IS_ENABLED(CONFIG_BRCMSMAC);
117module_param_named(allhwsupport, modparam_allhwsupport, int, 0444);
118MODULE_PARM_DESC(allhwsupport, "Enable support for all hardware (even it if overlaps with the brcmsmac driver)");
119
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200120#ifdef CONFIG_B43_BCMA
121static const struct bcma_device_id b43_bcma_tbl[] = {
Hauke Mehrtensc027ed42011-07-23 13:57:34 +0200122 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x11, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200123 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x17, BCMA_ANY_CLASS),
124 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x18, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200125 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1C, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200126 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1D, BCMA_ANY_CLASS),
Rafał Miłecki15be8e82014-07-01 16:33:57 +0200127 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1E, BCMA_ANY_CLASS),
128 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x28, BCMA_ANY_CLASS),
129 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x2A, BCMA_ANY_CLASS),
Rafał Miłecki3c65ab62011-06-02 09:56:04 +0200130 BCMA_CORETABLE_END
131};
132MODULE_DEVICE_TABLE(bcma, b43_bcma_tbl);
133#endif
134
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200135#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -0400136static const struct ssb_device_id b43_ssb_tbl[] = {
137 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
138 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
139 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
140 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
141 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
Michael Bueschd5c71e42008-01-04 17:06:29 +0100142 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
Rafał Miłecki003d6d22010-01-15 12:10:53 +0100143 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 12),
Larry Finger013978b2007-11-26 10:29:47 -0600144 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
Michael Buesch6b1c7c62008-12-25 00:39:28 +0100145 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
Johannes Berg92d61282008-12-24 12:44:09 +0100146 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
Michael Buesche4d6b792007-09-18 15:39:42 -0400147 SSB_DEVTABLE_END
148};
Michael Buesche4d6b792007-09-18 15:39:42 -0400149MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200150#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400151
152/* Channel and ratetables are shared for all devices.
153 * They can't be const, because ieee80211 puts some precalculated
154 * data in there. This data is the same for all devices, so we don't
155 * get concurrency issues */
156#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +0100157 { \
158 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
159 .hw_value = (_rateid), \
160 .flags = (_flags), \
Michael Buesche4d6b792007-09-18 15:39:42 -0400161 }
Johannes Berg8318d782008-01-24 19:38:38 +0100162
163/*
164 * NOTE: When changing this, sync with xmit.c's
165 * b43_plcp_get_bitrate_idx_* functions!
166 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400167static struct ieee80211_rate __b43_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100168 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
169 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
170 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
171 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
172 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
173 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
174 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
175 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
176 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
177 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
178 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
179 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400180};
181
182#define b43_a_ratetable (__b43_ratetable + 4)
183#define b43_a_ratetable_size 8
184#define b43_b_ratetable (__b43_ratetable + 0)
185#define b43_b_ratetable_size 4
186#define b43_g_ratetable (__b43_ratetable + 0)
187#define b43_g_ratetable_size 12
188
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200189#define CHAN2G(_channel, _freq, _flags) { \
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100190 .band = IEEE80211_BAND_2GHZ, \
191 .center_freq = (_freq), \
192 .hw_value = (_channel), \
193 .flags = (_flags), \
194 .max_antenna_gain = 0, \
195 .max_power = 30, \
196}
Michael Buesch96c755a2008-01-06 00:09:46 +0100197static struct ieee80211_channel b43_2ghz_chantable[] = {
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200198 CHAN2G(1, 2412, 0),
199 CHAN2G(2, 2417, 0),
200 CHAN2G(3, 2422, 0),
201 CHAN2G(4, 2427, 0),
202 CHAN2G(5, 2432, 0),
203 CHAN2G(6, 2437, 0),
204 CHAN2G(7, 2442, 0),
205 CHAN2G(8, 2447, 0),
206 CHAN2G(9, 2452, 0),
207 CHAN2G(10, 2457, 0),
208 CHAN2G(11, 2462, 0),
209 CHAN2G(12, 2467, 0),
210 CHAN2G(13, 2472, 0),
211 CHAN2G(14, 2484, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100212};
Rafał Miłecki3695b932014-07-08 15:11:10 +0200213
214/* No support for the last 3 channels (12, 13, 14) */
215#define b43_2ghz_chantable_limited_size 11
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200216#undef CHAN2G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100217
Rafał Miłecki91211732014-05-21 08:44:20 +0200218#define CHAN4G(_channel, _flags) { \
219 .band = IEEE80211_BAND_5GHZ, \
220 .center_freq = 4000 + (5 * (_channel)), \
221 .hw_value = (_channel), \
222 .flags = (_flags), \
223 .max_antenna_gain = 0, \
224 .max_power = 30, \
225}
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100226#define CHAN5G(_channel, _flags) { \
227 .band = IEEE80211_BAND_5GHZ, \
228 .center_freq = 5000 + (5 * (_channel)), \
229 .hw_value = (_channel), \
230 .flags = (_flags), \
231 .max_antenna_gain = 0, \
232 .max_power = 30, \
233}
234static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
Rafał Miłecki91211732014-05-21 08:44:20 +0200235 CHAN4G(184, 0), CHAN4G(186, 0),
236 CHAN4G(188, 0), CHAN4G(190, 0),
237 CHAN4G(192, 0), CHAN4G(194, 0),
238 CHAN4G(196, 0), CHAN4G(198, 0),
239 CHAN4G(200, 0), CHAN4G(202, 0),
240 CHAN4G(204, 0), CHAN4G(206, 0),
241 CHAN4G(208, 0), CHAN4G(210, 0),
242 CHAN4G(212, 0), CHAN4G(214, 0),
243 CHAN4G(216, 0), CHAN4G(218, 0),
244 CHAN4G(220, 0), CHAN4G(222, 0),
245 CHAN4G(224, 0), CHAN4G(226, 0),
246 CHAN4G(228, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100247 CHAN5G(32, 0), CHAN5G(34, 0),
248 CHAN5G(36, 0), CHAN5G(38, 0),
249 CHAN5G(40, 0), CHAN5G(42, 0),
250 CHAN5G(44, 0), CHAN5G(46, 0),
251 CHAN5G(48, 0), CHAN5G(50, 0),
252 CHAN5G(52, 0), CHAN5G(54, 0),
253 CHAN5G(56, 0), CHAN5G(58, 0),
254 CHAN5G(60, 0), CHAN5G(62, 0),
255 CHAN5G(64, 0), CHAN5G(66, 0),
256 CHAN5G(68, 0), CHAN5G(70, 0),
257 CHAN5G(72, 0), CHAN5G(74, 0),
258 CHAN5G(76, 0), CHAN5G(78, 0),
259 CHAN5G(80, 0), CHAN5G(82, 0),
260 CHAN5G(84, 0), CHAN5G(86, 0),
261 CHAN5G(88, 0), CHAN5G(90, 0),
262 CHAN5G(92, 0), CHAN5G(94, 0),
263 CHAN5G(96, 0), CHAN5G(98, 0),
264 CHAN5G(100, 0), CHAN5G(102, 0),
265 CHAN5G(104, 0), CHAN5G(106, 0),
266 CHAN5G(108, 0), CHAN5G(110, 0),
267 CHAN5G(112, 0), CHAN5G(114, 0),
268 CHAN5G(116, 0), CHAN5G(118, 0),
269 CHAN5G(120, 0), CHAN5G(122, 0),
270 CHAN5G(124, 0), CHAN5G(126, 0),
271 CHAN5G(128, 0), CHAN5G(130, 0),
272 CHAN5G(132, 0), CHAN5G(134, 0),
273 CHAN5G(136, 0), CHAN5G(138, 0),
274 CHAN5G(140, 0), CHAN5G(142, 0),
275 CHAN5G(144, 0), CHAN5G(145, 0),
276 CHAN5G(146, 0), CHAN5G(147, 0),
277 CHAN5G(148, 0), CHAN5G(149, 0),
278 CHAN5G(150, 0), CHAN5G(151, 0),
279 CHAN5G(152, 0), CHAN5G(153, 0),
280 CHAN5G(154, 0), CHAN5G(155, 0),
281 CHAN5G(156, 0), CHAN5G(157, 0),
282 CHAN5G(158, 0), CHAN5G(159, 0),
283 CHAN5G(160, 0), CHAN5G(161, 0),
284 CHAN5G(162, 0), CHAN5G(163, 0),
285 CHAN5G(164, 0), CHAN5G(165, 0),
286 CHAN5G(166, 0), CHAN5G(168, 0),
287 CHAN5G(170, 0), CHAN5G(172, 0),
288 CHAN5G(174, 0), CHAN5G(176, 0),
289 CHAN5G(178, 0), CHAN5G(180, 0),
Rafał Miłecki91211732014-05-21 08:44:20 +0200290 CHAN5G(182, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400291};
292
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100293static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
294 CHAN5G(34, 0), CHAN5G(36, 0),
295 CHAN5G(38, 0), CHAN5G(40, 0),
296 CHAN5G(42, 0), CHAN5G(44, 0),
297 CHAN5G(46, 0), CHAN5G(48, 0),
298 CHAN5G(52, 0), CHAN5G(56, 0),
299 CHAN5G(60, 0), CHAN5G(64, 0),
300 CHAN5G(100, 0), CHAN5G(104, 0),
301 CHAN5G(108, 0), CHAN5G(112, 0),
302 CHAN5G(116, 0), CHAN5G(120, 0),
303 CHAN5G(124, 0), CHAN5G(128, 0),
304 CHAN5G(132, 0), CHAN5G(136, 0),
305 CHAN5G(140, 0), CHAN5G(149, 0),
306 CHAN5G(153, 0), CHAN5G(157, 0),
307 CHAN5G(161, 0), CHAN5G(165, 0),
308 CHAN5G(184, 0), CHAN5G(188, 0),
309 CHAN5G(192, 0), CHAN5G(196, 0),
310 CHAN5G(200, 0), CHAN5G(204, 0),
311 CHAN5G(208, 0), CHAN5G(212, 0),
312 CHAN5G(216, 0),
313};
Rafał Miłecki91211732014-05-21 08:44:20 +0200314#undef CHAN4G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100315#undef CHAN5G
316
317static struct ieee80211_supported_band b43_band_5GHz_nphy = {
318 .band = IEEE80211_BAND_5GHZ,
319 .channels = b43_5ghz_nphy_chantable,
320 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
321 .bitrates = b43_a_ratetable,
322 .n_bitrates = b43_a_ratetable_size,
Michael Buesche4d6b792007-09-18 15:39:42 -0400323};
Johannes Berg8318d782008-01-24 19:38:38 +0100324
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100325static struct ieee80211_supported_band b43_band_5GHz_aphy = {
326 .band = IEEE80211_BAND_5GHZ,
327 .channels = b43_5ghz_aphy_chantable,
328 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
329 .bitrates = b43_a_ratetable,
330 .n_bitrates = b43_a_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100331};
Michael Buesche4d6b792007-09-18 15:39:42 -0400332
Johannes Berg8318d782008-01-24 19:38:38 +0100333static struct ieee80211_supported_band b43_band_2GHz = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100334 .band = IEEE80211_BAND_2GHZ,
335 .channels = b43_2ghz_chantable,
336 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
337 .bitrates = b43_g_ratetable,
338 .n_bitrates = b43_g_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100339};
340
Rafał Miłecki3695b932014-07-08 15:11:10 +0200341static struct ieee80211_supported_band b43_band_2ghz_limited = {
342 .band = IEEE80211_BAND_2GHZ,
343 .channels = b43_2ghz_chantable,
344 .n_channels = b43_2ghz_chantable_limited_size,
345 .bitrates = b43_g_ratetable,
346 .n_bitrates = b43_g_ratetable_size,
347};
348
Michael Buesche4d6b792007-09-18 15:39:42 -0400349static void b43_wireless_core_exit(struct b43_wldev *dev);
350static int b43_wireless_core_init(struct b43_wldev *dev);
Michael Buesch36dbd952009-09-04 22:51:29 +0200351static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400352static int b43_wireless_core_start(struct b43_wldev *dev);
Felix Fietkau2a190322011-08-10 13:50:30 -0600353static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
354 struct ieee80211_vif *vif,
355 struct ieee80211_bss_conf *conf,
356 u32 changed);
Michael Buesche4d6b792007-09-18 15:39:42 -0400357
358static int b43_ratelimit(struct b43_wl *wl)
359{
360 if (!wl || !wl->current_dev)
361 return 1;
362 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
363 return 1;
364 /* We are up and running.
365 * Ratelimit the messages to avoid DoS over the net. */
366 return net_ratelimit();
367}
368
369void b43info(struct b43_wl *wl, const char *fmt, ...)
370{
Joe Perches5b736d42010-11-09 16:35:18 -0800371 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400372 va_list args;
373
Michael Buesch060210f2009-01-25 15:49:59 +0100374 if (b43_modparam_verbose < B43_VERBOSITY_INFO)
375 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400376 if (!b43_ratelimit(wl))
377 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800378
Michael Buesche4d6b792007-09-18 15:39:42 -0400379 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800380
381 vaf.fmt = fmt;
382 vaf.va = &args;
383
384 printk(KERN_INFO "b43-%s: %pV",
385 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
386
Michael Buesche4d6b792007-09-18 15:39:42 -0400387 va_end(args);
388}
389
390void b43err(struct b43_wl *wl, const char *fmt, ...)
391{
Joe Perches5b736d42010-11-09 16:35:18 -0800392 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400393 va_list args;
394
Michael Buesch060210f2009-01-25 15:49:59 +0100395 if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
396 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400397 if (!b43_ratelimit(wl))
398 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800399
Michael Buesche4d6b792007-09-18 15:39:42 -0400400 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800401
402 vaf.fmt = fmt;
403 vaf.va = &args;
404
405 printk(KERN_ERR "b43-%s ERROR: %pV",
406 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
407
Michael Buesche4d6b792007-09-18 15:39:42 -0400408 va_end(args);
409}
410
411void b43warn(struct b43_wl *wl, const char *fmt, ...)
412{
Joe Perches5b736d42010-11-09 16:35:18 -0800413 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400414 va_list args;
415
Michael Buesch060210f2009-01-25 15:49:59 +0100416 if (b43_modparam_verbose < B43_VERBOSITY_WARN)
417 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400418 if (!b43_ratelimit(wl))
419 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800420
Michael Buesche4d6b792007-09-18 15:39:42 -0400421 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800422
423 vaf.fmt = fmt;
424 vaf.va = &args;
425
426 printk(KERN_WARNING "b43-%s warning: %pV",
427 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
428
Michael Buesche4d6b792007-09-18 15:39:42 -0400429 va_end(args);
430}
431
Michael Buesche4d6b792007-09-18 15:39:42 -0400432void b43dbg(struct b43_wl *wl, const char *fmt, ...)
433{
Joe Perches5b736d42010-11-09 16:35:18 -0800434 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400435 va_list args;
436
Michael Buesch060210f2009-01-25 15:49:59 +0100437 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
438 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800439
Michael Buesche4d6b792007-09-18 15:39:42 -0400440 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800441
442 vaf.fmt = fmt;
443 vaf.va = &args;
444
445 printk(KERN_DEBUG "b43-%s debug: %pV",
446 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
447
Michael Buesche4d6b792007-09-18 15:39:42 -0400448 va_end(args);
449}
Michael Buesche4d6b792007-09-18 15:39:42 -0400450
451static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
452{
453 u32 macctl;
454
455 B43_WARN_ON(offset % 4 != 0);
456
457 macctl = b43_read32(dev, B43_MMIO_MACCTL);
458 if (macctl & B43_MACCTL_BE)
459 val = swab32(val);
460
461 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
462 mmiowb();
463 b43_write32(dev, B43_MMIO_RAM_DATA, val);
464}
465
Michael Buesch280d0e12007-12-26 18:26:17 +0100466static inline void b43_shm_control_word(struct b43_wldev *dev,
467 u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400468{
469 u32 control;
470
471 /* "offset" is the WORD offset. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400472 control = routing;
473 control <<= 16;
474 control |= offset;
475 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
476}
477
Michael Buesch69eddc82009-09-04 22:57:26 +0200478u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400479{
480 u32 ret;
481
482 if (routing == B43_SHM_SHARED) {
483 B43_WARN_ON(offset & 0x0001);
484 if (offset & 0x0003) {
485 /* Unaligned access */
486 b43_shm_control_word(dev, routing, offset >> 2);
487 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
Michael Buesche4d6b792007-09-18 15:39:42 -0400488 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200489 ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400490
Michael Buesch280d0e12007-12-26 18:26:17 +0100491 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400492 }
493 offset >>= 2;
494 }
495 b43_shm_control_word(dev, routing, offset);
496 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100497out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200498 return ret;
499}
500
Michael Buesch69eddc82009-09-04 22:57:26 +0200501u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400502{
503 u16 ret;
504
505 if (routing == B43_SHM_SHARED) {
506 B43_WARN_ON(offset & 0x0001);
507 if (offset & 0x0003) {
508 /* Unaligned access */
509 b43_shm_control_word(dev, routing, offset >> 2);
510 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
511
Michael Buesch280d0e12007-12-26 18:26:17 +0100512 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400513 }
514 offset >>= 2;
515 }
516 b43_shm_control_word(dev, routing, offset);
517 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100518out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200519 return ret;
520}
521
Michael Buesch69eddc82009-09-04 22:57:26 +0200522void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400523{
524 if (routing == B43_SHM_SHARED) {
525 B43_WARN_ON(offset & 0x0001);
526 if (offset & 0x0003) {
527 /* Unaligned access */
528 b43_shm_control_word(dev, routing, offset >> 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400529 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200530 value & 0xFFFF);
Michael Buesche4d6b792007-09-18 15:39:42 -0400531 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200532 b43_write16(dev, B43_MMIO_SHM_DATA,
533 (value >> 16) & 0xFFFF);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200534 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400535 }
536 offset >>= 2;
537 }
538 b43_shm_control_word(dev, routing, offset);
Michael Buesche4d6b792007-09-18 15:39:42 -0400539 b43_write32(dev, B43_MMIO_SHM_DATA, value);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200540}
541
Michael Buesch69eddc82009-09-04 22:57:26 +0200542void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
Michael Buesch6bbc3212008-06-19 19:33:51 +0200543{
544 if (routing == B43_SHM_SHARED) {
545 B43_WARN_ON(offset & 0x0001);
546 if (offset & 0x0003) {
547 /* Unaligned access */
548 b43_shm_control_word(dev, routing, offset >> 2);
549 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
550 return;
551 }
552 offset >>= 2;
553 }
554 b43_shm_control_word(dev, routing, offset);
555 b43_write16(dev, B43_MMIO_SHM_DATA, value);
556}
557
Michael Buesche4d6b792007-09-18 15:39:42 -0400558/* Read HostFlags */
John Daiker99da1852009-02-24 02:16:42 -0800559u64 b43_hf_read(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400560{
Michael Buesch35f0d352008-02-13 14:31:08 +0100561 u64 ret;
Michael Buesche4d6b792007-09-18 15:39:42 -0400562
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200563 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400564 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200565 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2);
Michael Buesch35f0d352008-02-13 14:31:08 +0100566 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200567 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400568
569 return ret;
570}
571
572/* Write HostFlags */
Michael Buesch35f0d352008-02-13 14:31:08 +0100573void b43_hf_write(struct b43_wldev *dev, u64 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400574{
Michael Buesch35f0d352008-02-13 14:31:08 +0100575 u16 lo, mi, hi;
576
577 lo = (value & 0x00000000FFFFULL);
578 mi = (value & 0x0000FFFF0000ULL) >> 16;
579 hi = (value & 0xFFFF00000000ULL) >> 32;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200580 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1, lo);
581 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2, mi);
582 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3, hi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400583}
584
Michael Buesch403a3a12009-06-08 21:04:57 +0200585/* Read the firmware capabilities bitmask (Opensource firmware only) */
586static u16 b43_fwcapa_read(struct b43_wldev *dev)
587{
588 B43_WARN_ON(!dev->fw.opensource);
589 return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
590}
591
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100592void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
Michael Buesche4d6b792007-09-18 15:39:42 -0400593{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100594 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400595
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200596 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400597
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100598 /* The hardware guarantees us an atomic read, if we
599 * read the low register first. */
600 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
601 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400602
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100603 *tsf = high;
604 *tsf <<= 32;
605 *tsf |= low;
Michael Buesche4d6b792007-09-18 15:39:42 -0400606}
607
608static void b43_time_lock(struct b43_wldev *dev)
609{
Rafał Miłecki50566352012-01-02 19:31:21 +0100610 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_TBTTHOLD);
Michael Buesche4d6b792007-09-18 15:39:42 -0400611 /* Commit the write */
612 b43_read32(dev, B43_MMIO_MACCTL);
613}
614
615static void b43_time_unlock(struct b43_wldev *dev)
616{
Rafał Miłecki50566352012-01-02 19:31:21 +0100617 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_TBTTHOLD, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400618 /* Commit the write */
619 b43_read32(dev, B43_MMIO_MACCTL);
620}
621
622static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
623{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100624 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400625
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200626 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400627
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100628 low = tsf;
629 high = (tsf >> 32);
630 /* The hardware guarantees us an atomic write, if we
631 * write the low register first. */
632 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
633 mmiowb();
634 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
635 mmiowb();
Michael Buesche4d6b792007-09-18 15:39:42 -0400636}
637
638void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
639{
640 b43_time_lock(dev);
641 b43_tsf_write_locked(dev, tsf);
642 b43_time_unlock(dev);
643}
644
645static
John Daiker99da1852009-02-24 02:16:42 -0800646void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
Michael Buesche4d6b792007-09-18 15:39:42 -0400647{
648 static const u8 zero_addr[ETH_ALEN] = { 0 };
649 u16 data;
650
651 if (!mac)
652 mac = zero_addr;
653
654 offset |= 0x0020;
655 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
656
657 data = mac[0];
658 data |= mac[1] << 8;
659 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
660 data = mac[2];
661 data |= mac[3] << 8;
662 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
663 data = mac[4];
664 data |= mac[5] << 8;
665 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
666}
667
668static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
669{
670 const u8 *mac;
671 const u8 *bssid;
672 u8 mac_bssid[ETH_ALEN * 2];
673 int i;
674 u32 tmp;
675
676 bssid = dev->wl->bssid;
677 mac = dev->wl->mac_addr;
678
679 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
680
681 memcpy(mac_bssid, mac, ETH_ALEN);
682 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
683
684 /* Write our MAC address and BSSID to template ram */
685 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
686 tmp = (u32) (mac_bssid[i + 0]);
687 tmp |= (u32) (mac_bssid[i + 1]) << 8;
688 tmp |= (u32) (mac_bssid[i + 2]) << 16;
689 tmp |= (u32) (mac_bssid[i + 3]) << 24;
690 b43_ram_write(dev, 0x20 + i, tmp);
691 }
692}
693
Johannes Berg4150c572007-09-17 01:29:23 -0400694static void b43_upload_card_macaddress(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400695{
Michael Buesche4d6b792007-09-18 15:39:42 -0400696 b43_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400697 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400698}
699
700static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
701{
702 /* slot_time is in usec. */
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600703 /* This test used to exit for all but a G PHY. */
704 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Michael Buesche4d6b792007-09-18 15:39:42 -0400705 return;
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600706 b43_write16(dev, B43_MMIO_IFSSLOT, 510 + slot_time);
707 /* Shared memory location 0x0010 is the slot time and should be
708 * set to slot_time; however, this register is initially 0 and changing
709 * the value adversely affects the transmit rate for BCM4311
710 * devices. Until this behavior is unterstood, delete this step
711 *
712 * b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
713 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400714}
715
716static void b43_short_slot_timing_enable(struct b43_wldev *dev)
717{
718 b43_set_slot_time(dev, 9);
Michael Buesche4d6b792007-09-18 15:39:42 -0400719}
720
721static void b43_short_slot_timing_disable(struct b43_wldev *dev)
722{
723 b43_set_slot_time(dev, 20);
Michael Buesche4d6b792007-09-18 15:39:42 -0400724}
725
Michael Buesche4d6b792007-09-18 15:39:42 -0400726/* DummyTransmission function, as documented on
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200727 * http://bcm-v4.sipsolutions.net/802.11/DummyTransmission
Michael Buesche4d6b792007-09-18 15:39:42 -0400728 */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200729void b43_dummy_transmission(struct b43_wldev *dev, bool ofdm, bool pa_on)
Michael Buesche4d6b792007-09-18 15:39:42 -0400730{
731 struct b43_phy *phy = &dev->phy;
732 unsigned int i, max_loop;
733 u16 value;
734 u32 buffer[5] = {
735 0x00000000,
736 0x00D40000,
737 0x00000000,
738 0x01000000,
739 0x00000000,
740 };
741
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200742 if (ofdm) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400743 max_loop = 0x1E;
744 buffer[0] = 0x000201CC;
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200745 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -0400746 max_loop = 0xFA;
747 buffer[0] = 0x000B846E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400748 }
749
750 for (i = 0; i < 5; i++)
751 b43_ram_write(dev, i * 4, buffer[i]);
752
Rafał Miłecki7955d872011-09-21 21:44:13 +0200753 b43_write16(dev, B43_MMIO_XMTSEL, 0x0000);
754
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200755 if (dev->dev->core_rev < 11)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200756 b43_write16(dev, B43_MMIO_WEPCTL, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200757 else
Rafał Miłecki7955d872011-09-21 21:44:13 +0200758 b43_write16(dev, B43_MMIO_WEPCTL, 0x0100);
759
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200760 value = (ofdm ? 0x41 : 0x40);
Rafał Miłecki7955d872011-09-21 21:44:13 +0200761 b43_write16(dev, B43_MMIO_TXE0_PHYCTL, value);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200762 if (phy->type == B43_PHYTYPE_N || phy->type == B43_PHYTYPE_LP ||
763 phy->type == B43_PHYTYPE_LCN)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200764 b43_write16(dev, B43_MMIO_TXE0_PHYCTL1, 0x1A02);
765
766 b43_write16(dev, B43_MMIO_TXE0_WM_0, 0x0000);
767 b43_write16(dev, B43_MMIO_TXE0_WM_1, 0x0000);
768
769 b43_write16(dev, B43_MMIO_XMTTPLATETXPTR, 0x0000);
770 b43_write16(dev, B43_MMIO_XMTTXCNT, 0x0014);
771 b43_write16(dev, B43_MMIO_XMTSEL, 0x0826);
772 b43_write16(dev, B43_MMIO_TXE0_CTL, 0x0000);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200773
774 if (!pa_on && phy->type == B43_PHYTYPE_N)
775 ; /*b43_nphy_pa_override(dev, false) */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200776
777 switch (phy->type) {
778 case B43_PHYTYPE_N:
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200779 case B43_PHYTYPE_LCN:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200780 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x00D0);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200781 break;
782 case B43_PHYTYPE_LP:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200783 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0050);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200784 break;
785 default:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200786 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0030);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200787 }
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200788 b43_read16(dev, B43_MMIO_TXE0_AUX);
Michael Buesche4d6b792007-09-18 15:39:42 -0400789
790 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
791 b43_radio_write16(dev, 0x0051, 0x0017);
792 for (i = 0x00; i < max_loop; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200793 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400794 if (value & 0x0080)
795 break;
796 udelay(10);
797 }
798 for (i = 0x00; i < 0x0A; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200799 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400800 if (value & 0x0400)
801 break;
802 udelay(10);
803 }
Larry Finger1d280dd2008-09-29 14:19:29 -0500804 for (i = 0x00; i < 0x19; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200805 value = b43_read16(dev, B43_MMIO_IFSSTAT);
Michael Buesche4d6b792007-09-18 15:39:42 -0400806 if (!(value & 0x0100))
807 break;
808 udelay(10);
809 }
810 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
811 b43_radio_write16(dev, 0x0051, 0x0037);
812}
813
814static void key_write(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800815 u8 index, u8 algorithm, const u8 *key)
Michael Buesche4d6b792007-09-18 15:39:42 -0400816{
817 unsigned int i;
818 u32 offset;
819 u16 value;
820 u16 kidx;
821
822 /* Key index/algo block */
823 kidx = b43_kidx_to_fw(dev, index);
824 value = ((kidx << 4) | algorithm);
825 b43_shm_write16(dev, B43_SHM_SHARED,
826 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
827
828 /* Write the key to the Key Table Pointer offset */
829 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
830 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
831 value = key[i];
832 value |= (u16) (key[i + 1]) << 8;
833 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
834 }
835}
836
John Daiker99da1852009-02-24 02:16:42 -0800837static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400838{
839 u32 addrtmp[2] = { 0, 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200840 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400841
842 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200843 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400844
Michael Buesch66d2d082009-08-06 10:36:50 +0200845 B43_WARN_ON(index < pairwise_keys_start);
846 /* We have four default TX keys and possibly four default RX keys.
Michael Buesche4d6b792007-09-18 15:39:42 -0400847 * Physical mac 0 is mapped to physical key 4 or 8, depending
848 * on the firmware version.
849 * So we must adjust the index here.
850 */
Michael Buesch66d2d082009-08-06 10:36:50 +0200851 index -= pairwise_keys_start;
852 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400853
854 if (addr) {
855 addrtmp[0] = addr[0];
856 addrtmp[0] |= ((u32) (addr[1]) << 8);
857 addrtmp[0] |= ((u32) (addr[2]) << 16);
858 addrtmp[0] |= ((u32) (addr[3]) << 24);
859 addrtmp[1] = addr[4];
860 addrtmp[1] |= ((u32) (addr[5]) << 8);
861 }
862
Michael Buesch66d2d082009-08-06 10:36:50 +0200863 /* Receive match transmitter address (RCMTA) mechanism */
864 b43_shm_write32(dev, B43_SHM_RCMTA,
865 (index * 2) + 0, addrtmp[0]);
866 b43_shm_write16(dev, B43_SHM_RCMTA,
867 (index * 2) + 1, addrtmp[1]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400868}
869
gregor kowski035d0242009-08-19 22:35:45 +0200870/* The ucode will use phase1 key with TEK key to decrypt rx packets.
871 * When a packet is received, the iv32 is checked.
872 * - if it doesn't the packet is returned without modification (and software
873 * decryption can be done). That's what happen when iv16 wrap.
874 * - if it does, the rc4 key is computed, and decryption is tried.
875 * Either it will success and B43_RX_MAC_DEC is returned,
876 * either it fails and B43_RX_MAC_DEC|B43_RX_MAC_DECERR is returned
877 * and the packet is not usable (it got modified by the ucode).
878 * So in order to never have B43_RX_MAC_DECERR, we should provide
879 * a iv32 and phase1key that match. Because we drop packets in case of
880 * B43_RX_MAC_DECERR, if we have a correct iv32 but a wrong phase1key, all
881 * packets will be lost without higher layer knowing (ie no resync possible
882 * until next wrap).
883 *
884 * NOTE : this should support 50 key like RCMTA because
885 * (B43_SHM_SH_KEYIDXBLOCK - B43_SHM_SH_TKIPTSCTTAK)/14 = 50
886 */
887static void rx_tkip_phase1_write(struct b43_wldev *dev, u8 index, u32 iv32,
888 u16 *phase1key)
889{
890 unsigned int i;
891 u32 offset;
892 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
893
894 if (!modparam_hwtkip)
895 return;
896
897 if (b43_new_kidx_api(dev))
898 pairwise_keys_start = B43_NR_GROUP_KEYS;
899
900 B43_WARN_ON(index < pairwise_keys_start);
901 /* We have four default TX keys and possibly four default RX keys.
902 * Physical mac 0 is mapped to physical key 4 or 8, depending
903 * on the firmware version.
904 * So we must adjust the index here.
905 */
906 index -= pairwise_keys_start;
907 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
908
909 if (b43_debug(dev, B43_DBG_KEYS)) {
910 b43dbg(dev->wl, "rx_tkip_phase1_write : idx 0x%x, iv32 0x%x\n",
911 index, iv32);
912 }
913 /* Write the key to the RX tkip shared mem */
914 offset = B43_SHM_SH_TKIPTSCTTAK + index * (10 + 4);
915 for (i = 0; i < 10; i += 2) {
916 b43_shm_write16(dev, B43_SHM_SHARED, offset + i,
917 phase1key ? phase1key[i / 2] : 0);
918 }
919 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, iv32);
920 b43_shm_write16(dev, B43_SHM_SHARED, offset + i + 2, iv32 >> 16);
921}
922
923static void b43_op_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100924 struct ieee80211_vif *vif,
925 struct ieee80211_key_conf *keyconf,
926 struct ieee80211_sta *sta,
927 u32 iv32, u16 *phase1key)
gregor kowski035d0242009-08-19 22:35:45 +0200928{
929 struct b43_wl *wl = hw_to_b43_wl(hw);
930 struct b43_wldev *dev;
931 int index = keyconf->hw_key_idx;
932
933 if (B43_WARN_ON(!modparam_hwtkip))
934 return;
935
Michael Buesch96869a32010-01-24 13:13:32 +0100936 /* This is only called from the RX path through mac80211, where
937 * our mutex is already locked. */
938 B43_WARN_ON(!mutex_is_locked(&wl->mutex));
gregor kowski035d0242009-08-19 22:35:45 +0200939 dev = wl->current_dev;
Michael Buesch96869a32010-01-24 13:13:32 +0100940 B43_WARN_ON(!dev || b43_status(dev) < B43_STAT_INITIALIZED);
gregor kowski035d0242009-08-19 22:35:45 +0200941
942 keymac_write(dev, index, NULL); /* First zero out mac to avoid race */
943
944 rx_tkip_phase1_write(dev, index, iv32, phase1key);
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100945 /* only pairwise TKIP keys are supported right now */
946 if (WARN_ON(!sta))
Michael Buesch96869a32010-01-24 13:13:32 +0100947 return;
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100948 keymac_write(dev, index, sta->addr);
gregor kowski035d0242009-08-19 22:35:45 +0200949}
950
Michael Buesche4d6b792007-09-18 15:39:42 -0400951static void do_key_write(struct b43_wldev *dev,
952 u8 index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800953 const u8 *key, size_t key_len, const u8 *mac_addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400954{
955 u8 buf[B43_SEC_KEYSIZE] = { 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200956 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400957
958 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200959 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400960
Michael Buesch66d2d082009-08-06 10:36:50 +0200961 B43_WARN_ON(index >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400962 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
963
Michael Buesch66d2d082009-08-06 10:36:50 +0200964 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400965 keymac_write(dev, index, NULL); /* First zero out mac. */
gregor kowski035d0242009-08-19 22:35:45 +0200966 if (algorithm == B43_SEC_ALGO_TKIP) {
967 /*
968 * We should provide an initial iv32, phase1key pair.
969 * We could start with iv32=0 and compute the corresponding
970 * phase1key, but this means calling ieee80211_get_tkip_key
971 * with a fake skb (or export other tkip function).
972 * Because we are lazy we hope iv32 won't start with
973 * 0xffffffff and let's b43_op_update_tkip_key provide a
974 * correct pair.
975 */
976 rx_tkip_phase1_write(dev, index, 0xffffffff, (u16*)buf);
977 } else if (index >= pairwise_keys_start) /* clear it */
978 rx_tkip_phase1_write(dev, index, 0, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -0400979 if (key)
980 memcpy(buf, key, key_len);
981 key_write(dev, index, algorithm, buf);
Michael Buesch66d2d082009-08-06 10:36:50 +0200982 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400983 keymac_write(dev, index, mac_addr);
984
985 dev->key[index].algorithm = algorithm;
986}
987
988static int b43_key_write(struct b43_wldev *dev,
989 int index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800990 const u8 *key, size_t key_len,
991 const u8 *mac_addr,
Michael Buesche4d6b792007-09-18 15:39:42 -0400992 struct ieee80211_key_conf *keyconf)
993{
994 int i;
Michael Buesch66d2d082009-08-06 10:36:50 +0200995 int pairwise_keys_start;
Michael Buesche4d6b792007-09-18 15:39:42 -0400996
gregor kowski035d0242009-08-19 22:35:45 +0200997 /* For ALG_TKIP the key is encoded as a 256-bit (32 byte) data block:
998 * - Temporal Encryption Key (128 bits)
999 * - Temporal Authenticator Tx MIC Key (64 bits)
1000 * - Temporal Authenticator Rx MIC Key (64 bits)
1001 *
1002 * Hardware only store TEK
1003 */
1004 if (algorithm == B43_SEC_ALGO_TKIP && key_len == 32)
1005 key_len = 16;
Michael Buesche4d6b792007-09-18 15:39:42 -04001006 if (key_len > B43_SEC_KEYSIZE)
1007 return -EINVAL;
Michael Buesch66d2d082009-08-06 10:36:50 +02001008 for (i = 0; i < ARRAY_SIZE(dev->key); i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001009 /* Check that we don't already have this key. */
1010 B43_WARN_ON(dev->key[i].keyconf == keyconf);
1011 }
1012 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001013 /* Pairwise key. Get an empty slot for the key. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001014 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +02001015 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -04001016 else
Michael Buesch66d2d082009-08-06 10:36:50 +02001017 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1018 for (i = pairwise_keys_start;
1019 i < pairwise_keys_start + B43_NR_PAIRWISE_KEYS;
1020 i++) {
1021 B43_WARN_ON(i >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -04001022 if (!dev->key[i].keyconf) {
1023 /* found empty */
1024 index = i;
1025 break;
1026 }
1027 }
1028 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001029 b43warn(dev->wl, "Out of hardware key memory\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04001030 return -ENOSPC;
1031 }
1032 } else
1033 B43_WARN_ON(index > 3);
1034
1035 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
1036 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1037 /* Default RX key */
1038 B43_WARN_ON(mac_addr);
1039 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
1040 }
1041 keyconf->hw_key_idx = index;
1042 dev->key[index].keyconf = keyconf;
1043
1044 return 0;
1045}
1046
1047static int b43_key_clear(struct b43_wldev *dev, int index)
1048{
Michael Buesch66d2d082009-08-06 10:36:50 +02001049 if (B43_WARN_ON((index < 0) || (index >= ARRAY_SIZE(dev->key))))
Michael Buesche4d6b792007-09-18 15:39:42 -04001050 return -EINVAL;
1051 do_key_write(dev, index, B43_SEC_ALGO_NONE,
1052 NULL, B43_SEC_KEYSIZE, NULL);
1053 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1054 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
1055 NULL, B43_SEC_KEYSIZE, NULL);
1056 }
1057 dev->key[index].keyconf = NULL;
1058
1059 return 0;
1060}
1061
1062static void b43_clear_keys(struct b43_wldev *dev)
1063{
Michael Buesch66d2d082009-08-06 10:36:50 +02001064 int i, count;
Michael Buesche4d6b792007-09-18 15:39:42 -04001065
Michael Buesch66d2d082009-08-06 10:36:50 +02001066 if (b43_new_kidx_api(dev))
1067 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1068 else
1069 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1070 for (i = 0; i < count; i++)
Michael Buesche4d6b792007-09-18 15:39:42 -04001071 b43_key_clear(dev, i);
1072}
1073
Michael Buesch9cf7f242008-12-19 20:24:30 +01001074static void b43_dump_keymemory(struct b43_wldev *dev)
1075{
Michael Buesch66d2d082009-08-06 10:36:50 +02001076 unsigned int i, index, count, offset, pairwise_keys_start;
Michael Buesch9cf7f242008-12-19 20:24:30 +01001077 u8 mac[ETH_ALEN];
1078 u16 algo;
1079 u32 rcmta0;
1080 u16 rcmta1;
1081 u64 hf;
1082 struct b43_key *key;
1083
1084 if (!b43_debug(dev, B43_DBG_KEYS))
1085 return;
1086
1087 hf = b43_hf_read(dev);
1088 b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
1089 !!(hf & B43_HF_USEDEFKEYS));
Michael Buesch66d2d082009-08-06 10:36:50 +02001090 if (b43_new_kidx_api(dev)) {
1091 pairwise_keys_start = B43_NR_GROUP_KEYS;
1092 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1093 } else {
1094 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1095 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1096 }
1097 for (index = 0; index < count; index++) {
Michael Buesch9cf7f242008-12-19 20:24:30 +01001098 key = &(dev->key[index]);
1099 printk(KERN_DEBUG "Key slot %02u: %s",
1100 index, (key->keyconf == NULL) ? " " : "*");
1101 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
1102 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
1103 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1104 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1105 }
1106
1107 algo = b43_shm_read16(dev, B43_SHM_SHARED,
1108 B43_SHM_SH_KEYIDXBLOCK + (index * 2));
1109 printk(" Algo: %04X/%02X", algo, key->algorithm);
1110
Michael Buesch66d2d082009-08-06 10:36:50 +02001111 if (index >= pairwise_keys_start) {
gregor kowski035d0242009-08-19 22:35:45 +02001112 if (key->algorithm == B43_SEC_ALGO_TKIP) {
1113 printk(" TKIP: ");
1114 offset = B43_SHM_SH_TKIPTSCTTAK + (index - 4) * (10 + 4);
1115 for (i = 0; i < 14; i += 2) {
1116 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1117 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1118 }
1119 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01001120 rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001121 ((index - pairwise_keys_start) * 2) + 0);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001122 rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001123 ((index - pairwise_keys_start) * 2) + 1);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001124 *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
1125 *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
Johannes Berge91d8332009-07-15 17:21:41 +02001126 printk(" MAC: %pM", mac);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001127 } else
1128 printk(" DEFAULT KEY");
1129 printk("\n");
1130 }
1131}
1132
Michael Buesche4d6b792007-09-18 15:39:42 -04001133void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1134{
1135 u32 macctl;
1136 u16 ucstat;
1137 bool hwps;
1138 bool awake;
1139 int i;
1140
1141 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1142 (ps_flags & B43_PS_DISABLED));
1143 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1144
1145 if (ps_flags & B43_PS_ENABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001146 hwps = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001147 } else if (ps_flags & B43_PS_DISABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001148 hwps = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001149 } else {
1150 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1151 // and thus is not an AP and we are associated, set bit 25
1152 }
1153 if (ps_flags & B43_PS_AWAKE) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001154 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001155 } else if (ps_flags & B43_PS_ASLEEP) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001156 awake = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001157 } else {
1158 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1159 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1160 // successful, set bit26
1161 }
1162
1163/* FIXME: For now we force awake-on and hwps-off */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001164 hwps = false;
1165 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001166
1167 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1168 if (hwps)
1169 macctl |= B43_MACCTL_HWPS;
1170 else
1171 macctl &= ~B43_MACCTL_HWPS;
1172 if (awake)
1173 macctl |= B43_MACCTL_AWAKE;
1174 else
1175 macctl &= ~B43_MACCTL_AWAKE;
1176 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1177 /* Commit write */
1178 b43_read32(dev, B43_MMIO_MACCTL);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001179 if (awake && dev->dev->core_rev >= 5) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001180 /* Wait for the microcode to wake up. */
1181 for (i = 0; i < 100; i++) {
1182 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1183 B43_SHM_SH_UCODESTAT);
1184 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1185 break;
1186 udelay(10);
1187 }
1188 }
1189}
1190
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001191#ifdef CONFIG_B43_BCMA
Rafał Miłecki49173592011-07-17 01:06:06 +02001192static void b43_bcma_phy_reset(struct b43_wldev *dev)
1193{
1194 u32 flags;
1195
1196 /* Put PHY into reset */
1197 flags = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1198 flags |= B43_BCMA_IOCTL_PHY_RESET;
1199 flags |= B43_BCMA_IOCTL_PHY_BW_20MHZ; /* Make 20 MHz def */
1200 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, flags);
1201 udelay(2);
1202
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001203 b43_phy_take_out_of_reset(dev);
Rafał Miłecki49173592011-07-17 01:06:06 +02001204}
1205
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001206static void b43_bcma_wireless_core_reset(struct b43_wldev *dev, bool gmode)
1207{
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001208 u32 req = B43_BCMA_CLKCTLST_80211_PLL_REQ |
1209 B43_BCMA_CLKCTLST_PHY_PLL_REQ;
1210 u32 status = B43_BCMA_CLKCTLST_80211_PLL_ST |
1211 B43_BCMA_CLKCTLST_PHY_PLL_ST;
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001212 u32 flags;
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001213
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001214 flags = B43_BCMA_IOCTL_PHY_CLKEN;
1215 if (gmode)
1216 flags |= B43_BCMA_IOCTL_GMODE;
1217 b43_device_enable(dev, flags);
1218
Rafał Miłecki49173592011-07-17 01:06:06 +02001219 bcma_core_set_clockmode(dev->dev->bdev, BCMA_CLKMODE_FAST);
1220 b43_bcma_phy_reset(dev);
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001221 bcma_core_pll_ctl(dev->dev->bdev, req, status, true);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001222}
1223#endif
1224
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001225#ifdef CONFIG_B43_SSB
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001226static void b43_ssb_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001227{
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001228 u32 flags = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04001229
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001230 if (gmode)
1231 flags |= B43_TMSLOW_GMODE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001232 flags |= B43_TMSLOW_PHYCLKEN;
1233 flags |= B43_TMSLOW_PHYRESET;
Rafał Miłecki42ab1352010-12-09 20:56:01 +01001234 if (dev->phy.type == B43_PHYTYPE_N)
1235 flags |= B43_TMSLOW_PHY_BANDWIDTH_20MHZ; /* Make 20 MHz def */
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02001236 b43_device_enable(dev, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -04001237 msleep(2); /* Wait for the PLL to turn on. */
1238
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001239 b43_phy_take_out_of_reset(dev);
Rafał Miłecki14952982011-05-17 18:57:28 +02001240}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001241#endif
Rafał Miłecki14952982011-05-17 18:57:28 +02001242
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001243void b43_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Rafał Miłecki14952982011-05-17 18:57:28 +02001244{
1245 u32 macctl;
1246
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001247 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001248#ifdef CONFIG_B43_BCMA
1249 case B43_BUS_BCMA:
1250 b43_bcma_wireless_core_reset(dev, gmode);
1251 break;
1252#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001253#ifdef CONFIG_B43_SSB
1254 case B43_BUS_SSB:
1255 b43_ssb_wireless_core_reset(dev, gmode);
1256 break;
1257#endif
1258 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001259
Michael Bueschfb111372008-09-02 13:00:34 +02001260 /* Turn Analog ON, but only if we already know the PHY-type.
1261 * This protects against very early setup where we don't know the
1262 * PHY-type, yet. wireless_core_reset will be called once again later,
1263 * when we know the PHY-type. */
1264 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001265 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001266
1267 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1268 macctl &= ~B43_MACCTL_GMODE;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001269 if (gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001270 macctl |= B43_MACCTL_GMODE;
1271 macctl |= B43_MACCTL_IHR_ENABLED;
1272 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1273}
1274
1275static void handle_irq_transmit_status(struct b43_wldev *dev)
1276{
1277 u32 v0, v1;
1278 u16 tmp;
1279 struct b43_txstatus stat;
1280
1281 while (1) {
1282 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1283 if (!(v0 & 0x00000001))
1284 break;
1285 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1286
1287 stat.cookie = (v0 >> 16);
1288 stat.seq = (v1 & 0x0000FFFF);
1289 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1290 tmp = (v0 & 0x0000FFFF);
1291 stat.frame_count = ((tmp & 0xF000) >> 12);
1292 stat.rts_count = ((tmp & 0x0F00) >> 8);
1293 stat.supp_reason = ((tmp & 0x001C) >> 2);
1294 stat.pm_indicated = !!(tmp & 0x0080);
1295 stat.intermediate = !!(tmp & 0x0040);
1296 stat.for_ampdu = !!(tmp & 0x0020);
1297 stat.acked = !!(tmp & 0x0002);
1298
1299 b43_handle_txstatus(dev, &stat);
1300 }
1301}
1302
1303static void drain_txstatus_queue(struct b43_wldev *dev)
1304{
1305 u32 dummy;
1306
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001307 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04001308 return;
1309 /* Read all entries from the microcode TXstatus FIFO
1310 * and throw them away.
1311 */
1312 while (1) {
1313 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1314 if (!(dummy & 0x00000001))
1315 break;
1316 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1317 }
1318}
1319
1320static u32 b43_jssi_read(struct b43_wldev *dev)
1321{
1322 u32 val = 0;
1323
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001324 val = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001325 val <<= 16;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001326 val |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001327
1328 return val;
1329}
1330
1331static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1332{
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001333 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0,
1334 (jssi & 0x0000FFFF));
1335 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1,
1336 (jssi & 0xFFFF0000) >> 16);
Michael Buesche4d6b792007-09-18 15:39:42 -04001337}
1338
1339static void b43_generate_noise_sample(struct b43_wldev *dev)
1340{
1341 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001342 b43_write32(dev, B43_MMIO_MACCMD,
1343 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001344}
1345
1346static void b43_calculate_link_quality(struct b43_wldev *dev)
1347{
1348 /* Top half of Link Quality calculation. */
1349
Michael Bueschef1a6282008-08-27 18:53:02 +02001350 if (dev->phy.type != B43_PHYTYPE_G)
1351 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001352 if (dev->noisecalc.calculation_running)
1353 return;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001354 dev->noisecalc.calculation_running = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001355 dev->noisecalc.nr_samples = 0;
1356
1357 b43_generate_noise_sample(dev);
1358}
1359
1360static void handle_irq_noise(struct b43_wldev *dev)
1361{
Michael Bueschef1a6282008-08-27 18:53:02 +02001362 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001363 u16 tmp;
1364 u8 noise[4];
1365 u8 i, j;
1366 s32 average;
1367
1368 /* Bottom half of Link Quality calculation. */
1369
Michael Bueschef1a6282008-08-27 18:53:02 +02001370 if (dev->phy.type != B43_PHYTYPE_G)
1371 return;
1372
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001373 /* Possible race condition: It might be possible that the user
1374 * changed to a different channel in the meantime since we
1375 * started the calculation. We ignore that fact, since it's
1376 * not really that much of a problem. The background noise is
1377 * an estimation only anyway. Slightly wrong results will get damped
1378 * by the averaging of the 8 sample rounds. Additionally the
1379 * value is shortlived. So it will be replaced by the next noise
1380 * calculation round soon. */
1381
Michael Buesche4d6b792007-09-18 15:39:42 -04001382 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001383 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001384 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1385 noise[2] == 0x7F || noise[3] == 0x7F)
1386 goto generate_new;
1387
1388 /* Get the noise samples. */
1389 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1390 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001391 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1392 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1393 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1394 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001395 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1396 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1397 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1398 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1399 dev->noisecalc.nr_samples++;
1400 if (dev->noisecalc.nr_samples == 8) {
1401 /* Calculate the Link Quality by the noise samples. */
1402 average = 0;
1403 for (i = 0; i < 8; i++) {
1404 for (j = 0; j < 4; j++)
1405 average += dev->noisecalc.samples[i][j];
1406 }
1407 average /= (8 * 4);
1408 average *= 125;
1409 average += 64;
1410 average /= 128;
1411 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1412 tmp = (tmp / 128) & 0x1F;
1413 if (tmp >= 8)
1414 average += 2;
1415 else
1416 average -= 25;
1417 if (tmp == 8)
1418 average -= 72;
1419 else
1420 average -= 48;
1421
1422 dev->stats.link_noise = average;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001423 dev->noisecalc.calculation_running = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001424 return;
1425 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001426generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001427 b43_generate_noise_sample(dev);
1428}
1429
1430static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1431{
Johannes Berg05c914f2008-09-11 00:01:58 +02001432 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001433 ///TODO: PS TBTT
1434 } else {
1435 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1436 b43_power_saving_ctl_bits(dev, 0);
1437 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001438 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001439 dev->dfq_valid = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001440}
1441
1442static void handle_irq_atim_end(struct b43_wldev *dev)
1443{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001444 if (dev->dfq_valid) {
1445 b43_write32(dev, B43_MMIO_MACCMD,
1446 b43_read32(dev, B43_MMIO_MACCMD)
1447 | B43_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001448 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001449 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001450}
1451
1452static void handle_irq_pmq(struct b43_wldev *dev)
1453{
1454 u32 tmp;
1455
1456 //TODO: AP mode.
1457
1458 while (1) {
1459 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1460 if (!(tmp & 0x00000008))
1461 break;
1462 }
1463 /* 16bit write is odd, but correct. */
1464 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1465}
1466
1467static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001468 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001469 u16 ram_offset,
1470 u16 shm_size_offset, u8 rate)
1471{
1472 u32 i, tmp;
1473 struct b43_plcp_hdr4 plcp;
1474
1475 plcp.data = 0;
1476 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1477 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1478 ram_offset += sizeof(u32);
1479 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1480 * So leave the first two bytes of the next write blank.
1481 */
1482 tmp = (u32) (data[0]) << 16;
1483 tmp |= (u32) (data[1]) << 24;
1484 b43_ram_write(dev, ram_offset, tmp);
1485 ram_offset += sizeof(u32);
1486 for (i = 2; i < size; i += sizeof(u32)) {
1487 tmp = (u32) (data[i + 0]);
1488 if (i + 1 < size)
1489 tmp |= (u32) (data[i + 1]) << 8;
1490 if (i + 2 < size)
1491 tmp |= (u32) (data[i + 2]) << 16;
1492 if (i + 3 < size)
1493 tmp |= (u32) (data[i + 3]) << 24;
1494 b43_ram_write(dev, ram_offset + i - 2, tmp);
1495 }
1496 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1497 size + sizeof(struct b43_plcp_hdr6));
1498}
1499
Michael Buesch5042c502008-04-05 15:05:00 +02001500/* Check if the use of the antenna that ieee80211 told us to
1501 * use is possible. This will fall back to DEFAULT.
1502 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1503u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1504 u8 antenna_nr)
1505{
1506 u8 antenna_mask;
1507
1508 if (antenna_nr == 0) {
1509 /* Zero means "use default antenna". That's always OK. */
1510 return 0;
1511 }
1512
1513 /* Get the mask of available antennas. */
1514 if (dev->phy.gmode)
Rafał Miłecki05814832011-05-18 02:06:39 +02001515 antenna_mask = dev->dev->bus_sprom->ant_available_bg;
Michael Buesch5042c502008-04-05 15:05:00 +02001516 else
Rafał Miłecki05814832011-05-18 02:06:39 +02001517 antenna_mask = dev->dev->bus_sprom->ant_available_a;
Michael Buesch5042c502008-04-05 15:05:00 +02001518
1519 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1520 /* This antenna is not available. Fall back to default. */
1521 return 0;
1522 }
1523
1524 return antenna_nr;
1525}
1526
Michael Buesch5042c502008-04-05 15:05:00 +02001527/* Convert a b43 antenna number value to the PHY TX control value. */
1528static u16 b43_antenna_to_phyctl(int antenna)
1529{
1530 switch (antenna) {
1531 case B43_ANTENNA0:
1532 return B43_TXH_PHY_ANT0;
1533 case B43_ANTENNA1:
1534 return B43_TXH_PHY_ANT1;
1535 case B43_ANTENNA2:
1536 return B43_TXH_PHY_ANT2;
1537 case B43_ANTENNA3:
1538 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001539 case B43_ANTENNA_AUTO0:
1540 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001541 return B43_TXH_PHY_ANT01AUTO;
1542 }
1543 B43_WARN_ON(1);
1544 return 0;
1545}
1546
Michael Buesche4d6b792007-09-18 15:39:42 -04001547static void b43_write_beacon_template(struct b43_wldev *dev,
1548 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001549 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001550{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001551 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001552 const struct ieee80211_mgmt *bcn;
1553 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001554 bool tim_found = false;
Michael Buesch5042c502008-04-05 15:05:00 +02001555 unsigned int rate;
1556 u16 ctl;
1557 int antenna;
Johannes Berge039fa42008-05-15 12:55:29 +02001558 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001559
Michael Buesche66fee62007-12-26 17:47:10 +01001560 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
Silvan Jegenc8e49552014-02-25 18:12:52 +01001561 len = min_t(size_t, dev->wl->current_beacon->len,
Michael Buesche4d6b792007-09-18 15:39:42 -04001562 0x200 - sizeof(struct b43_plcp_hdr6));
Johannes Berge039fa42008-05-15 12:55:29 +02001563 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Buesche66fee62007-12-26 17:47:10 +01001564
1565 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001566 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001567
Michael Buesch5042c502008-04-05 15:05:00 +02001568 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001569 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001570 antenna = b43_antenna_to_phyctl(antenna);
1571 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1572 /* We can't send beacons with short preamble. Would get PHY errors. */
1573 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1574 ctl &= ~B43_TXH_PHY_ANT;
1575 ctl &= ~B43_TXH_PHY_ENC;
1576 ctl |= antenna;
1577 if (b43_is_cck_rate(rate))
1578 ctl |= B43_TXH_PHY_ENC_CCK;
1579 else
1580 ctl |= B43_TXH_PHY_ENC_OFDM;
1581 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1582
Michael Buesche66fee62007-12-26 17:47:10 +01001583 /* Find the position of the TIM and the DTIM_period value
1584 * and write them to SHM. */
1585 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001586 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1587 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001588 uint8_t ie_id, ie_len;
1589
1590 ie_id = ie[i];
1591 ie_len = ie[i + 1];
1592 if (ie_id == 5) {
1593 u16 tim_position;
1594 u16 dtim_period;
1595 /* This is the TIM Information Element */
1596
1597 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001598 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001599 break;
1600 /* A valid TIM is at least 4 bytes long. */
1601 if (ie_len < 4)
1602 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001603 tim_found = true;
Michael Buesche66fee62007-12-26 17:47:10 +01001604
1605 tim_position = sizeof(struct b43_plcp_hdr6);
1606 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1607 tim_position += i;
1608
1609 dtim_period = ie[i + 3];
1610
1611 b43_shm_write16(dev, B43_SHM_SHARED,
1612 B43_SHM_SH_TIMBPOS, tim_position);
1613 b43_shm_write16(dev, B43_SHM_SHARED,
1614 B43_SHM_SH_DTIMPER, dtim_period);
1615 break;
1616 }
1617 i += ie_len + 2;
1618 }
1619 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001620 /*
1621 * If ucode wants to modify TIM do it behind the beacon, this
1622 * will happen, for example, when doing mesh networking.
1623 */
1624 b43_shm_write16(dev, B43_SHM_SHARED,
1625 B43_SHM_SH_TIMBPOS,
1626 len + sizeof(struct b43_plcp_hdr6));
1627 b43_shm_write16(dev, B43_SHM_SHARED,
1628 B43_SHM_SH_DTIMPER, 0);
1629 }
1630 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Buesche4d6b792007-09-18 15:39:42 -04001631}
1632
Michael Buesch6b4bec012008-05-20 12:16:28 +02001633static void b43_upload_beacon0(struct b43_wldev *dev)
1634{
1635 struct b43_wl *wl = dev->wl;
1636
1637 if (wl->beacon0_uploaded)
1638 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001639 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE0, B43_SHM_SH_BTL0);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001640 wl->beacon0_uploaded = true;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001641}
1642
1643static void b43_upload_beacon1(struct b43_wldev *dev)
1644{
1645 struct b43_wl *wl = dev->wl;
1646
1647 if (wl->beacon1_uploaded)
1648 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001649 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE1, B43_SHM_SH_BTL1);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001650 wl->beacon1_uploaded = true;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001651}
1652
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001653static void handle_irq_beacon(struct b43_wldev *dev)
1654{
1655 struct b43_wl *wl = dev->wl;
1656 u32 cmd, beacon0_valid, beacon1_valid;
1657
Johannes Berg05c914f2008-09-11 00:01:58 +02001658 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
Manual Munz8c235162011-09-18 18:24:03 -05001659 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) &&
1660 !b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001661 return;
1662
1663 /* This is the bottom half of the asynchronous beacon update. */
1664
1665 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001666 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001667
1668 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1669 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1670 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1671
1672 /* Schedule interrupt manually, if busy. */
1673 if (beacon0_valid && beacon1_valid) {
1674 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001675 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001676 return;
1677 }
1678
Michael Buesch6b4bec012008-05-20 12:16:28 +02001679 if (unlikely(wl->beacon_templates_virgin)) {
1680 /* We never uploaded a beacon before.
1681 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001682 wl->beacon_templates_virgin = false;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001683 b43_upload_beacon0(dev);
1684 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001685 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1686 cmd |= B43_MACCMD_BEACON0_VALID;
1687 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec012008-05-20 12:16:28 +02001688 } else {
1689 if (!beacon0_valid) {
1690 b43_upload_beacon0(dev);
1691 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1692 cmd |= B43_MACCMD_BEACON0_VALID;
1693 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1694 } else if (!beacon1_valid) {
1695 b43_upload_beacon1(dev);
1696 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1697 cmd |= B43_MACCMD_BEACON1_VALID;
1698 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001699 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001700 }
1701}
1702
Michael Buesch36dbd952009-09-04 22:51:29 +02001703static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1704{
1705 u32 old_irq_mask = dev->irq_mask;
1706
1707 /* update beacon right away or defer to irq */
1708 handle_irq_beacon(dev);
1709 if (old_irq_mask != dev->irq_mask) {
1710 /* The handler updated the IRQ mask. */
1711 B43_WARN_ON(!dev->irq_mask);
1712 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1713 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1714 } else {
1715 /* Device interrupts are currently disabled. That means
1716 * we just ran the hardirq handler and scheduled the
1717 * IRQ thread. The thread will write the IRQ mask when
1718 * it finished, so there's nothing to do here. Writing
1719 * the mask _here_ would incorrectly re-enable IRQs. */
1720 }
1721 }
1722}
1723
Michael Buescha82d9922008-04-04 21:40:06 +02001724static void b43_beacon_update_trigger_work(struct work_struct *work)
1725{
1726 struct b43_wl *wl = container_of(work, struct b43_wl,
1727 beacon_update_trigger);
1728 struct b43_wldev *dev;
1729
1730 mutex_lock(&wl->mutex);
1731 dev = wl->current_dev;
1732 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Rafał Miłecki505fb012011-05-19 15:11:27 +02001733 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001734 /* wl->mutex is enough. */
1735 b43_do_beacon_update_trigger_work(dev);
1736 mmiowb();
1737 } else {
1738 spin_lock_irq(&wl->hardirq_lock);
1739 b43_do_beacon_update_trigger_work(dev);
1740 mmiowb();
1741 spin_unlock_irq(&wl->hardirq_lock);
1742 }
Michael Buescha82d9922008-04-04 21:40:06 +02001743 }
1744 mutex_unlock(&wl->mutex);
1745}
1746
Michael Bueschd4df6f12007-12-26 18:04:14 +01001747/* Asynchronously update the packet templates in template RAM.
Michael Buesch36dbd952009-09-04 22:51:29 +02001748 * Locking: Requires wl->mutex to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001749static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001750{
Johannes Berg9d139c82008-07-09 14:40:37 +02001751 struct sk_buff *beacon;
1752
Michael Buesche66fee62007-12-26 17:47:10 +01001753 /* This is the top half of the ansynchronous beacon update.
1754 * The bottom half is the beacon IRQ.
1755 * Beacon update must be asynchronous to avoid sending an
1756 * invalid beacon. This can happen for example, if the firmware
1757 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001758
Johannes Berg9d139c82008-07-09 14:40:37 +02001759 /* We could modify the existing beacon and set the aid bit in
1760 * the TIM field, but that would probably require resizing and
1761 * moving of data within the beacon template.
1762 * Simply request a new beacon and let mac80211 do the hard work. */
1763 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1764 if (unlikely(!beacon))
1765 return;
1766
Michael Buesche66fee62007-12-26 17:47:10 +01001767 if (wl->current_beacon)
1768 dev_kfree_skb_any(wl->current_beacon);
1769 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001770 wl->beacon0_uploaded = false;
1771 wl->beacon1_uploaded = false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001772 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Buesche4d6b792007-09-18 15:39:42 -04001773}
1774
Michael Buesche4d6b792007-09-18 15:39:42 -04001775static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1776{
1777 b43_time_lock(dev);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001778 if (dev->dev->core_rev >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001779 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1780 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001781 } else {
1782 b43_write16(dev, 0x606, (beacon_int >> 6));
1783 b43_write16(dev, 0x610, beacon_int);
1784 }
1785 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001786 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001787}
1788
Michael Bueschafa83e22008-05-19 23:51:37 +02001789static void b43_handle_firmware_panic(struct b43_wldev *dev)
1790{
1791 u16 reason;
1792
1793 /* Read the register that contains the reason code for the panic. */
1794 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1795 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1796
1797 switch (reason) {
1798 default:
1799 b43dbg(dev->wl, "The panic reason is unknown.\n");
1800 /* fallthrough */
1801 case B43_FWPANIC_DIE:
1802 /* Do not restart the controller or firmware.
1803 * The device is nonfunctional from now on.
1804 * Restarting would result in this panic to trigger again,
1805 * so we avoid that recursion. */
1806 break;
1807 case B43_FWPANIC_RESTART:
1808 b43_controller_restart(dev, "Microcode panic");
1809 break;
1810 }
1811}
1812
Michael Buesche4d6b792007-09-18 15:39:42 -04001813static void handle_irq_ucode_debug(struct b43_wldev *dev)
1814{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001815 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001816 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001817 __le16 *buf;
1818
1819 /* The proprietary firmware doesn't have this IRQ. */
1820 if (!dev->fw.opensource)
1821 return;
1822
Michael Bueschafa83e22008-05-19 23:51:37 +02001823 /* Read the register that contains the reason code for this IRQ. */
1824 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1825
Michael Buesche48b0ee2008-05-17 22:44:35 +02001826 switch (reason) {
1827 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001828 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001829 break;
1830 case B43_DEBUGIRQ_DUMP_SHM:
1831 if (!B43_DEBUG)
1832 break; /* Only with driver debugging enabled. */
1833 buf = kmalloc(4096, GFP_ATOMIC);
1834 if (!buf) {
1835 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1836 goto out;
1837 }
1838 for (i = 0; i < 4096; i += 2) {
1839 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1840 buf[i / 2] = cpu_to_le16(tmp);
1841 }
1842 b43info(dev->wl, "Shared memory dump:\n");
1843 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1844 16, 2, buf, 4096, 1);
1845 kfree(buf);
1846 break;
1847 case B43_DEBUGIRQ_DUMP_REGS:
1848 if (!B43_DEBUG)
1849 break; /* Only with driver debugging enabled. */
1850 b43info(dev->wl, "Microcode register dump:\n");
1851 for (i = 0, cnt = 0; i < 64; i++) {
1852 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1853 if (cnt == 0)
1854 printk(KERN_INFO);
1855 printk("r%02u: 0x%04X ", i, tmp);
1856 cnt++;
1857 if (cnt == 6) {
1858 printk("\n");
1859 cnt = 0;
1860 }
1861 }
1862 printk("\n");
1863 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001864 case B43_DEBUGIRQ_MARKER:
1865 if (!B43_DEBUG)
1866 break; /* Only with driver debugging enabled. */
1867 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1868 B43_MARKER_ID_REG);
1869 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1870 B43_MARKER_LINE_REG);
1871 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1872 "at line number %u\n",
1873 marker_id, marker_line);
1874 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001875 default:
1876 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1877 reason);
1878 }
1879out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001880 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1881 b43_shm_write16(dev, B43_SHM_SCRATCH,
1882 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001883}
1884
Michael Buesch36dbd952009-09-04 22:51:29 +02001885static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001886{
1887 u32 reason;
1888 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1889 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001890 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001891
Michael Buesch36dbd952009-09-04 22:51:29 +02001892 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1893 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001894
1895 reason = dev->irq_reason;
1896 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1897 dma_reason[i] = dev->dma_reason[i];
1898 merged_dma_reason |= dma_reason[i];
1899 }
1900
1901 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1902 b43err(dev->wl, "MAC transmission error\n");
1903
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001904 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001905 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001906 rmb();
1907 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1908 atomic_set(&dev->phy.txerr_cnt,
1909 B43_PHY_TX_BADNESS_LIMIT);
1910 b43err(dev->wl, "Too many PHY TX errors, "
1911 "restarting the controller\n");
1912 b43_controller_restart(dev, "PHY TX errors");
1913 }
1914 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001915
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001916 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK))) {
1917 b43err(dev->wl,
1918 "Fatal DMA error: 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X\n",
1919 dma_reason[0], dma_reason[1],
1920 dma_reason[2], dma_reason[3],
1921 dma_reason[4], dma_reason[5]);
1922 b43err(dev->wl, "This device does not support DMA "
Larry Fingerbb64d952010-06-19 08:29:08 -05001923 "on your system. It will now be switched to PIO.\n");
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001924 /* Fall back to PIO transfers if we get fatal DMA errors! */
1925 dev->use_pio = true;
1926 b43_controller_restart(dev, "DMA error");
1927 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001928 }
1929
1930 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1931 handle_irq_ucode_debug(dev);
1932 if (reason & B43_IRQ_TBTT_INDI)
1933 handle_irq_tbtt_indication(dev);
1934 if (reason & B43_IRQ_ATIM_END)
1935 handle_irq_atim_end(dev);
1936 if (reason & B43_IRQ_BEACON)
1937 handle_irq_beacon(dev);
1938 if (reason & B43_IRQ_PMQ)
1939 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02001940 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1941 ;/* TODO */
1942 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001943 handle_irq_noise(dev);
1944
1945 /* Check the DMA reason registers for received data. */
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001946 if (dma_reason[0] & B43_DMAIRQ_RDESC_UFLOW) {
1947 if (B43_DEBUG)
1948 b43warn(dev->wl, "RX descriptor underrun\n");
1949 b43_dma_handle_rx_overflow(dev->dma.rx_ring);
1950 }
Michael Buesch5100d5a2008-03-29 21:01:16 +01001951 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1952 if (b43_using_pio_transfers(dev))
1953 b43_pio_rx(dev->pio.rx_queue);
1954 else
1955 b43_dma_rx(dev->dma.rx_ring);
1956 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001957 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
1958 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01001959 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001960 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
1961 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
1962
Michael Buesch21954c32007-09-27 15:31:40 +02001963 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001964 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04001965
Michael Buesch36dbd952009-09-04 22:51:29 +02001966 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02001967 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesch990b86f2009-09-12 00:48:03 +02001968
1969#if B43_DEBUG
1970 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
1971 dev->irq_count++;
1972 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
1973 if (reason & (1 << i))
1974 dev->irq_bit_count[i]++;
1975 }
1976 }
1977#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04001978}
1979
Michael Buesch36dbd952009-09-04 22:51:29 +02001980/* Interrupt thread handler. Handles device interrupts in thread context. */
1981static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04001982{
Michael Buesche4d6b792007-09-18 15:39:42 -04001983 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02001984
1985 mutex_lock(&dev->wl->mutex);
1986 b43_do_interrupt_thread(dev);
1987 mmiowb();
1988 mutex_unlock(&dev->wl->mutex);
1989
1990 return IRQ_HANDLED;
1991}
1992
1993static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
1994{
Michael Buesche4d6b792007-09-18 15:39:42 -04001995 u32 reason;
1996
Michael Buesch36dbd952009-09-04 22:51:29 +02001997 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
1998 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001999
Michael Buesche4d6b792007-09-18 15:39:42 -04002000 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2001 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02002002 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02002003 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04002004 if (!reason)
Sebastian Andrzej Siewiorcae56142011-07-07 21:58:10 +02002005 return IRQ_NONE;
Michael Buesche4d6b792007-09-18 15:39:42 -04002006
2007 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02002008 & 0x0001FC00;
Michael Buesche4d6b792007-09-18 15:39:42 -04002009 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
2010 & 0x0000DC00;
2011 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
2012 & 0x0000DC00;
2013 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
2014 & 0x0001DC00;
2015 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
2016 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002017/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04002018 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
2019 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002020*/
Michael Buesche4d6b792007-09-18 15:39:42 -04002021
Michael Buesch36dbd952009-09-04 22:51:29 +02002022 /* ACK the interrupt. */
2023 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
2024 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
2025 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
2026 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
2027 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
2028 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
2029/* Unused ring
2030 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
2031*/
2032
2033 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02002034 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02002035 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002036 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02002037
2038 return IRQ_WAKE_THREAD;
2039}
2040
2041/* Interrupt handler top-half. This runs with interrupts disabled. */
2042static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
2043{
2044 struct b43_wldev *dev = dev_id;
2045 irqreturn_t ret;
2046
2047 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2048 return IRQ_NONE;
2049
2050 spin_lock(&dev->wl->hardirq_lock);
2051 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002052 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02002053 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04002054
2055 return ret;
2056}
2057
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002058/* SDIO interrupt handler. This runs in process context. */
2059static void b43_sdio_interrupt_handler(struct b43_wldev *dev)
2060{
2061 struct b43_wl *wl = dev->wl;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002062 irqreturn_t ret;
2063
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002064 mutex_lock(&wl->mutex);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002065
2066 ret = b43_do_interrupt(dev);
2067 if (ret == IRQ_WAKE_THREAD)
2068 b43_do_interrupt_thread(dev);
2069
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002070 mutex_unlock(&wl->mutex);
2071}
2072
Michael Buesch1a9f5092009-01-23 21:21:51 +01002073void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002074{
2075 release_firmware(fw->data);
2076 fw->data = NULL;
2077 fw->filename = NULL;
2078}
2079
Michael Buesche4d6b792007-09-18 15:39:42 -04002080static void b43_release_firmware(struct b43_wldev *dev)
2081{
Larry Finger0673eff2014-01-12 15:11:38 -06002082 complete(&dev->fw_load_complete);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002083 b43_do_release_fw(&dev->fw.ucode);
2084 b43_do_release_fw(&dev->fw.pcm);
2085 b43_do_release_fw(&dev->fw.initvals);
2086 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04002087}
2088
Michael Buescheb189d8b2008-01-28 14:47:41 -08002089static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04002090{
Hannes Ederfc68ed42009-02-14 11:50:06 +00002091 const char text[] =
2092 "You must go to " \
2093 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
2094 "and download the correct firmware for this driver version. " \
2095 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08002096
Michael Buescheb189d8b2008-01-28 14:47:41 -08002097 if (error)
2098 b43err(wl, text);
2099 else
2100 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04002101}
2102
Larry Finger5e20a4b2012-12-20 15:55:01 -06002103static void b43_fw_cb(const struct firmware *firmware, void *context)
2104{
2105 struct b43_request_fw_context *ctx = context;
2106
2107 ctx->blob = firmware;
Larry Finger0673eff2014-01-12 15:11:38 -06002108 complete(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002109}
2110
Michael Buesch1a9f5092009-01-23 21:21:51 +01002111int b43_do_request_fw(struct b43_request_fw_context *ctx,
2112 const char *name,
Larry Finger5e20a4b2012-12-20 15:55:01 -06002113 struct b43_firmware_file *fw, bool async)
Michael Buesche4d6b792007-09-18 15:39:42 -04002114{
Michael Buesche4d6b792007-09-18 15:39:42 -04002115 struct b43_fw_header *hdr;
2116 u32 size;
2117 int err;
2118
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002119 if (!name) {
2120 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002121 /* FIXME: We should probably keep it anyway, to save some headache
2122 * on suspend/resume with multiband devices. */
2123 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04002124 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002125 }
2126 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002127 if ((fw->type == ctx->req_type) &&
2128 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002129 return 0; /* Already have this fw. */
2130 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002131 /* FIXME: We should probably do this later after we successfully
2132 * got the new fw. This could reduce headache with multiband devices.
2133 * We could also redesign this to cache the firmware for all possible
2134 * bands all the time. */
2135 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002136 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002137
Michael Buesch1a9f5092009-01-23 21:21:51 +01002138 switch (ctx->req_type) {
2139 case B43_FWTYPE_PROPRIETARY:
2140 snprintf(ctx->fwname, sizeof(ctx->fwname),
2141 "b43%s/%s.fw",
2142 modparam_fwpostfix, name);
2143 break;
2144 case B43_FWTYPE_OPENSOURCE:
2145 snprintf(ctx->fwname, sizeof(ctx->fwname),
2146 "b43-open%s/%s.fw",
2147 modparam_fwpostfix, name);
2148 break;
2149 default:
2150 B43_WARN_ON(1);
2151 return -ENOSYS;
2152 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002153 if (async) {
2154 /* do this part asynchronously */
Larry Finger0673eff2014-01-12 15:11:38 -06002155 init_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002156 err = request_firmware_nowait(THIS_MODULE, 1, ctx->fwname,
2157 ctx->dev->dev->dev, GFP_KERNEL,
2158 ctx, b43_fw_cb);
2159 if (err < 0) {
2160 pr_err("Unable to load firmware\n");
2161 return err;
2162 }
Larry Finger0673eff2014-01-12 15:11:38 -06002163 wait_for_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002164 if (ctx->blob)
2165 goto fw_ready;
2166 /* On some ARM systems, the async request will fail, but the next sync
Larry Finger0673eff2014-01-12 15:11:38 -06002167 * request works. For this reason, we fall through here
Larry Finger5e20a4b2012-12-20 15:55:01 -06002168 */
2169 }
2170 err = request_firmware(&ctx->blob, ctx->fwname,
2171 ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002172 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002173 snprintf(ctx->errors[ctx->req_type],
2174 sizeof(ctx->errors[ctx->req_type]),
Larry Finger5e20a4b2012-12-20 15:55:01 -06002175 "Firmware file \"%s\" not found\n",
2176 ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002177 return err;
2178 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002179 snprintf(ctx->errors[ctx->req_type],
2180 sizeof(ctx->errors[ctx->req_type]),
2181 "Firmware file \"%s\" request failed (err=%d)\n",
2182 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002183 return err;
2184 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002185fw_ready:
2186 if (ctx->blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002187 goto err_format;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002188 hdr = (struct b43_fw_header *)(ctx->blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002189 switch (hdr->type) {
2190 case B43_FW_TYPE_UCODE:
2191 case B43_FW_TYPE_PCM:
2192 size = be32_to_cpu(hdr->size);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002193 if (size != ctx->blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002194 goto err_format;
2195 /* fallthrough */
2196 case B43_FW_TYPE_IV:
2197 if (hdr->ver != 1)
2198 goto err_format;
2199 break;
2200 default:
2201 goto err_format;
2202 }
2203
Larry Finger5e20a4b2012-12-20 15:55:01 -06002204 fw->data = ctx->blob;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002205 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002206 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002207
2208 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002209
2210err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002211 snprintf(ctx->errors[ctx->req_type],
2212 sizeof(ctx->errors[ctx->req_type]),
2213 "Firmware file \"%s\" format error.\n", ctx->fwname);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002214 release_firmware(ctx->blob);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002215
Michael Buesche4d6b792007-09-18 15:39:42 -04002216 return -EPROTO;
2217}
2218
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002219/* http://bcm-v4.sipsolutions.net/802.11/Init/Firmware */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002220static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002221{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002222 struct b43_wldev *dev = ctx->dev;
2223 struct b43_firmware *fw = &ctx->dev->fw;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002224 struct b43_phy *phy = &dev->phy;
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002225 const u8 rev = ctx->dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04002226 const char *filename;
Michael Buesche4d6b792007-09-18 15:39:42 -04002227 int err;
2228
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002229 /* Get microcode */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002230 filename = NULL;
2231 switch (rev) {
2232 case 42:
2233 if (phy->type == B43_PHYTYPE_AC)
2234 filename = "ucode42";
2235 break;
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002236 case 40:
2237 if (phy->type == B43_PHYTYPE_AC)
2238 filename = "ucode40";
2239 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002240 case 33:
2241 if (phy->type == B43_PHYTYPE_LCN40)
2242 filename = "ucode33_lcn40";
2243 break;
2244 case 30:
2245 if (phy->type == B43_PHYTYPE_N)
2246 filename = "ucode30_mimo";
2247 break;
2248 case 29:
2249 if (phy->type == B43_PHYTYPE_HT)
2250 filename = "ucode29_mimo";
2251 break;
2252 case 26:
2253 if (phy->type == B43_PHYTYPE_HT)
2254 filename = "ucode26_mimo";
2255 break;
2256 case 28:
2257 case 25:
2258 if (phy->type == B43_PHYTYPE_N)
2259 filename = "ucode25_mimo";
2260 else if (phy->type == B43_PHYTYPE_LCN)
2261 filename = "ucode25_lcn";
2262 break;
2263 case 24:
2264 if (phy->type == B43_PHYTYPE_LCN)
2265 filename = "ucode24_lcn";
2266 break;
2267 case 23:
2268 if (phy->type == B43_PHYTYPE_N)
2269 filename = "ucode16_mimo";
2270 break;
2271 case 16 ... 19:
2272 if (phy->type == B43_PHYTYPE_N)
2273 filename = "ucode16_mimo";
2274 else if (phy->type == B43_PHYTYPE_LP)
2275 filename = "ucode16_lp";
2276 break;
2277 case 15:
Gábor Stefanik759b9732009-08-14 14:39:53 +02002278 filename = "ucode15";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002279 break;
2280 case 14:
2281 filename = "ucode14";
2282 break;
2283 case 13:
2284 filename = "ucode13";
2285 break;
2286 case 11 ... 12:
2287 filename = "ucode11";
2288 break;
2289 case 5 ... 10:
2290 filename = "ucode5";
2291 break;
Rafał Miłecki6ff1e5c2011-07-06 17:41:55 +02002292 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002293 if (!filename)
2294 goto err_no_ucode;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002295 err = b43_do_request_fw(ctx, filename, &fw->ucode, true);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002296 if (err)
2297 goto err_load;
2298
2299 /* Get PCM code */
2300 if ((rev >= 5) && (rev <= 10))
2301 filename = "pcm5";
2302 else if (rev >= 11)
2303 filename = NULL;
2304 else
2305 goto err_no_pcm;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002306 fw->pcm_request_failed = false;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002307 err = b43_do_request_fw(ctx, filename, &fw->pcm, false);
Michael Buesch68217832008-05-17 23:43:57 +02002308 if (err == -ENOENT) {
2309 /* We did not find a PCM file? Not fatal, but
2310 * core rev <= 10 must do without hwcrypto then. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002311 fw->pcm_request_failed = true;
Michael Buesch68217832008-05-17 23:43:57 +02002312 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002313 goto err_load;
2314
2315 /* Get initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002316 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002317 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002318 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002319 if (rev == 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002320 filename = "b0g0initvals13";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002321 else if (rev >= 5 && rev <= 10)
2322 filename = "b0g0initvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002323 break;
2324 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002325 if (rev == 30)
2326 filename = "n16initvals30";
2327 else if (rev == 28 || rev == 25)
2328 filename = "n0initvals25";
2329 else if (rev == 24)
2330 filename = "n0initvals24";
2331 else if (rev == 23)
2332 filename = "n0initvals16"; /* What about n0initvals22? */
2333 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002334 filename = "n0initvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002335 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002336 filename = "n0initvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002337 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002338 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002339 if (rev >= 16 && rev <= 18)
2340 filename = "lp0initvals16";
2341 else if (rev == 15)
2342 filename = "lp0initvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002343 else if (rev == 14)
2344 filename = "lp0initvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002345 else if (rev == 13)
2346 filename = "lp0initvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002347 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002348 case B43_PHYTYPE_HT:
2349 if (rev == 29)
2350 filename = "ht0initvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002351 else if (rev == 26)
2352 filename = "ht0initvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002353 break;
2354 case B43_PHYTYPE_LCN:
2355 if (rev == 24)
2356 filename = "lcn0initvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002357 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002358 case B43_PHYTYPE_LCN40:
2359 if (rev == 33)
2360 filename = "lcn400initvals33";
2361 break;
2362 case B43_PHYTYPE_AC:
2363 if (rev == 42)
2364 filename = "ac1initvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002365 else if (rev == 40)
2366 filename = "ac0initvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002367 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002368 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002369 if (!filename)
2370 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002371 err = b43_do_request_fw(ctx, filename, &fw->initvals, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002372 if (err)
2373 goto err_load;
2374
2375 /* Get bandswitch initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002376 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002377 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002378 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002379 if (rev == 13)
2380 filename = "b0g0bsinitvals13";
2381 else if (rev >= 5 && rev <= 10)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002382 filename = "b0g0bsinitvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002383 break;
2384 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002385 if (rev == 30)
2386 filename = "n16bsinitvals30";
2387 else if (rev == 28 || rev == 25)
2388 filename = "n0bsinitvals25";
2389 else if (rev == 24)
2390 filename = "n0bsinitvals24";
2391 else if (rev == 23)
2392 filename = "n0bsinitvals16"; /* What about n0bsinitvals22? */
2393 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002394 filename = "n0bsinitvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002395 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002396 filename = "n0bsinitvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002397 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002398 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002399 if (rev >= 16 && rev <= 18)
2400 filename = "lp0bsinitvals16";
2401 else if (rev == 15)
2402 filename = "lp0bsinitvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002403 else if (rev == 14)
2404 filename = "lp0bsinitvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002405 else if (rev == 13)
2406 filename = "lp0bsinitvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002407 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002408 case B43_PHYTYPE_HT:
2409 if (rev == 29)
2410 filename = "ht0bsinitvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002411 else if (rev == 26)
2412 filename = "ht0bsinitvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002413 break;
2414 case B43_PHYTYPE_LCN:
2415 if (rev == 24)
2416 filename = "lcn0bsinitvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002417 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002418 case B43_PHYTYPE_LCN40:
2419 if (rev == 33)
2420 filename = "lcn400bsinitvals33";
2421 break;
2422 case B43_PHYTYPE_AC:
2423 if (rev == 42)
2424 filename = "ac1bsinitvals42";
Rafał Miłecki15be8e82014-07-01 16:33:57 +02002425 else if (rev == 40)
2426 filename = "ac0bsinitvals40";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002427 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002428 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002429 if (!filename)
2430 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002431 err = b43_do_request_fw(ctx, filename, &fw->initvals_band, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002432 if (err)
2433 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002434
Johannes Berg097b0e12012-07-17 17:12:29 +02002435 fw->opensource = (ctx->req_type == B43_FWTYPE_OPENSOURCE);
2436
Michael Buesche4d6b792007-09-18 15:39:42 -04002437 return 0;
2438
Michael Buesche4d6b792007-09-18 15:39:42 -04002439err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002440 err = ctx->fatal_failure = -EOPNOTSUPP;
2441 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2442 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002443 goto error;
2444
2445err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002446 err = ctx->fatal_failure = -EOPNOTSUPP;
2447 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2448 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002449 goto error;
2450
2451err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002452 err = ctx->fatal_failure = -EOPNOTSUPP;
2453 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2454 "is required for your device (wl-core rev %u)\n", rev);
2455 goto error;
2456
2457err_load:
2458 /* We failed to load this firmware image. The error message
2459 * already is in ctx->errors. Return and let our caller decide
2460 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002461 goto error;
2462
2463error:
2464 b43_release_firmware(dev);
2465 return err;
2466}
2467
Larry Finger6b6fa582012-03-08 22:27:46 -06002468static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl);
2469static void b43_one_core_detach(struct b43_bus_dev *dev);
Larry Finger09164042014-01-12 15:11:37 -06002470static int b43_rng_init(struct b43_wl *wl);
Larry Finger6b6fa582012-03-08 22:27:46 -06002471
2472static void b43_request_firmware(struct work_struct *work)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002473{
Larry Finger6b6fa582012-03-08 22:27:46 -06002474 struct b43_wl *wl = container_of(work,
2475 struct b43_wl, firmware_load);
2476 struct b43_wldev *dev = wl->current_dev;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002477 struct b43_request_fw_context *ctx;
2478 unsigned int i;
2479 int err;
2480 const char *errmsg;
2481
2482 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2483 if (!ctx)
Larry Finger6b6fa582012-03-08 22:27:46 -06002484 return;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002485 ctx->dev = dev;
2486
2487 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2488 err = b43_try_request_fw(ctx);
2489 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002490 goto start_ieee80211; /* Successfully loaded it. */
2491 /* Was fw version known? */
2492 if (ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002493 goto out;
2494
Larry Finger6b6fa582012-03-08 22:27:46 -06002495 /* proprietary fw not found, try open source */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002496 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2497 err = b43_try_request_fw(ctx);
2498 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002499 goto start_ieee80211; /* Successfully loaded it. */
2500 if(ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002501 goto out;
2502
2503 /* Could not find a usable firmware. Print the errors. */
2504 for (i = 0; i < B43_NR_FWTYPES; i++) {
2505 errmsg = ctx->errors[i];
2506 if (strlen(errmsg))
Kees Cooke0e29b62013-05-10 14:48:21 -07002507 b43err(dev->wl, "%s", errmsg);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002508 }
2509 b43_print_fw_helptext(dev->wl, 1);
Larry Finger6b6fa582012-03-08 22:27:46 -06002510 goto out;
2511
2512start_ieee80211:
Johannes Berg097b0e12012-07-17 17:12:29 +02002513 wl->hw->queues = B43_QOS_QUEUE_NUM;
2514 if (!modparam_qos || dev->fw.opensource)
2515 wl->hw->queues = 1;
2516
Larry Finger6b6fa582012-03-08 22:27:46 -06002517 err = ieee80211_register_hw(wl->hw);
2518 if (err)
2519 goto err_one_core_detach;
Oleksij Rempele64add22012-06-05 20:39:32 +02002520 wl->hw_registred = true;
Larry Finger6b6fa582012-03-08 22:27:46 -06002521 b43_leds_register(wl->current_dev);
Larry Finger09164042014-01-12 15:11:37 -06002522
2523 /* Register HW RNG driver */
2524 b43_rng_init(wl);
2525
Larry Finger6b6fa582012-03-08 22:27:46 -06002526 goto out;
2527
2528err_one_core_detach:
2529 b43_one_core_detach(dev->dev);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002530
2531out:
2532 kfree(ctx);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002533}
2534
Michael Buesche4d6b792007-09-18 15:39:42 -04002535static int b43_upload_microcode(struct b43_wldev *dev)
2536{
John W. Linville652caa52010-07-29 13:27:28 -04002537 struct wiphy *wiphy = dev->wl->hw->wiphy;
Michael Buesche4d6b792007-09-18 15:39:42 -04002538 const size_t hdr_len = sizeof(struct b43_fw_header);
2539 const __be32 *data;
2540 unsigned int i, len;
2541 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002542 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002543 int err = 0;
2544
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002545 /* Jump the microcode PSM to offset 0 */
2546 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2547 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2548 macctl |= B43_MACCTL_PSM_JMP0;
2549 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2550 /* Zero out all microcode PSM registers and shared memory. */
2551 for (i = 0; i < 64; i++)
2552 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2553 for (i = 0; i < 4096; i += 2)
2554 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2555
Michael Buesche4d6b792007-09-18 15:39:42 -04002556 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002557 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2558 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002559 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2560 for (i = 0; i < len; i++) {
2561 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2562 udelay(10);
2563 }
2564
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002565 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002566 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002567 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2568 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002569 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2570 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2571 /* No need for autoinc bit in SHM_HW */
2572 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2573 for (i = 0; i < len; i++) {
2574 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2575 udelay(10);
2576 }
2577 }
2578
2579 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002580
2581 /* Start the microcode PSM */
Rafał Miłecki50566352012-01-02 19:31:21 +01002582 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_JMP0,
2583 B43_MACCTL_PSM_RUN);
Michael Buesche4d6b792007-09-18 15:39:42 -04002584
2585 /* Wait for the microcode to load and respond */
2586 i = 0;
2587 while (1) {
2588 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2589 if (tmp == B43_IRQ_MAC_SUSPENDED)
2590 break;
2591 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002592 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002593 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002594 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002595 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002596 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002597 }
Michael Buesche175e992009-09-11 18:31:32 +02002598 msleep(50);
Michael Buesche4d6b792007-09-18 15:39:42 -04002599 }
2600 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2601
2602 /* Get and check the revisions. */
2603 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2604 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2605 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2606 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2607
2608 if (fwrev <= 0x128) {
2609 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2610 "binary drivers older than version 4.x is unsupported. "
2611 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002612 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002613 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002614 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002615 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002616 dev->fw.rev = fwrev;
2617 dev->fw.patch = fwpatch;
Rafał Miłecki5d852902011-08-11 15:07:16 +02002618 if (dev->fw.rev >= 598)
2619 dev->fw.hdr_format = B43_FW_HDR_598;
2620 else if (dev->fw.rev >= 410)
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002621 dev->fw.hdr_format = B43_FW_HDR_410;
2622 else
2623 dev->fw.hdr_format = B43_FW_HDR_351;
Johannes Berg097b0e12012-07-17 17:12:29 +02002624 WARN_ON(dev->fw.opensource != (fwdate == 0xFFFF));
Michael Buesche48b0ee2008-05-17 22:44:35 +02002625
Johannes Berg097b0e12012-07-17 17:12:29 +02002626 dev->qos_enabled = dev->wl->hw->queues > 1;
Michael Buesch403a3a12009-06-08 21:04:57 +02002627 /* Default to firmware/hardware crypto acceleration. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002628 dev->hwcrypto_enabled = true;
Michael Buesch403a3a12009-06-08 21:04:57 +02002629
Michael Buesche48b0ee2008-05-17 22:44:35 +02002630 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002631 u16 fwcapa;
2632
Michael Buesche48b0ee2008-05-17 22:44:35 +02002633 /* Patchlevel info is encoded in the "time" field. */
2634 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002635 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2636 dev->fw.rev, dev->fw.patch);
2637
2638 fwcapa = b43_fwcapa_read(dev);
2639 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2640 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2641 /* Disable hardware crypto and fall back to software crypto. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002642 dev->hwcrypto_enabled = false;
Michael Buesch403a3a12009-06-08 21:04:57 +02002643 }
Johannes Berg097b0e12012-07-17 17:12:29 +02002644 /* adding QoS support should use an offline discovery mechanism */
2645 WARN(fwcapa & B43_FWCAPA_QOS, "QoS in OpenFW not supported\n");
Michael Buesche48b0ee2008-05-17 22:44:35 +02002646 } else {
2647 b43info(dev->wl, "Loading firmware version %u.%u "
2648 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2649 fwrev, fwpatch,
2650 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2651 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002652 if (dev->fw.pcm_request_failed) {
2653 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2654 "Hardware accelerated cryptography is disabled.\n");
2655 b43_print_fw_helptext(dev->wl, 0);
2656 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002657 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002658
John W. Linville652caa52010-07-29 13:27:28 -04002659 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
2660 dev->fw.rev, dev->fw.patch);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002661 wiphy->hw_version = dev->dev->core_id;
John W. Linville652caa52010-07-29 13:27:28 -04002662
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002663 if (dev->fw.hdr_format == B43_FW_HDR_351) {
Michael Bueschc5572892008-12-27 18:26:39 +01002664 /* We're over the deadline, but we keep support for old fw
2665 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002666 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002667 "Support for old firmware will be removed soon "
2668 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002669 b43_print_fw_helptext(dev->wl, 0);
2670 }
2671
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002672 return 0;
2673
2674error:
Rafał Miłecki50566352012-01-02 19:31:21 +01002675 /* Stop the microcode PSM. */
2676 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
2677 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002678
Michael Buesche4d6b792007-09-18 15:39:42 -04002679 return err;
2680}
2681
2682static int b43_write_initvals(struct b43_wldev *dev,
2683 const struct b43_iv *ivals,
2684 size_t count,
2685 size_t array_size)
2686{
2687 const struct b43_iv *iv;
2688 u16 offset;
2689 size_t i;
2690 bool bit32;
2691
2692 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2693 iv = ivals;
2694 for (i = 0; i < count; i++) {
2695 if (array_size < sizeof(iv->offset_size))
2696 goto err_format;
2697 array_size -= sizeof(iv->offset_size);
2698 offset = be16_to_cpu(iv->offset_size);
2699 bit32 = !!(offset & B43_IV_32BIT);
2700 offset &= B43_IV_OFFSET_MASK;
2701 if (offset >= 0x1000)
2702 goto err_format;
2703 if (bit32) {
2704 u32 value;
2705
2706 if (array_size < sizeof(iv->data.d32))
2707 goto err_format;
2708 array_size -= sizeof(iv->data.d32);
2709
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002710 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002711 b43_write32(dev, offset, value);
2712
2713 iv = (const struct b43_iv *)((const uint8_t *)iv +
2714 sizeof(__be16) +
2715 sizeof(__be32));
2716 } else {
2717 u16 value;
2718
2719 if (array_size < sizeof(iv->data.d16))
2720 goto err_format;
2721 array_size -= sizeof(iv->data.d16);
2722
2723 value = be16_to_cpu(iv->data.d16);
2724 b43_write16(dev, offset, value);
2725
2726 iv = (const struct b43_iv *)((const uint8_t *)iv +
2727 sizeof(__be16) +
2728 sizeof(__be16));
2729 }
2730 }
2731 if (array_size)
2732 goto err_format;
2733
2734 return 0;
2735
2736err_format:
2737 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002738 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002739
2740 return -EPROTO;
2741}
2742
2743static int b43_upload_initvals(struct b43_wldev *dev)
2744{
2745 const size_t hdr_len = sizeof(struct b43_fw_header);
2746 const struct b43_fw_header *hdr;
2747 struct b43_firmware *fw = &dev->fw;
2748 const struct b43_iv *ivals;
2749 size_t count;
Michael Buesche4d6b792007-09-18 15:39:42 -04002750
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002751 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2752 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002753 count = be32_to_cpu(hdr->size);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002754 return b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002755 fw->initvals.data->size - hdr_len);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002756}
Michael Buesche4d6b792007-09-18 15:39:42 -04002757
Rafał Miłecki0f684232014-05-17 23:24:53 +02002758static int b43_upload_initvals_band(struct b43_wldev *dev)
2759{
2760 const size_t hdr_len = sizeof(struct b43_fw_header);
2761 const struct b43_fw_header *hdr;
2762 struct b43_firmware *fw = &dev->fw;
2763 const struct b43_iv *ivals;
2764 size_t count;
2765
2766 if (!fw->initvals_band.data)
2767 return 0;
2768
2769 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2770 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2771 count = be32_to_cpu(hdr->size);
2772 return b43_write_initvals(dev, ivals, count,
2773 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002774}
2775
2776/* Initialize the GPIOs
2777 * http://bcm-specs.sipsolutions.net/GPIO
2778 */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002779
2780#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002781static struct ssb_device *b43_ssb_gpio_dev(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002782{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02002783 struct ssb_bus *bus = dev->dev->sdev->bus;
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002784
2785#ifdef CONFIG_SSB_DRIVER_PCICORE
2786 return (bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev);
2787#else
2788 return bus->chipco.dev;
2789#endif
2790}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002791#endif
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002792
Michael Buesche4d6b792007-09-18 15:39:42 -04002793static int b43_gpio_init(struct b43_wldev *dev)
2794{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002795#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002796 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002797#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002798 u32 mask, set;
2799
Rafał Miłecki50566352012-01-02 19:31:21 +01002800 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_GPOUTSMSK, 0);
2801 b43_maskset16(dev, B43_MMIO_GPIO_MASK, ~0, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04002802
2803 mask = 0x0000001F;
2804 set = 0x0000000F;
Rafał Miłeckic244e082011-05-18 02:06:41 +02002805 if (dev->dev->chip_id == 0x4301) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002806 mask |= 0x0060;
2807 set |= 0x0060;
Rafał Miłecki828afd22012-07-23 22:57:01 +02002808 } else if (dev->dev->chip_id == 0x5354) {
2809 /* Don't allow overtaking buttons GPIOs */
2810 set &= 0x2; /* 0x2 is LED GPIO on BCM5354 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002811 }
Rafał Miłecki828afd22012-07-23 22:57:01 +02002812
Michael Buesche4d6b792007-09-18 15:39:42 -04002813 if (0 /* FIXME: conditional unknown */ ) {
2814 b43_write16(dev, B43_MMIO_GPIO_MASK,
2815 b43_read16(dev, B43_MMIO_GPIO_MASK)
2816 | 0x0100);
Rafał Miłecki828afd22012-07-23 22:57:01 +02002817 /* BT Coexistance Input */
2818 mask |= 0x0080;
2819 set |= 0x0080;
2820 /* BT Coexistance Out */
2821 mask |= 0x0100;
2822 set |= 0x0100;
Michael Buesche4d6b792007-09-18 15:39:42 -04002823 }
Rafał Miłecki05814832011-05-18 02:06:39 +02002824 if (dev->dev->bus_sprom->boardflags_lo & B43_BFL_PACTRL) {
Rafał Miłecki828afd22012-07-23 22:57:01 +02002825 /* PA is controlled by gpio 9, let ucode handle it */
Michael Buesche4d6b792007-09-18 15:39:42 -04002826 b43_write16(dev, B43_MMIO_GPIO_MASK,
2827 b43_read16(dev, B43_MMIO_GPIO_MASK)
2828 | 0x0200);
2829 mask |= 0x0200;
2830 set |= 0x0200;
2831 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002832
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002833 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002834#ifdef CONFIG_B43_BCMA
2835 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002836 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, mask, set);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002837 break;
2838#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002839#ifdef CONFIG_B43_SSB
2840 case B43_BUS_SSB:
2841 gpiodev = b43_ssb_gpio_dev(dev);
2842 if (gpiodev)
2843 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2844 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
Rafał Miłecki828afd22012-07-23 22:57:01 +02002845 & ~mask) | set);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002846 break;
2847#endif
2848 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002849
2850 return 0;
2851}
2852
2853/* Turn off all GPIO stuff. Call this on module unload, for example. */
2854static void b43_gpio_cleanup(struct b43_wldev *dev)
2855{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002856#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002857 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002858#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002859
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002860 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002861#ifdef CONFIG_B43_BCMA
2862 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002863 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, ~0, 0);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002864 break;
2865#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002866#ifdef CONFIG_B43_SSB
2867 case B43_BUS_SSB:
2868 gpiodev = b43_ssb_gpio_dev(dev);
2869 if (gpiodev)
2870 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2871 break;
2872#endif
2873 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002874}
2875
2876/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002877void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002878{
Michael Buesch923fd702008-06-20 18:02:08 +02002879 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2880 u16 fwstate;
2881
2882 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2883 B43_SHM_SH_UCODESTAT);
2884 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2885 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2886 b43err(dev->wl, "b43_mac_enable(): The firmware "
2887 "should be suspended, but current state is %u\n",
2888 fwstate);
2889 }
2890 }
2891
Michael Buesche4d6b792007-09-18 15:39:42 -04002892 dev->mac_suspended--;
2893 B43_WARN_ON(dev->mac_suspended < 0);
2894 if (dev->mac_suspended == 0) {
Rafał Miłecki50566352012-01-02 19:31:21 +01002895 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_ENABLED);
Michael Buesche4d6b792007-09-18 15:39:42 -04002896 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2897 B43_IRQ_MAC_SUSPENDED);
2898 /* Commit writes */
2899 b43_read32(dev, B43_MMIO_MACCTL);
2900 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2901 b43_power_saving_ctl_bits(dev, 0);
2902 }
2903}
2904
2905/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002906void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002907{
2908 int i;
2909 u32 tmp;
2910
Michael Buesch05b64b32007-09-28 16:19:03 +02002911 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002912 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002913
Michael Buesche4d6b792007-09-18 15:39:42 -04002914 if (dev->mac_suspended == 0) {
2915 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Rafał Miłecki50566352012-01-02 19:31:21 +01002916 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_ENABLED, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04002917 /* force pci to flush the write */
2918 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02002919 for (i = 35; i; i--) {
2920 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2921 if (tmp & B43_IRQ_MAC_SUSPENDED)
2922 goto out;
2923 udelay(10);
2924 }
2925 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02002926 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002927 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2928 if (tmp & B43_IRQ_MAC_SUSPENDED)
2929 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02002930 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002931 }
2932 b43err(dev->wl, "MAC suspend failed\n");
2933 }
Michael Buesch05b64b32007-09-28 16:19:03 +02002934out:
Michael Buesche4d6b792007-09-18 15:39:42 -04002935 dev->mac_suspended++;
2936}
2937
Rafał Miłecki858a1652011-05-10 16:05:33 +02002938/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MacPhyClkSet */
2939void b43_mac_phy_clock_set(struct b43_wldev *dev, bool on)
2940{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002941 u32 tmp;
2942
2943 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002944#ifdef CONFIG_B43_BCMA
2945 case B43_BUS_BCMA:
Rafał Miłecki36677872011-07-16 18:27:55 +02002946 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002947 if (on)
2948 tmp |= B43_BCMA_IOCTL_MACPHYCLKEN;
2949 else
2950 tmp &= ~B43_BCMA_IOCTL_MACPHYCLKEN;
Rafał Miłecki36677872011-07-16 18:27:55 +02002951 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002952 break;
2953#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002954#ifdef CONFIG_B43_SSB
2955 case B43_BUS_SSB:
2956 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
2957 if (on)
2958 tmp |= B43_TMSLOW_MACPHYCLKEN;
2959 else
2960 tmp &= ~B43_TMSLOW_MACPHYCLKEN;
2961 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
2962 break;
2963#endif
2964 }
Rafał Miłecki858a1652011-05-10 16:05:33 +02002965}
2966
Michael Buesche4d6b792007-09-18 15:39:42 -04002967static void b43_adjust_opmode(struct b43_wldev *dev)
2968{
2969 struct b43_wl *wl = dev->wl;
2970 u32 ctl;
2971 u16 cfp_pretbtt;
2972
2973 ctl = b43_read32(dev, B43_MMIO_MACCTL);
2974 /* Reset status to STA infrastructure mode. */
2975 ctl &= ~B43_MACCTL_AP;
2976 ctl &= ~B43_MACCTL_KEEP_CTL;
2977 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
2978 ctl &= ~B43_MACCTL_KEEP_BAD;
2979 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002980 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04002981 ctl |= B43_MACCTL_INFRA;
2982
Johannes Berg05c914f2008-09-11 00:01:58 +02002983 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
2984 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04002985 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02002986 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04002987 ctl &= ~B43_MACCTL_INFRA;
2988
2989 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04002990 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04002991 if (wl->filter_flags & FIF_FCSFAIL)
2992 ctl |= B43_MACCTL_KEEP_BAD;
2993 if (wl->filter_flags & FIF_PLCPFAIL)
2994 ctl |= B43_MACCTL_KEEP_BADPLCP;
2995 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04002996 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002997 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2998 ctl |= B43_MACCTL_BEACPROMISC;
2999
Michael Buesche4d6b792007-09-18 15:39:42 -04003000 /* Workaround: On old hardware the HW-MAC-address-filter
3001 * doesn't work properly, so always run promisc in filter
3002 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003003 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04003004 ctl |= B43_MACCTL_PROMISC;
3005
3006 b43_write32(dev, B43_MMIO_MACCTL, ctl);
3007
3008 cfp_pretbtt = 2;
3009 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02003010 if (dev->dev->chip_id == 0x4306 &&
3011 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04003012 cfp_pretbtt = 100;
3013 else
3014 cfp_pretbtt = 50;
3015 }
3016 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02003017
3018 /* FIXME: We don't currently implement the PMQ mechanism,
3019 * so always disable it. If we want to implement PMQ,
3020 * we need to enable it here (clear DISCPMQ) in AP mode.
3021 */
Rafał Miłecki50566352012-01-02 19:31:21 +01003022 if (0 /* ctl & B43_MACCTL_AP */)
3023 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3024 else
3025 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003026}
3027
3028static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3029{
3030 u16 offset;
3031
3032 if (is_ofdm) {
3033 offset = 0x480;
3034 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3035 } else {
3036 offset = 0x4C0;
3037 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3038 }
3039 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3040 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3041}
3042
3043static void b43_rate_memory_init(struct b43_wldev *dev)
3044{
3045 switch (dev->phy.type) {
3046 case B43_PHYTYPE_A:
3047 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003048 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003049 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003050 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003051 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003052 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
3053 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3054 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3055 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3056 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3057 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3058 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3059 if (dev->phy.type == B43_PHYTYPE_A)
3060 break;
3061 /* fallthrough */
3062 case B43_PHYTYPE_B:
3063 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3064 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3065 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3066 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3067 break;
3068 default:
3069 B43_WARN_ON(1);
3070 }
3071}
3072
Michael Buesch5042c502008-04-05 15:05:00 +02003073/* Set the default values for the PHY TX Control Words. */
3074static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3075{
3076 u16 ctl = 0;
3077
3078 ctl |= B43_TXH_PHY_ENC_CCK;
3079 ctl |= B43_TXH_PHY_ANT01AUTO;
3080 ctl |= B43_TXH_PHY_TXPWR;
3081
3082 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3083 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3084 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3085}
3086
Michael Buesche4d6b792007-09-18 15:39:42 -04003087/* Set the TX-Antenna for management frames sent by firmware. */
3088static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3089{
Michael Buesch5042c502008-04-05 15:05:00 +02003090 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003091 u16 tmp;
3092
Michael Buesch5042c502008-04-05 15:05:00 +02003093 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003094
Michael Buesche4d6b792007-09-18 15:39:42 -04003095 /* For ACK/CTS */
3096 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003097 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003098 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3099 /* For Probe Resposes */
3100 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003101 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003102 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3103}
3104
3105/* This is the opposite of b43_chip_init() */
3106static void b43_chip_exit(struct b43_wldev *dev)
3107{
Michael Bueschfb111372008-09-02 13:00:34 +02003108 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003109 b43_gpio_cleanup(dev);
3110 /* firmware is released later */
3111}
3112
3113/* Initialize the chip
3114 * http://bcm-specs.sipsolutions.net/ChipInit
3115 */
3116static int b43_chip_init(struct b43_wldev *dev)
3117{
3118 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003119 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003120 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003121 u16 value16;
3122
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003123 /* Initialize the MAC control */
3124 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3125 if (dev->phy.gmode)
3126 macctl |= B43_MACCTL_GMODE;
3127 macctl |= B43_MACCTL_INFRA;
3128 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003129
Michael Buesche4d6b792007-09-18 15:39:42 -04003130 err = b43_upload_microcode(dev);
3131 if (err)
3132 goto out; /* firmware is released later */
3133
3134 err = b43_gpio_init(dev);
3135 if (err)
3136 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003137
Michael Buesche4d6b792007-09-18 15:39:42 -04003138 err = b43_upload_initvals(dev);
3139 if (err)
Larry Finger1a8d1222007-12-14 13:59:11 +01003140 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003141
Rafał Miłecki0f684232014-05-17 23:24:53 +02003142 err = b43_upload_initvals_band(dev);
3143 if (err)
3144 goto err_gpio_clean;
3145
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003146 /* Turn the Analog on and initialize the PHY. */
3147 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003148 err = b43_phy_init(dev);
3149 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003150 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003151
Michael Bueschef1a6282008-08-27 18:53:02 +02003152 /* Disable Interference Mitigation. */
3153 if (phy->ops->interf_mitigation)
3154 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003155
Michael Bueschef1a6282008-08-27 18:53:02 +02003156 /* Select the antennae */
3157 if (phy->ops->set_rx_antenna)
3158 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003159 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3160
3161 if (phy->type == B43_PHYTYPE_B) {
3162 value16 = b43_read16(dev, 0x005E);
3163 value16 |= 0x0004;
3164 b43_write16(dev, 0x005E, value16);
3165 }
3166 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003167 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003168 b43_write32(dev, 0x010C, 0x01000000);
3169
Rafał Miłecki50566352012-01-02 19:31:21 +01003170 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3171 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003172
Michael Buesche4d6b792007-09-18 15:39:42 -04003173 /* Probe Response Timeout value */
3174 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003175 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003176
3177 /* Initially set the wireless operation mode. */
3178 b43_adjust_opmode(dev);
3179
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003180 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003181 b43_write16(dev, 0x060E, 0x0000);
3182 b43_write16(dev, 0x0610, 0x8000);
3183 b43_write16(dev, 0x0604, 0x0000);
3184 b43_write16(dev, 0x0606, 0x0200);
3185 } else {
3186 b43_write32(dev, 0x0188, 0x80000000);
3187 b43_write32(dev, 0x018C, 0x02000000);
3188 }
3189 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003190 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003191 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3192 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3193 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3194 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3195 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3196
Rafał Miłecki858a1652011-05-10 16:05:33 +02003197 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003198
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003199 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003200#ifdef CONFIG_B43_BCMA
3201 case B43_BUS_BCMA:
3202 /* FIXME: 0xE74 is quite common, but should be read from CC */
3203 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3204 break;
3205#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003206#ifdef CONFIG_B43_SSB
3207 case B43_BUS_SSB:
3208 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3209 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3210 break;
3211#endif
3212 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003213
3214 err = 0;
3215 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003216out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003217 return err;
3218
Larry Finger1a8d1222007-12-14 13:59:11 +01003219err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003220 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003221 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003222}
3223
Michael Buesche4d6b792007-09-18 15:39:42 -04003224static void b43_periodic_every60sec(struct b43_wldev *dev)
3225{
Michael Bueschef1a6282008-08-27 18:53:02 +02003226 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003227
Michael Bueschef1a6282008-08-27 18:53:02 +02003228 if (ops->pwork_60sec)
3229 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003230
3231 /* Force check the TX power emission now. */
3232 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003233}
3234
3235static void b43_periodic_every30sec(struct b43_wldev *dev)
3236{
3237 /* Update device statistics. */
3238 b43_calculate_link_quality(dev);
3239}
3240
3241static void b43_periodic_every15sec(struct b43_wldev *dev)
3242{
3243 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003244 u16 wdr;
3245
3246 if (dev->fw.opensource) {
3247 /* Check if the firmware is still alive.
3248 * It will reset the watchdog counter to 0 in its idle loop. */
3249 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3250 if (unlikely(wdr)) {
3251 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3252 b43_controller_restart(dev, "Firmware watchdog");
3253 return;
3254 } else {
3255 b43_shm_write16(dev, B43_SHM_SCRATCH,
3256 B43_WATCHDOG_REG, 1);
3257 }
3258 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003259
Michael Bueschef1a6282008-08-27 18:53:02 +02003260 if (phy->ops->pwork_15sec)
3261 phy->ops->pwork_15sec(dev);
3262
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003263 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3264 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003265
3266#if B43_DEBUG
3267 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3268 unsigned int i;
3269
3270 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3271 dev->irq_count / 15,
3272 dev->tx_count / 15,
3273 dev->rx_count / 15);
3274 dev->irq_count = 0;
3275 dev->tx_count = 0;
3276 dev->rx_count = 0;
3277 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3278 if (dev->irq_bit_count[i]) {
3279 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3280 dev->irq_bit_count[i] / 15, i, (1 << i));
3281 dev->irq_bit_count[i] = 0;
3282 }
3283 }
3284 }
3285#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003286}
3287
Michael Buesche4d6b792007-09-18 15:39:42 -04003288static void do_periodic_work(struct b43_wldev *dev)
3289{
3290 unsigned int state;
3291
3292 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003293 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003294 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003295 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003296 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003297 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003298}
3299
Michael Buesch05b64b32007-09-28 16:19:03 +02003300/* Periodic work locking policy:
3301 * The whole periodic work handler is protected by
3302 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003303 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003304 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003305static void b43_periodic_work_handler(struct work_struct *work)
3306{
Michael Buesch05b64b32007-09-28 16:19:03 +02003307 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3308 periodic_work.work);
3309 struct b43_wl *wl = dev->wl;
3310 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003311
Michael Buesch05b64b32007-09-28 16:19:03 +02003312 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003313
3314 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3315 goto out;
3316 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3317 goto out_requeue;
3318
Michael Buesch05b64b32007-09-28 16:19:03 +02003319 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003320
Michael Buesche4d6b792007-09-18 15:39:42 -04003321 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003322out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003323 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3324 delay = msecs_to_jiffies(50);
3325 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003326 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003327 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003328out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003329 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003330}
3331
3332static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3333{
3334 struct delayed_work *work = &dev->periodic_work;
3335
3336 dev->periodic_state = 0;
3337 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003338 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003339}
3340
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003341/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003342static int b43_validate_chipaccess(struct b43_wldev *dev)
3343{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003344 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003345
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003346 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3347 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003348
3349 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003350 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3351 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3352 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003353 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3354 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003355 goto error;
3356
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003357 /* Check if unaligned 32bit SHM_SHARED access works properly.
3358 * However, don't bail out on failure, because it's noncritical. */
3359 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3360 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3361 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3362 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3363 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3364 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3365 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3366 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3367 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3368 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3369 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3370 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3371
3372 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3373 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003374
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003375 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003376 /* The 32bit register shadows the two 16bit registers
3377 * with update sideeffects. Validate this. */
3378 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3379 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3380 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3381 goto error;
3382 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3383 goto error;
3384 }
3385 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3386
3387 v = b43_read32(dev, B43_MMIO_MACCTL);
3388 v |= B43_MACCTL_GMODE;
3389 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003390 goto error;
3391
3392 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003393error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003394 b43err(dev->wl, "Failed to validate the chipaccess\n");
3395 return -ENODEV;
3396}
3397
3398static void b43_security_init(struct b43_wldev *dev)
3399{
Michael Buesche4d6b792007-09-18 15:39:42 -04003400 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3401 /* KTP is a word address, but we address SHM bytewise.
3402 * So multiply by two.
3403 */
3404 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003405 /* Number of RCMTA address slots */
3406 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3407 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003408 b43_clear_keys(dev);
3409}
3410
Michael Buesch616de352009-03-29 13:19:31 +02003411#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003412static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003413{
3414 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003415 struct b43_wldev *dev;
3416 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003417
Michael Buescha78b3bb2009-09-11 21:44:05 +02003418 mutex_lock(&wl->mutex);
3419 dev = wl->current_dev;
3420 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3421 *data = b43_read16(dev, B43_MMIO_RNG);
3422 count = sizeof(u16);
3423 }
3424 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003425
Michael Buescha78b3bb2009-09-11 21:44:05 +02003426 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003427}
Michael Buesch616de352009-03-29 13:19:31 +02003428#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003429
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003430static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003431{
Michael Buesch616de352009-03-29 13:19:31 +02003432#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003433 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003434 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003435#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003436}
3437
3438static int b43_rng_init(struct b43_wl *wl)
3439{
Michael Buesch616de352009-03-29 13:19:31 +02003440 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003441
Michael Buesch616de352009-03-29 13:19:31 +02003442#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003443 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3444 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3445 wl->rng.name = wl->rng_name;
3446 wl->rng.data_read = b43_rng_read;
3447 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003448 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003449 err = hwrng_register(&wl->rng);
3450 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003451 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003452 b43err(wl, "Failed to register the random "
3453 "number generator (%d)\n", err);
3454 }
Michael Buesch616de352009-03-29 13:19:31 +02003455#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003456
3457 return err;
3458}
3459
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003460static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003461{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003462 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3463 struct b43_wldev *dev;
3464 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003465 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003466 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003467
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003468 mutex_lock(&wl->mutex);
3469 dev = wl->current_dev;
3470 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3471 mutex_unlock(&wl->mutex);
3472 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003473 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003474
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003475 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3476 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3477 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3478 if (b43_using_pio_transfers(dev))
3479 err = b43_pio_tx(dev, skb);
3480 else
3481 err = b43_dma_tx(dev, skb);
3482 if (err == -ENOSPC) {
3483 wl->tx_queue_stopped[queue_num] = 1;
3484 ieee80211_stop_queue(wl->hw, queue_num);
3485 skb_queue_head(&wl->tx_queue[queue_num], skb);
3486 break;
3487 }
3488 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003489 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003490 err = 0;
3491 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003492
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003493 if (!err)
3494 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003495 }
3496
Michael Buesch990b86f2009-09-12 00:48:03 +02003497#if B43_DEBUG
3498 dev->tx_count++;
3499#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003500 mutex_unlock(&wl->mutex);
3501}
Michael Buesch21a75d72008-04-25 19:29:08 +02003502
Johannes Berg7bb45682011-02-24 14:42:06 +01003503static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003504 struct ieee80211_tx_control *control,
3505 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003506{
3507 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003508
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003509 if (unlikely(skb->len < 2 + 2 + 6)) {
3510 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003511 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003512 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003513 }
3514 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3515
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003516 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3517 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3518 ieee80211_queue_work(wl->hw, &wl->tx_work);
3519 } else {
3520 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3521 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003522}
3523
Michael Buesche6f5b932008-03-05 21:18:49 +01003524static void b43_qos_params_upload(struct b43_wldev *dev,
3525 const struct ieee80211_tx_queue_params *p,
3526 u16 shm_offset)
3527{
3528 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003529 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003530 unsigned int i;
3531
Michael Bueschb0544eb2009-09-06 15:42:45 +02003532 if (!dev->qos_enabled)
3533 return;
3534
Johannes Berg0b576642008-07-15 02:08:24 -07003535 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003536
3537 memset(&params, 0, sizeof(params));
3538
3539 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003540 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3541 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3542 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3543 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003544 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003545 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003546
3547 for (i = 0; i < ARRAY_SIZE(params); i++) {
3548 if (i == B43_QOSPARAM_STATUS) {
3549 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3550 shm_offset + (i * 2));
3551 /* Mark the parameters as updated. */
3552 tmp |= 0x100;
3553 b43_shm_write16(dev, B43_SHM_SHARED,
3554 shm_offset + (i * 2),
3555 tmp);
3556 } else {
3557 b43_shm_write16(dev, B43_SHM_SHARED,
3558 shm_offset + (i * 2),
3559 params[i]);
3560 }
3561 }
3562}
3563
Michael Bueschc40c1122008-09-06 16:21:47 +02003564/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3565static const u16 b43_qos_shm_offsets[] = {
3566 /* [mac80211-queue-nr] = SHM_OFFSET, */
3567 [0] = B43_QOS_VOICE,
3568 [1] = B43_QOS_VIDEO,
3569 [2] = B43_QOS_BESTEFFORT,
3570 [3] = B43_QOS_BACKGROUND,
3571};
3572
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003573/* Update all QOS parameters in hardware. */
3574static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003575{
3576 struct b43_wl *wl = dev->wl;
3577 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003578 unsigned int i;
3579
Michael Bueschb0544eb2009-09-06 15:42:45 +02003580 if (!dev->qos_enabled)
3581 return;
3582
Michael Bueschc40c1122008-09-06 16:21:47 +02003583 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3584 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003585
3586 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003587 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3588 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003589 b43_qos_params_upload(dev, &(params->p),
3590 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003591 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003592 b43_mac_enable(dev);
3593}
3594
3595static void b43_qos_clear(struct b43_wl *wl)
3596{
3597 struct b43_qos_params *params;
3598 unsigned int i;
3599
Michael Bueschc40c1122008-09-06 16:21:47 +02003600 /* Initialize QoS parameters to sane defaults. */
3601
3602 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3603 ARRAY_SIZE(wl->qos_params));
3604
Michael Buesche6f5b932008-03-05 21:18:49 +01003605 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3606 params = &(wl->qos_params[i]);
3607
Michael Bueschc40c1122008-09-06 16:21:47 +02003608 switch (b43_qos_shm_offsets[i]) {
3609 case B43_QOS_VOICE:
3610 params->p.txop = 0;
3611 params->p.aifs = 2;
3612 params->p.cw_min = 0x0001;
3613 params->p.cw_max = 0x0001;
3614 break;
3615 case B43_QOS_VIDEO:
3616 params->p.txop = 0;
3617 params->p.aifs = 2;
3618 params->p.cw_min = 0x0001;
3619 params->p.cw_max = 0x0001;
3620 break;
3621 case B43_QOS_BESTEFFORT:
3622 params->p.txop = 0;
3623 params->p.aifs = 3;
3624 params->p.cw_min = 0x0001;
3625 params->p.cw_max = 0x03FF;
3626 break;
3627 case B43_QOS_BACKGROUND:
3628 params->p.txop = 0;
3629 params->p.aifs = 7;
3630 params->p.cw_min = 0x0001;
3631 params->p.cw_max = 0x03FF;
3632 break;
3633 default:
3634 B43_WARN_ON(1);
3635 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003636 }
3637}
3638
3639/* Initialize the core's QOS capabilities */
3640static void b43_qos_init(struct b43_wldev *dev)
3641{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003642 if (!dev->qos_enabled) {
3643 /* Disable QOS support. */
3644 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3645 b43_write16(dev, B43_MMIO_IFSCTL,
3646 b43_read16(dev, B43_MMIO_IFSCTL)
3647 & ~B43_MMIO_IFSCTL_USE_EDCF);
3648 b43dbg(dev->wl, "QoS disabled\n");
3649 return;
3650 }
3651
Michael Buesche6f5b932008-03-05 21:18:49 +01003652 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003653 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003654
3655 /* Enable QOS support. */
3656 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3657 b43_write16(dev, B43_MMIO_IFSCTL,
3658 b43_read16(dev, B43_MMIO_IFSCTL)
3659 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003660 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003661}
3662
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003663static int b43_op_conf_tx(struct ieee80211_hw *hw,
3664 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003665 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003666{
Michael Buesche6f5b932008-03-05 21:18:49 +01003667 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003668 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003669 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003670 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003671
3672 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3673 /* Queue not available or don't support setting
3674 * params on this queue. Return success to not
3675 * confuse mac80211. */
3676 return 0;
3677 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003678 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3679 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003680
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003681 mutex_lock(&wl->mutex);
3682 dev = wl->current_dev;
3683 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3684 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003685
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003686 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3687 b43_mac_suspend(dev);
3688 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3689 b43_qos_shm_offsets[queue]);
3690 b43_mac_enable(dev);
3691 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003692
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003693out_unlock:
3694 mutex_unlock(&wl->mutex);
3695
3696 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003697}
3698
Michael Buesch40faacc2007-10-28 16:29:32 +01003699static int b43_op_get_stats(struct ieee80211_hw *hw,
3700 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003701{
3702 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003703
Michael Buesch36dbd952009-09-04 22:51:29 +02003704 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003705 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003706 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003707
3708 return 0;
3709}
3710
Eliad Peller37a41b42011-09-21 14:06:11 +03003711static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003712{
3713 struct b43_wl *wl = hw_to_b43_wl(hw);
3714 struct b43_wldev *dev;
3715 u64 tsf;
3716
3717 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003718 dev = wl->current_dev;
3719
3720 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3721 b43_tsf_read(dev, &tsf);
3722 else
3723 tsf = 0;
3724
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003725 mutex_unlock(&wl->mutex);
3726
3727 return tsf;
3728}
3729
Eliad Peller37a41b42011-09-21 14:06:11 +03003730static void b43_op_set_tsf(struct ieee80211_hw *hw,
3731 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003732{
3733 struct b43_wl *wl = hw_to_b43_wl(hw);
3734 struct b43_wldev *dev;
3735
3736 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003737 dev = wl->current_dev;
3738
3739 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3740 b43_tsf_write(dev, tsf);
3741
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003742 mutex_unlock(&wl->mutex);
3743}
3744
John Daiker99da1852009-02-24 02:16:42 -08003745static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003746{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003747 switch (band) {
3748 case IEEE80211_BAND_5GHZ:
3749 return "5";
3750 case IEEE80211_BAND_2GHZ:
3751 return "2.4";
3752 default:
3753 break;
3754 }
3755 B43_WARN_ON(1);
3756 return "";
3757}
3758
3759/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003760static int b43_switch_band(struct b43_wldev *dev,
3761 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003762{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003763 struct b43_phy *phy = &dev->phy;
3764 bool gmode;
3765 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003766
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003767 switch (chan->band) {
3768 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003769 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003770 break;
3771 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003772 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003773 break;
3774 default:
3775 B43_WARN_ON(1);
3776 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003777 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003778
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003779 if (!((gmode && phy->supports_2ghz) ||
3780 (!gmode && phy->supports_5ghz))) {
3781 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003782 band_to_string(chan->band));
3783 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003784 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003785
3786 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003787 /* This device is already running. */
3788 return 0;
3789 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003790
3791 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003792 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003793
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003794 /* Some new devices don't need disabling radio for band switching */
3795 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3796 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003797
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003798 phy->gmode = gmode;
3799 b43_phy_put_into_reset(dev);
3800 switch (dev->dev->bus_type) {
3801#ifdef CONFIG_B43_BCMA
3802 case B43_BUS_BCMA:
3803 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3804 if (gmode)
3805 tmp |= B43_BCMA_IOCTL_GMODE;
3806 else
3807 tmp &= ~B43_BCMA_IOCTL_GMODE;
3808 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3809 break;
3810#endif
3811#ifdef CONFIG_B43_SSB
3812 case B43_BUS_SSB:
3813 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3814 if (gmode)
3815 tmp |= B43_TMSLOW_GMODE;
3816 else
3817 tmp &= ~B43_TMSLOW_GMODE;
3818 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3819 break;
3820#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003821 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003822 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003823
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003824 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003825
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003826 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003827
3828 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003829}
3830
Johannes Berg9124b072008-10-14 19:17:54 +02003831/* Write the short and long frame retry limit values. */
3832static void b43_set_retry_limits(struct b43_wldev *dev,
3833 unsigned int short_retry,
3834 unsigned int long_retry)
3835{
3836 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3837 * the chip-internal counter. */
3838 short_retry = min(short_retry, (unsigned int)0xF);
3839 long_retry = min(long_retry, (unsigned int)0xF);
3840
3841 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3842 short_retry);
3843 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3844 long_retry);
3845}
3846
Johannes Berge8975582008-10-09 12:18:51 +02003847static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003848{
3849 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003850 struct b43_wldev *dev = wl->current_dev;
3851 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003852 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003853 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003854 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003855
Michael Buesche4d6b792007-09-18 15:39:42 -04003856 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003857 b43_mac_suspend(dev);
3858
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003859 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02003860 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003861 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06003862
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003863 /* Switch the band (if necessary). */
3864 err = b43_switch_band(dev, conf->chandef.chan);
3865 if (err)
3866 goto out_mac_enable;
3867
3868 /* Switch to the requested channel.
3869 * The firmware takes care of races with the TX handler.
3870 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003871 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003872 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01003873
Johannes Berg9124b072008-10-14 19:17:54 +02003874 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3875 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
3876 conf->long_frame_max_tx_count);
3877 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
3878 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01003879 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04003880
Johannes Berg0869aea2009-10-28 10:03:35 +01003881 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01003882
Michael Buesche4d6b792007-09-18 15:39:42 -04003883 /* Adjust the desired TX power level. */
3884 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02003885 if (conf->power_level != phy->desired_txpower) {
3886 phy->desired_txpower = conf->power_level;
3887 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3888 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04003889 }
3890 }
3891
3892 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02003893 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003894 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02003895 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02003896 if (phy->ops->set_rx_antenna)
3897 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003898
Larry Fingerfd4973c2009-06-20 12:58:11 -05003899 if (wl->radio_enabled != phy->radio_on) {
3900 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003901 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003902 b43info(dev->wl, "Radio turned on by software\n");
3903 if (!dev->radio_hw_enable) {
3904 b43info(dev->wl, "The hardware RF-kill button "
3905 "still turns the radio physically off. "
3906 "Press the button to turn it on.\n");
3907 }
3908 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02003909 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003910 b43info(dev->wl, "Radio turned off by software\n");
3911 }
3912 }
3913
Michael Bueschd10d0e52008-12-18 22:13:39 +01003914out_mac_enable:
3915 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003916 mutex_unlock(&wl->mutex);
3917
3918 return err;
3919}
3920
Johannes Berg881d9482009-01-21 15:13:48 +01003921static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003922{
3923 struct ieee80211_supported_band *sband =
3924 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
3925 struct ieee80211_rate *rate;
3926 int i;
3927 u16 basic, direct, offset, basic_offset, rateptr;
3928
3929 for (i = 0; i < sband->n_bitrates; i++) {
3930 rate = &sband->bitrates[i];
3931
3932 if (b43_is_cck_rate(rate->hw_value)) {
3933 direct = B43_SHM_SH_CCKDIRECT;
3934 basic = B43_SHM_SH_CCKBASIC;
3935 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3936 offset &= 0xF;
3937 } else {
3938 direct = B43_SHM_SH_OFDMDIRECT;
3939 basic = B43_SHM_SH_OFDMBASIC;
3940 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3941 offset &= 0xF;
3942 }
3943
3944 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
3945
3946 if (b43_is_cck_rate(rate->hw_value)) {
3947 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3948 basic_offset &= 0xF;
3949 } else {
3950 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3951 basic_offset &= 0xF;
3952 }
3953
3954 /*
3955 * Get the pointer that we need to point to
3956 * from the direct map
3957 */
3958 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
3959 direct + 2 * basic_offset);
3960 /* and write it to the basic map */
3961 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
3962 rateptr);
3963 }
3964}
3965
3966static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
3967 struct ieee80211_vif *vif,
3968 struct ieee80211_bss_conf *conf,
3969 u32 changed)
3970{
3971 struct b43_wl *wl = hw_to_b43_wl(hw);
3972 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003973
3974 mutex_lock(&wl->mutex);
3975
3976 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01003977 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003978 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003979
3980 B43_WARN_ON(wl->vif != vif);
3981
3982 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003983 if (conf->bssid)
3984 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
3985 else
3986 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003987 }
3988
Johannes Berg3f0d8432009-05-18 10:53:18 +02003989 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
3990 if (changed & BSS_CHANGED_BEACON &&
3991 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3992 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
3993 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
3994 b43_update_templates(wl);
3995
3996 if (changed & BSS_CHANGED_BSSID)
3997 b43_write_mac_bssid_templates(dev);
3998 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02003999
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004000 b43_mac_suspend(dev);
4001
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004002 /* Update templates for AP/mesh mode. */
4003 if (changed & BSS_CHANGED_BEACON_INT &&
4004 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4005 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06004006 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
4007 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004008 b43_set_beacon_int(dev, conf->beacon_int);
4009
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004010 if (changed & BSS_CHANGED_BASIC_RATES)
4011 b43_update_basic_rates(dev, conf->basic_rates);
4012
4013 if (changed & BSS_CHANGED_ERP_SLOT) {
4014 if (conf->use_short_slot)
4015 b43_short_slot_timing_enable(dev);
4016 else
4017 b43_short_slot_timing_disable(dev);
4018 }
4019
4020 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01004021out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004022 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004023}
4024
Michael Buesch40faacc2007-10-28 16:29:32 +01004025static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004026 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4027 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004028{
4029 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004030 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004031 u8 algorithm;
4032 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004033 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004034 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004035
4036 if (modparam_nohwcrypt)
4037 return -ENOSPC; /* User disabled HW-crypto */
4038
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004039 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4040 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4041 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4042 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4043 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4044 /*
4045 * For now, disable hw crypto for the RSN IBSS group keys. This
4046 * could be optimized in the future, but until that gets
4047 * implemented, use of software crypto for group addressed
4048 * frames is a acceptable to allow RSN IBSS to be used.
4049 */
4050 return -EOPNOTSUPP;
4051 }
4052
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004053 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004054
4055 dev = wl->current_dev;
4056 err = -ENODEV;
4057 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4058 goto out_unlock;
4059
Michael Buesch403a3a12009-06-08 21:04:57 +02004060 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004061 /* We don't have firmware for the crypto engine.
4062 * Must use software-crypto. */
4063 err = -EOPNOTSUPP;
4064 goto out_unlock;
4065 }
4066
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004067 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004068 switch (key->cipher) {
4069 case WLAN_CIPHER_SUITE_WEP40:
4070 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004071 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004072 case WLAN_CIPHER_SUITE_WEP104:
4073 algorithm = B43_SEC_ALGO_WEP104;
4074 break;
4075 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004076 algorithm = B43_SEC_ALGO_TKIP;
4077 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004078 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004079 algorithm = B43_SEC_ALGO_AES;
4080 break;
4081 default:
4082 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004083 goto out_unlock;
4084 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004085 index = (u8) (key->keyidx);
4086 if (index > 3)
4087 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004088
4089 switch (cmd) {
4090 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004091 if (algorithm == B43_SEC_ALGO_TKIP &&
4092 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4093 !modparam_hwtkip)) {
4094 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004095 err = -EOPNOTSUPP;
4096 goto out_unlock;
4097 }
4098
Michael Buesche808e582008-12-19 21:30:52 +01004099 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004100 if (WARN_ON(!sta)) {
4101 err = -EOPNOTSUPP;
4102 goto out_unlock;
4103 }
Michael Buesche808e582008-12-19 21:30:52 +01004104 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004105 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004106 key->key, key->keylen,
4107 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004108 } else {
4109 /* Group key */
4110 err = b43_key_write(dev, index, algorithm,
4111 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004112 }
4113 if (err)
4114 goto out_unlock;
4115
4116 if (algorithm == B43_SEC_ALGO_WEP40 ||
4117 algorithm == B43_SEC_ALGO_WEP104) {
4118 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4119 } else {
4120 b43_hf_write(dev,
4121 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4122 }
4123 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004124 if (algorithm == B43_SEC_ALGO_TKIP)
4125 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004126 break;
4127 case DISABLE_KEY: {
4128 err = b43_key_clear(dev, key->hw_key_idx);
4129 if (err)
4130 goto out_unlock;
4131 break;
4132 }
4133 default:
4134 B43_WARN_ON(1);
4135 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004136
Michael Buesche4d6b792007-09-18 15:39:42 -04004137out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004138 if (!err) {
4139 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004140 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004141 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004142 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004143 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004144 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004145 mutex_unlock(&wl->mutex);
4146
Michael Buesche4d6b792007-09-18 15:39:42 -04004147 return err;
4148}
4149
Michael Buesch40faacc2007-10-28 16:29:32 +01004150static void b43_op_configure_filter(struct ieee80211_hw *hw,
4151 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004152 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004153{
4154 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004155 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004156
Michael Buesch36dbd952009-09-04 22:51:29 +02004157 mutex_lock(&wl->mutex);
4158 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004159 if (!dev) {
4160 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004161 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004162 }
Johannes Berg4150c572007-09-17 01:29:23 -04004163
Johannes Berg4150c572007-09-17 01:29:23 -04004164 *fflags &= FIF_PROMISC_IN_BSS |
4165 FIF_ALLMULTI |
4166 FIF_FCSFAIL |
4167 FIF_PLCPFAIL |
4168 FIF_CONTROL |
4169 FIF_OTHER_BSS |
4170 FIF_BCN_PRBRESP_PROMISC;
4171
4172 changed &= FIF_PROMISC_IN_BSS |
4173 FIF_ALLMULTI |
4174 FIF_FCSFAIL |
4175 FIF_PLCPFAIL |
4176 FIF_CONTROL |
4177 FIF_OTHER_BSS |
4178 FIF_BCN_PRBRESP_PROMISC;
4179
4180 wl->filter_flags = *fflags;
4181
4182 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4183 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004184
4185out_unlock:
4186 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004187}
4188
Michael Buesch36dbd952009-09-04 22:51:29 +02004189/* Locking: wl->mutex
4190 * Returns the current dev. This might be different from the passed in dev,
4191 * because the core might be gone away while we unlocked the mutex. */
4192static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004193{
Larry Finger9a53bf52011-08-27 15:53:42 -05004194 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004195 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004196 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004197 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004198
Larry Finger9a53bf52011-08-27 15:53:42 -05004199 if (!dev)
4200 return NULL;
4201 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004202redo:
4203 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4204 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004205
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004206 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004207 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004208 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004209 cancel_work_sync(&wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04004210 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004211 dev = wl->current_dev;
4212 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4213 /* Whoops, aliens ate up the device while we were unlocked. */
4214 return dev;
4215 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004216
Michael Buesch36dbd952009-09-04 22:51:29 +02004217 /* Disable interrupts on the device. */
4218 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004219 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004220 /* wl->mutex is locked. That is enough. */
4221 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4222 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4223 } else {
4224 spin_lock_irq(&wl->hardirq_lock);
4225 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4226 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4227 spin_unlock_irq(&wl->hardirq_lock);
4228 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004229 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004230 orig_dev = dev;
4231 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004232 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004233 b43_sdio_free_irq(dev);
4234 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004235 synchronize_irq(dev->dev->irq);
4236 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004237 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004238 mutex_lock(&wl->mutex);
4239 dev = wl->current_dev;
4240 if (!dev)
4241 return dev;
4242 if (dev != orig_dev) {
4243 if (b43_status(dev) >= B43_STAT_STARTED)
4244 goto redo;
4245 return dev;
4246 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004247 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4248 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004249
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004250 /* Drain all TX queues. */
4251 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004252 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4253 struct sk_buff *skb;
4254
4255 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4256 ieee80211_free_txskb(wl->hw, skb);
4257 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004258 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004259
Michael Buesche4d6b792007-09-18 15:39:42 -04004260 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004261 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004262 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004263
4264 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004265}
4266
4267/* Locking: wl->mutex */
4268static int b43_wireless_core_start(struct b43_wldev *dev)
4269{
4270 int err;
4271
4272 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4273
4274 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004275 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004276 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4277 if (err) {
4278 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4279 goto out;
4280 }
4281 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004282 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004283 b43_interrupt_thread_handler,
4284 IRQF_SHARED, KBUILD_MODNAME, dev);
4285 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004286 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004287 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004288 goto out;
4289 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004290 }
4291
4292 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004293 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004294 b43_set_status(dev, B43_STAT_STARTED);
4295
4296 /* Start data flow (TX/RX). */
4297 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004298 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004299
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004300 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004301 b43_periodic_tasks_setup(dev);
4302
Michael Buescha78b3bb2009-09-11 21:44:05 +02004303 b43_leds_init(dev);
4304
Michael Buesche4d6b792007-09-18 15:39:42 -04004305 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004306out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004307 return err;
4308}
4309
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004310static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4311{
4312 switch (phy_type) {
4313 case B43_PHYTYPE_A:
4314 return "A";
4315 case B43_PHYTYPE_B:
4316 return "B";
4317 case B43_PHYTYPE_G:
4318 return "G";
4319 case B43_PHYTYPE_N:
4320 return "N";
4321 case B43_PHYTYPE_LP:
4322 return "LP";
4323 case B43_PHYTYPE_SSLPN:
4324 return "SSLPN";
4325 case B43_PHYTYPE_HT:
4326 return "HT";
4327 case B43_PHYTYPE_LCN:
4328 return "LCN";
4329 case B43_PHYTYPE_LCNXN:
4330 return "LCNXN";
4331 case B43_PHYTYPE_LCN40:
4332 return "LCN40";
4333 case B43_PHYTYPE_AC:
4334 return "AC";
4335 }
4336 return "UNKNOWN";
4337}
4338
Michael Buesche4d6b792007-09-18 15:39:42 -04004339/* Get PHY and RADIO versioning numbers */
4340static int b43_phy_versioning(struct b43_wldev *dev)
4341{
4342 struct b43_phy *phy = &dev->phy;
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004343 const u8 core_rev = dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004344 u32 tmp;
4345 u8 analog_type;
4346 u8 phy_type;
4347 u8 phy_rev;
4348 u16 radio_manuf;
4349 u16 radio_ver;
4350 u16 radio_rev;
4351 int unsupported = 0;
4352
4353 /* Get PHY versioning */
4354 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4355 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4356 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4357 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004358
4359 /* LCNXN is continuation of N which run out of revisions */
4360 if (phy_type == B43_PHYTYPE_LCNXN) {
4361 phy_type = B43_PHYTYPE_N;
4362 phy_rev += 16;
4363 }
4364
Michael Buesche4d6b792007-09-18 15:39:42 -04004365 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004366#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004367 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004368 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004369 unsupported = 1;
4370 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004371#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004372#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004373 case B43_PHYTYPE_N:
Rafał Miłecki40c68f22014-07-08 15:11:07 +02004374 if (phy_rev >= 19)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004375 unsupported = 1;
4376 break;
4377#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004378#ifdef CONFIG_B43_PHY_LP
4379 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004380 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004381 unsupported = 1;
4382 break;
4383#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004384#ifdef CONFIG_B43_PHY_HT
4385 case B43_PHYTYPE_HT:
4386 if (phy_rev > 1)
4387 unsupported = 1;
4388 break;
4389#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004390#ifdef CONFIG_B43_PHY_LCN
4391 case B43_PHYTYPE_LCN:
4392 if (phy_rev > 1)
4393 unsupported = 1;
4394 break;
4395#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004396 default:
4397 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004398 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004399 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004400 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4401 analog_type, phy_type, b43_phy_name(dev, phy_type),
4402 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004403 return -EOPNOTSUPP;
4404 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004405 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4406 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004407
4408 /* Get RADIO versioning */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004409 if (core_rev == 40 || core_rev == 42) {
4410 radio_manuf = 0x17F;
4411
4412 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, 0);
4413 radio_rev = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4414
4415 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, 1);
4416 radio_ver = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4417 } else if (core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004418 u16 radio24[3];
4419
4420 for (tmp = 0; tmp < 3; tmp++) {
4421 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, tmp);
4422 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4423 }
4424
4425 /* Broadcom uses "id" for our "ver" and has separated "ver" */
4426 /* radio_ver = (radio24[0] & 0xF0) >> 4; */
4427
4428 radio_manuf = 0x17F;
4429 radio_ver = (radio24[2] << 8) | radio24[1];
4430 radio_rev = (radio24[0] & 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004431 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004432 if (dev->dev->chip_id == 0x4317) {
4433 if (dev->dev->chip_rev == 0)
4434 tmp = 0x3205017F;
4435 else if (dev->dev->chip_rev == 1)
4436 tmp = 0x4205017F;
4437 else
4438 tmp = 0x5205017F;
4439 } else {
4440 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4441 B43_RADIOCTL_ID);
4442 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
4443 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4444 B43_RADIOCTL_ID);
4445 tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH)
4446 << 16;
4447 }
4448 radio_manuf = (tmp & 0x00000FFF);
4449 radio_ver = (tmp & 0x0FFFF000) >> 12;
4450 radio_rev = (tmp & 0xF0000000) >> 28;
Michael Buesche4d6b792007-09-18 15:39:42 -04004451 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004452
Michael Buesch96c755a2008-01-06 00:09:46 +01004453 if (radio_manuf != 0x17F /* Broadcom */)
4454 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004455 switch (phy_type) {
4456 case B43_PHYTYPE_A:
4457 if (radio_ver != 0x2060)
4458 unsupported = 1;
4459 if (radio_rev != 1)
4460 unsupported = 1;
4461 if (radio_manuf != 0x17F)
4462 unsupported = 1;
4463 break;
4464 case B43_PHYTYPE_B:
4465 if ((radio_ver & 0xFFF0) != 0x2050)
4466 unsupported = 1;
4467 break;
4468 case B43_PHYTYPE_G:
4469 if (radio_ver != 0x2050)
4470 unsupported = 1;
4471 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004472 case B43_PHYTYPE_N:
Rafał Miłecki3695b932014-07-08 15:11:10 +02004473 if (radio_ver != 0x2055 && radio_ver != 0x2056 &&
4474 radio_ver != 0x2057)
4475 unsupported = 1;
4476 if (radio_ver == 0x2057 && !(radio_rev == 9))
Michael Buesch96c755a2008-01-06 00:09:46 +01004477 unsupported = 1;
4478 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004479 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004480 if (radio_ver != 0x2062 && radio_ver != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004481 unsupported = 1;
4482 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004483 case B43_PHYTYPE_HT:
4484 if (radio_ver != 0x2059)
4485 unsupported = 1;
4486 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004487 case B43_PHYTYPE_LCN:
4488 if (radio_ver != 0x2064)
4489 unsupported = 1;
4490 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004491 default:
4492 B43_WARN_ON(1);
4493 }
4494 if (unsupported) {
Rafał Miłecki88d825b2014-07-02 19:07:43 +02004495 b43err(dev->wl,
4496 "FOUND UNSUPPORTED RADIO (Manuf 0x%X, ID 0x%X, Revision %u)\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004497 radio_manuf, radio_ver, radio_rev);
4498 return -EOPNOTSUPP;
4499 }
Rafał Miłecki88d825b2014-07-02 19:07:43 +02004500 b43info(dev->wl, "Found Radio: Manuf 0x%X, ID 0x%X, Revision %u\n",
4501 radio_manuf, radio_ver, radio_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004502
4503 phy->radio_manuf = radio_manuf;
4504 phy->radio_ver = radio_ver;
4505 phy->radio_rev = radio_rev;
4506
4507 phy->analog = analog_type;
4508 phy->type = phy_type;
4509 phy->rev = phy_rev;
4510
4511 return 0;
4512}
4513
4514static void setup_struct_phy_for_init(struct b43_wldev *dev,
4515 struct b43_phy *phy)
4516{
Michael Buesche4d6b792007-09-18 15:39:42 -04004517 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004518 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004519 /* PHY TX errors counter. */
4520 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004521
4522#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004523 phy->phy_locked = false;
4524 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004525#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004526}
4527
4528static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4529{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004530 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004531
Michael Buesch6a724d62007-09-20 22:12:58 +02004532 /* Assume the radio is enabled. If it's not enabled, the state will
4533 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004534 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004535
4536 /* Stats */
4537 memset(&dev->stats, 0, sizeof(dev->stats));
4538
4539 setup_struct_phy_for_init(dev, &dev->phy);
4540
4541 /* IRQ related flags */
4542 dev->irq_reason = 0;
4543 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004544 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004545 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004546 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004547
4548 dev->mac_suspended = 1;
4549
4550 /* Noise calculation context */
4551 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4552}
4553
4554static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4555{
Rafał Miłecki05814832011-05-18 02:06:39 +02004556 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004557 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004558
Michael Buesch1855ba72008-04-18 20:51:41 +02004559 if (!modparam_btcoex)
4560 return;
Larry Finger95de2842007-11-09 16:57:18 -06004561 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004562 return;
4563 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4564 return;
4565
4566 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004567 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004568 hf |= B43_HF_BTCOEXALT;
4569 else
4570 hf |= B43_HF_BTCOEX;
4571 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004572}
4573
4574static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004575{
4576 if (!modparam_btcoex)
4577 return;
4578 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004579}
4580
4581static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4582{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004583 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004584 u32 tmp;
4585
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004586#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004587 if (dev->dev->bus_type != B43_BUS_SSB)
4588 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004589#else
4590 return;
4591#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004592
4593 bus = dev->dev->sdev->bus;
4594
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004595 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4596 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004597 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004598 tmp &= ~SSB_IMCFGLO_REQTO;
4599 tmp &= ~SSB_IMCFGLO_SERTO;
4600 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004601 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004602 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004603 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004604}
4605
Michael Bueschd59f7202008-04-03 18:56:19 +02004606static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4607{
4608 u16 pu_delay;
4609
4610 /* The time value is in microseconds. */
4611 if (dev->phy.type == B43_PHYTYPE_A)
4612 pu_delay = 3700;
4613 else
4614 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004615 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004616 pu_delay = 500;
4617 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4618 pu_delay = max(pu_delay, (u16)2400);
4619
4620 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4621}
4622
4623/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4624static void b43_set_pretbtt(struct b43_wldev *dev)
4625{
4626 u16 pretbtt;
4627
4628 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004629 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004630 pretbtt = 2;
4631 } else {
4632 if (dev->phy.type == B43_PHYTYPE_A)
4633 pretbtt = 120;
4634 else
4635 pretbtt = 250;
4636 }
4637 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4638 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4639}
4640
Michael Buesche4d6b792007-09-18 15:39:42 -04004641/* Shutdown a wireless core */
4642/* Locking: wl->mutex */
4643static void b43_wireless_core_exit(struct b43_wldev *dev)
4644{
Michael Buesch36dbd952009-09-04 22:51:29 +02004645 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4646 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004647 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004648
Michael Buesche4d6b792007-09-18 15:39:42 -04004649 b43_set_status(dev, B43_STAT_UNINIT);
4650
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004651 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004652 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4653 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004654
Hauke Mehrtens50023002013-08-24 00:32:34 +02004655 switch (dev->dev->bus_type) {
4656#ifdef CONFIG_B43_BCMA
4657 case B43_BUS_BCMA:
4658 bcma_core_pci_down(dev->dev->bdev->bus);
4659 break;
4660#endif
4661#ifdef CONFIG_B43_SSB
4662 case B43_BUS_SSB:
4663 /* TODO */
4664 break;
4665#endif
4666 }
4667
Michael Buesche4d6b792007-09-18 15:39:42 -04004668 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004669 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004670 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004671 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004672 if (dev->wl->current_beacon) {
4673 dev_kfree_skb_any(dev->wl->current_beacon);
4674 dev->wl->current_beacon = NULL;
4675 }
4676
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004677 b43_device_disable(dev, 0);
4678 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004679}
4680
4681/* Initialize a wireless core */
4682static int b43_wireless_core_init(struct b43_wldev *dev)
4683{
Rafał Miłecki05814832011-05-18 02:06:39 +02004684 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004685 struct b43_phy *phy = &dev->phy;
4686 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004687 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004688
4689 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4690
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004691 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004692 if (err)
4693 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004694 if (!b43_device_is_enabled(dev))
4695 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004696
Michael Bueschfb111372008-09-02 13:00:34 +02004697 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004698 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004699 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004700
4701 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004702 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004703#ifdef CONFIG_B43_BCMA
4704 case B43_BUS_BCMA:
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004705 bcma_core_pci_irq_ctl(&dev->dev->bdev->bus->drv_pci[0],
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004706 dev->dev->bdev, true);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004707 bcma_core_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004708 break;
4709#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004710#ifdef CONFIG_B43_SSB
4711 case B43_BUS_SSB:
4712 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4713 dev->dev->sdev);
4714 break;
4715#endif
4716 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004717
4718 b43_imcfglo_timeouts_workaround(dev);
4719 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004720 if (phy->ops->prepare_hardware) {
4721 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004722 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004723 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004724 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004725 err = b43_chip_init(dev);
4726 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004727 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004728 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004729 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004730 hf = b43_hf_read(dev);
4731 if (phy->type == B43_PHYTYPE_G) {
4732 hf |= B43_HF_SYMW;
4733 if (phy->rev == 1)
4734 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004735 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004736 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004737 }
4738 if (phy->radio_ver == 0x2050) {
4739 if (phy->radio_rev == 6)
4740 hf |= B43_HF_4318TSSI;
4741 if (phy->radio_rev < 6)
4742 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004743 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004744 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4745 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004746#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004747 if (dev->dev->bus_type == B43_BUS_SSB &&
4748 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4749 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004750 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004751#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004752 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004753 b43_hf_write(dev, hf);
4754
Michael Buesch74cfdba2007-10-28 16:19:44 +01004755 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4756 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004757 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4758 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4759
4760 /* Disable sending probe responses from firmware.
4761 * Setting the MaxTime to one usec will always trigger
4762 * a timeout, so we never send any probe resp.
4763 * A timeout of zero is infinite. */
4764 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4765
4766 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004767 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004768
4769 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004770 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004771 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004772 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004773 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004774 /* Maximum Contention Window */
4775 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4776
Rafał Miłecki505fb012011-05-19 15:11:27 +02004777 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004778 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004779 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004780 err = b43_pio_init(dev);
4781 } else if (dev->use_pio) {
4782 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4783 "This should not be needed and will result in lower "
4784 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004785 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004786 err = b43_pio_init(dev);
4787 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004788 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004789 err = b43_dma_init(dev);
4790 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004791 if (err)
4792 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004793 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004794 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004795 b43_bluetooth_coext_enable(dev);
4796
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004797 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004798 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004799 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004800
Michael Buesch5ab95492009-09-10 20:31:46 +02004801 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004802
4803 b43_set_status(dev, B43_STAT_INITIALIZED);
4804
Larry Finger1a8d1222007-12-14 13:59:11 +01004805out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004806 return err;
4807
Michael Bueschef1a6282008-08-27 18:53:02 +02004808err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004809 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004810err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004811 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004812 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4813 return err;
4814}
4815
Michael Buesch40faacc2007-10-28 16:29:32 +01004816static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004817 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004818{
4819 struct b43_wl *wl = hw_to_b43_wl(hw);
4820 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004821 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004822
4823 /* TODO: allow WDS/AP devices to coexist */
4824
Johannes Berg1ed32e42009-12-23 13:15:45 +01004825 if (vif->type != NL80211_IFTYPE_AP &&
4826 vif->type != NL80211_IFTYPE_MESH_POINT &&
4827 vif->type != NL80211_IFTYPE_STATION &&
4828 vif->type != NL80211_IFTYPE_WDS &&
4829 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004830 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004831
4832 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004833 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004834 goto out_mutex_unlock;
4835
Johannes Berg1ed32e42009-12-23 13:15:45 +01004836 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004837
4838 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004839 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01004840 wl->vif = vif;
4841 wl->if_type = vif->type;
4842 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004843
Michael Buesche4d6b792007-09-18 15:39:42 -04004844 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004845 b43_set_pretbtt(dev);
4846 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004847 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004848
4849 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004850 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004851 mutex_unlock(&wl->mutex);
4852
Felix Fietkau2a190322011-08-10 13:50:30 -06004853 if (err == 0)
4854 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
4855
Michael Buesche4d6b792007-09-18 15:39:42 -04004856 return err;
4857}
4858
Michael Buesch40faacc2007-10-28 16:29:32 +01004859static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004860 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004861{
4862 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04004863 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004864
Johannes Berg1ed32e42009-12-23 13:15:45 +01004865 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004866
4867 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004868
4869 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01004870 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01004871 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04004872
Rusty Russell3db1cd52011-12-19 13:56:45 +00004873 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04004874
Johannes Berg4150c572007-09-17 01:29:23 -04004875 b43_adjust_opmode(dev);
4876 memset(wl->mac_addr, 0, ETH_ALEN);
4877 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04004878
4879 mutex_unlock(&wl->mutex);
4880}
4881
Michael Buesch40faacc2007-10-28 16:29:32 +01004882static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004883{
4884 struct b43_wl *wl = hw_to_b43_wl(hw);
4885 struct b43_wldev *dev = wl->current_dev;
4886 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07004887 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004888
Michael Buesch7be1bb62008-01-23 21:10:56 +01004889 /* Kill all old instance specific information to make sure
4890 * the card won't use it in the short timeframe between start
4891 * and mac80211 reconfiguring it. */
4892 memset(wl->bssid, 0, ETH_ALEN);
4893 memset(wl->mac_addr, 0, ETH_ALEN);
4894 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004895 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01004896 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004897 wl->beacon0_uploaded = false;
4898 wl->beacon1_uploaded = false;
4899 wl->beacon_templates_virgin = true;
4900 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01004901
Johannes Berg4150c572007-09-17 01:29:23 -04004902 mutex_lock(&wl->mutex);
4903
4904 if (b43_status(dev) < B43_STAT_INITIALIZED) {
4905 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05004906 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04004907 goto out_mutex_unlock;
4908 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004909 }
4910
Johannes Berg4150c572007-09-17 01:29:23 -04004911 if (b43_status(dev) < B43_STAT_STARTED) {
4912 err = b43_wireless_core_start(dev);
4913 if (err) {
4914 if (did_init)
4915 b43_wireless_core_exit(dev);
4916 goto out_mutex_unlock;
4917 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004918 }
Johannes Berg4150c572007-09-17 01:29:23 -04004919
Johannes Bergf41f3f32009-06-07 12:30:34 -05004920 /* XXX: only do if device doesn't support rfkill irq */
4921 wiphy_rfkill_start_polling(hw->wiphy);
4922
Johannes Berg4150c572007-09-17 01:29:23 -04004923 out_mutex_unlock:
4924 mutex_unlock(&wl->mutex);
4925
Seth Forsheedbdedbd2012-04-25 17:28:00 -05004926 /*
4927 * Configuration may have been overwritten during initialization.
4928 * Reload the configuration, but only if initialization was
4929 * successful. Reloading the configuration after a failed init
4930 * may hang the system.
4931 */
4932 if (!err)
4933 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06004934
Johannes Berg4150c572007-09-17 01:29:23 -04004935 return err;
4936}
4937
Michael Buesch40faacc2007-10-28 16:29:32 +01004938static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004939{
4940 struct b43_wl *wl = hw_to_b43_wl(hw);
4941 struct b43_wldev *dev = wl->current_dev;
4942
Michael Buescha82d9922008-04-04 21:40:06 +02004943 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d1222007-12-14 13:59:11 +01004944
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004945 if (!dev)
4946 goto out;
4947
Johannes Berg4150c572007-09-17 01:29:23 -04004948 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004949 if (b43_status(dev) >= B43_STAT_STARTED) {
4950 dev = b43_wireless_core_stop(dev);
4951 if (!dev)
4952 goto out_unlock;
4953 }
Johannes Berg4150c572007-09-17 01:29:23 -04004954 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004955 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02004956
4957out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004958 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004959out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02004960 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04004961}
4962
Johannes Berg17741cd2008-09-11 00:02:02 +02004963static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
4964 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01004965{
4966 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01004967
Felix Fietkau8f611282009-11-07 18:37:37 +01004968 /* FIXME: add locking */
Johannes Berg9d139c82008-07-09 14:40:37 +02004969 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01004970
4971 return 0;
4972}
4973
Johannes Berg38968d02008-02-25 16:27:50 +01004974static void b43_op_sta_notify(struct ieee80211_hw *hw,
4975 struct ieee80211_vif *vif,
4976 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02004977 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01004978{
4979 struct b43_wl *wl = hw_to_b43_wl(hw);
4980
4981 B43_WARN_ON(!vif || wl->vif != vif);
4982}
4983
Michael Buesch25d3ef52009-02-20 15:39:21 +01004984static void b43_op_sw_scan_start_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 /* Disable CFP update during scan on other channels. */
4993 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
4994 }
4995 mutex_unlock(&wl->mutex);
4996}
4997
4998static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
4999{
5000 struct b43_wl *wl = hw_to_b43_wl(hw);
5001 struct b43_wldev *dev;
5002
5003 mutex_lock(&wl->mutex);
5004 dev = wl->current_dev;
5005 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5006 /* Re-enable CFP update. */
5007 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
5008 }
5009 mutex_unlock(&wl->mutex);
5010}
5011
John W. Linville354b4f02010-04-29 15:56:06 -04005012static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
5013 struct survey_info *survey)
5014{
5015 struct b43_wl *wl = hw_to_b43_wl(hw);
5016 struct b43_wldev *dev = wl->current_dev;
5017 struct ieee80211_conf *conf = &hw->conf;
5018
5019 if (idx != 0)
5020 return -ENOENT;
5021
Karl Beldan675a0b02013-03-25 16:26:57 +01005022 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04005023 survey->filled = SURVEY_INFO_NOISE_DBM;
5024 survey->noise = dev->stats.link_noise;
5025
5026 return 0;
5027}
5028
Michael Buesche4d6b792007-09-18 15:39:42 -04005029static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01005030 .tx = b43_op_tx,
5031 .conf_tx = b43_op_conf_tx,
5032 .add_interface = b43_op_add_interface,
5033 .remove_interface = b43_op_remove_interface,
5034 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005035 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005036 .configure_filter = b43_op_configure_filter,
5037 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005038 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005039 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005040 .get_tsf = b43_op_get_tsf,
5041 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005042 .start = b43_op_start,
5043 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005044 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005045 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005046 .sw_scan_start = b43_op_sw_scan_start_notifier,
5047 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005048 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005049 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005050};
5051
5052/* Hard-reset the chip. Do not call this directly.
5053 * Use b43_controller_restart()
5054 */
5055static void b43_chip_reset(struct work_struct *work)
5056{
5057 struct b43_wldev *dev =
5058 container_of(work, struct b43_wldev, restart_work);
5059 struct b43_wl *wl = dev->wl;
5060 int err = 0;
5061 int prev_status;
5062
5063 mutex_lock(&wl->mutex);
5064
5065 prev_status = b43_status(dev);
5066 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005067 if (prev_status >= B43_STAT_STARTED) {
5068 dev = b43_wireless_core_stop(dev);
5069 if (!dev) {
5070 err = -ENODEV;
5071 goto out;
5072 }
5073 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005074 if (prev_status >= B43_STAT_INITIALIZED)
5075 b43_wireless_core_exit(dev);
5076
5077 /* ...and up again. */
5078 if (prev_status >= B43_STAT_INITIALIZED) {
5079 err = b43_wireless_core_init(dev);
5080 if (err)
5081 goto out;
5082 }
5083 if (prev_status >= B43_STAT_STARTED) {
5084 err = b43_wireless_core_start(dev);
5085 if (err) {
5086 b43_wireless_core_exit(dev);
5087 goto out;
5088 }
5089 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005090out:
5091 if (err)
5092 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005093 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005094
5095 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005096 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005097 return;
5098 }
5099
5100 /* reload configuration */
5101 b43_op_config(wl->hw, ~0);
5102 if (wl->vif)
5103 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5104
5105 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005106}
5107
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005108static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005109 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005110{
5111 struct ieee80211_hw *hw = dev->wl->hw;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005112 struct b43_phy *phy = &dev->phy;
5113 bool limited_2g;
5114
5115 /* We don't support all 2 GHz channels on some devices */
5116 limited_2g = phy->radio_ver == 0x2057 && phy->radio_rev == 9;
Michael Buesche4d6b792007-09-18 15:39:42 -04005117
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005118 if (have_2ghz_phy)
Rafał Miłecki3695b932014-07-08 15:11:10 +02005119 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = limited_2g ?
5120 &b43_band_2ghz_limited : &b43_band_2GHz;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005121 if (dev->phy.type == B43_PHYTYPE_N) {
5122 if (have_5ghz_phy)
5123 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
5124 } else {
5125 if (have_5ghz_phy)
5126 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5127 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005128
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005129 dev->phy.supports_2ghz = have_2ghz_phy;
5130 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005131
5132 return 0;
5133}
5134
5135static void b43_wireless_core_detach(struct b43_wldev *dev)
5136{
5137 /* We release firmware that late to not be required to re-request
5138 * is all the time when we reinit the core. */
5139 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005140 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005141}
5142
Rafał Miłecki075ca602014-05-19 23:18:54 +02005143static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5144 bool *have_5ghz_phy)
5145{
5146 u16 dev_id = 0;
5147
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005148#ifdef CONFIG_B43_BCMA
5149 if (dev->dev->bus_type == B43_BUS_BCMA &&
5150 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5151 dev_id = dev->dev->bdev->bus->host_pci->device;
5152#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005153#ifdef CONFIG_B43_SSB
5154 if (dev->dev->bus_type == B43_BUS_SSB &&
5155 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5156 dev_id = dev->dev->sdev->bus->host_pci->device;
5157#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005158 /* Override with SPROM value if available */
5159 if (dev->dev->bus_sprom->dev_id)
5160 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005161
5162 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5163 switch (dev_id) {
5164 case 0x4324: /* BCM4306 */
5165 case 0x4312: /* BCM4311 */
5166 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005167 case 0x4328: /* BCM4321 */
5168 case 0x432b: /* BCM4322 */
5169 case 0x4350: /* BCM43222 */
5170 case 0x4353: /* BCM43224 */
5171 case 0x0576: /* BCM43224 */
5172 case 0x435f: /* BCM6362 */
5173 case 0x4331: /* BCM4331 */
5174 case 0x4359: /* BCM43228 */
5175 case 0x43a0: /* BCM4360 */
5176 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005177 /* Dual band devices */
5178 *have_2ghz_phy = true;
5179 *have_5ghz_phy = true;
5180 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005181 case 0x4321: /* BCM4306 */
5182 case 0x4313: /* BCM4311 */
5183 case 0x431a: /* BCM4318 */
5184 case 0x432a: /* BCM4321 */
5185 case 0x432d: /* BCM4322 */
5186 case 0x4352: /* BCM43222 */
5187 case 0x4333: /* BCM4331 */
5188 case 0x43a2: /* BCM4360 */
5189 case 0x43b3: /* BCM4352 */
5190 /* 5 GHz only devices */
5191 *have_2ghz_phy = false;
5192 *have_5ghz_phy = true;
5193 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005194 }
5195
5196 /* As a fallback, try to guess using PHY type */
5197 switch (dev->phy.type) {
5198 case B43_PHYTYPE_A:
5199 *have_2ghz_phy = false;
5200 *have_5ghz_phy = true;
5201 return;
5202 case B43_PHYTYPE_G:
5203 case B43_PHYTYPE_N:
5204 case B43_PHYTYPE_LP:
5205 case B43_PHYTYPE_HT:
5206 case B43_PHYTYPE_LCN:
5207 *have_2ghz_phy = true;
5208 *have_5ghz_phy = false;
5209 return;
5210 }
5211
5212 B43_WARN_ON(1);
5213}
5214
Michael Buesche4d6b792007-09-18 15:39:42 -04005215static int b43_wireless_core_attach(struct b43_wldev *dev)
5216{
5217 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005218 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005219 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005220 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005221 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005222
5223 /* Do NOT do any device initialization here.
5224 * Do it in wireless_core_init() instead.
5225 * This function is for gathering basic information about the HW, only.
5226 * Also some structs may be set up here. But most likely you want to have
5227 * that in core_init(), too.
5228 */
5229
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005230 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005231 if (err) {
5232 b43err(wl, "Bus powerup failed\n");
5233 goto out;
5234 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005235
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005236 phy->do_full_init = true;
5237
Rafał Miłecki075ca602014-05-19 23:18:54 +02005238 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005239 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005240#ifdef CONFIG_B43_BCMA
5241 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005242 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5243 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5244 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005245 break;
5246#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005247#ifdef CONFIG_B43_SSB
5248 case B43_BUS_SSB:
5249 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005250 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5251 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5252 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005253 } else
5254 B43_WARN_ON(1);
5255 break;
5256#endif
5257 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005258
Michael Buesch96c755a2008-01-06 00:09:46 +01005259 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005260 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005261
Rafał Miłecki075ca602014-05-19 23:18:54 +02005262 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005263 err = b43_phy_versioning(dev);
5264 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005265 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005266
5267 /* Get real info about supported bands */
5268 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5269
5270 /* We don't support 5 GHz on some PHYs yet */
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005271 if (have_5ghz_phy) {
5272 switch (dev->phy.type) {
5273 case B43_PHYTYPE_A:
5274 case B43_PHYTYPE_G:
5275 case B43_PHYTYPE_N:
5276 case B43_PHYTYPE_LP:
5277 case B43_PHYTYPE_HT:
5278 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
5279 have_5ghz_phy = false;
5280 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005281 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005282
5283 if (!have_2ghz_phy && !have_5ghz_phy) {
5284 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005285 err = -EOPNOTSUPP;
5286 goto err_powerdown;
5287 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005288
Michael Bueschfb111372008-09-02 13:00:34 +02005289 err = b43_phy_allocate(dev);
5290 if (err)
5291 goto err_powerdown;
5292
Michael Buesch96c755a2008-01-06 00:09:46 +01005293 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005294 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005295
5296 err = b43_validate_chipaccess(dev);
5297 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005298 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005299 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005300 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005301 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005302
5303 /* Now set some default "current_dev" */
5304 if (!wl->current_dev)
5305 wl->current_dev = dev;
5306 INIT_WORK(&dev->restart_work, b43_chip_reset);
5307
Michael Bueschcb24f572008-09-03 12:12:20 +02005308 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005309 b43_device_disable(dev, 0);
5310 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005311
5312out:
5313 return err;
5314
Michael Bueschfb111372008-09-02 13:00:34 +02005315err_phy_free:
5316 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005317err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005318 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005319 return err;
5320}
5321
Rafał Miłecki482f0532011-05-18 02:06:36 +02005322static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005323{
5324 struct b43_wldev *wldev;
5325 struct b43_wl *wl;
5326
Michael Buesch3bf0a322008-05-22 16:32:16 +02005327 /* Do not cancel ieee80211-workqueue based work here.
5328 * See comment in b43_remove(). */
5329
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005330 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005331 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005332 b43_debugfs_remove_device(wldev);
5333 b43_wireless_core_detach(wldev);
5334 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005335 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005336 kfree(wldev);
5337}
5338
Rafał Miłecki482f0532011-05-18 02:06:36 +02005339static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005340{
5341 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005342 int err = -ENOMEM;
5343
Michael Buesche4d6b792007-09-18 15:39:42 -04005344 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5345 if (!wldev)
5346 goto out;
5347
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005348 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005349 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005350 wldev->wl = wl;
5351 b43_set_status(wldev, B43_STAT_UNINIT);
5352 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005353 INIT_LIST_HEAD(&wldev->list);
5354
5355 err = b43_wireless_core_attach(wldev);
5356 if (err)
5357 goto err_kfree_wldev;
5358
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005359 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005360 b43_debugfs_add_device(wldev);
5361
5362 out:
5363 return err;
5364
5365 err_kfree_wldev:
5366 kfree(wldev);
5367 return err;
5368}
5369
Michael Buesch9fc38452008-04-19 16:53:00 +02005370#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5371 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5372 (pdev->device == _device) && \
5373 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5374 (pdev->subsystem_device == _subdevice) )
5375
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005376#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005377static void b43_sprom_fixup(struct ssb_bus *bus)
5378{
Michael Buesch1855ba72008-04-18 20:51:41 +02005379 struct pci_dev *pdev;
5380
Michael Buesche4d6b792007-09-18 15:39:42 -04005381 /* boardflags workarounds */
5382 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005383 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005384 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005385 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005386 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005387 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005388 if (bus->bustype == SSB_BUSTYPE_PCI) {
5389 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005390 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005391 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005392 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005393 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005394 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005395 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5396 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005397 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5398 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005399}
5400
Rafał Miłecki482f0532011-05-18 02:06:36 +02005401static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005402{
5403 struct ieee80211_hw *hw = wl->hw;
5404
Rafał Miłecki482f0532011-05-18 02:06:36 +02005405 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005406 ieee80211_free_hw(hw);
5407}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005408#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005409
Rafał Miłeckid1507052011-07-05 23:54:07 +02005410static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005411{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005412 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005413 struct ieee80211_hw *hw;
5414 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005415 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005416 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005417
5418 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5419 if (!hw) {
5420 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005421 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005422 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005423 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005424
5425 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005426 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005427 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005428
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005429 hw->wiphy->interface_modes =
5430 BIT(NL80211_IFTYPE_AP) |
5431 BIT(NL80211_IFTYPE_MESH_POINT) |
5432 BIT(NL80211_IFTYPE_STATION) |
5433 BIT(NL80211_IFTYPE_WDS) |
5434 BIT(NL80211_IFTYPE_ADHOC);
5435
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005436 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5437
Oleksij Rempele64add22012-06-05 20:39:32 +02005438 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005439 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005440 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005441 if (is_valid_ether_addr(sprom->et1mac))
5442 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005443 else
Larry Finger95de2842007-11-09 16:57:18 -06005444 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005445
Michael Buesch403a3a12009-06-08 21:04:57 +02005446 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005447 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005448 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005449 spin_lock_init(&wl->hardirq_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005450 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005451 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005452 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005453
5454 /* Initialize queues and flags. */
5455 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5456 skb_queue_head_init(&wl->tx_queue[queue_num]);
5457 wl->tx_queue_stopped[queue_num] = 0;
5458 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005459
Rafał Miłecki2729df22011-07-18 22:45:58 +02005460 snprintf(chip_name, ARRAY_SIZE(chip_name),
5461 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5462 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5463 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005464 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005465}
5466
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005467#ifdef CONFIG_B43_BCMA
5468static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005469{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005470 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005471 struct b43_wl *wl;
5472 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005473
Rafał Miłecki89604002013-06-26 09:55:54 +02005474 if (!modparam_allhwsupport &&
5475 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5476 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5477 return -ENOTSUPP;
5478 }
5479
Rafał Miłecki397915c2011-07-06 19:03:46 +02005480 dev = b43_bus_dev_bcma_init(core);
5481 if (!dev)
5482 return -ENODEV;
5483
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005484 wl = b43_wireless_init(dev);
5485 if (IS_ERR(wl)) {
5486 err = PTR_ERR(wl);
5487 goto bcma_out;
5488 }
5489
5490 err = b43_one_core_attach(dev, wl);
5491 if (err)
5492 goto bcma_err_wireless_exit;
5493
Larry Finger6b6fa582012-03-08 22:27:46 -06005494 /* setup and start work to load firmware */
5495 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5496 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005497
5498bcma_out:
5499 return err;
5500
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005501bcma_err_wireless_exit:
5502 ieee80211_free_hw(wl->hw);
5503 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005504}
5505
5506static void b43_bcma_remove(struct bcma_device *core)
5507{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005508 struct b43_wldev *wldev = bcma_get_drvdata(core);
5509 struct b43_wl *wl = wldev->wl;
5510
5511 /* We must cancel any work here before unregistering from ieee80211,
5512 * as the ieee80211 unreg will destroy the workqueue. */
5513 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005514 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005515
Oleksij Rempele64add22012-06-05 20:39:32 +02005516 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005517 if (!wldev->fw.ucode.data)
5518 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005519 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005520 b43_leds_stop(wldev);
5521 ieee80211_unregister_hw(wl->hw);
5522 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005523
5524 b43_one_core_detach(wldev->dev);
5525
Larry Finger09164042014-01-12 15:11:37 -06005526 /* Unregister HW RNG driver */
5527 b43_rng_exit(wl);
5528
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005529 b43_leds_unregister(wl);
5530
5531 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005532}
5533
5534static struct bcma_driver b43_bcma_driver = {
5535 .name = KBUILD_MODNAME,
5536 .id_table = b43_bcma_tbl,
5537 .probe = b43_bcma_probe,
5538 .remove = b43_bcma_remove,
5539};
5540#endif
5541
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005542#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005543static
5544int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005545{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005546 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005547 struct b43_wl *wl;
5548 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005549
Rafał Miłecki482f0532011-05-18 02:06:36 +02005550 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005551 if (!dev)
5552 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005553
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005554 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005555 if (wl) {
5556 b43err(NULL, "Dual-core devices are not supported\n");
5557 err = -ENOTSUPP;
5558 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005559 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005560
5561 b43_sprom_fixup(sdev->bus);
5562
5563 wl = b43_wireless_init(dev);
5564 if (IS_ERR(wl)) {
5565 err = PTR_ERR(wl);
5566 goto err_ssb_kfree_dev;
5567 }
5568 ssb_set_devtypedata(sdev, wl);
5569 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5570
Michael Buesche4d6b792007-09-18 15:39:42 -04005571 err = b43_one_core_attach(dev, wl);
5572 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005573 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005574
Larry Finger6b6fa582012-03-08 22:27:46 -06005575 /* setup and start work to load firmware */
5576 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5577 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005578
Michael Buesche4d6b792007-09-18 15:39:42 -04005579 return err;
5580
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005581err_ssb_wireless_exit:
5582 b43_wireless_exit(dev, wl);
5583err_ssb_kfree_dev:
5584 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005585 return err;
5586}
5587
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005588static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005589{
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005590 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5591 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005592 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005593
Michael Buesch3bf0a322008-05-22 16:32:16 +02005594 /* We must cancel any work here before unregistering from ieee80211,
5595 * as the ieee80211 unreg will destroy the workqueue. */
5596 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005597 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005598
Michael Buesche4d6b792007-09-18 15:39:42 -04005599 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005600 if (!wldev->fw.ucode.data)
5601 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005602 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005603 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005604 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005605 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005606
Pavel Roskine61b52d2011-07-22 18:07:13 -04005607 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005608
Larry Finger09164042014-01-12 15:11:37 -06005609 /* Unregister HW RNG driver */
5610 b43_rng_exit(wl);
5611
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005612 b43_leds_unregister(wl);
5613 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005614}
5615
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005616static struct ssb_driver b43_ssb_driver = {
5617 .name = KBUILD_MODNAME,
5618 .id_table = b43_ssb_tbl,
5619 .probe = b43_ssb_probe,
5620 .remove = b43_ssb_remove,
5621};
5622#endif /* CONFIG_B43_SSB */
5623
Michael Buesche4d6b792007-09-18 15:39:42 -04005624/* Perform a hardware reset. This can be called from any context. */
5625void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5626{
5627 /* Must avoid requeueing, if we are in shutdown. */
5628 if (b43_status(dev) < B43_STAT_INITIALIZED)
5629 return;
5630 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005631 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005632}
5633
Michael Buesch26bc7832008-02-09 00:18:35 +01005634static void b43_print_driverinfo(void)
5635{
5636 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005637 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005638
5639#ifdef CONFIG_B43_PCI_AUTOSELECT
5640 feat_pci = "P";
5641#endif
5642#ifdef CONFIG_B43_PCMCIA
5643 feat_pcmcia = "M";
5644#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005645#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005646 feat_nphy = "N";
5647#endif
5648#ifdef CONFIG_B43_LEDS
5649 feat_leds = "L";
5650#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005651#ifdef CONFIG_B43_SDIO
5652 feat_sdio = "S";
5653#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005654 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005655 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005656 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005657 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005658}
5659
Michael Buesche4d6b792007-09-18 15:39:42 -04005660static int __init b43_init(void)
5661{
5662 int err;
5663
5664 b43_debugfs_init();
5665 err = b43_pcmcia_init();
5666 if (err)
5667 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005668 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005669 if (err)
5670 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005671#ifdef CONFIG_B43_BCMA
5672 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005673 if (err)
5674 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005675#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005676#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005677 err = ssb_driver_register(&b43_ssb_driver);
5678 if (err)
5679 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005680#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005681 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005682
5683 return err;
5684
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005685#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005686err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005687#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005688#ifdef CONFIG_B43_BCMA
5689 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005690err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005691#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005692 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005693err_pcmcia_exit:
5694 b43_pcmcia_exit();
5695err_dfs_exit:
5696 b43_debugfs_exit();
5697 return err;
5698}
5699
5700static void __exit b43_exit(void)
5701{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005702#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005703 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005704#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005705#ifdef CONFIG_B43_BCMA
5706 bcma_driver_unregister(&b43_bcma_driver);
5707#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005708 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005709 b43_pcmcia_exit();
5710 b43_debugfs_exit();
5711}
5712
5713module_init(b43_init)
5714module_exit(b43_exit)