blob: 3740b76162f970de58c51979789ec921d1a25eba [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),
125 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, 0x1D, BCMA_ANY_CLASS),
126 BCMA_CORETABLE_END
127};
128MODULE_DEVICE_TABLE(bcma, b43_bcma_tbl);
129#endif
130
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200131#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -0400132static const struct ssb_device_id b43_ssb_tbl[] = {
133 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
134 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
135 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
136 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
137 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
Michael Bueschd5c71e42008-01-04 17:06:29 +0100138 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
Rafał Miłecki003d6d22010-01-15 12:10:53 +0100139 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 12),
Larry Finger013978b2007-11-26 10:29:47 -0600140 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
Michael Buesch6b1c7c62008-12-25 00:39:28 +0100141 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
Johannes Berg92d61282008-12-24 12:44:09 +0100142 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
Michael Buesche4d6b792007-09-18 15:39:42 -0400143 SSB_DEVTABLE_END
144};
Michael Buesche4d6b792007-09-18 15:39:42 -0400145MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +0200146#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400147
148/* Channel and ratetables are shared for all devices.
149 * They can't be const, because ieee80211 puts some precalculated
150 * data in there. This data is the same for all devices, so we don't
151 * get concurrency issues */
152#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +0100153 { \
154 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
155 .hw_value = (_rateid), \
156 .flags = (_flags), \
Michael Buesche4d6b792007-09-18 15:39:42 -0400157 }
Johannes Berg8318d782008-01-24 19:38:38 +0100158
159/*
160 * NOTE: When changing this, sync with xmit.c's
161 * b43_plcp_get_bitrate_idx_* functions!
162 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400163static struct ieee80211_rate __b43_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100164 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
165 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
166 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
167 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
168 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
169 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
170 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
171 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
172 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
173 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
174 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
175 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400176};
177
178#define b43_a_ratetable (__b43_ratetable + 4)
179#define b43_a_ratetable_size 8
180#define b43_b_ratetable (__b43_ratetable + 0)
181#define b43_b_ratetable_size 4
182#define b43_g_ratetable (__b43_ratetable + 0)
183#define b43_g_ratetable_size 12
184
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200185#define CHAN2G(_channel, _freq, _flags) { \
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100186 .band = IEEE80211_BAND_2GHZ, \
187 .center_freq = (_freq), \
188 .hw_value = (_channel), \
189 .flags = (_flags), \
190 .max_antenna_gain = 0, \
191 .max_power = 30, \
192}
Michael Buesch96c755a2008-01-06 00:09:46 +0100193static struct ieee80211_channel b43_2ghz_chantable[] = {
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200194 CHAN2G(1, 2412, 0),
195 CHAN2G(2, 2417, 0),
196 CHAN2G(3, 2422, 0),
197 CHAN2G(4, 2427, 0),
198 CHAN2G(5, 2432, 0),
199 CHAN2G(6, 2437, 0),
200 CHAN2G(7, 2442, 0),
201 CHAN2G(8, 2447, 0),
202 CHAN2G(9, 2452, 0),
203 CHAN2G(10, 2457, 0),
204 CHAN2G(11, 2462, 0),
205 CHAN2G(12, 2467, 0),
206 CHAN2G(13, 2472, 0),
207 CHAN2G(14, 2484, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100208};
Rafał Miłeckie9cdcb72014-05-21 08:44:19 +0200209#undef CHAN2G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100210
Rafał Miłecki91211732014-05-21 08:44:20 +0200211#define CHAN4G(_channel, _flags) { \
212 .band = IEEE80211_BAND_5GHZ, \
213 .center_freq = 4000 + (5 * (_channel)), \
214 .hw_value = (_channel), \
215 .flags = (_flags), \
216 .max_antenna_gain = 0, \
217 .max_power = 30, \
218}
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100219#define CHAN5G(_channel, _flags) { \
220 .band = IEEE80211_BAND_5GHZ, \
221 .center_freq = 5000 + (5 * (_channel)), \
222 .hw_value = (_channel), \
223 .flags = (_flags), \
224 .max_antenna_gain = 0, \
225 .max_power = 30, \
226}
227static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
Rafał Miłecki91211732014-05-21 08:44:20 +0200228 CHAN4G(184, 0), CHAN4G(186, 0),
229 CHAN4G(188, 0), CHAN4G(190, 0),
230 CHAN4G(192, 0), CHAN4G(194, 0),
231 CHAN4G(196, 0), CHAN4G(198, 0),
232 CHAN4G(200, 0), CHAN4G(202, 0),
233 CHAN4G(204, 0), CHAN4G(206, 0),
234 CHAN4G(208, 0), CHAN4G(210, 0),
235 CHAN4G(212, 0), CHAN4G(214, 0),
236 CHAN4G(216, 0), CHAN4G(218, 0),
237 CHAN4G(220, 0), CHAN4G(222, 0),
238 CHAN4G(224, 0), CHAN4G(226, 0),
239 CHAN4G(228, 0),
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100240 CHAN5G(32, 0), CHAN5G(34, 0),
241 CHAN5G(36, 0), CHAN5G(38, 0),
242 CHAN5G(40, 0), CHAN5G(42, 0),
243 CHAN5G(44, 0), CHAN5G(46, 0),
244 CHAN5G(48, 0), CHAN5G(50, 0),
245 CHAN5G(52, 0), CHAN5G(54, 0),
246 CHAN5G(56, 0), CHAN5G(58, 0),
247 CHAN5G(60, 0), CHAN5G(62, 0),
248 CHAN5G(64, 0), CHAN5G(66, 0),
249 CHAN5G(68, 0), CHAN5G(70, 0),
250 CHAN5G(72, 0), CHAN5G(74, 0),
251 CHAN5G(76, 0), CHAN5G(78, 0),
252 CHAN5G(80, 0), CHAN5G(82, 0),
253 CHAN5G(84, 0), CHAN5G(86, 0),
254 CHAN5G(88, 0), CHAN5G(90, 0),
255 CHAN5G(92, 0), CHAN5G(94, 0),
256 CHAN5G(96, 0), CHAN5G(98, 0),
257 CHAN5G(100, 0), CHAN5G(102, 0),
258 CHAN5G(104, 0), CHAN5G(106, 0),
259 CHAN5G(108, 0), CHAN5G(110, 0),
260 CHAN5G(112, 0), CHAN5G(114, 0),
261 CHAN5G(116, 0), CHAN5G(118, 0),
262 CHAN5G(120, 0), CHAN5G(122, 0),
263 CHAN5G(124, 0), CHAN5G(126, 0),
264 CHAN5G(128, 0), CHAN5G(130, 0),
265 CHAN5G(132, 0), CHAN5G(134, 0),
266 CHAN5G(136, 0), CHAN5G(138, 0),
267 CHAN5G(140, 0), CHAN5G(142, 0),
268 CHAN5G(144, 0), CHAN5G(145, 0),
269 CHAN5G(146, 0), CHAN5G(147, 0),
270 CHAN5G(148, 0), CHAN5G(149, 0),
271 CHAN5G(150, 0), CHAN5G(151, 0),
272 CHAN5G(152, 0), CHAN5G(153, 0),
273 CHAN5G(154, 0), CHAN5G(155, 0),
274 CHAN5G(156, 0), CHAN5G(157, 0),
275 CHAN5G(158, 0), CHAN5G(159, 0),
276 CHAN5G(160, 0), CHAN5G(161, 0),
277 CHAN5G(162, 0), CHAN5G(163, 0),
278 CHAN5G(164, 0), CHAN5G(165, 0),
279 CHAN5G(166, 0), CHAN5G(168, 0),
280 CHAN5G(170, 0), CHAN5G(172, 0),
281 CHAN5G(174, 0), CHAN5G(176, 0),
282 CHAN5G(178, 0), CHAN5G(180, 0),
Rafał Miłecki91211732014-05-21 08:44:20 +0200283 CHAN5G(182, 0),
Michael Buesche4d6b792007-09-18 15:39:42 -0400284};
285
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100286static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
287 CHAN5G(34, 0), CHAN5G(36, 0),
288 CHAN5G(38, 0), CHAN5G(40, 0),
289 CHAN5G(42, 0), CHAN5G(44, 0),
290 CHAN5G(46, 0), CHAN5G(48, 0),
291 CHAN5G(52, 0), CHAN5G(56, 0),
292 CHAN5G(60, 0), CHAN5G(64, 0),
293 CHAN5G(100, 0), CHAN5G(104, 0),
294 CHAN5G(108, 0), CHAN5G(112, 0),
295 CHAN5G(116, 0), CHAN5G(120, 0),
296 CHAN5G(124, 0), CHAN5G(128, 0),
297 CHAN5G(132, 0), CHAN5G(136, 0),
298 CHAN5G(140, 0), CHAN5G(149, 0),
299 CHAN5G(153, 0), CHAN5G(157, 0),
300 CHAN5G(161, 0), CHAN5G(165, 0),
301 CHAN5G(184, 0), CHAN5G(188, 0),
302 CHAN5G(192, 0), CHAN5G(196, 0),
303 CHAN5G(200, 0), CHAN5G(204, 0),
304 CHAN5G(208, 0), CHAN5G(212, 0),
305 CHAN5G(216, 0),
306};
Rafał Miłecki91211732014-05-21 08:44:20 +0200307#undef CHAN4G
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100308#undef CHAN5G
309
310static struct ieee80211_supported_band b43_band_5GHz_nphy = {
311 .band = IEEE80211_BAND_5GHZ,
312 .channels = b43_5ghz_nphy_chantable,
313 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
314 .bitrates = b43_a_ratetable,
315 .n_bitrates = b43_a_ratetable_size,
Michael Buesche4d6b792007-09-18 15:39:42 -0400316};
Johannes Berg8318d782008-01-24 19:38:38 +0100317
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100318static struct ieee80211_supported_band b43_band_5GHz_aphy = {
319 .band = IEEE80211_BAND_5GHZ,
320 .channels = b43_5ghz_aphy_chantable,
321 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
322 .bitrates = b43_a_ratetable,
323 .n_bitrates = b43_a_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100324};
Michael Buesche4d6b792007-09-18 15:39:42 -0400325
Johannes Berg8318d782008-01-24 19:38:38 +0100326static struct ieee80211_supported_band b43_band_2GHz = {
Michael Bueschbb1eeff2008-02-09 12:08:58 +0100327 .band = IEEE80211_BAND_2GHZ,
328 .channels = b43_2ghz_chantable,
329 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
330 .bitrates = b43_g_ratetable,
331 .n_bitrates = b43_g_ratetable_size,
Johannes Berg8318d782008-01-24 19:38:38 +0100332};
333
Michael Buesche4d6b792007-09-18 15:39:42 -0400334static void b43_wireless_core_exit(struct b43_wldev *dev);
335static int b43_wireless_core_init(struct b43_wldev *dev);
Michael Buesch36dbd952009-09-04 22:51:29 +0200336static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400337static int b43_wireless_core_start(struct b43_wldev *dev);
Felix Fietkau2a190322011-08-10 13:50:30 -0600338static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
339 struct ieee80211_vif *vif,
340 struct ieee80211_bss_conf *conf,
341 u32 changed);
Michael Buesche4d6b792007-09-18 15:39:42 -0400342
343static int b43_ratelimit(struct b43_wl *wl)
344{
345 if (!wl || !wl->current_dev)
346 return 1;
347 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
348 return 1;
349 /* We are up and running.
350 * Ratelimit the messages to avoid DoS over the net. */
351 return net_ratelimit();
352}
353
354void b43info(struct b43_wl *wl, const char *fmt, ...)
355{
Joe Perches5b736d42010-11-09 16:35:18 -0800356 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400357 va_list args;
358
Michael Buesch060210f2009-01-25 15:49:59 +0100359 if (b43_modparam_verbose < B43_VERBOSITY_INFO)
360 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400361 if (!b43_ratelimit(wl))
362 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800363
Michael Buesche4d6b792007-09-18 15:39:42 -0400364 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800365
366 vaf.fmt = fmt;
367 vaf.va = &args;
368
369 printk(KERN_INFO "b43-%s: %pV",
370 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
371
Michael Buesche4d6b792007-09-18 15:39:42 -0400372 va_end(args);
373}
374
375void b43err(struct b43_wl *wl, const char *fmt, ...)
376{
Joe Perches5b736d42010-11-09 16:35:18 -0800377 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400378 va_list args;
379
Michael Buesch060210f2009-01-25 15:49:59 +0100380 if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
381 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400382 if (!b43_ratelimit(wl))
383 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800384
Michael Buesche4d6b792007-09-18 15:39:42 -0400385 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800386
387 vaf.fmt = fmt;
388 vaf.va = &args;
389
390 printk(KERN_ERR "b43-%s ERROR: %pV",
391 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
392
Michael Buesche4d6b792007-09-18 15:39:42 -0400393 va_end(args);
394}
395
396void b43warn(struct b43_wl *wl, const char *fmt, ...)
397{
Joe Perches5b736d42010-11-09 16:35:18 -0800398 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400399 va_list args;
400
Michael Buesch060210f2009-01-25 15:49:59 +0100401 if (b43_modparam_verbose < B43_VERBOSITY_WARN)
402 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 if (!b43_ratelimit(wl))
404 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800405
Michael Buesche4d6b792007-09-18 15:39:42 -0400406 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800407
408 vaf.fmt = fmt;
409 vaf.va = &args;
410
411 printk(KERN_WARNING "b43-%s warning: %pV",
412 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
413
Michael Buesche4d6b792007-09-18 15:39:42 -0400414 va_end(args);
415}
416
Michael Buesche4d6b792007-09-18 15:39:42 -0400417void b43dbg(struct b43_wl *wl, const char *fmt, ...)
418{
Joe Perches5b736d42010-11-09 16:35:18 -0800419 struct va_format vaf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400420 va_list args;
421
Michael Buesch060210f2009-01-25 15:49:59 +0100422 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
423 return;
Joe Perches5b736d42010-11-09 16:35:18 -0800424
Michael Buesche4d6b792007-09-18 15:39:42 -0400425 va_start(args, fmt);
Joe Perches5b736d42010-11-09 16:35:18 -0800426
427 vaf.fmt = fmt;
428 vaf.va = &args;
429
430 printk(KERN_DEBUG "b43-%s debug: %pV",
431 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
432
Michael Buesche4d6b792007-09-18 15:39:42 -0400433 va_end(args);
434}
Michael Buesche4d6b792007-09-18 15:39:42 -0400435
436static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
437{
438 u32 macctl;
439
440 B43_WARN_ON(offset % 4 != 0);
441
442 macctl = b43_read32(dev, B43_MMIO_MACCTL);
443 if (macctl & B43_MACCTL_BE)
444 val = swab32(val);
445
446 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
447 mmiowb();
448 b43_write32(dev, B43_MMIO_RAM_DATA, val);
449}
450
Michael Buesch280d0e12007-12-26 18:26:17 +0100451static inline void b43_shm_control_word(struct b43_wldev *dev,
452 u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400453{
454 u32 control;
455
456 /* "offset" is the WORD offset. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400457 control = routing;
458 control <<= 16;
459 control |= offset;
460 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
461}
462
Michael Buesch69eddc82009-09-04 22:57:26 +0200463u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400464{
465 u32 ret;
466
467 if (routing == B43_SHM_SHARED) {
468 B43_WARN_ON(offset & 0x0001);
469 if (offset & 0x0003) {
470 /* Unaligned access */
471 b43_shm_control_word(dev, routing, offset >> 2);
472 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
Michael Buesche4d6b792007-09-18 15:39:42 -0400473 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200474 ret |= ((u32)b43_read16(dev, B43_MMIO_SHM_DATA)) << 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400475
Michael Buesch280d0e12007-12-26 18:26:17 +0100476 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400477 }
478 offset >>= 2;
479 }
480 b43_shm_control_word(dev, routing, offset);
481 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100482out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200483 return ret;
484}
485
Michael Buesch69eddc82009-09-04 22:57:26 +0200486u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
Michael Buesche4d6b792007-09-18 15:39:42 -0400487{
488 u16 ret;
489
490 if (routing == B43_SHM_SHARED) {
491 B43_WARN_ON(offset & 0x0001);
492 if (offset & 0x0003) {
493 /* Unaligned access */
494 b43_shm_control_word(dev, routing, offset >> 2);
495 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
496
Michael Buesch280d0e12007-12-26 18:26:17 +0100497 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -0400498 }
499 offset >>= 2;
500 }
501 b43_shm_control_word(dev, routing, offset);
502 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
Michael Buesch280d0e12007-12-26 18:26:17 +0100503out:
Michael Buesch6bbc3212008-06-19 19:33:51 +0200504 return ret;
505}
506
Michael Buesch69eddc82009-09-04 22:57:26 +0200507void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400508{
509 if (routing == B43_SHM_SHARED) {
510 B43_WARN_ON(offset & 0x0001);
511 if (offset & 0x0003) {
512 /* Unaligned access */
513 b43_shm_control_word(dev, routing, offset >> 2);
Michael Buesche4d6b792007-09-18 15:39:42 -0400514 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200515 value & 0xFFFF);
Michael Buesche4d6b792007-09-18 15:39:42 -0400516 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
Michael Bueschf62ae6c2009-07-31 20:51:41 +0200517 b43_write16(dev, B43_MMIO_SHM_DATA,
518 (value >> 16) & 0xFFFF);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200519 return;
Michael Buesche4d6b792007-09-18 15:39:42 -0400520 }
521 offset >>= 2;
522 }
523 b43_shm_control_word(dev, routing, offset);
Michael Buesche4d6b792007-09-18 15:39:42 -0400524 b43_write32(dev, B43_MMIO_SHM_DATA, value);
Michael Buesch6bbc3212008-06-19 19:33:51 +0200525}
526
Michael Buesch69eddc82009-09-04 22:57:26 +0200527void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
Michael Buesch6bbc3212008-06-19 19:33:51 +0200528{
529 if (routing == B43_SHM_SHARED) {
530 B43_WARN_ON(offset & 0x0001);
531 if (offset & 0x0003) {
532 /* Unaligned access */
533 b43_shm_control_word(dev, routing, offset >> 2);
534 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
535 return;
536 }
537 offset >>= 2;
538 }
539 b43_shm_control_word(dev, routing, offset);
540 b43_write16(dev, B43_MMIO_SHM_DATA, value);
541}
542
Michael Buesche4d6b792007-09-18 15:39:42 -0400543/* Read HostFlags */
John Daiker99da1852009-02-24 02:16:42 -0800544u64 b43_hf_read(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400545{
Michael Buesch35f0d352008-02-13 14:31:08 +0100546 u64 ret;
Michael Buesche4d6b792007-09-18 15:39:42 -0400547
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200548 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400549 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200550 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2);
Michael Buesch35f0d352008-02-13 14:31:08 +0100551 ret <<= 16;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200552 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1);
Michael Buesche4d6b792007-09-18 15:39:42 -0400553
554 return ret;
555}
556
557/* Write HostFlags */
Michael Buesch35f0d352008-02-13 14:31:08 +0100558void b43_hf_write(struct b43_wldev *dev, u64 value)
Michael Buesche4d6b792007-09-18 15:39:42 -0400559{
Michael Buesch35f0d352008-02-13 14:31:08 +0100560 u16 lo, mi, hi;
561
562 lo = (value & 0x00000000FFFFULL);
563 mi = (value & 0x0000FFFF0000ULL) >> 16;
564 hi = (value & 0xFFFF00000000ULL) >> 32;
Rafał Miłecki6e6a2cd2012-07-25 16:58:38 +0200565 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1, lo);
566 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF2, mi);
567 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF3, hi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400568}
569
Michael Buesch403a3a12009-06-08 21:04:57 +0200570/* Read the firmware capabilities bitmask (Opensource firmware only) */
571static u16 b43_fwcapa_read(struct b43_wldev *dev)
572{
573 B43_WARN_ON(!dev->fw.opensource);
574 return b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_FWCAPA);
575}
576
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100577void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
Michael Buesche4d6b792007-09-18 15:39:42 -0400578{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100579 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400580
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200581 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400582
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100583 /* The hardware guarantees us an atomic read, if we
584 * read the low register first. */
585 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
586 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400587
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100588 *tsf = high;
589 *tsf <<= 32;
590 *tsf |= low;
Michael Buesche4d6b792007-09-18 15:39:42 -0400591}
592
593static void b43_time_lock(struct b43_wldev *dev)
594{
Rafał Miłecki50566352012-01-02 19:31:21 +0100595 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_TBTTHOLD);
Michael Buesche4d6b792007-09-18 15:39:42 -0400596 /* Commit the write */
597 b43_read32(dev, B43_MMIO_MACCTL);
598}
599
600static void b43_time_unlock(struct b43_wldev *dev)
601{
Rafał Miłecki50566352012-01-02 19:31:21 +0100602 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_TBTTHOLD, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400603 /* Commit the write */
604 b43_read32(dev, B43_MMIO_MACCTL);
605}
606
607static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
608{
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100609 u32 low, high;
Michael Buesche4d6b792007-09-18 15:39:42 -0400610
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200611 B43_WARN_ON(dev->dev->core_rev < 3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400612
Michael Buesch3ebbbb52008-12-19 22:51:57 +0100613 low = tsf;
614 high = (tsf >> 32);
615 /* The hardware guarantees us an atomic write, if we
616 * write the low register first. */
617 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
618 mmiowb();
619 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
620 mmiowb();
Michael Buesche4d6b792007-09-18 15:39:42 -0400621}
622
623void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
624{
625 b43_time_lock(dev);
626 b43_tsf_write_locked(dev, tsf);
627 b43_time_unlock(dev);
628}
629
630static
John Daiker99da1852009-02-24 02:16:42 -0800631void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
Michael Buesche4d6b792007-09-18 15:39:42 -0400632{
633 static const u8 zero_addr[ETH_ALEN] = { 0 };
634 u16 data;
635
636 if (!mac)
637 mac = zero_addr;
638
639 offset |= 0x0020;
640 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
641
642 data = mac[0];
643 data |= mac[1] << 8;
644 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
645 data = mac[2];
646 data |= mac[3] << 8;
647 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
648 data = mac[4];
649 data |= mac[5] << 8;
650 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
651}
652
653static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
654{
655 const u8 *mac;
656 const u8 *bssid;
657 u8 mac_bssid[ETH_ALEN * 2];
658 int i;
659 u32 tmp;
660
661 bssid = dev->wl->bssid;
662 mac = dev->wl->mac_addr;
663
664 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
665
666 memcpy(mac_bssid, mac, ETH_ALEN);
667 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
668
669 /* Write our MAC address and BSSID to template ram */
670 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
671 tmp = (u32) (mac_bssid[i + 0]);
672 tmp |= (u32) (mac_bssid[i + 1]) << 8;
673 tmp |= (u32) (mac_bssid[i + 2]) << 16;
674 tmp |= (u32) (mac_bssid[i + 3]) << 24;
675 b43_ram_write(dev, 0x20 + i, tmp);
676 }
677}
678
Johannes Berg4150c572007-09-17 01:29:23 -0400679static void b43_upload_card_macaddress(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400680{
Michael Buesche4d6b792007-09-18 15:39:42 -0400681 b43_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400682 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400683}
684
685static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
686{
687 /* slot_time is in usec. */
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600688 /* This test used to exit for all but a G PHY. */
689 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Michael Buesche4d6b792007-09-18 15:39:42 -0400690 return;
Larry Fingerb6c3f5b2010-02-02 10:08:19 -0600691 b43_write16(dev, B43_MMIO_IFSSLOT, 510 + slot_time);
692 /* Shared memory location 0x0010 is the slot time and should be
693 * set to slot_time; however, this register is initially 0 and changing
694 * the value adversely affects the transmit rate for BCM4311
695 * devices. Until this behavior is unterstood, delete this step
696 *
697 * b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
698 */
Michael Buesche4d6b792007-09-18 15:39:42 -0400699}
700
701static void b43_short_slot_timing_enable(struct b43_wldev *dev)
702{
703 b43_set_slot_time(dev, 9);
Michael Buesche4d6b792007-09-18 15:39:42 -0400704}
705
706static void b43_short_slot_timing_disable(struct b43_wldev *dev)
707{
708 b43_set_slot_time(dev, 20);
Michael Buesche4d6b792007-09-18 15:39:42 -0400709}
710
Michael Buesche4d6b792007-09-18 15:39:42 -0400711/* DummyTransmission function, as documented on
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200712 * http://bcm-v4.sipsolutions.net/802.11/DummyTransmission
Michael Buesche4d6b792007-09-18 15:39:42 -0400713 */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200714void b43_dummy_transmission(struct b43_wldev *dev, bool ofdm, bool pa_on)
Michael Buesche4d6b792007-09-18 15:39:42 -0400715{
716 struct b43_phy *phy = &dev->phy;
717 unsigned int i, max_loop;
718 u16 value;
719 u32 buffer[5] = {
720 0x00000000,
721 0x00D40000,
722 0x00000000,
723 0x01000000,
724 0x00000000,
725 };
726
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200727 if (ofdm) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400728 max_loop = 0x1E;
729 buffer[0] = 0x000201CC;
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200730 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -0400731 max_loop = 0xFA;
732 buffer[0] = 0x000B846E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400733 }
734
735 for (i = 0; i < 5; i++)
736 b43_ram_write(dev, i * 4, buffer[i]);
737
Rafał Miłecki7955d872011-09-21 21:44:13 +0200738 b43_write16(dev, B43_MMIO_XMTSEL, 0x0000);
739
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200740 if (dev->dev->core_rev < 11)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200741 b43_write16(dev, B43_MMIO_WEPCTL, 0x0000);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200742 else
Rafał Miłecki7955d872011-09-21 21:44:13 +0200743 b43_write16(dev, B43_MMIO_WEPCTL, 0x0100);
744
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200745 value = (ofdm ? 0x41 : 0x40);
Rafał Miłecki7955d872011-09-21 21:44:13 +0200746 b43_write16(dev, B43_MMIO_TXE0_PHYCTL, value);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200747 if (phy->type == B43_PHYTYPE_N || phy->type == B43_PHYTYPE_LP ||
748 phy->type == B43_PHYTYPE_LCN)
Rafał Miłecki7955d872011-09-21 21:44:13 +0200749 b43_write16(dev, B43_MMIO_TXE0_PHYCTL1, 0x1A02);
750
751 b43_write16(dev, B43_MMIO_TXE0_WM_0, 0x0000);
752 b43_write16(dev, B43_MMIO_TXE0_WM_1, 0x0000);
753
754 b43_write16(dev, B43_MMIO_XMTTPLATETXPTR, 0x0000);
755 b43_write16(dev, B43_MMIO_XMTTXCNT, 0x0014);
756 b43_write16(dev, B43_MMIO_XMTSEL, 0x0826);
757 b43_write16(dev, B43_MMIO_TXE0_CTL, 0x0000);
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200758
759 if (!pa_on && phy->type == B43_PHYTYPE_N)
760 ; /*b43_nphy_pa_override(dev, false) */
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200761
762 switch (phy->type) {
763 case B43_PHYTYPE_N:
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200764 case B43_PHYTYPE_LCN:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200765 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x00D0);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200766 break;
767 case B43_PHYTYPE_LP:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200768 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0050);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200769 break;
770 default:
Rafał Miłecki7955d872011-09-21 21:44:13 +0200771 b43_write16(dev, B43_MMIO_TXE0_AUX, 0x0030);
Gábor Stefanik2f19c282009-08-13 16:51:51 +0200772 }
Rafał Miłecki93dbd822011-09-21 21:44:14 +0200773 b43_read16(dev, B43_MMIO_TXE0_AUX);
Michael Buesche4d6b792007-09-18 15:39:42 -0400774
775 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
776 b43_radio_write16(dev, 0x0051, 0x0017);
777 for (i = 0x00; i < max_loop; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200778 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400779 if (value & 0x0080)
780 break;
781 udelay(10);
782 }
783 for (i = 0x00; i < 0x0A; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200784 value = b43_read16(dev, B43_MMIO_TXE0_STATUS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400785 if (value & 0x0400)
786 break;
787 udelay(10);
788 }
Larry Finger1d280dd2008-09-29 14:19:29 -0500789 for (i = 0x00; i < 0x19; i++) {
Rafał Miłecki7955d872011-09-21 21:44:13 +0200790 value = b43_read16(dev, B43_MMIO_IFSSTAT);
Michael Buesche4d6b792007-09-18 15:39:42 -0400791 if (!(value & 0x0100))
792 break;
793 udelay(10);
794 }
795 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
796 b43_radio_write16(dev, 0x0051, 0x0037);
797}
798
799static void key_write(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -0800800 u8 index, u8 algorithm, const u8 *key)
Michael Buesche4d6b792007-09-18 15:39:42 -0400801{
802 unsigned int i;
803 u32 offset;
804 u16 value;
805 u16 kidx;
806
807 /* Key index/algo block */
808 kidx = b43_kidx_to_fw(dev, index);
809 value = ((kidx << 4) | algorithm);
810 b43_shm_write16(dev, B43_SHM_SHARED,
811 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
812
813 /* Write the key to the Key Table Pointer offset */
814 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
815 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
816 value = key[i];
817 value |= (u16) (key[i + 1]) << 8;
818 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
819 }
820}
821
John Daiker99da1852009-02-24 02:16:42 -0800822static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400823{
824 u32 addrtmp[2] = { 0, 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200825 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400826
827 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200828 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400829
Michael Buesch66d2d082009-08-06 10:36:50 +0200830 B43_WARN_ON(index < pairwise_keys_start);
831 /* We have four default TX keys and possibly four default RX keys.
Michael Buesche4d6b792007-09-18 15:39:42 -0400832 * Physical mac 0 is mapped to physical key 4 or 8, depending
833 * on the firmware version.
834 * So we must adjust the index here.
835 */
Michael Buesch66d2d082009-08-06 10:36:50 +0200836 index -= pairwise_keys_start;
837 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
Michael Buesche4d6b792007-09-18 15:39:42 -0400838
839 if (addr) {
840 addrtmp[0] = addr[0];
841 addrtmp[0] |= ((u32) (addr[1]) << 8);
842 addrtmp[0] |= ((u32) (addr[2]) << 16);
843 addrtmp[0] |= ((u32) (addr[3]) << 24);
844 addrtmp[1] = addr[4];
845 addrtmp[1] |= ((u32) (addr[5]) << 8);
846 }
847
Michael Buesch66d2d082009-08-06 10:36:50 +0200848 /* Receive match transmitter address (RCMTA) mechanism */
849 b43_shm_write32(dev, B43_SHM_RCMTA,
850 (index * 2) + 0, addrtmp[0]);
851 b43_shm_write16(dev, B43_SHM_RCMTA,
852 (index * 2) + 1, addrtmp[1]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400853}
854
gregor kowski035d0242009-08-19 22:35:45 +0200855/* The ucode will use phase1 key with TEK key to decrypt rx packets.
856 * When a packet is received, the iv32 is checked.
857 * - if it doesn't the packet is returned without modification (and software
858 * decryption can be done). That's what happen when iv16 wrap.
859 * - if it does, the rc4 key is computed, and decryption is tried.
860 * Either it will success and B43_RX_MAC_DEC is returned,
861 * either it fails and B43_RX_MAC_DEC|B43_RX_MAC_DECERR is returned
862 * and the packet is not usable (it got modified by the ucode).
863 * So in order to never have B43_RX_MAC_DECERR, we should provide
864 * a iv32 and phase1key that match. Because we drop packets in case of
865 * B43_RX_MAC_DECERR, if we have a correct iv32 but a wrong phase1key, all
866 * packets will be lost without higher layer knowing (ie no resync possible
867 * until next wrap).
868 *
869 * NOTE : this should support 50 key like RCMTA because
870 * (B43_SHM_SH_KEYIDXBLOCK - B43_SHM_SH_TKIPTSCTTAK)/14 = 50
871 */
872static void rx_tkip_phase1_write(struct b43_wldev *dev, u8 index, u32 iv32,
873 u16 *phase1key)
874{
875 unsigned int i;
876 u32 offset;
877 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
878
879 if (!modparam_hwtkip)
880 return;
881
882 if (b43_new_kidx_api(dev))
883 pairwise_keys_start = B43_NR_GROUP_KEYS;
884
885 B43_WARN_ON(index < pairwise_keys_start);
886 /* We have four default TX keys and possibly four default RX keys.
887 * Physical mac 0 is mapped to physical key 4 or 8, depending
888 * on the firmware version.
889 * So we must adjust the index here.
890 */
891 index -= pairwise_keys_start;
892 B43_WARN_ON(index >= B43_NR_PAIRWISE_KEYS);
893
894 if (b43_debug(dev, B43_DBG_KEYS)) {
895 b43dbg(dev->wl, "rx_tkip_phase1_write : idx 0x%x, iv32 0x%x\n",
896 index, iv32);
897 }
898 /* Write the key to the RX tkip shared mem */
899 offset = B43_SHM_SH_TKIPTSCTTAK + index * (10 + 4);
900 for (i = 0; i < 10; i += 2) {
901 b43_shm_write16(dev, B43_SHM_SHARED, offset + i,
902 phase1key ? phase1key[i / 2] : 0);
903 }
904 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, iv32);
905 b43_shm_write16(dev, B43_SHM_SHARED, offset + i + 2, iv32 >> 16);
906}
907
908static void b43_op_update_tkip_key(struct ieee80211_hw *hw,
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100909 struct ieee80211_vif *vif,
910 struct ieee80211_key_conf *keyconf,
911 struct ieee80211_sta *sta,
912 u32 iv32, u16 *phase1key)
gregor kowski035d0242009-08-19 22:35:45 +0200913{
914 struct b43_wl *wl = hw_to_b43_wl(hw);
915 struct b43_wldev *dev;
916 int index = keyconf->hw_key_idx;
917
918 if (B43_WARN_ON(!modparam_hwtkip))
919 return;
920
Michael Buesch96869a32010-01-24 13:13:32 +0100921 /* This is only called from the RX path through mac80211, where
922 * our mutex is already locked. */
923 B43_WARN_ON(!mutex_is_locked(&wl->mutex));
gregor kowski035d0242009-08-19 22:35:45 +0200924 dev = wl->current_dev;
Michael Buesch96869a32010-01-24 13:13:32 +0100925 B43_WARN_ON(!dev || b43_status(dev) < B43_STAT_INITIALIZED);
gregor kowski035d0242009-08-19 22:35:45 +0200926
927 keymac_write(dev, index, NULL); /* First zero out mac to avoid race */
928
929 rx_tkip_phase1_write(dev, index, iv32, phase1key);
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100930 /* only pairwise TKIP keys are supported right now */
931 if (WARN_ON(!sta))
Michael Buesch96869a32010-01-24 13:13:32 +0100932 return;
Johannes Bergb3fbdcf2010-01-21 11:40:47 +0100933 keymac_write(dev, index, sta->addr);
gregor kowski035d0242009-08-19 22:35:45 +0200934}
935
Michael Buesche4d6b792007-09-18 15:39:42 -0400936static void do_key_write(struct b43_wldev *dev,
937 u8 index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800938 const u8 *key, size_t key_len, const u8 *mac_addr)
Michael Buesche4d6b792007-09-18 15:39:42 -0400939{
940 u8 buf[B43_SEC_KEYSIZE] = { 0, };
Michael Buesch66d2d082009-08-06 10:36:50 +0200941 u8 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
Michael Buesche4d6b792007-09-18 15:39:42 -0400942
943 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +0200944 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400945
Michael Buesch66d2d082009-08-06 10:36:50 +0200946 B43_WARN_ON(index >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400947 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
948
Michael Buesch66d2d082009-08-06 10:36:50 +0200949 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400950 keymac_write(dev, index, NULL); /* First zero out mac. */
gregor kowski035d0242009-08-19 22:35:45 +0200951 if (algorithm == B43_SEC_ALGO_TKIP) {
952 /*
953 * We should provide an initial iv32, phase1key pair.
954 * We could start with iv32=0 and compute the corresponding
955 * phase1key, but this means calling ieee80211_get_tkip_key
956 * with a fake skb (or export other tkip function).
957 * Because we are lazy we hope iv32 won't start with
958 * 0xffffffff and let's b43_op_update_tkip_key provide a
959 * correct pair.
960 */
961 rx_tkip_phase1_write(dev, index, 0xffffffff, (u16*)buf);
962 } else if (index >= pairwise_keys_start) /* clear it */
963 rx_tkip_phase1_write(dev, index, 0, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -0400964 if (key)
965 memcpy(buf, key, key_len);
966 key_write(dev, index, algorithm, buf);
Michael Buesch66d2d082009-08-06 10:36:50 +0200967 if (index >= pairwise_keys_start)
Michael Buesche4d6b792007-09-18 15:39:42 -0400968 keymac_write(dev, index, mac_addr);
969
970 dev->key[index].algorithm = algorithm;
971}
972
973static int b43_key_write(struct b43_wldev *dev,
974 int index, u8 algorithm,
John Daiker99da1852009-02-24 02:16:42 -0800975 const u8 *key, size_t key_len,
976 const u8 *mac_addr,
Michael Buesche4d6b792007-09-18 15:39:42 -0400977 struct ieee80211_key_conf *keyconf)
978{
979 int i;
Michael Buesch66d2d082009-08-06 10:36:50 +0200980 int pairwise_keys_start;
Michael Buesche4d6b792007-09-18 15:39:42 -0400981
gregor kowski035d0242009-08-19 22:35:45 +0200982 /* For ALG_TKIP the key is encoded as a 256-bit (32 byte) data block:
983 * - Temporal Encryption Key (128 bits)
984 * - Temporal Authenticator Tx MIC Key (64 bits)
985 * - Temporal Authenticator Rx MIC Key (64 bits)
986 *
987 * Hardware only store TEK
988 */
989 if (algorithm == B43_SEC_ALGO_TKIP && key_len == 32)
990 key_len = 16;
Michael Buesche4d6b792007-09-18 15:39:42 -0400991 if (key_len > B43_SEC_KEYSIZE)
992 return -EINVAL;
Michael Buesch66d2d082009-08-06 10:36:50 +0200993 for (i = 0; i < ARRAY_SIZE(dev->key); i++) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400994 /* Check that we don't already have this key. */
995 B43_WARN_ON(dev->key[i].keyconf == keyconf);
996 }
997 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +0100998 /* Pairwise key. Get an empty slot for the key. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400999 if (b43_new_kidx_api(dev))
Michael Buesch66d2d082009-08-06 10:36:50 +02001000 pairwise_keys_start = B43_NR_GROUP_KEYS;
Michael Buesche4d6b792007-09-18 15:39:42 -04001001 else
Michael Buesch66d2d082009-08-06 10:36:50 +02001002 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1003 for (i = pairwise_keys_start;
1004 i < pairwise_keys_start + B43_NR_PAIRWISE_KEYS;
1005 i++) {
1006 B43_WARN_ON(i >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -04001007 if (!dev->key[i].keyconf) {
1008 /* found empty */
1009 index = i;
1010 break;
1011 }
1012 }
1013 if (index < 0) {
Michael Buesche808e582008-12-19 21:30:52 +01001014 b43warn(dev->wl, "Out of hardware key memory\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04001015 return -ENOSPC;
1016 }
1017 } else
1018 B43_WARN_ON(index > 3);
1019
1020 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
1021 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1022 /* Default RX key */
1023 B43_WARN_ON(mac_addr);
1024 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
1025 }
1026 keyconf->hw_key_idx = index;
1027 dev->key[index].keyconf = keyconf;
1028
1029 return 0;
1030}
1031
1032static int b43_key_clear(struct b43_wldev *dev, int index)
1033{
Michael Buesch66d2d082009-08-06 10:36:50 +02001034 if (B43_WARN_ON((index < 0) || (index >= ARRAY_SIZE(dev->key))))
Michael Buesche4d6b792007-09-18 15:39:42 -04001035 return -EINVAL;
1036 do_key_write(dev, index, B43_SEC_ALGO_NONE,
1037 NULL, B43_SEC_KEYSIZE, NULL);
1038 if ((index <= 3) && !b43_new_kidx_api(dev)) {
1039 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
1040 NULL, B43_SEC_KEYSIZE, NULL);
1041 }
1042 dev->key[index].keyconf = NULL;
1043
1044 return 0;
1045}
1046
1047static void b43_clear_keys(struct b43_wldev *dev)
1048{
Michael Buesch66d2d082009-08-06 10:36:50 +02001049 int i, count;
Michael Buesche4d6b792007-09-18 15:39:42 -04001050
Michael Buesch66d2d082009-08-06 10:36:50 +02001051 if (b43_new_kidx_api(dev))
1052 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1053 else
1054 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1055 for (i = 0; i < count; i++)
Michael Buesche4d6b792007-09-18 15:39:42 -04001056 b43_key_clear(dev, i);
1057}
1058
Michael Buesch9cf7f242008-12-19 20:24:30 +01001059static void b43_dump_keymemory(struct b43_wldev *dev)
1060{
Michael Buesch66d2d082009-08-06 10:36:50 +02001061 unsigned int i, index, count, offset, pairwise_keys_start;
Michael Buesch9cf7f242008-12-19 20:24:30 +01001062 u8 mac[ETH_ALEN];
1063 u16 algo;
1064 u32 rcmta0;
1065 u16 rcmta1;
1066 u64 hf;
1067 struct b43_key *key;
1068
1069 if (!b43_debug(dev, B43_DBG_KEYS))
1070 return;
1071
1072 hf = b43_hf_read(dev);
1073 b43dbg(dev->wl, "Hardware key memory dump: USEDEFKEYS=%u\n",
1074 !!(hf & B43_HF_USEDEFKEYS));
Michael Buesch66d2d082009-08-06 10:36:50 +02001075 if (b43_new_kidx_api(dev)) {
1076 pairwise_keys_start = B43_NR_GROUP_KEYS;
1077 count = B43_NR_GROUP_KEYS + B43_NR_PAIRWISE_KEYS;
1078 } else {
1079 pairwise_keys_start = B43_NR_GROUP_KEYS * 2;
1080 count = B43_NR_GROUP_KEYS * 2 + B43_NR_PAIRWISE_KEYS;
1081 }
1082 for (index = 0; index < count; index++) {
Michael Buesch9cf7f242008-12-19 20:24:30 +01001083 key = &(dev->key[index]);
1084 printk(KERN_DEBUG "Key slot %02u: %s",
1085 index, (key->keyconf == NULL) ? " " : "*");
1086 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
1087 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
1088 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1089 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1090 }
1091
1092 algo = b43_shm_read16(dev, B43_SHM_SHARED,
1093 B43_SHM_SH_KEYIDXBLOCK + (index * 2));
1094 printk(" Algo: %04X/%02X", algo, key->algorithm);
1095
Michael Buesch66d2d082009-08-06 10:36:50 +02001096 if (index >= pairwise_keys_start) {
gregor kowski035d0242009-08-19 22:35:45 +02001097 if (key->algorithm == B43_SEC_ALGO_TKIP) {
1098 printk(" TKIP: ");
1099 offset = B43_SHM_SH_TKIPTSCTTAK + (index - 4) * (10 + 4);
1100 for (i = 0; i < 14; i += 2) {
1101 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
1102 printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
1103 }
1104 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01001105 rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001106 ((index - pairwise_keys_start) * 2) + 0);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001107 rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
Michael Buesch66d2d082009-08-06 10:36:50 +02001108 ((index - pairwise_keys_start) * 2) + 1);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001109 *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
1110 *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
Johannes Berge91d8332009-07-15 17:21:41 +02001111 printk(" MAC: %pM", mac);
Michael Buesch9cf7f242008-12-19 20:24:30 +01001112 } else
1113 printk(" DEFAULT KEY");
1114 printk("\n");
1115 }
1116}
1117
Michael Buesche4d6b792007-09-18 15:39:42 -04001118void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1119{
1120 u32 macctl;
1121 u16 ucstat;
1122 bool hwps;
1123 bool awake;
1124 int i;
1125
1126 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1127 (ps_flags & B43_PS_DISABLED));
1128 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1129
1130 if (ps_flags & B43_PS_ENABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001131 hwps = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001132 } else if (ps_flags & B43_PS_DISABLED) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001133 hwps = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001134 } else {
1135 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1136 // and thus is not an AP and we are associated, set bit 25
1137 }
1138 if (ps_flags & B43_PS_AWAKE) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001139 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001140 } else if (ps_flags & B43_PS_ASLEEP) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00001141 awake = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001142 } else {
1143 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1144 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1145 // successful, set bit26
1146 }
1147
1148/* FIXME: For now we force awake-on and hwps-off */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001149 hwps = false;
1150 awake = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001151
1152 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1153 if (hwps)
1154 macctl |= B43_MACCTL_HWPS;
1155 else
1156 macctl &= ~B43_MACCTL_HWPS;
1157 if (awake)
1158 macctl |= B43_MACCTL_AWAKE;
1159 else
1160 macctl &= ~B43_MACCTL_AWAKE;
1161 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1162 /* Commit write */
1163 b43_read32(dev, B43_MMIO_MACCTL);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001164 if (awake && dev->dev->core_rev >= 5) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001165 /* Wait for the microcode to wake up. */
1166 for (i = 0; i < 100; i++) {
1167 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1168 B43_SHM_SH_UCODESTAT);
1169 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1170 break;
1171 udelay(10);
1172 }
1173 }
1174}
1175
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001176#ifdef CONFIG_B43_BCMA
Rafał Miłecki49173592011-07-17 01:06:06 +02001177static void b43_bcma_phy_reset(struct b43_wldev *dev)
1178{
1179 u32 flags;
1180
1181 /* Put PHY into reset */
1182 flags = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
1183 flags |= B43_BCMA_IOCTL_PHY_RESET;
1184 flags |= B43_BCMA_IOCTL_PHY_BW_20MHZ; /* Make 20 MHz def */
1185 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, flags);
1186 udelay(2);
1187
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001188 b43_phy_take_out_of_reset(dev);
Rafał Miłecki49173592011-07-17 01:06:06 +02001189}
1190
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001191static void b43_bcma_wireless_core_reset(struct b43_wldev *dev, bool gmode)
1192{
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001193 u32 req = B43_BCMA_CLKCTLST_80211_PLL_REQ |
1194 B43_BCMA_CLKCTLST_PHY_PLL_REQ;
1195 u32 status = B43_BCMA_CLKCTLST_80211_PLL_ST |
1196 B43_BCMA_CLKCTLST_PHY_PLL_ST;
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001197 u32 flags;
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001198
Rafał Miłecki6b9e03e2014-04-22 13:54:35 +02001199 flags = B43_BCMA_IOCTL_PHY_CLKEN;
1200 if (gmode)
1201 flags |= B43_BCMA_IOCTL_GMODE;
1202 b43_device_enable(dev, flags);
1203
Rafał Miłecki49173592011-07-17 01:06:06 +02001204 bcma_core_set_clockmode(dev->dev->bdev, BCMA_CLKMODE_FAST);
1205 b43_bcma_phy_reset(dev);
Rafał Miłecki88cceab2013-02-26 10:07:57 +01001206 bcma_core_pll_ctl(dev->dev->bdev, req, status, true);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001207}
1208#endif
1209
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001210#ifdef CONFIG_B43_SSB
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001211static void b43_ssb_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001212{
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001213 u32 flags = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04001214
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001215 if (gmode)
1216 flags |= B43_TMSLOW_GMODE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001217 flags |= B43_TMSLOW_PHYCLKEN;
1218 flags |= B43_TMSLOW_PHYRESET;
Rafał Miłecki42ab1352010-12-09 20:56:01 +01001219 if (dev->phy.type == B43_PHYTYPE_N)
1220 flags |= B43_TMSLOW_PHY_BANDWIDTH_20MHZ; /* Make 20 MHz def */
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02001221 b43_device_enable(dev, flags);
Michael Buesche4d6b792007-09-18 15:39:42 -04001222 msleep(2); /* Wait for the PLL to turn on. */
1223
Rafał Miłecki50c1b592014-05-17 23:24:55 +02001224 b43_phy_take_out_of_reset(dev);
Rafał Miłecki14952982011-05-17 18:57:28 +02001225}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02001226#endif
Rafał Miłecki14952982011-05-17 18:57:28 +02001227
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001228void b43_wireless_core_reset(struct b43_wldev *dev, bool gmode)
Rafał Miłecki14952982011-05-17 18:57:28 +02001229{
1230 u32 macctl;
1231
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001232 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02001233#ifdef CONFIG_B43_BCMA
1234 case B43_BUS_BCMA:
1235 b43_bcma_wireless_core_reset(dev, gmode);
1236 break;
1237#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001238#ifdef CONFIG_B43_SSB
1239 case B43_BUS_SSB:
1240 b43_ssb_wireless_core_reset(dev, gmode);
1241 break;
1242#endif
1243 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001244
Michael Bueschfb111372008-09-02 13:00:34 +02001245 /* Turn Analog ON, but only if we already know the PHY-type.
1246 * This protects against very early setup where we don't know the
1247 * PHY-type, yet. wireless_core_reset will be called once again later,
1248 * when we know the PHY-type. */
1249 if (dev->phy.ops)
Michael Bueschcb24f572008-09-03 12:12:20 +02001250 dev->phy.ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001251
1252 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1253 macctl &= ~B43_MACCTL_GMODE;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02001254 if (gmode)
Michael Buesche4d6b792007-09-18 15:39:42 -04001255 macctl |= B43_MACCTL_GMODE;
1256 macctl |= B43_MACCTL_IHR_ENABLED;
1257 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1258}
1259
1260static void handle_irq_transmit_status(struct b43_wldev *dev)
1261{
1262 u32 v0, v1;
1263 u16 tmp;
1264 struct b43_txstatus stat;
1265
1266 while (1) {
1267 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1268 if (!(v0 & 0x00000001))
1269 break;
1270 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1271
1272 stat.cookie = (v0 >> 16);
1273 stat.seq = (v1 & 0x0000FFFF);
1274 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1275 tmp = (v0 & 0x0000FFFF);
1276 stat.frame_count = ((tmp & 0xF000) >> 12);
1277 stat.rts_count = ((tmp & 0x0F00) >> 8);
1278 stat.supp_reason = ((tmp & 0x001C) >> 2);
1279 stat.pm_indicated = !!(tmp & 0x0080);
1280 stat.intermediate = !!(tmp & 0x0040);
1281 stat.for_ampdu = !!(tmp & 0x0020);
1282 stat.acked = !!(tmp & 0x0002);
1283
1284 b43_handle_txstatus(dev, &stat);
1285 }
1286}
1287
1288static void drain_txstatus_queue(struct b43_wldev *dev)
1289{
1290 u32 dummy;
1291
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001292 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04001293 return;
1294 /* Read all entries from the microcode TXstatus FIFO
1295 * and throw them away.
1296 */
1297 while (1) {
1298 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1299 if (!(dummy & 0x00000001))
1300 break;
1301 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1302 }
1303}
1304
1305static u32 b43_jssi_read(struct b43_wldev *dev)
1306{
1307 u32 val = 0;
1308
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001309 val = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001310 val <<= 16;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001311 val |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001312
1313 return val;
1314}
1315
1316static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1317{
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001318 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI0,
1319 (jssi & 0x0000FFFF));
1320 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_JSSI1,
1321 (jssi & 0xFFFF0000) >> 16);
Michael Buesche4d6b792007-09-18 15:39:42 -04001322}
1323
1324static void b43_generate_noise_sample(struct b43_wldev *dev)
1325{
1326 b43_jssi_write(dev, 0x7F7F7F7F);
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001327 b43_write32(dev, B43_MMIO_MACCMD,
1328 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001329}
1330
1331static void b43_calculate_link_quality(struct b43_wldev *dev)
1332{
1333 /* Top half of Link Quality calculation. */
1334
Michael Bueschef1a6282008-08-27 18:53:02 +02001335 if (dev->phy.type != B43_PHYTYPE_G)
1336 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001337 if (dev->noisecalc.calculation_running)
1338 return;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001339 dev->noisecalc.calculation_running = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001340 dev->noisecalc.nr_samples = 0;
1341
1342 b43_generate_noise_sample(dev);
1343}
1344
1345static void handle_irq_noise(struct b43_wldev *dev)
1346{
Michael Bueschef1a6282008-08-27 18:53:02 +02001347 struct b43_phy_g *phy = dev->phy.g;
Michael Buesche4d6b792007-09-18 15:39:42 -04001348 u16 tmp;
1349 u8 noise[4];
1350 u8 i, j;
1351 s32 average;
1352
1353 /* Bottom half of Link Quality calculation. */
1354
Michael Bueschef1a6282008-08-27 18:53:02 +02001355 if (dev->phy.type != B43_PHYTYPE_G)
1356 return;
1357
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001358 /* Possible race condition: It might be possible that the user
1359 * changed to a different channel in the meantime since we
1360 * started the calculation. We ignore that fact, since it's
1361 * not really that much of a problem. The background noise is
1362 * an estimation only anyway. Slightly wrong results will get damped
1363 * by the averaging of the 8 sample rounds. Additionally the
1364 * value is shortlived. So it will be replaced by the next noise
1365 * calculation round soon. */
1366
Michael Buesche4d6b792007-09-18 15:39:42 -04001367 B43_WARN_ON(!dev->noisecalc.calculation_running);
Michael Buesch1a094042007-09-20 11:13:40 -07001368 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
Michael Buesche4d6b792007-09-18 15:39:42 -04001369 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1370 noise[2] == 0x7F || noise[3] == 0x7F)
1371 goto generate_new;
1372
1373 /* Get the noise samples. */
1374 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1375 i = dev->noisecalc.nr_samples;
Harvey Harrisoncdbf0842008-05-02 13:47:48 -07001376 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1377 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1378 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1379 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001380 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1381 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1382 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1383 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1384 dev->noisecalc.nr_samples++;
1385 if (dev->noisecalc.nr_samples == 8) {
1386 /* Calculate the Link Quality by the noise samples. */
1387 average = 0;
1388 for (i = 0; i < 8; i++) {
1389 for (j = 0; j < 4; j++)
1390 average += dev->noisecalc.samples[i][j];
1391 }
1392 average /= (8 * 4);
1393 average *= 125;
1394 average += 64;
1395 average /= 128;
1396 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1397 tmp = (tmp / 128) & 0x1F;
1398 if (tmp >= 8)
1399 average += 2;
1400 else
1401 average -= 25;
1402 if (tmp == 8)
1403 average -= 72;
1404 else
1405 average -= 48;
1406
1407 dev->stats.link_noise = average;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001408 dev->noisecalc.calculation_running = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04001409 return;
1410 }
Michael Buesch98a3b2f2008-06-12 12:36:29 +02001411generate_new:
Michael Buesche4d6b792007-09-18 15:39:42 -04001412 b43_generate_noise_sample(dev);
1413}
1414
1415static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1416{
Johannes Berg05c914f2008-09-11 00:01:58 +02001417 if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001418 ///TODO: PS TBTT
1419 } else {
1420 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1421 b43_power_saving_ctl_bits(dev, 0);
1422 }
Johannes Berg05c914f2008-09-11 00:01:58 +02001423 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Rusty Russell3db1cd52011-12-19 13:56:45 +00001424 dev->dfq_valid = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001425}
1426
1427static void handle_irq_atim_end(struct b43_wldev *dev)
1428{
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001429 if (dev->dfq_valid) {
1430 b43_write32(dev, B43_MMIO_MACCMD,
1431 b43_read32(dev, B43_MMIO_MACCMD)
1432 | B43_MACCMD_DFQ_VALID);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001433 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01001434 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001435}
1436
1437static void handle_irq_pmq(struct b43_wldev *dev)
1438{
1439 u32 tmp;
1440
1441 //TODO: AP mode.
1442
1443 while (1) {
1444 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1445 if (!(tmp & 0x00000008))
1446 break;
1447 }
1448 /* 16bit write is odd, but correct. */
1449 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1450}
1451
1452static void b43_write_template_common(struct b43_wldev *dev,
John Daiker99da1852009-02-24 02:16:42 -08001453 const u8 *data, u16 size,
Michael Buesche4d6b792007-09-18 15:39:42 -04001454 u16 ram_offset,
1455 u16 shm_size_offset, u8 rate)
1456{
1457 u32 i, tmp;
1458 struct b43_plcp_hdr4 plcp;
1459
1460 plcp.data = 0;
1461 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1462 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1463 ram_offset += sizeof(u32);
1464 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1465 * So leave the first two bytes of the next write blank.
1466 */
1467 tmp = (u32) (data[0]) << 16;
1468 tmp |= (u32) (data[1]) << 24;
1469 b43_ram_write(dev, ram_offset, tmp);
1470 ram_offset += sizeof(u32);
1471 for (i = 2; i < size; i += sizeof(u32)) {
1472 tmp = (u32) (data[i + 0]);
1473 if (i + 1 < size)
1474 tmp |= (u32) (data[i + 1]) << 8;
1475 if (i + 2 < size)
1476 tmp |= (u32) (data[i + 2]) << 16;
1477 if (i + 3 < size)
1478 tmp |= (u32) (data[i + 3]) << 24;
1479 b43_ram_write(dev, ram_offset + i - 2, tmp);
1480 }
1481 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1482 size + sizeof(struct b43_plcp_hdr6));
1483}
1484
Michael Buesch5042c502008-04-05 15:05:00 +02001485/* Check if the use of the antenna that ieee80211 told us to
1486 * use is possible. This will fall back to DEFAULT.
1487 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1488u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1489 u8 antenna_nr)
1490{
1491 u8 antenna_mask;
1492
1493 if (antenna_nr == 0) {
1494 /* Zero means "use default antenna". That's always OK. */
1495 return 0;
1496 }
1497
1498 /* Get the mask of available antennas. */
1499 if (dev->phy.gmode)
Rafał Miłecki05814832011-05-18 02:06:39 +02001500 antenna_mask = dev->dev->bus_sprom->ant_available_bg;
Michael Buesch5042c502008-04-05 15:05:00 +02001501 else
Rafał Miłecki05814832011-05-18 02:06:39 +02001502 antenna_mask = dev->dev->bus_sprom->ant_available_a;
Michael Buesch5042c502008-04-05 15:05:00 +02001503
1504 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1505 /* This antenna is not available. Fall back to default. */
1506 return 0;
1507 }
1508
1509 return antenna_nr;
1510}
1511
Michael Buesch5042c502008-04-05 15:05:00 +02001512/* Convert a b43 antenna number value to the PHY TX control value. */
1513static u16 b43_antenna_to_phyctl(int antenna)
1514{
1515 switch (antenna) {
1516 case B43_ANTENNA0:
1517 return B43_TXH_PHY_ANT0;
1518 case B43_ANTENNA1:
1519 return B43_TXH_PHY_ANT1;
1520 case B43_ANTENNA2:
1521 return B43_TXH_PHY_ANT2;
1522 case B43_ANTENNA3:
1523 return B43_TXH_PHY_ANT3;
Gábor Stefanik64e368b2009-08-27 22:49:49 +02001524 case B43_ANTENNA_AUTO0:
1525 case B43_ANTENNA_AUTO1:
Michael Buesch5042c502008-04-05 15:05:00 +02001526 return B43_TXH_PHY_ANT01AUTO;
1527 }
1528 B43_WARN_ON(1);
1529 return 0;
1530}
1531
Michael Buesche4d6b792007-09-18 15:39:42 -04001532static void b43_write_beacon_template(struct b43_wldev *dev,
1533 u16 ram_offset,
Michael Buesch5042c502008-04-05 15:05:00 +02001534 u16 shm_size_offset)
Michael Buesche4d6b792007-09-18 15:39:42 -04001535{
Michael Buesch47f76ca2007-12-27 22:15:11 +01001536 unsigned int i, len, variable_len;
Michael Buesche66fee62007-12-26 17:47:10 +01001537 const struct ieee80211_mgmt *bcn;
1538 const u8 *ie;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001539 bool tim_found = false;
Michael Buesch5042c502008-04-05 15:05:00 +02001540 unsigned int rate;
1541 u16 ctl;
1542 int antenna;
Johannes Berge039fa42008-05-15 12:55:29 +02001543 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
Michael Buesche4d6b792007-09-18 15:39:42 -04001544
Michael Buesche66fee62007-12-26 17:47:10 +01001545 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
Silvan Jegenc8e49552014-02-25 18:12:52 +01001546 len = min_t(size_t, dev->wl->current_beacon->len,
Michael Buesche4d6b792007-09-18 15:39:42 -04001547 0x200 - sizeof(struct b43_plcp_hdr6));
Johannes Berge039fa42008-05-15 12:55:29 +02001548 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
Michael Buesche66fee62007-12-26 17:47:10 +01001549
1550 b43_write_template_common(dev, (const u8 *)bcn,
Michael Buesche4d6b792007-09-18 15:39:42 -04001551 len, ram_offset, shm_size_offset, rate);
Michael Buesche66fee62007-12-26 17:47:10 +01001552
Michael Buesch5042c502008-04-05 15:05:00 +02001553 /* Write the PHY TX control parameters. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02001554 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch5042c502008-04-05 15:05:00 +02001555 antenna = b43_antenna_to_phyctl(antenna);
1556 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1557 /* We can't send beacons with short preamble. Would get PHY errors. */
1558 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1559 ctl &= ~B43_TXH_PHY_ANT;
1560 ctl &= ~B43_TXH_PHY_ENC;
1561 ctl |= antenna;
1562 if (b43_is_cck_rate(rate))
1563 ctl |= B43_TXH_PHY_ENC_CCK;
1564 else
1565 ctl |= B43_TXH_PHY_ENC_OFDM;
1566 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1567
Michael Buesche66fee62007-12-26 17:47:10 +01001568 /* Find the position of the TIM and the DTIM_period value
1569 * and write them to SHM. */
1570 ie = bcn->u.beacon.variable;
Michael Buesch47f76ca2007-12-27 22:15:11 +01001571 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1572 for (i = 0; i < variable_len - 2; ) {
Michael Buesche66fee62007-12-26 17:47:10 +01001573 uint8_t ie_id, ie_len;
1574
1575 ie_id = ie[i];
1576 ie_len = ie[i + 1];
1577 if (ie_id == 5) {
1578 u16 tim_position;
1579 u16 dtim_period;
1580 /* This is the TIM Information Element */
1581
1582 /* Check whether the ie_len is in the beacon data range. */
Michael Buesch47f76ca2007-12-27 22:15:11 +01001583 if (variable_len < ie_len + 2 + i)
Michael Buesche66fee62007-12-26 17:47:10 +01001584 break;
1585 /* A valid TIM is at least 4 bytes long. */
1586 if (ie_len < 4)
1587 break;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001588 tim_found = true;
Michael Buesche66fee62007-12-26 17:47:10 +01001589
1590 tim_position = sizeof(struct b43_plcp_hdr6);
1591 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1592 tim_position += i;
1593
1594 dtim_period = ie[i + 3];
1595
1596 b43_shm_write16(dev, B43_SHM_SHARED,
1597 B43_SHM_SH_TIMBPOS, tim_position);
1598 b43_shm_write16(dev, B43_SHM_SHARED,
1599 B43_SHM_SH_DTIMPER, dtim_period);
1600 break;
1601 }
1602 i += ie_len + 2;
1603 }
1604 if (!tim_found) {
Johannes Berg04dea132008-05-20 12:10:49 +02001605 /*
1606 * If ucode wants to modify TIM do it behind the beacon, this
1607 * will happen, for example, when doing mesh networking.
1608 */
1609 b43_shm_write16(dev, B43_SHM_SHARED,
1610 B43_SHM_SH_TIMBPOS,
1611 len + sizeof(struct b43_plcp_hdr6));
1612 b43_shm_write16(dev, B43_SHM_SHARED,
1613 B43_SHM_SH_DTIMPER, 0);
1614 }
1615 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
Michael Buesche4d6b792007-09-18 15:39:42 -04001616}
1617
Michael Buesch6b4bec012008-05-20 12:16:28 +02001618static void b43_upload_beacon0(struct b43_wldev *dev)
1619{
1620 struct b43_wl *wl = dev->wl;
1621
1622 if (wl->beacon0_uploaded)
1623 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001624 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE0, B43_SHM_SH_BTL0);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001625 wl->beacon0_uploaded = true;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001626}
1627
1628static void b43_upload_beacon1(struct b43_wldev *dev)
1629{
1630 struct b43_wl *wl = dev->wl;
1631
1632 if (wl->beacon1_uploaded)
1633 return;
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01001634 b43_write_beacon_template(dev, B43_SHM_SH_BT_BASE1, B43_SHM_SH_BTL1);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001635 wl->beacon1_uploaded = true;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001636}
1637
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001638static void handle_irq_beacon(struct b43_wldev *dev)
1639{
1640 struct b43_wl *wl = dev->wl;
1641 u32 cmd, beacon0_valid, beacon1_valid;
1642
Johannes Berg05c914f2008-09-11 00:01:58 +02001643 if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
Manual Munz8c235162011-09-18 18:24:03 -05001644 !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) &&
1645 !b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001646 return;
1647
1648 /* This is the bottom half of the asynchronous beacon update. */
1649
1650 /* Ignore interrupt in the future. */
Michael Buesch13790722009-04-08 21:26:27 +02001651 dev->irq_mask &= ~B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001652
1653 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1654 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1655 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1656
1657 /* Schedule interrupt manually, if busy. */
1658 if (beacon0_valid && beacon1_valid) {
1659 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
Michael Buesch13790722009-04-08 21:26:27 +02001660 dev->irq_mask |= B43_IRQ_BEACON;
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001661 return;
1662 }
1663
Michael Buesch6b4bec012008-05-20 12:16:28 +02001664 if (unlikely(wl->beacon_templates_virgin)) {
1665 /* We never uploaded a beacon before.
1666 * Upload both templates now, but only mark one valid. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00001667 wl->beacon_templates_virgin = false;
Michael Buesch6b4bec012008-05-20 12:16:28 +02001668 b43_upload_beacon0(dev);
1669 b43_upload_beacon1(dev);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001670 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1671 cmd |= B43_MACCMD_BEACON0_VALID;
1672 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Buesch6b4bec012008-05-20 12:16:28 +02001673 } else {
1674 if (!beacon0_valid) {
1675 b43_upload_beacon0(dev);
1676 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1677 cmd |= B43_MACCMD_BEACON0_VALID;
1678 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1679 } else if (!beacon1_valid) {
1680 b43_upload_beacon1(dev);
1681 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1682 cmd |= B43_MACCMD_BEACON1_VALID;
1683 b43_write32(dev, B43_MMIO_MACCMD, cmd);
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001684 }
Michael Bueschc97a4cc2008-04-05 15:02:09 +02001685 }
1686}
1687
Michael Buesch36dbd952009-09-04 22:51:29 +02001688static void b43_do_beacon_update_trigger_work(struct b43_wldev *dev)
1689{
1690 u32 old_irq_mask = dev->irq_mask;
1691
1692 /* update beacon right away or defer to irq */
1693 handle_irq_beacon(dev);
1694 if (old_irq_mask != dev->irq_mask) {
1695 /* The handler updated the IRQ mask. */
1696 B43_WARN_ON(!dev->irq_mask);
1697 if (b43_read32(dev, B43_MMIO_GEN_IRQ_MASK)) {
1698 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1699 } else {
1700 /* Device interrupts are currently disabled. That means
1701 * we just ran the hardirq handler and scheduled the
1702 * IRQ thread. The thread will write the IRQ mask when
1703 * it finished, so there's nothing to do here. Writing
1704 * the mask _here_ would incorrectly re-enable IRQs. */
1705 }
1706 }
1707}
1708
Michael Buescha82d9922008-04-04 21:40:06 +02001709static void b43_beacon_update_trigger_work(struct work_struct *work)
1710{
1711 struct b43_wl *wl = container_of(work, struct b43_wl,
1712 beacon_update_trigger);
1713 struct b43_wldev *dev;
1714
1715 mutex_lock(&wl->mutex);
1716 dev = wl->current_dev;
1717 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
Rafał Miłecki505fb012011-05-19 15:11:27 +02001718 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02001719 /* wl->mutex is enough. */
1720 b43_do_beacon_update_trigger_work(dev);
1721 mmiowb();
1722 } else {
1723 spin_lock_irq(&wl->hardirq_lock);
1724 b43_do_beacon_update_trigger_work(dev);
1725 mmiowb();
1726 spin_unlock_irq(&wl->hardirq_lock);
1727 }
Michael Buescha82d9922008-04-04 21:40:06 +02001728 }
1729 mutex_unlock(&wl->mutex);
1730}
1731
Michael Bueschd4df6f12007-12-26 18:04:14 +01001732/* Asynchronously update the packet templates in template RAM.
Michael Buesch36dbd952009-09-04 22:51:29 +02001733 * Locking: Requires wl->mutex to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001734static void b43_update_templates(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04001735{
Johannes Berg9d139c82008-07-09 14:40:37 +02001736 struct sk_buff *beacon;
1737
Michael Buesche66fee62007-12-26 17:47:10 +01001738 /* This is the top half of the ansynchronous beacon update.
1739 * The bottom half is the beacon IRQ.
1740 * Beacon update must be asynchronous to avoid sending an
1741 * invalid beacon. This can happen for example, if the firmware
1742 * transmits a beacon while we are updating it. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001743
Johannes Berg9d139c82008-07-09 14:40:37 +02001744 /* We could modify the existing beacon and set the aid bit in
1745 * the TIM field, but that would probably require resizing and
1746 * moving of data within the beacon template.
1747 * Simply request a new beacon and let mac80211 do the hard work. */
1748 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1749 if (unlikely(!beacon))
1750 return;
1751
Michael Buesche66fee62007-12-26 17:47:10 +01001752 if (wl->current_beacon)
1753 dev_kfree_skb_any(wl->current_beacon);
1754 wl->current_beacon = beacon;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001755 wl->beacon0_uploaded = false;
1756 wl->beacon1_uploaded = false;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001757 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
Michael Buesche4d6b792007-09-18 15:39:42 -04001758}
1759
Michael Buesche4d6b792007-09-18 15:39:42 -04001760static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1761{
1762 b43_time_lock(dev);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001763 if (dev->dev->core_rev >= 3) {
Michael Buescha82d9922008-04-04 21:40:06 +02001764 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1765 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
Michael Buesche4d6b792007-09-18 15:39:42 -04001766 } else {
1767 b43_write16(dev, 0x606, (beacon_int >> 6));
1768 b43_write16(dev, 0x610, beacon_int);
1769 }
1770 b43_time_unlock(dev);
Michael Buescha82d9922008-04-04 21:40:06 +02001771 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
Michael Buesche4d6b792007-09-18 15:39:42 -04001772}
1773
Michael Bueschafa83e22008-05-19 23:51:37 +02001774static void b43_handle_firmware_panic(struct b43_wldev *dev)
1775{
1776 u16 reason;
1777
1778 /* Read the register that contains the reason code for the panic. */
1779 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1780 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1781
1782 switch (reason) {
1783 default:
1784 b43dbg(dev->wl, "The panic reason is unknown.\n");
1785 /* fallthrough */
1786 case B43_FWPANIC_DIE:
1787 /* Do not restart the controller or firmware.
1788 * The device is nonfunctional from now on.
1789 * Restarting would result in this panic to trigger again,
1790 * so we avoid that recursion. */
1791 break;
1792 case B43_FWPANIC_RESTART:
1793 b43_controller_restart(dev, "Microcode panic");
1794 break;
1795 }
1796}
1797
Michael Buesche4d6b792007-09-18 15:39:42 -04001798static void handle_irq_ucode_debug(struct b43_wldev *dev)
1799{
Michael Buesche48b0ee2008-05-17 22:44:35 +02001800 unsigned int i, cnt;
Michael Buesch53c06852008-05-20 00:24:36 +02001801 u16 reason, marker_id, marker_line;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001802 __le16 *buf;
1803
1804 /* The proprietary firmware doesn't have this IRQ. */
1805 if (!dev->fw.opensource)
1806 return;
1807
Michael Bueschafa83e22008-05-19 23:51:37 +02001808 /* Read the register that contains the reason code for this IRQ. */
1809 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1810
Michael Buesche48b0ee2008-05-17 22:44:35 +02001811 switch (reason) {
1812 case B43_DEBUGIRQ_PANIC:
Michael Bueschafa83e22008-05-19 23:51:37 +02001813 b43_handle_firmware_panic(dev);
Michael Buesche48b0ee2008-05-17 22:44:35 +02001814 break;
1815 case B43_DEBUGIRQ_DUMP_SHM:
1816 if (!B43_DEBUG)
1817 break; /* Only with driver debugging enabled. */
1818 buf = kmalloc(4096, GFP_ATOMIC);
1819 if (!buf) {
1820 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1821 goto out;
1822 }
1823 for (i = 0; i < 4096; i += 2) {
1824 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1825 buf[i / 2] = cpu_to_le16(tmp);
1826 }
1827 b43info(dev->wl, "Shared memory dump:\n");
1828 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1829 16, 2, buf, 4096, 1);
1830 kfree(buf);
1831 break;
1832 case B43_DEBUGIRQ_DUMP_REGS:
1833 if (!B43_DEBUG)
1834 break; /* Only with driver debugging enabled. */
1835 b43info(dev->wl, "Microcode register dump:\n");
1836 for (i = 0, cnt = 0; i < 64; i++) {
1837 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1838 if (cnt == 0)
1839 printk(KERN_INFO);
1840 printk("r%02u: 0x%04X ", i, tmp);
1841 cnt++;
1842 if (cnt == 6) {
1843 printk("\n");
1844 cnt = 0;
1845 }
1846 }
1847 printk("\n");
1848 break;
Michael Buesch53c06852008-05-20 00:24:36 +02001849 case B43_DEBUGIRQ_MARKER:
1850 if (!B43_DEBUG)
1851 break; /* Only with driver debugging enabled. */
1852 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1853 B43_MARKER_ID_REG);
1854 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1855 B43_MARKER_LINE_REG);
1856 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1857 "at line number %u\n",
1858 marker_id, marker_line);
1859 break;
Michael Buesche48b0ee2008-05-17 22:44:35 +02001860 default:
1861 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1862 reason);
1863 }
1864out:
Michael Bueschafa83e22008-05-19 23:51:37 +02001865 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1866 b43_shm_write16(dev, B43_SHM_SCRATCH,
1867 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001868}
1869
Michael Buesch36dbd952009-09-04 22:51:29 +02001870static void b43_do_interrupt_thread(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001871{
1872 u32 reason;
1873 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1874 u32 merged_dma_reason = 0;
Michael Buesch21954c32007-09-27 15:31:40 +02001875 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -04001876
Michael Buesch36dbd952009-09-04 22:51:29 +02001877 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
1878 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001879
1880 reason = dev->irq_reason;
1881 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1882 dma_reason[i] = dev->dma_reason[i];
1883 merged_dma_reason |= dma_reason[i];
1884 }
1885
1886 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1887 b43err(dev->wl, "MAC transmission error\n");
1888
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001889 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001890 b43err(dev->wl, "PHY transmission error\n");
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01001891 rmb();
1892 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1893 atomic_set(&dev->phy.txerr_cnt,
1894 B43_PHY_TX_BADNESS_LIMIT);
1895 b43err(dev->wl, "Too many PHY TX errors, "
1896 "restarting the controller\n");
1897 b43_controller_restart(dev, "PHY TX errors");
1898 }
1899 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001900
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001901 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK))) {
1902 b43err(dev->wl,
1903 "Fatal DMA error: 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X, 0x%08X\n",
1904 dma_reason[0], dma_reason[1],
1905 dma_reason[2], dma_reason[3],
1906 dma_reason[4], dma_reason[5]);
1907 b43err(dev->wl, "This device does not support DMA "
Larry Fingerbb64d952010-06-19 08:29:08 -05001908 "on your system. It will now be switched to PIO.\n");
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001909 /* Fall back to PIO transfers if we get fatal DMA errors! */
1910 dev->use_pio = true;
1911 b43_controller_restart(dev, "DMA error");
1912 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001913 }
1914
1915 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1916 handle_irq_ucode_debug(dev);
1917 if (reason & B43_IRQ_TBTT_INDI)
1918 handle_irq_tbtt_indication(dev);
1919 if (reason & B43_IRQ_ATIM_END)
1920 handle_irq_atim_end(dev);
1921 if (reason & B43_IRQ_BEACON)
1922 handle_irq_beacon(dev);
1923 if (reason & B43_IRQ_PMQ)
1924 handle_irq_pmq(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02001925 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1926 ;/* TODO */
1927 if (reason & B43_IRQ_NOISESAMPLE_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001928 handle_irq_noise(dev);
1929
1930 /* Check the DMA reason registers for received data. */
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001931 if (dma_reason[0] & B43_DMAIRQ_RDESC_UFLOW) {
1932 if (B43_DEBUG)
1933 b43warn(dev->wl, "RX descriptor underrun\n");
1934 b43_dma_handle_rx_overflow(dev->dma.rx_ring);
1935 }
Michael Buesch5100d5a2008-03-29 21:01:16 +01001936 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1937 if (b43_using_pio_transfers(dev))
1938 b43_pio_rx(dev->pio.rx_queue);
1939 else
1940 b43_dma_rx(dev->dma.rx_ring);
1941 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001942 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
1943 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
Michael Bueschb27faf82008-03-06 16:32:46 +01001944 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04001945 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
1946 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
1947
Michael Buesch21954c32007-09-27 15:31:40 +02001948 if (reason & B43_IRQ_TX_OK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001949 handle_irq_transmit_status(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04001950
Michael Buesch36dbd952009-09-04 22:51:29 +02001951 /* Re-enable interrupts on the device by restoring the current interrupt mask. */
Michael Buesch13790722009-04-08 21:26:27 +02001952 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesch990b86f2009-09-12 00:48:03 +02001953
1954#if B43_DEBUG
1955 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
1956 dev->irq_count++;
1957 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
1958 if (reason & (1 << i))
1959 dev->irq_bit_count[i]++;
1960 }
1961 }
1962#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04001963}
1964
Michael Buesch36dbd952009-09-04 22:51:29 +02001965/* Interrupt thread handler. Handles device interrupts in thread context. */
1966static irqreturn_t b43_interrupt_thread_handler(int irq, void *dev_id)
Michael Buesche4d6b792007-09-18 15:39:42 -04001967{
Michael Buesche4d6b792007-09-18 15:39:42 -04001968 struct b43_wldev *dev = dev_id;
Michael Buesch36dbd952009-09-04 22:51:29 +02001969
1970 mutex_lock(&dev->wl->mutex);
1971 b43_do_interrupt_thread(dev);
1972 mmiowb();
1973 mutex_unlock(&dev->wl->mutex);
1974
1975 return IRQ_HANDLED;
1976}
1977
1978static irqreturn_t b43_do_interrupt(struct b43_wldev *dev)
1979{
Michael Buesche4d6b792007-09-18 15:39:42 -04001980 u32 reason;
1981
Michael Buesch36dbd952009-09-04 22:51:29 +02001982 /* This code runs under wl->hardirq_lock, but _only_ on non-SDIO busses.
1983 * On SDIO, this runs under wl->mutex. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001984
Michael Buesche4d6b792007-09-18 15:39:42 -04001985 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1986 if (reason == 0xffffffff) /* shared IRQ */
Michael Buesch36dbd952009-09-04 22:51:29 +02001987 return IRQ_NONE;
Michael Buesch13790722009-04-08 21:26:27 +02001988 reason &= dev->irq_mask;
Michael Buesche4d6b792007-09-18 15:39:42 -04001989 if (!reason)
Sebastian Andrzej Siewiorcae56142011-07-07 21:58:10 +02001990 return IRQ_NONE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001991
1992 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001993 & 0x0001FC00;
Michael Buesche4d6b792007-09-18 15:39:42 -04001994 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
1995 & 0x0000DC00;
1996 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
1997 & 0x0000DC00;
1998 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
1999 & 0x0001DC00;
2000 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
2001 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002002/* Unused ring
Michael Buesche4d6b792007-09-18 15:39:42 -04002003 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
2004 & 0x0000DC00;
Michael Buesch13790722009-04-08 21:26:27 +02002005*/
Michael Buesche4d6b792007-09-18 15:39:42 -04002006
Michael Buesch36dbd952009-09-04 22:51:29 +02002007 /* ACK the interrupt. */
2008 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
2009 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
2010 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
2011 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
2012 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
2013 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
2014/* Unused ring
2015 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
2016*/
2017
2018 /* Disable IRQs on the device. The IRQ thread handler will re-enable them. */
Michael Buesch13790722009-04-08 21:26:27 +02002019 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
Michael Buesch36dbd952009-09-04 22:51:29 +02002020 /* Save the reason bitmasks for the IRQ thread handler. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002021 dev->irq_reason = reason;
Michael Buesch36dbd952009-09-04 22:51:29 +02002022
2023 return IRQ_WAKE_THREAD;
2024}
2025
2026/* Interrupt handler top-half. This runs with interrupts disabled. */
2027static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
2028{
2029 struct b43_wldev *dev = dev_id;
2030 irqreturn_t ret;
2031
2032 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2033 return IRQ_NONE;
2034
2035 spin_lock(&dev->wl->hardirq_lock);
2036 ret = b43_do_interrupt(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002037 mmiowb();
Michael Buesch36dbd952009-09-04 22:51:29 +02002038 spin_unlock(&dev->wl->hardirq_lock);
Michael Buesche4d6b792007-09-18 15:39:42 -04002039
2040 return ret;
2041}
2042
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002043/* SDIO interrupt handler. This runs in process context. */
2044static void b43_sdio_interrupt_handler(struct b43_wldev *dev)
2045{
2046 struct b43_wl *wl = dev->wl;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002047 irqreturn_t ret;
2048
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002049 mutex_lock(&wl->mutex);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002050
2051 ret = b43_do_interrupt(dev);
2052 if (ret == IRQ_WAKE_THREAD)
2053 b43_do_interrupt_thread(dev);
2054
Albert Herranz3dbba8e2009-09-10 19:34:49 +02002055 mutex_unlock(&wl->mutex);
2056}
2057
Michael Buesch1a9f5092009-01-23 21:21:51 +01002058void b43_do_release_fw(struct b43_firmware_file *fw)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002059{
2060 release_firmware(fw->data);
2061 fw->data = NULL;
2062 fw->filename = NULL;
2063}
2064
Michael Buesche4d6b792007-09-18 15:39:42 -04002065static void b43_release_firmware(struct b43_wldev *dev)
2066{
Larry Finger0673eff2014-01-12 15:11:38 -06002067 complete(&dev->fw_load_complete);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002068 b43_do_release_fw(&dev->fw.ucode);
2069 b43_do_release_fw(&dev->fw.pcm);
2070 b43_do_release_fw(&dev->fw.initvals);
2071 b43_do_release_fw(&dev->fw.initvals_band);
Michael Buesche4d6b792007-09-18 15:39:42 -04002072}
2073
Michael Buescheb189d8b2008-01-28 14:47:41 -08002074static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
Michael Buesche4d6b792007-09-18 15:39:42 -04002075{
Hannes Ederfc68ed42009-02-14 11:50:06 +00002076 const char text[] =
2077 "You must go to " \
2078 "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
2079 "and download the correct firmware for this driver version. " \
2080 "Please carefully read all instructions on this website.\n";
Michael Buescheb189d8b2008-01-28 14:47:41 -08002081
Michael Buescheb189d8b2008-01-28 14:47:41 -08002082 if (error)
2083 b43err(wl, text);
2084 else
2085 b43warn(wl, text);
Michael Buesche4d6b792007-09-18 15:39:42 -04002086}
2087
Larry Finger5e20a4b2012-12-20 15:55:01 -06002088static void b43_fw_cb(const struct firmware *firmware, void *context)
2089{
2090 struct b43_request_fw_context *ctx = context;
2091
2092 ctx->blob = firmware;
Larry Finger0673eff2014-01-12 15:11:38 -06002093 complete(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002094}
2095
Michael Buesch1a9f5092009-01-23 21:21:51 +01002096int b43_do_request_fw(struct b43_request_fw_context *ctx,
2097 const char *name,
Larry Finger5e20a4b2012-12-20 15:55:01 -06002098 struct b43_firmware_file *fw, bool async)
Michael Buesche4d6b792007-09-18 15:39:42 -04002099{
Michael Buesche4d6b792007-09-18 15:39:42 -04002100 struct b43_fw_header *hdr;
2101 u32 size;
2102 int err;
2103
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002104 if (!name) {
2105 /* Don't fetch anything. Free possibly cached firmware. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002106 /* FIXME: We should probably keep it anyway, to save some headache
2107 * on suspend/resume with multiband devices. */
2108 b43_do_release_fw(fw);
Michael Buesche4d6b792007-09-18 15:39:42 -04002109 return 0;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002110 }
2111 if (fw->filename) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002112 if ((fw->type == ctx->req_type) &&
2113 (strcmp(fw->filename, name) == 0))
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002114 return 0; /* Already have this fw. */
2115 /* Free the cached firmware first. */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002116 /* FIXME: We should probably do this later after we successfully
2117 * got the new fw. This could reduce headache with multiband devices.
2118 * We could also redesign this to cache the firmware for all possible
2119 * bands all the time. */
2120 b43_do_release_fw(fw);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002121 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002122
Michael Buesch1a9f5092009-01-23 21:21:51 +01002123 switch (ctx->req_type) {
2124 case B43_FWTYPE_PROPRIETARY:
2125 snprintf(ctx->fwname, sizeof(ctx->fwname),
2126 "b43%s/%s.fw",
2127 modparam_fwpostfix, name);
2128 break;
2129 case B43_FWTYPE_OPENSOURCE:
2130 snprintf(ctx->fwname, sizeof(ctx->fwname),
2131 "b43-open%s/%s.fw",
2132 modparam_fwpostfix, name);
2133 break;
2134 default:
2135 B43_WARN_ON(1);
2136 return -ENOSYS;
2137 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002138 if (async) {
2139 /* do this part asynchronously */
Larry Finger0673eff2014-01-12 15:11:38 -06002140 init_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002141 err = request_firmware_nowait(THIS_MODULE, 1, ctx->fwname,
2142 ctx->dev->dev->dev, GFP_KERNEL,
2143 ctx, b43_fw_cb);
2144 if (err < 0) {
2145 pr_err("Unable to load firmware\n");
2146 return err;
2147 }
Larry Finger0673eff2014-01-12 15:11:38 -06002148 wait_for_completion(&ctx->dev->fw_load_complete);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002149 if (ctx->blob)
2150 goto fw_ready;
2151 /* On some ARM systems, the async request will fail, but the next sync
Larry Finger0673eff2014-01-12 15:11:38 -06002152 * request works. For this reason, we fall through here
Larry Finger5e20a4b2012-12-20 15:55:01 -06002153 */
2154 }
2155 err = request_firmware(&ctx->blob, ctx->fwname,
2156 ctx->dev->dev->dev);
Michael Buesch68217832008-05-17 23:43:57 +02002157 if (err == -ENOENT) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002158 snprintf(ctx->errors[ctx->req_type],
2159 sizeof(ctx->errors[ctx->req_type]),
Larry Finger5e20a4b2012-12-20 15:55:01 -06002160 "Firmware file \"%s\" not found\n",
2161 ctx->fwname);
Michael Buesch68217832008-05-17 23:43:57 +02002162 return err;
2163 } else if (err) {
Michael Buesch1a9f5092009-01-23 21:21:51 +01002164 snprintf(ctx->errors[ctx->req_type],
2165 sizeof(ctx->errors[ctx->req_type]),
2166 "Firmware file \"%s\" request failed (err=%d)\n",
2167 ctx->fwname, err);
Michael Buesche4d6b792007-09-18 15:39:42 -04002168 return err;
2169 }
Larry Finger5e20a4b2012-12-20 15:55:01 -06002170fw_ready:
2171 if (ctx->blob->size < sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002172 goto err_format;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002173 hdr = (struct b43_fw_header *)(ctx->blob->data);
Michael Buesche4d6b792007-09-18 15:39:42 -04002174 switch (hdr->type) {
2175 case B43_FW_TYPE_UCODE:
2176 case B43_FW_TYPE_PCM:
2177 size = be32_to_cpu(hdr->size);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002178 if (size != ctx->blob->size - sizeof(struct b43_fw_header))
Michael Buesche4d6b792007-09-18 15:39:42 -04002179 goto err_format;
2180 /* fallthrough */
2181 case B43_FW_TYPE_IV:
2182 if (hdr->ver != 1)
2183 goto err_format;
2184 break;
2185 default:
2186 goto err_format;
2187 }
2188
Larry Finger5e20a4b2012-12-20 15:55:01 -06002189 fw->data = ctx->blob;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002190 fw->filename = name;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002191 fw->type = ctx->req_type;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002192
2193 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04002194
2195err_format:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002196 snprintf(ctx->errors[ctx->req_type],
2197 sizeof(ctx->errors[ctx->req_type]),
2198 "Firmware file \"%s\" format error.\n", ctx->fwname);
Larry Finger5e20a4b2012-12-20 15:55:01 -06002199 release_firmware(ctx->blob);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002200
Michael Buesche4d6b792007-09-18 15:39:42 -04002201 return -EPROTO;
2202}
2203
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002204/* http://bcm-v4.sipsolutions.net/802.11/Init/Firmware */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002205static int b43_try_request_fw(struct b43_request_fw_context *ctx)
Michael Buesche4d6b792007-09-18 15:39:42 -04002206{
Michael Buesch1a9f5092009-01-23 21:21:51 +01002207 struct b43_wldev *dev = ctx->dev;
2208 struct b43_firmware *fw = &ctx->dev->fw;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002209 struct b43_phy *phy = &dev->phy;
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002210 const u8 rev = ctx->dev->dev->core_rev;
Michael Buesche4d6b792007-09-18 15:39:42 -04002211 const char *filename;
Michael Buesche4d6b792007-09-18 15:39:42 -04002212 int err;
2213
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002214 /* Get microcode */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002215 filename = NULL;
2216 switch (rev) {
2217 case 42:
2218 if (phy->type == B43_PHYTYPE_AC)
2219 filename = "ucode42";
2220 break;
2221 case 33:
2222 if (phy->type == B43_PHYTYPE_LCN40)
2223 filename = "ucode33_lcn40";
2224 break;
2225 case 30:
2226 if (phy->type == B43_PHYTYPE_N)
2227 filename = "ucode30_mimo";
2228 break;
2229 case 29:
2230 if (phy->type == B43_PHYTYPE_HT)
2231 filename = "ucode29_mimo";
2232 break;
2233 case 26:
2234 if (phy->type == B43_PHYTYPE_HT)
2235 filename = "ucode26_mimo";
2236 break;
2237 case 28:
2238 case 25:
2239 if (phy->type == B43_PHYTYPE_N)
2240 filename = "ucode25_mimo";
2241 else if (phy->type == B43_PHYTYPE_LCN)
2242 filename = "ucode25_lcn";
2243 break;
2244 case 24:
2245 if (phy->type == B43_PHYTYPE_LCN)
2246 filename = "ucode24_lcn";
2247 break;
2248 case 23:
2249 if (phy->type == B43_PHYTYPE_N)
2250 filename = "ucode16_mimo";
2251 break;
2252 case 16 ... 19:
2253 if (phy->type == B43_PHYTYPE_N)
2254 filename = "ucode16_mimo";
2255 else if (phy->type == B43_PHYTYPE_LP)
2256 filename = "ucode16_lp";
2257 break;
2258 case 15:
Gábor Stefanik759b9732009-08-14 14:39:53 +02002259 filename = "ucode15";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002260 break;
2261 case 14:
2262 filename = "ucode14";
2263 break;
2264 case 13:
2265 filename = "ucode13";
2266 break;
2267 case 11 ... 12:
2268 filename = "ucode11";
2269 break;
2270 case 5 ... 10:
2271 filename = "ucode5";
2272 break;
Rafał Miłecki6ff1e5c2011-07-06 17:41:55 +02002273 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002274 if (!filename)
2275 goto err_no_ucode;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002276 err = b43_do_request_fw(ctx, filename, &fw->ucode, true);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002277 if (err)
2278 goto err_load;
2279
2280 /* Get PCM code */
2281 if ((rev >= 5) && (rev <= 10))
2282 filename = "pcm5";
2283 else if (rev >= 11)
2284 filename = NULL;
2285 else
2286 goto err_no_pcm;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002287 fw->pcm_request_failed = false;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002288 err = b43_do_request_fw(ctx, filename, &fw->pcm, false);
Michael Buesch68217832008-05-17 23:43:57 +02002289 if (err == -ENOENT) {
2290 /* We did not find a PCM file? Not fatal, but
2291 * core rev <= 10 must do without hwcrypto then. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002292 fw->pcm_request_failed = true;
Michael Buesch68217832008-05-17 23:43:57 +02002293 } else if (err)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002294 goto err_load;
2295
2296 /* Get initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002297 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002298 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002299 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002300 if (rev == 13)
Larry.Finger@lwfinger.nete9304882008-05-15 14:07:36 -05002301 filename = "b0g0initvals13";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002302 else if (rev >= 5 && rev <= 10)
2303 filename = "b0g0initvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002304 break;
2305 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002306 if (rev == 30)
2307 filename = "n16initvals30";
2308 else if (rev == 28 || rev == 25)
2309 filename = "n0initvals25";
2310 else if (rev == 24)
2311 filename = "n0initvals24";
2312 else if (rev == 23)
2313 filename = "n0initvals16"; /* What about n0initvals22? */
2314 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002315 filename = "n0initvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002316 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002317 filename = "n0initvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002318 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002319 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002320 if (rev >= 16 && rev <= 18)
2321 filename = "lp0initvals16";
2322 else if (rev == 15)
2323 filename = "lp0initvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002324 else if (rev == 14)
2325 filename = "lp0initvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002326 else if (rev == 13)
2327 filename = "lp0initvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002328 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002329 case B43_PHYTYPE_HT:
2330 if (rev == 29)
2331 filename = "ht0initvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002332 else if (rev == 26)
2333 filename = "ht0initvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002334 break;
2335 case B43_PHYTYPE_LCN:
2336 if (rev == 24)
2337 filename = "lcn0initvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002338 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002339 case B43_PHYTYPE_LCN40:
2340 if (rev == 33)
2341 filename = "lcn400initvals33";
2342 break;
2343 case B43_PHYTYPE_AC:
2344 if (rev == 42)
2345 filename = "ac1initvals42";
2346 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002347 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002348 if (!filename)
2349 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002350 err = b43_do_request_fw(ctx, filename, &fw->initvals, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002351 if (err)
2352 goto err_load;
2353
2354 /* Get bandswitch initvals */
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002355 filename = NULL;
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002356 switch (dev->phy.type) {
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002357 case B43_PHYTYPE_G:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002358 if (rev == 13)
2359 filename = "b0g0bsinitvals13";
2360 else if (rev >= 5 && rev <= 10)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002361 filename = "b0g0bsinitvals5";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002362 break;
2363 case B43_PHYTYPE_N:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002364 if (rev == 30)
2365 filename = "n16bsinitvals30";
2366 else if (rev == 28 || rev == 25)
2367 filename = "n0bsinitvals25";
2368 else if (rev == 24)
2369 filename = "n0bsinitvals24";
2370 else if (rev == 23)
2371 filename = "n0bsinitvals16"; /* What about n0bsinitvals22? */
2372 else if (rev >= 16 && rev <= 18)
Rafał Miłeckie41596a2010-12-21 11:50:19 +01002373 filename = "n0bsinitvals16";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002374 else if (rev >= 11 && rev <= 12)
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002375 filename = "n0bsinitvals11";
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002376 break;
Gábor Stefanik759b9732009-08-14 14:39:53 +02002377 case B43_PHYTYPE_LP:
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002378 if (rev >= 16 && rev <= 18)
2379 filename = "lp0bsinitvals16";
2380 else if (rev == 15)
2381 filename = "lp0bsinitvals15";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002382 else if (rev == 14)
2383 filename = "lp0bsinitvals14";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002384 else if (rev == 13)
2385 filename = "lp0bsinitvals13";
Gábor Stefanik759b9732009-08-14 14:39:53 +02002386 break;
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002387 case B43_PHYTYPE_HT:
2388 if (rev == 29)
2389 filename = "ht0bsinitvals29";
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002390 else if (rev == 26)
2391 filename = "ht0bsinitvals26";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002392 break;
2393 case B43_PHYTYPE_LCN:
2394 if (rev == 24)
2395 filename = "lcn0bsinitvals24";
Rafał Miłecki8b9bda72011-07-07 18:58:24 +02002396 break;
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002397 case B43_PHYTYPE_LCN40:
2398 if (rev == 33)
2399 filename = "lcn400bsinitvals33";
2400 break;
2401 case B43_PHYTYPE_AC:
2402 if (rev == 42)
2403 filename = "ac1bsinitvals42";
2404 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04002405 }
Rafał Miłeckia60f99f2014-06-24 10:50:41 +02002406 if (!filename)
2407 goto err_no_initvals;
Larry Finger5e20a4b2012-12-20 15:55:01 -06002408 err = b43_do_request_fw(ctx, filename, &fw->initvals_band, false);
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002409 if (err)
2410 goto err_load;
Michael Buesche4d6b792007-09-18 15:39:42 -04002411
Johannes Berg097b0e12012-07-17 17:12:29 +02002412 fw->opensource = (ctx->req_type == B43_FWTYPE_OPENSOURCE);
2413
Michael Buesche4d6b792007-09-18 15:39:42 -04002414 return 0;
2415
Michael Buesche4d6b792007-09-18 15:39:42 -04002416err_no_ucode:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002417 err = ctx->fatal_failure = -EOPNOTSUPP;
2418 b43err(dev->wl, "The driver does not know which firmware (ucode) "
2419 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002420 goto error;
2421
2422err_no_pcm:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002423 err = ctx->fatal_failure = -EOPNOTSUPP;
2424 b43err(dev->wl, "The driver does not know which firmware (PCM) "
2425 "is required for your device (wl-core rev %u)\n", rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04002426 goto error;
2427
2428err_no_initvals:
Michael Buesch1a9f5092009-01-23 21:21:51 +01002429 err = ctx->fatal_failure = -EOPNOTSUPP;
2430 b43err(dev->wl, "The driver does not know which firmware (initvals) "
2431 "is required for your device (wl-core rev %u)\n", rev);
2432 goto error;
2433
2434err_load:
2435 /* We failed to load this firmware image. The error message
2436 * already is in ctx->errors. Return and let our caller decide
2437 * what to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -04002438 goto error;
2439
2440error:
2441 b43_release_firmware(dev);
2442 return err;
2443}
2444
Larry Finger6b6fa582012-03-08 22:27:46 -06002445static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl);
2446static void b43_one_core_detach(struct b43_bus_dev *dev);
Larry Finger09164042014-01-12 15:11:37 -06002447static int b43_rng_init(struct b43_wl *wl);
Larry Finger6b6fa582012-03-08 22:27:46 -06002448
2449static void b43_request_firmware(struct work_struct *work)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002450{
Larry Finger6b6fa582012-03-08 22:27:46 -06002451 struct b43_wl *wl = container_of(work,
2452 struct b43_wl, firmware_load);
2453 struct b43_wldev *dev = wl->current_dev;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002454 struct b43_request_fw_context *ctx;
2455 unsigned int i;
2456 int err;
2457 const char *errmsg;
2458
2459 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2460 if (!ctx)
Larry Finger6b6fa582012-03-08 22:27:46 -06002461 return;
Michael Buesch1a9f5092009-01-23 21:21:51 +01002462 ctx->dev = dev;
2463
2464 ctx->req_type = B43_FWTYPE_PROPRIETARY;
2465 err = b43_try_request_fw(ctx);
2466 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002467 goto start_ieee80211; /* Successfully loaded it. */
2468 /* Was fw version known? */
2469 if (ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002470 goto out;
2471
Larry Finger6b6fa582012-03-08 22:27:46 -06002472 /* proprietary fw not found, try open source */
Michael Buesch1a9f5092009-01-23 21:21:51 +01002473 ctx->req_type = B43_FWTYPE_OPENSOURCE;
2474 err = b43_try_request_fw(ctx);
2475 if (!err)
Larry Finger6b6fa582012-03-08 22:27:46 -06002476 goto start_ieee80211; /* Successfully loaded it. */
2477 if(ctx->fatal_failure)
Michael Buesch1a9f5092009-01-23 21:21:51 +01002478 goto out;
2479
2480 /* Could not find a usable firmware. Print the errors. */
2481 for (i = 0; i < B43_NR_FWTYPES; i++) {
2482 errmsg = ctx->errors[i];
2483 if (strlen(errmsg))
Kees Cooke0e29b62013-05-10 14:48:21 -07002484 b43err(dev->wl, "%s", errmsg);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002485 }
2486 b43_print_fw_helptext(dev->wl, 1);
Larry Finger6b6fa582012-03-08 22:27:46 -06002487 goto out;
2488
2489start_ieee80211:
Johannes Berg097b0e12012-07-17 17:12:29 +02002490 wl->hw->queues = B43_QOS_QUEUE_NUM;
2491 if (!modparam_qos || dev->fw.opensource)
2492 wl->hw->queues = 1;
2493
Larry Finger6b6fa582012-03-08 22:27:46 -06002494 err = ieee80211_register_hw(wl->hw);
2495 if (err)
2496 goto err_one_core_detach;
Oleksij Rempele64add22012-06-05 20:39:32 +02002497 wl->hw_registred = true;
Larry Finger6b6fa582012-03-08 22:27:46 -06002498 b43_leds_register(wl->current_dev);
Larry Finger09164042014-01-12 15:11:37 -06002499
2500 /* Register HW RNG driver */
2501 b43_rng_init(wl);
2502
Larry Finger6b6fa582012-03-08 22:27:46 -06002503 goto out;
2504
2505err_one_core_detach:
2506 b43_one_core_detach(dev->dev);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002507
2508out:
2509 kfree(ctx);
Michael Buesch1a9f5092009-01-23 21:21:51 +01002510}
2511
Michael Buesche4d6b792007-09-18 15:39:42 -04002512static int b43_upload_microcode(struct b43_wldev *dev)
2513{
John W. Linville652caa52010-07-29 13:27:28 -04002514 struct wiphy *wiphy = dev->wl->hw->wiphy;
Michael Buesche4d6b792007-09-18 15:39:42 -04002515 const size_t hdr_len = sizeof(struct b43_fw_header);
2516 const __be32 *data;
2517 unsigned int i, len;
2518 u16 fwrev, fwpatch, fwdate, fwtime;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002519 u32 tmp, macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04002520 int err = 0;
2521
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002522 /* Jump the microcode PSM to offset 0 */
2523 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2524 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2525 macctl |= B43_MACCTL_PSM_JMP0;
2526 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2527 /* Zero out all microcode PSM registers and shared memory. */
2528 for (i = 0; i < 64; i++)
2529 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2530 for (i = 0; i < 4096; i += 2)
2531 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2532
Michael Buesche4d6b792007-09-18 15:39:42 -04002533 /* Upload Microcode. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002534 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2535 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002536 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2537 for (i = 0; i < len; i++) {
2538 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2539 udelay(10);
2540 }
2541
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002542 if (dev->fw.pcm.data) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002543 /* Upload PCM data. */
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002544 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2545 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002546 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2547 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2548 /* No need for autoinc bit in SHM_HW */
2549 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2550 for (i = 0; i < len; i++) {
2551 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2552 udelay(10);
2553 }
2554 }
2555
2556 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002557
2558 /* Start the microcode PSM */
Rafał Miłecki50566352012-01-02 19:31:21 +01002559 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_JMP0,
2560 B43_MACCTL_PSM_RUN);
Michael Buesche4d6b792007-09-18 15:39:42 -04002561
2562 /* Wait for the microcode to load and respond */
2563 i = 0;
2564 while (1) {
2565 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2566 if (tmp == B43_IRQ_MAC_SUSPENDED)
2567 break;
2568 i++;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002569 if (i >= 20) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002570 b43err(dev->wl, "Microcode not responding\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002571 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002572 err = -ENODEV;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002573 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002574 }
Michael Buesche175e992009-09-11 18:31:32 +02002575 msleep(50);
Michael Buesche4d6b792007-09-18 15:39:42 -04002576 }
2577 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2578
2579 /* Get and check the revisions. */
2580 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2581 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2582 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2583 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2584
2585 if (fwrev <= 0x128) {
2586 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2587 "binary drivers older than version 4.x is unsupported. "
2588 "You must upgrade your firmware files.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002589 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002590 err = -EOPNOTSUPP;
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002591 goto error;
Michael Buesche4d6b792007-09-18 15:39:42 -04002592 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002593 dev->fw.rev = fwrev;
2594 dev->fw.patch = fwpatch;
Rafał Miłecki5d852902011-08-11 15:07:16 +02002595 if (dev->fw.rev >= 598)
2596 dev->fw.hdr_format = B43_FW_HDR_598;
2597 else if (dev->fw.rev >= 410)
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002598 dev->fw.hdr_format = B43_FW_HDR_410;
2599 else
2600 dev->fw.hdr_format = B43_FW_HDR_351;
Johannes Berg097b0e12012-07-17 17:12:29 +02002601 WARN_ON(dev->fw.opensource != (fwdate == 0xFFFF));
Michael Buesche48b0ee2008-05-17 22:44:35 +02002602
Johannes Berg097b0e12012-07-17 17:12:29 +02002603 dev->qos_enabled = dev->wl->hw->queues > 1;
Michael Buesch403a3a12009-06-08 21:04:57 +02002604 /* Default to firmware/hardware crypto acceleration. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002605 dev->hwcrypto_enabled = true;
Michael Buesch403a3a12009-06-08 21:04:57 +02002606
Michael Buesche48b0ee2008-05-17 22:44:35 +02002607 if (dev->fw.opensource) {
Michael Buesch403a3a12009-06-08 21:04:57 +02002608 u16 fwcapa;
2609
Michael Buesche48b0ee2008-05-17 22:44:35 +02002610 /* Patchlevel info is encoded in the "time" field. */
2611 dev->fw.patch = fwtime;
Michael Buesch403a3a12009-06-08 21:04:57 +02002612 b43info(dev->wl, "Loading OpenSource firmware version %u.%u\n",
2613 dev->fw.rev, dev->fw.patch);
2614
2615 fwcapa = b43_fwcapa_read(dev);
2616 if (!(fwcapa & B43_FWCAPA_HWCRYPTO) || dev->fw.pcm_request_failed) {
2617 b43info(dev->wl, "Hardware crypto acceleration not supported by firmware\n");
2618 /* Disable hardware crypto and fall back to software crypto. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00002619 dev->hwcrypto_enabled = false;
Michael Buesch403a3a12009-06-08 21:04:57 +02002620 }
Johannes Berg097b0e12012-07-17 17:12:29 +02002621 /* adding QoS support should use an offline discovery mechanism */
2622 WARN(fwcapa & B43_FWCAPA_QOS, "QoS in OpenFW not supported\n");
Michael Buesche48b0ee2008-05-17 22:44:35 +02002623 } else {
2624 b43info(dev->wl, "Loading firmware version %u.%u "
2625 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2626 fwrev, fwpatch,
2627 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2628 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
Michael Buesch68217832008-05-17 23:43:57 +02002629 if (dev->fw.pcm_request_failed) {
2630 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2631 "Hardware accelerated cryptography is disabled.\n");
2632 b43_print_fw_helptext(dev->wl, 0);
2633 }
Michael Buesche48b0ee2008-05-17 22:44:35 +02002634 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002635
John W. Linville652caa52010-07-29 13:27:28 -04002636 snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
2637 dev->fw.rev, dev->fw.patch);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002638 wiphy->hw_version = dev->dev->core_id;
John W. Linville652caa52010-07-29 13:27:28 -04002639
Rafał Miłeckiefe02492011-08-11 15:07:15 +02002640 if (dev->fw.hdr_format == B43_FW_HDR_351) {
Michael Bueschc5572892008-12-27 18:26:39 +01002641 /* We're over the deadline, but we keep support for old fw
2642 * until it turns out to be in major conflict with something new. */
Michael Buescheb189d8b2008-01-28 14:47:41 -08002643 b43warn(dev->wl, "You are using an old firmware image. "
Michael Bueschc5572892008-12-27 18:26:39 +01002644 "Support for old firmware will be removed soon "
2645 "(official deadline was July 2008).\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002646 b43_print_fw_helptext(dev->wl, 0);
2647 }
2648
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002649 return 0;
2650
2651error:
Rafał Miłecki50566352012-01-02 19:31:21 +01002652 /* Stop the microcode PSM. */
2653 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
2654 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01002655
Michael Buesche4d6b792007-09-18 15:39:42 -04002656 return err;
2657}
2658
2659static int b43_write_initvals(struct b43_wldev *dev,
2660 const struct b43_iv *ivals,
2661 size_t count,
2662 size_t array_size)
2663{
2664 const struct b43_iv *iv;
2665 u16 offset;
2666 size_t i;
2667 bool bit32;
2668
2669 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2670 iv = ivals;
2671 for (i = 0; i < count; i++) {
2672 if (array_size < sizeof(iv->offset_size))
2673 goto err_format;
2674 array_size -= sizeof(iv->offset_size);
2675 offset = be16_to_cpu(iv->offset_size);
2676 bit32 = !!(offset & B43_IV_32BIT);
2677 offset &= B43_IV_OFFSET_MASK;
2678 if (offset >= 0x1000)
2679 goto err_format;
2680 if (bit32) {
2681 u32 value;
2682
2683 if (array_size < sizeof(iv->data.d32))
2684 goto err_format;
2685 array_size -= sizeof(iv->data.d32);
2686
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002687 value = get_unaligned_be32(&iv->data.d32);
Michael Buesche4d6b792007-09-18 15:39:42 -04002688 b43_write32(dev, offset, value);
2689
2690 iv = (const struct b43_iv *)((const uint8_t *)iv +
2691 sizeof(__be16) +
2692 sizeof(__be32));
2693 } else {
2694 u16 value;
2695
2696 if (array_size < sizeof(iv->data.d16))
2697 goto err_format;
2698 array_size -= sizeof(iv->data.d16);
2699
2700 value = be16_to_cpu(iv->data.d16);
2701 b43_write16(dev, offset, value);
2702
2703 iv = (const struct b43_iv *)((const uint8_t *)iv +
2704 sizeof(__be16) +
2705 sizeof(__be16));
2706 }
2707 }
2708 if (array_size)
2709 goto err_format;
2710
2711 return 0;
2712
2713err_format:
2714 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
Michael Buescheb189d8b2008-01-28 14:47:41 -08002715 b43_print_fw_helptext(dev->wl, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002716
2717 return -EPROTO;
2718}
2719
2720static int b43_upload_initvals(struct b43_wldev *dev)
2721{
2722 const size_t hdr_len = sizeof(struct b43_fw_header);
2723 const struct b43_fw_header *hdr;
2724 struct b43_firmware *fw = &dev->fw;
2725 const struct b43_iv *ivals;
2726 size_t count;
Michael Buesche4d6b792007-09-18 15:39:42 -04002727
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002728 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2729 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002730 count = be32_to_cpu(hdr->size);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002731 return b43_write_initvals(dev, ivals, count,
Michael Buesch61cb5dd2008-01-21 19:55:09 +01002732 fw->initvals.data->size - hdr_len);
Rafał Miłecki0f684232014-05-17 23:24:53 +02002733}
Michael Buesche4d6b792007-09-18 15:39:42 -04002734
Rafał Miłecki0f684232014-05-17 23:24:53 +02002735static int b43_upload_initvals_band(struct b43_wldev *dev)
2736{
2737 const size_t hdr_len = sizeof(struct b43_fw_header);
2738 const struct b43_fw_header *hdr;
2739 struct b43_firmware *fw = &dev->fw;
2740 const struct b43_iv *ivals;
2741 size_t count;
2742
2743 if (!fw->initvals_band.data)
2744 return 0;
2745
2746 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2747 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2748 count = be32_to_cpu(hdr->size);
2749 return b43_write_initvals(dev, ivals, count,
2750 fw->initvals_band.data->size - hdr_len);
Michael Buesche4d6b792007-09-18 15:39:42 -04002751}
2752
2753/* Initialize the GPIOs
2754 * http://bcm-specs.sipsolutions.net/GPIO
2755 */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002756
2757#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002758static struct ssb_device *b43_ssb_gpio_dev(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002759{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02002760 struct ssb_bus *bus = dev->dev->sdev->bus;
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002761
2762#ifdef CONFIG_SSB_DRIVER_PCICORE
2763 return (bus->chipco.dev ? bus->chipco.dev : bus->pcicore.dev);
2764#else
2765 return bus->chipco.dev;
2766#endif
2767}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002768#endif
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002769
Michael Buesche4d6b792007-09-18 15:39:42 -04002770static int b43_gpio_init(struct b43_wldev *dev)
2771{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002772#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002773 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002774#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002775 u32 mask, set;
2776
Rafał Miłecki50566352012-01-02 19:31:21 +01002777 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_GPOUTSMSK, 0);
2778 b43_maskset16(dev, B43_MMIO_GPIO_MASK, ~0, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04002779
2780 mask = 0x0000001F;
2781 set = 0x0000000F;
Rafał Miłeckic244e082011-05-18 02:06:41 +02002782 if (dev->dev->chip_id == 0x4301) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002783 mask |= 0x0060;
2784 set |= 0x0060;
Rafał Miłecki828afd22012-07-23 22:57:01 +02002785 } else if (dev->dev->chip_id == 0x5354) {
2786 /* Don't allow overtaking buttons GPIOs */
2787 set &= 0x2; /* 0x2 is LED GPIO on BCM5354 */
Michael Buesche4d6b792007-09-18 15:39:42 -04002788 }
Rafał Miłecki828afd22012-07-23 22:57:01 +02002789
Michael Buesche4d6b792007-09-18 15:39:42 -04002790 if (0 /* FIXME: conditional unknown */ ) {
2791 b43_write16(dev, B43_MMIO_GPIO_MASK,
2792 b43_read16(dev, B43_MMIO_GPIO_MASK)
2793 | 0x0100);
Rafał Miłecki828afd22012-07-23 22:57:01 +02002794 /* BT Coexistance Input */
2795 mask |= 0x0080;
2796 set |= 0x0080;
2797 /* BT Coexistance Out */
2798 mask |= 0x0100;
2799 set |= 0x0100;
Michael Buesche4d6b792007-09-18 15:39:42 -04002800 }
Rafał Miłecki05814832011-05-18 02:06:39 +02002801 if (dev->dev->bus_sprom->boardflags_lo & B43_BFL_PACTRL) {
Rafał Miłecki828afd22012-07-23 22:57:01 +02002802 /* PA is controlled by gpio 9, let ucode handle it */
Michael Buesche4d6b792007-09-18 15:39:42 -04002803 b43_write16(dev, B43_MMIO_GPIO_MASK,
2804 b43_read16(dev, B43_MMIO_GPIO_MASK)
2805 | 0x0200);
2806 mask |= 0x0200;
2807 set |= 0x0200;
2808 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002809
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002810 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002811#ifdef CONFIG_B43_BCMA
2812 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002813 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, mask, set);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002814 break;
2815#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002816#ifdef CONFIG_B43_SSB
2817 case B43_BUS_SSB:
2818 gpiodev = b43_ssb_gpio_dev(dev);
2819 if (gpiodev)
2820 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2821 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
Rafał Miłecki828afd22012-07-23 22:57:01 +02002822 & ~mask) | set);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002823 break;
2824#endif
2825 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002826
2827 return 0;
2828}
2829
2830/* Turn off all GPIO stuff. Call this on module unload, for example. */
2831static void b43_gpio_cleanup(struct b43_wldev *dev)
2832{
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002833#ifdef CONFIG_B43_SSB
Rafał Miłeckic4a2a082011-05-17 18:57:27 +02002834 struct ssb_device *gpiodev;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02002835#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04002836
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002837 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002838#ifdef CONFIG_B43_BCMA
2839 case B43_BUS_BCMA:
Hauke Mehrtens0a64bae2013-03-21 16:19:45 +01002840 bcma_chipco_gpio_control(&dev->dev->bdev->bus->drv_cc, ~0, 0);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002841 break;
2842#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002843#ifdef CONFIG_B43_SSB
2844 case B43_BUS_SSB:
2845 gpiodev = b43_ssb_gpio_dev(dev);
2846 if (gpiodev)
2847 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2848 break;
2849#endif
2850 }
Michael Buesche4d6b792007-09-18 15:39:42 -04002851}
2852
2853/* http://bcm-specs.sipsolutions.net/EnableMac */
Michael Bueschf5eda472008-04-20 16:03:32 +02002854void b43_mac_enable(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002855{
Michael Buesch923fd702008-06-20 18:02:08 +02002856 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2857 u16 fwstate;
2858
2859 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2860 B43_SHM_SH_UCODESTAT);
2861 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2862 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2863 b43err(dev->wl, "b43_mac_enable(): The firmware "
2864 "should be suspended, but current state is %u\n",
2865 fwstate);
2866 }
2867 }
2868
Michael Buesche4d6b792007-09-18 15:39:42 -04002869 dev->mac_suspended--;
2870 B43_WARN_ON(dev->mac_suspended < 0);
2871 if (dev->mac_suspended == 0) {
Rafał Miłecki50566352012-01-02 19:31:21 +01002872 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_ENABLED);
Michael Buesche4d6b792007-09-18 15:39:42 -04002873 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2874 B43_IRQ_MAC_SUSPENDED);
2875 /* Commit writes */
2876 b43_read32(dev, B43_MMIO_MACCTL);
2877 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2878 b43_power_saving_ctl_bits(dev, 0);
2879 }
2880}
2881
2882/* http://bcm-specs.sipsolutions.net/SuspendMAC */
Michael Bueschf5eda472008-04-20 16:03:32 +02002883void b43_mac_suspend(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04002884{
2885 int i;
2886 u32 tmp;
2887
Michael Buesch05b64b32007-09-28 16:19:03 +02002888 might_sleep();
Michael Buesche4d6b792007-09-18 15:39:42 -04002889 B43_WARN_ON(dev->mac_suspended < 0);
Michael Buesch05b64b32007-09-28 16:19:03 +02002890
Michael Buesche4d6b792007-09-18 15:39:42 -04002891 if (dev->mac_suspended == 0) {
2892 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Rafał Miłecki50566352012-01-02 19:31:21 +01002893 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_ENABLED, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04002894 /* force pci to flush the write */
2895 b43_read32(dev, B43_MMIO_MACCTL);
Michael Bueschba380012008-04-15 21:13:36 +02002896 for (i = 35; i; i--) {
2897 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2898 if (tmp & B43_IRQ_MAC_SUSPENDED)
2899 goto out;
2900 udelay(10);
2901 }
2902 /* Hm, it seems this will take some time. Use msleep(). */
Michael Buesch05b64b32007-09-28 16:19:03 +02002903 for (i = 40; i; i--) {
Michael Buesche4d6b792007-09-18 15:39:42 -04002904 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2905 if (tmp & B43_IRQ_MAC_SUSPENDED)
2906 goto out;
Michael Buesch05b64b32007-09-28 16:19:03 +02002907 msleep(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04002908 }
2909 b43err(dev->wl, "MAC suspend failed\n");
2910 }
Michael Buesch05b64b32007-09-28 16:19:03 +02002911out:
Michael Buesche4d6b792007-09-18 15:39:42 -04002912 dev->mac_suspended++;
2913}
2914
Rafał Miłecki858a1652011-05-10 16:05:33 +02002915/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MacPhyClkSet */
2916void b43_mac_phy_clock_set(struct b43_wldev *dev, bool on)
2917{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002918 u32 tmp;
2919
2920 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002921#ifdef CONFIG_B43_BCMA
2922 case B43_BUS_BCMA:
Rafał Miłecki36677872011-07-16 18:27:55 +02002923 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002924 if (on)
2925 tmp |= B43_BCMA_IOCTL_MACPHYCLKEN;
2926 else
2927 tmp &= ~B43_BCMA_IOCTL_MACPHYCLKEN;
Rafał Miłecki36677872011-07-16 18:27:55 +02002928 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02002929 break;
2930#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02002931#ifdef CONFIG_B43_SSB
2932 case B43_BUS_SSB:
2933 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
2934 if (on)
2935 tmp |= B43_TMSLOW_MACPHYCLKEN;
2936 else
2937 tmp &= ~B43_TMSLOW_MACPHYCLKEN;
2938 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
2939 break;
2940#endif
2941 }
Rafał Miłecki858a1652011-05-10 16:05:33 +02002942}
2943
Michael Buesche4d6b792007-09-18 15:39:42 -04002944static void b43_adjust_opmode(struct b43_wldev *dev)
2945{
2946 struct b43_wl *wl = dev->wl;
2947 u32 ctl;
2948 u16 cfp_pretbtt;
2949
2950 ctl = b43_read32(dev, B43_MMIO_MACCTL);
2951 /* Reset status to STA infrastructure mode. */
2952 ctl &= ~B43_MACCTL_AP;
2953 ctl &= ~B43_MACCTL_KEEP_CTL;
2954 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
2955 ctl &= ~B43_MACCTL_KEEP_BAD;
2956 ctl &= ~B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002957 ctl &= ~B43_MACCTL_BEACPROMISC;
Michael Buesche4d6b792007-09-18 15:39:42 -04002958 ctl |= B43_MACCTL_INFRA;
2959
Johannes Berg05c914f2008-09-11 00:01:58 +02002960 if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
2961 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
Johannes Berg4150c572007-09-17 01:29:23 -04002962 ctl |= B43_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02002963 else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04002964 ctl &= ~B43_MACCTL_INFRA;
2965
2966 if (wl->filter_flags & FIF_CONTROL)
Michael Buesche4d6b792007-09-18 15:39:42 -04002967 ctl |= B43_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04002968 if (wl->filter_flags & FIF_FCSFAIL)
2969 ctl |= B43_MACCTL_KEEP_BAD;
2970 if (wl->filter_flags & FIF_PLCPFAIL)
2971 ctl |= B43_MACCTL_KEEP_BADPLCP;
2972 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Michael Buesche4d6b792007-09-18 15:39:42 -04002973 ctl |= B43_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04002974 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2975 ctl |= B43_MACCTL_BEACPROMISC;
2976
Michael Buesche4d6b792007-09-18 15:39:42 -04002977 /* Workaround: On old hardware the HW-MAC-address-filter
2978 * doesn't work properly, so always run promisc in filter
2979 * it in software. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02002980 if (dev->dev->core_rev <= 4)
Michael Buesche4d6b792007-09-18 15:39:42 -04002981 ctl |= B43_MACCTL_PROMISC;
2982
2983 b43_write32(dev, B43_MMIO_MACCTL, ctl);
2984
2985 cfp_pretbtt = 2;
2986 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
Rafał Miłeckic244e082011-05-18 02:06:41 +02002987 if (dev->dev->chip_id == 0x4306 &&
2988 dev->dev->chip_rev == 3)
Michael Buesche4d6b792007-09-18 15:39:42 -04002989 cfp_pretbtt = 100;
2990 else
2991 cfp_pretbtt = 50;
2992 }
2993 b43_write16(dev, 0x612, cfp_pretbtt);
Michael Buesch09ebe2f2009-09-12 00:52:48 +02002994
2995 /* FIXME: We don't currently implement the PMQ mechanism,
2996 * so always disable it. If we want to implement PMQ,
2997 * we need to enable it here (clear DISCPMQ) in AP mode.
2998 */
Rafał Miłecki50566352012-01-02 19:31:21 +01002999 if (0 /* ctl & B43_MACCTL_AP */)
3000 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_DISCPMQ, 0);
3001 else
3002 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_DISCPMQ);
Michael Buesche4d6b792007-09-18 15:39:42 -04003003}
3004
3005static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
3006{
3007 u16 offset;
3008
3009 if (is_ofdm) {
3010 offset = 0x480;
3011 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
3012 } else {
3013 offset = 0x4C0;
3014 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
3015 }
3016 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
3017 b43_shm_read16(dev, B43_SHM_SHARED, offset));
3018}
3019
3020static void b43_rate_memory_init(struct b43_wldev *dev)
3021{
3022 switch (dev->phy.type) {
3023 case B43_PHYTYPE_A:
3024 case B43_PHYTYPE_G:
Michael Buesch53a6e232008-01-13 21:23:44 +01003025 case B43_PHYTYPE_N:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02003026 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +02003027 case B43_PHYTYPE_HT:
Rafał Miłecki0b4ff452011-08-31 23:36:16 +02003028 case B43_PHYTYPE_LCN:
Michael Buesche4d6b792007-09-18 15:39:42 -04003029 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
3030 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
3031 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
3032 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
3033 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
3034 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
3035 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
3036 if (dev->phy.type == B43_PHYTYPE_A)
3037 break;
3038 /* fallthrough */
3039 case B43_PHYTYPE_B:
3040 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
3041 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
3042 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
3043 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
3044 break;
3045 default:
3046 B43_WARN_ON(1);
3047 }
3048}
3049
Michael Buesch5042c502008-04-05 15:05:00 +02003050/* Set the default values for the PHY TX Control Words. */
3051static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
3052{
3053 u16 ctl = 0;
3054
3055 ctl |= B43_TXH_PHY_ENC_CCK;
3056 ctl |= B43_TXH_PHY_ANT01AUTO;
3057 ctl |= B43_TXH_PHY_TXPWR;
3058
3059 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
3060 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
3061 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
3062}
3063
Michael Buesche4d6b792007-09-18 15:39:42 -04003064/* Set the TX-Antenna for management frames sent by firmware. */
3065static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
3066{
Michael Buesch5042c502008-04-05 15:05:00 +02003067 u16 ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003068 u16 tmp;
3069
Michael Buesch5042c502008-04-05 15:05:00 +02003070 ant = b43_antenna_to_phyctl(antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003071
Michael Buesche4d6b792007-09-18 15:39:42 -04003072 /* For ACK/CTS */
3073 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003074 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003075 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
3076 /* For Probe Resposes */
3077 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
Michael Buescheb189d8b2008-01-28 14:47:41 -08003078 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
Michael Buesche4d6b792007-09-18 15:39:42 -04003079 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
3080}
3081
3082/* This is the opposite of b43_chip_init() */
3083static void b43_chip_exit(struct b43_wldev *dev)
3084{
Michael Bueschfb111372008-09-02 13:00:34 +02003085 b43_phy_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003086 b43_gpio_cleanup(dev);
3087 /* firmware is released later */
3088}
3089
3090/* Initialize the chip
3091 * http://bcm-specs.sipsolutions.net/ChipInit
3092 */
3093static int b43_chip_init(struct b43_wldev *dev)
3094{
3095 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +02003096 int err;
Rafał Miłecki858a1652011-05-10 16:05:33 +02003097 u32 macctl;
Michael Buesche4d6b792007-09-18 15:39:42 -04003098 u16 value16;
3099
Michael Buesch1f7d87b2008-01-22 20:23:34 +01003100 /* Initialize the MAC control */
3101 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
3102 if (dev->phy.gmode)
3103 macctl |= B43_MACCTL_GMODE;
3104 macctl |= B43_MACCTL_INFRA;
3105 b43_write32(dev, B43_MMIO_MACCTL, macctl);
Michael Buesche4d6b792007-09-18 15:39:42 -04003106
Michael Buesche4d6b792007-09-18 15:39:42 -04003107 err = b43_upload_microcode(dev);
3108 if (err)
3109 goto out; /* firmware is released later */
3110
3111 err = b43_gpio_init(dev);
3112 if (err)
3113 goto out; /* firmware is released later */
Michael Buesch21954c32007-09-27 15:31:40 +02003114
Michael Buesche4d6b792007-09-18 15:39:42 -04003115 err = b43_upload_initvals(dev);
3116 if (err)
Larry Finger1a8d1222007-12-14 13:59:11 +01003117 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003118
Rafał Miłecki0f684232014-05-17 23:24:53 +02003119 err = b43_upload_initvals_band(dev);
3120 if (err)
3121 goto err_gpio_clean;
3122
Michael Buesch0b7dcd92008-09-03 12:31:54 +02003123 /* Turn the Analog on and initialize the PHY. */
3124 phy->ops->switch_analog(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04003125 err = b43_phy_init(dev);
3126 if (err)
Michael Bueschef1a6282008-08-27 18:53:02 +02003127 goto err_gpio_clean;
Michael Buesche4d6b792007-09-18 15:39:42 -04003128
Michael Bueschef1a6282008-08-27 18:53:02 +02003129 /* Disable Interference Mitigation. */
3130 if (phy->ops->interf_mitigation)
3131 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
Michael Buesche4d6b792007-09-18 15:39:42 -04003132
Michael Bueschef1a6282008-08-27 18:53:02 +02003133 /* Select the antennae */
3134 if (phy->ops->set_rx_antenna)
3135 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
Michael Buesche4d6b792007-09-18 15:39:42 -04003136 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
3137
3138 if (phy->type == B43_PHYTYPE_B) {
3139 value16 = b43_read16(dev, 0x005E);
3140 value16 |= 0x0004;
3141 b43_write16(dev, 0x005E, value16);
3142 }
3143 b43_write32(dev, 0x0100, 0x01000000);
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003144 if (dev->dev->core_rev < 5)
Michael Buesche4d6b792007-09-18 15:39:42 -04003145 b43_write32(dev, 0x010C, 0x01000000);
3146
Rafał Miłecki50566352012-01-02 19:31:21 +01003147 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_INFRA, 0);
3148 b43_maskset32(dev, B43_MMIO_MACCTL, ~0, B43_MACCTL_INFRA);
Michael Buesche4d6b792007-09-18 15:39:42 -04003149
Michael Buesche4d6b792007-09-18 15:39:42 -04003150 /* Probe Response Timeout value */
3151 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
Hauke Mehrtens5c1da232013-03-23 18:07:02 +01003152 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003153
3154 /* Initially set the wireless operation mode. */
3155 b43_adjust_opmode(dev);
3156
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003157 if (dev->dev->core_rev < 3) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003158 b43_write16(dev, 0x060E, 0x0000);
3159 b43_write16(dev, 0x0610, 0x8000);
3160 b43_write16(dev, 0x0604, 0x0000);
3161 b43_write16(dev, 0x0606, 0x0200);
3162 } else {
3163 b43_write32(dev, 0x0188, 0x80000000);
3164 b43_write32(dev, 0x018C, 0x02000000);
3165 }
3166 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02003167 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001FC00);
Michael Buesche4d6b792007-09-18 15:39:42 -04003168 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
3169 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
3170 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
3171 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
3172 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
3173
Rafał Miłecki858a1652011-05-10 16:05:33 +02003174 b43_mac_phy_clock_set(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003175
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003176 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02003177#ifdef CONFIG_B43_BCMA
3178 case B43_BUS_BCMA:
3179 /* FIXME: 0xE74 is quite common, but should be read from CC */
3180 b43_write16(dev, B43_MMIO_POWERUP_DELAY, 0xE74);
3181 break;
3182#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003183#ifdef CONFIG_B43_SSB
3184 case B43_BUS_SSB:
3185 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
3186 dev->dev->sdev->bus->chipco.fast_pwrup_delay);
3187 break;
3188#endif
3189 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003190
3191 err = 0;
3192 b43dbg(dev->wl, "Chip initialized\n");
Michael Buesch21954c32007-09-27 15:31:40 +02003193out:
Michael Buesche4d6b792007-09-18 15:39:42 -04003194 return err;
3195
Larry Finger1a8d1222007-12-14 13:59:11 +01003196err_gpio_clean:
Michael Buesche4d6b792007-09-18 15:39:42 -04003197 b43_gpio_cleanup(dev);
Michael Buesch21954c32007-09-27 15:31:40 +02003198 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003199}
3200
Michael Buesche4d6b792007-09-18 15:39:42 -04003201static void b43_periodic_every60sec(struct b43_wldev *dev)
3202{
Michael Bueschef1a6282008-08-27 18:53:02 +02003203 const struct b43_phy_operations *ops = dev->phy.ops;
Michael Buesche4d6b792007-09-18 15:39:42 -04003204
Michael Bueschef1a6282008-08-27 18:53:02 +02003205 if (ops->pwork_60sec)
3206 ops->pwork_60sec(dev);
Michael Buesch18c8ade2008-08-28 19:33:40 +02003207
3208 /* Force check the TX power emission now. */
3209 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
Michael Buesche4d6b792007-09-18 15:39:42 -04003210}
3211
3212static void b43_periodic_every30sec(struct b43_wldev *dev)
3213{
3214 /* Update device statistics. */
3215 b43_calculate_link_quality(dev);
3216}
3217
3218static void b43_periodic_every15sec(struct b43_wldev *dev)
3219{
3220 struct b43_phy *phy = &dev->phy;
Michael Buesch9b839a72008-06-20 17:44:02 +02003221 u16 wdr;
3222
3223 if (dev->fw.opensource) {
3224 /* Check if the firmware is still alive.
3225 * It will reset the watchdog counter to 0 in its idle loop. */
3226 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
3227 if (unlikely(wdr)) {
3228 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
3229 b43_controller_restart(dev, "Firmware watchdog");
3230 return;
3231 } else {
3232 b43_shm_write16(dev, B43_SHM_SCRATCH,
3233 B43_WATCHDOG_REG, 1);
3234 }
3235 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003236
Michael Bueschef1a6282008-08-27 18:53:02 +02003237 if (phy->ops->pwork_15sec)
3238 phy->ops->pwork_15sec(dev);
3239
Stefano Brivio00e0b8c2007-11-25 11:10:33 +01003240 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3241 wmb();
Michael Buesch990b86f2009-09-12 00:48:03 +02003242
3243#if B43_DEBUG
3244 if (b43_debug(dev, B43_DBG_VERBOSESTATS)) {
3245 unsigned int i;
3246
3247 b43dbg(dev->wl, "Stats: %7u IRQs/sec, %7u TX/sec, %7u RX/sec\n",
3248 dev->irq_count / 15,
3249 dev->tx_count / 15,
3250 dev->rx_count / 15);
3251 dev->irq_count = 0;
3252 dev->tx_count = 0;
3253 dev->rx_count = 0;
3254 for (i = 0; i < ARRAY_SIZE(dev->irq_bit_count); i++) {
3255 if (dev->irq_bit_count[i]) {
3256 b43dbg(dev->wl, "Stats: %7u IRQ-%02u/sec (0x%08X)\n",
3257 dev->irq_bit_count[i] / 15, i, (1 << i));
3258 dev->irq_bit_count[i] = 0;
3259 }
3260 }
3261 }
3262#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003263}
3264
Michael Buesche4d6b792007-09-18 15:39:42 -04003265static void do_periodic_work(struct b43_wldev *dev)
3266{
3267 unsigned int state;
3268
3269 state = dev->periodic_state;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003270 if (state % 4 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003271 b43_periodic_every60sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003272 if (state % 2 == 0)
Michael Buesche4d6b792007-09-18 15:39:42 -04003273 b43_periodic_every30sec(dev);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003274 b43_periodic_every15sec(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003275}
3276
Michael Buesch05b64b32007-09-28 16:19:03 +02003277/* Periodic work locking policy:
3278 * The whole periodic work handler is protected by
3279 * wl->mutex. If another lock is needed somewhere in the
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003280 * pwork callchain, it's acquired in-place, where it's needed.
Michael Buesche4d6b792007-09-18 15:39:42 -04003281 */
Michael Buesche4d6b792007-09-18 15:39:42 -04003282static void b43_periodic_work_handler(struct work_struct *work)
3283{
Michael Buesch05b64b32007-09-28 16:19:03 +02003284 struct b43_wldev *dev = container_of(work, struct b43_wldev,
3285 periodic_work.work);
3286 struct b43_wl *wl = dev->wl;
3287 unsigned long delay;
Michael Buesche4d6b792007-09-18 15:39:42 -04003288
Michael Buesch05b64b32007-09-28 16:19:03 +02003289 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003290
3291 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
3292 goto out;
3293 if (b43_debug(dev, B43_DBG_PWORK_STOP))
3294 goto out_requeue;
3295
Michael Buesch05b64b32007-09-28 16:19:03 +02003296 do_periodic_work(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003297
Michael Buesche4d6b792007-09-18 15:39:42 -04003298 dev->periodic_state++;
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003299out_requeue:
Michael Buesche4d6b792007-09-18 15:39:42 -04003300 if (b43_debug(dev, B43_DBG_PWORK_FAST))
3301 delay = msecs_to_jiffies(50);
3302 else
Anton Blanchard82cd6822007-10-15 00:42:23 -05003303 delay = round_jiffies_relative(HZ * 15);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003304 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
Michael Buesch42bb4cd2007-09-28 14:22:33 +02003305out:
Michael Buesch05b64b32007-09-28 16:19:03 +02003306 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003307}
3308
3309static void b43_periodic_tasks_setup(struct b43_wldev *dev)
3310{
3311 struct delayed_work *work = &dev->periodic_work;
3312
3313 dev->periodic_state = 0;
3314 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003315 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04003316}
3317
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003318/* Check if communication with the device works correctly. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003319static int b43_validate_chipaccess(struct b43_wldev *dev)
3320{
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003321 u32 v, backup0, backup4;
Michael Buesche4d6b792007-09-18 15:39:42 -04003322
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003323 backup0 = b43_shm_read32(dev, B43_SHM_SHARED, 0);
3324 backup4 = b43_shm_read32(dev, B43_SHM_SHARED, 4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003325
3326 /* Check for read/write and endianness problems. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003327 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
3328 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
3329 goto error;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003330 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
3331 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
Michael Buesche4d6b792007-09-18 15:39:42 -04003332 goto error;
3333
Michael Bueschf62ae6c2009-07-31 20:51:41 +02003334 /* Check if unaligned 32bit SHM_SHARED access works properly.
3335 * However, don't bail out on failure, because it's noncritical. */
3336 b43_shm_write16(dev, B43_SHM_SHARED, 0, 0x1122);
3337 b43_shm_write16(dev, B43_SHM_SHARED, 2, 0x3344);
3338 b43_shm_write16(dev, B43_SHM_SHARED, 4, 0x5566);
3339 b43_shm_write16(dev, B43_SHM_SHARED, 6, 0x7788);
3340 if (b43_shm_read32(dev, B43_SHM_SHARED, 2) != 0x55663344)
3341 b43warn(dev->wl, "Unaligned 32bit SHM read access is broken\n");
3342 b43_shm_write32(dev, B43_SHM_SHARED, 2, 0xAABBCCDD);
3343 if (b43_shm_read16(dev, B43_SHM_SHARED, 0) != 0x1122 ||
3344 b43_shm_read16(dev, B43_SHM_SHARED, 2) != 0xCCDD ||
3345 b43_shm_read16(dev, B43_SHM_SHARED, 4) != 0xAABB ||
3346 b43_shm_read16(dev, B43_SHM_SHARED, 6) != 0x7788)
3347 b43warn(dev->wl, "Unaligned 32bit SHM write access is broken\n");
3348
3349 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup0);
3350 b43_shm_write32(dev, B43_SHM_SHARED, 4, backup4);
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003351
Rafał Miłecki21d889d2011-05-18 02:06:38 +02003352 if ((dev->dev->core_rev >= 3) && (dev->dev->core_rev <= 10)) {
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003353 /* The 32bit register shadows the two 16bit registers
3354 * with update sideeffects. Validate this. */
3355 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
3356 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
3357 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
3358 goto error;
3359 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
3360 goto error;
3361 }
3362 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
3363
3364 v = b43_read32(dev, B43_MMIO_MACCTL);
3365 v |= B43_MACCTL_GMODE;
3366 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
Michael Buesche4d6b792007-09-18 15:39:42 -04003367 goto error;
3368
3369 return 0;
Michael Bueschf3dd3fc2007-12-22 21:56:30 +01003370error:
Michael Buesche4d6b792007-09-18 15:39:42 -04003371 b43err(dev->wl, "Failed to validate the chipaccess\n");
3372 return -ENODEV;
3373}
3374
3375static void b43_security_init(struct b43_wldev *dev)
3376{
Michael Buesche4d6b792007-09-18 15:39:42 -04003377 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
3378 /* KTP is a word address, but we address SHM bytewise.
3379 * So multiply by two.
3380 */
3381 dev->ktp *= 2;
Michael Buesch66d2d082009-08-06 10:36:50 +02003382 /* Number of RCMTA address slots */
3383 b43_write16(dev, B43_MMIO_RCMTA_COUNT, B43_NR_PAIRWISE_KEYS);
3384 /* Clear the key memory. */
Michael Buesche4d6b792007-09-18 15:39:42 -04003385 b43_clear_keys(dev);
3386}
3387
Michael Buesch616de352009-03-29 13:19:31 +02003388#ifdef CONFIG_B43_HWRNG
John Daiker99da1852009-02-24 02:16:42 -08003389static int b43_rng_read(struct hwrng *rng, u32 *data)
Michael Buesche4d6b792007-09-18 15:39:42 -04003390{
3391 struct b43_wl *wl = (struct b43_wl *)rng->priv;
Michael Buescha78b3bb2009-09-11 21:44:05 +02003392 struct b43_wldev *dev;
3393 int count = -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003394
Michael Buescha78b3bb2009-09-11 21:44:05 +02003395 mutex_lock(&wl->mutex);
3396 dev = wl->current_dev;
3397 if (likely(dev && b43_status(dev) >= B43_STAT_INITIALIZED)) {
3398 *data = b43_read16(dev, B43_MMIO_RNG);
3399 count = sizeof(u16);
3400 }
3401 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003402
Michael Buescha78b3bb2009-09-11 21:44:05 +02003403 return count;
Michael Buesche4d6b792007-09-18 15:39:42 -04003404}
Michael Buesch616de352009-03-29 13:19:31 +02003405#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003406
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003407static void b43_rng_exit(struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04003408{
Michael Buesch616de352009-03-29 13:19:31 +02003409#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003410 if (wl->rng_initialized)
Rafael J. Wysockib844eba2008-03-23 20:28:24 +01003411 hwrng_unregister(&wl->rng);
Michael Buesch616de352009-03-29 13:19:31 +02003412#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003413}
3414
3415static int b43_rng_init(struct b43_wl *wl)
3416{
Michael Buesch616de352009-03-29 13:19:31 +02003417 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003418
Michael Buesch616de352009-03-29 13:19:31 +02003419#ifdef CONFIG_B43_HWRNG
Michael Buesche4d6b792007-09-18 15:39:42 -04003420 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
3421 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
3422 wl->rng.name = wl->rng_name;
3423 wl->rng.data_read = b43_rng_read;
3424 wl->rng.priv = (unsigned long)wl;
Rusty Russell3db1cd52011-12-19 13:56:45 +00003425 wl->rng_initialized = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04003426 err = hwrng_register(&wl->rng);
3427 if (err) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00003428 wl->rng_initialized = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04003429 b43err(wl, "Failed to register the random "
3430 "number generator (%d)\n", err);
3431 }
Michael Buesch616de352009-03-29 13:19:31 +02003432#endif /* CONFIG_B43_HWRNG */
Michael Buesche4d6b792007-09-18 15:39:42 -04003433
3434 return err;
3435}
3436
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003437static void b43_tx_work(struct work_struct *work)
Michael Buesche4d6b792007-09-18 15:39:42 -04003438{
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003439 struct b43_wl *wl = container_of(work, struct b43_wl, tx_work);
3440 struct b43_wldev *dev;
3441 struct sk_buff *skb;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003442 int queue_num;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003443 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003444
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003445 mutex_lock(&wl->mutex);
3446 dev = wl->current_dev;
3447 if (unlikely(!dev || b43_status(dev) < B43_STAT_STARTED)) {
3448 mutex_unlock(&wl->mutex);
3449 return;
Michael Buesch5100d5a2008-03-29 21:01:16 +01003450 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003451
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003452 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
3453 while (skb_queue_len(&wl->tx_queue[queue_num])) {
3454 skb = skb_dequeue(&wl->tx_queue[queue_num]);
3455 if (b43_using_pio_transfers(dev))
3456 err = b43_pio_tx(dev, skb);
3457 else
3458 err = b43_dma_tx(dev, skb);
3459 if (err == -ENOSPC) {
3460 wl->tx_queue_stopped[queue_num] = 1;
3461 ieee80211_stop_queue(wl->hw, queue_num);
3462 skb_queue_head(&wl->tx_queue[queue_num], skb);
3463 break;
3464 }
3465 if (unlikely(err))
Felix Fietkau78f18df2012-12-10 17:40:21 +01003466 ieee80211_free_txskb(wl->hw, skb);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003467 err = 0;
3468 }
Michael Buesch21a75d72008-04-25 19:29:08 +02003469
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003470 if (!err)
3471 wl->tx_queue_stopped[queue_num] = 0;
Michael Buesch21a75d72008-04-25 19:29:08 +02003472 }
3473
Michael Buesch990b86f2009-09-12 00:48:03 +02003474#if B43_DEBUG
3475 dev->tx_count++;
3476#endif
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003477 mutex_unlock(&wl->mutex);
3478}
Michael Buesch21a75d72008-04-25 19:29:08 +02003479
Johannes Berg7bb45682011-02-24 14:42:06 +01003480static void b43_op_tx(struct ieee80211_hw *hw,
Thomas Huehn36323f82012-07-23 21:33:42 +02003481 struct ieee80211_tx_control *control,
3482 struct sk_buff *skb)
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003483{
3484 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc9e8eae2008-06-15 15:17:29 +02003485
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003486 if (unlikely(skb->len < 2 + 2 + 6)) {
3487 /* Too short, this can't be a valid frame. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01003488 ieee80211_free_txskb(hw, skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003489 return;
Michael Bueschf5d40ee2009-09-04 22:53:18 +02003490 }
3491 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3492
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01003493 skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
3494 if (!wl->tx_queue_stopped[skb->queue_mapping]) {
3495 ieee80211_queue_work(wl->hw, &wl->tx_work);
3496 } else {
3497 ieee80211_stop_queue(wl->hw, skb->queue_mapping);
3498 }
Michael Buesche4d6b792007-09-18 15:39:42 -04003499}
3500
Michael Buesche6f5b932008-03-05 21:18:49 +01003501static void b43_qos_params_upload(struct b43_wldev *dev,
3502 const struct ieee80211_tx_queue_params *p,
3503 u16 shm_offset)
3504{
3505 u16 params[B43_NR_QOSPARAMS];
Johannes Berg0b576642008-07-15 02:08:24 -07003506 int bslots, tmp;
Michael Buesche6f5b932008-03-05 21:18:49 +01003507 unsigned int i;
3508
Michael Bueschb0544eb2009-09-06 15:42:45 +02003509 if (!dev->qos_enabled)
3510 return;
3511
Johannes Berg0b576642008-07-15 02:08:24 -07003512 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
Michael Buesche6f5b932008-03-05 21:18:49 +01003513
3514 memset(&params, 0, sizeof(params));
3515
3516 params[B43_QOSPARAM_TXOP] = p->txop * 32;
Johannes Berg0b576642008-07-15 02:08:24 -07003517 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3518 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3519 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3520 params[B43_QOSPARAM_AIFS] = p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003521 params[B43_QOSPARAM_BSLOTS] = bslots;
Johannes Berg0b576642008-07-15 02:08:24 -07003522 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
Michael Buesche6f5b932008-03-05 21:18:49 +01003523
3524 for (i = 0; i < ARRAY_SIZE(params); i++) {
3525 if (i == B43_QOSPARAM_STATUS) {
3526 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3527 shm_offset + (i * 2));
3528 /* Mark the parameters as updated. */
3529 tmp |= 0x100;
3530 b43_shm_write16(dev, B43_SHM_SHARED,
3531 shm_offset + (i * 2),
3532 tmp);
3533 } else {
3534 b43_shm_write16(dev, B43_SHM_SHARED,
3535 shm_offset + (i * 2),
3536 params[i]);
3537 }
3538 }
3539}
3540
Michael Bueschc40c1122008-09-06 16:21:47 +02003541/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
3542static const u16 b43_qos_shm_offsets[] = {
3543 /* [mac80211-queue-nr] = SHM_OFFSET, */
3544 [0] = B43_QOS_VOICE,
3545 [1] = B43_QOS_VIDEO,
3546 [2] = B43_QOS_BESTEFFORT,
3547 [3] = B43_QOS_BACKGROUND,
3548};
3549
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003550/* Update all QOS parameters in hardware. */
3551static void b43_qos_upload_all(struct b43_wldev *dev)
Michael Buesche6f5b932008-03-05 21:18:49 +01003552{
3553 struct b43_wl *wl = dev->wl;
3554 struct b43_qos_params *params;
Michael Buesche6f5b932008-03-05 21:18:49 +01003555 unsigned int i;
3556
Michael Bueschb0544eb2009-09-06 15:42:45 +02003557 if (!dev->qos_enabled)
3558 return;
3559
Michael Bueschc40c1122008-09-06 16:21:47 +02003560 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3561 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003562
3563 b43_mac_suspend(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003564 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3565 params = &(wl->qos_params[i]);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003566 b43_qos_params_upload(dev, &(params->p),
3567 b43_qos_shm_offsets[i]);
Michael Buesche6f5b932008-03-05 21:18:49 +01003568 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003569 b43_mac_enable(dev);
3570}
3571
3572static void b43_qos_clear(struct b43_wl *wl)
3573{
3574 struct b43_qos_params *params;
3575 unsigned int i;
3576
Michael Bueschc40c1122008-09-06 16:21:47 +02003577 /* Initialize QoS parameters to sane defaults. */
3578
3579 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3580 ARRAY_SIZE(wl->qos_params));
3581
Michael Buesche6f5b932008-03-05 21:18:49 +01003582 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3583 params = &(wl->qos_params[i]);
3584
Michael Bueschc40c1122008-09-06 16:21:47 +02003585 switch (b43_qos_shm_offsets[i]) {
3586 case B43_QOS_VOICE:
3587 params->p.txop = 0;
3588 params->p.aifs = 2;
3589 params->p.cw_min = 0x0001;
3590 params->p.cw_max = 0x0001;
3591 break;
3592 case B43_QOS_VIDEO:
3593 params->p.txop = 0;
3594 params->p.aifs = 2;
3595 params->p.cw_min = 0x0001;
3596 params->p.cw_max = 0x0001;
3597 break;
3598 case B43_QOS_BESTEFFORT:
3599 params->p.txop = 0;
3600 params->p.aifs = 3;
3601 params->p.cw_min = 0x0001;
3602 params->p.cw_max = 0x03FF;
3603 break;
3604 case B43_QOS_BACKGROUND:
3605 params->p.txop = 0;
3606 params->p.aifs = 7;
3607 params->p.cw_min = 0x0001;
3608 params->p.cw_max = 0x03FF;
3609 break;
3610 default:
3611 B43_WARN_ON(1);
3612 }
Michael Buesche6f5b932008-03-05 21:18:49 +01003613 }
3614}
3615
3616/* Initialize the core's QOS capabilities */
3617static void b43_qos_init(struct b43_wldev *dev)
3618{
Michael Bueschb0544eb2009-09-06 15:42:45 +02003619 if (!dev->qos_enabled) {
3620 /* Disable QOS support. */
3621 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_EDCF);
3622 b43_write16(dev, B43_MMIO_IFSCTL,
3623 b43_read16(dev, B43_MMIO_IFSCTL)
3624 & ~B43_MMIO_IFSCTL_USE_EDCF);
3625 b43dbg(dev->wl, "QoS disabled\n");
3626 return;
3627 }
3628
Michael Buesche6f5b932008-03-05 21:18:49 +01003629 /* Upload the current QOS parameters. */
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003630 b43_qos_upload_all(dev);
Michael Buesche6f5b932008-03-05 21:18:49 +01003631
3632 /* Enable QOS support. */
3633 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3634 b43_write16(dev, B43_MMIO_IFSCTL,
3635 b43_read16(dev, B43_MMIO_IFSCTL)
3636 | B43_MMIO_IFSCTL_USE_EDCF);
Michael Bueschb0544eb2009-09-06 15:42:45 +02003637 b43dbg(dev->wl, "QoS enabled\n");
Michael Buesche6f5b932008-03-05 21:18:49 +01003638}
3639
Eliad Peller8a3a3c82011-10-02 10:15:52 +02003640static int b43_op_conf_tx(struct ieee80211_hw *hw,
3641 struct ieee80211_vif *vif, u16 _queue,
Michael Buesch40faacc2007-10-28 16:29:32 +01003642 const struct ieee80211_tx_queue_params *params)
Michael Buesche4d6b792007-09-18 15:39:42 -04003643{
Michael Buesche6f5b932008-03-05 21:18:49 +01003644 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003645 struct b43_wldev *dev;
Michael Buesche6f5b932008-03-05 21:18:49 +01003646 unsigned int queue = (unsigned int)_queue;
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003647 int err = -ENODEV;
Michael Buesche6f5b932008-03-05 21:18:49 +01003648
3649 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3650 /* Queue not available or don't support setting
3651 * params on this queue. Return success to not
3652 * confuse mac80211. */
3653 return 0;
3654 }
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003655 BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
3656 ARRAY_SIZE(wl->qos_params));
Michael Buesche6f5b932008-03-05 21:18:49 +01003657
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003658 mutex_lock(&wl->mutex);
3659 dev = wl->current_dev;
3660 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
3661 goto out_unlock;
Michael Buesche6f5b932008-03-05 21:18:49 +01003662
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003663 memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
3664 b43_mac_suspend(dev);
3665 b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
3666 b43_qos_shm_offsets[queue]);
3667 b43_mac_enable(dev);
3668 err = 0;
Michael Buesche6f5b932008-03-05 21:18:49 +01003669
Michael Buesch5a5f3b42008-09-06 20:07:31 +02003670out_unlock:
3671 mutex_unlock(&wl->mutex);
3672
3673 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04003674}
3675
Michael Buesch40faacc2007-10-28 16:29:32 +01003676static int b43_op_get_stats(struct ieee80211_hw *hw,
3677 struct ieee80211_low_level_stats *stats)
Michael Buesche4d6b792007-09-18 15:39:42 -04003678{
3679 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04003680
Michael Buesch36dbd952009-09-04 22:51:29 +02003681 mutex_lock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003682 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
Michael Buesch36dbd952009-09-04 22:51:29 +02003683 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04003684
3685 return 0;
3686}
3687
Eliad Peller37a41b42011-09-21 14:06:11 +03003688static u64 b43_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003689{
3690 struct b43_wl *wl = hw_to_b43_wl(hw);
3691 struct b43_wldev *dev;
3692 u64 tsf;
3693
3694 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003695 dev = wl->current_dev;
3696
3697 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3698 b43_tsf_read(dev, &tsf);
3699 else
3700 tsf = 0;
3701
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003702 mutex_unlock(&wl->mutex);
3703
3704 return tsf;
3705}
3706
Eliad Peller37a41b42011-09-21 14:06:11 +03003707static void b43_op_set_tsf(struct ieee80211_hw *hw,
3708 struct ieee80211_vif *vif, u64 tsf)
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003709{
3710 struct b43_wl *wl = hw_to_b43_wl(hw);
3711 struct b43_wldev *dev;
3712
3713 mutex_lock(&wl->mutex);
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003714 dev = wl->current_dev;
3715
3716 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
3717 b43_tsf_write(dev, tsf);
3718
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01003719 mutex_unlock(&wl->mutex);
3720}
3721
John Daiker99da1852009-02-24 02:16:42 -08003722static const char *band_to_string(enum ieee80211_band band)
Michael Buesche4d6b792007-09-18 15:39:42 -04003723{
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003724 switch (band) {
3725 case IEEE80211_BAND_5GHZ:
3726 return "5";
3727 case IEEE80211_BAND_2GHZ:
3728 return "2.4";
3729 default:
3730 break;
3731 }
3732 B43_WARN_ON(1);
3733 return "";
3734}
3735
3736/* Expects wl->mutex locked */
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003737static int b43_switch_band(struct b43_wldev *dev,
3738 struct ieee80211_channel *chan)
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003739{
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003740 struct b43_phy *phy = &dev->phy;
3741 bool gmode;
3742 u32 tmp;
Michael Buesche4d6b792007-09-18 15:39:42 -04003743
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003744 switch (chan->band) {
3745 case IEEE80211_BAND_5GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003746 gmode = false;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003747 break;
3748 case IEEE80211_BAND_2GHZ:
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003749 gmode = true;
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003750 break;
3751 default:
3752 B43_WARN_ON(1);
3753 return -EINVAL;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003754 }
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02003755
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003756 if (!((gmode && phy->supports_2ghz) ||
3757 (!gmode && phy->supports_5ghz))) {
3758 b43err(dev->wl, "This device doesn't support %s-GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003759 band_to_string(chan->band));
3760 return -ENODEV;
Michael Buesche4d6b792007-09-18 15:39:42 -04003761 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003762
3763 if (!!phy->gmode == !!gmode) {
Michael Buesche4d6b792007-09-18 15:39:42 -04003764 /* This device is already running. */
3765 return 0;
3766 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003767
3768 b43dbg(dev->wl, "Switching to %s GHz band\n",
Michael Bueschbb1eeff2008-02-09 12:08:58 +01003769 band_to_string(chan->band));
Michael Buesche4d6b792007-09-18 15:39:42 -04003770
Rafał Miłecki6fe55142014-05-27 22:07:33 +02003771 /* Some new devices don't need disabling radio for band switching */
3772 if (!(phy->type == B43_PHYTYPE_N && phy->rev >= 3))
3773 b43_software_rfkill(dev, true);
Michael Buesche4d6b792007-09-18 15:39:42 -04003774
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003775 phy->gmode = gmode;
3776 b43_phy_put_into_reset(dev);
3777 switch (dev->dev->bus_type) {
3778#ifdef CONFIG_B43_BCMA
3779 case B43_BUS_BCMA:
3780 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
3781 if (gmode)
3782 tmp |= B43_BCMA_IOCTL_GMODE;
3783 else
3784 tmp &= ~B43_BCMA_IOCTL_GMODE;
3785 bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
3786 break;
3787#endif
3788#ifdef CONFIG_B43_SSB
3789 case B43_BUS_SSB:
3790 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
3791 if (gmode)
3792 tmp |= B43_TMSLOW_GMODE;
3793 else
3794 tmp &= ~B43_TMSLOW_GMODE;
3795 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
3796 break;
3797#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04003798 }
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003799 b43_phy_take_out_of_reset(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003800
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003801 b43_upload_initvals_band(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003802
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003803 b43_phy_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003804
3805 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003806}
3807
Johannes Berg9124b072008-10-14 19:17:54 +02003808/* Write the short and long frame retry limit values. */
3809static void b43_set_retry_limits(struct b43_wldev *dev,
3810 unsigned int short_retry,
3811 unsigned int long_retry)
3812{
3813 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3814 * the chip-internal counter. */
3815 short_retry = min(short_retry, (unsigned int)0xF);
3816 long_retry = min(long_retry, (unsigned int)0xF);
3817
3818 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3819 short_retry);
3820 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3821 long_retry);
3822}
3823
Johannes Berge8975582008-10-09 12:18:51 +02003824static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
Michael Buesche4d6b792007-09-18 15:39:42 -04003825{
3826 struct b43_wl *wl = hw_to_b43_wl(hw);
Rafał Miłecki53256512014-05-31 20:49:34 +02003827 struct b43_wldev *dev = wl->current_dev;
3828 struct b43_phy *phy = &dev->phy;
Johannes Berge8975582008-10-09 12:18:51 +02003829 struct ieee80211_conf *conf = &hw->conf;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003830 int antenna;
Michael Buesche4d6b792007-09-18 15:39:42 -04003831 int err = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -04003832
Michael Buesche4d6b792007-09-18 15:39:42 -04003833 mutex_lock(&wl->mutex);
Rafał Miłecki7a8af8c2014-05-17 23:24:56 +02003834 b43_mac_suspend(dev);
3835
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003836 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Rafał Miłeckiea42e712014-05-31 20:49:38 +02003837 phy->chandef = &conf->chandef;
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003838 phy->channel = conf->chandef.chan->hw_value;
Felix Fietkau2a190322011-08-10 13:50:30 -06003839
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003840 /* Switch the band (if necessary). */
3841 err = b43_switch_band(dev, conf->chandef.chan);
3842 if (err)
3843 goto out_mac_enable;
3844
3845 /* Switch to the requested channel.
3846 * The firmware takes care of races with the TX handler.
3847 */
Rafał Miłeckif9471e92014-05-31 20:49:37 +02003848 b43_switch_channel(dev, phy->channel);
Rafał Miłecki8c79e5e2014-05-31 20:49:35 +02003849 }
Rafał Miłeckiaa4c7b22010-01-22 01:53:12 +01003850
Johannes Berg9124b072008-10-14 19:17:54 +02003851 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
3852 b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
3853 conf->long_frame_max_tx_count);
3854 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
3855 if (!changed)
Michael Bueschd10d0e52008-12-18 22:13:39 +01003856 goto out_mac_enable;
Michael Buesche4d6b792007-09-18 15:39:42 -04003857
Johannes Berg0869aea2009-10-28 10:03:35 +01003858 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
Johannes Bergd42ce842007-11-23 14:50:51 +01003859
Michael Buesche4d6b792007-09-18 15:39:42 -04003860 /* Adjust the desired TX power level. */
3861 if (conf->power_level != 0) {
Michael Buesch18c8ade2008-08-28 19:33:40 +02003862 if (conf->power_level != phy->desired_txpower) {
3863 phy->desired_txpower = conf->power_level;
3864 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3865 B43_TXPWR_IGNORE_TSSI);
Michael Buesche4d6b792007-09-18 15:39:42 -04003866 }
3867 }
3868
3869 /* Antennas for RX and management frame TX. */
Johannes Berg0f4ac382008-10-09 12:18:04 +02003870 antenna = B43_ANTENNA_DEFAULT;
Michael Buesch9db1f6d2007-12-22 21:54:20 +01003871 b43_mgmtframe_txantenna(dev, antenna);
Johannes Berg0f4ac382008-10-09 12:18:04 +02003872 antenna = B43_ANTENNA_DEFAULT;
Michael Bueschef1a6282008-08-27 18:53:02 +02003873 if (phy->ops->set_rx_antenna)
3874 phy->ops->set_rx_antenna(dev, antenna);
Michael Buesche4d6b792007-09-18 15:39:42 -04003875
Larry Fingerfd4973c2009-06-20 12:58:11 -05003876 if (wl->radio_enabled != phy->radio_on) {
3877 if (wl->radio_enabled) {
Johannes Berg19d337d2009-06-02 13:01:37 +02003878 b43_software_rfkill(dev, false);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003879 b43info(dev->wl, "Radio turned on by software\n");
3880 if (!dev->radio_hw_enable) {
3881 b43info(dev->wl, "The hardware RF-kill button "
3882 "still turns the radio physically off. "
3883 "Press the button to turn it on.\n");
3884 }
3885 } else {
Johannes Berg19d337d2009-06-02 13:01:37 +02003886 b43_software_rfkill(dev, true);
Michael Bueschfda9abc2007-09-20 22:14:18 +02003887 b43info(dev->wl, "Radio turned off by software\n");
3888 }
3889 }
3890
Michael Bueschd10d0e52008-12-18 22:13:39 +01003891out_mac_enable:
3892 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04003893 mutex_unlock(&wl->mutex);
3894
3895 return err;
3896}
3897
Johannes Berg881d9482009-01-21 15:13:48 +01003898static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003899{
3900 struct ieee80211_supported_band *sband =
3901 dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
3902 struct ieee80211_rate *rate;
3903 int i;
3904 u16 basic, direct, offset, basic_offset, rateptr;
3905
3906 for (i = 0; i < sband->n_bitrates; i++) {
3907 rate = &sband->bitrates[i];
3908
3909 if (b43_is_cck_rate(rate->hw_value)) {
3910 direct = B43_SHM_SH_CCKDIRECT;
3911 basic = B43_SHM_SH_CCKBASIC;
3912 offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3913 offset &= 0xF;
3914 } else {
3915 direct = B43_SHM_SH_OFDMDIRECT;
3916 basic = B43_SHM_SH_OFDMBASIC;
3917 offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3918 offset &= 0xF;
3919 }
3920
3921 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
3922
3923 if (b43_is_cck_rate(rate->hw_value)) {
3924 basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
3925 basic_offset &= 0xF;
3926 } else {
3927 basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
3928 basic_offset &= 0xF;
3929 }
3930
3931 /*
3932 * Get the pointer that we need to point to
3933 * from the direct map
3934 */
3935 rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
3936 direct + 2 * basic_offset);
3937 /* and write it to the basic map */
3938 b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
3939 rateptr);
3940 }
3941}
3942
3943static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
3944 struct ieee80211_vif *vif,
3945 struct ieee80211_bss_conf *conf,
3946 u32 changed)
3947{
3948 struct b43_wl *wl = hw_to_b43_wl(hw);
3949 struct b43_wldev *dev;
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003950
3951 mutex_lock(&wl->mutex);
3952
3953 dev = wl->current_dev;
Michael Bueschd10d0e52008-12-18 22:13:39 +01003954 if (!dev || b43_status(dev) < B43_STAT_STARTED)
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003955 goto out_unlock_mutex;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003956
3957 B43_WARN_ON(wl->vif != vif);
3958
3959 if (changed & BSS_CHANGED_BSSID) {
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003960 if (conf->bssid)
3961 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
3962 else
3963 memset(wl->bssid, 0, ETH_ALEN);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003964 }
3965
Johannes Berg3f0d8432009-05-18 10:53:18 +02003966 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
3967 if (changed & BSS_CHANGED_BEACON &&
3968 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3969 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
3970 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
3971 b43_update_templates(wl);
3972
3973 if (changed & BSS_CHANGED_BSSID)
3974 b43_write_mac_bssid_templates(dev);
3975 }
Johannes Berg3f0d8432009-05-18 10:53:18 +02003976
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003977 b43_mac_suspend(dev);
3978
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003979 /* Update templates for AP/mesh mode. */
3980 if (changed & BSS_CHANGED_BEACON_INT &&
3981 (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
3982 b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
Felix Fietkau2a190322011-08-10 13:50:30 -06003983 b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) &&
3984 conf->beacon_int)
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003985 b43_set_beacon_int(dev, conf->beacon_int);
3986
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003987 if (changed & BSS_CHANGED_BASIC_RATES)
3988 b43_update_basic_rates(dev, conf->basic_rates);
3989
3990 if (changed & BSS_CHANGED_ERP_SLOT) {
3991 if (conf->use_short_slot)
3992 b43_short_slot_timing_enable(dev);
3993 else
3994 b43_short_slot_timing_disable(dev);
3995 }
3996
3997 b43_mac_enable(dev);
Michael Bueschd10d0e52008-12-18 22:13:39 +01003998out_unlock_mutex:
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01003999 mutex_unlock(&wl->mutex);
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01004000}
4001
Michael Buesch40faacc2007-10-28 16:29:32 +01004002static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01004003 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4004 struct ieee80211_key_conf *key)
Michael Buesche4d6b792007-09-18 15:39:42 -04004005{
4006 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004007 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004008 u8 algorithm;
4009 u8 index;
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004010 int err;
Michael Buesch060210f2009-01-25 15:49:59 +01004011 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Michael Buesche4d6b792007-09-18 15:39:42 -04004012
4013 if (modparam_nohwcrypt)
4014 return -ENOSPC; /* User disabled HW-crypto */
4015
Antonio Quartulli78f9c852012-04-01 00:35:40 +03004016 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4017 vif->type == NL80211_IFTYPE_MESH_POINT) &&
4018 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
4019 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
4020 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
4021 /*
4022 * For now, disable hw crypto for the RSN IBSS group keys. This
4023 * could be optimized in the future, but until that gets
4024 * implemented, use of software crypto for group addressed
4025 * frames is a acceptable to allow RSN IBSS to be used.
4026 */
4027 return -EOPNOTSUPP;
4028 }
4029
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004030 mutex_lock(&wl->mutex);
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004031
4032 dev = wl->current_dev;
4033 err = -ENODEV;
4034 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
4035 goto out_unlock;
4036
Michael Buesch403a3a12009-06-08 21:04:57 +02004037 if (dev->fw.pcm_request_failed || !dev->hwcrypto_enabled) {
Michael Buesch68217832008-05-17 23:43:57 +02004038 /* We don't have firmware for the crypto engine.
4039 * Must use software-crypto. */
4040 err = -EOPNOTSUPP;
4041 goto out_unlock;
4042 }
4043
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004044 err = -EINVAL;
Johannes Berg97359d12010-08-10 09:46:38 +02004045 switch (key->cipher) {
4046 case WLAN_CIPHER_SUITE_WEP40:
4047 algorithm = B43_SEC_ALGO_WEP40;
Michael Buesche4d6b792007-09-18 15:39:42 -04004048 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004049 case WLAN_CIPHER_SUITE_WEP104:
4050 algorithm = B43_SEC_ALGO_WEP104;
4051 break;
4052 case WLAN_CIPHER_SUITE_TKIP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004053 algorithm = B43_SEC_ALGO_TKIP;
4054 break;
Johannes Berg97359d12010-08-10 09:46:38 +02004055 case WLAN_CIPHER_SUITE_CCMP:
Michael Buesche4d6b792007-09-18 15:39:42 -04004056 algorithm = B43_SEC_ALGO_AES;
4057 break;
4058 default:
4059 B43_WARN_ON(1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004060 goto out_unlock;
4061 }
Michael Bueschc6dfc9a2007-10-28 15:59:58 +01004062 index = (u8) (key->keyidx);
4063 if (index > 3)
4064 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004065
4066 switch (cmd) {
4067 case SET_KEY:
gregor kowski035d0242009-08-19 22:35:45 +02004068 if (algorithm == B43_SEC_ALGO_TKIP &&
4069 (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
4070 !modparam_hwtkip)) {
4071 /* We support only pairwise key */
Michael Buesche4d6b792007-09-18 15:39:42 -04004072 err = -EOPNOTSUPP;
4073 goto out_unlock;
4074 }
4075
Michael Buesche808e582008-12-19 21:30:52 +01004076 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
Johannes Bergdc822b52008-12-29 12:55:09 +01004077 if (WARN_ON(!sta)) {
4078 err = -EOPNOTSUPP;
4079 goto out_unlock;
4080 }
Michael Buesche808e582008-12-19 21:30:52 +01004081 /* Pairwise key with an assigned MAC address. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004082 err = b43_key_write(dev, -1, algorithm,
Johannes Bergdc822b52008-12-29 12:55:09 +01004083 key->key, key->keylen,
4084 sta->addr, key);
Michael Buesche808e582008-12-19 21:30:52 +01004085 } else {
4086 /* Group key */
4087 err = b43_key_write(dev, index, algorithm,
4088 key->key, key->keylen, NULL, key);
Michael Buesche4d6b792007-09-18 15:39:42 -04004089 }
4090 if (err)
4091 goto out_unlock;
4092
4093 if (algorithm == B43_SEC_ALGO_WEP40 ||
4094 algorithm == B43_SEC_ALGO_WEP104) {
4095 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
4096 } else {
4097 b43_hf_write(dev,
4098 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
4099 }
4100 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
gregor kowski035d0242009-08-19 22:35:45 +02004101 if (algorithm == B43_SEC_ALGO_TKIP)
4102 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004103 break;
4104 case DISABLE_KEY: {
4105 err = b43_key_clear(dev, key->hw_key_idx);
4106 if (err)
4107 goto out_unlock;
4108 break;
4109 }
4110 default:
4111 B43_WARN_ON(1);
4112 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004113
Michael Buesche4d6b792007-09-18 15:39:42 -04004114out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004115 if (!err) {
4116 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
Johannes Berge1749612008-10-27 15:59:26 -07004117 "mac: %pM\n",
Michael Buesche4d6b792007-09-18 15:39:42 -04004118 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
Larry Fingera1d882102009-01-14 11:15:25 -06004119 sta ? sta->addr : bcast_addr);
Michael Buesch9cf7f242008-12-19 20:24:30 +01004120 b43_dump_keymemory(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004121 }
Michael Buesch9cf7f242008-12-19 20:24:30 +01004122 mutex_unlock(&wl->mutex);
4123
Michael Buesche4d6b792007-09-18 15:39:42 -04004124 return err;
4125}
4126
Michael Buesch40faacc2007-10-28 16:29:32 +01004127static void b43_op_configure_filter(struct ieee80211_hw *hw,
4128 unsigned int changed, unsigned int *fflags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004129 u64 multicast)
Michael Buesche4d6b792007-09-18 15:39:42 -04004130{
4131 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesch36dbd952009-09-04 22:51:29 +02004132 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004133
Michael Buesch36dbd952009-09-04 22:51:29 +02004134 mutex_lock(&wl->mutex);
4135 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04004136 if (!dev) {
4137 *fflags = 0;
Michael Buesch36dbd952009-09-04 22:51:29 +02004138 goto out_unlock;
Michael Buesche4d6b792007-09-18 15:39:42 -04004139 }
Johannes Berg4150c572007-09-17 01:29:23 -04004140
Johannes Berg4150c572007-09-17 01:29:23 -04004141 *fflags &= FIF_PROMISC_IN_BSS |
4142 FIF_ALLMULTI |
4143 FIF_FCSFAIL |
4144 FIF_PLCPFAIL |
4145 FIF_CONTROL |
4146 FIF_OTHER_BSS |
4147 FIF_BCN_PRBRESP_PROMISC;
4148
4149 changed &= FIF_PROMISC_IN_BSS |
4150 FIF_ALLMULTI |
4151 FIF_FCSFAIL |
4152 FIF_PLCPFAIL |
4153 FIF_CONTROL |
4154 FIF_OTHER_BSS |
4155 FIF_BCN_PRBRESP_PROMISC;
4156
4157 wl->filter_flags = *fflags;
4158
4159 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
4160 b43_adjust_opmode(dev);
Michael Buesch36dbd952009-09-04 22:51:29 +02004161
4162out_unlock:
4163 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004164}
4165
Michael Buesch36dbd952009-09-04 22:51:29 +02004166/* Locking: wl->mutex
4167 * Returns the current dev. This might be different from the passed in dev,
4168 * because the core might be gone away while we unlocked the mutex. */
4169static struct b43_wldev * b43_wireless_core_stop(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04004170{
Larry Finger9a53bf52011-08-27 15:53:42 -05004171 struct b43_wl *wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004172 struct b43_wldev *orig_dev;
Michael Buesch49d965c2009-10-03 00:57:58 +02004173 u32 mask;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004174 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04004175
Larry Finger9a53bf52011-08-27 15:53:42 -05004176 if (!dev)
4177 return NULL;
4178 wl = dev->wl;
Michael Buesch36dbd952009-09-04 22:51:29 +02004179redo:
4180 if (!dev || b43_status(dev) < B43_STAT_STARTED)
4181 return dev;
Stefano Brivioa19d12d2007-11-07 18:16:11 +01004182
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004183 /* Cancel work. Unlock to avoid deadlocks. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004184 mutex_unlock(&wl->mutex);
Michael Buesche4d6b792007-09-18 15:39:42 -04004185 cancel_delayed_work_sync(&dev->periodic_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004186 cancel_work_sync(&wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04004187 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004188 dev = wl->current_dev;
4189 if (!dev || b43_status(dev) < B43_STAT_STARTED) {
4190 /* Whoops, aliens ate up the device while we were unlocked. */
4191 return dev;
4192 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004193
Michael Buesch36dbd952009-09-04 22:51:29 +02004194 /* Disable interrupts on the device. */
4195 b43_set_status(dev, B43_STAT_INITIALIZED);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004196 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch36dbd952009-09-04 22:51:29 +02004197 /* wl->mutex is locked. That is enough. */
4198 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4199 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4200 } else {
4201 spin_lock_irq(&wl->hardirq_lock);
4202 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
4203 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* Flush */
4204 spin_unlock_irq(&wl->hardirq_lock);
4205 }
Michael Buesch176e9f62009-09-11 23:04:04 +02004206 /* Synchronize and free the interrupt handlers. Unlock to avoid deadlocks. */
Michael Buesch36dbd952009-09-04 22:51:29 +02004207 orig_dev = dev;
4208 mutex_unlock(&wl->mutex);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004209 if (b43_bus_host_is_sdio(dev->dev)) {
Michael Buesch176e9f62009-09-11 23:04:04 +02004210 b43_sdio_free_irq(dev);
4211 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004212 synchronize_irq(dev->dev->irq);
4213 free_irq(dev->dev->irq, dev);
Michael Buesch176e9f62009-09-11 23:04:04 +02004214 }
Michael Buesch36dbd952009-09-04 22:51:29 +02004215 mutex_lock(&wl->mutex);
4216 dev = wl->current_dev;
4217 if (!dev)
4218 return dev;
4219 if (dev != orig_dev) {
4220 if (b43_status(dev) >= B43_STAT_STARTED)
4221 goto redo;
4222 return dev;
4223 }
Michael Buesch49d965c2009-10-03 00:57:58 +02004224 mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
4225 B43_WARN_ON(mask != 0xFFFFFFFF && mask);
Michael Buesch36dbd952009-09-04 22:51:29 +02004226
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004227 /* Drain all TX queues. */
4228 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
Felix Fietkau78f18df2012-12-10 17:40:21 +01004229 while (skb_queue_len(&wl->tx_queue[queue_num])) {
4230 struct sk_buff *skb;
4231
4232 skb = skb_dequeue(&wl->tx_queue[queue_num]);
4233 ieee80211_free_txskb(wl->hw, skb);
4234 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01004235 }
Michael Bueschf5d40ee2009-09-04 22:53:18 +02004236
Michael Buesche4d6b792007-09-18 15:39:42 -04004237 b43_mac_suspend(dev);
Michael Buescha78b3bb2009-09-11 21:44:05 +02004238 b43_leds_exit(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004239 b43dbg(wl, "Wireless interface stopped\n");
Michael Buesch36dbd952009-09-04 22:51:29 +02004240
4241 return dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004242}
4243
4244/* Locking: wl->mutex */
4245static int b43_wireless_core_start(struct b43_wldev *dev)
4246{
4247 int err;
4248
4249 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
4250
4251 drain_txstatus_queue(dev);
Rafał Miłecki505fb012011-05-19 15:11:27 +02004252 if (b43_bus_host_is_sdio(dev->dev)) {
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004253 err = b43_sdio_request_irq(dev, b43_sdio_interrupt_handler);
4254 if (err) {
4255 b43err(dev->wl, "Cannot request SDIO IRQ\n");
4256 goto out;
4257 }
4258 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004259 err = request_threaded_irq(dev->dev->irq, b43_interrupt_handler,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004260 b43_interrupt_thread_handler,
4261 IRQF_SHARED, KBUILD_MODNAME, dev);
4262 if (err) {
Rafał Miłeckidedb1eb2011-05-14 00:04:38 +02004263 b43err(dev->wl, "Cannot request IRQ-%d\n",
Rafał Miłeckia18c7152011-05-18 02:06:40 +02004264 dev->dev->irq);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02004265 goto out;
4266 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004267 }
4268
4269 /* We are ready to run. */
Larry Finger0866b032010-02-03 13:33:44 -06004270 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004271 b43_set_status(dev, B43_STAT_STARTED);
4272
4273 /* Start data flow (TX/RX). */
4274 b43_mac_enable(dev);
Michael Buesch13790722009-04-08 21:26:27 +02004275 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
Michael Buesche4d6b792007-09-18 15:39:42 -04004276
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004277 /* Start maintenance work */
Michael Buesche4d6b792007-09-18 15:39:42 -04004278 b43_periodic_tasks_setup(dev);
4279
Michael Buescha78b3bb2009-09-11 21:44:05 +02004280 b43_leds_init(dev);
4281
Michael Buesche4d6b792007-09-18 15:39:42 -04004282 b43dbg(dev->wl, "Wireless interface started\n");
Michael Buescha78b3bb2009-09-11 21:44:05 +02004283out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004284 return err;
4285}
4286
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004287static char *b43_phy_name(struct b43_wldev *dev, u8 phy_type)
4288{
4289 switch (phy_type) {
4290 case B43_PHYTYPE_A:
4291 return "A";
4292 case B43_PHYTYPE_B:
4293 return "B";
4294 case B43_PHYTYPE_G:
4295 return "G";
4296 case B43_PHYTYPE_N:
4297 return "N";
4298 case B43_PHYTYPE_LP:
4299 return "LP";
4300 case B43_PHYTYPE_SSLPN:
4301 return "SSLPN";
4302 case B43_PHYTYPE_HT:
4303 return "HT";
4304 case B43_PHYTYPE_LCN:
4305 return "LCN";
4306 case B43_PHYTYPE_LCNXN:
4307 return "LCNXN";
4308 case B43_PHYTYPE_LCN40:
4309 return "LCN40";
4310 case B43_PHYTYPE_AC:
4311 return "AC";
4312 }
4313 return "UNKNOWN";
4314}
4315
Michael Buesche4d6b792007-09-18 15:39:42 -04004316/* Get PHY and RADIO versioning numbers */
4317static int b43_phy_versioning(struct b43_wldev *dev)
4318{
4319 struct b43_phy *phy = &dev->phy;
4320 u32 tmp;
4321 u8 analog_type;
4322 u8 phy_type;
4323 u8 phy_rev;
4324 u16 radio_manuf;
4325 u16 radio_ver;
4326 u16 radio_rev;
4327 int unsupported = 0;
4328
4329 /* Get PHY versioning */
4330 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
4331 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
4332 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
4333 phy_rev = (tmp & B43_PHYVER_VERSION);
Rafał Miłeckib49c3ca2014-06-29 21:46:45 +02004334
4335 /* LCNXN is continuation of N which run out of revisions */
4336 if (phy_type == B43_PHYTYPE_LCNXN) {
4337 phy_type = B43_PHYTYPE_N;
4338 phy_rev += 16;
4339 }
4340
Michael Buesche4d6b792007-09-18 15:39:42 -04004341 switch (phy_type) {
Rafał Miłecki418378f2014-06-20 17:22:01 +02004342#ifdef CONFIG_B43_PHY_G
Michael Buesche4d6b792007-09-18 15:39:42 -04004343 case B43_PHYTYPE_G:
Larry Finger013978b2007-11-26 10:29:47 -06004344 if (phy_rev > 9)
Michael Buesche4d6b792007-09-18 15:39:42 -04004345 unsupported = 1;
4346 break;
Rafał Miłecki418378f2014-06-20 17:22:01 +02004347#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01004348#ifdef CONFIG_B43_PHY_N
Michael Bueschd5c71e42008-01-04 17:06:29 +01004349 case B43_PHYTYPE_N:
Rafał Miłeckiab72efd2010-12-21 21:29:44 +01004350 if (phy_rev > 9)
Michael Bueschd5c71e42008-01-04 17:06:29 +01004351 unsupported = 1;
4352 break;
4353#endif
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004354#ifdef CONFIG_B43_PHY_LP
4355 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004356 if (phy_rev > 2)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004357 unsupported = 1;
4358 break;
4359#endif
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004360#ifdef CONFIG_B43_PHY_HT
4361 case B43_PHYTYPE_HT:
4362 if (phy_rev > 1)
4363 unsupported = 1;
4364 break;
4365#endif
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004366#ifdef CONFIG_B43_PHY_LCN
4367 case B43_PHYTYPE_LCN:
4368 if (phy_rev > 1)
4369 unsupported = 1;
4370 break;
4371#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004372 default:
4373 unsupported = 1;
Joe Perches6403eab2011-06-03 11:51:20 +00004374 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004375 if (unsupported) {
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004376 b43err(dev->wl, "FOUND UNSUPPORTED PHY (Analog %u, Type %d (%s), Revision %u)\n",
4377 analog_type, phy_type, b43_phy_name(dev, phy_type),
4378 phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004379 return -EOPNOTSUPP;
4380 }
Rafał Miłecki2fdf8c52012-07-26 08:16:01 +02004381 b43info(dev->wl, "Found PHY: Analog %u, Type %d (%s), Revision %u\n",
4382 analog_type, phy_type, b43_phy_name(dev, phy_type), phy_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004383
4384 /* Get RADIO versioning */
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004385 if (dev->dev->core_rev >= 24) {
Rafał Miłecki544e5d82011-07-06 20:27:25 +02004386 u16 radio24[3];
4387
4388 for (tmp = 0; tmp < 3; tmp++) {
4389 b43_write16(dev, B43_MMIO_RADIO24_CONTROL, tmp);
4390 radio24[tmp] = b43_read16(dev, B43_MMIO_RADIO24_DATA);
4391 }
4392
4393 /* Broadcom uses "id" for our "ver" and has separated "ver" */
4394 /* radio_ver = (radio24[0] & 0xF0) >> 4; */
4395
4396 radio_manuf = 0x17F;
4397 radio_ver = (radio24[2] << 8) | radio24[1];
4398 radio_rev = (radio24[0] & 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004399 } else {
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004400 if (dev->dev->chip_id == 0x4317) {
4401 if (dev->dev->chip_rev == 0)
4402 tmp = 0x3205017F;
4403 else if (dev->dev->chip_rev == 1)
4404 tmp = 0x4205017F;
4405 else
4406 tmp = 0x5205017F;
4407 } else {
4408 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4409 B43_RADIOCTL_ID);
4410 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
4411 b43_write16(dev, B43_MMIO_RADIO_CONTROL,
4412 B43_RADIOCTL_ID);
4413 tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH)
4414 << 16;
4415 }
4416 radio_manuf = (tmp & 0x00000FFF);
4417 radio_ver = (tmp & 0x0FFFF000) >> 12;
4418 radio_rev = (tmp & 0xF0000000) >> 28;
Michael Buesche4d6b792007-09-18 15:39:42 -04004419 }
Rafał Miłecki3fd48502011-07-06 20:27:24 +02004420
Michael Buesch96c755a2008-01-06 00:09:46 +01004421 if (radio_manuf != 0x17F /* Broadcom */)
4422 unsupported = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004423 switch (phy_type) {
4424 case B43_PHYTYPE_A:
4425 if (radio_ver != 0x2060)
4426 unsupported = 1;
4427 if (radio_rev != 1)
4428 unsupported = 1;
4429 if (radio_manuf != 0x17F)
4430 unsupported = 1;
4431 break;
4432 case B43_PHYTYPE_B:
4433 if ((radio_ver & 0xFFF0) != 0x2050)
4434 unsupported = 1;
4435 break;
4436 case B43_PHYTYPE_G:
4437 if (radio_ver != 0x2050)
4438 unsupported = 1;
4439 break;
Michael Buesch96c755a2008-01-06 00:09:46 +01004440 case B43_PHYTYPE_N:
Johannes Bergbb519be2008-12-24 15:26:40 +01004441 if (radio_ver != 0x2055 && radio_ver != 0x2056)
Michael Buesch96c755a2008-01-06 00:09:46 +01004442 unsupported = 1;
4443 break;
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004444 case B43_PHYTYPE_LP:
Gábor Stefanik9d86a2d2009-08-14 14:54:46 +02004445 if (radio_ver != 0x2062 && radio_ver != 0x2063)
Michael Buesch6b1c7c62008-12-25 00:39:28 +01004446 unsupported = 1;
4447 break;
Rafał Miłeckid7520b12011-06-13 16:20:06 +02004448 case B43_PHYTYPE_HT:
4449 if (radio_ver != 0x2059)
4450 unsupported = 1;
4451 break;
Rafał Miłecki1d738e62011-07-07 15:25:27 +02004452 case B43_PHYTYPE_LCN:
4453 if (radio_ver != 0x2064)
4454 unsupported = 1;
4455 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04004456 default:
4457 B43_WARN_ON(1);
4458 }
4459 if (unsupported) {
4460 b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
4461 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
4462 radio_manuf, radio_ver, radio_rev);
4463 return -EOPNOTSUPP;
4464 }
4465 b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
4466 radio_manuf, radio_ver, radio_rev);
4467
4468 phy->radio_manuf = radio_manuf;
4469 phy->radio_ver = radio_ver;
4470 phy->radio_rev = radio_rev;
4471
4472 phy->analog = analog_type;
4473 phy->type = phy_type;
4474 phy->rev = phy_rev;
4475
4476 return 0;
4477}
4478
4479static void setup_struct_phy_for_init(struct b43_wldev *dev,
4480 struct b43_phy *phy)
4481{
Michael Buesche4d6b792007-09-18 15:39:42 -04004482 phy->hardware_power_control = !!modparam_hwpctl;
Michael Buesch18c8ade2008-08-28 19:33:40 +02004483 phy->next_txpwr_check_time = jiffies;
Michael Buesch8ed7fc42007-12-09 22:34:59 +01004484 /* PHY TX errors counter. */
4485 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
Michael Buesch591f3dc2009-03-31 12:27:32 +02004486
4487#if B43_DEBUG
Rusty Russell3db1cd52011-12-19 13:56:45 +00004488 phy->phy_locked = false;
4489 phy->radio_locked = false;
Michael Buesch591f3dc2009-03-31 12:27:32 +02004490#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04004491}
4492
4493static void setup_struct_wldev_for_init(struct b43_wldev *dev)
4494{
Rusty Russell3db1cd52011-12-19 13:56:45 +00004495 dev->dfq_valid = false;
Michael Bueschaa6c7ae2007-12-26 16:26:36 +01004496
Michael Buesch6a724d62007-09-20 22:12:58 +02004497 /* Assume the radio is enabled. If it's not enabled, the state will
4498 * immediately get fixed on the first periodic work run. */
Rusty Russell3db1cd52011-12-19 13:56:45 +00004499 dev->radio_hw_enable = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04004500
4501 /* Stats */
4502 memset(&dev->stats, 0, sizeof(dev->stats));
4503
4504 setup_struct_phy_for_init(dev, &dev->phy);
4505
4506 /* IRQ related flags */
4507 dev->irq_reason = 0;
4508 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
Michael Buesch13790722009-04-08 21:26:27 +02004509 dev->irq_mask = B43_IRQ_MASKTEMPLATE;
Michael Buesch3e3ccb32009-03-19 19:27:21 +01004510 if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
Michael Buesch13790722009-04-08 21:26:27 +02004511 dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
Michael Buesche4d6b792007-09-18 15:39:42 -04004512
4513 dev->mac_suspended = 1;
4514
4515 /* Noise calculation context */
4516 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
4517}
4518
4519static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
4520{
Rafał Miłecki05814832011-05-18 02:06:39 +02004521 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buescha259d6a2008-04-18 21:06:37 +02004522 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004523
Michael Buesch1855ba72008-04-18 20:51:41 +02004524 if (!modparam_btcoex)
4525 return;
Larry Finger95de2842007-11-09 16:57:18 -06004526 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
Michael Buesche4d6b792007-09-18 15:39:42 -04004527 return;
4528 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
4529 return;
4530
4531 hf = b43_hf_read(dev);
Larry Finger95de2842007-11-09 16:57:18 -06004532 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
Michael Buesche4d6b792007-09-18 15:39:42 -04004533 hf |= B43_HF_BTCOEXALT;
4534 else
4535 hf |= B43_HF_BTCOEX;
4536 b43_hf_write(dev, hf);
Michael Buesche4d6b792007-09-18 15:39:42 -04004537}
4538
4539static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
Michael Buesch1855ba72008-04-18 20:51:41 +02004540{
4541 if (!modparam_btcoex)
4542 return;
4543 //TODO
Michael Buesche4d6b792007-09-18 15:39:42 -04004544}
4545
4546static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
4547{
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004548 struct ssb_bus *bus;
Michael Buesche4d6b792007-09-18 15:39:42 -04004549 u32 tmp;
4550
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004551#ifdef CONFIG_B43_SSB
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004552 if (dev->dev->bus_type != B43_BUS_SSB)
4553 return;
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004554#else
4555 return;
4556#endif
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004557
4558 bus = dev->dev->sdev->bus;
4559
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004560 if ((bus->chip_id == 0x4311 && bus->chip_rev == 2) ||
4561 (bus->chip_id == 0x4312)) {
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004562 tmp = ssb_read32(dev->dev->sdev, SSB_IMCFGLO);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004563 tmp &= ~SSB_IMCFGLO_REQTO;
4564 tmp &= ~SSB_IMCFGLO_SERTO;
4565 tmp |= 0x3;
Rafał Miłeckid48ae5c2011-05-19 15:11:26 +02004566 ssb_write32(dev->dev->sdev, SSB_IMCFGLO, tmp);
Rafał Miłecki0fd82ea2011-05-11 02:10:59 +02004567 ssb_commit_settings(bus);
Michael Buesche4d6b792007-09-18 15:39:42 -04004568 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004569}
4570
Michael Bueschd59f7202008-04-03 18:56:19 +02004571static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
4572{
4573 u16 pu_delay;
4574
4575 /* The time value is in microseconds. */
4576 if (dev->phy.type == B43_PHYTYPE_A)
4577 pu_delay = 3700;
4578 else
4579 pu_delay = 1050;
Johannes Berg05c914f2008-09-11 00:01:58 +02004580 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Michael Bueschd59f7202008-04-03 18:56:19 +02004581 pu_delay = 500;
4582 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
4583 pu_delay = max(pu_delay, (u16)2400);
4584
4585 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
4586}
4587
4588/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
4589static void b43_set_pretbtt(struct b43_wldev *dev)
4590{
4591 u16 pretbtt;
4592
4593 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02004594 if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
Michael Bueschd59f7202008-04-03 18:56:19 +02004595 pretbtt = 2;
4596 } else {
4597 if (dev->phy.type == B43_PHYTYPE_A)
4598 pretbtt = 120;
4599 else
4600 pretbtt = 250;
4601 }
4602 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
4603 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
4604}
4605
Michael Buesche4d6b792007-09-18 15:39:42 -04004606/* Shutdown a wireless core */
4607/* Locking: wl->mutex */
4608static void b43_wireless_core_exit(struct b43_wldev *dev)
4609{
Michael Buesch36dbd952009-09-04 22:51:29 +02004610 B43_WARN_ON(dev && b43_status(dev) > B43_STAT_INITIALIZED);
4611 if (!dev || b43_status(dev) != B43_STAT_INITIALIZED)
Michael Buesche4d6b792007-09-18 15:39:42 -04004612 return;
John W. Linville84c164a2010-08-06 15:31:45 -04004613
Michael Buesche4d6b792007-09-18 15:39:42 -04004614 b43_set_status(dev, B43_STAT_UNINIT);
4615
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004616 /* Stop the microcode PSM. */
Rafał Miłecki50566352012-01-02 19:31:21 +01004617 b43_maskset32(dev, B43_MMIO_MACCTL, ~B43_MACCTL_PSM_RUN,
4618 B43_MACCTL_PSM_JMP0);
Michael Buesch1f7d87b2008-01-22 20:23:34 +01004619
Hauke Mehrtens50023002013-08-24 00:32:34 +02004620 switch (dev->dev->bus_type) {
4621#ifdef CONFIG_B43_BCMA
4622 case B43_BUS_BCMA:
4623 bcma_core_pci_down(dev->dev->bdev->bus);
4624 break;
4625#endif
4626#ifdef CONFIG_B43_SSB
4627 case B43_BUS_SSB:
4628 /* TODO */
4629 break;
4630#endif
4631 }
4632
Michael Buesche4d6b792007-09-18 15:39:42 -04004633 b43_dma_free(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01004634 b43_pio_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004635 b43_chip_exit(dev);
Michael Bueschcb24f572008-09-03 12:12:20 +02004636 dev->phy.ops->switch_analog(dev, 0);
Michael Buesche66fee62007-12-26 17:47:10 +01004637 if (dev->wl->current_beacon) {
4638 dev_kfree_skb_any(dev->wl->current_beacon);
4639 dev->wl->current_beacon = NULL;
4640 }
4641
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004642 b43_device_disable(dev, 0);
4643 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004644}
4645
4646/* Initialize a wireless core */
4647static int b43_wireless_core_init(struct b43_wldev *dev)
4648{
Rafał Miłecki05814832011-05-18 02:06:39 +02004649 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04004650 struct b43_phy *phy = &dev->phy;
4651 int err;
Michael Buescha259d6a2008-04-18 21:06:37 +02004652 u64 hf;
Michael Buesche4d6b792007-09-18 15:39:42 -04004653
4654 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4655
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004656 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04004657 if (err)
4658 goto out;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02004659 if (!b43_device_is_enabled(dev))
4660 b43_wireless_core_reset(dev, phy->gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04004661
Michael Bueschfb111372008-09-02 13:00:34 +02004662 /* Reset all data structures. */
Michael Buesche4d6b792007-09-18 15:39:42 -04004663 setup_struct_wldev_for_init(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004664 phy->ops->prepare_structs(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004665
4666 /* Enable IRQ routing to this device. */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004667 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004668#ifdef CONFIG_B43_BCMA
4669 case B43_BUS_BCMA:
Hauke Mehrtensdfae7142012-09-29 20:40:18 +02004670 bcma_core_pci_irq_ctl(&dev->dev->bdev->bus->drv_pci[0],
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004671 dev->dev->bdev, true);
Hauke Mehrtens50023002013-08-24 00:32:34 +02004672 bcma_core_pci_up(dev->dev->bdev->bus);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02004673 break;
4674#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004675#ifdef CONFIG_B43_SSB
4676 case B43_BUS_SSB:
4677 ssb_pcicore_dev_irqvecs_enable(&dev->dev->sdev->bus->pcicore,
4678 dev->dev->sdev);
4679 break;
4680#endif
4681 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004682
4683 b43_imcfglo_timeouts_workaround(dev);
4684 b43_bluetooth_coext_disable(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02004685 if (phy->ops->prepare_hardware) {
4686 err = phy->ops->prepare_hardware(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004687 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004688 goto err_busdown;
Michael Bueschef1a6282008-08-27 18:53:02 +02004689 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004690 err = b43_chip_init(dev);
4691 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02004692 goto err_busdown;
Michael Buesche4d6b792007-09-18 15:39:42 -04004693 b43_shm_write16(dev, B43_SHM_SHARED,
Rafał Miłecki21d889d2011-05-18 02:06:38 +02004694 B43_SHM_SH_WLCOREREV, dev->dev->core_rev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004695 hf = b43_hf_read(dev);
4696 if (phy->type == B43_PHYTYPE_G) {
4697 hf |= B43_HF_SYMW;
4698 if (phy->rev == 1)
4699 hf |= B43_HF_GDCW;
Larry Finger95de2842007-11-09 16:57:18 -06004700 if (sprom->boardflags_lo & B43_BFL_PACTRL)
Michael Buesche4d6b792007-09-18 15:39:42 -04004701 hf |= B43_HF_OFDMPABOOST;
Michael Buesch969d15c2009-02-20 14:27:15 +01004702 }
4703 if (phy->radio_ver == 0x2050) {
4704 if (phy->radio_rev == 6)
4705 hf |= B43_HF_4318TSSI;
4706 if (phy->radio_rev < 6)
4707 hf |= B43_HF_VCORECALC;
Michael Buesche4d6b792007-09-18 15:39:42 -04004708 }
Michael Buesch1cc8f472009-02-20 14:47:56 +01004709 if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
4710 hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02004711#if defined(CONFIG_B43_SSB) && defined(CONFIG_SSB_DRIVER_PCICORE)
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02004712 if (dev->dev->bus_type == B43_BUS_SSB &&
4713 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
4714 dev->dev->sdev->bus->pcicore.dev->id.revision <= 10)
Michael Buesch88219052009-02-20 14:58:59 +01004715 hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
Michael Buesch1a777332009-03-04 16:41:10 +01004716#endif
Michael Buesch25d3ef52009-02-20 15:39:21 +01004717 hf &= ~B43_HF_SKCFPUP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004718 b43_hf_write(dev, hf);
4719
Michael Buesch74cfdba2007-10-28 16:19:44 +01004720 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4721 B43_DEFAULT_LONG_RETRY_LIMIT);
Michael Buesche4d6b792007-09-18 15:39:42 -04004722 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4723 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4724
4725 /* Disable sending probe responses from firmware.
4726 * Setting the MaxTime to one usec will always trigger
4727 * a timeout, so we never send any probe resp.
4728 * A timeout of zero is infinite. */
4729 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4730
4731 b43_rate_memory_init(dev);
Michael Buesch5042c502008-04-05 15:05:00 +02004732 b43_set_phytxctl_defaults(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004733
4734 /* Minimum Contention Window */
Daniel Nguc5a079f2010-03-23 00:52:44 +13004735 if (phy->type == B43_PHYTYPE_B)
Michael Buesche4d6b792007-09-18 15:39:42 -04004736 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
Daniel Nguc5a079f2010-03-23 00:52:44 +13004737 else
Michael Buesche4d6b792007-09-18 15:39:42 -04004738 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
Michael Buesche4d6b792007-09-18 15:39:42 -04004739 /* Maximum Contention Window */
4740 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4741
Rafał Miłecki505fb012011-05-19 15:11:27 +02004742 if (b43_bus_host_is_pcmcia(dev->dev) ||
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004743 b43_bus_host_is_sdio(dev->dev)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004744 dev->__using_pio_transfers = true;
Rafał Miłeckicbe1e822011-08-16 21:44:21 +02004745 err = b43_pio_init(dev);
4746 } else if (dev->use_pio) {
4747 b43warn(dev->wl, "Forced PIO by use_pio module parameter. "
4748 "This should not be needed and will result in lower "
4749 "performance.\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00004750 dev->__using_pio_transfers = true;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004751 err = b43_pio_init(dev);
4752 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00004753 dev->__using_pio_transfers = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +01004754 err = b43_dma_init(dev);
4755 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004756 if (err)
4757 goto err_chip_exit;
Michael Buesch03b29772007-12-26 14:41:30 +01004758 b43_qos_init(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004759 b43_set_synth_pu_delay(dev, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04004760 b43_bluetooth_coext_enable(dev);
4761
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004762 b43_bus_powerup(dev, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
Johannes Berg4150c572007-09-17 01:29:23 -04004763 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004764 b43_security_init(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004765
Michael Buesch5ab95492009-09-10 20:31:46 +02004766 ieee80211_wake_queues(dev->wl->hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04004767
4768 b43_set_status(dev, B43_STAT_INITIALIZED);
4769
Larry Finger1a8d1222007-12-14 13:59:11 +01004770out:
Michael Buesche4d6b792007-09-18 15:39:42 -04004771 return err;
4772
Michael Bueschef1a6282008-08-27 18:53:02 +02004773err_chip_exit:
Michael Buesche4d6b792007-09-18 15:39:42 -04004774 b43_chip_exit(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02004775err_busdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02004776 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004777 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4778 return err;
4779}
4780
Michael Buesch40faacc2007-10-28 16:29:32 +01004781static int b43_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004782 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004783{
4784 struct b43_wl *wl = hw_to_b43_wl(hw);
4785 struct b43_wldev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004786 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04004787
4788 /* TODO: allow WDS/AP devices to coexist */
4789
Johannes Berg1ed32e42009-12-23 13:15:45 +01004790 if (vif->type != NL80211_IFTYPE_AP &&
4791 vif->type != NL80211_IFTYPE_MESH_POINT &&
4792 vif->type != NL80211_IFTYPE_STATION &&
4793 vif->type != NL80211_IFTYPE_WDS &&
4794 vif->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04004795 return -EOPNOTSUPP;
Michael Buesche4d6b792007-09-18 15:39:42 -04004796
4797 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004798 if (wl->operating)
Michael Buesche4d6b792007-09-18 15:39:42 -04004799 goto out_mutex_unlock;
4800
Johannes Berg1ed32e42009-12-23 13:15:45 +01004801 b43dbg(wl, "Adding Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004802
4803 dev = wl->current_dev;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004804 wl->operating = true;
Johannes Berg1ed32e42009-12-23 13:15:45 +01004805 wl->vif = vif;
4806 wl->if_type = vif->type;
4807 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Michael Buesche4d6b792007-09-18 15:39:42 -04004808
Michael Buesche4d6b792007-09-18 15:39:42 -04004809 b43_adjust_opmode(dev);
Michael Bueschd59f7202008-04-03 18:56:19 +02004810 b43_set_pretbtt(dev);
4811 b43_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04004812 b43_upload_card_macaddress(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04004813
4814 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004815 out_mutex_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004816 mutex_unlock(&wl->mutex);
4817
Felix Fietkau2a190322011-08-10 13:50:30 -06004818 if (err == 0)
4819 b43_op_bss_info_changed(hw, vif, &vif->bss_conf, ~0);
4820
Michael Buesche4d6b792007-09-18 15:39:42 -04004821 return err;
4822}
4823
Michael Buesch40faacc2007-10-28 16:29:32 +01004824static void b43_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004825 struct ieee80211_vif *vif)
Michael Buesche4d6b792007-09-18 15:39:42 -04004826{
4827 struct b43_wl *wl = hw_to_b43_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04004828 struct b43_wldev *dev = wl->current_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04004829
Johannes Berg1ed32e42009-12-23 13:15:45 +01004830 b43dbg(wl, "Removing Interface type %d\n", vif->type);
Michael Buesche4d6b792007-09-18 15:39:42 -04004831
4832 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04004833
4834 B43_WARN_ON(!wl->operating);
Johannes Berg1ed32e42009-12-23 13:15:45 +01004835 B43_WARN_ON(wl->vif != vif);
Johannes Berg32bfd352007-12-19 01:31:26 +01004836 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04004837
Rusty Russell3db1cd52011-12-19 13:56:45 +00004838 wl->operating = false;
Johannes Berg4150c572007-09-17 01:29:23 -04004839
Johannes Berg4150c572007-09-17 01:29:23 -04004840 b43_adjust_opmode(dev);
4841 memset(wl->mac_addr, 0, ETH_ALEN);
4842 b43_upload_card_macaddress(dev);
Johannes Berg4150c572007-09-17 01:29:23 -04004843
4844 mutex_unlock(&wl->mutex);
4845}
4846
Michael Buesch40faacc2007-10-28 16:29:32 +01004847static int b43_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004848{
4849 struct b43_wl *wl = hw_to_b43_wl(hw);
4850 struct b43_wldev *dev = wl->current_dev;
4851 int did_init = 0;
WANG Cong923403b2007-10-16 14:29:38 -07004852 int err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04004853
Michael Buesch7be1bb62008-01-23 21:10:56 +01004854 /* Kill all old instance specific information to make sure
4855 * the card won't use it in the short timeframe between start
4856 * and mac80211 reconfiguring it. */
4857 memset(wl->bssid, 0, ETH_ALEN);
4858 memset(wl->mac_addr, 0, ETH_ALEN);
4859 wl->filter_flags = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00004860 wl->radiotap_enabled = false;
Michael Buesche6f5b932008-03-05 21:18:49 +01004861 b43_qos_clear(wl);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004862 wl->beacon0_uploaded = false;
4863 wl->beacon1_uploaded = false;
4864 wl->beacon_templates_virgin = true;
4865 wl->radio_enabled = true;
Michael Buesch7be1bb62008-01-23 21:10:56 +01004866
Johannes Berg4150c572007-09-17 01:29:23 -04004867 mutex_lock(&wl->mutex);
4868
4869 if (b43_status(dev) < B43_STAT_INITIALIZED) {
4870 err = b43_wireless_core_init(dev);
Johannes Bergf41f3f32009-06-07 12:30:34 -05004871 if (err)
Johannes Berg4150c572007-09-17 01:29:23 -04004872 goto out_mutex_unlock;
4873 did_init = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -04004874 }
4875
Johannes Berg4150c572007-09-17 01:29:23 -04004876 if (b43_status(dev) < B43_STAT_STARTED) {
4877 err = b43_wireless_core_start(dev);
4878 if (err) {
4879 if (did_init)
4880 b43_wireless_core_exit(dev);
4881 goto out_mutex_unlock;
4882 }
Michael Buesche4d6b792007-09-18 15:39:42 -04004883 }
Johannes Berg4150c572007-09-17 01:29:23 -04004884
Johannes Bergf41f3f32009-06-07 12:30:34 -05004885 /* XXX: only do if device doesn't support rfkill irq */
4886 wiphy_rfkill_start_polling(hw->wiphy);
4887
Johannes Berg4150c572007-09-17 01:29:23 -04004888 out_mutex_unlock:
4889 mutex_unlock(&wl->mutex);
4890
Seth Forsheedbdedbd2012-04-25 17:28:00 -05004891 /*
4892 * Configuration may have been overwritten during initialization.
4893 * Reload the configuration, but only if initialization was
4894 * successful. Reloading the configuration after a failed init
4895 * may hang the system.
4896 */
4897 if (!err)
4898 b43_op_config(hw, ~0);
Felix Fietkau2a190322011-08-10 13:50:30 -06004899
Johannes Berg4150c572007-09-17 01:29:23 -04004900 return err;
4901}
4902
Michael Buesch40faacc2007-10-28 16:29:32 +01004903static void b43_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04004904{
4905 struct b43_wl *wl = hw_to_b43_wl(hw);
4906 struct b43_wldev *dev = wl->current_dev;
4907
Michael Buescha82d9922008-04-04 21:40:06 +02004908 cancel_work_sync(&(wl->beacon_update_trigger));
Larry Finger1a8d1222007-12-14 13:59:11 +01004909
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004910 if (!dev)
4911 goto out;
4912
Johannes Berg4150c572007-09-17 01:29:23 -04004913 mutex_lock(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02004914 if (b43_status(dev) >= B43_STAT_STARTED) {
4915 dev = b43_wireless_core_stop(dev);
4916 if (!dev)
4917 goto out_unlock;
4918 }
Johannes Berg4150c572007-09-17 01:29:23 -04004919 b43_wireless_core_exit(dev);
Rusty Russell3db1cd52011-12-19 13:56:45 +00004920 wl->radio_enabled = false;
Michael Buesch36dbd952009-09-04 22:51:29 +02004921
4922out_unlock:
Michael Buesche4d6b792007-09-18 15:39:42 -04004923 mutex_unlock(&wl->mutex);
Guennadi Liakhovetskiccde8a42012-01-06 12:58:16 +01004924out:
Michael Buesch18c8ade2008-08-28 19:33:40 +02004925 cancel_work_sync(&(wl->txpower_adjust_work));
Michael Buesche4d6b792007-09-18 15:39:42 -04004926}
4927
Johannes Berg17741cd2008-09-11 00:02:02 +02004928static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
4929 struct ieee80211_sta *sta, bool set)
Michael Buesche66fee62007-12-26 17:47:10 +01004930{
4931 struct b43_wl *wl = hw_to_b43_wl(hw);
Michael Buesche66fee62007-12-26 17:47:10 +01004932
Felix Fietkau8f611282009-11-07 18:37:37 +01004933 /* FIXME: add locking */
Johannes Berg9d139c82008-07-09 14:40:37 +02004934 b43_update_templates(wl);
Michael Buesche66fee62007-12-26 17:47:10 +01004935
4936 return 0;
4937}
4938
Johannes Berg38968d02008-02-25 16:27:50 +01004939static void b43_op_sta_notify(struct ieee80211_hw *hw,
4940 struct ieee80211_vif *vif,
4941 enum sta_notify_cmd notify_cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02004942 struct ieee80211_sta *sta)
Johannes Berg38968d02008-02-25 16:27:50 +01004943{
4944 struct b43_wl *wl = hw_to_b43_wl(hw);
4945
4946 B43_WARN_ON(!vif || wl->vif != vif);
4947}
4948
Michael Buesch25d3ef52009-02-20 15:39:21 +01004949static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
4950{
4951 struct b43_wl *wl = hw_to_b43_wl(hw);
4952 struct b43_wldev *dev;
4953
4954 mutex_lock(&wl->mutex);
4955 dev = wl->current_dev;
4956 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
4957 /* Disable CFP update during scan on other channels. */
4958 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
4959 }
4960 mutex_unlock(&wl->mutex);
4961}
4962
4963static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
4964{
4965 struct b43_wl *wl = hw_to_b43_wl(hw);
4966 struct b43_wldev *dev;
4967
4968 mutex_lock(&wl->mutex);
4969 dev = wl->current_dev;
4970 if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
4971 /* Re-enable CFP update. */
4972 b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
4973 }
4974 mutex_unlock(&wl->mutex);
4975}
4976
John W. Linville354b4f02010-04-29 15:56:06 -04004977static int b43_op_get_survey(struct ieee80211_hw *hw, int idx,
4978 struct survey_info *survey)
4979{
4980 struct b43_wl *wl = hw_to_b43_wl(hw);
4981 struct b43_wldev *dev = wl->current_dev;
4982 struct ieee80211_conf *conf = &hw->conf;
4983
4984 if (idx != 0)
4985 return -ENOENT;
4986
Karl Beldan675a0b02013-03-25 16:26:57 +01004987 survey->channel = conf->chandef.chan;
John W. Linville354b4f02010-04-29 15:56:06 -04004988 survey->filled = SURVEY_INFO_NOISE_DBM;
4989 survey->noise = dev->stats.link_noise;
4990
4991 return 0;
4992}
4993
Michael Buesche4d6b792007-09-18 15:39:42 -04004994static const struct ieee80211_ops b43_hw_ops = {
Michael Buesch40faacc2007-10-28 16:29:32 +01004995 .tx = b43_op_tx,
4996 .conf_tx = b43_op_conf_tx,
4997 .add_interface = b43_op_add_interface,
4998 .remove_interface = b43_op_remove_interface,
4999 .config = b43_op_config,
Johannes Bergc7ab5ef2008-10-29 20:02:12 +01005000 .bss_info_changed = b43_op_bss_info_changed,
Michael Buesch40faacc2007-10-28 16:29:32 +01005001 .configure_filter = b43_op_configure_filter,
5002 .set_key = b43_op_set_key,
gregor kowski035d0242009-08-19 22:35:45 +02005003 .update_tkip_key = b43_op_update_tkip_key,
Michael Buesch40faacc2007-10-28 16:29:32 +01005004 .get_stats = b43_op_get_stats,
Alina Friedrichsen08e87a82009-01-25 15:28:28 +01005005 .get_tsf = b43_op_get_tsf,
5006 .set_tsf = b43_op_set_tsf,
Michael Buesch40faacc2007-10-28 16:29:32 +01005007 .start = b43_op_start,
5008 .stop = b43_op_stop,
Michael Buesche66fee62007-12-26 17:47:10 +01005009 .set_tim = b43_op_beacon_set_tim,
Johannes Berg38968d02008-02-25 16:27:50 +01005010 .sta_notify = b43_op_sta_notify,
Michael Buesch25d3ef52009-02-20 15:39:21 +01005011 .sw_scan_start = b43_op_sw_scan_start_notifier,
5012 .sw_scan_complete = b43_op_sw_scan_complete_notifier,
John W. Linville354b4f02010-04-29 15:56:06 -04005013 .get_survey = b43_op_get_survey,
Johannes Bergf41f3f32009-06-07 12:30:34 -05005014 .rfkill_poll = b43_rfkill_poll,
Michael Buesche4d6b792007-09-18 15:39:42 -04005015};
5016
5017/* Hard-reset the chip. Do not call this directly.
5018 * Use b43_controller_restart()
5019 */
5020static void b43_chip_reset(struct work_struct *work)
5021{
5022 struct b43_wldev *dev =
5023 container_of(work, struct b43_wldev, restart_work);
5024 struct b43_wl *wl = dev->wl;
5025 int err = 0;
5026 int prev_status;
5027
5028 mutex_lock(&wl->mutex);
5029
5030 prev_status = b43_status(dev);
5031 /* Bring the device down... */
Michael Buesch36dbd952009-09-04 22:51:29 +02005032 if (prev_status >= B43_STAT_STARTED) {
5033 dev = b43_wireless_core_stop(dev);
5034 if (!dev) {
5035 err = -ENODEV;
5036 goto out;
5037 }
5038 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005039 if (prev_status >= B43_STAT_INITIALIZED)
5040 b43_wireless_core_exit(dev);
5041
5042 /* ...and up again. */
5043 if (prev_status >= B43_STAT_INITIALIZED) {
5044 err = b43_wireless_core_init(dev);
5045 if (err)
5046 goto out;
5047 }
5048 if (prev_status >= B43_STAT_STARTED) {
5049 err = b43_wireless_core_start(dev);
5050 if (err) {
5051 b43_wireless_core_exit(dev);
5052 goto out;
5053 }
5054 }
Michael Buesch3bf0a322008-05-22 16:32:16 +02005055out:
5056 if (err)
5057 wl->current_dev = NULL; /* Failed to init the dev. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005058 mutex_unlock(&wl->mutex);
Felix Fietkau2a190322011-08-10 13:50:30 -06005059
5060 if (err) {
Michael Buesche4d6b792007-09-18 15:39:42 -04005061 b43err(wl, "Controller restart FAILED\n");
Felix Fietkau2a190322011-08-10 13:50:30 -06005062 return;
5063 }
5064
5065 /* reload configuration */
5066 b43_op_config(wl->hw, ~0);
5067 if (wl->vif)
5068 b43_op_bss_info_changed(wl->hw, wl->vif, &wl->vif->bss_conf, ~0);
5069
5070 b43info(wl, "Controller restarted\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04005071}
5072
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005073static int b43_setup_bands(struct b43_wldev *dev,
Michael Buesch96c755a2008-01-06 00:09:46 +01005074 bool have_2ghz_phy, bool have_5ghz_phy)
Michael Buesche4d6b792007-09-18 15:39:42 -04005075{
5076 struct ieee80211_hw *hw = dev->wl->hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005077
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005078 if (have_2ghz_phy)
5079 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
5080 if (dev->phy.type == B43_PHYTYPE_N) {
5081 if (have_5ghz_phy)
5082 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
5083 } else {
5084 if (have_5ghz_phy)
5085 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
5086 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005087
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005088 dev->phy.supports_2ghz = have_2ghz_phy;
5089 dev->phy.supports_5ghz = have_5ghz_phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005090
5091 return 0;
5092}
5093
5094static void b43_wireless_core_detach(struct b43_wldev *dev)
5095{
5096 /* We release firmware that late to not be required to re-request
5097 * is all the time when we reinit the core. */
5098 b43_release_firmware(dev);
Michael Bueschfb111372008-09-02 13:00:34 +02005099 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005100}
5101
Rafał Miłecki075ca602014-05-19 23:18:54 +02005102static void b43_supported_bands(struct b43_wldev *dev, bool *have_2ghz_phy,
5103 bool *have_5ghz_phy)
5104{
5105 u16 dev_id = 0;
5106
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005107#ifdef CONFIG_B43_BCMA
5108 if (dev->dev->bus_type == B43_BUS_BCMA &&
5109 dev->dev->bdev->bus->hosttype == BCMA_HOSTTYPE_PCI)
5110 dev_id = dev->dev->bdev->bus->host_pci->device;
5111#endif
Rafał Miłecki075ca602014-05-19 23:18:54 +02005112#ifdef CONFIG_B43_SSB
5113 if (dev->dev->bus_type == B43_BUS_SSB &&
5114 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI)
5115 dev_id = dev->dev->sdev->bus->host_pci->device;
5116#endif
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005117 /* Override with SPROM value if available */
5118 if (dev->dev->bus_sprom->dev_id)
5119 dev_id = dev->dev->bus_sprom->dev_id;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005120
5121 /* Note: below IDs can be "virtual" (not maching e.g. real PCI ID) */
5122 switch (dev_id) {
5123 case 0x4324: /* BCM4306 */
5124 case 0x4312: /* BCM4311 */
5125 case 0x4319: /* BCM4318 */
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005126 case 0x4328: /* BCM4321 */
5127 case 0x432b: /* BCM4322 */
5128 case 0x4350: /* BCM43222 */
5129 case 0x4353: /* BCM43224 */
5130 case 0x0576: /* BCM43224 */
5131 case 0x435f: /* BCM6362 */
5132 case 0x4331: /* BCM4331 */
5133 case 0x4359: /* BCM43228 */
5134 case 0x43a0: /* BCM4360 */
5135 case 0x43b1: /* BCM4352 */
Rafał Miłecki075ca602014-05-19 23:18:54 +02005136 /* Dual band devices */
5137 *have_2ghz_phy = true;
5138 *have_5ghz_phy = true;
5139 return;
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005140 case 0x4321: /* BCM4306 */
5141 case 0x4313: /* BCM4311 */
5142 case 0x431a: /* BCM4318 */
5143 case 0x432a: /* BCM4321 */
5144 case 0x432d: /* BCM4322 */
5145 case 0x4352: /* BCM43222 */
5146 case 0x4333: /* BCM4331 */
5147 case 0x43a2: /* BCM4360 */
5148 case 0x43b3: /* BCM4352 */
5149 /* 5 GHz only devices */
5150 *have_2ghz_phy = false;
5151 *have_5ghz_phy = true;
5152 return;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005153 }
5154
5155 /* As a fallback, try to guess using PHY type */
5156 switch (dev->phy.type) {
5157 case B43_PHYTYPE_A:
5158 *have_2ghz_phy = false;
5159 *have_5ghz_phy = true;
5160 return;
5161 case B43_PHYTYPE_G:
5162 case B43_PHYTYPE_N:
5163 case B43_PHYTYPE_LP:
5164 case B43_PHYTYPE_HT:
5165 case B43_PHYTYPE_LCN:
5166 *have_2ghz_phy = true;
5167 *have_5ghz_phy = false;
5168 return;
5169 }
5170
5171 B43_WARN_ON(1);
5172}
5173
Michael Buesche4d6b792007-09-18 15:39:42 -04005174static int b43_wireless_core_attach(struct b43_wldev *dev)
5175{
5176 struct b43_wl *wl = dev->wl;
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005177 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04005178 int err;
Rafał Miłecki40c62262011-07-18 02:01:30 +02005179 u32 tmp;
Rusty Russell3db1cd52011-12-19 13:56:45 +00005180 bool have_2ghz_phy = false, have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005181
5182 /* Do NOT do any device initialization here.
5183 * Do it in wireless_core_init() instead.
5184 * This function is for gathering basic information about the HW, only.
5185 * Also some structs may be set up here. But most likely you want to have
5186 * that in core_init(), too.
5187 */
5188
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005189 err = b43_bus_powerup(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04005190 if (err) {
5191 b43err(wl, "Bus powerup failed\n");
5192 goto out;
5193 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005194
Rafał Miłecki09951ad2014-05-27 22:07:31 +02005195 phy->do_full_init = true;
5196
Rafał Miłecki075ca602014-05-19 23:18:54 +02005197 /* Try to guess supported bands for the first init needs */
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005198 switch (dev->dev->bus_type) {
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005199#ifdef CONFIG_B43_BCMA
5200 case B43_BUS_BCMA:
Rafał Miłecki40c62262011-07-18 02:01:30 +02005201 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
5202 have_2ghz_phy = !!(tmp & B43_BCMA_IOST_2G_PHY);
5203 have_5ghz_phy = !!(tmp & B43_BCMA_IOST_5G_PHY);
Rafał Miłecki42c9a452011-07-06 15:45:27 +02005204 break;
5205#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005206#ifdef CONFIG_B43_SSB
5207 case B43_BUS_SSB:
5208 if (dev->dev->core_rev >= 5) {
Rafał Miłecki40c62262011-07-18 02:01:30 +02005209 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
5210 have_2ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_2GHZ_PHY);
5211 have_5ghz_phy = !!(tmp & B43_TMSHIGH_HAVE_5GHZ_PHY);
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02005212 } else
5213 B43_WARN_ON(1);
5214 break;
5215#endif
5216 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005217
Michael Buesch96c755a2008-01-06 00:09:46 +01005218 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005219 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005220
Rafał Miłecki075ca602014-05-19 23:18:54 +02005221 /* Get the PHY type. */
Michael Buesche4d6b792007-09-18 15:39:42 -04005222 err = b43_phy_versioning(dev);
5223 if (err)
Michael Buesch21954c32007-09-27 15:31:40 +02005224 goto err_powerdown;
Rafał Miłecki075ca602014-05-19 23:18:54 +02005225
5226 /* Get real info about supported bands */
5227 b43_supported_bands(dev, &have_2ghz_phy, &have_5ghz_phy);
5228
5229 /* We don't support 5 GHz on some PHYs yet */
5230 switch (dev->phy.type) {
5231 case B43_PHYTYPE_A:
5232 case B43_PHYTYPE_N:
5233 case B43_PHYTYPE_LP:
Rafał Miłecki773cfc52014-05-19 23:18:55 +02005234 case B43_PHYTYPE_HT:
Rafał Miłecki075ca602014-05-19 23:18:54 +02005235 b43warn(wl, "5 GHz band is unsupported on this PHY\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +00005236 have_5ghz_phy = false;
Michael Buesche4d6b792007-09-18 15:39:42 -04005237 }
Rafał Miłecki075ca602014-05-19 23:18:54 +02005238
5239 if (!have_2ghz_phy && !have_5ghz_phy) {
5240 b43err(wl, "b43 can't support any band on this device\n");
Michael Buesch96c755a2008-01-06 00:09:46 +01005241 err = -EOPNOTSUPP;
5242 goto err_powerdown;
5243 }
Michael Buesch2e35af12008-04-27 19:06:18 +02005244
Michael Bueschfb111372008-09-02 13:00:34 +02005245 err = b43_phy_allocate(dev);
5246 if (err)
5247 goto err_powerdown;
5248
Michael Buesch96c755a2008-01-06 00:09:46 +01005249 dev->phy.gmode = have_2ghz_phy;
Rafał Miłecki4da909e2011-06-02 01:07:12 +02005250 b43_wireless_core_reset(dev, dev->phy.gmode);
Michael Buesche4d6b792007-09-18 15:39:42 -04005251
5252 err = b43_validate_chipaccess(dev);
5253 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005254 goto err_phy_free;
Michael Bueschbb1eeff2008-02-09 12:08:58 +01005255 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
Michael Buesche4d6b792007-09-18 15:39:42 -04005256 if (err)
Michael Bueschfb111372008-09-02 13:00:34 +02005257 goto err_phy_free;
Michael Buesche4d6b792007-09-18 15:39:42 -04005258
5259 /* Now set some default "current_dev" */
5260 if (!wl->current_dev)
5261 wl->current_dev = dev;
5262 INIT_WORK(&dev->restart_work, b43_chip_reset);
5263
Michael Bueschcb24f572008-09-03 12:12:20 +02005264 dev->phy.ops->switch_analog(dev, 0);
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005265 b43_device_disable(dev, 0);
5266 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005267
5268out:
5269 return err;
5270
Michael Bueschfb111372008-09-02 13:00:34 +02005271err_phy_free:
5272 b43_phy_free(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005273err_powerdown:
Rafał Miłecki24ca39d2011-05-18 02:06:43 +02005274 b43_bus_may_powerdown(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005275 return err;
5276}
5277
Rafał Miłecki482f0532011-05-18 02:06:36 +02005278static void b43_one_core_detach(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005279{
5280 struct b43_wldev *wldev;
5281 struct b43_wl *wl;
5282
Michael Buesch3bf0a322008-05-22 16:32:16 +02005283 /* Do not cancel ieee80211-workqueue based work here.
5284 * See comment in b43_remove(). */
5285
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005286 wldev = b43_bus_get_wldev(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005287 wl = wldev->wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005288 b43_debugfs_remove_device(wldev);
5289 b43_wireless_core_detach(wldev);
5290 list_del(&wldev->list);
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005291 b43_bus_set_wldev(dev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005292 kfree(wldev);
5293}
5294
Rafał Miłecki482f0532011-05-18 02:06:36 +02005295static int b43_one_core_attach(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005296{
5297 struct b43_wldev *wldev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005298 int err = -ENOMEM;
5299
Michael Buesche4d6b792007-09-18 15:39:42 -04005300 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
5301 if (!wldev)
5302 goto out;
5303
Linus Torvalds9e3bd912010-02-26 10:34:27 -08005304 wldev->use_pio = b43_modparam_pio;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005305 wldev->dev = dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005306 wldev->wl = wl;
5307 b43_set_status(wldev, B43_STAT_UNINIT);
5308 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
Michael Buesche4d6b792007-09-18 15:39:42 -04005309 INIT_LIST_HEAD(&wldev->list);
5310
5311 err = b43_wireless_core_attach(wldev);
5312 if (err)
5313 goto err_kfree_wldev;
5314
Rafał Miłecki74abacb2011-07-06 15:45:28 +02005315 b43_bus_set_wldev(dev, wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005316 b43_debugfs_add_device(wldev);
5317
5318 out:
5319 return err;
5320
5321 err_kfree_wldev:
5322 kfree(wldev);
5323 return err;
5324}
5325
Michael Buesch9fc38452008-04-19 16:53:00 +02005326#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
5327 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
5328 (pdev->device == _device) && \
5329 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
5330 (pdev->subsystem_device == _subdevice) )
5331
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005332#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005333static void b43_sprom_fixup(struct ssb_bus *bus)
5334{
Michael Buesch1855ba72008-04-18 20:51:41 +02005335 struct pci_dev *pdev;
5336
Michael Buesche4d6b792007-09-18 15:39:42 -04005337 /* boardflags workarounds */
5338 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005339 bus->chip_id == 0x4301 && bus->sprom.board_rev == 0x74)
Larry Finger95de2842007-11-09 16:57:18 -06005340 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
Michael Buesche4d6b792007-09-18 15:39:42 -04005341 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
Hauke Mehrtens5a20ef32012-04-29 02:04:06 +02005342 bus->boardinfo.type == 0x4E && bus->sprom.board_rev > 0x40)
Larry Finger95de2842007-11-09 16:57:18 -06005343 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
Michael Buesch1855ba72008-04-18 20:51:41 +02005344 if (bus->bustype == SSB_BUSTYPE_PCI) {
5345 pdev = bus->host_pci;
Michael Buesch9fc38452008-04-19 16:53:00 +02005346 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
Larry Finger430cd472008-08-14 18:57:11 -05005347 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
Larry Finger570bdfb2008-09-26 08:23:00 -05005348 IS_PDEV(pdev, BROADCOM, 0x4320, HP, 0x12f8) ||
Michael Buesch9fc38452008-04-19 16:53:00 +02005349 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
Larry Fingera58d4522008-08-10 10:19:33 -05005350 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
Larry Finger3bb91bf2008-09-19 14:47:38 -05005351 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
5352 IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
Michael Buesch1855ba72008-04-18 20:51:41 +02005353 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
5354 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005355}
5356
Rafał Miłecki482f0532011-05-18 02:06:36 +02005357static void b43_wireless_exit(struct b43_bus_dev *dev, struct b43_wl *wl)
Michael Buesche4d6b792007-09-18 15:39:42 -04005358{
5359 struct ieee80211_hw *hw = wl->hw;
5360
Rafał Miłecki482f0532011-05-18 02:06:36 +02005361 ssb_set_devtypedata(dev->sdev, NULL);
Michael Buesche4d6b792007-09-18 15:39:42 -04005362 ieee80211_free_hw(hw);
5363}
Rafał Miłeckibd7c8a52014-05-10 19:52:18 +02005364#endif
Michael Buesche4d6b792007-09-18 15:39:42 -04005365
Rafał Miłeckid1507052011-07-05 23:54:07 +02005366static struct b43_wl *b43_wireless_init(struct b43_bus_dev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005367{
Rafał Miłeckid1507052011-07-05 23:54:07 +02005368 struct ssb_sprom *sprom = dev->bus_sprom;
Michael Buesche4d6b792007-09-18 15:39:42 -04005369 struct ieee80211_hw *hw;
5370 struct b43_wl *wl;
Rafał Miłecki2729df22011-07-18 22:45:58 +02005371 char chip_name[6];
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005372 int queue_num;
Michael Buesche4d6b792007-09-18 15:39:42 -04005373
5374 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
5375 if (!hw) {
5376 b43err(NULL, "Could not allocate ieee80211 device\n");
Rafał Miłecki0355a342011-05-17 14:00:01 +02005377 return ERR_PTR(-ENOMEM);
Michael Buesche4d6b792007-09-18 15:39:42 -04005378 }
Michael Buesch403a3a12009-06-08 21:04:57 +02005379 wl = hw_to_b43_wl(hw);
Michael Buesche4d6b792007-09-18 15:39:42 -04005380
5381 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02005382 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
John W. Linvillef5c044e2010-04-30 15:37:00 -04005383 IEEE80211_HW_SIGNAL_DBM;
Bruno Randolf566bfe52008-05-08 19:15:40 +02005384
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07005385 hw->wiphy->interface_modes =
5386 BIT(NL80211_IFTYPE_AP) |
5387 BIT(NL80211_IFTYPE_MESH_POINT) |
5388 BIT(NL80211_IFTYPE_STATION) |
5389 BIT(NL80211_IFTYPE_WDS) |
5390 BIT(NL80211_IFTYPE_ADHOC);
5391
Antonio Quartulli78f9c852012-04-01 00:35:40 +03005392 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5393
Oleksij Rempele64add22012-06-05 20:39:32 +02005394 wl->hw_registred = false;
Johannes Berge6a98542008-10-21 12:40:02 +02005395 hw->max_rates = 2;
Michael Buesche4d6b792007-09-18 15:39:42 -04005396 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger95de2842007-11-09 16:57:18 -06005397 if (is_valid_ether_addr(sprom->et1mac))
5398 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005399 else
Larry Finger95de2842007-11-09 16:57:18 -06005400 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Michael Buesche4d6b792007-09-18 15:39:42 -04005401
Michael Buesch403a3a12009-06-08 21:04:57 +02005402 /* Initialize struct b43_wl */
Michael Buesche4d6b792007-09-18 15:39:42 -04005403 wl->hw = hw;
Michael Buesche4d6b792007-09-18 15:39:42 -04005404 mutex_init(&wl->mutex);
Michael Buesch36dbd952009-09-04 22:51:29 +02005405 spin_lock_init(&wl->hardirq_lock);
Michael Buescha82d9922008-04-04 21:40:06 +02005406 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
Michael Buesch18c8ade2008-08-28 19:33:40 +02005407 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
Michael Bueschf5d40ee2009-09-04 22:53:18 +02005408 INIT_WORK(&wl->tx_work, b43_tx_work);
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01005409
5410 /* Initialize queues and flags. */
5411 for (queue_num = 0; queue_num < B43_QOS_QUEUE_NUM; queue_num++) {
5412 skb_queue_head_init(&wl->tx_queue[queue_num]);
5413 wl->tx_queue_stopped[queue_num] = 0;
5414 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005415
Rafał Miłecki2729df22011-07-18 22:45:58 +02005416 snprintf(chip_name, ARRAY_SIZE(chip_name),
5417 (dev->chip_id > 0x9999) ? "%d" : "%04X", dev->chip_id);
5418 b43info(wl, "Broadcom %s WLAN found (core revision %u)\n", chip_name,
5419 dev->core_rev);
Rafał Miłecki0355a342011-05-17 14:00:01 +02005420 return wl;
Michael Buesche4d6b792007-09-18 15:39:42 -04005421}
5422
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005423#ifdef CONFIG_B43_BCMA
5424static int b43_bcma_probe(struct bcma_device *core)
Michael Buesche4d6b792007-09-18 15:39:42 -04005425{
Rafał Miłecki397915c2011-07-06 19:03:46 +02005426 struct b43_bus_dev *dev;
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005427 struct b43_wl *wl;
5428 int err;
Rafał Miłecki397915c2011-07-06 19:03:46 +02005429
Rafał Miłecki89604002013-06-26 09:55:54 +02005430 if (!modparam_allhwsupport &&
5431 (core->id.rev == 0x17 || core->id.rev == 0x18)) {
5432 pr_err("Support for cores revisions 0x17 and 0x18 disabled by module param allhwsupport=0. Try b43.allhwsupport=1\n");
5433 return -ENOTSUPP;
5434 }
5435
Rafał Miłecki397915c2011-07-06 19:03:46 +02005436 dev = b43_bus_dev_bcma_init(core);
5437 if (!dev)
5438 return -ENODEV;
5439
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005440 wl = b43_wireless_init(dev);
5441 if (IS_ERR(wl)) {
5442 err = PTR_ERR(wl);
5443 goto bcma_out;
5444 }
5445
5446 err = b43_one_core_attach(dev, wl);
5447 if (err)
5448 goto bcma_err_wireless_exit;
5449
Larry Finger6b6fa582012-03-08 22:27:46 -06005450 /* setup and start work to load firmware */
5451 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5452 schedule_work(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005453
5454bcma_out:
5455 return err;
5456
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005457bcma_err_wireless_exit:
5458 ieee80211_free_hw(wl->hw);
5459 return err;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005460}
5461
5462static void b43_bcma_remove(struct bcma_device *core)
5463{
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005464 struct b43_wldev *wldev = bcma_get_drvdata(core);
5465 struct b43_wl *wl = wldev->wl;
5466
5467 /* We must cancel any work here before unregistering from ieee80211,
5468 * as the ieee80211 unreg will destroy the workqueue. */
5469 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005470 cancel_work_sync(&wl->firmware_load);
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005471
Oleksij Rempele64add22012-06-05 20:39:32 +02005472 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005473 if (!wldev->fw.ucode.data)
5474 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005475 if (wl->current_dev == wldev && wl->hw_registred) {
Oleksij Rempele64add22012-06-05 20:39:32 +02005476 b43_leds_stop(wldev);
5477 ieee80211_unregister_hw(wl->hw);
5478 }
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005479
5480 b43_one_core_detach(wldev->dev);
5481
Larry Finger09164042014-01-12 15:11:37 -06005482 /* Unregister HW RNG driver */
5483 b43_rng_exit(wl);
5484
Rafał Miłecki24aad3f2011-07-20 20:02:39 +02005485 b43_leds_unregister(wl);
5486
5487 ieee80211_free_hw(wl->hw);
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005488}
5489
5490static struct bcma_driver b43_bcma_driver = {
5491 .name = KBUILD_MODNAME,
5492 .id_table = b43_bcma_tbl,
5493 .probe = b43_bcma_probe,
5494 .remove = b43_bcma_remove,
5495};
5496#endif
5497
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005498#ifdef CONFIG_B43_SSB
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005499static
5500int b43_ssb_probe(struct ssb_device *sdev, const struct ssb_device_id *id)
Michael Buesche4d6b792007-09-18 15:39:42 -04005501{
Rafał Miłecki482f0532011-05-18 02:06:36 +02005502 struct b43_bus_dev *dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005503 struct b43_wl *wl;
5504 int err;
Michael Buesche4d6b792007-09-18 15:39:42 -04005505
Rafał Miłecki482f0532011-05-18 02:06:36 +02005506 dev = b43_bus_dev_ssb_init(sdev);
Dan Carpenter5b49b352011-06-09 10:09:34 +03005507 if (!dev)
5508 return -ENOMEM;
Rafał Miłecki482f0532011-05-18 02:06:36 +02005509
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005510 wl = ssb_get_devtypedata(sdev);
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005511 if (wl) {
5512 b43err(NULL, "Dual-core devices are not supported\n");
5513 err = -ENOTSUPP;
5514 goto err_ssb_kfree_dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005515 }
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005516
5517 b43_sprom_fixup(sdev->bus);
5518
5519 wl = b43_wireless_init(dev);
5520 if (IS_ERR(wl)) {
5521 err = PTR_ERR(wl);
5522 goto err_ssb_kfree_dev;
5523 }
5524 ssb_set_devtypedata(sdev, wl);
5525 B43_WARN_ON(ssb_get_devtypedata(sdev) != wl);
5526
Michael Buesche4d6b792007-09-18 15:39:42 -04005527 err = b43_one_core_attach(dev, wl);
5528 if (err)
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005529 goto err_ssb_wireless_exit;
Michael Buesche4d6b792007-09-18 15:39:42 -04005530
Larry Finger6b6fa582012-03-08 22:27:46 -06005531 /* setup and start work to load firmware */
5532 INIT_WORK(&wl->firmware_load, b43_request_firmware);
5533 schedule_work(&wl->firmware_load);
Michael Buesche4d6b792007-09-18 15:39:42 -04005534
Michael Buesche4d6b792007-09-18 15:39:42 -04005535 return err;
5536
Rafał Miłecki8f15e282014-04-20 20:30:58 +02005537err_ssb_wireless_exit:
5538 b43_wireless_exit(dev, wl);
5539err_ssb_kfree_dev:
5540 kfree(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005541 return err;
5542}
5543
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005544static void b43_ssb_remove(struct ssb_device *sdev)
Michael Buesche4d6b792007-09-18 15:39:42 -04005545{
Rafał Miłeckiaa634182011-05-18 02:06:35 +02005546 struct b43_wl *wl = ssb_get_devtypedata(sdev);
5547 struct b43_wldev *wldev = ssb_get_drvdata(sdev);
Pavel Roskine61b52d2011-07-22 18:07:13 -04005548 struct b43_bus_dev *dev = wldev->dev;
Michael Buesche4d6b792007-09-18 15:39:42 -04005549
Michael Buesch3bf0a322008-05-22 16:32:16 +02005550 /* We must cancel any work here before unregistering from ieee80211,
5551 * as the ieee80211 unreg will destroy the workqueue. */
5552 cancel_work_sync(&wldev->restart_work);
Larry Finger63a02ce2013-02-25 06:09:24 +00005553 cancel_work_sync(&wl->firmware_load);
Michael Buesch3bf0a322008-05-22 16:32:16 +02005554
Michael Buesche4d6b792007-09-18 15:39:42 -04005555 B43_WARN_ON(!wl);
Larry Fingerf89ff642012-10-24 08:57:16 -05005556 if (!wldev->fw.ucode.data)
5557 return; /* NULL if firmware never loaded */
Oleksij Rempele64add22012-06-05 20:39:32 +02005558 if (wl->current_dev == wldev && wl->hw_registred) {
Albert Herranz82905ac2009-09-16 00:26:19 +02005559 b43_leds_stop(wldev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005560 ieee80211_unregister_hw(wl->hw);
Michael Buesch403a3a12009-06-08 21:04:57 +02005561 }
Michael Buesche4d6b792007-09-18 15:39:42 -04005562
Pavel Roskine61b52d2011-07-22 18:07:13 -04005563 b43_one_core_detach(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04005564
Larry Finger09164042014-01-12 15:11:37 -06005565 /* Unregister HW RNG driver */
5566 b43_rng_exit(wl);
5567
Rafał Miłecki644aa4d2014-04-21 10:54:29 +02005568 b43_leds_unregister(wl);
5569 b43_wireless_exit(dev, wl);
Michael Buesche4d6b792007-09-18 15:39:42 -04005570}
5571
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005572static struct ssb_driver b43_ssb_driver = {
5573 .name = KBUILD_MODNAME,
5574 .id_table = b43_ssb_tbl,
5575 .probe = b43_ssb_probe,
5576 .remove = b43_ssb_remove,
5577};
5578#endif /* CONFIG_B43_SSB */
5579
Michael Buesche4d6b792007-09-18 15:39:42 -04005580/* Perform a hardware reset. This can be called from any context. */
5581void b43_controller_restart(struct b43_wldev *dev, const char *reason)
5582{
5583 /* Must avoid requeueing, if we are in shutdown. */
5584 if (b43_status(dev) < B43_STAT_INITIALIZED)
5585 return;
5586 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04005587 ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04005588}
5589
Michael Buesch26bc7832008-02-09 00:18:35 +01005590static void b43_print_driverinfo(void)
5591{
5592 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005593 *feat_leds = "", *feat_sdio = "";
Michael Buesch26bc7832008-02-09 00:18:35 +01005594
5595#ifdef CONFIG_B43_PCI_AUTOSELECT
5596 feat_pci = "P";
5597#endif
5598#ifdef CONFIG_B43_PCMCIA
5599 feat_pcmcia = "M";
5600#endif
Rafał Miłecki692d2c02010-12-07 21:56:00 +01005601#ifdef CONFIG_B43_PHY_N
Michael Buesch26bc7832008-02-09 00:18:35 +01005602 feat_nphy = "N";
5603#endif
5604#ifdef CONFIG_B43_LEDS
5605 feat_leds = "L";
5606#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005607#ifdef CONFIG_B43_SDIO
5608 feat_sdio = "S";
5609#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005610 printk(KERN_INFO "Broadcom 43xx driver loaded "
Michael Büsch8b0be902011-08-21 17:24:47 +02005611 "[ Features: %s%s%s%s%s ]\n",
Michael Buesch26bc7832008-02-09 00:18:35 +01005612 feat_pci, feat_pcmcia, feat_nphy,
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005613 feat_leds, feat_sdio);
Michael Buesch26bc7832008-02-09 00:18:35 +01005614}
5615
Michael Buesche4d6b792007-09-18 15:39:42 -04005616static int __init b43_init(void)
5617{
5618 int err;
5619
5620 b43_debugfs_init();
5621 err = b43_pcmcia_init();
5622 if (err)
5623 goto err_dfs_exit;
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005624 err = b43_sdio_init();
Michael Buesche4d6b792007-09-18 15:39:42 -04005625 if (err)
5626 goto err_pcmcia_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005627#ifdef CONFIG_B43_BCMA
5628 err = bcma_driver_register(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005629 if (err)
5630 goto err_sdio_exit;
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005631#endif
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005632#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005633 err = ssb_driver_register(&b43_ssb_driver);
5634 if (err)
5635 goto err_bcma_driver_exit;
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005636#endif
Michael Buesch26bc7832008-02-09 00:18:35 +01005637 b43_print_driverinfo();
Michael Buesche4d6b792007-09-18 15:39:42 -04005638
5639 return err;
5640
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005641#ifdef CONFIG_B43_SSB
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005642err_bcma_driver_exit:
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005643#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005644#ifdef CONFIG_B43_BCMA
5645 bcma_driver_unregister(&b43_bcma_driver);
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005646err_sdio_exit:
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005647#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005648 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005649err_pcmcia_exit:
5650 b43_pcmcia_exit();
5651err_dfs_exit:
5652 b43_debugfs_exit();
5653 return err;
5654}
5655
5656static void __exit b43_exit(void)
5657{
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005658#ifdef CONFIG_B43_SSB
Michael Buesche4d6b792007-09-18 15:39:42 -04005659 ssb_driver_unregister(&b43_ssb_driver);
Rafał Miłeckiaec7ffd2011-06-14 08:18:59 +02005660#endif
Rafał Miłecki3c65ab62011-06-02 09:56:04 +02005661#ifdef CONFIG_B43_BCMA
5662 bcma_driver_unregister(&b43_bcma_driver);
5663#endif
Albert Herranz3dbba8e2009-09-10 19:34:49 +02005664 b43_sdio_exit();
Michael Buesche4d6b792007-09-18 15:39:42 -04005665 b43_pcmcia_exit();
5666 b43_debugfs_exit();
5667}
5668
5669module_init(b43_init)
5670module_exit(b43_exit)