3 Broadcom B43 wireless driver
5 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
6 Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
7 Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
8 Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
11 Some parts of the code in this file are derived from the ipw2200
12 driver Copyright(c) 2003 - 2004 Intel Corporation.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27 Boston, MA 02110-1301, USA.
31 #include <linux/delay.h>
32 #include <linux/init.h>
33 #include <linux/moduleparam.h>
34 #include <linux/if_arp.h>
35 #include <linux/etherdevice.h>
36 #include <linux/firmware.h>
37 #include <linux/wireless.h>
38 #include <linux/workqueue.h>
39 #include <linux/skbuff.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/unaligned.h>
47 #include "phy_common.h"
57 MODULE_DESCRIPTION("Broadcom B43 wireless driver");
58 MODULE_AUTHOR("Martin Langer");
59 MODULE_AUTHOR("Stefano Brivio");
60 MODULE_AUTHOR("Michael Buesch");
61 MODULE_LICENSE("GPL");
63 MODULE_FIRMWARE(B43_SUPPORTED_FIRMWARE_ID);
66 static int modparam_bad_frames_preempt;
67 module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
68 MODULE_PARM_DESC(bad_frames_preempt,
69 "enable(1) / disable(0) Bad Frames Preemption");
71 static char modparam_fwpostfix[16];
72 module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
73 MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
75 static int modparam_hwpctl;
76 module_param_named(hwpctl, modparam_hwpctl, int, 0444);
77 MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
79 static int modparam_nohwcrypt;
80 module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
81 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
83 int b43_modparam_qos = 1;
84 module_param_named(qos, b43_modparam_qos, int, 0444);
85 MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
87 static int modparam_btcoex = 1;
88 module_param_named(btcoex, modparam_btcoex, int, 0444);
89 MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistance (default on)");
92 static const struct ssb_device_id b43_ssb_tbl[] = {
93 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
94 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
95 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
96 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
97 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
98 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
99 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
103 MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
105 /* Channel and ratetables are shared for all devices.
106 * They can't be const, because ieee80211 puts some precalculated
107 * data in there. This data is the same for all devices, so we don't
108 * get concurrency issues */
109 #define RATETAB_ENT(_rateid, _flags) \
111 .bitrate = B43_RATE_TO_BASE100KBPS(_rateid), \
112 .hw_value = (_rateid), \
117 * NOTE: When changing this, sync with xmit.c's
118 * b43_plcp_get_bitrate_idx_* functions!
120 static struct ieee80211_rate __b43_ratetable[] = {
121 RATETAB_ENT(B43_CCK_RATE_1MB, 0),
122 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
123 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
124 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
125 RATETAB_ENT(B43_OFDM_RATE_6MB, 0),
126 RATETAB_ENT(B43_OFDM_RATE_9MB, 0),
127 RATETAB_ENT(B43_OFDM_RATE_12MB, 0),
128 RATETAB_ENT(B43_OFDM_RATE_18MB, 0),
129 RATETAB_ENT(B43_OFDM_RATE_24MB, 0),
130 RATETAB_ENT(B43_OFDM_RATE_36MB, 0),
131 RATETAB_ENT(B43_OFDM_RATE_48MB, 0),
132 RATETAB_ENT(B43_OFDM_RATE_54MB, 0),
135 #define b43_a_ratetable (__b43_ratetable + 4)
136 #define b43_a_ratetable_size 8
137 #define b43_b_ratetable (__b43_ratetable + 0)
138 #define b43_b_ratetable_size 4
139 #define b43_g_ratetable (__b43_ratetable + 0)
140 #define b43_g_ratetable_size 12
142 #define CHAN4G(_channel, _freq, _flags) { \
143 .band = IEEE80211_BAND_2GHZ, \
144 .center_freq = (_freq), \
145 .hw_value = (_channel), \
147 .max_antenna_gain = 0, \
150 static struct ieee80211_channel b43_2ghz_chantable[] = {
168 #define CHAN5G(_channel, _flags) { \
169 .band = IEEE80211_BAND_5GHZ, \
170 .center_freq = 5000 + (5 * (_channel)), \
171 .hw_value = (_channel), \
173 .max_antenna_gain = 0, \
176 static struct ieee80211_channel b43_5ghz_nphy_chantable[] = {
177 CHAN5G(32, 0), CHAN5G(34, 0),
178 CHAN5G(36, 0), CHAN5G(38, 0),
179 CHAN5G(40, 0), CHAN5G(42, 0),
180 CHAN5G(44, 0), CHAN5G(46, 0),
181 CHAN5G(48, 0), CHAN5G(50, 0),
182 CHAN5G(52, 0), CHAN5G(54, 0),
183 CHAN5G(56, 0), CHAN5G(58, 0),
184 CHAN5G(60, 0), CHAN5G(62, 0),
185 CHAN5G(64, 0), CHAN5G(66, 0),
186 CHAN5G(68, 0), CHAN5G(70, 0),
187 CHAN5G(72, 0), CHAN5G(74, 0),
188 CHAN5G(76, 0), CHAN5G(78, 0),
189 CHAN5G(80, 0), CHAN5G(82, 0),
190 CHAN5G(84, 0), CHAN5G(86, 0),
191 CHAN5G(88, 0), CHAN5G(90, 0),
192 CHAN5G(92, 0), CHAN5G(94, 0),
193 CHAN5G(96, 0), CHAN5G(98, 0),
194 CHAN5G(100, 0), CHAN5G(102, 0),
195 CHAN5G(104, 0), CHAN5G(106, 0),
196 CHAN5G(108, 0), CHAN5G(110, 0),
197 CHAN5G(112, 0), CHAN5G(114, 0),
198 CHAN5G(116, 0), CHAN5G(118, 0),
199 CHAN5G(120, 0), CHAN5G(122, 0),
200 CHAN5G(124, 0), CHAN5G(126, 0),
201 CHAN5G(128, 0), CHAN5G(130, 0),
202 CHAN5G(132, 0), CHAN5G(134, 0),
203 CHAN5G(136, 0), CHAN5G(138, 0),
204 CHAN5G(140, 0), CHAN5G(142, 0),
205 CHAN5G(144, 0), CHAN5G(145, 0),
206 CHAN5G(146, 0), CHAN5G(147, 0),
207 CHAN5G(148, 0), CHAN5G(149, 0),
208 CHAN5G(150, 0), CHAN5G(151, 0),
209 CHAN5G(152, 0), CHAN5G(153, 0),
210 CHAN5G(154, 0), CHAN5G(155, 0),
211 CHAN5G(156, 0), CHAN5G(157, 0),
212 CHAN5G(158, 0), CHAN5G(159, 0),
213 CHAN5G(160, 0), CHAN5G(161, 0),
214 CHAN5G(162, 0), CHAN5G(163, 0),
215 CHAN5G(164, 0), CHAN5G(165, 0),
216 CHAN5G(166, 0), CHAN5G(168, 0),
217 CHAN5G(170, 0), CHAN5G(172, 0),
218 CHAN5G(174, 0), CHAN5G(176, 0),
219 CHAN5G(178, 0), CHAN5G(180, 0),
220 CHAN5G(182, 0), CHAN5G(184, 0),
221 CHAN5G(186, 0), CHAN5G(188, 0),
222 CHAN5G(190, 0), CHAN5G(192, 0),
223 CHAN5G(194, 0), CHAN5G(196, 0),
224 CHAN5G(198, 0), CHAN5G(200, 0),
225 CHAN5G(202, 0), CHAN5G(204, 0),
226 CHAN5G(206, 0), CHAN5G(208, 0),
227 CHAN5G(210, 0), CHAN5G(212, 0),
228 CHAN5G(214, 0), CHAN5G(216, 0),
229 CHAN5G(218, 0), CHAN5G(220, 0),
230 CHAN5G(222, 0), CHAN5G(224, 0),
231 CHAN5G(226, 0), CHAN5G(228, 0),
234 static struct ieee80211_channel b43_5ghz_aphy_chantable[] = {
235 CHAN5G(34, 0), CHAN5G(36, 0),
236 CHAN5G(38, 0), CHAN5G(40, 0),
237 CHAN5G(42, 0), CHAN5G(44, 0),
238 CHAN5G(46, 0), CHAN5G(48, 0),
239 CHAN5G(52, 0), CHAN5G(56, 0),
240 CHAN5G(60, 0), CHAN5G(64, 0),
241 CHAN5G(100, 0), CHAN5G(104, 0),
242 CHAN5G(108, 0), CHAN5G(112, 0),
243 CHAN5G(116, 0), CHAN5G(120, 0),
244 CHAN5G(124, 0), CHAN5G(128, 0),
245 CHAN5G(132, 0), CHAN5G(136, 0),
246 CHAN5G(140, 0), CHAN5G(149, 0),
247 CHAN5G(153, 0), CHAN5G(157, 0),
248 CHAN5G(161, 0), CHAN5G(165, 0),
249 CHAN5G(184, 0), CHAN5G(188, 0),
250 CHAN5G(192, 0), CHAN5G(196, 0),
251 CHAN5G(200, 0), CHAN5G(204, 0),
252 CHAN5G(208, 0), CHAN5G(212, 0),
257 static struct ieee80211_supported_band b43_band_5GHz_nphy = {
258 .band = IEEE80211_BAND_5GHZ,
259 .channels = b43_5ghz_nphy_chantable,
260 .n_channels = ARRAY_SIZE(b43_5ghz_nphy_chantable),
261 .bitrates = b43_a_ratetable,
262 .n_bitrates = b43_a_ratetable_size,
265 static struct ieee80211_supported_band b43_band_5GHz_aphy = {
266 .band = IEEE80211_BAND_5GHZ,
267 .channels = b43_5ghz_aphy_chantable,
268 .n_channels = ARRAY_SIZE(b43_5ghz_aphy_chantable),
269 .bitrates = b43_a_ratetable,
270 .n_bitrates = b43_a_ratetable_size,
273 static struct ieee80211_supported_band b43_band_2GHz = {
274 .band = IEEE80211_BAND_2GHZ,
275 .channels = b43_2ghz_chantable,
276 .n_channels = ARRAY_SIZE(b43_2ghz_chantable),
277 .bitrates = b43_g_ratetable,
278 .n_bitrates = b43_g_ratetable_size,
281 static void b43_wireless_core_exit(struct b43_wldev *dev);
282 static int b43_wireless_core_init(struct b43_wldev *dev);
283 static void b43_wireless_core_stop(struct b43_wldev *dev);
284 static int b43_wireless_core_start(struct b43_wldev *dev);
286 static int b43_ratelimit(struct b43_wl *wl)
288 if (!wl || !wl->current_dev)
290 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
292 /* We are up and running.
293 * Ratelimit the messages to avoid DoS over the net. */
294 return net_ratelimit();
297 void b43info(struct b43_wl *wl, const char *fmt, ...)
301 if (!b43_ratelimit(wl))
304 printk(KERN_INFO "b43-%s: ",
305 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
310 void b43err(struct b43_wl *wl, const char *fmt, ...)
314 if (!b43_ratelimit(wl))
317 printk(KERN_ERR "b43-%s ERROR: ",
318 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
323 void b43warn(struct b43_wl *wl, const char *fmt, ...)
327 if (!b43_ratelimit(wl))
330 printk(KERN_WARNING "b43-%s warning: ",
331 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
337 void b43dbg(struct b43_wl *wl, const char *fmt, ...)
342 printk(KERN_DEBUG "b43-%s debug: ",
343 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
349 static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
353 B43_WARN_ON(offset % 4 != 0);
355 macctl = b43_read32(dev, B43_MMIO_MACCTL);
356 if (macctl & B43_MACCTL_BE)
359 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
361 b43_write32(dev, B43_MMIO_RAM_DATA, val);
364 static inline void b43_shm_control_word(struct b43_wldev *dev,
365 u16 routing, u16 offset)
369 /* "offset" is the WORD offset. */
373 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
376 u32 __b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
380 if (routing == B43_SHM_SHARED) {
381 B43_WARN_ON(offset & 0x0001);
382 if (offset & 0x0003) {
383 /* Unaligned access */
384 b43_shm_control_word(dev, routing, offset >> 2);
385 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
387 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
388 ret |= b43_read16(dev, B43_MMIO_SHM_DATA);
394 b43_shm_control_word(dev, routing, offset);
395 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
400 u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
402 struct b43_wl *wl = dev->wl;
406 spin_lock_irqsave(&wl->shm_lock, flags);
407 ret = __b43_shm_read32(dev, routing, offset);
408 spin_unlock_irqrestore(&wl->shm_lock, flags);
413 u16 __b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
417 if (routing == B43_SHM_SHARED) {
418 B43_WARN_ON(offset & 0x0001);
419 if (offset & 0x0003) {
420 /* Unaligned access */
421 b43_shm_control_word(dev, routing, offset >> 2);
422 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
428 b43_shm_control_word(dev, routing, offset);
429 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
434 u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
436 struct b43_wl *wl = dev->wl;
440 spin_lock_irqsave(&wl->shm_lock, flags);
441 ret = __b43_shm_read16(dev, routing, offset);
442 spin_unlock_irqrestore(&wl->shm_lock, flags);
447 void __b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
449 if (routing == B43_SHM_SHARED) {
450 B43_WARN_ON(offset & 0x0001);
451 if (offset & 0x0003) {
452 /* Unaligned access */
453 b43_shm_control_word(dev, routing, offset >> 2);
454 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
455 (value >> 16) & 0xffff);
456 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
457 b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
462 b43_shm_control_word(dev, routing, offset);
463 b43_write32(dev, B43_MMIO_SHM_DATA, value);
466 void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
468 struct b43_wl *wl = dev->wl;
471 spin_lock_irqsave(&wl->shm_lock, flags);
472 __b43_shm_write32(dev, routing, offset, value);
473 spin_unlock_irqrestore(&wl->shm_lock, flags);
476 void __b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
478 if (routing == B43_SHM_SHARED) {
479 B43_WARN_ON(offset & 0x0001);
480 if (offset & 0x0003) {
481 /* Unaligned access */
482 b43_shm_control_word(dev, routing, offset >> 2);
483 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
488 b43_shm_control_word(dev, routing, offset);
489 b43_write16(dev, B43_MMIO_SHM_DATA, value);
492 void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
494 struct b43_wl *wl = dev->wl;
497 spin_lock_irqsave(&wl->shm_lock, flags);
498 __b43_shm_write16(dev, routing, offset, value);
499 spin_unlock_irqrestore(&wl->shm_lock, flags);
503 u64 b43_hf_read(struct b43_wldev * dev)
507 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
509 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI);
511 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
516 /* Write HostFlags */
517 void b43_hf_write(struct b43_wldev *dev, u64 value)
521 lo = (value & 0x00000000FFFFULL);
522 mi = (value & 0x0000FFFF0000ULL) >> 16;
523 hi = (value & 0xFFFF00000000ULL) >> 32;
524 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO, lo);
525 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFMI, mi);
526 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI, hi);
529 void b43_tsf_read(struct b43_wldev *dev, u64 * tsf)
531 /* We need to be careful. As we read the TSF from multiple
532 * registers, we should take care of register overflows.
533 * In theory, the whole tsf read process should be atomic.
534 * We try to be atomic here, by restaring the read process,
535 * if any of the high registers changed (overflew).
537 if (dev->dev->id.revision >= 3) {
538 u32 low, high, high2;
541 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
542 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
543 high2 = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
544 } while (unlikely(high != high2));
552 u16 test1, test2, test3;
555 v3 = b43_read16(dev, B43_MMIO_TSF_3);
556 v2 = b43_read16(dev, B43_MMIO_TSF_2);
557 v1 = b43_read16(dev, B43_MMIO_TSF_1);
558 v0 = b43_read16(dev, B43_MMIO_TSF_0);
560 test3 = b43_read16(dev, B43_MMIO_TSF_3);
561 test2 = b43_read16(dev, B43_MMIO_TSF_2);
562 test1 = b43_read16(dev, B43_MMIO_TSF_1);
563 } while (v3 != test3 || v2 != test2 || v1 != test1);
577 static void b43_time_lock(struct b43_wldev *dev)
581 macctl = b43_read32(dev, B43_MMIO_MACCTL);
582 macctl |= B43_MACCTL_TBTTHOLD;
583 b43_write32(dev, B43_MMIO_MACCTL, macctl);
584 /* Commit the write */
585 b43_read32(dev, B43_MMIO_MACCTL);
588 static void b43_time_unlock(struct b43_wldev *dev)
592 macctl = b43_read32(dev, B43_MMIO_MACCTL);
593 macctl &= ~B43_MACCTL_TBTTHOLD;
594 b43_write32(dev, B43_MMIO_MACCTL, macctl);
595 /* Commit the write */
596 b43_read32(dev, B43_MMIO_MACCTL);
599 static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
601 /* Be careful with the in-progress timer.
602 * First zero out the low register, so we have a full
603 * register-overflow duration to complete the operation.
605 if (dev->dev->id.revision >= 3) {
606 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
607 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
609 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, 0);
611 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, hi);
613 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, lo);
615 u16 v0 = (tsf & 0x000000000000FFFFULL);
616 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
617 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
618 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
620 b43_write16(dev, B43_MMIO_TSF_0, 0);
622 b43_write16(dev, B43_MMIO_TSF_3, v3);
624 b43_write16(dev, B43_MMIO_TSF_2, v2);
626 b43_write16(dev, B43_MMIO_TSF_1, v1);
628 b43_write16(dev, B43_MMIO_TSF_0, v0);
632 void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
635 b43_tsf_write_locked(dev, tsf);
636 b43_time_unlock(dev);
640 void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 * mac)
642 static const u8 zero_addr[ETH_ALEN] = { 0 };
649 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
653 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
656 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
659 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
662 static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
666 u8 mac_bssid[ETH_ALEN * 2];
670 bssid = dev->wl->bssid;
671 mac = dev->wl->mac_addr;
673 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
675 memcpy(mac_bssid, mac, ETH_ALEN);
676 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
678 /* Write our MAC address and BSSID to template ram */
679 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
680 tmp = (u32) (mac_bssid[i + 0]);
681 tmp |= (u32) (mac_bssid[i + 1]) << 8;
682 tmp |= (u32) (mac_bssid[i + 2]) << 16;
683 tmp |= (u32) (mac_bssid[i + 3]) << 24;
684 b43_ram_write(dev, 0x20 + i, tmp);
688 static void b43_upload_card_macaddress(struct b43_wldev *dev)
690 b43_write_mac_bssid_templates(dev);
691 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
694 static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
696 /* slot_time is in usec. */
697 if (dev->phy.type != B43_PHYTYPE_G)
699 b43_write16(dev, 0x684, 510 + slot_time);
700 b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
703 static void b43_short_slot_timing_enable(struct b43_wldev *dev)
705 b43_set_slot_time(dev, 9);
709 static void b43_short_slot_timing_disable(struct b43_wldev *dev)
711 b43_set_slot_time(dev, 20);
715 /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
716 * Returns the _previously_ enabled IRQ mask.
718 static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
722 old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
723 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
728 /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
729 * Returns the _previously_ enabled IRQ mask.
731 static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
735 old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
736 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
741 /* Synchronize IRQ top- and bottom-half.
742 * IRQs must be masked before calling this.
743 * This must not be called with the irq_lock held.
745 static void b43_synchronize_irq(struct b43_wldev *dev)
747 synchronize_irq(dev->dev->irq);
748 tasklet_kill(&dev->isr_tasklet);
751 /* DummyTransmission function, as documented on
752 * http://bcm-specs.sipsolutions.net/DummyTransmission
754 void b43_dummy_transmission(struct b43_wldev *dev)
756 struct b43_wl *wl = dev->wl;
757 struct b43_phy *phy = &dev->phy;
758 unsigned int i, max_loop;
771 buffer[0] = 0x000201CC;
776 buffer[0] = 0x000B846E;
783 spin_lock_irq(&wl->irq_lock);
784 write_lock(&wl->tx_lock);
786 for (i = 0; i < 5; i++)
787 b43_ram_write(dev, i * 4, buffer[i]);
790 b43_read32(dev, B43_MMIO_MACCTL);
792 b43_write16(dev, 0x0568, 0x0000);
793 b43_write16(dev, 0x07C0, 0x0000);
794 value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
795 b43_write16(dev, 0x050C, value);
796 b43_write16(dev, 0x0508, 0x0000);
797 b43_write16(dev, 0x050A, 0x0000);
798 b43_write16(dev, 0x054C, 0x0000);
799 b43_write16(dev, 0x056A, 0x0014);
800 b43_write16(dev, 0x0568, 0x0826);
801 b43_write16(dev, 0x0500, 0x0000);
802 b43_write16(dev, 0x0502, 0x0030);
804 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
805 b43_radio_write16(dev, 0x0051, 0x0017);
806 for (i = 0x00; i < max_loop; i++) {
807 value = b43_read16(dev, 0x050E);
812 for (i = 0x00; i < 0x0A; i++) {
813 value = b43_read16(dev, 0x050E);
818 for (i = 0x00; i < 0x0A; i++) {
819 value = b43_read16(dev, 0x0690);
820 if (!(value & 0x0100))
824 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
825 b43_radio_write16(dev, 0x0051, 0x0037);
827 write_unlock(&wl->tx_lock);
828 spin_unlock_irq(&wl->irq_lock);
831 static void key_write(struct b43_wldev *dev,
832 u8 index, u8 algorithm, const u8 * key)
839 /* Key index/algo block */
840 kidx = b43_kidx_to_fw(dev, index);
841 value = ((kidx << 4) | algorithm);
842 b43_shm_write16(dev, B43_SHM_SHARED,
843 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
845 /* Write the key to the Key Table Pointer offset */
846 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
847 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
849 value |= (u16) (key[i + 1]) << 8;
850 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
854 static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
856 u32 addrtmp[2] = { 0, 0, };
857 u8 per_sta_keys_start = 8;
859 if (b43_new_kidx_api(dev))
860 per_sta_keys_start = 4;
862 B43_WARN_ON(index < per_sta_keys_start);
863 /* We have two default TX keys and possibly two default RX keys.
864 * Physical mac 0 is mapped to physical key 4 or 8, depending
865 * on the firmware version.
866 * So we must adjust the index here.
868 index -= per_sta_keys_start;
871 addrtmp[0] = addr[0];
872 addrtmp[0] |= ((u32) (addr[1]) << 8);
873 addrtmp[0] |= ((u32) (addr[2]) << 16);
874 addrtmp[0] |= ((u32) (addr[3]) << 24);
875 addrtmp[1] = addr[4];
876 addrtmp[1] |= ((u32) (addr[5]) << 8);
879 if (dev->dev->id.revision >= 5) {
880 /* Receive match transmitter address mechanism */
881 b43_shm_write32(dev, B43_SHM_RCMTA,
882 (index * 2) + 0, addrtmp[0]);
883 b43_shm_write16(dev, B43_SHM_RCMTA,
884 (index * 2) + 1, addrtmp[1]);
886 /* RXE (Receive Engine) and
887 * PSM (Programmable State Machine) mechanism
890 /* TODO write to RCM 16, 19, 22 and 25 */
892 b43_shm_write32(dev, B43_SHM_SHARED,
893 B43_SHM_SH_PSM + (index * 6) + 0,
895 b43_shm_write16(dev, B43_SHM_SHARED,
896 B43_SHM_SH_PSM + (index * 6) + 4,
902 static void do_key_write(struct b43_wldev *dev,
903 u8 index, u8 algorithm,
904 const u8 * key, size_t key_len, const u8 * mac_addr)
906 u8 buf[B43_SEC_KEYSIZE] = { 0, };
907 u8 per_sta_keys_start = 8;
909 if (b43_new_kidx_api(dev))
910 per_sta_keys_start = 4;
912 B43_WARN_ON(index >= dev->max_nr_keys);
913 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
915 if (index >= per_sta_keys_start)
916 keymac_write(dev, index, NULL); /* First zero out mac. */
918 memcpy(buf, key, key_len);
919 key_write(dev, index, algorithm, buf);
920 if (index >= per_sta_keys_start)
921 keymac_write(dev, index, mac_addr);
923 dev->key[index].algorithm = algorithm;
926 static int b43_key_write(struct b43_wldev *dev,
927 int index, u8 algorithm,
928 const u8 * key, size_t key_len,
930 struct ieee80211_key_conf *keyconf)
935 if (key_len > B43_SEC_KEYSIZE)
937 for (i = 0; i < dev->max_nr_keys; i++) {
938 /* Check that we don't already have this key. */
939 B43_WARN_ON(dev->key[i].keyconf == keyconf);
942 /* Either pairwise key or address is 00:00:00:00:00:00
943 * for transmit-only keys. Search the index. */
944 if (b43_new_kidx_api(dev))
948 for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
949 if (!dev->key[i].keyconf) {
956 b43err(dev->wl, "Out of hardware key memory\n");
960 B43_WARN_ON(index > 3);
962 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
963 if ((index <= 3) && !b43_new_kidx_api(dev)) {
965 B43_WARN_ON(mac_addr);
966 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
968 keyconf->hw_key_idx = index;
969 dev->key[index].keyconf = keyconf;
974 static int b43_key_clear(struct b43_wldev *dev, int index)
976 if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
978 do_key_write(dev, index, B43_SEC_ALGO_NONE,
979 NULL, B43_SEC_KEYSIZE, NULL);
980 if ((index <= 3) && !b43_new_kidx_api(dev)) {
981 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
982 NULL, B43_SEC_KEYSIZE, NULL);
984 dev->key[index].keyconf = NULL;
989 static void b43_clear_keys(struct b43_wldev *dev)
993 for (i = 0; i < dev->max_nr_keys; i++)
994 b43_key_clear(dev, i);
997 void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
1005 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
1006 (ps_flags & B43_PS_DISABLED));
1007 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
1009 if (ps_flags & B43_PS_ENABLED) {
1011 } else if (ps_flags & B43_PS_DISABLED) {
1014 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
1015 // and thus is not an AP and we are associated, set bit 25
1017 if (ps_flags & B43_PS_AWAKE) {
1019 } else if (ps_flags & B43_PS_ASLEEP) {
1022 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
1023 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
1024 // successful, set bit26
1027 /* FIXME: For now we force awake-on and hwps-off */
1031 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1033 macctl |= B43_MACCTL_HWPS;
1035 macctl &= ~B43_MACCTL_HWPS;
1037 macctl |= B43_MACCTL_AWAKE;
1039 macctl &= ~B43_MACCTL_AWAKE;
1040 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1042 b43_read32(dev, B43_MMIO_MACCTL);
1043 if (awake && dev->dev->id.revision >= 5) {
1044 /* Wait for the microcode to wake up. */
1045 for (i = 0; i < 100; i++) {
1046 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
1047 B43_SHM_SH_UCODESTAT);
1048 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
1055 /* Turn the Analog ON/OFF */
1056 static void b43_switch_analog(struct b43_wldev *dev, int on)
1058 switch (dev->phy.type) {
1061 b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
1064 b43_phy_write(dev, B43_NPHY_AFECTL_OVER,
1072 void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
1077 flags |= B43_TMSLOW_PHYCLKEN;
1078 flags |= B43_TMSLOW_PHYRESET;
1079 ssb_device_enable(dev->dev, flags);
1080 msleep(2); /* Wait for the PLL to turn on. */
1082 /* Now take the PHY out of Reset again */
1083 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
1084 tmslow |= SSB_TMSLOW_FGC;
1085 tmslow &= ~B43_TMSLOW_PHYRESET;
1086 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
1087 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
1089 tmslow &= ~SSB_TMSLOW_FGC;
1090 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
1091 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
1094 /* Turn Analog ON */
1095 b43_switch_analog(dev, 1);
1097 macctl = b43_read32(dev, B43_MMIO_MACCTL);
1098 macctl &= ~B43_MACCTL_GMODE;
1099 if (flags & B43_TMSLOW_GMODE)
1100 macctl |= B43_MACCTL_GMODE;
1101 macctl |= B43_MACCTL_IHR_ENABLED;
1102 b43_write32(dev, B43_MMIO_MACCTL, macctl);
1105 static void handle_irq_transmit_status(struct b43_wldev *dev)
1109 struct b43_txstatus stat;
1112 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1113 if (!(v0 & 0x00000001))
1115 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1117 stat.cookie = (v0 >> 16);
1118 stat.seq = (v1 & 0x0000FFFF);
1119 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
1120 tmp = (v0 & 0x0000FFFF);
1121 stat.frame_count = ((tmp & 0xF000) >> 12);
1122 stat.rts_count = ((tmp & 0x0F00) >> 8);
1123 stat.supp_reason = ((tmp & 0x001C) >> 2);
1124 stat.pm_indicated = !!(tmp & 0x0080);
1125 stat.intermediate = !!(tmp & 0x0040);
1126 stat.for_ampdu = !!(tmp & 0x0020);
1127 stat.acked = !!(tmp & 0x0002);
1129 b43_handle_txstatus(dev, &stat);
1133 static void drain_txstatus_queue(struct b43_wldev *dev)
1137 if (dev->dev->id.revision < 5)
1139 /* Read all entries from the microcode TXstatus FIFO
1140 * and throw them away.
1143 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
1144 if (!(dummy & 0x00000001))
1146 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
1150 static u32 b43_jssi_read(struct b43_wldev *dev)
1154 val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
1156 val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
1161 static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1163 b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
1164 b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
1167 static void b43_generate_noise_sample(struct b43_wldev *dev)
1169 b43_jssi_write(dev, 0x7F7F7F7F);
1170 b43_write32(dev, B43_MMIO_MACCMD,
1171 b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
1174 static void b43_calculate_link_quality(struct b43_wldev *dev)
1176 /* Top half of Link Quality calculation. */
1178 if (dev->phy.type != B43_PHYTYPE_G)
1180 if (dev->noisecalc.calculation_running)
1182 dev->noisecalc.calculation_running = 1;
1183 dev->noisecalc.nr_samples = 0;
1185 b43_generate_noise_sample(dev);
1188 static void handle_irq_noise(struct b43_wldev *dev)
1190 struct b43_phy_g *phy = dev->phy.g;
1196 /* Bottom half of Link Quality calculation. */
1198 if (dev->phy.type != B43_PHYTYPE_G)
1201 /* Possible race condition: It might be possible that the user
1202 * changed to a different channel in the meantime since we
1203 * started the calculation. We ignore that fact, since it's
1204 * not really that much of a problem. The background noise is
1205 * an estimation only anyway. Slightly wrong results will get damped
1206 * by the averaging of the 8 sample rounds. Additionally the
1207 * value is shortlived. So it will be replaced by the next noise
1208 * calculation round soon. */
1210 B43_WARN_ON(!dev->noisecalc.calculation_running);
1211 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
1212 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1213 noise[2] == 0x7F || noise[3] == 0x7F)
1216 /* Get the noise samples. */
1217 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1218 i = dev->noisecalc.nr_samples;
1219 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1220 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1221 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1222 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1223 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1224 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1225 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1226 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1227 dev->noisecalc.nr_samples++;
1228 if (dev->noisecalc.nr_samples == 8) {
1229 /* Calculate the Link Quality by the noise samples. */
1231 for (i = 0; i < 8; i++) {
1232 for (j = 0; j < 4; j++)
1233 average += dev->noisecalc.samples[i][j];
1239 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1240 tmp = (tmp / 128) & 0x1F;
1250 dev->stats.link_noise = average;
1251 dev->noisecalc.calculation_running = 0;
1255 b43_generate_noise_sample(dev);
1258 static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1260 if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
1263 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1264 b43_power_saving_ctl_bits(dev, 0);
1266 if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
1270 static void handle_irq_atim_end(struct b43_wldev *dev)
1272 if (dev->dfq_valid) {
1273 b43_write32(dev, B43_MMIO_MACCMD,
1274 b43_read32(dev, B43_MMIO_MACCMD)
1275 | B43_MACCMD_DFQ_VALID);
1280 static void handle_irq_pmq(struct b43_wldev *dev)
1287 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1288 if (!(tmp & 0x00000008))
1291 /* 16bit write is odd, but correct. */
1292 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1295 static void b43_write_template_common(struct b43_wldev *dev,
1296 const u8 * data, u16 size,
1298 u16 shm_size_offset, u8 rate)
1301 struct b43_plcp_hdr4 plcp;
1304 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1305 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1306 ram_offset += sizeof(u32);
1307 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1308 * So leave the first two bytes of the next write blank.
1310 tmp = (u32) (data[0]) << 16;
1311 tmp |= (u32) (data[1]) << 24;
1312 b43_ram_write(dev, ram_offset, tmp);
1313 ram_offset += sizeof(u32);
1314 for (i = 2; i < size; i += sizeof(u32)) {
1315 tmp = (u32) (data[i + 0]);
1317 tmp |= (u32) (data[i + 1]) << 8;
1319 tmp |= (u32) (data[i + 2]) << 16;
1321 tmp |= (u32) (data[i + 3]) << 24;
1322 b43_ram_write(dev, ram_offset + i - 2, tmp);
1324 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1325 size + sizeof(struct b43_plcp_hdr6));
1328 /* Check if the use of the antenna that ieee80211 told us to
1329 * use is possible. This will fall back to DEFAULT.
1330 * "antenna_nr" is the antenna identifier we got from ieee80211. */
1331 u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
1336 if (antenna_nr == 0) {
1337 /* Zero means "use default antenna". That's always OK. */
1341 /* Get the mask of available antennas. */
1343 antenna_mask = dev->dev->bus->sprom.ant_available_bg;
1345 antenna_mask = dev->dev->bus->sprom.ant_available_a;
1347 if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
1348 /* This antenna is not available. Fall back to default. */
1355 static int b43_antenna_from_ieee80211(struct b43_wldev *dev, u8 antenna)
1357 antenna = b43_ieee80211_antenna_sanitize(dev, antenna);
1359 case 0: /* default/diversity */
1360 return B43_ANTENNA_DEFAULT;
1361 case 1: /* Antenna 0 */
1362 return B43_ANTENNA0;
1363 case 2: /* Antenna 1 */
1364 return B43_ANTENNA1;
1365 case 3: /* Antenna 2 */
1366 return B43_ANTENNA2;
1367 case 4: /* Antenna 3 */
1368 return B43_ANTENNA3;
1370 return B43_ANTENNA_DEFAULT;
1374 /* Convert a b43 antenna number value to the PHY TX control value. */
1375 static u16 b43_antenna_to_phyctl(int antenna)
1379 return B43_TXH_PHY_ANT0;
1381 return B43_TXH_PHY_ANT1;
1383 return B43_TXH_PHY_ANT2;
1385 return B43_TXH_PHY_ANT3;
1386 case B43_ANTENNA_AUTO:
1387 return B43_TXH_PHY_ANT01AUTO;
1393 static void b43_write_beacon_template(struct b43_wldev *dev,
1395 u16 shm_size_offset)
1397 unsigned int i, len, variable_len;
1398 const struct ieee80211_mgmt *bcn;
1404 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
1406 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
1407 len = min((size_t) dev->wl->current_beacon->len,
1408 0x200 - sizeof(struct b43_plcp_hdr6));
1409 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
1411 b43_write_template_common(dev, (const u8 *)bcn,
1412 len, ram_offset, shm_size_offset, rate);
1414 /* Write the PHY TX control parameters. */
1415 antenna = b43_antenna_from_ieee80211(dev, info->antenna_sel_tx);
1416 antenna = b43_antenna_to_phyctl(antenna);
1417 ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
1418 /* We can't send beacons with short preamble. Would get PHY errors. */
1419 ctl &= ~B43_TXH_PHY_SHORTPRMBL;
1420 ctl &= ~B43_TXH_PHY_ANT;
1421 ctl &= ~B43_TXH_PHY_ENC;
1423 if (b43_is_cck_rate(rate))
1424 ctl |= B43_TXH_PHY_ENC_CCK;
1426 ctl |= B43_TXH_PHY_ENC_OFDM;
1427 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
1429 /* Find the position of the TIM and the DTIM_period value
1430 * and write them to SHM. */
1431 ie = bcn->u.beacon.variable;
1432 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
1433 for (i = 0; i < variable_len - 2; ) {
1434 uint8_t ie_id, ie_len;
1441 /* This is the TIM Information Element */
1443 /* Check whether the ie_len is in the beacon data range. */
1444 if (variable_len < ie_len + 2 + i)
1446 /* A valid TIM is at least 4 bytes long. */
1451 tim_position = sizeof(struct b43_plcp_hdr6);
1452 tim_position += offsetof(struct ieee80211_mgmt, u.beacon.variable);
1455 dtim_period = ie[i + 3];
1457 b43_shm_write16(dev, B43_SHM_SHARED,
1458 B43_SHM_SH_TIMBPOS, tim_position);
1459 b43_shm_write16(dev, B43_SHM_SHARED,
1460 B43_SHM_SH_DTIMPER, dtim_period);
1467 * If ucode wants to modify TIM do it behind the beacon, this
1468 * will happen, for example, when doing mesh networking.
1470 b43_shm_write16(dev, B43_SHM_SHARED,
1472 len + sizeof(struct b43_plcp_hdr6));
1473 b43_shm_write16(dev, B43_SHM_SHARED,
1474 B43_SHM_SH_DTIMPER, 0);
1476 b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
1479 static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
1480 u16 shm_offset, u16 size,
1481 struct ieee80211_rate *rate)
1483 struct b43_plcp_hdr4 plcp;
1488 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
1489 dur = ieee80211_generic_frame_duration(dev->wl->hw,
1492 /* Write PLCP in two parts and timing for packet transfer */
1493 tmp = le32_to_cpu(plcp.data);
1494 b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
1495 b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
1496 b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
1499 /* Instead of using custom probe response template, this function
1500 * just patches custom beacon template by:
1501 * 1) Changing packet type
1502 * 2) Patching duration field
1505 static const u8 * b43_generate_probe_resp(struct b43_wldev *dev,
1507 struct ieee80211_rate *rate)
1511 u16 src_size, elem_size, src_pos, dest_pos;
1513 struct ieee80211_hdr *hdr;
1516 src_size = dev->wl->current_beacon->len;
1517 src_data = (const u8 *)dev->wl->current_beacon->data;
1519 /* Get the start offset of the variable IEs in the packet. */
1520 ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
1521 B43_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt, u.beacon.variable));
1523 if (B43_WARN_ON(src_size < ie_start))
1526 dest_data = kmalloc(src_size, GFP_ATOMIC);
1527 if (unlikely(!dest_data))
1530 /* Copy the static data and all Information Elements, except the TIM. */
1531 memcpy(dest_data, src_data, ie_start);
1533 dest_pos = ie_start;
1534 for ( ; src_pos < src_size - 2; src_pos += elem_size) {
1535 elem_size = src_data[src_pos + 1] + 2;
1536 if (src_data[src_pos] == 5) {
1537 /* This is the TIM. */
1540 memcpy(dest_data + dest_pos, src_data + src_pos,
1542 dest_pos += elem_size;
1544 *dest_size = dest_pos;
1545 hdr = (struct ieee80211_hdr *)dest_data;
1547 /* Set the frame control. */
1548 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1549 IEEE80211_STYPE_PROBE_RESP);
1550 dur = ieee80211_generic_frame_duration(dev->wl->hw,
1551 dev->wl->vif, *dest_size,
1553 hdr->duration_id = dur;
1558 static void b43_write_probe_resp_template(struct b43_wldev *dev,
1560 u16 shm_size_offset,
1561 struct ieee80211_rate *rate)
1563 const u8 *probe_resp_data;
1566 size = dev->wl->current_beacon->len;
1567 probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
1568 if (unlikely(!probe_resp_data))
1571 /* Looks like PLCP headers plus packet timings are stored for
1572 * all possible basic rates
1574 b43_write_probe_resp_plcp(dev, 0x31A, size, &b43_b_ratetable[0]);
1575 b43_write_probe_resp_plcp(dev, 0x32C, size, &b43_b_ratetable[1]);
1576 b43_write_probe_resp_plcp(dev, 0x33E, size, &b43_b_ratetable[2]);
1577 b43_write_probe_resp_plcp(dev, 0x350, size, &b43_b_ratetable[3]);
1579 size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
1580 b43_write_template_common(dev, probe_resp_data,
1581 size, ram_offset, shm_size_offset,
1583 kfree(probe_resp_data);
1586 static void b43_upload_beacon0(struct b43_wldev *dev)
1588 struct b43_wl *wl = dev->wl;
1590 if (wl->beacon0_uploaded)
1592 b43_write_beacon_template(dev, 0x68, 0x18);
1593 /* FIXME: Probe resp upload doesn't really belong here,
1594 * but we don't use that feature anyway. */
1595 b43_write_probe_resp_template(dev, 0x268, 0x4A,
1596 &__b43_ratetable[3]);
1597 wl->beacon0_uploaded = 1;
1600 static void b43_upload_beacon1(struct b43_wldev *dev)
1602 struct b43_wl *wl = dev->wl;
1604 if (wl->beacon1_uploaded)
1606 b43_write_beacon_template(dev, 0x468, 0x1A);
1607 wl->beacon1_uploaded = 1;
1610 static void handle_irq_beacon(struct b43_wldev *dev)
1612 struct b43_wl *wl = dev->wl;
1613 u32 cmd, beacon0_valid, beacon1_valid;
1615 if (!b43_is_mode(wl, IEEE80211_IF_TYPE_AP) &&
1616 !b43_is_mode(wl, IEEE80211_IF_TYPE_MESH_POINT))
1619 /* This is the bottom half of the asynchronous beacon update. */
1621 /* Ignore interrupt in the future. */
1622 dev->irq_savedstate &= ~B43_IRQ_BEACON;
1624 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1625 beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
1626 beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
1628 /* Schedule interrupt manually, if busy. */
1629 if (beacon0_valid && beacon1_valid) {
1630 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
1631 dev->irq_savedstate |= B43_IRQ_BEACON;
1635 if (unlikely(wl->beacon_templates_virgin)) {
1636 /* We never uploaded a beacon before.
1637 * Upload both templates now, but only mark one valid. */
1638 wl->beacon_templates_virgin = 0;
1639 b43_upload_beacon0(dev);
1640 b43_upload_beacon1(dev);
1641 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1642 cmd |= B43_MACCMD_BEACON0_VALID;
1643 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1645 if (!beacon0_valid) {
1646 b43_upload_beacon0(dev);
1647 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1648 cmd |= B43_MACCMD_BEACON0_VALID;
1649 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1650 } else if (!beacon1_valid) {
1651 b43_upload_beacon1(dev);
1652 cmd = b43_read32(dev, B43_MMIO_MACCMD);
1653 cmd |= B43_MACCMD_BEACON1_VALID;
1654 b43_write32(dev, B43_MMIO_MACCMD, cmd);
1659 static void b43_beacon_update_trigger_work(struct work_struct *work)
1661 struct b43_wl *wl = container_of(work, struct b43_wl,
1662 beacon_update_trigger);
1663 struct b43_wldev *dev;
1665 mutex_lock(&wl->mutex);
1666 dev = wl->current_dev;
1667 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
1668 spin_lock_irq(&wl->irq_lock);
1669 /* update beacon right away or defer to irq */
1670 dev->irq_savedstate = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
1671 handle_irq_beacon(dev);
1672 /* The handler might have updated the IRQ mask. */
1673 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK,
1674 dev->irq_savedstate);
1676 spin_unlock_irq(&wl->irq_lock);
1678 mutex_unlock(&wl->mutex);
1681 /* Asynchronously update the packet templates in template RAM.
1682 * Locking: Requires wl->irq_lock to be locked. */
1683 static void b43_update_templates(struct b43_wl *wl)
1685 struct sk_buff *beacon;
1687 /* This is the top half of the ansynchronous beacon update.
1688 * The bottom half is the beacon IRQ.
1689 * Beacon update must be asynchronous to avoid sending an
1690 * invalid beacon. This can happen for example, if the firmware
1691 * transmits a beacon while we are updating it. */
1693 /* We could modify the existing beacon and set the aid bit in
1694 * the TIM field, but that would probably require resizing and
1695 * moving of data within the beacon template.
1696 * Simply request a new beacon and let mac80211 do the hard work. */
1697 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1698 if (unlikely(!beacon))
1701 if (wl->current_beacon)
1702 dev_kfree_skb_any(wl->current_beacon);
1703 wl->current_beacon = beacon;
1704 wl->beacon0_uploaded = 0;
1705 wl->beacon1_uploaded = 0;
1706 queue_work(wl->hw->workqueue, &wl->beacon_update_trigger);
1709 static void b43_set_ssid(struct b43_wldev *dev, const u8 * ssid, u8 ssid_len)
1714 len = min((u16) ssid_len, (u16) 0x100);
1715 for (i = 0; i < len; i += sizeof(u32)) {
1716 tmp = (u32) (ssid[i + 0]);
1718 tmp |= (u32) (ssid[i + 1]) << 8;
1720 tmp |= (u32) (ssid[i + 2]) << 16;
1722 tmp |= (u32) (ssid[i + 3]) << 24;
1723 b43_shm_write32(dev, B43_SHM_SHARED, 0x380 + i, tmp);
1725 b43_shm_write16(dev, B43_SHM_SHARED, 0x48, len);
1728 static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1731 if (dev->dev->id.revision >= 3) {
1732 b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
1733 b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
1735 b43_write16(dev, 0x606, (beacon_int >> 6));
1736 b43_write16(dev, 0x610, beacon_int);
1738 b43_time_unlock(dev);
1739 b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
1742 static void b43_handle_firmware_panic(struct b43_wldev *dev)
1746 /* Read the register that contains the reason code for the panic. */
1747 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
1748 b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
1752 b43dbg(dev->wl, "The panic reason is unknown.\n");
1754 case B43_FWPANIC_DIE:
1755 /* Do not restart the controller or firmware.
1756 * The device is nonfunctional from now on.
1757 * Restarting would result in this panic to trigger again,
1758 * so we avoid that recursion. */
1760 case B43_FWPANIC_RESTART:
1761 b43_controller_restart(dev, "Microcode panic");
1766 static void handle_irq_ucode_debug(struct b43_wldev *dev)
1768 unsigned int i, cnt;
1769 u16 reason, marker_id, marker_line;
1772 /* The proprietary firmware doesn't have this IRQ. */
1773 if (!dev->fw.opensource)
1776 /* Read the register that contains the reason code for this IRQ. */
1777 reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
1780 case B43_DEBUGIRQ_PANIC:
1781 b43_handle_firmware_panic(dev);
1783 case B43_DEBUGIRQ_DUMP_SHM:
1785 break; /* Only with driver debugging enabled. */
1786 buf = kmalloc(4096, GFP_ATOMIC);
1788 b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
1791 for (i = 0; i < 4096; i += 2) {
1792 u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
1793 buf[i / 2] = cpu_to_le16(tmp);
1795 b43info(dev->wl, "Shared memory dump:\n");
1796 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
1797 16, 2, buf, 4096, 1);
1800 case B43_DEBUGIRQ_DUMP_REGS:
1802 break; /* Only with driver debugging enabled. */
1803 b43info(dev->wl, "Microcode register dump:\n");
1804 for (i = 0, cnt = 0; i < 64; i++) {
1805 u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
1808 printk("r%02u: 0x%04X ", i, tmp);
1817 case B43_DEBUGIRQ_MARKER:
1819 break; /* Only with driver debugging enabled. */
1820 marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
1822 marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
1823 B43_MARKER_LINE_REG);
1824 b43info(dev->wl, "The firmware just executed the MARKER(%u) "
1825 "at line number %u\n",
1826 marker_id, marker_line);
1829 b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
1833 /* Acknowledge the debug-IRQ, so the firmware can continue. */
1834 b43_shm_write16(dev, B43_SHM_SCRATCH,
1835 B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
1838 /* Interrupt handler bottom-half */
1839 static void b43_interrupt_tasklet(struct b43_wldev *dev)
1842 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1843 u32 merged_dma_reason = 0;
1845 unsigned long flags;
1847 spin_lock_irqsave(&dev->wl->irq_lock, flags);
1849 B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
1851 reason = dev->irq_reason;
1852 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1853 dma_reason[i] = dev->dma_reason[i];
1854 merged_dma_reason |= dma_reason[i];
1857 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1858 b43err(dev->wl, "MAC transmission error\n");
1860 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
1861 b43err(dev->wl, "PHY transmission error\n");
1863 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1864 atomic_set(&dev->phy.txerr_cnt,
1865 B43_PHY_TX_BADNESS_LIMIT);
1866 b43err(dev->wl, "Too many PHY TX errors, "
1867 "restarting the controller\n");
1868 b43_controller_restart(dev, "PHY TX errors");
1872 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
1873 B43_DMAIRQ_NONFATALMASK))) {
1874 if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
1875 b43err(dev->wl, "Fatal DMA error: "
1876 "0x%08X, 0x%08X, 0x%08X, "
1877 "0x%08X, 0x%08X, 0x%08X\n",
1878 dma_reason[0], dma_reason[1],
1879 dma_reason[2], dma_reason[3],
1880 dma_reason[4], dma_reason[5]);
1881 b43_controller_restart(dev, "DMA error");
1883 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1886 if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
1887 b43err(dev->wl, "DMA error: "
1888 "0x%08X, 0x%08X, 0x%08X, "
1889 "0x%08X, 0x%08X, 0x%08X\n",
1890 dma_reason[0], dma_reason[1],
1891 dma_reason[2], dma_reason[3],
1892 dma_reason[4], dma_reason[5]);
1896 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1897 handle_irq_ucode_debug(dev);
1898 if (reason & B43_IRQ_TBTT_INDI)
1899 handle_irq_tbtt_indication(dev);
1900 if (reason & B43_IRQ_ATIM_END)
1901 handle_irq_atim_end(dev);
1902 if (reason & B43_IRQ_BEACON)
1903 handle_irq_beacon(dev);
1904 if (reason & B43_IRQ_PMQ)
1905 handle_irq_pmq(dev);
1906 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1908 if (reason & B43_IRQ_NOISESAMPLE_OK)
1909 handle_irq_noise(dev);
1911 /* Check the DMA reason registers for received data. */
1912 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1913 if (b43_using_pio_transfers(dev))
1914 b43_pio_rx(dev->pio.rx_queue);
1916 b43_dma_rx(dev->dma.rx_ring);
1918 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
1919 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
1920 B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
1921 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
1922 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
1924 if (reason & B43_IRQ_TX_OK)
1925 handle_irq_transmit_status(dev);
1927 b43_interrupt_enable(dev, dev->irq_savedstate);
1929 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1932 static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
1934 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
1936 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
1937 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
1938 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
1939 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
1940 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
1941 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
1944 /* Interrupt handler top-half */
1945 static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
1947 irqreturn_t ret = IRQ_NONE;
1948 struct b43_wldev *dev = dev_id;
1954 spin_lock(&dev->wl->irq_lock);
1956 if (b43_status(dev) < B43_STAT_STARTED)
1958 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1959 if (reason == 0xffffffff) /* shared IRQ */
1962 reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
1966 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
1968 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
1970 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
1972 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
1974 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
1976 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
1979 b43_interrupt_ack(dev, reason);
1980 /* disable all IRQs. They are enabled again in the bottom half. */
1981 dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
1982 /* save the reason code and call our bottom half. */
1983 dev->irq_reason = reason;
1984 tasklet_schedule(&dev->isr_tasklet);
1987 spin_unlock(&dev->wl->irq_lock);
1992 static void do_release_fw(struct b43_firmware_file *fw)
1994 release_firmware(fw->data);
1996 fw->filename = NULL;
1999 static void b43_release_firmware(struct b43_wldev *dev)
2001 do_release_fw(&dev->fw.ucode);
2002 do_release_fw(&dev->fw.pcm);
2003 do_release_fw(&dev->fw.initvals);
2004 do_release_fw(&dev->fw.initvals_band);
2007 static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
2011 text = "You must go to "
2012 "http://linuxwireless.org/en/users/Drivers/b43#devicefirmware "
2013 "and download the latest firmware (version 4).\n";
2020 static int do_request_fw(struct b43_wldev *dev,
2022 struct b43_firmware_file *fw,
2025 char path[sizeof(modparam_fwpostfix) + 32];
2026 const struct firmware *blob;
2027 struct b43_fw_header *hdr;
2032 /* Don't fetch anything. Free possibly cached firmware. */
2037 if (strcmp(fw->filename, name) == 0)
2038 return 0; /* Already have this fw. */
2039 /* Free the cached firmware first. */
2043 snprintf(path, ARRAY_SIZE(path),
2045 modparam_fwpostfix, name);
2046 err = request_firmware(&blob, path, dev->dev->dev);
2047 if (err == -ENOENT) {
2049 b43err(dev->wl, "Firmware file \"%s\" not found\n",
2054 b43err(dev->wl, "Firmware file \"%s\" request failed (err=%d)\n",
2058 if (blob->size < sizeof(struct b43_fw_header))
2060 hdr = (struct b43_fw_header *)(blob->data);
2061 switch (hdr->type) {
2062 case B43_FW_TYPE_UCODE:
2063 case B43_FW_TYPE_PCM:
2064 size = be32_to_cpu(hdr->size);
2065 if (size != blob->size - sizeof(struct b43_fw_header))
2068 case B43_FW_TYPE_IV:
2077 fw->filename = name;
2082 b43err(dev->wl, "Firmware file \"%s\" format error.\n", path);
2083 release_firmware(blob);
2088 static int b43_request_firmware(struct b43_wldev *dev)
2090 struct b43_firmware *fw = &dev->fw;
2091 const u8 rev = dev->dev->id.revision;
2092 const char *filename;
2097 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
2098 if ((rev >= 5) && (rev <= 10))
2099 filename = "ucode5";
2100 else if ((rev >= 11) && (rev <= 12))
2101 filename = "ucode11";
2103 filename = "ucode13";
2106 err = do_request_fw(dev, filename, &fw->ucode, 0);
2111 if ((rev >= 5) && (rev <= 10))
2117 fw->pcm_request_failed = 0;
2118 err = do_request_fw(dev, filename, &fw->pcm, 1);
2119 if (err == -ENOENT) {
2120 /* We did not find a PCM file? Not fatal, but
2121 * core rev <= 10 must do without hwcrypto then. */
2122 fw->pcm_request_failed = 1;
2127 switch (dev->phy.type) {
2129 if ((rev >= 5) && (rev <= 10)) {
2130 if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
2131 filename = "a0g1initvals5";
2133 filename = "a0g0initvals5";
2135 goto err_no_initvals;
2138 if ((rev >= 5) && (rev <= 10))
2139 filename = "b0g0initvals5";
2141 filename = "b0g0initvals13";
2143 goto err_no_initvals;
2146 if ((rev >= 11) && (rev <= 12))
2147 filename = "n0initvals11";
2149 goto err_no_initvals;
2152 goto err_no_initvals;
2154 err = do_request_fw(dev, filename, &fw->initvals, 0);
2158 /* Get bandswitch initvals */
2159 switch (dev->phy.type) {
2161 if ((rev >= 5) && (rev <= 10)) {
2162 if (tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY)
2163 filename = "a0g1bsinitvals5";
2165 filename = "a0g0bsinitvals5";
2166 } else if (rev >= 11)
2169 goto err_no_initvals;
2172 if ((rev >= 5) && (rev <= 10))
2173 filename = "b0g0bsinitvals5";
2177 goto err_no_initvals;
2180 if ((rev >= 11) && (rev <= 12))
2181 filename = "n0bsinitvals11";
2183 goto err_no_initvals;
2186 goto err_no_initvals;
2188 err = do_request_fw(dev, filename, &fw->initvals_band, 0);
2195 b43_print_fw_helptext(dev->wl, 1);
2200 b43err(dev->wl, "No microcode available for core rev %u\n", rev);
2205 b43err(dev->wl, "No PCM available for core rev %u\n", rev);
2210 b43err(dev->wl, "No Initial Values firmware file for PHY %u, "
2211 "core rev %u\n", dev->phy.type, rev);
2215 b43_release_firmware(dev);
2219 static int b43_upload_microcode(struct b43_wldev *dev)
2221 const size_t hdr_len = sizeof(struct b43_fw_header);
2223 unsigned int i, len;
2224 u16 fwrev, fwpatch, fwdate, fwtime;
2228 /* Jump the microcode PSM to offset 0 */
2229 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2230 B43_WARN_ON(macctl & B43_MACCTL_PSM_RUN);
2231 macctl |= B43_MACCTL_PSM_JMP0;
2232 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2233 /* Zero out all microcode PSM registers and shared memory. */
2234 for (i = 0; i < 64; i++)
2235 b43_shm_write16(dev, B43_SHM_SCRATCH, i, 0);
2236 for (i = 0; i < 4096; i += 2)
2237 b43_shm_write16(dev, B43_SHM_SHARED, i, 0);
2239 /* Upload Microcode. */
2240 data = (__be32 *) (dev->fw.ucode.data->data + hdr_len);
2241 len = (dev->fw.ucode.data->size - hdr_len) / sizeof(__be32);
2242 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
2243 for (i = 0; i < len; i++) {
2244 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2248 if (dev->fw.pcm.data) {
2249 /* Upload PCM data. */
2250 data = (__be32 *) (dev->fw.pcm.data->data + hdr_len);
2251 len = (dev->fw.pcm.data->size - hdr_len) / sizeof(__be32);
2252 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
2253 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
2254 /* No need for autoinc bit in SHM_HW */
2255 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
2256 for (i = 0; i < len; i++) {
2257 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
2262 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
2264 /* Start the microcode PSM */
2265 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2266 macctl &= ~B43_MACCTL_PSM_JMP0;
2267 macctl |= B43_MACCTL_PSM_RUN;
2268 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2270 /* Wait for the microcode to load and respond */
2273 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2274 if (tmp == B43_IRQ_MAC_SUSPENDED)
2278 b43err(dev->wl, "Microcode not responding\n");
2279 b43_print_fw_helptext(dev->wl, 1);
2283 msleep_interruptible(50);
2284 if (signal_pending(current)) {
2289 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
2291 /* Get and check the revisions. */
2292 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
2293 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
2294 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
2295 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
2297 if (fwrev <= 0x128) {
2298 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
2299 "binary drivers older than version 4.x is unsupported. "
2300 "You must upgrade your firmware files.\n");
2301 b43_print_fw_helptext(dev->wl, 1);
2305 dev->fw.rev = fwrev;
2306 dev->fw.patch = fwpatch;
2307 dev->fw.opensource = (fwdate == 0xFFFF);
2309 if (dev->fw.opensource) {
2310 /* Patchlevel info is encoded in the "time" field. */
2311 dev->fw.patch = fwtime;
2312 b43info(dev->wl, "Loading OpenSource firmware version %u.%u%s\n",
2313 dev->fw.rev, dev->fw.patch,
2314 dev->fw.pcm_request_failed ? " (Hardware crypto not supported)" : "");
2316 b43info(dev->wl, "Loading firmware version %u.%u "
2317 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
2319 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
2320 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
2321 if (dev->fw.pcm_request_failed) {
2322 b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
2323 "Hardware accelerated cryptography is disabled.\n");
2324 b43_print_fw_helptext(dev->wl, 0);
2328 if (b43_is_old_txhdr_format(dev)) {
2329 b43warn(dev->wl, "You are using an old firmware image. "
2330 "Support for old firmware will be removed in July 2008.\n");
2331 b43_print_fw_helptext(dev->wl, 0);
2337 macctl = b43_read32(dev, B43_MMIO_MACCTL);
2338 macctl &= ~B43_MACCTL_PSM_RUN;
2339 macctl |= B43_MACCTL_PSM_JMP0;
2340 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2345 static int b43_write_initvals(struct b43_wldev *dev,
2346 const struct b43_iv *ivals,
2350 const struct b43_iv *iv;
2355 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
2357 for (i = 0; i < count; i++) {
2358 if (array_size < sizeof(iv->offset_size))
2360 array_size -= sizeof(iv->offset_size);
2361 offset = be16_to_cpu(iv->offset_size);
2362 bit32 = !!(offset & B43_IV_32BIT);
2363 offset &= B43_IV_OFFSET_MASK;
2364 if (offset >= 0x1000)
2369 if (array_size < sizeof(iv->data.d32))
2371 array_size -= sizeof(iv->data.d32);
2373 value = get_unaligned_be32(&iv->data.d32);
2374 b43_write32(dev, offset, value);
2376 iv = (const struct b43_iv *)((const uint8_t *)iv +
2382 if (array_size < sizeof(iv->data.d16))
2384 array_size -= sizeof(iv->data.d16);
2386 value = be16_to_cpu(iv->data.d16);
2387 b43_write16(dev, offset, value);
2389 iv = (const struct b43_iv *)((const uint8_t *)iv +
2400 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
2401 b43_print_fw_helptext(dev->wl, 1);
2406 static int b43_upload_initvals(struct b43_wldev *dev)
2408 const size_t hdr_len = sizeof(struct b43_fw_header);
2409 const struct b43_fw_header *hdr;
2410 struct b43_firmware *fw = &dev->fw;
2411 const struct b43_iv *ivals;
2415 hdr = (const struct b43_fw_header *)(fw->initvals.data->data);
2416 ivals = (const struct b43_iv *)(fw->initvals.data->data + hdr_len);
2417 count = be32_to_cpu(hdr->size);
2418 err = b43_write_initvals(dev, ivals, count,
2419 fw->initvals.data->size - hdr_len);
2422 if (fw->initvals_band.data) {
2423 hdr = (const struct b43_fw_header *)(fw->initvals_band.data->data);
2424 ivals = (const struct b43_iv *)(fw->initvals_band.data->data + hdr_len);
2425 count = be32_to_cpu(hdr->size);
2426 err = b43_write_initvals(dev, ivals, count,
2427 fw->initvals_band.data->size - hdr_len);
2436 /* Initialize the GPIOs
2437 * http://bcm-specs.sipsolutions.net/GPIO
2439 static int b43_gpio_init(struct b43_wldev *dev)
2441 struct ssb_bus *bus = dev->dev->bus;
2442 struct ssb_device *gpiodev, *pcidev = NULL;
2445 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2446 & ~B43_MACCTL_GPOUTSMSK);
2448 b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
2453 if (dev->dev->bus->chip_id == 0x4301) {
2457 if (0 /* FIXME: conditional unknown */ ) {
2458 b43_write16(dev, B43_MMIO_GPIO_MASK,
2459 b43_read16(dev, B43_MMIO_GPIO_MASK)
2464 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
2465 b43_write16(dev, B43_MMIO_GPIO_MASK,
2466 b43_read16(dev, B43_MMIO_GPIO_MASK)
2471 if (dev->dev->id.revision >= 2)
2472 mask |= 0x0010; /* FIXME: This is redundant. */
2474 #ifdef CONFIG_SSB_DRIVER_PCICORE
2475 pcidev = bus->pcicore.dev;
2477 gpiodev = bus->chipco.dev ? : pcidev;
2480 ssb_write32(gpiodev, B43_GPIO_CONTROL,
2481 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
2487 /* Turn off all GPIO stuff. Call this on module unload, for example. */
2488 static void b43_gpio_cleanup(struct b43_wldev *dev)
2490 struct ssb_bus *bus = dev->dev->bus;
2491 struct ssb_device *gpiodev, *pcidev = NULL;
2493 #ifdef CONFIG_SSB_DRIVER_PCICORE
2494 pcidev = bus->pcicore.dev;
2496 gpiodev = bus->chipco.dev ? : pcidev;
2499 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
2502 /* http://bcm-specs.sipsolutions.net/EnableMac */
2503 void b43_mac_enable(struct b43_wldev *dev)
2505 if (b43_debug(dev, B43_DBG_FIRMWARE)) {
2508 fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
2509 B43_SHM_SH_UCODESTAT);
2510 if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
2511 (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
2512 b43err(dev->wl, "b43_mac_enable(): The firmware "
2513 "should be suspended, but current state is %u\n",
2518 dev->mac_suspended--;
2519 B43_WARN_ON(dev->mac_suspended < 0);
2520 if (dev->mac_suspended == 0) {
2521 b43_write32(dev, B43_MMIO_MACCTL,
2522 b43_read32(dev, B43_MMIO_MACCTL)
2523 | B43_MACCTL_ENABLED);
2524 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
2525 B43_IRQ_MAC_SUSPENDED);
2527 b43_read32(dev, B43_MMIO_MACCTL);
2528 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2529 b43_power_saving_ctl_bits(dev, 0);
2533 /* http://bcm-specs.sipsolutions.net/SuspendMAC */
2534 void b43_mac_suspend(struct b43_wldev *dev)
2540 B43_WARN_ON(dev->mac_suspended < 0);
2542 if (dev->mac_suspended == 0) {
2543 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
2544 b43_write32(dev, B43_MMIO_MACCTL,
2545 b43_read32(dev, B43_MMIO_MACCTL)
2546 & ~B43_MACCTL_ENABLED);
2547 /* force pci to flush the write */
2548 b43_read32(dev, B43_MMIO_MACCTL);
2549 for (i = 35; i; i--) {
2550 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2551 if (tmp & B43_IRQ_MAC_SUSPENDED)
2555 /* Hm, it seems this will take some time. Use msleep(). */
2556 for (i = 40; i; i--) {
2557 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2558 if (tmp & B43_IRQ_MAC_SUSPENDED)
2562 b43err(dev->wl, "MAC suspend failed\n");
2565 dev->mac_suspended++;
2568 static void b43_adjust_opmode(struct b43_wldev *dev)
2570 struct b43_wl *wl = dev->wl;
2574 ctl = b43_read32(dev, B43_MMIO_MACCTL);
2575 /* Reset status to STA infrastructure mode. */
2576 ctl &= ~B43_MACCTL_AP;
2577 ctl &= ~B43_MACCTL_KEEP_CTL;
2578 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
2579 ctl &= ~B43_MACCTL_KEEP_BAD;
2580 ctl &= ~B43_MACCTL_PROMISC;
2581 ctl &= ~B43_MACCTL_BEACPROMISC;
2582 ctl |= B43_MACCTL_INFRA;
2584 if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP) ||
2585 b43_is_mode(wl, IEEE80211_IF_TYPE_MESH_POINT))
2586 ctl |= B43_MACCTL_AP;
2587 else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
2588 ctl &= ~B43_MACCTL_INFRA;
2590 if (wl->filter_flags & FIF_CONTROL)
2591 ctl |= B43_MACCTL_KEEP_CTL;
2592 if (wl->filter_flags & FIF_FCSFAIL)
2593 ctl |= B43_MACCTL_KEEP_BAD;
2594 if (wl->filter_flags & FIF_PLCPFAIL)
2595 ctl |= B43_MACCTL_KEEP_BADPLCP;
2596 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
2597 ctl |= B43_MACCTL_PROMISC;
2598 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2599 ctl |= B43_MACCTL_BEACPROMISC;
2601 /* Workaround: On old hardware the HW-MAC-address-filter
2602 * doesn't work properly, so always run promisc in filter
2603 * it in software. */
2604 if (dev->dev->id.revision <= 4)
2605 ctl |= B43_MACCTL_PROMISC;
2607 b43_write32(dev, B43_MMIO_MACCTL, ctl);
2610 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
2611 if (dev->dev->bus->chip_id == 0x4306 &&
2612 dev->dev->bus->chip_rev == 3)
2617 b43_write16(dev, 0x612, cfp_pretbtt);
2620 static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
2626 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
2629 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
2631 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
2632 b43_shm_read16(dev, B43_SHM_SHARED, offset));
2635 static void b43_rate_memory_init(struct b43_wldev *dev)
2637 switch (dev->phy.type) {
2641 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
2642 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
2643 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
2644 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
2645 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
2646 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
2647 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
2648 if (dev->phy.type == B43_PHYTYPE_A)
2652 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
2653 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
2654 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
2655 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
2662 /* Set the default values for the PHY TX Control Words. */
2663 static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
2667 ctl |= B43_TXH_PHY_ENC_CCK;
2668 ctl |= B43_TXH_PHY_ANT01AUTO;
2669 ctl |= B43_TXH_PHY_TXPWR;
2671 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
2672 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
2673 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
2676 /* Set the TX-Antenna for management frames sent by firmware. */
2677 static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
2682 ant = b43_antenna_to_phyctl(antenna);
2685 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
2686 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
2687 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
2688 /* For Probe Resposes */
2689 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
2690 tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
2691 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
2694 /* This is the opposite of b43_chip_init() */
2695 static void b43_chip_exit(struct b43_wldev *dev)
2697 b43_gpio_cleanup(dev);
2698 /* firmware is released later */
2701 /* Initialize the chip
2702 * http://bcm-specs.sipsolutions.net/ChipInit
2704 static int b43_chip_init(struct b43_wldev *dev)
2706 struct b43_phy *phy = &dev->phy;
2708 u32 value32, macctl;
2711 /* Initialize the MAC control */
2712 macctl = B43_MACCTL_IHR_ENABLED | B43_MACCTL_SHM_ENABLED;
2714 macctl |= B43_MACCTL_GMODE;
2715 macctl |= B43_MACCTL_INFRA;
2716 b43_write32(dev, B43_MMIO_MACCTL, macctl);
2718 err = b43_request_firmware(dev);
2721 err = b43_upload_microcode(dev);
2723 goto out; /* firmware is released later */
2725 err = b43_gpio_init(dev);
2727 goto out; /* firmware is released later */
2729 err = b43_upload_initvals(dev);
2731 goto err_gpio_clean;
2733 b43_write16(dev, 0x03E6, 0x0000);
2734 err = b43_phy_init(dev);
2736 goto err_gpio_clean;
2738 /* Disable Interference Mitigation. */
2739 if (phy->ops->interf_mitigation)
2740 phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
2742 /* Select the antennae */
2743 if (phy->ops->set_rx_antenna)
2744 phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
2745 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
2747 if (phy->type == B43_PHYTYPE_B) {
2748 value16 = b43_read16(dev, 0x005E);
2750 b43_write16(dev, 0x005E, value16);
2752 b43_write32(dev, 0x0100, 0x01000000);
2753 if (dev->dev->id.revision < 5)
2754 b43_write32(dev, 0x010C, 0x01000000);
2756 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2757 & ~B43_MACCTL_INFRA);
2758 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2759 | B43_MACCTL_INFRA);
2761 /* Probe Response Timeout value */
2762 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2763 b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
2765 /* Initially set the wireless operation mode. */
2766 b43_adjust_opmode(dev);
2768 if (dev->dev->id.revision < 3) {
2769 b43_write16(dev, 0x060E, 0x0000);
2770 b43_write16(dev, 0x0610, 0x8000);
2771 b43_write16(dev, 0x0604, 0x0000);
2772 b43_write16(dev, 0x0606, 0x0200);
2774 b43_write32(dev, 0x0188, 0x80000000);
2775 b43_write32(dev, 0x018C, 0x02000000);
2777 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
2778 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2779 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2780 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2781 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2782 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2783 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2785 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
2786 value32 |= 0x00100000;
2787 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2789 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
2790 dev->dev->bus->chipco.fast_pwrup_delay);
2793 b43dbg(dev->wl, "Chip initialized\n");
2798 b43_gpio_cleanup(dev);
2802 static void b43_periodic_every60sec(struct b43_wldev *dev)
2804 const struct b43_phy_operations *ops = dev->phy.ops;
2806 if (ops->pwork_60sec)
2807 ops->pwork_60sec(dev);
2809 /* Force check the TX power emission now. */
2810 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
2813 static void b43_periodic_every30sec(struct b43_wldev *dev)
2815 /* Update device statistics. */
2816 b43_calculate_link_quality(dev);
2819 static void b43_periodic_every15sec(struct b43_wldev *dev)
2821 struct b43_phy *phy = &dev->phy;
2824 if (dev->fw.opensource) {
2825 /* Check if the firmware is still alive.
2826 * It will reset the watchdog counter to 0 in its idle loop. */
2827 wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
2828 if (unlikely(wdr)) {
2829 b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
2830 b43_controller_restart(dev, "Firmware watchdog");
2833 b43_shm_write16(dev, B43_SHM_SCRATCH,
2834 B43_WATCHDOG_REG, 1);
2838 if (phy->ops->pwork_15sec)
2839 phy->ops->pwork_15sec(dev);
2841 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
2845 static void do_periodic_work(struct b43_wldev *dev)
2849 state = dev->periodic_state;
2851 b43_periodic_every60sec(dev);
2853 b43_periodic_every30sec(dev);
2854 b43_periodic_every15sec(dev);
2857 /* Periodic work locking policy:
2858 * The whole periodic work handler is protected by
2859 * wl->mutex. If another lock is needed somewhere in the
2860 * pwork callchain, it's aquired in-place, where it's needed.
2862 static void b43_periodic_work_handler(struct work_struct *work)
2864 struct b43_wldev *dev = container_of(work, struct b43_wldev,
2865 periodic_work.work);
2866 struct b43_wl *wl = dev->wl;
2867 unsigned long delay;
2869 mutex_lock(&wl->mutex);
2871 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
2873 if (b43_debug(dev, B43_DBG_PWORK_STOP))
2876 do_periodic_work(dev);
2878 dev->periodic_state++;
2880 if (b43_debug(dev, B43_DBG_PWORK_FAST))
2881 delay = msecs_to_jiffies(50);
2883 delay = round_jiffies_relative(HZ * 15);
2884 queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
2886 mutex_unlock(&wl->mutex);
2889 static void b43_periodic_tasks_setup(struct b43_wldev *dev)
2891 struct delayed_work *work = &dev->periodic_work;
2893 dev->periodic_state = 0;
2894 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
2895 queue_delayed_work(dev->wl->hw->workqueue, work, 0);
2898 /* Check if communication with the device works correctly. */
2899 static int b43_validate_chipaccess(struct b43_wldev *dev)
2903 backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
2905 /* Check for read/write and endianness problems. */
2906 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
2907 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
2909 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
2910 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
2913 b43_shm_write32(dev, B43_SHM_SHARED, 0, backup);
2915 if ((dev->dev->id.revision >= 3) && (dev->dev->id.revision <= 10)) {
2916 /* The 32bit register shadows the two 16bit registers
2917 * with update sideeffects. Validate this. */
2918 b43_write16(dev, B43_MMIO_TSF_CFP_START, 0xAAAA);
2919 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0xCCCCBBBB);
2920 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_LOW) != 0xBBBB)
2922 if (b43_read16(dev, B43_MMIO_TSF_CFP_START_HIGH) != 0xCCCC)
2925 b43_write32(dev, B43_MMIO_TSF_CFP_START, 0);
2927 v = b43_read32(dev, B43_MMIO_MACCTL);
2928 v |= B43_MACCTL_GMODE;
2929 if (v != (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
2934 b43err(dev->wl, "Failed to validate the chipaccess\n");
2938 static void b43_security_init(struct b43_wldev *dev)
2940 dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
2941 B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
2942 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
2943 /* KTP is a word address, but we address SHM bytewise.
2944 * So multiply by two.
2947 if (dev->dev->id.revision >= 5) {
2948 /* Number of RCMTA address slots */
2949 b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
2951 b43_clear_keys(dev);
2954 static int b43_rng_read(struct hwrng *rng, u32 * data)
2956 struct b43_wl *wl = (struct b43_wl *)rng->priv;
2957 unsigned long flags;
2959 /* Don't take wl->mutex here, as it could deadlock with
2960 * hwrng internal locking. It's not needed to take
2961 * wl->mutex here, anyway. */
2963 spin_lock_irqsave(&wl->irq_lock, flags);
2964 *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
2965 spin_unlock_irqrestore(&wl->irq_lock, flags);
2967 return (sizeof(u16));
2970 static void b43_rng_exit(struct b43_wl *wl)
2972 if (wl->rng_initialized)
2973 hwrng_unregister(&wl->rng);
2976 static int b43_rng_init(struct b43_wl *wl)
2980 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
2981 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
2982 wl->rng.name = wl->rng_name;
2983 wl->rng.data_read = b43_rng_read;
2984 wl->rng.priv = (unsigned long)wl;
2985 wl->rng_initialized = 1;
2986 err = hwrng_register(&wl->rng);
2988 wl->rng_initialized = 0;
2989 b43err(wl, "Failed to register the random "
2990 "number generator (%d)\n", err);
2996 static int b43_op_tx(struct ieee80211_hw *hw,
2997 struct sk_buff *skb)
2999 struct b43_wl *wl = hw_to_b43_wl(hw);
3000 struct b43_wldev *dev = wl->current_dev;
3001 unsigned long flags;
3004 if (unlikely(skb->len < 2 + 2 + 6)) {
3005 /* Too short, this can't be a valid frame. */
3008 B43_WARN_ON(skb_shinfo(skb)->nr_frags);
3012 /* Transmissions on seperate queues can run concurrently. */
3013 read_lock_irqsave(&wl->tx_lock, flags);
3016 if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
3017 if (b43_using_pio_transfers(dev))
3018 err = b43_pio_tx(dev, skb);
3020 err = b43_dma_tx(dev, skb);
3023 read_unlock_irqrestore(&wl->tx_lock, flags);
3027 return NETDEV_TX_OK;
3030 /* We can not transmit this packet. Drop it. */
3031 dev_kfree_skb_any(skb);
3032 return NETDEV_TX_OK;
3035 /* Locking: wl->irq_lock */
3036 static void b43_qos_params_upload(struct b43_wldev *dev,
3037 const struct ieee80211_tx_queue_params *p,
3040 u16 params[B43_NR_QOSPARAMS];
3044 bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
3046 memset(¶ms, 0, sizeof(params));
3048 params[B43_QOSPARAM_TXOP] = p->txop * 32;
3049 params[B43_QOSPARAM_CWMIN] = p->cw_min;
3050 params[B43_QOSPARAM_CWMAX] = p->cw_max;
3051 params[B43_QOSPARAM_CWCUR] = p->cw_min;
3052 params[B43_QOSPARAM_AIFS] = p->aifs;
3053 params[B43_QOSPARAM_BSLOTS] = bslots;
3054 params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
3056 for (i = 0; i < ARRAY_SIZE(params); i++) {
3057 if (i == B43_QOSPARAM_STATUS) {
3058 tmp = b43_shm_read16(dev, B43_SHM_SHARED,
3059 shm_offset + (i * 2));
3060 /* Mark the parameters as updated. */
3062 b43_shm_write16(dev, B43_SHM_SHARED,
3063 shm_offset + (i * 2),
3066 b43_shm_write16(dev, B43_SHM_SHARED,
3067 shm_offset + (i * 2),
3073 /* Update the QOS parameters in hardware. */
3074 static void b43_qos_update(struct b43_wldev *dev)
3076 struct b43_wl *wl = dev->wl;
3077 struct b43_qos_params *params;
3078 unsigned long flags;
3081 /* Mapping of mac80211 queues to b43 SHM offsets. */
3082 static const u16 qos_shm_offsets[] = {
3083 [0] = B43_QOS_VOICE,
3084 [1] = B43_QOS_VIDEO,
3085 [2] = B43_QOS_BESTEFFORT,
3086 [3] = B43_QOS_BACKGROUND,
3088 BUILD_BUG_ON(ARRAY_SIZE(qos_shm_offsets) != ARRAY_SIZE(wl->qos_params));
3090 b43_mac_suspend(dev);
3091 spin_lock_irqsave(&wl->irq_lock, flags);
3093 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3094 params = &(wl->qos_params[i]);
3095 if (params->need_hw_update) {
3096 b43_qos_params_upload(dev, &(params->p),
3097 qos_shm_offsets[i]);
3098 params->need_hw_update = 0;
3102 spin_unlock_irqrestore(&wl->irq_lock, flags);
3103 b43_mac_enable(dev);
3106 static void b43_qos_clear(struct b43_wl *wl)
3108 struct b43_qos_params *params;
3111 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
3112 params = &(wl->qos_params[i]);
3114 memset(&(params->p), 0, sizeof(params->p));
3115 params->p.aifs = -1;
3116 params->need_hw_update = 1;
3120 /* Initialize the core's QOS capabilities */
3121 static void b43_qos_init(struct b43_wldev *dev)
3123 struct b43_wl *wl = dev->wl;
3126 /* Upload the current QOS parameters. */
3127 for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++)
3128 wl->qos_params[i].need_hw_update = 1;
3129 b43_qos_update(dev);
3131 /* Enable QOS support. */
3132 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
3133 b43_write16(dev, B43_MMIO_IFSCTL,
3134 b43_read16(dev, B43_MMIO_IFSCTL)
3135 | B43_MMIO_IFSCTL_USE_EDCF);
3138 static void b43_qos_update_work(struct work_struct *work)
3140 struct b43_wl *wl = container_of(work, struct b43_wl, qos_update_work);
3141 struct b43_wldev *dev;
3143 mutex_lock(&wl->mutex);
3144 dev = wl->current_dev;
3145 if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED)))
3146 b43_qos_update(dev);
3147 mutex_unlock(&wl->mutex);
3150 static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
3151 const struct ieee80211_tx_queue_params *params)
3153 struct b43_wl *wl = hw_to_b43_wl(hw);
3154 unsigned long flags;
3155 unsigned int queue = (unsigned int)_queue;
3156 struct b43_qos_params *p;
3158 if (queue >= ARRAY_SIZE(wl->qos_params)) {
3159 /* Queue not available or don't support setting
3160 * params on this queue. Return success to not
3161 * confuse mac80211. */
3165 spin_lock_irqsave(&wl->irq_lock, flags);
3166 p = &(wl->qos_params[queue]);
3167 memcpy(&(p->p), params, sizeof(p->p));
3168 p->need_hw_update = 1;
3169 spin_unlock_irqrestore(&wl->irq_lock, flags);
3171 queue_work(hw->workqueue, &wl->qos_update_work);
3176 static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
3177 struct ieee80211_tx_queue_stats *stats)
3179 struct b43_wl *wl = hw_to_b43_wl(hw);
3180 struct b43_wldev *dev = wl->current_dev;
3181 unsigned long flags;
3186 spin_lock_irqsave(&wl->irq_lock, flags);
3187 if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
3188 if (b43_using_pio_transfers(dev))
3189 b43_pio_get_tx_stats(dev, stats);
3191 b43_dma_get_tx_stats(dev, stats);
3194 spin_unlock_irqrestore(&wl->irq_lock, flags);
3199 static int b43_op_get_stats(struct ieee80211_hw *hw,
3200 struct ieee80211_low_level_stats *stats)
3202 struct b43_wl *wl = hw_to_b43_wl(hw);
3203 unsigned long flags;
3205 spin_lock_irqsave(&wl->irq_lock, flags);
3206 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
3207 spin_unlock_irqrestore(&wl->irq_lock, flags);
3212 static void b43_put_phy_into_reset(struct b43_wldev *dev)
3214 struct ssb_device *sdev = dev->dev;
3217 tmslow = ssb_read32(sdev, SSB_TMSLOW);
3218 tmslow &= ~B43_TMSLOW_GMODE;
3219 tmslow |= B43_TMSLOW_PHYRESET;
3220 tmslow |= SSB_TMSLOW_FGC;
3221 ssb_write32(sdev, SSB_TMSLOW, tmslow);
3224 tmslow = ssb_read32(sdev, SSB_TMSLOW);
3225 tmslow &= ~SSB_TMSLOW_FGC;
3226 tmslow |= B43_TMSLOW_PHYRESET;
3227 ssb_write32(sdev, SSB_TMSLOW, tmslow);
3231 static const char * band_to_string(enum ieee80211_band band)
3234 case IEEE80211_BAND_5GHZ:
3236 case IEEE80211_BAND_2GHZ:
3245 /* Expects wl->mutex locked */
3246 static int b43_switch_band(struct b43_wl *wl, struct ieee80211_channel *chan)
3248 struct b43_wldev *up_dev = NULL;
3249 struct b43_wldev *down_dev;
3250 struct b43_wldev *d;
3255 /* Find a device and PHY which supports the band. */
3256 list_for_each_entry(d, &wl->devlist, list) {
3257 switch (chan->band) {
3258 case IEEE80211_BAND_5GHZ:
3259 if (d->phy.supports_5ghz) {
3264 case IEEE80211_BAND_2GHZ:
3265 if (d->phy.supports_2ghz) {
3278 b43err(wl, "Could not find a device for %s-GHz band operation\n",
3279 band_to_string(chan->band));
3282 if ((up_dev == wl->current_dev) &&
3283 (!!wl->current_dev->phy.gmode == !!gmode)) {
3284 /* This device is already running. */
3287 b43dbg(wl, "Switching to %s-GHz band\n",
3288 band_to_string(chan->band));
3289 down_dev = wl->current_dev;
3291 prev_status = b43_status(down_dev);
3292 /* Shutdown the currently running core. */
3293 if (prev_status >= B43_STAT_STARTED)
3294 b43_wireless_core_stop(down_dev);
3295 if (prev_status >= B43_STAT_INITIALIZED)
3296 b43_wireless_core_exit(down_dev);
3298 if (down_dev != up_dev) {
3299 /* We switch to a different core, so we put PHY into
3300 * RESET on the old core. */
3301 b43_put_phy_into_reset(down_dev);
3304 /* Now start the new core. */
3305 up_dev->phy.gmode = gmode;
3306 if (prev_status >= B43_STAT_INITIALIZED) {
3307 err = b43_wireless_core_init(up_dev);
3309 b43err(wl, "Fatal: Could not initialize device for "
3310 "selected %s-GHz band\n",
3311 band_to_string(chan->band));
3315 if (prev_status >= B43_STAT_STARTED) {
3316 err = b43_wireless_core_start(up_dev);
3318 b43err(wl, "Fatal: Coult not start device for "
3319 "selected %s-GHz band\n",
3320 band_to_string(chan->band));
3321 b43_wireless_core_exit(up_dev);
3325 B43_WARN_ON(b43_status(up_dev) != prev_status);
3327 wl->current_dev = up_dev;
3331 /* Whoops, failed to init the new core. No core is operating now. */
3332 wl->current_dev = NULL;
3336 static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
3338 struct b43_wl *wl = hw_to_b43_wl(hw);
3339 struct b43_wldev *dev;
3340 struct b43_phy *phy;
3341 unsigned long flags;
3346 mutex_lock(&wl->mutex);
3348 /* Switch the band (if necessary). This might change the active core. */
3349 err = b43_switch_band(wl, conf->channel);
3351 goto out_unlock_mutex;
3352 dev = wl->current_dev;
3355 /* Disable IRQs while reconfiguring the device.
3356 * This makes it possible to drop the spinlock throughout
3357 * the reconfiguration process. */
3358 spin_lock_irqsave(&wl->irq_lock, flags);
3359 if (b43_status(dev) < B43_STAT_STARTED) {
3360 spin_unlock_irqrestore(&wl->irq_lock, flags);
3361 goto out_unlock_mutex;
3363 savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
3364 spin_unlock_irqrestore(&wl->irq_lock, flags);
3365 b43_synchronize_irq(dev);
3367 /* Switch to the requested channel.
3368 * The firmware takes care of races with the TX handler. */
3369 if (conf->channel->hw_value != phy->channel)
3370 b43_switch_channel(dev, conf->channel->hw_value);
3372 /* Enable/Disable ShortSlot timing. */
3373 if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
3375 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
3376 if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
3377 b43_short_slot_timing_enable(dev);
3379 b43_short_slot_timing_disable(dev);
3382 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
3384 /* Adjust the desired TX power level. */
3385 if (conf->power_level != 0) {
3386 spin_lock_irqsave(&wl->irq_lock, flags);
3387 if (conf->power_level != phy->desired_txpower) {
3388 phy->desired_txpower = conf->power_level;
3389 b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
3390 B43_TXPWR_IGNORE_TSSI);
3392 spin_unlock_irqrestore(&wl->irq_lock, flags);
3395 /* Antennas for RX and management frame TX. */
3396 antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_tx);
3397 b43_mgmtframe_txantenna(dev, antenna);
3398 antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_rx);
3399 if (phy->ops->set_rx_antenna)
3400 phy->ops->set_rx_antenna(dev, antenna);
3402 /* Update templates for AP/mesh mode. */
3403 if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP) ||
3404 b43_is_mode(wl, IEEE80211_IF_TYPE_MESH_POINT))
3405 b43_set_beacon_int(dev, conf->beacon_int);
3407 if (!!conf->radio_enabled != phy->radio_on) {
3408 if (conf->radio_enabled) {
3409 b43_software_rfkill(dev, RFKILL_STATE_UNBLOCKED);
3410 b43info(dev->wl, "Radio turned on by software\n");
3411 if (!dev->radio_hw_enable) {
3412 b43info(dev->wl, "The hardware RF-kill button "
3413 "still turns the radio physically off. "
3414 "Press the button to turn it on.\n");
3417 b43_software_rfkill(dev, RFKILL_STATE_SOFT_BLOCKED);
3418 b43info(dev->wl, "Radio turned off by software\n");
3422 spin_lock_irqsave(&wl->irq_lock, flags);
3423 b43_interrupt_enable(dev, savedirqs);
3425 spin_unlock_irqrestore(&wl->irq_lock, flags);
3427 mutex_unlock(&wl->mutex);
3432 static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3433 const u8 *local_addr, const u8 *addr,
3434 struct ieee80211_key_conf *key)
3436 struct b43_wl *wl = hw_to_b43_wl(hw);
3437 struct b43_wldev *dev;
3438 unsigned long flags;
3442 DECLARE_MAC_BUF(mac);
3444 if (modparam_nohwcrypt)
3445 return -ENOSPC; /* User disabled HW-crypto */
3447 mutex_lock(&wl->mutex);
3448 spin_lock_irqsave(&wl->irq_lock, flags);
3450 dev = wl->current_dev;
3452 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
3455 if (dev->fw.pcm_request_failed) {
3456 /* We don't have firmware for the crypto engine.
3457 * Must use software-crypto. */
3465 if (key->keylen == 5)
3466 algorithm = B43_SEC_ALGO_WEP40;
3468 algorithm = B43_SEC_ALGO_WEP104;
3471 algorithm = B43_SEC_ALGO_TKIP;
3474 algorithm = B43_SEC_ALGO_AES;
3480 index = (u8) (key->keyidx);
3486 if (algorithm == B43_SEC_ALGO_TKIP) {
3487 /* FIXME: No TKIP hardware encryption for now. */
3492 if (is_broadcast_ether_addr(addr)) {
3493 /* addr is FF:FF:FF:FF:FF:FF for default keys */
3494 err = b43_key_write(dev, index, algorithm,
3495 key->key, key->keylen, NULL, key);
3498 * either pairwise key or address is 00:00:00:00:00:00
3499 * for transmit-only keys
3501 err = b43_key_write(dev, -1, algorithm,
3502 key->key, key->keylen, addr, key);
3507 if (algorithm == B43_SEC_ALGO_WEP40 ||
3508 algorithm == B43_SEC_ALGO_WEP104) {
3509 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
3512 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
3514 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3517 err = b43_key_clear(dev, key->hw_key_idx);
3526 spin_unlock_irqrestore(&wl->irq_lock, flags);
3527 mutex_unlock(&wl->mutex);
3529 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
3531 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
3532 print_mac(mac, addr));
3537 static void b43_op_configure_filter(struct ieee80211_hw *hw,
3538 unsigned int changed, unsigned int *fflags,
3539 int mc_count, struct dev_addr_list *mc_list)
3541 struct b43_wl *wl = hw_to_b43_wl(hw);
3542 struct b43_wldev *dev = wl->current_dev;
3543 unsigned long flags;
3550 spin_lock_irqsave(&wl->irq_lock, flags);
3551 *fflags &= FIF_PROMISC_IN_BSS |
3557 FIF_BCN_PRBRESP_PROMISC;
3559 changed &= FIF_PROMISC_IN_BSS |
3565 FIF_BCN_PRBRESP_PROMISC;
3567 wl->filter_flags = *fflags;
3569 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
3570 b43_adjust_opmode(dev);
3571 spin_unlock_irqrestore(&wl->irq_lock, flags);
3574 static int b43_op_config_interface(struct ieee80211_hw *hw,
3575 struct ieee80211_vif *vif,
3576 struct ieee80211_if_conf *conf)
3578 struct b43_wl *wl = hw_to_b43_wl(hw);
3579 struct b43_wldev *dev = wl->current_dev;
3580 unsigned long flags;
3584 mutex_lock(&wl->mutex);
3585 spin_lock_irqsave(&wl->irq_lock, flags);
3586 B43_WARN_ON(wl->vif != vif);
3588 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
3590 memset(wl->bssid, 0, ETH_ALEN);
3591 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
3592 if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP) ||
3593 b43_is_mode(wl, IEEE80211_IF_TYPE_MESH_POINT)) {
3594 B43_WARN_ON(vif->type != wl->if_type);
3595 if (conf->changed & IEEE80211_IFCC_SSID)
3596 b43_set_ssid(dev, conf->ssid, conf->ssid_len);
3597 if (conf->changed & IEEE80211_IFCC_BEACON)
3598 b43_update_templates(wl);
3599 } else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS)) {
3600 if (conf->changed & IEEE80211_IFCC_BEACON)
3601 b43_update_templates(wl);
3603 b43_write_mac_bssid_templates(dev);
3605 spin_unlock_irqrestore(&wl->irq_lock, flags);
3606 mutex_unlock(&wl->mutex);
3611 /* Locking: wl->mutex */
3612 static void b43_wireless_core_stop(struct b43_wldev *dev)
3614 struct b43_wl *wl = dev->wl;
3615 unsigned long flags;
3617 if (b43_status(dev) < B43_STAT_STARTED)
3620 /* Disable and sync interrupts. We must do this before than
3621 * setting the status to INITIALIZED, as the interrupt handler
3622 * won't care about IRQs then. */
3623 spin_lock_irqsave(&wl->irq_lock, flags);
3624 dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
3625 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
3626 spin_unlock_irqrestore(&wl->irq_lock, flags);
3627 b43_synchronize_irq(dev);
3629 write_lock_irqsave(&wl->tx_lock, flags);
3630 b43_set_status(dev, B43_STAT_INITIALIZED);
3631 write_unlock_irqrestore(&wl->tx_lock, flags);
3634 mutex_unlock(&wl->mutex);
3635 /* Must unlock as it would otherwise deadlock. No races here.
3636 * Cancel the possibly running self-rearming periodic work. */
3637 cancel_delayed_work_sync(&dev->periodic_work);
3638 mutex_lock(&wl->mutex);
3640 b43_mac_suspend(dev);
3641 free_irq(dev->dev->irq, dev);
3642 b43dbg(wl, "Wireless interface stopped\n");
3645 /* Locking: wl->mutex */
3646 static int b43_wireless_core_start(struct b43_wldev *dev)
3650 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
3652 drain_txstatus_queue(dev);
3653 err = request_irq(dev->dev->irq, b43_interrupt_handler,
3654 IRQF_SHARED, KBUILD_MODNAME, dev);
3656 b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
3660 /* We are ready to run. */
3661 b43_set_status(dev, B43_STAT_STARTED);
3663 /* Start data flow (TX/RX). */
3664 b43_mac_enable(dev);
3665 b43_interrupt_enable(dev, dev->irq_savedstate);
3667 /* Start maintainance work */
3668 b43_periodic_tasks_setup(dev);
3670 b43dbg(dev->wl, "Wireless interface started\n");
3675 /* Get PHY and RADIO versioning numbers */
3676 static int b43_phy_versioning(struct b43_wldev *dev)
3678 struct b43_phy *phy = &dev->phy;
3686 int unsupported = 0;
3688 /* Get PHY versioning */
3689 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
3690 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
3691 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
3692 phy_rev = (tmp & B43_PHYVER_VERSION);
3699 if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
3707 #ifdef CONFIG_B43_NPHY
3717 b43err(dev->wl, "FOUND UNSUPPORTED PHY "
3718 "(Analog %u, Type %u, Revision %u)\n",
3719 analog_type, phy_type, phy_rev);
3722 b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
3723 analog_type, phy_type, phy_rev);
3725 /* Get RADIO versioning */
3726 if (dev->dev->bus->chip_id == 0x4317) {
3727 if (dev->dev->bus->chip_rev == 0)
3729 else if (dev->dev->bus->chip_rev == 1)
3734 b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
3735 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
3736 b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
3737 tmp |= (u32)b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH) << 16;
3739 radio_manuf = (tmp & 0x00000FFF);
3740 radio_ver = (tmp & 0x0FFFF000) >> 12;
3741 radio_rev = (tmp & 0xF0000000) >> 28;
3742 if (radio_manuf != 0x17F /* Broadcom */)
3746 if (radio_ver != 0x2060)
3750 if (radio_manuf != 0x17F)
3754 if ((radio_ver & 0xFFF0) != 0x2050)
3758 if (radio_ver != 0x2050)
3762 if (radio_ver != 0x2055)
3769 b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
3770 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
3771 radio_manuf, radio_ver, radio_rev);
3774 b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
3775 radio_manuf, radio_ver, radio_rev);
3777 phy->radio_manuf = radio_manuf;
3778 phy->radio_ver = radio_ver;
3779 phy->radio_rev = radio_rev;
3781 phy->analog = analog_type;
3782 phy->type = phy_type;
3788 static void setup_struct_phy_for_init(struct b43_wldev *dev,
3789 struct b43_phy *phy)
3791 phy->hardware_power_control = !!modparam_hwpctl;
3792 phy->next_txpwr_check_time = jiffies;
3793 /* PHY TX errors counter. */
3794 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3797 static void setup_struct_wldev_for_init(struct b43_wldev *dev)
3801 /* Assume the radio is enabled. If it's not enabled, the state will
3802 * immediately get fixed on the first periodic work run. */
3803 dev->radio_hw_enable = 1;
3806 memset(&dev->stats, 0, sizeof(dev->stats));
3808 setup_struct_phy_for_init(dev, &dev->phy);
3810 /* IRQ related flags */
3811 dev->irq_reason = 0;
3812 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
3813 dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
3815 dev->mac_suspended = 1;
3817 /* Noise calculation context */
3818 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
3821 static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
3823 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
3826 if (!modparam_btcoex)
3828 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
3830 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
3833 hf = b43_hf_read(dev);
3834 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
3835 hf |= B43_HF_BTCOEXALT;
3837 hf |= B43_HF_BTCOEX;
3838 b43_hf_write(dev, hf);
3841 static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
3843 if (!modparam_btcoex)
3848 static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
3850 #ifdef CONFIG_SSB_DRIVER_PCICORE
3851 struct ssb_bus *bus = dev->dev->bus;
3854 if (bus->pcicore.dev &&
3855 bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
3856 bus->pcicore.dev->id.revision <= 5) {
3857 /* IMCFGLO timeouts workaround. */
3858 tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
3859 tmp &= ~SSB_IMCFGLO_REQTO;
3860 tmp &= ~SSB_IMCFGLO_SERTO;
3861 switch (bus->bustype) {
3862 case SSB_BUSTYPE_PCI:
3863 case SSB_BUSTYPE_PCMCIA:
3866 case SSB_BUSTYPE_SSB:
3870 ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
3872 #endif /* CONFIG_SSB_DRIVER_PCICORE */
3875 /* Write the short and long frame retry limit values. */
3876 static void b43_set_retry_limits(struct b43_wldev *dev,
3877 unsigned int short_retry,
3878 unsigned int long_retry)
3880 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3881 * the chip-internal counter. */
3882 short_retry = min(short_retry, (unsigned int)0xF);
3883 long_retry = min(long_retry, (unsigned int)0xF);
3885 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3887 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3891 static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
3895 /* The time value is in microseconds. */
3896 if (dev->phy.type == B43_PHYTYPE_A)
3900 if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS) || idle)
3902 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
3903 pu_delay = max(pu_delay, (u16)2400);
3905 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
3908 /* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
3909 static void b43_set_pretbtt(struct b43_wldev *dev)
3913 /* The time value is in microseconds. */
3914 if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS)) {
3917 if (dev->phy.type == B43_PHYTYPE_A)
3922 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
3923 b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
3926 /* Shutdown a wireless core */
3927 /* Locking: wl->mutex */
3928 static void b43_wireless_core_exit(struct b43_wldev *dev)
3932 B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
3933 if (b43_status(dev) != B43_STAT_INITIALIZED)
3935 b43_set_status(dev, B43_STAT_UNINIT);
3937 /* Stop the microcode PSM. */
3938 macctl = b43_read32(dev, B43_MMIO_MACCTL);
3939 macctl &= ~B43_MACCTL_PSM_RUN;
3940 macctl |= B43_MACCTL_PSM_JMP0;
3941 b43_write32(dev, B43_MMIO_MACCTL, macctl);
3943 if (!dev->suspend_in_progress) {
3945 b43_rng_exit(dev->wl);
3950 b43_switch_analog(dev, 0);
3951 if (dev->wl->current_beacon) {
3952 dev_kfree_skb_any(dev->wl->current_beacon);
3953 dev->wl->current_beacon = NULL;
3957 ssb_device_disable(dev->dev, 0);
3958 ssb_bus_may_powerdown(dev->dev->bus);
3961 /* Initialize a wireless core */
3962 static int b43_wireless_core_init(struct b43_wldev *dev)
3964 struct b43_wl *wl = dev->wl;
3965 struct ssb_bus *bus = dev->dev->bus;
3966 struct ssb_sprom *sprom = &bus->sprom;
3967 struct b43_phy *phy = &dev->phy;
3972 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
3974 err = ssb_bus_powerup(bus, 0);
3977 if (!ssb_device_is_enabled(dev->dev)) {
3978 tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
3979 b43_wireless_core_reset(dev, tmp);
3982 setup_struct_wldev_for_init(dev);
3983 err = b43_phy_operations_setup(dev);
3987 /* Enable IRQ routing to this device. */
3988 ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
3990 b43_imcfglo_timeouts_workaround(dev);
3991 b43_bluetooth_coext_disable(dev);
3992 if (phy->ops->prepare) {
3993 err = phy->ops->prepare(dev);
3997 err = b43_chip_init(dev);
4000 b43_shm_write16(dev, B43_SHM_SHARED,
4001 B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
4002 hf = b43_hf_read(dev);
4003 if (phy->type == B43_PHYTYPE_G) {
4007 if (sprom->boardflags_lo & B43_BFL_PACTRL)
4008 hf |= B43_HF_OFDMPABOOST;
4009 } else if (phy->type == B43_PHYTYPE_B) {
4011 if (phy->rev >= 2 && phy->radio_ver == 0x2050)
4014 b43_hf_write(dev, hf);
4016 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
4017 B43_DEFAULT_LONG_RETRY_LIMIT);
4018 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
4019 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
4021 /* Disable sending probe responses from firmware.
4022 * Setting the MaxTime to one usec will always trigger
4023 * a timeout, so we never send any probe resp.
4024 * A timeout of zero is infinite. */
4025 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
4027 b43_rate_memory_init(dev);
4028 b43_set_phytxctl_defaults(dev);
4030 /* Minimum Contention Window */
4031 if (phy->type == B43_PHYTYPE_B) {
4032 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
4034 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
4036 /* Maximum Contention Window */
4037 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
4039 if ((dev->dev->bus->bustype == SSB_BUSTYPE_PCMCIA) || B43_FORCE_PIO) {
4040 dev->__using_pio_transfers = 1;
4041 err = b43_pio_init(dev);
4043 dev->__using_pio_transfers = 0;
4044 err = b43_dma_init(dev);
4049 b43_set_synth_pu_delay(dev, 1);
4050 b43_bluetooth_coext_enable(dev);
4052 ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
4053 b43_upload_card_macaddress(dev);
4054 b43_security_init(dev);
4055 if (!dev->suspend_in_progress)
4058 b43_set_status(dev, B43_STAT_INITIALIZED);
4060 if (!dev->suspend_in_progress)
4070 ssb_bus_may_powerdown(bus);
4071 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
4075 static int b43_op_add_interface(struct ieee80211_hw *hw,
4076 struct ieee80211_if_init_conf *conf)
4078 struct b43_wl *wl = hw_to_b43_wl(hw);
4079 struct b43_wldev *dev;
4080 unsigned long flags;
4081 int err = -EOPNOTSUPP;
4083 /* TODO: allow WDS/AP devices to coexist */
4085 if (conf->type != IEEE80211_IF_TYPE_AP &&
4086 conf->type != IEEE80211_IF_TYPE_MESH_POINT &&
4087 conf->type != IEEE80211_IF_TYPE_STA &&
4088 conf->type != IEEE80211_IF_TYPE_WDS &&
4089 conf->type != IEEE80211_IF_TYPE_IBSS)
4092 mutex_lock(&wl->mutex);
4094 goto out_mutex_unlock;
4096 b43dbg(wl, "Adding Interface type %d\n", conf->type);
4098 dev = wl->current_dev;
4100 wl->vif = conf->vif;
4101 wl->if_type = conf->type;
4102 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
4104 spin_lock_irqsave(&wl->irq_lock, flags);
4105 b43_adjust_opmode(dev);
4106 b43_set_pretbtt(dev);
4107 b43_set_synth_pu_delay(dev, 0);
4108 b43_upload_card_macaddress(dev);
4109 spin_unlock_irqrestore(&wl->irq_lock, flags);
4113 mutex_unlock(&wl->mutex);
4118 static void b43_op_remove_interface(struct ieee80211_hw *hw,
4119 struct ieee80211_if_init_conf *conf)
4121 struct b43_wl *wl = hw_to_b43_wl(hw);
4122 struct b43_wldev *dev = wl->current_dev;
4123 unsigned long flags;
4125 b43dbg(wl, "Removing Interface type %d\n", conf->type);
4127 mutex_lock(&wl->mutex);
4129 B43_WARN_ON(!wl->operating);
4130 B43_WARN_ON(wl->vif != conf->vif);
4135 spin_lock_irqsave(&wl->irq_lock, flags);
4136 b43_adjust_opmode(dev);
4137 memset(wl->mac_addr, 0, ETH_ALEN);
4138 b43_upload_card_macaddress(dev);
4139 spin_unlock_irqrestore(&wl->irq_lock, flags);
4141 mutex_unlock(&wl->mutex);
4144 static int b43_op_start(struct ieee80211_hw *hw)
4146 struct b43_wl *wl = hw_to_b43_wl(hw);
4147 struct b43_wldev *dev = wl->current_dev;
4150 bool do_rfkill_exit = 0;
4152 /* Kill all old instance specific information to make sure
4153 * the card won't use it in the short timeframe between start
4154 * and mac80211 reconfiguring it. */
4155 memset(wl->bssid, 0, ETH_ALEN);
4156 memset(wl->mac_addr, 0, ETH_ALEN);
4157 wl->filter_flags = 0;
4158 wl->radiotap_enabled = 0;
4160 wl->beacon0_uploaded = 0;
4161 wl->beacon1_uploaded = 0;
4162 wl->beacon_templates_virgin = 1;
4164 /* First register RFkill.
4165 * LEDs that are registered later depend on it. */
4166 b43_rfkill_init(dev);
4168 mutex_lock(&wl->mutex);
4170 if (b43_status(dev) < B43_STAT_INITIALIZED) {
4171 err = b43_wireless_core_init(dev);
4174 goto out_mutex_unlock;
4179 if (b43_status(dev) < B43_STAT_STARTED) {
4180 err = b43_wireless_core_start(dev);
4183 b43_wireless_core_exit(dev);
4185 goto out_mutex_unlock;
4190 mutex_unlock(&wl->mutex);
4193 b43_rfkill_exit(dev);
4198 static void b43_op_stop(struct ieee80211_hw *hw)
4200 struct b43_wl *wl = hw_to_b43_wl(hw);
4201 struct b43_wldev *dev = wl->current_dev;
4203 b43_rfkill_exit(dev);
4204 cancel_work_sync(&(wl->qos_update_work));
4205 cancel_work_sync(&(wl->beacon_update_trigger));
4207 mutex_lock(&wl->mutex);
4208 if (b43_status(dev) >= B43_STAT_STARTED)
4209 b43_wireless_core_stop(dev);
4210 b43_wireless_core_exit(dev);
4211 mutex_unlock(&wl->mutex);
4213 cancel_work_sync(&(wl->txpower_adjust_work));
4216 static int b43_op_set_retry_limit(struct ieee80211_hw *hw,
4217 u32 short_retry_limit, u32 long_retry_limit)
4219 struct b43_wl *wl = hw_to_b43_wl(hw);
4220 struct b43_wldev *dev;
4223 mutex_lock(&wl->mutex);
4224 dev = wl->current_dev;
4225 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED))) {
4229 b43_set_retry_limits(dev, short_retry_limit, long_retry_limit);
4231 mutex_unlock(&wl->mutex);
4236 static int b43_op_beacon_set_tim(struct ieee80211_hw *hw, int aid, int set)
4238 struct b43_wl *wl = hw_to_b43_wl(hw);
4239 unsigned long flags;
4241 spin_lock_irqsave(&wl->irq_lock, flags);
4242 b43_update_templates(wl);
4243 spin_unlock_irqrestore(&wl->irq_lock, flags);
4248 static void b43_op_sta_notify(struct ieee80211_hw *hw,
4249 struct ieee80211_vif *vif,
4250 enum sta_notify_cmd notify_cmd,
4253 struct b43_wl *wl = hw_to_b43_wl(hw);
4255 B43_WARN_ON(!vif || wl->vif != vif);
4258 static const struct ieee80211_ops b43_hw_ops = {
4260 .conf_tx = b43_op_conf_tx,
4261 .add_interface = b43_op_add_interface,
4262 .remove_interface = b43_op_remove_interface,
4263 .config = b43_op_config,
4264 .config_interface = b43_op_config_interface,
4265 .configure_filter = b43_op_configure_filter,
4266 .set_key = b43_op_set_key,
4267 .get_stats = b43_op_get_stats,
4268 .get_tx_stats = b43_op_get_tx_stats,
4269 .start = b43_op_start,
4270 .stop = b43_op_stop,
4271 .set_retry_limit = b43_op_set_retry_limit,
4272 .set_tim = b43_op_beacon_set_tim,
4273 .sta_notify = b43_op_sta_notify,
4276 /* Hard-reset the chip. Do not call this directly.
4277 * Use b43_controller_restart()
4279 static void b43_chip_reset(struct work_struct *work)
4281 struct b43_wldev *dev =
4282 container_of(work, struct b43_wldev, restart_work);
4283 struct b43_wl *wl = dev->wl;
4287 mutex_lock(&wl->mutex);
4289 prev_status = b43_status(dev);
4290 /* Bring the device down... */
4291 if (prev_status >= B43_STAT_STARTED)
4292 b43_wireless_core_stop(dev);
4293 if (prev_status >= B43_STAT_INITIALIZED)
4294 b43_wireless_core_exit(dev);
4296 /* ...and up again. */
4297 if (prev_status >= B43_STAT_INITIALIZED) {
4298 err = b43_wireless_core_init(dev);
4302 if (prev_status >= B43_STAT_STARTED) {
4303 err = b43_wireless_core_start(dev);
4305 b43_wireless_core_exit(dev);
4311 wl->current_dev = NULL; /* Failed to init the dev. */
4312 mutex_unlock(&wl->mutex);
4314 b43err(wl, "Controller restart FAILED\n");
4316 b43info(wl, "Controller restarted\n");
4319 static int b43_setup_bands(struct b43_wldev *dev,
4320 bool have_2ghz_phy, bool have_5ghz_phy)
4322 struct ieee80211_hw *hw = dev->wl->hw;
4325 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &b43_band_2GHz;
4326 if (dev->phy.type == B43_PHYTYPE_N) {
4328 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_nphy;
4331 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &b43_band_5GHz_aphy;
4334 dev->phy.supports_2ghz = have_2ghz_phy;
4335 dev->phy.supports_5ghz = have_5ghz_phy;
4340 static void b43_wireless_core_detach(struct b43_wldev *dev)
4342 /* We release firmware that late to not be required to re-request
4343 * is all the time when we reinit the core. */
4344 b43_release_firmware(dev);
4347 static int b43_wireless_core_attach(struct b43_wldev *dev)
4349 struct b43_wl *wl = dev->wl;
4350 struct ssb_bus *bus = dev->dev->bus;
4351 struct pci_dev *pdev = bus->host_pci;
4353 bool have_2ghz_phy = 0, have_5ghz_phy = 0;
4356 /* Do NOT do any device initialization here.
4357 * Do it in wireless_core_init() instead.
4358 * This function is for gathering basic information about the HW, only.
4359 * Also some structs may be set up here. But most likely you want to have
4360 * that in core_init(), too.
4363 err = ssb_bus_powerup(bus, 0);
4365 b43err(wl, "Bus powerup failed\n");
4368 /* Get the PHY type. */
4369 if (dev->dev->id.revision >= 5) {
4372 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
4373 have_2ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_2GHZ_PHY);
4374 have_5ghz_phy = !!(tmshigh & B43_TMSHIGH_HAVE_5GHZ_PHY);
4378 dev->phy.gmode = have_2ghz_phy;
4379 tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
4380 b43_wireless_core_reset(dev, tmp);
4382 err = b43_phy_versioning(dev);
4385 /* Check if this device supports multiband. */
4387 (pdev->device != 0x4312 &&
4388 pdev->device != 0x4319 && pdev->device != 0x4324)) {
4389 /* No multiband support. */
4392 switch (dev->phy.type) {
4404 if (dev->phy.type == B43_PHYTYPE_A) {
4406 b43err(wl, "IEEE 802.11a devices are unsupported\n");
4410 if (1 /* disable A-PHY */) {
4411 /* FIXME: For now we disable the A-PHY on multi-PHY devices. */
4412 if (dev->phy.type != B43_PHYTYPE_N) {
4418 dev->phy.gmode = have_2ghz_phy;
4419 tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
4420 b43_wireless_core_reset(dev, tmp);
4422 err = b43_validate_chipaccess(dev);
4425 err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
4429 /* Now set some default "current_dev" */
4430 if (!wl->current_dev)
4431 wl->current_dev = dev;
4432 INIT_WORK(&dev->restart_work, b43_chip_reset);
4434 b43_switch_analog(dev, 0);
4435 ssb_device_disable(dev->dev, 0);
4436 ssb_bus_may_powerdown(bus);
4442 ssb_bus_may_powerdown(bus);
4446 static void b43_one_core_detach(struct ssb_device *dev)
4448 struct b43_wldev *wldev;
4451 /* Do not cancel ieee80211-workqueue based work here.
4452 * See comment in b43_remove(). */
4454 wldev = ssb_get_drvdata(dev);
4456 b43_debugfs_remove_device(wldev);
4457 b43_wireless_core_detach(wldev);
4458 list_del(&wldev->list);
4460 ssb_set_drvdata(dev, NULL);
4464 static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
4466 struct b43_wldev *wldev;
4467 struct pci_dev *pdev;
4470 if (!list_empty(&wl->devlist)) {
4471 /* We are not the first core on this chip. */
4472 pdev = dev->bus->host_pci;
4473 /* Only special chips support more than one wireless
4474 * core, although some of the other chips have more than
4475 * one wireless core as well. Check for this and
4479 ((pdev->device != 0x4321) &&
4480 (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
4481 b43dbg(wl, "Ignoring unconnected 802.11 core\n");
4486 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
4492 b43_set_status(wldev, B43_STAT_UNINIT);
4493 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
4494 tasklet_init(&wldev->isr_tasklet,
4495 (void (*)(unsigned long))b43_interrupt_tasklet,
4496 (unsigned long)wldev);
4497 INIT_LIST_HEAD(&wldev->list);
4499 err = b43_wireless_core_attach(wldev);
4501 goto err_kfree_wldev;
4503 list_add(&wldev->list, &wl->devlist);
4505 ssb_set_drvdata(dev, wldev);
4506 b43_debugfs_add_device(wldev);
4516 #define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice) ( \
4517 (pdev->vendor == PCI_VENDOR_ID_##_vendor) && \
4518 (pdev->device == _device) && \
4519 (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) && \
4520 (pdev->subsystem_device == _subdevice) )
4522 static void b43_sprom_fixup(struct ssb_bus *bus)
4524 struct pci_dev *pdev;
4526 /* boardflags workarounds */
4527 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
4528 bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
4529 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
4530 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
4531 bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
4532 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
4533 if (bus->bustype == SSB_BUSTYPE_PCI) {
4534 pdev = bus->host_pci;
4535 if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
4536 IS_PDEV(pdev, BROADCOM, 0x4320, DELL, 0x0003) ||
4537 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
4538 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
4539 IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013))
4540 bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
4544 static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
4546 struct ieee80211_hw *hw = wl->hw;
4548 ssb_set_devtypedata(dev, NULL);
4549 ieee80211_free_hw(hw);
4552 static int b43_wireless_init(struct ssb_device *dev)
4554 struct ssb_sprom *sprom = &dev->bus->sprom;
4555 struct ieee80211_hw *hw;
4559 b43_sprom_fixup(dev->bus);
4561 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
4563 b43err(NULL, "Could not allocate ieee80211 device\n");
4568 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
4569 IEEE80211_HW_SIGNAL_DBM |
4570 IEEE80211_HW_NOISE_DBM;
4572 hw->queues = b43_modparam_qos ? 4 : 1;
4573 SET_IEEE80211_DEV(hw, dev->dev);
4574 if (is_valid_ether_addr(sprom->et1mac))
4575 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
4577 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
4579 /* Get and initialize struct b43_wl */
4580 wl = hw_to_b43_wl(hw);
4581 memset(wl, 0, sizeof(*wl));
4583 spin_lock_init(&wl->irq_lock);
4584 rwlock_init(&wl->tx_lock);
4585 spin_lock_init(&wl->leds_lock);
4586 spin_lock_init(&wl->shm_lock);
4587 mutex_init(&wl->mutex);
4588 INIT_LIST_HEAD(&wl->devlist);
4589 INIT_WORK(&wl->qos_update_work, b43_qos_update_work);
4590 INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
4591 INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
4593 ssb_set_devtypedata(dev, wl);
4594 b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
4600 static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
4606 wl = ssb_get_devtypedata(dev);
4608 /* Probing the first core. Must setup common struct b43_wl */
4610 err = b43_wireless_init(dev);
4613 wl = ssb_get_devtypedata(dev);
4616 err = b43_one_core_attach(dev, wl);
4618 goto err_wireless_exit;
4621 err = ieee80211_register_hw(wl->hw);
4623 goto err_one_core_detach;
4629 err_one_core_detach:
4630 b43_one_core_detach(dev);
4633 b43_wireless_exit(dev, wl);
4637 static void b43_remove(struct ssb_device *dev)
4639 struct b43_wl *wl = ssb_get_devtypedata(dev);
4640 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4642 /* We must cancel any work here before unregistering from ieee80211,
4643 * as the ieee80211 unreg will destroy the workqueue. */
4644 cancel_work_sync(&wldev->restart_work);
4647 if (wl->current_dev == wldev)
4648 ieee80211_unregister_hw(wl->hw);
4650 b43_one_core_detach(dev);
4652 if (list_empty(&wl->devlist)) {
4653 /* Last core on the chip unregistered.
4654 * We can destroy common struct b43_wl.
4656 b43_wireless_exit(dev, wl);
4660 /* Perform a hardware reset. This can be called from any context. */
4661 void b43_controller_restart(struct b43_wldev *dev, const char *reason)
4663 /* Must avoid requeueing, if we are in shutdown. */
4664 if (b43_status(dev) < B43_STAT_INITIALIZED)
4666 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
4667 queue_work(dev->wl->hw->workqueue, &dev->restart_work);
4672 static int b43_suspend(struct ssb_device *dev, pm_message_t state)
4674 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4675 struct b43_wl *wl = wldev->wl;
4677 b43dbg(wl, "Suspending...\n");
4679 mutex_lock(&wl->mutex);
4680 wldev->suspend_in_progress = true;
4681 wldev->suspend_init_status = b43_status(wldev);
4682 if (wldev->suspend_init_status >= B43_STAT_STARTED)
4683 b43_wireless_core_stop(wldev);
4684 if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
4685 b43_wireless_core_exit(wldev);
4686 mutex_unlock(&wl->mutex);
4688 b43dbg(wl, "Device suspended.\n");
4693 static int b43_resume(struct ssb_device *dev)
4695 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4696 struct b43_wl *wl = wldev->wl;
4699 b43dbg(wl, "Resuming...\n");
4701 mutex_lock(&wl->mutex);
4702 if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
4703 err = b43_wireless_core_init(wldev);
4705 b43err(wl, "Resume failed at core init\n");
4709 if (wldev->suspend_init_status >= B43_STAT_STARTED) {
4710 err = b43_wireless_core_start(wldev);
4712 b43_leds_exit(wldev);
4713 b43_rng_exit(wldev->wl);
4714 b43_wireless_core_exit(wldev);
4715 b43err(wl, "Resume failed at core start\n");
4719 b43dbg(wl, "Device resumed.\n");
4721 wldev->suspend_in_progress = false;
4722 mutex_unlock(&wl->mutex);
4726 #else /* CONFIG_PM */
4727 # define b43_suspend NULL
4728 # define b43_resume NULL
4729 #endif /* CONFIG_PM */
4731 static struct ssb_driver b43_ssb_driver = {
4732 .name = KBUILD_MODNAME,
4733 .id_table = b43_ssb_tbl,
4735 .remove = b43_remove,
4736 .suspend = b43_suspend,
4737 .resume = b43_resume,
4740 static void b43_print_driverinfo(void)
4742 const char *feat_pci = "", *feat_pcmcia = "", *feat_nphy = "",
4743 *feat_leds = "", *feat_rfkill = "";
4745 #ifdef CONFIG_B43_PCI_AUTOSELECT
4748 #ifdef CONFIG_B43_PCMCIA
4751 #ifdef CONFIG_B43_NPHY
4754 #ifdef CONFIG_B43_LEDS
4757 #ifdef CONFIG_B43_RFKILL
4760 printk(KERN_INFO "Broadcom 43xx driver loaded "
4761 "[ Features: %s%s%s%s%s, Firmware-ID: "
4762 B43_SUPPORTED_FIRMWARE_ID " ]\n",
4763 feat_pci, feat_pcmcia, feat_nphy,
4764 feat_leds, feat_rfkill);
4767 static int __init b43_init(void)
4772 err = b43_pcmcia_init();
4775 err = ssb_driver_register(&b43_ssb_driver);
4777 goto err_pcmcia_exit;
4778 b43_print_driverinfo();
4789 static void __exit b43_exit(void)
4791 ssb_driver_unregister(&b43_ssb_driver);
4796 module_init(b43_init)
4797 module_exit(b43_exit)