blob: 47b6fa5fa5b2578859e3ae341e0a686418248bcf [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 Buesch6b4bec02008-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 Buesch6b4bec02008-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 Buesch6b4bec02008-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 Buesch6b4bec02008-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 Buesch6b4bec02008-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 Buesch6b4bec02008-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 Bueschd4df6f1a2007-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łeckic4a2a0812011-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łeckic4a2a0812011-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łeckic4a2a0812011-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łeckic4a2a0812011-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łeckic4a2a0812011-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
Rafał Miłeckic2cb2c42014-07-17 19:31:05 +02002967/* brcms_b_switch_macfreq */
2968void b43_mac_switch_freq(struct b43_wldev *dev, u8 spurmode)
2969{
2970 u16 chip_id = dev->dev->chip_id;
2971
2972 if (chip_id == BCMA_CHIP_ID_BCM43217 ||
2973 chip_id == BCMA_CHIP_ID_BCM43222 ||
2974 chip_id == BCMA_CHIP_ID_BCM43224 ||
2975 chip_id == BCMA_CHIP_ID_BCM43225 ||
2976 chip_id == BCMA_CHIP_ID_BCM43227 ||
2977 chip_id == BCMA_CHIP_ID_BCM43228) {
2978 switch (spurmode) {
2979 case 2: /* 126 Mhz */
2980 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x2082);
2981 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
2982 break;
2983 case 1: /* 123 Mhz */
2984 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x5341);
2985 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
2986 break;
2987 default: /* 120 Mhz */
2988 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x8889);
2989 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0x8);
2990 break;
2991 }
2992 } else if (dev->phy.type == B43_PHYTYPE_LCN) {
2993 switch (spurmode) {
2994 case 1: /* 82 Mhz */
2995 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0x7CE0);
2996 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
2997 break;
2998 default: /* 80 Mhz */
2999 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_LOW, 0xCCCD);
3000 b43_write16(dev, B43_MMIO_TSF_CLK_FRAC_HIGH, 0xC);
3001 break;
3002 }
3003 }
3004}
3005
Michael Buesche4d6b792007-09-18 15:39:42 -04003006static void b43_adjust_opmode(struct b43_wldev *dev)
3007{
3008 struct b43_wl *wl = dev->wl;
3009 u32 ctl;
3010 u16 cfp_pretbtt;
3011
3012 ctl = b43_read32(dev, B43_MMIO_MACCTL);
3013 /* Reset status to STA infrastructure mode. */
3014 ctl &= ~B43_MACCTL_AP;
3015 ctl &= ~B43_MACCTL_KEEP_CTL;
3016 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
3017 ctl &= ~B43_MACCTL_KEEP_BAD;
3018 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003019 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04003020 ctl |= B43_MACCTL_INFRA;
3021
Johannes Berg05c914f2008-09-11 00:01:58 +02003022 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3023 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04003024 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02003025 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04003026 ctl &= ~B43_MACCTL_INFRA;
3027
3028 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04003029 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04003030 if (wl->filter_flags & FIF_FCSFAIL)
3031 ctl |= B43_MACCTL_KEEP_BAD;
3032 if (wl->filter_flags & FIF_PLCPFAIL)
3033 ctl |= B43_MACCTL_KEEP_BADPLCP;
3034 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04003035 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04003036 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
3037 ctl |= B43_MACCTL_BEACPROMISC;
3038
Michael Buesche4d6b792007-09-18 15:39:42 -04003039 /* Workaround: On old hardware the HW-MAC-address-filter
3040 * doesn't work properly, so always run promisc in filter
3041 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003042 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04003043 ctl |= B43_MACCTL_PROMISC;
3044
3045 b43_write32(dev, B43_MMIO_MACCTL, ctl);
3046
3047 cfp_pretbtt = 2;
3048 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02003049 if (dev->dev->chip_id == 0x4306 &&
3050 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04003051 cfp_pretbtt = 100;
3052 else
3053 cfp_pretbtt = 50;
3054 }
3055 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02003056
3057 /* FIXME: We don't currently implement the PMQ mechanism,
3058 * so always disable it. If we want to implement PMQ,
3059 * we need to enable it here (clear DISCPMQ) in AP mode.
3060 */
Rafał Miłecki50566352012-01-02 19:31:21 +01003061 if (0 /* ctl & B43_MACCTL_AP */)
3062 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3063 else
3064 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003065}
3066
3067static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3068{
3069 u16 offset;
3070
3071 if (is_ofdm) {
3072 offset = 0x480;
3073 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3074 } else {
3075 offset = 0x4C0;
3076 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3077 }
3078 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3079 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3080}
3081
3082static void b43_rate_memory_init(struct b43_wldev *dev)
3083{
3084 switch (dev->phy.type) {
3085 case B43_PHYTYPE_A:
3086 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003087 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003088 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003089 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003090 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003091 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
3092 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3093 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3094 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3095 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3096 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3097 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3098 if (dev->phy.type == B43_PHYTYPE_A)
3099 break;
3100 /* fallthrough */
3101 case B43_PHYTYPE_B:
3102 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3103 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3104 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3105 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3106 break;
3107 default:
3108 B43_WARN_ON(1);
3109 }
3110}
3111
Michael Buesch5042c502008-04-05 15:05:00 +02003112/* Set the default values for the PHY TX Control Words. */
3113static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3114{
3115 u16 ctl = 0;
3116
3117 ctl |= B43_TXH_PHY_ENC_CCK;
3118 ctl |= B43_TXH_PHY_ANT01AUTO;
3119 ctl |= B43_TXH_PHY_TXPWR;
3120
3121 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3122 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3123 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3124}
3125
Michael Buesche4d6b792007-09-18 15:39:42 -04003126/* Set the TX-Antenna for management frames sent by firmware. */
3127static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3128{
Michael Buesch5042c502008-04-05 15:05:00 +02003129 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003130 u16 tmp;
3131
Michael Buesch5042c502008-04-05 15:05:00 +02003132 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003133
Michael Buesche4d6b792007-09-18 15:39:42 -04003134 /* For ACK/CTS */
3135 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003136 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003137 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3138 /* For Probe Resposes */
3139 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003140 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003141 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3142}
3143
3144/* This is the opposite of b43_chip_init() */
3145static void b43_chip_exit(struct b43_wldev *dev)
3146{
Michael Bueschfb111372008-09-02 13:00:34 +02003147 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003148 b43_gpio_cleanup(dev);
3149 /* firmware is released later */
3150}
3151
3152/* Initialize the chip
3153 * http://bcm-specs.sipsolutions.net/ChipInit
3154 */
3155static int b43_chip_init(struct b43_wldev *dev)
3156{
3157 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003158 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003159 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003160 u16 value16;
3161
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003162 /* Initialize the MAC control */
3163 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3164 if (dev->phy.gmode)
3165 macctl |= B43_MACCTL_GMODE;
3166 macctl |= B43_MACCTL_INFRA;
3167 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003168
Michael Buesche4d6b792007-09-18 15:39:42 -04003169 err = b43_upload_microcode(dev);
3170 if (err)
3171 goto out; /* firmware is released later */
3172
3173 err = b43_gpio_init(dev);
3174 if (err)
3175 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003176
Michael Buesche4d6b792007-09-18 15:39:42 -04003177 err = b43_upload_initvals(dev);
3178 if (err)
Larry Finger1a8d12272007-12-14 13:59:11 +01003179 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003180
Rafał Miłecki0f684232014-05-17 23:24:53 +02003181 err = b43_upload_initvals_band(dev);
3182 if (err)
3183 goto err_gpio_clean;
3184
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003185 /* Turn the Analog on and initialize the PHY. */
3186 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003187 err = b43_phy_init(dev);
3188 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003189 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003190
Michael Bueschef1a6282008-08-27 18:53:02 +02003191 /* Disable Interference Mitigation. */
3192 if (phy->ops->interf_mitigation)
3193 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003194
Michael Bueschef1a6282008-08-27 18:53:02 +02003195 /* Select the antennae */
3196 if (phy->ops->set_rx_antenna)
3197 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003198 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3199
3200 if (phy->type == B43_PHYTYPE_B) {
3201 value16 = b43_read16(dev, 0x005E);
3202 value16 |= 0x0004;
3203 b43_write16(dev, 0x005E, value16);
3204 }
3205 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003206 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003207 b43_write32(dev, 0x010C, 0x01000000);
3208
Rafał Miłecki50566352012-01-02 19:31:21 +01003209 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3210 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003211
Michael Buesche4d6b792007-09-18 15:39:42 -04003212 /* Probe Response Timeout value */
3213 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003214 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003215
3216 /* Initially set the wireless operation mode. */
3217 b43_adjust_opmode(dev);
3218
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003219 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003220 b43_write16(dev, 0x060E, 0x0000);
3221 b43_write16(dev, 0x0610, 0x8000);
3222 b43_write16(dev, 0x0604, 0x0000);
3223 b43_write16(dev, 0x0606, 0x0200);
3224 } else {
3225 b43_write32(dev, 0x0188, 0x80000000);
3226 b43_write32(dev, 0x018C, 0x02000000);
3227 }
3228 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003229 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003230 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3231 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3232 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3233 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3234 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3235
Rafał Miłecki858a1652011-05-10 16:05:33 +02003236 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003237
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003238 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003239#ifdef CONFIG_B43_BCMA
3240 case B43_BUS_BCMA:
3241 /* FIXME: 0xE74 is quite common, but should be read from CC */
3242 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3243 break;
3244#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003245#ifdef CONFIG_B43_SSB
3246 case B43_BUS_SSB:
3247 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3248 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3249 break;
3250#endif
3251 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003252
3253 err = 0;
3254 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003255out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003256 return err;
3257
Larry Finger1a8d12272007-12-14 13:59:11 +01003258err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003259 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003260 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003261}
3262
Michael Buesche4d6b792007-09-18 15:39:42 -04003263static void b43_periodic_every60sec(struct b43_wldev *dev)
3264{
Michael Bueschef1a6282008-08-27 18:53:02 +02003265 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003266
Michael Bueschef1a6282008-08-27 18:53:02 +02003267 if (ops->pwork_60sec)
3268 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003269
3270 /* Force check the TX power emission now. */
3271 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003272}
3273
3274static void b43_periodic_every30sec(struct b43_wldev *dev)
3275{
3276 /* Update device statistics. */
3277 b43_calculate_link_quality(dev);
3278}
3279
3280static void b43_periodic_every15sec(struct b43_wldev *dev)
3281{
3282 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003283 u16 wdr;
3284
3285 if (dev->fw.opensource) {
3286 /* Check if the firmware is still alive.
3287 * It will reset the watchdog counter to 0 in its idle loop. */
3288 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3289 if (unlikely(wdr)) {
3290 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3291 b43_controller_restart(dev, "Firmware watchdog");
3292 return;
3293 } else {
3294 b43_shm_write16(dev, B43_SHM_SCRATCH,
3295 B43_WATCHDOG_REG, 1);
3296 }
3297 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003298
Michael Bueschef1a6282008-08-27 18:53:02 +02003299 if (phy->ops->pwork_15sec)
3300 phy->ops->pwork_15sec(dev);
3301
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003302 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3303 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003304
3305#if B43_DEBUG
3306 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3307 unsigned int i;
3308
3309 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3310 dev->irq_count / 15,
3311 dev->tx_count / 15,
3312 dev->rx_count / 15);
3313 dev->irq_count = 0;
3314 dev->tx_count = 0;
3315 dev->rx_count = 0;
3316 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3317 if (dev->irq_bit_count[i]) {
3318 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3319 dev->irq_bit_count[i] / 15, i, (1 << i));
3320 dev->irq_bit_count[i] = 0;
3321 }
3322 }
3323 }
3324#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003325}
3326
Michael Buesche4d6b792007-09-18 15:39:42 -04003327static void do_periodic_work(struct b43_wldev *dev)
3328{
3329 unsigned int state;
3330
3331 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003332 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003333 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003334 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003335 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003336 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003337}
3338
Michael Buesch05b64b32007-09-28 16:19:03 +02003339/* Periodic work locking policy:
3340 * The whole periodic work handler is protected by
3341 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003342 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003343 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003344static void b43_periodic_work_handler(struct work_struct *work)
3345{
Michael Buesch05b64b32007-09-28 16:19:03 +02003346 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3347 periodic_work.work);
3348 struct b43_wl *wl = dev->wl;
3349 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003350
Michael Buesch05b64b32007-09-28 16:19:03 +02003351 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003352
3353 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3354 goto out;
3355 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3356 goto out_requeue;
3357
Michael Buesch05b64b32007-09-28 16:19:03 +02003358 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003359
Michael Buesche4d6b792007-09-18 15:39:42 -04003360 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003361out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003362 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3363 delay = msecs_to_jiffies(50);
3364 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003365 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003366 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003367out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003368 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003369}
3370
3371static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3372{
3373 struct delayed_work *work = &dev->periodic_work;
3374
3375 dev->periodic_state = 0;
3376 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003377 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003378}
3379
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003380/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003381static int b43_validate_chipaccess(struct b43_wldev *dev)
3382{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003383 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003384
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003385 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3386 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003387
3388 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003389 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3390 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3391 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003392 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3393 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003394 goto error;
3395
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003396 /* Check if unaligned 32bit SHM_SHARED access works properly.
3397 * However, don't bail out on failure, because it's noncritical. */
3398 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3399 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3400 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3401 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3402 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3403 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3404 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3405 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3406 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3407 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3408 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3409 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3410
3411 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3412 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003413
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003414 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003415 /* The 32bit register shadows the two 16bit registers
3416 * with update sideeffects. Validate this. */
3417 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3418 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3419 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3420 goto error;
3421 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3422 goto error;
3423 }
3424 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3425
3426 v = b43_read32(dev, B43_MMIO_MACCTL);
3427 v |= B43_MACCTL_GMODE;
3428 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003429 goto error;
3430
3431 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003432error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003433 b43err(dev->wl, "Failed to validate the chipaccess\n");
3434 return -ENODEV;
3435}
3436
3437static void b43_security_init(struct b43_wldev *dev)
3438{
Michael Buesche4d6b792007-09-18 15:39:42 -04003439 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3440 /* KTP is a word address, but we address SHM bytewise.
3441 * So multiply by two.
3442 */
3443 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003444 /* Number of RCMTA address slots */
3445 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3446 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003447 b43_clear_keys(dev);
3448}
3449
Michael Buesch616de352009-03-29 13:19:31 +02003450#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003451static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003452{
3453 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003454 struct b43_wldev *dev;
3455 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003456
Michael Buescha78b3bb2009-09-11 21:44:05 +02003457 mutex_lock(&wl->mutex);
3458 dev = wl->current_dev;
3459 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3460 *data = b43_read16(dev, B43_MMIO_RNG);
3461 count = sizeof(u16);
3462 }
3463 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003464
Michael Buescha78b3bb2009-09-11 21:44:05 +02003465 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003466}
Michael Buesch616de352009-03-29 13:19:31 +02003467#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003468
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003469static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003470{
Michael Buesch616de352009-03-29 13:19:31 +02003471#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003472 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003473 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003474#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003475}
3476
3477static int b43_rng_init(struct b43_wl *wl)
3478{
Michael Buesch616de352009-03-29 13:19:31 +02003479 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003480
Michael Buesch616de352009-03-29 13:19:31 +02003481#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003482 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3483 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3484 wl->rng.name = wl->rng_name;
3485 wl->rng.data_read = b43_rng_read;
3486 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003487 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003488 err = hwrng_register(&wl->rng);
3489 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003490 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003491 b43err(wl, "Failed to register the random "
3492 "number generator (%d)\n", err);
3493 }
Michael Buesch616de352009-03-29 13:19:31 +02003494#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003495
3496 return err;
3497}
3498
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003499static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003500{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003501 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3502 struct b43_wldev *dev;
3503 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003504 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003505 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003506
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003507 mutex_lock(&wl->mutex);
3508 dev = wl->current_dev;
3509 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3510 mutex_unlock(&wl->mutex);
3511 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003512 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003513
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003514 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3515 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3516 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3517 if (b43_using_pio_transfers(dev))
3518 err = b43_pio_tx(dev, skb);
3519 else
3520 err = b43_dma_tx(dev, skb);
3521 if (err == -ENOSPC) {
3522 wl->tx_queue_stopped[queue_num] = 1;
3523 ieee80211_stop_queue(wl->hw, queue_num);
3524 skb_queue_head(&wl->tx_queue[queue_num], skb);
3525 break;
3526 }
3527 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003528 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003529 err = 0;
3530 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003531
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003532 if (!err)
3533 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003534 }
3535
Michael Buesch990b86f2009-09-12 00:48:03 +02003536#if B43_DEBUG
3537 dev->tx_count++;
3538#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003539 mutex_unlock(&wl->mutex);
3540}
Michael Buesch21a75d72008-04-25 19:29:08 +02003541
Johannes Berg7bb45682011-02-24 14:42:06 +01003542static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003543 struct ieee80211_tx_control *control,
3544 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003545{
3546 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003547
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003548 if (unlikely(skb->len < 2 + 2 + 6)) {
3549 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003550 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003551 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003552 }
3553 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3554
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003555 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3556 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3557 ieee80211_queue_work(wl->hw, &wl->tx_work);
3558 } else {
3559 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3560 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003561}
3562
Michael Buesche6f5b932008-03-05 21:18:49 +01003563static void b43_qos_params_upload(struct b43_wldev *dev,
3564 const struct ieee80211_tx_queue_params *p,
3565 u16 shm_offset)
3566{
3567 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003568 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003569 unsigned int i;
3570
Michael Bueschb0544eb2009-09-06 15:42:45 +02003571 if (!dev->qos_enabled)
3572 return;
3573
Johannes Berg0b576642008-07-15 02:08:24 -07003574 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003575
3576 memset(&params, 0, sizeof(params));
3577
3578 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003579 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3580 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3581 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3582 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003583 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003584 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003585
3586 for (i = 0; i < ARRAY_SIZE(params); i++) {
3587 if (i == B43_QOSPARAM_STATUS) {
3588 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3589 shm_offset + (i * 2));
3590 /* Mark the parameters as updated. */
3591 tmp |= 0x100;
3592 b43_shm_write16(dev, B43_SHM_SHARED,
3593 shm_offset + (i * 2),
3594 tmp);
3595 } else {
3596 b43_shm_write16(dev, B43_SHM_SHARED,
3597 shm_offset + (i * 2),
3598 params[i]);
3599 }
3600 }
3601}
3602
Michael Bueschc40c1122008-09-06 16:21:47 +02003603/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3604static const u16 b43_qos_shm_offsets[] = {
3605 /* [mac80211-queue-nr] = SHM_OFFSET, */
3606 [0] = B43_QOS_VOICE,
3607 [1] = B43_QOS_VIDEO,
3608 [2] = B43_QOS_BESTEFFORT,
3609 [3] = B43_QOS_BACKGROUND,
3610};
3611
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003612/* Update all QOS parameters in hardware. */
3613static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003614{
3615 struct b43_wl *wl = dev->wl;
3616 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003617 unsigned int i;
3618
Michael Bueschb0544eb2009-09-06 15:42:45 +02003619 if (!dev->qos_enabled)
3620 return;
3621
Michael Bueschc40c1122008-09-06 16:21:47 +02003622 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3623 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003624
3625 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003626 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3627 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003628 b43_qos_params_upload(dev, &(params->p),
3629 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003630 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003631 b43_mac_enable(dev);
3632}
3633
3634static void b43_qos_clear(struct b43_wl *wl)
3635{
3636 struct b43_qos_params *params;
3637 unsigned int i;
3638
Michael Bueschc40c1122008-09-06 16:21:47 +02003639 /* Initialize QoS parameters to sane defaults. */
3640
3641 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3642 ARRAY_SIZE(wl->qos_params));
3643
Michael Buesche6f5b932008-03-05 21:18:49 +01003644 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3645 params = &(wl->qos_params[i]);
3646
Michael Bueschc40c1122008-09-06 16:21:47 +02003647 switch (b43_qos_shm_offsets[i]) {
3648 case B43_QOS_VOICE:
3649 params->p.txop = 0;
3650 params->p.aifs = 2;
3651 params->p.cw_min = 0x0001;
3652 params->p.cw_max = 0x0001;
3653 break;
3654 case B43_QOS_VIDEO:
3655 params->p.txop = 0;
3656 params->p.aifs = 2;
3657 params->p.cw_min = 0x0001;
3658 params->p.cw_max = 0x0001;
3659 break;
3660 case B43_QOS_BESTEFFORT:
3661 params->p.txop = 0;
3662 params->p.aifs = 3;
3663 params->p.cw_min = 0x0001;
3664 params->p.cw_max = 0x03FF;
3665 break;
3666 case B43_QOS_BACKGROUND:
3667 params->p.txop = 0;
3668 params->p.aifs = 7;
3669 params->p.cw_min = 0x0001;
3670 params->p.cw_max = 0x03FF;
3671 break;
3672 default:
3673 B43_WARN_ON(1);
3674 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003675 }
3676}
3677
3678/* Initialize the core's QOS capabilities */
3679static void b43_qos_init(struct b43_wldev *dev)
3680{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003681 if (!dev->qos_enabled) {
3682 /* Disable QOS support. */
3683 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3684 b43_write16(dev, B43_MMIO_IFSCTL,
3685 b43_read16(dev, B43_MMIO_IFSCTL)
3686 & ~B43_MMIO_IFSCTL_USE_EDCF);
3687 b43dbg(dev->wl, "QoS disabled\n");
3688 return;
3689 }
3690
Michael Buesche6f5b932008-03-05 21:18:49 +01003691 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003692 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003693
3694 /* Enable QOS support. */
3695 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3696 b43_write16(dev, B43_MMIO_IFSCTL,
3697 b43_read16(dev, B43_MMIO_IFSCTL)
3698 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003699 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003700}
3701
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003702static int b43_op_conf_tx(struct ieee80211_hw *hw,
3703 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003704 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003705{
Michael Buesche6f5b932008-03-05 21:18:49 +01003706 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003707 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003708 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003709 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003710
3711 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3712 /* Queue not available or don't support setting
3713 * params on this queue. Return success to not
3714 * confuse mac80211. */
3715 return 0;
3716 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003717 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3718 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003719
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003720 mutex_lock(&wl->mutex);
3721 dev = wl->current_dev;
3722 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3723 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003724
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003725 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3726 b43_mac_suspend(dev);
3727 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3728 b43_qos_shm_offsets[queue]);
3729 b43_mac_enable(dev);
3730 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003731
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003732out_unlock:
3733 mutex_unlock(&wl->mutex);
3734
3735 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003736}
3737
Michael Buesch40faacc2007-10-28 16:29:32 +01003738static int b43_op_get_stats(struct ieee80211_hw *hw,
3739 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003740{
3741 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003742
Michael Buesch36dbd952009-09-04 22:51:29 +02003743 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003744 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003745 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003746
3747 return 0;
3748}
3749
Eliad Peller37a41b42011-09-21 14:06:11 +03003750static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003751{
3752 struct b43_wl *wl = hw_to_b43_wl(hw);
3753 struct b43_wldev *dev;
3754 u64 tsf;
3755
3756 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003757 dev = wl->current_dev;
3758
3759 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3760 b43_tsf_read(dev, &tsf);
3761 else
3762 tsf = 0;
3763
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003764 mutex_unlock(&wl->mutex);
3765
3766 return tsf;
3767}
3768
Eliad Peller37a41b42011-09-21 14:06:11 +03003769static void b43_op_set_tsf(struct ieee80211_hw *hw,
3770 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003771{
3772 struct b43_wl *wl = hw_to_b43_wl(hw);
3773 struct b43_wldev *dev;
3774
3775 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003776 dev = wl->current_dev;
3777
3778 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3779 b43_tsf_write(dev, tsf);
3780
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003781 mutex_unlock(&wl->mutex);
3782}
3783
John Daiker99da1852009-02-24 02:16:42 -08003784static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003785{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003786 switch (band) {
3787 case IEEE80211_BAND_5GHZ:
3788 return "5";
3789 case IEEE80211_BAND_2GHZ:
3790 return "2.4";
3791 default:
3792 break;
3793 }
3794 B43_WARN_ON(1);
3795 return "";
3796}
3797
3798/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003799static int b43_switch_band(struct b43_wldev *dev,
3800 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003801{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003802 struct b43_phy *phy = &dev->phy;
3803 bool gmode;
3804 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003805
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003806 switch (chan->band) {
3807 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003808 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003809 break;
3810 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003811 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003812 break;
3813 default:
3814 B43_WARN_ON(1);
3815 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003816 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003817
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003818 if (!((gmode && phy->supports_2ghz) ||
3819 (!gmode && phy->supports_5ghz))) {
3820 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003821 band_to_string(chan->band));
3822 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003823 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003824
3825 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003826 /* This device is already running. */
3827 return 0;
3828 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003829
3830 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003831 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003832
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003833 /* Some new devices don't need disabling radio for band switching */
3834 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3835 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003836
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003837 phy->gmode = gmode;
3838 b43_phy_put_into_reset(dev);
3839 switch (dev->dev->bus_type) {
3840#ifdef CONFIG_B43_BCMA
3841 case B43_BUS_BCMA:
3842 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3843 if (gmode)
3844 tmp |= B43_BCMA_IOCTL_GMODE;
3845 else
3846 tmp &= ~B43_BCMA_IOCTL_GMODE;
3847 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3848 break;
3849#endif
3850#ifdef CONFIG_B43_SSB
3851 case B43_BUS_SSB:
3852 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3853 if (gmode)
3854 tmp |= B43_TMSLOW_GMODE;
3855 else
3856 tmp &= ~B43_TMSLOW_GMODE;
3857 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3858 break;
3859#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003860 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003861 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003862
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003863 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003864
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003865 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003866
3867 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003868}
3869
Johannes Berg9124b072008-10-14 19:17:54 +02003870/* Write the short and long frame retry limit values. */
3871static void b43_set_retry_limits(struct b43_wldev *dev,
3872 unsigned int short_retry,
3873 unsigned int long_retry)
3874{
3875 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3876 * the chip-internal counter. */
3877 short_retry = min(short_retry, (unsigned int)0xF);
3878 long_retry = min(long_retry, (unsigned int)0xF);
3879
3880 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3881 short_retry);
3882 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3883 long_retry);
3884}
3885
Johannes Berge8975582008-10-09 12:18:51 +02003886static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003887{
3888 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003889 struct b43_wldev *dev = wl->current_dev;
3890 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003891 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003892 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003893 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003894
Michael Buesche4d6b792007-09-18 15:39:42 -04003895 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003896 b43_mac_suspend(dev);
3897
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003898 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02003899 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003900 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06003901
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003902 /* Switch the band (if necessary). */
3903 err = b43_switch_band(dev, conf->chandef.chan);
3904 if (err)
3905 goto out_mac_enable;
3906
3907 /* Switch to the requested channel.
3908 * The firmware takes care of races with the TX handler.
3909 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003910 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003911 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01003912
Johannes Berg9124b072008-10-14 19:17:54 +02003913 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3914 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
3915 conf->long_frame_max_tx_count);
3916 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
3917 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01003918 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04003919
Johannes Berg0869aea02009-10-28 10:03:35 +01003920 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01003921
Michael Buesche4d6b792007-09-18 15:39:42 -04003922 /* Adjust the desired TX power level. */
3923 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02003924 if (conf->power_level != phy->desired_txpower) {
3925 phy->desired_txpower = conf->power_level;
3926 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3927 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04003928 }
3929 }
3930
3931 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02003932 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003933 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02003934 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02003935 if (phy->ops->set_rx_antenna)
3936 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003937
Larry Fingerfd4973c2009-06-20 12:58:11 -05003938 if (wl->radio_enabled != phy->radio_on) {
3939 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003940 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003941 b43info(dev->wl, "Radio turned on by software\n");
3942 if (!dev->radio_hw_enable) {
3943 b43info(dev->wl, "The hardware RF-kill button "
3944 "still turns the radio physically off. "
3945 "Press the button to turn it on.\n");
3946 }
3947 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02003948 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003949 b43info(dev->wl, "Radio turned off by software\n");
3950 }
3951 }
3952
Michael Bueschd10d0e52008-12-18 22:13:39 +01003953out_mac_enable:
3954 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003955 mutex_unlock(&wl->mutex);
3956
3957 return err;
3958}
3959
Johannes Berg881d9482009-01-21 15:13:48 +01003960static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003961{
3962 struct ieee80211_supported_band *sband =
3963 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
3964 struct ieee80211_rate *rate;
3965 int i;
3966 u16 basic, direct, offset, basic_offset, rateptr;
3967
3968 for (i = 0; i < sband->n_bitrates; i++) {
3969 rate = &sband->bitrates[i];
3970
3971 if (b43_is_cck_rate(rate->hw_value)) {
3972 direct = B43_SHM_SH_CCKDIRECT;
3973 basic = B43_SHM_SH_CCKBASIC;
3974 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3975 offset &= 0xF;
3976 } else {
3977 direct = B43_SHM_SH_OFDMDIRECT;
3978 basic = B43_SHM_SH_OFDMBASIC;
3979 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3980 offset &= 0xF;
3981 }
3982
3983 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
3984
3985 if (b43_is_cck_rate(rate->hw_value)) {
3986 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3987 basic_offset &= 0xF;
3988 } else {
3989 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3990 basic_offset &= 0xF;
3991 }
3992
3993 /*
3994 * Get the pointer that we need to point to
3995 * from the direct map
3996 */
3997 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
3998 direct + 2 * basic_offset);
3999 /* and write it to the basic map */
4000 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
4001 rateptr);
4002 }
4003}
4004
4005static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
4006 struct ieee80211_vif *vif,
4007 struct ieee80211_bss_conf *conf,
4008 u32 changed)
4009{
4010 struct b43_wl *wl = hw_to_b43_wl(hw);
4011 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004012
4013 mutex_lock(&wl->mutex);
4014
4015 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01004016 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004017 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004018
4019 B43_WARN_ON(wl->vif != vif);
4020
4021 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004022 if (conf->bssid)
4023 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
4024 else
4025 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02004026 }
4027
Johannes Berg3f0d8432009-05-18 10:53:18 +02004028 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
4029 if (changed & BSS_CHANGED_BEACON &&
4030 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4031 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
4032 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
4033 b43_update_templates(wl);
4034
4035 if (changed & BSS_CHANGED_BSSID)
4036 b43_write_mac_bssid_templates(dev);
4037 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02004038
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004039 b43_mac_suspend(dev);
4040
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004041 /* Update templates for AP/mesh mode. */
4042 if (changed & BSS_CHANGED_BEACON_INT &&
4043 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
4044 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06004045 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
4046 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02004047 b43_set_beacon_int(dev, conf->beacon_int);
4048
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004049 if (changed & BSS_CHANGED_BASIC_RATES)
4050 b43_update_basic_rates(dev, conf->basic_rates);
4051
4052 if (changed & BSS_CHANGED_ERP_SLOT) {
4053 if (conf->use_short_slot)
4054 b43_short_slot_timing_enable(dev);
4055 else
4056 b43_short_slot_timing_disable(dev);
4057 }
4058
4059 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01004060out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004061 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004062}
4063
Michael Buesch40faacc2007-10-28 16:29:32 +01004064static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004065 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4066 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004067{
4068 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004069 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004070 u8 algorithm;
4071 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004072 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004073 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004074
4075 if (modparam_nohwcrypt)
4076 return -ENOSPC; /* User disabled HW-crypto */
4077
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004078 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4079 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4080 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4081 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4082 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4083 /*
4084 * For now, disable hw crypto for the RSN IBSS group keys. This
4085 * could be optimized in the future, but until that gets
4086 * implemented, use of software crypto for group addressed
4087 * frames is a acceptable to allow RSN IBSS to be used.
4088 */
4089 return -EOPNOTSUPP;
4090 }
4091
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004092 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004093
4094 dev = wl->current_dev;
4095 err = -ENODEV;
4096 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4097 goto out_unlock;
4098
Michael Buesch403a3a12009-06-08 21:04:57 +02004099 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004100 /* We don't have firmware for the crypto engine.
4101 * Must use software-crypto. */
4102 err = -EOPNOTSUPP;
4103 goto out_unlock;
4104 }
4105
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004106 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004107 switch (key->cipher) {
4108 case WLAN_CIPHER_SUITE_WEP40:
4109 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004110 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004111 case WLAN_CIPHER_SUITE_WEP104:
4112 algorithm = B43_SEC_ALGO_WEP104;
4113 break;
4114 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004115 algorithm = B43_SEC_ALGO_TKIP;
4116 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004117 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004118 algorithm = B43_SEC_ALGO_AES;
4119 break;
4120 default:
4121 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004122 goto out_unlock;
4123 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004124 index = (u8) (key->keyidx);
4125 if (index > 3)
4126 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004127
4128 switch (cmd) {
4129 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004130 if (algorithm == B43_SEC_ALGO_TKIP &&
4131 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4132 !modparam_hwtkip)) {
4133 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004134 err = -EOPNOTSUPP;
4135 goto out_unlock;
4136 }
4137
Michael Buesche808e582008-12-19 21:30:52 +01004138 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004139 if (WARN_ON(!sta)) {
4140 err = -EOPNOTSUPP;
4141 goto out_unlock;
4142 }
Michael Buesche808e582008-12-19 21:30:52 +01004143 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004144 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004145 key->key, key->keylen,
4146 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004147 } else {
4148 /* Group key */
4149 err = b43_key_write(dev, index, algorithm,
4150 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004151 }
4152 if (err)
4153 goto out_unlock;
4154
4155 if (algorithm == B43_SEC_ALGO_WEP40 ||
4156 algorithm == B43_SEC_ALGO_WEP104) {
4157 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4158 } else {
4159 b43_hf_write(dev,
4160 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4161 }
4162 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004163 if (algorithm == B43_SEC_ALGO_TKIP)
4164 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004165 break;
4166 case DISABLE_KEY: {
4167 err = b43_key_clear(dev, key->hw_key_idx);
4168 if (err)
4169 goto out_unlock;
4170 break;
4171 }
4172 default:
4173 B43_WARN_ON(1);
4174 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004175
Michael Buesche4d6b792007-09-18 15:39:42 -04004176out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004177 if (!err) {
4178 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004179 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004180 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004181 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004182 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004183 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004184 mutex_unlock(&wl->mutex);
4185
Michael Buesche4d6b792007-09-18 15:39:42 -04004186 return err;
4187}
4188
Michael Buesch40faacc2007-10-28 16:29:32 +01004189static void b43_op_configure_filter(struct ieee80211_hw *hw,
4190 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004191 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004192{
4193 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004194 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004195
Michael Buesch36dbd952009-09-04 22:51:29 +02004196 mutex_lock(&wl->mutex);
4197 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004198 if (!dev) {
4199 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004200 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004201 }
Johannes Berg4150c572007-09-17 01:29:23 -04004202
Johannes Berg4150c572007-09-17 01:29:23 -04004203 *fflags &= FIF_PROMISC_IN_BSS |
4204 FIF_ALLMULTI |
4205 FIF_FCSFAIL |
4206 FIF_PLCPFAIL |
4207 FIF_CONTROL |
4208 FIF_OTHER_BSS |
4209 FIF_BCN_PRBRESP_PROMISC;
4210
4211 changed &= FIF_PROMISC_IN_BSS |
4212 FIF_ALLMULTI |
4213 FIF_FCSFAIL |
4214 FIF_PLCPFAIL |
4215 FIF_CONTROL |
4216 FIF_OTHER_BSS |
4217 FIF_BCN_PRBRESP_PROMISC;
4218
4219 wl->filter_flags = *fflags;
4220
4221 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4222 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004223
4224out_unlock:
4225 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004226}
4227
Michael Buesch36dbd952009-09-04 22:51:29 +02004228/* Locking: wl->mutex
4229 * Returns the current dev. This might be different from the passed in dev,
4230 * because the core might be gone away while we unlocked the mutex. */
4231static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004232{
Larry Finger9a53bf52011-08-27 15:53:42 -05004233 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004234 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004235 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004236 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004237
Larry Finger9a53bf52011-08-27 15:53:42 -05004238 if (!dev)
4239 return NULL;
4240 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004241redo:
4242 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4243 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004244
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004245 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004246 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004247 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004248 cancel_work_sync(&wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04004249 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004250 dev = wl->current_dev;
4251 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4252 /* Whoops, aliens ate up the device while we were unlocked. */
4253 return dev;
4254 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004255
Michael Buesch36dbd952009-09-04 22:51:29 +02004256 /* Disable interrupts on the device. */
4257 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004258 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004259 /* wl->mutex is locked. That is enough. */
4260 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4261 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4262 } else {
4263 spin_lock_irq(&wl->hardirq_lock);
4264 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4265 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4266 spin_unlock_irq(&wl->hardirq_lock);
4267 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004268 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004269 orig_dev = dev;
4270 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004271 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004272 b43_sdio_free_irq(dev);
4273 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004274 synchronize_irq(dev->dev->irq);
4275 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004276 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004277 mutex_lock(&wl->mutex);
4278 dev = wl->current_dev;
4279 if (!dev)
4280 return dev;
4281 if (dev != orig_dev) {
4282 if (b43_status(dev) >= B43_STAT_STARTED)
4283 goto redo;
4284 return dev;
4285 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004286 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4287 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004288
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004289 /* Drain all TX queues. */
4290 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004291 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4292 struct sk_buff *skb;
4293
4294 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4295 ieee80211_free_txskb(wl->hw, skb);
4296 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004297 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004298
Michael Buesche4d6b792007-09-18 15:39:42 -04004299 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004300 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004301 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004302
4303 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004304}
4305
4306/* Locking: wl->mutex */
4307static int b43_wireless_core_start(struct b43_wldev *dev)
4308{
4309 int err;
4310
4311 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4312
4313 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004314 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004315 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4316 if (err) {
4317 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4318 goto out;
4319 }
4320 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004321 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004322 b43_interrupt_thread_handler,
4323 IRQF_SHARED, KBUILD_MODNAME, dev);
4324 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004325 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004326 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004327 goto out;
4328 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004329 }
4330
4331 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004332 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004333 b43_set_status(dev, B43_STAT_STARTED);
4334
4335 /* Start data flow (TX/RX). */
4336 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004337 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004338
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004339 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004340 b43_periodic_tasks_setup(dev);
4341
Michael Buescha78b3bb2009-09-11 21:44:05 +02004342 b43_leds_init(dev);
4343
Michael Buesche4d6b792007-09-18 15:39:42 -04004344 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004345out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004346 return err;
4347}
4348
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004349static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4350{
4351 switch (phy_type) {
4352 case B43_PHYTYPE_A:
4353 return "A";
4354 case B43_PHYTYPE_B:
4355 return "B";
4356 case B43_PHYTYPE_G:
4357 return "G";
4358 case B43_PHYTYPE_N:
4359 return "N";
4360 case B43_PHYTYPE_LP:
4361 return "LP";
4362 case B43_PHYTYPE_SSLPN:
4363 return "SSLPN";
4364 case B43_PHYTYPE_HT:
4365 return "HT";
4366 case B43_PHYTYPE_LCN:
4367 return "LCN";
4368 case B43_PHYTYPE_LCNXN:
4369 return "LCNXN";
4370 case B43_PHYTYPE_LCN40:
4371 return "LCN40";
4372 case B43_PHYTYPE_AC:
4373 return "AC";
4374 }
4375 return "UNKNOWN";
4376}
4377
Michael Buesche4d6b792007-09-18 15:39:42 -04004378/* Get PHY and RADIO versioning numbers */
4379static int b43_phy_versioning(struct b43_wldev *dev)
4380{
4381 struct b43_phy *phy = &dev->phy;
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004382 const u8 core_rev = dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004383 u32 tmp;
4384 u8 analog_type;
4385 u8 phy_type;
4386 u8 phy_rev;
4387 u16 radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004388 u16 radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004389 u16 radio_rev;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004390 u8 radio_ver;
Michael Buesche4d6b792007-09-18 15:39:42 -04004391 int unsupported = 0;
4392
4393 /* Get PHY versioning */
4394 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4395 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4396 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4397 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004398
4399 /* LCNXN is continuation of N which run out of revisions */
4400 if (phy_type == B43_PHYTYPE_LCNXN) {
4401 phy_type = B43_PHYTYPE_N;
4402 phy_rev += 16;
4403 }
4404
Michael Buesche4d6b792007-09-18 15:39:42 -04004405 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004406#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004407 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004408 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004409 unsupported = 1;
4410 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004411#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004412#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004413 case B43_PHYTYPE_N:
Rafał Miłecki40c68f22014-07-08 15:11:07 +02004414 if (phy_rev >= 19)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004415 unsupported = 1;
4416 break;
4417#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004418#ifdef CONFIG_B43_PHY_LP
4419 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004420 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004421 unsupported = 1;
4422 break;
4423#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004424#ifdef CONFIG_B43_PHY_HT
4425 case B43_PHYTYPE_HT:
4426 if (phy_rev > 1)
4427 unsupported = 1;
4428 break;
4429#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004430#ifdef CONFIG_B43_PHY_LCN
4431 case B43_PHYTYPE_LCN:
4432 if (phy_rev > 1)
4433 unsupported = 1;
4434 break;
4435#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004436 default:
4437 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004438 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004439 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004440 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4441 analog_type, phy_type, b43_phy_name(dev, phy_type),
4442 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004443 return -EOPNOTSUPP;
4444 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004445 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4446 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004447
4448 /* Get RADIO versioning */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004449 if (core_rev == 40 || core_rev == 42) {
4450 radio_manuf = 0x17F;
4451
4452 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, 0);
4453 radio_rev = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4454
4455 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, 1);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004456 radio_id = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4457
4458 radio_ver = 0; /* Is there version somewhere? */
Rafał Miłeckife5e499f2014-07-04 09:21:56 +02004459 } else if (core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004460 u16 radio24[3];
4461
4462 for (tmp = 0; tmp < 3; tmp++) {
4463 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, tmp);
4464 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4465 }
4466
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004467 radio_manuf = 0x17F;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004468 radio_id = (radio24[2] << 8) | radio24[1];
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004469 radio_rev = (radio24[0] & 0xF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004470 radio_ver = (radio24[0] & 0xF0) >> 4;
Michael Buesche4d6b792007-09-18 15:39:42 -04004471 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004472 if (dev->dev->chip_id == 0x4317) {
4473 if (dev->dev->chip_rev == 0)
4474 tmp = 0x3205017F;
4475 else if (dev->dev->chip_rev == 1)
4476 tmp = 0x4205017F;
4477 else
4478 tmp = 0x5205017F;
4479 } else {
4480 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4481 B43_RADIOCTL_ID);
4482 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
4483 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4484 B43_RADIOCTL_ID);
4485 tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH)
4486 << 16;
4487 }
4488 radio_manuf = (tmp & 0x00000FFF);
Rafał Miłecki16e75452014-07-20 12:57:45 +02004489 radio_id = (tmp & 0x0FFFF000) >> 12;
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004490 radio_rev = (tmp & 0xF0000000) >> 28;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004491 radio_ver = 0; /* Probably not available on old hw */
Michael Buesche4d6b792007-09-18 15:39:42 -04004492 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004493
Michael Buesch96c755a2008-01-06 00:09:46 +01004494 if (radio_manuf != 0x17F /* Broadcom */)
4495 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004496 switch (phy_type) {
4497 case B43_PHYTYPE_A:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004498 if (radio_id != 0x2060)
Michael Buesche4d6b792007-09-18 15:39:42 -04004499 unsupported = 1;
4500 if (radio_rev != 1)
4501 unsupported = 1;
4502 if (radio_manuf != 0x17F)
4503 unsupported = 1;
4504 break;
4505 case B43_PHYTYPE_B:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004506 if ((radio_id & 0xFFF0) != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004507 unsupported = 1;
4508 break;
4509 case B43_PHYTYPE_G:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004510 if (radio_id != 0x2050)
Michael Buesche4d6b792007-09-18 15:39:42 -04004511 unsupported = 1;
4512 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004513 case B43_PHYTYPE_N:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004514 if (radio_id != 0x2055 && radio_id != 0x2056 &&
4515 radio_id != 0x2057)
Rafał Miłecki3695b932014-07-08 15:11:10 +02004516 unsupported = 1;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004517 if (radio_id == 0x2057 &&
Rafał Miłeckic11082f2014-07-19 12:52:47 +02004518 !(radio_rev == 9 || radio_rev == 14))
Michael Buesch96c755a2008-01-06 00:09:46 +01004519 unsupported = 1;
4520 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004521 case B43_PHYTYPE_LP:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004522 if (radio_id != 0x2062 && radio_id != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004523 unsupported = 1;
4524 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004525 case B43_PHYTYPE_HT:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004526 if (radio_id != 0x2059)
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004527 unsupported = 1;
4528 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004529 case B43_PHYTYPE_LCN:
Rafał Miłecki16e75452014-07-20 12:57:45 +02004530 if (radio_id != 0x2064)
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004531 unsupported = 1;
4532 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004533 default:
4534 B43_WARN_ON(1);
4535 }
4536 if (unsupported) {
Rafał Miłecki88d825b2014-07-02 19:07:43 +02004537 b43err(dev->wl,
Rafał Miłecki16e75452014-07-20 12:57:45 +02004538 "FOUND UNSUPPORTED RADIO (Manuf 0x%X, ID 0x%X, Revision %u, Version %u)\n",
4539 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004540 return -EOPNOTSUPP;
4541 }
Rafał Miłecki16e75452014-07-20 12:57:45 +02004542 b43info(dev->wl,
4543 "Found Radio: Manuf 0x%X, ID 0x%X, Revision %u, Version %u\n",
4544 radio_manuf, radio_id, radio_rev, radio_ver);
Michael Buesche4d6b792007-09-18 15:39:42 -04004545
Rafał Miłecki16e75452014-07-20 12:57:45 +02004546 /* FIXME: b43 treats "id" as "ver" and ignores the real "ver" */
Michael Buesche4d6b792007-09-18 15:39:42 -04004547 phy->radio_manuf = radio_manuf;
Rafał Miłecki16e75452014-07-20 12:57:45 +02004548 phy->radio_ver = radio_id;
Michael Buesche4d6b792007-09-18 15:39:42 -04004549 phy->radio_rev = radio_rev;
4550
4551 phy->analog = analog_type;
4552 phy->type = phy_type;
4553 phy->rev = phy_rev;
4554
4555 return 0;
4556}
4557
4558static void setup_struct_phy_for_init(struct b43_wldev *dev,
4559 struct b43_phy *phy)
4560{
Michael Buesche4d6b792007-09-18 15:39:42 -04004561 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004562 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004563 /* PHY TX errors counter. */
4564 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004565
4566#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004567 phy->phy_locked = false;
4568 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004569#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004570}
4571
4572static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4573{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004574 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004575
Michael Buesch6a724d62007-09-20 22:12:58 +02004576 /* Assume the radio is enabled. If it's not enabled, the state will
4577 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004578 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004579
4580 /* Stats */
4581 memset(&dev->stats, 0, sizeof(dev->stats));
4582
4583 setup_struct_phy_for_init(dev, &dev->phy);
4584
4585 /* IRQ related flags */
4586 dev->irq_reason = 0;
4587 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004588 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004589 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004590 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004591
4592 dev->mac_suspended = 1;
4593
4594 /* Noise calculation context */
4595 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4596}
4597
4598static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4599{
Rafał Miłecki05814832011-05-18 02:06:39 +02004600 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004601 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004602
Michael Buesch1855ba72008-04-18 20:51:41 +02004603 if (!modparam_btcoex)
4604 return;
Larry Finger95de2842007-11-09 16:57:18 -06004605 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004606 return;
4607 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4608 return;
4609
4610 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004611 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004612 hf |= B43_HF_BTCOEXALT;
4613 else
4614 hf |= B43_HF_BTCOEX;
4615 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004616}
4617
4618static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004619{
4620 if (!modparam_btcoex)
4621 return;
4622 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004623}
4624
4625static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4626{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004627 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004628 u32 tmp;
4629
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004630#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004631 if (dev->dev->bus_type != B43_BUS_SSB)
4632 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004633#else
4634 return;
4635#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004636
4637 bus = dev->dev->sdev->bus;
4638
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004639 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4640 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004641 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004642 tmp &= ~SSB_IMCFGLO_REQTO;
4643 tmp &= ~SSB_IMCFGLO_SERTO;
4644 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004645 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004646 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004647 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004648}
4649
Michael Bueschd59f7202008-04-03 18:56:19 +02004650static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4651{
4652 u16 pu_delay;
4653
4654 /* The time value is in microseconds. */
4655 if (dev->phy.type == B43_PHYTYPE_A)
4656 pu_delay = 3700;
4657 else
4658 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004659 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004660 pu_delay = 500;
4661 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4662 pu_delay = max(pu_delay, (u16)2400);
4663
4664 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4665}
4666
4667/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4668static void b43_set_pretbtt(struct b43_wldev *dev)
4669{
4670 u16 pretbtt;
4671
4672 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004673 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004674 pretbtt = 2;
4675 } else {
4676 if (dev->phy.type == B43_PHYTYPE_A)
4677 pretbtt = 120;
4678 else
4679 pretbtt = 250;
4680 }
4681 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4682 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4683}
4684
Michael Buesche4d6b792007-09-18 15:39:42 -04004685/* Shutdown a wireless core */
4686/* Locking: wl->mutex */
4687static void b43_wireless_core_exit(struct b43_wldev *dev)
4688{
Michael Buesch36dbd952009-09-04 22:51:29 +02004689 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4690 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004691 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004692
Michael Buesche4d6b792007-09-18 15:39:42 -04004693 b43_set_status(dev, B43_STAT_UNINIT);
4694
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004695 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004696 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4697 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004698
Hauke Mehrtens50023002013-08-24 00:32:34 +02004699 switch (dev->dev->bus_type) {
4700#ifdef CONFIG_B43_BCMA
4701 case B43_BUS_BCMA:
4702 bcma_core_pci_down(dev->dev->bdev->bus);
4703 break;
4704#endif
4705#ifdef CONFIG_B43_SSB
4706 case B43_BUS_SSB:
4707 /* TODO */
4708 break;
4709#endif
4710 }
4711
Michael Buesche4d6b792007-09-18 15:39:42 -04004712 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004713 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004714 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004715 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004716 if (dev->wl->current_beacon) {
4717 dev_kfree_skb_any(dev->wl->current_beacon);
4718 dev->wl->current_beacon = NULL;
4719 }
4720
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004721 b43_device_disable(dev, 0);
4722 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004723}
4724
4725/* Initialize a wireless core */
4726static int b43_wireless_core_init(struct b43_wldev *dev)
4727{
Rafał Miłecki05814832011-05-18 02:06:39 +02004728 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004729 struct b43_phy *phy = &dev->phy;
4730 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004731 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004732
4733 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4734
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004735 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004736 if (err)
4737 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004738 if (!b43_device_is_enabled(dev))
4739 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004740
Michael Bueschfb111372008-09-02 13:00:34 +02004741 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004742 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004743 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004744
4745 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004746 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004747#ifdef CONFIG_B43_BCMA
4748 case B43_BUS_BCMA:
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004749 bcma_core_pci_irq_ctl(&dev->dev->bdev->bus->drv_pci[0],
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004750 dev->dev->bdev, true);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004751 bcma_core_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004752 break;
4753#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004754#ifdef CONFIG_B43_SSB
4755 case B43_BUS_SSB:
4756 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4757 dev->dev->sdev);
4758 break;
4759#endif
4760 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004761
4762 b43_imcfglo_timeouts_workaround(dev);
4763 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004764 if (phy->ops->prepare_hardware) {
4765 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004766 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004767 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004768 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004769 err = b43_chip_init(dev);
4770 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004771 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004772 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004773 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004774 hf = b43_hf_read(dev);
4775 if (phy->type == B43_PHYTYPE_G) {
4776 hf |= B43_HF_SYMW;
4777 if (phy->rev == 1)
4778 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004779 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004780 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004781 }
4782 if (phy->radio_ver == 0x2050) {
4783 if (phy->radio_rev == 6)
4784 hf |= B43_HF_4318TSSI;
4785 if (phy->radio_rev < 6)
4786 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004787 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004788 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4789 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004790#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004791 if (dev->dev->bus_type == B43_BUS_SSB &&
4792 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4793 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004794 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004795#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004796 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004797 b43_hf_write(dev, hf);
4798
Michael Buesch74cfdba2007-10-28 16:19:44 +01004799 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4800 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004801 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4802 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4803
4804 /* Disable sending probe responses from firmware.
4805 * Setting the MaxTime to one usec will always trigger
4806 * a timeout, so we never send any probe resp.
4807 * A timeout of zero is infinite. */
4808 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4809
4810 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004811 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004812
4813 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004814 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004815 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004816 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004817 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004818 /* Maximum Contention Window */
4819 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4820
Rafał Miłecki505fb012011-05-19 15:11:27 +02004821 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004822 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004823 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004824 err = b43_pio_init(dev);
4825 } else if (dev->use_pio) {
4826 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4827 "This should not be needed and will result in lower "
4828 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004829 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004830 err = b43_pio_init(dev);
4831 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004832 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004833 err = b43_dma_init(dev);
4834 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004835 if (err)
4836 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004837 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004838 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004839 b43_bluetooth_coext_enable(dev);
4840
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004841 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004842 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004843 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004844
Michael Buesch5ab95492009-09-10 20:31:46 +02004845 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004846
4847 b43_set_status(dev, B43_STAT_INITIALIZED);
4848
Larry Finger1a8d12272007-12-14 13:59:11 +01004849out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004850 return err;
4851
Michael Bueschef1a6282008-08-27 18:53:02 +02004852err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004853 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004854err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004855 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004856 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4857 return err;
4858}
4859
Michael Buesch40faacc2007-10-28 16:29:32 +01004860static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004861 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004862{
4863 struct b43_wl *wl = hw_to_b43_wl(hw);
4864 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004865 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004866
4867 /* TODO: allow WDS/AP devices to coexist */
4868
Johannes Berg1ed32e42009-12-23 13:15:45 +01004869 if (vif->type != NL80211_IFTYPE_AP &&
4870 vif->type != NL80211_IFTYPE_MESH_POINT &&
4871 vif->type != NL80211_IFTYPE_STATION &&
4872 vif->type != NL80211_IFTYPE_WDS &&
4873 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004874 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004875
4876 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004877 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004878 goto out_mutex_unlock;
4879
Johannes Berg1ed32e42009-12-23 13:15:45 +01004880 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004881
4882 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004883 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01004884 wl->vif = vif;
4885 wl->if_type = vif->type;
4886 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004887
Michael Buesche4d6b792007-09-18 15:39:42 -04004888 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004889 b43_set_pretbtt(dev);
4890 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004891 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004892
4893 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004894 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004895 mutex_unlock(&wl->mutex);
4896
Felix Fietkau2a190322011-08-10 13:50:30 -06004897 if (err == 0)
4898 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
4899
Michael Buesche4d6b792007-09-18 15:39:42 -04004900 return err;
4901}
4902
Michael Buesch40faacc2007-10-28 16:29:32 +01004903static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004904 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004905{
4906 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04004907 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004908
Johannes Berg1ed32e42009-12-23 13:15:45 +01004909 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004910
4911 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004912
4913 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01004914 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01004915 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04004916
Rusty Russell3db1cd52011-12-19 13:56:45 +00004917 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04004918
Johannes Berg4150c572007-09-17 01:29:23 -04004919 b43_adjust_opmode(dev);
4920 memset(wl->mac_addr, 0, ETH_ALEN);
4921 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04004922
4923 mutex_unlock(&wl->mutex);
4924}
4925
Michael Buesch40faacc2007-10-28 16:29:32 +01004926static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004927{
4928 struct b43_wl *wl = hw_to_b43_wl(hw);
4929 struct b43_wldev *dev = wl->current_dev;
4930 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07004931 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004932
Michael Buesch7be1bb62008-01-23 21:10:56 +01004933 /* Kill all old instance specific information to make sure
4934 * the card won't use it in the short timeframe between start
4935 * and mac80211 reconfiguring it. */
4936 memset(wl->bssid, 0, ETH_ALEN);
4937 memset(wl->mac_addr, 0, ETH_ALEN);
4938 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004939 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01004940 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004941 wl->beacon0_uploaded = false;
4942 wl->beacon1_uploaded = false;
4943 wl->beacon_templates_virgin = true;
4944 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01004945
Johannes Berg4150c572007-09-17 01:29:23 -04004946 mutex_lock(&wl->mutex);
4947
4948 if (b43_status(dev) < B43_STAT_INITIALIZED) {
4949 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05004950 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04004951 goto out_mutex_unlock;
4952 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004953 }
4954
Johannes Berg4150c572007-09-17 01:29:23 -04004955 if (b43_status(dev) < B43_STAT_STARTED) {
4956 err = b43_wireless_core_start(dev);
4957 if (err) {
4958 if (did_init)
4959 b43_wireless_core_exit(dev);
4960 goto out_mutex_unlock;
4961 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004962 }
Johannes Berg4150c572007-09-17 01:29:23 -04004963
Johannes Bergf41f3f32009-06-07 12:30:34 -05004964 /* XXX: only do if device doesn't support rfkill irq */
4965 wiphy_rfkill_start_polling(hw->wiphy);
4966
Johannes Berg4150c572007-09-17 01:29:23 -04004967 out_mutex_unlock:
4968 mutex_unlock(&wl->mutex);
4969
Seth Forsheedbdedbd2012-04-25 17:28:00 -05004970 /*
4971 * Configuration may have been overwritten during initialization.
4972 * Reload the configuration, but only if initialization was
4973 * successful. Reloading the configuration after a failed init
4974 * may hang the system.
4975 */
4976 if (!err)
4977 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06004978
Johannes Berg4150c572007-09-17 01:29:23 -04004979 return err;
4980}
4981
Michael Buesch40faacc2007-10-28 16:29:32 +01004982static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004983{
4984 struct b43_wl *wl = hw_to_b43_wl(hw);
4985 struct b43_wldev *dev = wl->current_dev;
4986
Michael Buescha82d9922008-04-04 21:40:06 +02004987 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d12272007-12-14 13:59:11 +01004988
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004989 if (!dev)
4990 goto out;
4991
Johannes Berg4150c572007-09-17 01:29:23 -04004992 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004993 if (b43_status(dev) >= B43_STAT_STARTED) {
4994 dev = b43_wireless_core_stop(dev);
4995 if (!dev)
4996 goto out_unlock;
4997 }
Johannes Berg4150c572007-09-17 01:29:23 -04004998 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004999 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02005000
5001out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04005002 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01005003out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02005004 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04005005}
5006
Johannes Berg17741cd2008-09-11 00:02:02 +02005007static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
5008 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01005009{
5010 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01005011
Felix Fietkau8f611282009-11-07 18:37:37 +01005012 /* FIXME: add locking */
Johannes Berg9d139c82008-07-09 14:40:37 +02005013 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01005014
5015 return 0;
5016}
5017
Johannes Berg38968d02008-02-25 16:27:50 +01005018static void b43_op_sta_notify(struct ieee80211_hw *hw,
5019 struct ieee80211_vif *vif,
5020 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02005021 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01005022{
5023 struct b43_wl *wl = hw_to_b43_wl(hw);
5024
5025 B43_WARN_ON(!vif || wl->vif != vif);
5026}
5027
Michael Buesch25d3ef52009-02-20 15:39:21 +01005028static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
5029{
5030 struct b43_wl *wl = hw_to_b43_wl(hw);
5031 struct b43_wldev *dev;
5032
5033 mutex_lock(&wl->mutex);
5034 dev = wl->current_dev;
5035 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5036 /* Disable CFP update during scan on other channels. */
5037 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
5038 }
5039 mutex_unlock(&wl->mutex);
5040}
5041
5042static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
5043{
5044 struct b43_wl *wl = hw_to_b43_wl(hw);
5045 struct b43_wldev *dev;
5046
5047 mutex_lock(&wl->mutex);
5048 dev = wl->current_dev;
5049 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
5050 /* Re-enable CFP update. */
5051 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
5052 }
5053 mutex_unlock(&wl->mutex);
5054}
5055
John W. Linville354b4f02010-04-29 15:56:06 -04005056static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
5057 struct survey_info *survey)
5058{
5059 struct b43_wl *wl = hw_to_b43_wl(hw);
5060 struct b43_wldev *dev = wl->current_dev;
5061 struct ieee80211_conf *conf = &hw->conf;
5062
5063 if (idx != 0)
5064 return -ENOENT;
5065
Karl Beldan675a0b02013-03-25 16:26:57 +01005066 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04005067 survey->filled = SURVEY_INFO_NOISE_DBM;
5068 survey->noise = dev->stats.link_noise;
5069
5070 return 0;
5071}
5072
Michael Buesche4d6b792007-09-18 15:39:42 -04005073static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01005074 .tx = b43_op_tx,
5075 .conf_tx = b43_op_conf_tx,
5076 .add_interface = b43_op_add_interface,
5077 .remove_interface = b43_op_remove_interface,
5078 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005079 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005080 .configure_filter = b43_op_configure_filter,
5081 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005082 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005083 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005084 .get_tsf = b43_op_get_tsf,
5085 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005086 .start = b43_op_start,
5087 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005088 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005089 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005090 .sw_scan_start = b43_op_sw_scan_start_notifier,
5091 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005092 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005093 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005094};
5095
5096/* Hard-reset the chip. Do not call this directly.
5097 * Use b43_controller_restart()
5098 */
5099static void b43_chip_reset(struct work_struct *work)
5100{
5101 struct b43_wldev *dev =
5102 container_of(work, struct b43_wldev, restart_work);
5103 struct b43_wl *wl = dev->wl;
5104 int err = 0;
5105 int prev_status;
5106
5107 mutex_lock(&wl->mutex);
5108
5109 prev_status = b43_status(dev);
5110 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005111 if (prev_status >= B43_STAT_STARTED) {
5112 dev = b43_wireless_core_stop(dev);
5113 if (!dev) {
5114 err = -ENODEV;
5115 goto out;
5116 }
5117 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005118 if (prev_status >= B43_STAT_INITIALIZED)
5119 b43_wireless_core_exit(dev);
5120
5121 /* ...and up again. */
5122 if (prev_status >= B43_STAT_INITIALIZED) {
5123 err = b43_wireless_core_init(dev);
5124 if (err)
5125 goto out;
5126 }
5127 if (prev_status >= B43_STAT_STARTED) {
5128 err = b43_wireless_core_start(dev);
5129 if (err) {
5130 b43_wireless_core_exit(dev);
5131 goto out;
5132 }
5133 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005134out:
5135 if (err)
5136 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005137 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005138
5139 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005140 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005141 return;
5142 }
5143
5144 /* reload configuration */
5145 b43_op_config(wl->hw, ~0);
5146 if (wl->vif)
5147 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5148
5149 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005150}
5151
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005152static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005153 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005154{
5155 struct ieee80211_hw *hw = dev->wl->hw;
Rafał Miłecki3695b932014-07-08 15:11:10 +02005156 struct b43_phy *phy = &dev->phy;
5157 bool limited_2g;
5158
5159 /* We don't support all 2 GHz channels on some devices */
Rafał Miłeckic11082f2014-07-19 12:52:47 +02005160 limited_2g = phy->radio_ver == 0x2057 &&
5161 (phy->radio_rev == 9 || phy->radio_rev == 14);
Michael Buesche4d6b792007-09-18 15:39:42 -04005162
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005163 if (have_2ghz_phy)
Rafał Miłecki3695b932014-07-08 15:11:10 +02005164 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = limited_2g ?
5165 &b43_band_2ghz_limited : &b43_band_2GHz;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005166 if (dev->phy.type == B43_PHYTYPE_N) {
5167 if (have_5ghz_phy)
5168 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
5169 } else {
5170 if (have_5ghz_phy)
5171 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5172 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005173
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005174 dev->phy.supports_2ghz = have_2ghz_phy;
5175 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005176
5177 return 0;
5178}
5179
5180static void b43_wireless_core_detach(struct b43_wldev *dev)
5181{
5182 /* We release firmware that late to not be required to re-request
5183 * is all the time when we reinit the core. */
5184 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005185 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005186}
5187
Rafał Miłecki075ca602014-05-19 23:18:54 +02005188static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5189 bool *have_5ghz_phy)
5190{
5191 u16 dev_id = 0;
5192
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005193#ifdef CONFIG_B43_BCMA
5194 if (dev->dev->bus_type == B43_BUS_BCMA &&
5195 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5196 dev_id = dev->dev->bdev->bus->host_pci->device;
5197#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005198#ifdef CONFIG_B43_SSB
5199 if (dev->dev->bus_type == B43_BUS_SSB &&
5200 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5201 dev_id = dev->dev->sdev->bus->host_pci->device;
5202#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005203 /* Override with SPROM value if available */
5204 if (dev->dev->bus_sprom->dev_id)
5205 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005206
5207 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5208 switch (dev_id) {
5209 case 0x4324: /* BCM4306 */
5210 case 0x4312: /* BCM4311 */
5211 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005212 case 0x4328: /* BCM4321 */
5213 case 0x432b: /* BCM4322 */
5214 case 0x4350: /* BCM43222 */
5215 case 0x4353: /* BCM43224 */
5216 case 0x0576: /* BCM43224 */
5217 case 0x435f: /* BCM6362 */
5218 case 0x4331: /* BCM4331 */
5219 case 0x4359: /* BCM43228 */
5220 case 0x43a0: /* BCM4360 */
5221 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005222 /* Dual band devices */
5223 *have_2ghz_phy = true;
5224 *have_5ghz_phy = true;
5225 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005226 case 0x4321: /* BCM4306 */
5227 case 0x4313: /* BCM4311 */
5228 case 0x431a: /* BCM4318 */
5229 case 0x432a: /* BCM4321 */
5230 case 0x432d: /* BCM4322 */
5231 case 0x4352: /* BCM43222 */
5232 case 0x4333: /* BCM4331 */
5233 case 0x43a2: /* BCM4360 */
5234 case 0x43b3: /* BCM4352 */
5235 /* 5 GHz only devices */
5236 *have_2ghz_phy = false;
5237 *have_5ghz_phy = true;
5238 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005239 }
5240
5241 /* As a fallback, try to guess using PHY type */
5242 switch (dev->phy.type) {
5243 case B43_PHYTYPE_A:
5244 *have_2ghz_phy = false;
5245 *have_5ghz_phy = true;
5246 return;
5247 case B43_PHYTYPE_G:
5248 case B43_PHYTYPE_N:
5249 case B43_PHYTYPE_LP:
5250 case B43_PHYTYPE_HT:
5251 case B43_PHYTYPE_LCN:
5252 *have_2ghz_phy = true;
5253 *have_5ghz_phy = false;
5254 return;
5255 }
5256
5257 B43_WARN_ON(1);
5258}
5259
Michael Buesche4d6b792007-09-18 15:39:42 -04005260static int b43_wireless_core_attach(struct b43_wldev *dev)
5261{
5262 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005263 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005264 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005265 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005266 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005267
5268 /* Do NOT do any device initialization here.
5269 * Do it in wireless_core_init() instead.
5270 * This function is for gathering basic information about the HW, only.
5271 * Also some structs may be set up here. But most likely you want to have
5272 * that in core_init(), too.
5273 */
5274
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005275 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005276 if (err) {
5277 b43err(wl, "Bus powerup failed\n");
5278 goto out;
5279 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005280
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005281 phy->do_full_init = true;
5282
Rafał Miłecki075ca602014-05-19 23:18:54 +02005283 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005284 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005285#ifdef CONFIG_B43_BCMA
5286 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005287 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5288 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5289 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005290 break;
5291#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005292#ifdef CONFIG_B43_SSB
5293 case B43_BUS_SSB:
5294 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005295 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5296 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5297 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005298 } else
5299 B43_WARN_ON(1);
5300 break;
5301#endif
5302 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005303
Michael Buesch96c755a2008-01-06 00:09:46 +01005304 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005305 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005306
Rafał Miłecki075ca602014-05-19 23:18:54 +02005307 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005308 err = b43_phy_versioning(dev);
5309 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005310 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005311
5312 /* Get real info about supported bands */
5313 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5314
5315 /* We don't support 5 GHz on some PHYs yet */
Rafał Miłecki72fcd3d2014-07-08 21:00:19 +02005316 if (have_5ghz_phy) {
5317 switch (dev->phy.type) {
5318 case B43_PHYTYPE_A:
5319 case B43_PHYTYPE_G:
5320 case B43_PHYTYPE_N:
5321 case B43_PHYTYPE_LP:
5322 case B43_PHYTYPE_HT:
5323 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
5324 have_5ghz_phy = false;
5325 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005326 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005327
5328 if (!have_2ghz_phy && !have_5ghz_phy) {
5329 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005330 err = -EOPNOTSUPP;
5331 goto err_powerdown;
5332 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005333
Michael Bueschfb111372008-09-02 13:00:34 +02005334 err = b43_phy_allocate(dev);
5335 if (err)
5336 goto err_powerdown;
5337
Michael Buesch96c755a2008-01-06 00:09:46 +01005338 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005339 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005340
5341 err = b43_validate_chipaccess(dev);
5342 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005343 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005344 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005345 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005346 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005347
5348 /* Now set some default "current_dev" */
5349 if (!wl->current_dev)
5350 wl->current_dev = dev;
5351 INIT_WORK(&dev->restart_work, b43_chip_reset);
5352
Michael Bueschcb24f572008-09-03 12:12:20 +02005353 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005354 b43_device_disable(dev, 0);
5355 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005356
5357out:
5358 return err;
5359
Michael Bueschfb111372008-09-02 13:00:34 +02005360err_phy_free:
5361 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005362err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005363 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005364 return err;
5365}
5366
Rafał Miłecki482f0532011-05-18 02:06:36 +02005367static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005368{
5369 struct b43_wldev *wldev;
5370 struct b43_wl *wl;
5371
Michael Buesch3bf0a322008-05-22 16:32:16 +02005372 /* Do not cancel ieee80211-workqueue based work here.
5373 * See comment in b43_remove(). */
5374
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005375 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005376 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005377 b43_debugfs_remove_device(wldev);
5378 b43_wireless_core_detach(wldev);
5379 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005380 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005381 kfree(wldev);
5382}
5383
Rafał Miłecki482f0532011-05-18 02:06:36 +02005384static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005385{
5386 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005387 int err = -ENOMEM;
5388
Michael Buesche4d6b792007-09-18 15:39:42 -04005389 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5390 if (!wldev)
5391 goto out;
5392
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005393 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005394 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005395 wldev->wl = wl;
5396 b43_set_status(wldev, B43_STAT_UNINIT);
5397 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005398 INIT_LIST_HEAD(&wldev->list);
5399
5400 err = b43_wireless_core_attach(wldev);
5401 if (err)
5402 goto err_kfree_wldev;
5403
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005404 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005405 b43_debugfs_add_device(wldev);
5406
5407 out:
5408 return err;
5409
5410 err_kfree_wldev:
5411 kfree(wldev);
5412 return err;
5413}
5414
Michael Buesch9fc38452008-04-19 16:53:00 +02005415#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5416 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5417 (pdev->device == _device) && \
5418 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5419 (pdev->subsystem_device == _subdevice) )
5420
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005421#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005422static void b43_sprom_fixup(struct ssb_bus *bus)
5423{
Michael Buesch1855ba72008-04-18 20:51:41 +02005424 struct pci_dev *pdev;
5425
Michael Buesche4d6b792007-09-18 15:39:42 -04005426 /* boardflags workarounds */
5427 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005428 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005429 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005430 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005431 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005432 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005433 if (bus->bustype == SSB_BUSTYPE_PCI) {
5434 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005435 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005436 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005437 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005438 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005439 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005440 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5441 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005442 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5443 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005444}
5445
Rafał Miłecki482f0532011-05-18 02:06:36 +02005446static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005447{
5448 struct ieee80211_hw *hw = wl->hw;
5449
Rafał Miłecki482f0532011-05-18 02:06:36 +02005450 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005451 ieee80211_free_hw(hw);
5452}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005453#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005454
Rafał Miłeckid1507052011-07-05 23:54:07 +02005455static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005456{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005457 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005458 struct ieee80211_hw *hw;
5459 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005460 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005461 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005462
5463 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5464 if (!hw) {
5465 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005466 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005467 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005468 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005469
5470 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005471 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005472 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005473
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005474 hw->wiphy->interface_modes =
5475 BIT(NL80211_IFTYPE_AP) |
5476 BIT(NL80211_IFTYPE_MESH_POINT) |
5477 BIT(NL80211_IFTYPE_STATION) |
5478 BIT(NL80211_IFTYPE_WDS) |
5479 BIT(NL80211_IFTYPE_ADHOC);
5480
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005481 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5482
Oleksij Rempele64add22012-06-05 20:39:32 +02005483 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005484 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005485 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005486 if (is_valid_ether_addr(sprom->et1mac))
5487 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005488 else
Larry Finger95de2842007-11-09 16:57:18 -06005489 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005490
Michael Buesch403a3a12009-06-08 21:04:57 +02005491 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005492 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005493 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005494 spin_lock_init(&wl->hardirq_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005495 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005496 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005497 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005498
5499 /* Initialize queues and flags. */
5500 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5501 skb_queue_head_init(&wl->tx_queue[queue_num]);
5502 wl->tx_queue_stopped[queue_num] = 0;
5503 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005504
Rafał Miłecki2729df22011-07-18 22:45:58 +02005505 snprintf(chip_name, ARRAY_SIZE(chip_name),
5506 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5507 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5508 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005509 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005510}
5511
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005512#ifdef CONFIG_B43_BCMA
5513static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005514{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005515 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005516 struct b43_wl *wl;
5517 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005518
Rafał Miłecki89604002013-06-26 09:55:54 +02005519 if (!modparam_allhwsupport &&
5520 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5521 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5522 return -ENOTSUPP;
5523 }
5524
Rafał Miłecki397915c2011-07-06 19:03:46 +02005525 dev = b43_bus_dev_bcma_init(core);
5526 if (!dev)
5527 return -ENODEV;
5528
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005529 wl = b43_wireless_init(dev);
5530 if (IS_ERR(wl)) {
5531 err = PTR_ERR(wl);
5532 goto bcma_out;
5533 }
5534
5535 err = b43_one_core_attach(dev, wl);
5536 if (err)
5537 goto bcma_err_wireless_exit;
5538
Larry Finger6b6fa582012-03-08 22:27:46 -06005539 /* setup and start work to load firmware */
5540 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5541 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005542
5543bcma_out:
5544 return err;
5545
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005546bcma_err_wireless_exit:
5547 ieee80211_free_hw(wl->hw);
5548 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005549}
5550
5551static void b43_bcma_remove(struct bcma_device *core)
5552{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005553 struct b43_wldev *wldev = bcma_get_drvdata(core);
5554 struct b43_wl *wl = wldev->wl;
5555
5556 /* We must cancel any work here before unregistering from ieee80211,
5557 * as the ieee80211 unreg will destroy the workqueue. */
5558 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005559 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005560
Oleksij Rempele64add22012-06-05 20:39:32 +02005561 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005562 if (!wldev->fw.ucode.data)
5563 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005564 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005565 b43_leds_stop(wldev);
5566 ieee80211_unregister_hw(wl->hw);
5567 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005568
5569 b43_one_core_detach(wldev->dev);
5570
Larry Finger09164042014-01-12 15:11:37 -06005571 /* Unregister HW RNG driver */
5572 b43_rng_exit(wl);
5573
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005574 b43_leds_unregister(wl);
5575
5576 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005577}
5578
5579static struct bcma_driver b43_bcma_driver = {
5580 .name = KBUILD_MODNAME,
5581 .id_table = b43_bcma_tbl,
5582 .probe = b43_bcma_probe,
5583 .remove = b43_bcma_remove,
5584};
5585#endif
5586
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005587#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005588static
5589int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005590{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005591 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005592 struct b43_wl *wl;
5593 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005594
Rafał Miłecki482f0532011-05-18 02:06:36 +02005595 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005596 if (!dev)
5597 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005598
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005599 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005600 if (wl) {
5601 b43err(NULL, "Dual-core devices are not supported\n");
5602 err = -ENOTSUPP;
5603 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005604 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005605
5606 b43_sprom_fixup(sdev->bus);
5607
5608 wl = b43_wireless_init(dev);
5609 if (IS_ERR(wl)) {
5610 err = PTR_ERR(wl);
5611 goto err_ssb_kfree_dev;
5612 }
5613 ssb_set_devtypedata(sdev, wl);
5614 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5615
Michael Buesche4d6b792007-09-18 15:39:42 -04005616 err = b43_one_core_attach(dev, wl);
5617 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005618 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005619
Larry Finger6b6fa582012-03-08 22:27:46 -06005620 /* setup and start work to load firmware */
5621 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5622 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005623
Michael Buesche4d6b792007-09-18 15:39:42 -04005624 return err;
5625
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005626err_ssb_wireless_exit:
5627 b43_wireless_exit(dev, wl);
5628err_ssb_kfree_dev:
5629 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005630 return err;
5631}
5632
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005633static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005634{
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005635 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5636 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005637 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005638
Michael Buesch3bf0a322008-05-22 16:32:16 +02005639 /* We must cancel any work here before unregistering from ieee80211,
5640 * as the ieee80211 unreg will destroy the workqueue. */
5641 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005642 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005643
Michael Buesche4d6b792007-09-18 15:39:42 -04005644 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005645 if (!wldev->fw.ucode.data)
5646 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005647 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005648 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005649 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005650 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005651
Pavel Roskine61b52d2011-07-22 18:07:13 -04005652 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005653
Larry Finger09164042014-01-12 15:11:37 -06005654 /* Unregister HW RNG driver */
5655 b43_rng_exit(wl);
5656
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005657 b43_leds_unregister(wl);
5658 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005659}
5660
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005661static struct ssb_driver b43_ssb_driver = {
5662 .name = KBUILD_MODNAME,
5663 .id_table = b43_ssb_tbl,
5664 .probe = b43_ssb_probe,
5665 .remove = b43_ssb_remove,
5666};
5667#endif /* CONFIG_B43_SSB */
5668
Michael Buesche4d6b792007-09-18 15:39:42 -04005669/* Perform a hardware reset. This can be called from any context. */
5670void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5671{
5672 /* Must avoid requeueing, if we are in shutdown. */
5673 if (b43_status(dev) < B43_STAT_INITIALIZED)
5674 return;
5675 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005676 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005677}
5678
Michael Buesch26bc7832008-02-09 00:18:35 +01005679static void b43_print_driverinfo(void)
5680{
5681 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005682 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005683
5684#ifdef CONFIG_B43_PCI_AUTOSELECT
5685 feat_pci = "P";
5686#endif
5687#ifdef CONFIG_B43_PCMCIA
5688 feat_pcmcia = "M";
5689#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005690#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005691 feat_nphy = "N";
5692#endif
5693#ifdef CONFIG_B43_LEDS
5694 feat_leds = "L";
5695#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005696#ifdef CONFIG_B43_SDIO
5697 feat_sdio = "S";
5698#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005699 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005700 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005701 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005702 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005703}
5704
Michael Buesche4d6b792007-09-18 15:39:42 -04005705static int __init b43_init(void)
5706{
5707 int err;
5708
5709 b43_debugfs_init();
5710 err = b43_pcmcia_init();
5711 if (err)
5712 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005713 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005714 if (err)
5715 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005716#ifdef CONFIG_B43_BCMA
5717 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005718 if (err)
5719 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005720#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005721#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005722 err = ssb_driver_register(&b43_ssb_driver);
5723 if (err)
5724 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005725#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005726 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005727
5728 return err;
5729
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005730#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005731err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005732#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005733#ifdef CONFIG_B43_BCMA
5734 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005735err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005736#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005737 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005738err_pcmcia_exit:
5739 b43_pcmcia_exit();
5740err_dfs_exit:
5741 b43_debugfs_exit();
5742 return err;
5743}
5744
5745static void __exit b43_exit(void)
5746{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005747#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005748 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005749#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005750#ifdef CONFIG_B43_BCMA
5751 bcma_driver_unregister(&b43_bcma_driver);
5752#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005753 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005754 b43_pcmcia_exit();
5755 b43_debugfs_exit();
5756}
5757
5758module_init(b43_init)
5759module_exit(b43_exit)