3 * Broadcom B43legacy wireless driver
5 * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
6 * Copyright (c) 2005-2008 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>
10 * Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
12 * Some parts of the code in this file are derived from the ipw2200
13 * driver Copyright(c) 2003 - 2004 Intel Corporation.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; see the file COPYING. If not, write to
27 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <linux/moduleparam.h>
35 #include <linux/if_arp.h>
36 #include <linux/etherdevice.h>
37 #include <linux/firmware.h>
38 #include <linux/wireless.h>
39 #include <linux/workqueue.h>
40 #include <linux/sched.h>
41 #include <linux/skbuff.h>
42 #include <linux/dma-mapping.h>
44 #include <asm/unaligned.h>
46 #include "b43legacy.h"
57 MODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
58 MODULE_AUTHOR("Martin Langer");
59 MODULE_AUTHOR("Stefano Brivio");
60 MODULE_AUTHOR("Michael Buesch");
61 MODULE_LICENSE("GPL");
63 MODULE_FIRMWARE(B43legacy_SUPPORTED_FIRMWARE_ID);
64 MODULE_FIRMWARE("b43legacy/ucode2.fw");
65 MODULE_FIRMWARE("b43legacy/ucode4.fw");
67 #if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
68 static int modparam_pio;
69 module_param_named(pio, modparam_pio, int, 0444);
70 MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
71 #elif defined(CONFIG_B43LEGACY_DMA)
72 # define modparam_pio 0
73 #elif defined(CONFIG_B43LEGACY_PIO)
74 # define modparam_pio 1
77 static int modparam_bad_frames_preempt;
78 module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
79 MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
82 static char modparam_fwpostfix[16];
83 module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
84 MODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
86 /* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
87 static const struct ssb_device_id b43legacy_ssb_tbl[] = {
88 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
89 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
92 MODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
95 /* Channel and ratetables are shared for all devices.
96 * They can't be const, because ieee80211 puts some precalculated
97 * data in there. This data is the same for all devices, so we don't
98 * get concurrency issues */
99 #define RATETAB_ENT(_rateid, _flags) \
101 .bitrate = B43legacy_RATE_TO_100KBPS(_rateid), \
102 .hw_value = (_rateid), \
106 * NOTE: When changing this, sync with xmit.c's
107 * b43legacy_plcp_get_bitrate_idx_* functions!
109 static struct ieee80211_rate __b43legacy_ratetable[] = {
110 RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
111 RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
112 RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
113 RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
114 RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
115 RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
116 RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
117 RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
118 RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
119 RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
120 RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
121 RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
123 #define b43legacy_b_ratetable (__b43legacy_ratetable + 0)
124 #define b43legacy_b_ratetable_size 4
125 #define b43legacy_g_ratetable (__b43legacy_ratetable + 0)
126 #define b43legacy_g_ratetable_size 12
128 #define CHANTAB_ENT(_chanid, _freq) \
130 .center_freq = (_freq), \
131 .hw_value = (_chanid), \
133 static struct ieee80211_channel b43legacy_bg_chantable[] = {
134 CHANTAB_ENT(1, 2412),
135 CHANTAB_ENT(2, 2417),
136 CHANTAB_ENT(3, 2422),
137 CHANTAB_ENT(4, 2427),
138 CHANTAB_ENT(5, 2432),
139 CHANTAB_ENT(6, 2437),
140 CHANTAB_ENT(7, 2442),
141 CHANTAB_ENT(8, 2447),
142 CHANTAB_ENT(9, 2452),
143 CHANTAB_ENT(10, 2457),
144 CHANTAB_ENT(11, 2462),
145 CHANTAB_ENT(12, 2467),
146 CHANTAB_ENT(13, 2472),
147 CHANTAB_ENT(14, 2484),
150 static struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
151 .channels = b43legacy_bg_chantable,
152 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
153 .bitrates = b43legacy_b_ratetable,
154 .n_bitrates = b43legacy_b_ratetable_size,
157 static struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
158 .channels = b43legacy_bg_chantable,
159 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
160 .bitrates = b43legacy_g_ratetable,
161 .n_bitrates = b43legacy_g_ratetable_size,
164 static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
165 static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
166 static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
167 static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
170 static int b43legacy_ratelimit(struct b43legacy_wl *wl)
172 if (!wl || !wl->current_dev)
174 if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
176 /* We are up and running.
177 * Ratelimit the messages to avoid DoS over the net. */
178 return net_ratelimit();
181 void b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
185 if (!b43legacy_ratelimit(wl))
188 printk(KERN_INFO "b43legacy-%s: ",
189 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
194 void b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
198 if (!b43legacy_ratelimit(wl))
201 printk(KERN_ERR "b43legacy-%s ERROR: ",
202 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
207 void b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
211 if (!b43legacy_ratelimit(wl))
214 printk(KERN_WARNING "b43legacy-%s warning: ",
215 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
221 void b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
226 printk(KERN_DEBUG "b43legacy-%s debug: ",
227 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
233 static void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
238 B43legacy_WARN_ON(offset % 4 != 0);
240 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
241 if (status & B43legacy_MACCTL_BE)
244 b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
246 b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
250 void b43legacy_shm_control_word(struct b43legacy_wldev *dev,
251 u16 routing, u16 offset)
255 /* "offset" is the WORD offset. */
260 b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
263 u32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
264 u16 routing, u16 offset)
268 if (routing == B43legacy_SHM_SHARED) {
269 B43legacy_WARN_ON((offset & 0x0001) != 0);
270 if (offset & 0x0003) {
271 /* Unaligned access */
272 b43legacy_shm_control_word(dev, routing, offset >> 2);
273 ret = b43legacy_read16(dev,
274 B43legacy_MMIO_SHM_DATA_UNALIGNED);
276 b43legacy_shm_control_word(dev, routing,
278 ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
284 b43legacy_shm_control_word(dev, routing, offset);
285 ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
290 u16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
291 u16 routing, u16 offset)
295 if (routing == B43legacy_SHM_SHARED) {
296 B43legacy_WARN_ON((offset & 0x0001) != 0);
297 if (offset & 0x0003) {
298 /* Unaligned access */
299 b43legacy_shm_control_word(dev, routing, offset >> 2);
300 ret = b43legacy_read16(dev,
301 B43legacy_MMIO_SHM_DATA_UNALIGNED);
307 b43legacy_shm_control_word(dev, routing, offset);
308 ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
313 void b43legacy_shm_write32(struct b43legacy_wldev *dev,
314 u16 routing, u16 offset,
317 if (routing == B43legacy_SHM_SHARED) {
318 B43legacy_WARN_ON((offset & 0x0001) != 0);
319 if (offset & 0x0003) {
320 /* Unaligned access */
321 b43legacy_shm_control_word(dev, routing, offset >> 2);
323 b43legacy_write16(dev,
324 B43legacy_MMIO_SHM_DATA_UNALIGNED,
325 (value >> 16) & 0xffff);
327 b43legacy_shm_control_word(dev, routing,
330 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
336 b43legacy_shm_control_word(dev, routing, offset);
338 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
341 void b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
344 if (routing == B43legacy_SHM_SHARED) {
345 B43legacy_WARN_ON((offset & 0x0001) != 0);
346 if (offset & 0x0003) {
347 /* Unaligned access */
348 b43legacy_shm_control_word(dev, routing, offset >> 2);
350 b43legacy_write16(dev,
351 B43legacy_MMIO_SHM_DATA_UNALIGNED,
357 b43legacy_shm_control_word(dev, routing, offset);
359 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
363 u32 b43legacy_hf_read(struct b43legacy_wldev *dev)
367 ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
368 B43legacy_SHM_SH_HOSTFHI);
370 ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
371 B43legacy_SHM_SH_HOSTFLO);
376 /* Write HostFlags */
377 void b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
379 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
380 B43legacy_SHM_SH_HOSTFLO,
381 (value & 0x0000FFFF));
382 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
383 B43legacy_SHM_SH_HOSTFHI,
384 ((value & 0xFFFF0000) >> 16));
387 void b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
389 /* We need to be careful. As we read the TSF from multiple
390 * registers, we should take care of register overflows.
391 * In theory, the whole tsf read process should be atomic.
392 * We try to be atomic here, by restaring the read process,
393 * if any of the high registers changed (overflew).
395 if (dev->dev->id.revision >= 3) {
401 high = b43legacy_read32(dev,
402 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
403 low = b43legacy_read32(dev,
404 B43legacy_MMIO_REV3PLUS_TSF_LOW);
405 high2 = b43legacy_read32(dev,
406 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
407 } while (unlikely(high != high2));
423 v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
424 v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
425 v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
426 v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
428 test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
429 test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
430 test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
431 } while (v3 != test3 || v2 != test2 || v1 != test1);
445 static void b43legacy_time_lock(struct b43legacy_wldev *dev)
449 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
450 status |= B43legacy_MACCTL_TBTTHOLD;
451 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
455 static void b43legacy_time_unlock(struct b43legacy_wldev *dev)
459 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
460 status &= ~B43legacy_MACCTL_TBTTHOLD;
461 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
464 static void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
466 /* Be careful with the in-progress timer.
467 * First zero out the low register, so we have a full
468 * register-overflow duration to complete the operation.
470 if (dev->dev->id.revision >= 3) {
471 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
472 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
474 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
476 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
479 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
482 u16 v0 = (tsf & 0x000000000000FFFFULL);
483 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
484 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
485 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
487 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
489 b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
491 b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
493 b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
495 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
499 void b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
501 b43legacy_time_lock(dev);
502 b43legacy_tsf_write_locked(dev, tsf);
503 b43legacy_time_unlock(dev);
507 void b43legacy_macfilter_set(struct b43legacy_wldev *dev,
508 u16 offset, const u8 *mac)
510 static const u8 zero_addr[ETH_ALEN] = { 0 };
517 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
521 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
524 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
527 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
530 static void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
532 static const u8 zero_addr[ETH_ALEN] = { 0 };
533 const u8 *mac = dev->wl->mac_addr;
534 const u8 *bssid = dev->wl->bssid;
535 u8 mac_bssid[ETH_ALEN * 2];
544 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
546 memcpy(mac_bssid, mac, ETH_ALEN);
547 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
549 /* Write our MAC address and BSSID to template ram */
550 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
551 tmp = (u32)(mac_bssid[i + 0]);
552 tmp |= (u32)(mac_bssid[i + 1]) << 8;
553 tmp |= (u32)(mac_bssid[i + 2]) << 16;
554 tmp |= (u32)(mac_bssid[i + 3]) << 24;
555 b43legacy_ram_write(dev, 0x20 + i, tmp);
556 b43legacy_ram_write(dev, 0x78 + i, tmp);
557 b43legacy_ram_write(dev, 0x478 + i, tmp);
561 static void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
563 b43legacy_write_mac_bssid_templates(dev);
564 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
568 static void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
571 /* slot_time is in usec. */
572 if (dev->phy.type != B43legacy_PHYTYPE_G)
574 b43legacy_write16(dev, 0x684, 510 + slot_time);
575 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
579 static void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
581 b43legacy_set_slot_time(dev, 9);
584 static void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
586 b43legacy_set_slot_time(dev, 20);
589 /* Synchronize IRQ top- and bottom-half.
590 * IRQs must be masked before calling this.
591 * This must not be called with the irq_lock held.
593 static void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
595 synchronize_irq(dev->dev->irq);
596 tasklet_kill(&dev->isr_tasklet);
599 /* DummyTransmission function, as documented on
600 * http://bcm-specs.sipsolutions.net/DummyTransmission
602 void b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
604 struct b43legacy_phy *phy = &dev->phy;
606 unsigned int max_loop;
617 case B43legacy_PHYTYPE_B:
618 case B43legacy_PHYTYPE_G:
620 buffer[0] = 0x000B846E;
627 for (i = 0; i < 5; i++)
628 b43legacy_ram_write(dev, i * 4, buffer[i]);
630 /* dummy read follows */
631 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
633 b43legacy_write16(dev, 0x0568, 0x0000);
634 b43legacy_write16(dev, 0x07C0, 0x0000);
635 b43legacy_write16(dev, 0x050C, 0x0000);
636 b43legacy_write16(dev, 0x0508, 0x0000);
637 b43legacy_write16(dev, 0x050A, 0x0000);
638 b43legacy_write16(dev, 0x054C, 0x0000);
639 b43legacy_write16(dev, 0x056A, 0x0014);
640 b43legacy_write16(dev, 0x0568, 0x0826);
641 b43legacy_write16(dev, 0x0500, 0x0000);
642 b43legacy_write16(dev, 0x0502, 0x0030);
644 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
645 b43legacy_radio_write16(dev, 0x0051, 0x0017);
646 for (i = 0x00; i < max_loop; i++) {
647 value = b43legacy_read16(dev, 0x050E);
652 for (i = 0x00; i < 0x0A; i++) {
653 value = b43legacy_read16(dev, 0x050E);
658 for (i = 0x00; i < 0x0A; i++) {
659 value = b43legacy_read16(dev, 0x0690);
660 if (!(value & 0x0100))
664 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
665 b43legacy_radio_write16(dev, 0x0051, 0x0037);
668 /* Turn the Analog ON/OFF */
669 static void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
671 b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
674 void b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
679 flags |= B43legacy_TMSLOW_PHYCLKEN;
680 flags |= B43legacy_TMSLOW_PHYRESET;
681 ssb_device_enable(dev->dev, flags);
682 msleep(2); /* Wait for the PLL to turn on. */
684 /* Now take the PHY out of Reset again */
685 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
686 tmslow |= SSB_TMSLOW_FGC;
687 tmslow &= ~B43legacy_TMSLOW_PHYRESET;
688 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
689 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
691 tmslow &= ~SSB_TMSLOW_FGC;
692 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
693 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
697 b43legacy_switch_analog(dev, 1);
699 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
700 macctl &= ~B43legacy_MACCTL_GMODE;
701 if (flags & B43legacy_TMSLOW_GMODE) {
702 macctl |= B43legacy_MACCTL_GMODE;
706 macctl |= B43legacy_MACCTL_IHR_ENABLED;
707 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
710 static void handle_irq_transmit_status(struct b43legacy_wldev *dev)
715 struct b43legacy_txstatus stat;
718 v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
719 if (!(v0 & 0x00000001))
721 v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
723 stat.cookie = (v0 >> 16);
724 stat.seq = (v1 & 0x0000FFFF);
725 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
726 tmp = (v0 & 0x0000FFFF);
727 stat.frame_count = ((tmp & 0xF000) >> 12);
728 stat.rts_count = ((tmp & 0x0F00) >> 8);
729 stat.supp_reason = ((tmp & 0x001C) >> 2);
730 stat.pm_indicated = !!(tmp & 0x0080);
731 stat.intermediate = !!(tmp & 0x0040);
732 stat.for_ampdu = !!(tmp & 0x0020);
733 stat.acked = !!(tmp & 0x0002);
735 b43legacy_handle_txstatus(dev, &stat);
739 static void drain_txstatus_queue(struct b43legacy_wldev *dev)
743 if (dev->dev->id.revision < 5)
745 /* Read all entries from the microcode TXstatus FIFO
746 * and throw them away.
749 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
750 if (!(dummy & 0x00000001))
752 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
756 static u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
760 val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
762 val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
767 static void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
769 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
770 (jssi & 0x0000FFFF));
771 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
772 (jssi & 0xFFFF0000) >> 16);
775 static void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
777 b43legacy_jssi_write(dev, 0x7F7F7F7F);
778 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
779 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
780 | B43legacy_MACCMD_BGNOISE);
781 B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
785 static void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
787 /* Top half of Link Quality calculation. */
789 if (dev->noisecalc.calculation_running)
791 dev->noisecalc.channel_at_start = dev->phy.channel;
792 dev->noisecalc.calculation_running = 1;
793 dev->noisecalc.nr_samples = 0;
795 b43legacy_generate_noise_sample(dev);
798 static void handle_irq_noise(struct b43legacy_wldev *dev)
800 struct b43legacy_phy *phy = &dev->phy;
807 /* Bottom half of Link Quality calculation. */
809 B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
810 if (dev->noisecalc.channel_at_start != phy->channel)
811 goto drop_calculation;
812 *((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
813 if (noise[0] == 0x7F || noise[1] == 0x7F ||
814 noise[2] == 0x7F || noise[3] == 0x7F)
817 /* Get the noise samples. */
818 B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
819 i = dev->noisecalc.nr_samples;
820 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
821 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
822 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
823 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
824 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
825 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
826 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
827 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
828 dev->noisecalc.nr_samples++;
829 if (dev->noisecalc.nr_samples == 8) {
830 /* Calculate the Link Quality by the noise samples. */
832 for (i = 0; i < 8; i++) {
833 for (j = 0; j < 4; j++)
834 average += dev->noisecalc.samples[i][j];
840 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
842 tmp = (tmp / 128) & 0x1F;
852 dev->stats.link_noise = average;
854 dev->noisecalc.calculation_running = 0;
858 b43legacy_generate_noise_sample(dev);
861 static void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
863 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
866 if (1/*FIXME: the last PSpoll frame was sent successfully */)
867 b43legacy_power_saving_ctl_bits(dev, -1, -1);
869 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
873 static void handle_irq_atim_end(struct b43legacy_wldev *dev)
875 if (dev->dfq_valid) {
876 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
877 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
878 | B43legacy_MACCMD_DFQ_VALID);
883 static void handle_irq_pmq(struct b43legacy_wldev *dev)
890 tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
891 if (!(tmp & 0x00000008))
894 /* 16bit write is odd, but correct. */
895 b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
898 static void b43legacy_write_template_common(struct b43legacy_wldev *dev,
899 const u8 *data, u16 size,
901 u16 shm_size_offset, u8 rate)
905 struct b43legacy_plcp_hdr4 plcp;
908 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
909 b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
910 ram_offset += sizeof(u32);
911 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
912 * So leave the first two bytes of the next write blank.
914 tmp = (u32)(data[0]) << 16;
915 tmp |= (u32)(data[1]) << 24;
916 b43legacy_ram_write(dev, ram_offset, tmp);
917 ram_offset += sizeof(u32);
918 for (i = 2; i < size; i += sizeof(u32)) {
919 tmp = (u32)(data[i + 0]);
921 tmp |= (u32)(data[i + 1]) << 8;
923 tmp |= (u32)(data[i + 2]) << 16;
925 tmp |= (u32)(data[i + 3]) << 24;
926 b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
928 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
929 size + sizeof(struct b43legacy_plcp_hdr6));
932 /* Convert a b43legacy antenna number value to the PHY TX control value. */
933 static u16 b43legacy_antenna_to_phyctl(int antenna)
936 case B43legacy_ANTENNA0:
937 return B43legacy_TX4_PHY_ANT0;
938 case B43legacy_ANTENNA1:
939 return B43legacy_TX4_PHY_ANT1;
941 return B43legacy_TX4_PHY_ANTLAST;
944 static void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
949 unsigned int i, len, variable_len;
950 const struct ieee80211_mgmt *bcn;
956 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
958 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
959 len = min((size_t)dev->wl->current_beacon->len,
960 0x200 - sizeof(struct b43legacy_plcp_hdr6));
961 rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
963 b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
964 shm_size_offset, rate);
966 /* Write the PHY TX control parameters. */
967 antenna = B43legacy_ANTENNA_DEFAULT;
968 antenna = b43legacy_antenna_to_phyctl(antenna);
969 ctl = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
970 B43legacy_SHM_SH_BEACPHYCTL);
971 /* We can't send beacons with short preamble. Would get PHY errors. */
972 ctl &= ~B43legacy_TX4_PHY_SHORTPRMBL;
973 ctl &= ~B43legacy_TX4_PHY_ANT;
974 ctl &= ~B43legacy_TX4_PHY_ENC;
976 ctl |= B43legacy_TX4_PHY_ENC_CCK;
977 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
978 B43legacy_SHM_SH_BEACPHYCTL, ctl);
980 /* Find the position of the TIM and the DTIM_period value
981 * and write them to SHM. */
982 ie = bcn->u.beacon.variable;
983 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
984 for (i = 0; i < variable_len - 2; ) {
985 uint8_t ie_id, ie_len;
992 /* This is the TIM Information Element */
994 /* Check whether the ie_len is in the beacon data range. */
995 if (variable_len < ie_len + 2 + i)
997 /* A valid TIM is at least 4 bytes long. */
1002 tim_position = sizeof(struct b43legacy_plcp_hdr6);
1003 tim_position += offsetof(struct ieee80211_mgmt,
1007 dtim_period = ie[i + 3];
1009 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1010 B43legacy_SHM_SH_TIMPOS, tim_position);
1011 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1012 B43legacy_SHM_SH_DTIMP, dtim_period);
1018 b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
1019 "beacon template packet. AP or IBSS operation "
1020 "may be broken.\n");
1022 b43legacydbg(dev->wl, "Updated beacon template\n");
1025 static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
1026 u16 shm_offset, u16 size,
1027 struct ieee80211_rate *rate)
1029 struct b43legacy_plcp_hdr4 plcp;
1034 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
1035 dur = ieee80211_generic_frame_duration(dev->wl->hw,
1039 /* Write PLCP in two parts and timing for packet transfer */
1040 tmp = le32_to_cpu(plcp.data);
1041 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
1043 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
1045 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
1049 /* Instead of using custom probe response template, this function
1050 * just patches custom beacon template by:
1051 * 1) Changing packet type
1052 * 2) Patching duration field
1055 static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
1057 struct ieee80211_rate *rate)
1061 u16 src_size, elem_size, src_pos, dest_pos;
1063 struct ieee80211_hdr *hdr;
1066 src_size = dev->wl->current_beacon->len;
1067 src_data = (const u8 *)dev->wl->current_beacon->data;
1069 /* Get the start offset of the variable IEs in the packet. */
1070 ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
1071 B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
1072 u.beacon.variable));
1074 if (B43legacy_WARN_ON(src_size < ie_start))
1077 dest_data = kmalloc(src_size, GFP_ATOMIC);
1078 if (unlikely(!dest_data))
1081 /* Copy the static data and all Information Elements, except the TIM. */
1082 memcpy(dest_data, src_data, ie_start);
1084 dest_pos = ie_start;
1085 for ( ; src_pos < src_size - 2; src_pos += elem_size) {
1086 elem_size = src_data[src_pos + 1] + 2;
1087 if (src_data[src_pos] == 5) {
1088 /* This is the TIM. */
1091 memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
1092 dest_pos += elem_size;
1094 *dest_size = dest_pos;
1095 hdr = (struct ieee80211_hdr *)dest_data;
1097 /* Set the frame control. */
1098 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1099 IEEE80211_STYPE_PROBE_RESP);
1100 dur = ieee80211_generic_frame_duration(dev->wl->hw,
1104 hdr->duration_id = dur;
1109 static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
1111 u16 shm_size_offset,
1112 struct ieee80211_rate *rate)
1114 const u8 *probe_resp_data;
1117 size = dev->wl->current_beacon->len;
1118 probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
1119 if (unlikely(!probe_resp_data))
1122 /* Looks like PLCP headers plus packet timings are stored for
1123 * all possible basic rates
1125 b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
1126 &b43legacy_b_ratetable[0]);
1127 b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
1128 &b43legacy_b_ratetable[1]);
1129 b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
1130 &b43legacy_b_ratetable[2]);
1131 b43legacy_write_probe_resp_plcp(dev, 0x350, size,
1132 &b43legacy_b_ratetable[3]);
1134 size = min((size_t)size,
1135 0x200 - sizeof(struct b43legacy_plcp_hdr6));
1136 b43legacy_write_template_common(dev, probe_resp_data,
1138 shm_size_offset, rate->hw_value);
1139 kfree(probe_resp_data);
1142 static void b43legacy_upload_beacon0(struct b43legacy_wldev *dev)
1144 struct b43legacy_wl *wl = dev->wl;
1146 if (wl->beacon0_uploaded)
1148 b43legacy_write_beacon_template(dev, 0x68, 0x18);
1149 /* FIXME: Probe resp upload doesn't really belong here,
1150 * but we don't use that feature anyway. */
1151 b43legacy_write_probe_resp_template(dev, 0x268, 0x4A,
1152 &__b43legacy_ratetable[3]);
1153 wl->beacon0_uploaded = 1;
1156 static void b43legacy_upload_beacon1(struct b43legacy_wldev *dev)
1158 struct b43legacy_wl *wl = dev->wl;
1160 if (wl->beacon1_uploaded)
1162 b43legacy_write_beacon_template(dev, 0x468, 0x1A);
1163 wl->beacon1_uploaded = 1;
1166 static void handle_irq_beacon(struct b43legacy_wldev *dev)
1168 struct b43legacy_wl *wl = dev->wl;
1169 u32 cmd, beacon0_valid, beacon1_valid;
1171 if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
1174 /* This is the bottom half of the asynchronous beacon update. */
1176 /* Ignore interrupt in the future. */
1177 dev->irq_mask &= ~B43legacy_IRQ_BEACON;
1179 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1180 beacon0_valid = (cmd & B43legacy_MACCMD_BEACON0_VALID);
1181 beacon1_valid = (cmd & B43legacy_MACCMD_BEACON1_VALID);
1183 /* Schedule interrupt manually, if busy. */
1184 if (beacon0_valid && beacon1_valid) {
1185 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, B43legacy_IRQ_BEACON);
1186 dev->irq_mask |= B43legacy_IRQ_BEACON;
1190 if (unlikely(wl->beacon_templates_virgin)) {
1191 /* We never uploaded a beacon before.
1192 * Upload both templates now, but only mark one valid. */
1193 wl->beacon_templates_virgin = 0;
1194 b43legacy_upload_beacon0(dev);
1195 b43legacy_upload_beacon1(dev);
1196 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1197 cmd |= B43legacy_MACCMD_BEACON0_VALID;
1198 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
1200 if (!beacon0_valid) {
1201 b43legacy_upload_beacon0(dev);
1202 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1203 cmd |= B43legacy_MACCMD_BEACON0_VALID;
1204 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
1205 } else if (!beacon1_valid) {
1206 b43legacy_upload_beacon1(dev);
1207 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1208 cmd |= B43legacy_MACCMD_BEACON1_VALID;
1209 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
1214 static void b43legacy_beacon_update_trigger_work(struct work_struct *work)
1216 struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
1217 beacon_update_trigger);
1218 struct b43legacy_wldev *dev;
1220 mutex_lock(&wl->mutex);
1221 dev = wl->current_dev;
1222 if (likely(dev && (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED))) {
1223 spin_lock_irq(&wl->irq_lock);
1224 /* Update beacon right away or defer to IRQ. */
1225 handle_irq_beacon(dev);
1226 /* The handler might have updated the IRQ mask. */
1227 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
1230 spin_unlock_irq(&wl->irq_lock);
1232 mutex_unlock(&wl->mutex);
1235 /* Asynchronously update the packet templates in template RAM.
1236 * Locking: Requires wl->irq_lock to be locked. */
1237 static void b43legacy_update_templates(struct b43legacy_wl *wl)
1239 struct sk_buff *beacon;
1240 /* This is the top half of the ansynchronous beacon update. The bottom
1241 * half is the beacon IRQ. Beacon update must be asynchronous to avoid
1242 * sending an invalid beacon. This can happen for example, if the
1243 * firmware transmits a beacon while we are updating it. */
1245 /* We could modify the existing beacon and set the aid bit in the TIM
1246 * field, but that would probably require resizing and moving of data
1247 * within the beacon template. Simply request a new beacon and let
1248 * mac80211 do the hard work. */
1249 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1250 if (unlikely(!beacon))
1253 if (wl->current_beacon)
1254 dev_kfree_skb_any(wl->current_beacon);
1255 wl->current_beacon = beacon;
1256 wl->beacon0_uploaded = 0;
1257 wl->beacon1_uploaded = 0;
1258 ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
1261 static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
1264 b43legacy_time_lock(dev);
1265 if (dev->dev->id.revision >= 3) {
1266 b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_REP,
1267 (beacon_int << 16));
1268 b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_START,
1269 (beacon_int << 10));
1271 b43legacy_write16(dev, 0x606, (beacon_int >> 6));
1272 b43legacy_write16(dev, 0x610, beacon_int);
1274 b43legacy_time_unlock(dev);
1275 b43legacydbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
1278 static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
1282 /* Interrupt handler bottom-half */
1283 static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
1286 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1287 u32 merged_dma_reason = 0;
1289 unsigned long flags;
1291 spin_lock_irqsave(&dev->wl->irq_lock, flags);
1293 B43legacy_WARN_ON(b43legacy_status(dev) <
1294 B43legacy_STAT_INITIALIZED);
1296 reason = dev->irq_reason;
1297 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1298 dma_reason[i] = dev->dma_reason[i];
1299 merged_dma_reason |= dma_reason[i];
1302 if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
1303 b43legacyerr(dev->wl, "MAC transmission error\n");
1305 if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
1306 b43legacyerr(dev->wl, "PHY transmission error\n");
1308 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1309 b43legacyerr(dev->wl, "Too many PHY TX errors, "
1310 "restarting the controller\n");
1311 b43legacy_controller_restart(dev, "PHY TX errors");
1315 if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
1316 B43legacy_DMAIRQ_NONFATALMASK))) {
1317 if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
1318 b43legacyerr(dev->wl, "Fatal DMA error: "
1319 "0x%08X, 0x%08X, 0x%08X, "
1320 "0x%08X, 0x%08X, 0x%08X\n",
1321 dma_reason[0], dma_reason[1],
1322 dma_reason[2], dma_reason[3],
1323 dma_reason[4], dma_reason[5]);
1324 b43legacy_controller_restart(dev, "DMA error");
1326 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1329 if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
1330 b43legacyerr(dev->wl, "DMA error: "
1331 "0x%08X, 0x%08X, 0x%08X, "
1332 "0x%08X, 0x%08X, 0x%08X\n",
1333 dma_reason[0], dma_reason[1],
1334 dma_reason[2], dma_reason[3],
1335 dma_reason[4], dma_reason[5]);
1338 if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
1339 handle_irq_ucode_debug(dev);
1340 if (reason & B43legacy_IRQ_TBTT_INDI)
1341 handle_irq_tbtt_indication(dev);
1342 if (reason & B43legacy_IRQ_ATIM_END)
1343 handle_irq_atim_end(dev);
1344 if (reason & B43legacy_IRQ_BEACON)
1345 handle_irq_beacon(dev);
1346 if (reason & B43legacy_IRQ_PMQ)
1347 handle_irq_pmq(dev);
1348 if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
1350 if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
1351 handle_irq_noise(dev);
1353 /* Check the DMA reason registers for received data. */
1354 if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
1355 if (b43legacy_using_pio(dev))
1356 b43legacy_pio_rx(dev->pio.queue0);
1358 b43legacy_dma_rx(dev->dma.rx_ring0);
1360 B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
1361 B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
1362 if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
1363 if (b43legacy_using_pio(dev))
1364 b43legacy_pio_rx(dev->pio.queue3);
1366 b43legacy_dma_rx(dev->dma.rx_ring3);
1368 B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
1369 B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
1371 if (reason & B43legacy_IRQ_TX_OK)
1372 handle_irq_transmit_status(dev);
1374 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
1376 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1379 static void pio_irq_workaround(struct b43legacy_wldev *dev,
1380 u16 base, int queueidx)
1384 rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
1385 if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
1386 dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
1388 dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
1391 static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
1393 if (b43legacy_using_pio(dev) &&
1394 (dev->dev->id.revision < 3) &&
1395 (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
1396 /* Apply a PIO specific workaround to the dma_reasons */
1397 pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
1398 pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
1399 pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
1400 pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
1403 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
1405 b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
1406 dev->dma_reason[0]);
1407 b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
1408 dev->dma_reason[1]);
1409 b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
1410 dev->dma_reason[2]);
1411 b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
1412 dev->dma_reason[3]);
1413 b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
1414 dev->dma_reason[4]);
1415 b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
1416 dev->dma_reason[5]);
1419 /* Interrupt handler top-half */
1420 static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
1422 irqreturn_t ret = IRQ_NONE;
1423 struct b43legacy_wldev *dev = dev_id;
1426 B43legacy_WARN_ON(!dev);
1428 spin_lock(&dev->wl->irq_lock);
1430 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
1431 /* This can only happen on shared IRQ lines. */
1433 reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1434 if (reason == 0xffffffff) /* shared IRQ */
1437 reason &= dev->irq_mask;
1441 dev->dma_reason[0] = b43legacy_read32(dev,
1442 B43legacy_MMIO_DMA0_REASON)
1444 dev->dma_reason[1] = b43legacy_read32(dev,
1445 B43legacy_MMIO_DMA1_REASON)
1447 dev->dma_reason[2] = b43legacy_read32(dev,
1448 B43legacy_MMIO_DMA2_REASON)
1450 dev->dma_reason[3] = b43legacy_read32(dev,
1451 B43legacy_MMIO_DMA3_REASON)
1453 dev->dma_reason[4] = b43legacy_read32(dev,
1454 B43legacy_MMIO_DMA4_REASON)
1456 dev->dma_reason[5] = b43legacy_read32(dev,
1457 B43legacy_MMIO_DMA5_REASON)
1460 b43legacy_interrupt_ack(dev, reason);
1461 /* Disable all IRQs. They are enabled again in the bottom half. */
1462 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
1463 /* Save the reason code and call our bottom half. */
1464 dev->irq_reason = reason;
1465 tasklet_schedule(&dev->isr_tasklet);
1468 spin_unlock(&dev->wl->irq_lock);
1473 static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
1475 release_firmware(dev->fw.ucode);
1476 dev->fw.ucode = NULL;
1477 release_firmware(dev->fw.pcm);
1479 release_firmware(dev->fw.initvals);
1480 dev->fw.initvals = NULL;
1481 release_firmware(dev->fw.initvals_band);
1482 dev->fw.initvals_band = NULL;
1485 static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
1487 b43legacyerr(wl, "You must go to http://linuxwireless.org/en/users/"
1488 "Drivers/b43#devicefirmware "
1489 "and download the correct firmware (version 3).\n");
1492 static int do_request_fw(struct b43legacy_wldev *dev,
1494 const struct firmware **fw)
1496 char path[sizeof(modparam_fwpostfix) + 32];
1497 struct b43legacy_fw_header *hdr;
1504 snprintf(path, ARRAY_SIZE(path),
1505 "b43legacy%s/%s.fw",
1506 modparam_fwpostfix, name);
1507 err = request_firmware(fw, path, dev->dev->dev);
1509 b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
1510 "or load failed.\n", path);
1513 if ((*fw)->size < sizeof(struct b43legacy_fw_header))
1515 hdr = (struct b43legacy_fw_header *)((*fw)->data);
1516 switch (hdr->type) {
1517 case B43legacy_FW_TYPE_UCODE:
1518 case B43legacy_FW_TYPE_PCM:
1519 size = be32_to_cpu(hdr->size);
1520 if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
1523 case B43legacy_FW_TYPE_IV:
1534 b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
1538 static int b43legacy_request_firmware(struct b43legacy_wldev *dev)
1540 struct b43legacy_firmware *fw = &dev->fw;
1541 const u8 rev = dev->dev->id.revision;
1542 const char *filename;
1546 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
1549 filename = "ucode2";
1551 filename = "ucode4";
1553 filename = "ucode5";
1554 err = do_request_fw(dev, filename, &fw->ucode);
1563 err = do_request_fw(dev, filename, &fw->pcm);
1567 if (!fw->initvals) {
1568 switch (dev->phy.type) {
1569 case B43legacy_PHYTYPE_B:
1570 case B43legacy_PHYTYPE_G:
1571 if ((rev >= 5) && (rev <= 10))
1572 filename = "b0g0initvals5";
1573 else if (rev == 2 || rev == 4)
1574 filename = "b0g0initvals2";
1576 goto err_no_initvals;
1579 goto err_no_initvals;
1581 err = do_request_fw(dev, filename, &fw->initvals);
1585 if (!fw->initvals_band) {
1586 switch (dev->phy.type) {
1587 case B43legacy_PHYTYPE_B:
1588 case B43legacy_PHYTYPE_G:
1589 if ((rev >= 5) && (rev <= 10))
1590 filename = "b0g0bsinitvals5";
1593 else if (rev == 2 || rev == 4)
1596 goto err_no_initvals;
1599 goto err_no_initvals;
1601 err = do_request_fw(dev, filename, &fw->initvals_band);
1609 b43legacy_print_fw_helptext(dev->wl);
1614 b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
1615 "core rev %u\n", dev->phy.type, rev);
1619 b43legacy_release_firmware(dev);
1623 static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
1625 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1636 /* Jump the microcode PSM to offset 0 */
1637 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1638 B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
1639 macctl |= B43legacy_MACCTL_PSM_JMP0;
1640 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1641 /* Zero out all microcode PSM registers and shared memory. */
1642 for (i = 0; i < 64; i++)
1643 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
1644 for (i = 0; i < 4096; i += 2)
1645 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
1647 /* Upload Microcode. */
1648 data = (__be32 *) (dev->fw.ucode->data + hdr_len);
1649 len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
1650 b43legacy_shm_control_word(dev,
1651 B43legacy_SHM_UCODE |
1652 B43legacy_SHM_AUTOINC_W,
1654 for (i = 0; i < len; i++) {
1655 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1656 be32_to_cpu(data[i]));
1661 /* Upload PCM data. */
1662 data = (__be32 *) (dev->fw.pcm->data + hdr_len);
1663 len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
1664 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
1665 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
1666 /* No need for autoinc bit in SHM_HW */
1667 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
1668 for (i = 0; i < len; i++) {
1669 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1670 be32_to_cpu(data[i]));
1675 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1678 /* Start the microcode PSM */
1679 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1680 macctl &= ~B43legacy_MACCTL_PSM_JMP0;
1681 macctl |= B43legacy_MACCTL_PSM_RUN;
1682 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1684 /* Wait for the microcode to load and respond */
1687 tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1688 if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
1691 if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
1692 b43legacyerr(dev->wl, "Microcode not responding\n");
1693 b43legacy_print_fw_helptext(dev->wl);
1697 msleep_interruptible(50);
1698 if (signal_pending(current)) {
1703 /* dummy read follows */
1704 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1706 /* Get and check the revisions. */
1707 fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1708 B43legacy_SHM_SH_UCODEREV);
1709 fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1710 B43legacy_SHM_SH_UCODEPATCH);
1711 fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1712 B43legacy_SHM_SH_UCODEDATE);
1713 fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1714 B43legacy_SHM_SH_UCODETIME);
1716 if (fwrev > 0x128) {
1717 b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
1718 " Only firmware from binary drivers version 3.x"
1719 " is supported. You must change your firmware"
1721 b43legacy_print_fw_helptext(dev->wl);
1725 b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
1726 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
1727 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
1728 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
1731 dev->fw.rev = fwrev;
1732 dev->fw.patch = fwpatch;
1737 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1738 macctl &= ~B43legacy_MACCTL_PSM_RUN;
1739 macctl |= B43legacy_MACCTL_PSM_JMP0;
1740 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1745 static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
1746 const struct b43legacy_iv *ivals,
1750 const struct b43legacy_iv *iv;
1755 BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
1757 for (i = 0; i < count; i++) {
1758 if (array_size < sizeof(iv->offset_size))
1760 array_size -= sizeof(iv->offset_size);
1761 offset = be16_to_cpu(iv->offset_size);
1762 bit32 = !!(offset & B43legacy_IV_32BIT);
1763 offset &= B43legacy_IV_OFFSET_MASK;
1764 if (offset >= 0x1000)
1769 if (array_size < sizeof(iv->data.d32))
1771 array_size -= sizeof(iv->data.d32);
1773 value = get_unaligned_be32(&iv->data.d32);
1774 b43legacy_write32(dev, offset, value);
1776 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1782 if (array_size < sizeof(iv->data.d16))
1784 array_size -= sizeof(iv->data.d16);
1786 value = be16_to_cpu(iv->data.d16);
1787 b43legacy_write16(dev, offset, value);
1789 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1800 b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
1801 b43legacy_print_fw_helptext(dev->wl);
1806 static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
1808 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1809 const struct b43legacy_fw_header *hdr;
1810 struct b43legacy_firmware *fw = &dev->fw;
1811 const struct b43legacy_iv *ivals;
1815 hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
1816 ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
1817 count = be32_to_cpu(hdr->size);
1818 err = b43legacy_write_initvals(dev, ivals, count,
1819 fw->initvals->size - hdr_len);
1822 if (fw->initvals_band) {
1823 hdr = (const struct b43legacy_fw_header *)
1824 (fw->initvals_band->data);
1825 ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
1827 count = be32_to_cpu(hdr->size);
1828 err = b43legacy_write_initvals(dev, ivals, count,
1829 fw->initvals_band->size - hdr_len);
1838 /* Initialize the GPIOs
1839 * http://bcm-specs.sipsolutions.net/GPIO
1841 static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
1843 struct ssb_bus *bus = dev->dev->bus;
1844 struct ssb_device *gpiodev, *pcidev = NULL;
1848 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
1849 b43legacy_read32(dev,
1850 B43legacy_MMIO_MACCTL)
1853 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1854 b43legacy_read16(dev,
1855 B43legacy_MMIO_GPIO_MASK)
1860 if (dev->dev->bus->chip_id == 0x4301) {
1864 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
1865 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1866 b43legacy_read16(dev,
1867 B43legacy_MMIO_GPIO_MASK)
1872 if (dev->dev->id.revision >= 2)
1873 mask |= 0x0010; /* FIXME: This is redundant. */
1875 #ifdef CONFIG_SSB_DRIVER_PCICORE
1876 pcidev = bus->pcicore.dev;
1878 gpiodev = bus->chipco.dev ? : pcidev;
1881 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
1882 (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
1888 /* Turn off all GPIO stuff. Call this on module unload, for example. */
1889 static void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
1891 struct ssb_bus *bus = dev->dev->bus;
1892 struct ssb_device *gpiodev, *pcidev = NULL;
1894 #ifdef CONFIG_SSB_DRIVER_PCICORE
1895 pcidev = bus->pcicore.dev;
1897 gpiodev = bus->chipco.dev ? : pcidev;
1900 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
1903 /* http://bcm-specs.sipsolutions.net/EnableMac */
1904 void b43legacy_mac_enable(struct b43legacy_wldev *dev)
1906 dev->mac_suspended--;
1907 B43legacy_WARN_ON(dev->mac_suspended < 0);
1908 B43legacy_WARN_ON(irqs_disabled());
1909 if (dev->mac_suspended == 0) {
1910 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
1911 b43legacy_read32(dev,
1912 B43legacy_MMIO_MACCTL)
1913 | B43legacy_MACCTL_ENABLED);
1914 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1915 B43legacy_IRQ_MAC_SUSPENDED);
1916 /* the next two are dummy reads */
1917 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1918 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1919 b43legacy_power_saving_ctl_bits(dev, -1, -1);
1921 /* Re-enable IRQs. */
1922 spin_lock_irq(&dev->wl->irq_lock);
1923 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
1925 spin_unlock_irq(&dev->wl->irq_lock);
1929 /* http://bcm-specs.sipsolutions.net/SuspendMAC */
1930 void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
1936 B43legacy_WARN_ON(irqs_disabled());
1937 B43legacy_WARN_ON(dev->mac_suspended < 0);
1939 if (dev->mac_suspended == 0) {
1940 /* Mask IRQs before suspending MAC. Otherwise
1941 * the MAC stays busy and won't suspend. */
1942 spin_lock_irq(&dev->wl->irq_lock);
1943 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
1944 spin_unlock_irq(&dev->wl->irq_lock);
1945 b43legacy_synchronize_irq(dev);
1947 b43legacy_power_saving_ctl_bits(dev, -1, 1);
1948 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
1949 b43legacy_read32(dev,
1950 B43legacy_MMIO_MACCTL)
1951 & ~B43legacy_MACCTL_ENABLED);
1952 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1953 for (i = 40; i; i--) {
1954 tmp = b43legacy_read32(dev,
1955 B43legacy_MMIO_GEN_IRQ_REASON);
1956 if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
1960 b43legacyerr(dev->wl, "MAC suspend failed\n");
1963 dev->mac_suspended++;
1966 static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
1968 struct b43legacy_wl *wl = dev->wl;
1972 ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1973 /* Reset status to STA infrastructure mode. */
1974 ctl &= ~B43legacy_MACCTL_AP;
1975 ctl &= ~B43legacy_MACCTL_KEEP_CTL;
1976 ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
1977 ctl &= ~B43legacy_MACCTL_KEEP_BAD;
1978 ctl &= ~B43legacy_MACCTL_PROMISC;
1979 ctl &= ~B43legacy_MACCTL_BEACPROMISC;
1980 ctl |= B43legacy_MACCTL_INFRA;
1982 if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
1983 ctl |= B43legacy_MACCTL_AP;
1984 else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
1985 ctl &= ~B43legacy_MACCTL_INFRA;
1987 if (wl->filter_flags & FIF_CONTROL)
1988 ctl |= B43legacy_MACCTL_KEEP_CTL;
1989 if (wl->filter_flags & FIF_FCSFAIL)
1990 ctl |= B43legacy_MACCTL_KEEP_BAD;
1991 if (wl->filter_flags & FIF_PLCPFAIL)
1992 ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
1993 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
1994 ctl |= B43legacy_MACCTL_PROMISC;
1995 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
1996 ctl |= B43legacy_MACCTL_BEACPROMISC;
1998 /* Workaround: On old hardware the HW-MAC-address-filter
1999 * doesn't work properly, so always run promisc in filter
2000 * it in software. */
2001 if (dev->dev->id.revision <= 4)
2002 ctl |= B43legacy_MACCTL_PROMISC;
2004 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
2007 if ((ctl & B43legacy_MACCTL_INFRA) &&
2008 !(ctl & B43legacy_MACCTL_AP)) {
2009 if (dev->dev->bus->chip_id == 0x4306 &&
2010 dev->dev->bus->chip_rev == 3)
2015 b43legacy_write16(dev, 0x612, cfp_pretbtt);
2018 static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
2026 offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
2029 offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
2031 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
2032 b43legacy_shm_read16(dev,
2033 B43legacy_SHM_SHARED, offset));
2036 static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
2038 switch (dev->phy.type) {
2039 case B43legacy_PHYTYPE_G:
2040 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
2041 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
2042 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
2043 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
2044 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
2045 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
2046 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
2048 case B43legacy_PHYTYPE_B:
2049 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
2050 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
2051 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
2052 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
2055 B43legacy_BUG_ON(1);
2059 /* Set the TX-Antenna for management frames sent by firmware. */
2060 static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
2067 case B43legacy_ANTENNA0:
2068 ant |= B43legacy_TX4_PHY_ANT0;
2070 case B43legacy_ANTENNA1:
2071 ant |= B43legacy_TX4_PHY_ANT1;
2073 case B43legacy_ANTENNA_AUTO:
2074 ant |= B43legacy_TX4_PHY_ANTLAST;
2077 B43legacy_BUG_ON(1);
2080 /* FIXME We also need to set the other flags of the PHY control
2081 * field somewhere. */
2084 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2085 B43legacy_SHM_SH_BEACPHYCTL);
2086 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2087 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2088 B43legacy_SHM_SH_BEACPHYCTL, tmp);
2090 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2091 B43legacy_SHM_SH_ACKCTSPHYCTL);
2092 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2093 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2094 B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
2095 /* For Probe Resposes */
2096 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2097 B43legacy_SHM_SH_PRPHYCTL);
2098 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2099 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2100 B43legacy_SHM_SH_PRPHYCTL, tmp);
2103 /* This is the opposite of b43legacy_chip_init() */
2104 static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
2106 b43legacy_radio_turn_off(dev, 1);
2107 b43legacy_gpio_cleanup(dev);
2108 /* firmware is released later */
2111 /* Initialize the chip
2112 * http://bcm-specs.sipsolutions.net/ChipInit
2114 static int b43legacy_chip_init(struct b43legacy_wldev *dev)
2116 struct b43legacy_phy *phy = &dev->phy;
2119 u32 value32, macctl;
2122 /* Initialize the MAC control */
2123 macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
2125 macctl |= B43legacy_MACCTL_GMODE;
2126 macctl |= B43legacy_MACCTL_INFRA;
2127 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
2129 err = b43legacy_request_firmware(dev);
2132 err = b43legacy_upload_microcode(dev);
2134 goto out; /* firmware is released later */
2136 err = b43legacy_gpio_init(dev);
2138 goto out; /* firmware is released later */
2140 err = b43legacy_upload_initvals(dev);
2142 goto err_gpio_clean;
2143 b43legacy_radio_turn_on(dev);
2145 b43legacy_write16(dev, 0x03E6, 0x0000);
2146 err = b43legacy_phy_init(dev);
2150 /* Select initial Interference Mitigation. */
2151 tmp = phy->interfmode;
2152 phy->interfmode = B43legacy_INTERFMODE_NONE;
2153 b43legacy_radio_set_interference_mitigation(dev, tmp);
2155 b43legacy_phy_set_antenna_diversity(dev);
2156 b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
2158 if (phy->type == B43legacy_PHYTYPE_B) {
2159 value16 = b43legacy_read16(dev, 0x005E);
2161 b43legacy_write16(dev, 0x005E, value16);
2163 b43legacy_write32(dev, 0x0100, 0x01000000);
2164 if (dev->dev->id.revision < 5)
2165 b43legacy_write32(dev, 0x010C, 0x01000000);
2167 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2168 value32 &= ~B43legacy_MACCTL_INFRA;
2169 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
2170 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2171 value32 |= B43legacy_MACCTL_INFRA;
2172 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
2174 if (b43legacy_using_pio(dev)) {
2175 b43legacy_write32(dev, 0x0210, 0x00000100);
2176 b43legacy_write32(dev, 0x0230, 0x00000100);
2177 b43legacy_write32(dev, 0x0250, 0x00000100);
2178 b43legacy_write32(dev, 0x0270, 0x00000100);
2179 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
2183 /* Probe Response Timeout value */
2184 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2185 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
2187 /* Initially set the wireless operation mode. */
2188 b43legacy_adjust_opmode(dev);
2190 if (dev->dev->id.revision < 3) {
2191 b43legacy_write16(dev, 0x060E, 0x0000);
2192 b43legacy_write16(dev, 0x0610, 0x8000);
2193 b43legacy_write16(dev, 0x0604, 0x0000);
2194 b43legacy_write16(dev, 0x0606, 0x0200);
2196 b43legacy_write32(dev, 0x0188, 0x80000000);
2197 b43legacy_write32(dev, 0x018C, 0x02000000);
2199 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
2200 b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2201 b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2202 b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2203 b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2204 b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2205 b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2207 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
2208 value32 |= 0x00100000;
2209 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2211 b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
2212 dev->dev->bus->chipco.fast_pwrup_delay);
2214 /* PHY TX errors counter. */
2215 atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2217 B43legacy_WARN_ON(err != 0);
2218 b43legacydbg(dev->wl, "Chip initialized\n");
2223 b43legacy_radio_turn_off(dev, 1);
2225 b43legacy_gpio_cleanup(dev);
2229 static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
2231 struct b43legacy_phy *phy = &dev->phy;
2233 if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
2236 b43legacy_mac_suspend(dev);
2237 b43legacy_phy_lo_g_measure(dev);
2238 b43legacy_mac_enable(dev);
2241 static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
2243 b43legacy_phy_lo_mark_all_unused(dev);
2244 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
2245 b43legacy_mac_suspend(dev);
2246 b43legacy_calc_nrssi_slope(dev);
2247 b43legacy_mac_enable(dev);
2251 static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
2253 /* Update device statistics. */
2254 b43legacy_calculate_link_quality(dev);
2257 static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
2259 b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
2261 atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2265 static void do_periodic_work(struct b43legacy_wldev *dev)
2269 state = dev->periodic_state;
2271 b43legacy_periodic_every120sec(dev);
2273 b43legacy_periodic_every60sec(dev);
2275 b43legacy_periodic_every30sec(dev);
2276 b43legacy_periodic_every15sec(dev);
2279 /* Periodic work locking policy:
2280 * The whole periodic work handler is protected by
2281 * wl->mutex. If another lock is needed somewhere in the
2282 * pwork callchain, it's aquired in-place, where it's needed.
2284 static void b43legacy_periodic_work_handler(struct work_struct *work)
2286 struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
2287 periodic_work.work);
2288 struct b43legacy_wl *wl = dev->wl;
2289 unsigned long delay;
2291 mutex_lock(&wl->mutex);
2293 if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
2295 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
2298 do_periodic_work(dev);
2300 dev->periodic_state++;
2302 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
2303 delay = msecs_to_jiffies(50);
2305 delay = round_jiffies_relative(HZ * 15);
2306 ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
2308 mutex_unlock(&wl->mutex);
2311 static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
2313 struct delayed_work *work = &dev->periodic_work;
2315 dev->periodic_state = 0;
2316 INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
2317 ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
2320 /* Validate access to the chip (SHM) */
2321 static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
2326 shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
2327 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
2328 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2331 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
2332 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2335 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
2337 value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2338 if ((value | B43legacy_MACCTL_GMODE) !=
2339 (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
2342 value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
2348 b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
2352 static void b43legacy_security_init(struct b43legacy_wldev *dev)
2354 dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
2355 B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
2356 dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2358 /* KTP is a word address, but we address SHM bytewise.
2359 * So multiply by two.
2362 if (dev->dev->id.revision >= 5)
2363 /* Number of RCMTA address slots */
2364 b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
2365 dev->max_nr_keys - 8);
2368 #ifdef CONFIG_B43LEGACY_HWRNG
2369 static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
2371 struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
2372 unsigned long flags;
2374 /* Don't take wl->mutex here, as it could deadlock with
2375 * hwrng internal locking. It's not needed to take
2376 * wl->mutex here, anyway. */
2378 spin_lock_irqsave(&wl->irq_lock, flags);
2379 *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
2380 spin_unlock_irqrestore(&wl->irq_lock, flags);
2382 return (sizeof(u16));
2386 static void b43legacy_rng_exit(struct b43legacy_wl *wl)
2388 #ifdef CONFIG_B43LEGACY_HWRNG
2389 if (wl->rng_initialized)
2390 hwrng_unregister(&wl->rng);
2394 static int b43legacy_rng_init(struct b43legacy_wl *wl)
2398 #ifdef CONFIG_B43LEGACY_HWRNG
2399 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
2400 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
2401 wl->rng.name = wl->rng_name;
2402 wl->rng.data_read = b43legacy_rng_read;
2403 wl->rng.priv = (unsigned long)wl;
2404 wl->rng_initialized = 1;
2405 err = hwrng_register(&wl->rng);
2407 wl->rng_initialized = 0;
2408 b43legacyerr(wl, "Failed to register the random "
2409 "number generator (%d)\n", err);
2416 static int b43legacy_op_tx(struct ieee80211_hw *hw,
2417 struct sk_buff *skb)
2419 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2420 struct b43legacy_wldev *dev = wl->current_dev;
2422 unsigned long flags;
2426 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
2428 /* DMA-TX is done without a global lock. */
2429 if (b43legacy_using_pio(dev)) {
2430 spin_lock_irqsave(&wl->irq_lock, flags);
2431 err = b43legacy_pio_tx(dev, skb);
2432 spin_unlock_irqrestore(&wl->irq_lock, flags);
2434 err = b43legacy_dma_tx(dev, skb);
2436 if (unlikely(err)) {
2437 /* Drop the packet. */
2438 dev_kfree_skb_any(skb);
2440 return NETDEV_TX_OK;
2443 static int b43legacy_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
2444 const struct ieee80211_tx_queue_params *params)
2449 static int b43legacy_op_get_tx_stats(struct ieee80211_hw *hw,
2450 struct ieee80211_tx_queue_stats *stats)
2452 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2453 struct b43legacy_wldev *dev = wl->current_dev;
2454 unsigned long flags;
2459 spin_lock_irqsave(&wl->irq_lock, flags);
2460 if (likely(b43legacy_status(dev) >= B43legacy_STAT_STARTED)) {
2461 if (b43legacy_using_pio(dev))
2462 b43legacy_pio_get_tx_stats(dev, stats);
2464 b43legacy_dma_get_tx_stats(dev, stats);
2467 spin_unlock_irqrestore(&wl->irq_lock, flags);
2472 static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
2473 struct ieee80211_low_level_stats *stats)
2475 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2476 unsigned long flags;
2478 spin_lock_irqsave(&wl->irq_lock, flags);
2479 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
2480 spin_unlock_irqrestore(&wl->irq_lock, flags);
2485 static const char *phymode_to_string(unsigned int phymode)
2488 case B43legacy_PHYMODE_B:
2490 case B43legacy_PHYMODE_G:
2493 B43legacy_BUG_ON(1);
2498 static int find_wldev_for_phymode(struct b43legacy_wl *wl,
2499 unsigned int phymode,
2500 struct b43legacy_wldev **dev,
2503 struct b43legacy_wldev *d;
2505 list_for_each_entry(d, &wl->devlist, list) {
2506 if (d->phy.possible_phymodes & phymode) {
2507 /* Ok, this device supports the PHY-mode.
2508 * Set the gmode bit. */
2519 static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
2521 struct ssb_device *sdev = dev->dev;
2524 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2525 tmslow &= ~B43legacy_TMSLOW_GMODE;
2526 tmslow |= B43legacy_TMSLOW_PHYRESET;
2527 tmslow |= SSB_TMSLOW_FGC;
2528 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2531 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2532 tmslow &= ~SSB_TMSLOW_FGC;
2533 tmslow |= B43legacy_TMSLOW_PHYRESET;
2534 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2538 /* Expects wl->mutex locked */
2539 static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
2540 unsigned int new_mode)
2542 struct b43legacy_wldev *uninitialized_var(up_dev);
2543 struct b43legacy_wldev *down_dev;
2548 err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
2550 b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
2551 phymode_to_string(new_mode));
2554 if ((up_dev == wl->current_dev) &&
2555 (!!wl->current_dev->phy.gmode == !!gmode))
2556 /* This device is already running. */
2558 b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
2559 phymode_to_string(new_mode));
2560 down_dev = wl->current_dev;
2562 prev_status = b43legacy_status(down_dev);
2563 /* Shutdown the currently running core. */
2564 if (prev_status >= B43legacy_STAT_STARTED)
2565 b43legacy_wireless_core_stop(down_dev);
2566 if (prev_status >= B43legacy_STAT_INITIALIZED)
2567 b43legacy_wireless_core_exit(down_dev);
2569 if (down_dev != up_dev)
2570 /* We switch to a different core, so we put PHY into
2571 * RESET on the old core. */
2572 b43legacy_put_phy_into_reset(down_dev);
2574 /* Now start the new core. */
2575 up_dev->phy.gmode = gmode;
2576 if (prev_status >= B43legacy_STAT_INITIALIZED) {
2577 err = b43legacy_wireless_core_init(up_dev);
2579 b43legacyerr(wl, "Fatal: Could not initialize device"
2580 " for newly selected %s-PHY mode\n",
2581 phymode_to_string(new_mode));
2585 if (prev_status >= B43legacy_STAT_STARTED) {
2586 err = b43legacy_wireless_core_start(up_dev);
2588 b43legacyerr(wl, "Fatal: Coult not start device for "
2589 "newly selected %s-PHY mode\n",
2590 phymode_to_string(new_mode));
2591 b43legacy_wireless_core_exit(up_dev);
2595 B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
2597 b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
2599 wl->current_dev = up_dev;
2603 /* Whoops, failed to init the new core. No core is operating now. */
2604 wl->current_dev = NULL;
2608 /* Write the short and long frame retry limit values. */
2609 static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
2610 unsigned int short_retry,
2611 unsigned int long_retry)
2613 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
2614 * the chip-internal counter. */
2615 short_retry = min(short_retry, (unsigned int)0xF);
2616 long_retry = min(long_retry, (unsigned int)0xF);
2618 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
2619 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
2622 static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
2625 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2626 struct b43legacy_wldev *dev;
2627 struct b43legacy_phy *phy;
2628 struct ieee80211_conf *conf = &hw->conf;
2629 unsigned long flags;
2630 unsigned int new_phymode = 0xFFFF;
2635 antenna_tx = B43legacy_ANTENNA_DEFAULT;
2636 antenna_rx = B43legacy_ANTENNA_DEFAULT;
2638 mutex_lock(&wl->mutex);
2639 dev = wl->current_dev;
2642 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
2643 b43legacy_set_retry_limits(dev,
2644 conf->short_frame_max_tx_count,
2645 conf->long_frame_max_tx_count);
2646 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
2648 goto out_unlock_mutex;
2650 /* Switch the PHY mode (if necessary). */
2651 switch (conf->channel->band) {
2652 case IEEE80211_BAND_2GHZ:
2653 if (phy->type == B43legacy_PHYTYPE_B)
2654 new_phymode = B43legacy_PHYMODE_B;
2656 new_phymode = B43legacy_PHYMODE_G;
2659 B43legacy_WARN_ON(1);
2661 err = b43legacy_switch_phymode(wl, new_phymode);
2663 goto out_unlock_mutex;
2665 /* Disable IRQs while reconfiguring the device.
2666 * This makes it possible to drop the spinlock throughout
2667 * the reconfiguration process. */
2668 spin_lock_irqsave(&wl->irq_lock, flags);
2669 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
2670 spin_unlock_irqrestore(&wl->irq_lock, flags);
2671 goto out_unlock_mutex;
2673 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
2674 spin_unlock_irqrestore(&wl->irq_lock, flags);
2675 b43legacy_synchronize_irq(dev);
2677 /* Switch to the requested channel.
2678 * The firmware takes care of races with the TX handler. */
2679 if (conf->channel->hw_value != phy->channel)
2680 b43legacy_radio_selectchannel(dev, conf->channel->hw_value, 0);
2682 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
2684 /* Adjust the desired TX power level. */
2685 if (conf->power_level != 0) {
2686 if (conf->power_level != phy->power_level) {
2687 phy->power_level = conf->power_level;
2688 b43legacy_phy_xmitpower(dev);
2692 /* Antennas for RX and management frame TX. */
2693 b43legacy_mgmtframe_txantenna(dev, antenna_tx);
2695 if (wl->radio_enabled != phy->radio_on) {
2696 if (wl->radio_enabled) {
2697 b43legacy_radio_turn_on(dev);
2698 b43legacyinfo(dev->wl, "Radio turned on by software\n");
2699 if (!dev->radio_hw_enable)
2700 b43legacyinfo(dev->wl, "The hardware RF-kill"
2701 " button still turns the radio"
2702 " physically off. Press the"
2703 " button to turn it on.\n");
2705 b43legacy_radio_turn_off(dev, 0);
2706 b43legacyinfo(dev->wl, "Radio turned off by"
2711 spin_lock_irqsave(&wl->irq_lock, flags);
2712 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
2714 spin_unlock_irqrestore(&wl->irq_lock, flags);
2716 mutex_unlock(&wl->mutex);
2721 static void b43legacy_update_basic_rates(struct b43legacy_wldev *dev, u32 brates)
2723 struct ieee80211_supported_band *sband =
2724 dev->wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
2725 struct ieee80211_rate *rate;
2727 u16 basic, direct, offset, basic_offset, rateptr;
2729 for (i = 0; i < sband->n_bitrates; i++) {
2730 rate = &sband->bitrates[i];
2732 if (b43legacy_is_cck_rate(rate->hw_value)) {
2733 direct = B43legacy_SHM_SH_CCKDIRECT;
2734 basic = B43legacy_SHM_SH_CCKBASIC;
2735 offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
2738 direct = B43legacy_SHM_SH_OFDMDIRECT;
2739 basic = B43legacy_SHM_SH_OFDMBASIC;
2740 offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
2744 rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
2746 if (b43legacy_is_cck_rate(rate->hw_value)) {
2747 basic_offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
2748 basic_offset &= 0xF;
2750 basic_offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
2751 basic_offset &= 0xF;
2755 * Get the pointer that we need to point to
2756 * from the direct map
2758 rateptr = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2759 direct + 2 * basic_offset);
2760 /* and write it to the basic map */
2761 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2762 basic + 2 * offset, rateptr);
2766 static void b43legacy_op_bss_info_changed(struct ieee80211_hw *hw,
2767 struct ieee80211_vif *vif,
2768 struct ieee80211_bss_conf *conf,
2771 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2772 struct b43legacy_wldev *dev;
2773 struct b43legacy_phy *phy;
2774 unsigned long flags;
2776 mutex_lock(&wl->mutex);
2777 B43legacy_WARN_ON(wl->vif != vif);
2779 dev = wl->current_dev;
2782 /* Disable IRQs while reconfiguring the device.
2783 * This makes it possible to drop the spinlock throughout
2784 * the reconfiguration process. */
2785 spin_lock_irqsave(&wl->irq_lock, flags);
2786 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
2787 spin_unlock_irqrestore(&wl->irq_lock, flags);
2788 goto out_unlock_mutex;
2790 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
2792 if (changed & BSS_CHANGED_BSSID) {
2793 b43legacy_synchronize_irq(dev);
2796 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
2798 memset(wl->bssid, 0, ETH_ALEN);
2801 if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
2802 if (changed & BSS_CHANGED_BEACON &&
2803 (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
2804 b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
2805 b43legacy_update_templates(wl);
2807 if (changed & BSS_CHANGED_BSSID)
2808 b43legacy_write_mac_bssid_templates(dev);
2810 spin_unlock_irqrestore(&wl->irq_lock, flags);
2812 b43legacy_mac_suspend(dev);
2814 if (changed & BSS_CHANGED_BEACON_INT &&
2815 (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
2816 b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
2817 b43legacy_set_beacon_int(dev, conf->beacon_int);
2819 if (changed & BSS_CHANGED_BASIC_RATES)
2820 b43legacy_update_basic_rates(dev, conf->basic_rates);
2822 if (changed & BSS_CHANGED_ERP_SLOT) {
2823 if (conf->use_short_slot)
2824 b43legacy_short_slot_timing_enable(dev);
2826 b43legacy_short_slot_timing_disable(dev);
2829 b43legacy_mac_enable(dev);
2831 spin_lock_irqsave(&wl->irq_lock, flags);
2832 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
2835 spin_unlock_irqrestore(&wl->irq_lock, flags);
2837 mutex_unlock(&wl->mutex);
2840 static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
2841 unsigned int changed,
2842 unsigned int *fflags,u64 multicast)
2844 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2845 struct b43legacy_wldev *dev = wl->current_dev;
2846 unsigned long flags;
2853 spin_lock_irqsave(&wl->irq_lock, flags);
2854 *fflags &= FIF_PROMISC_IN_BSS |
2860 FIF_BCN_PRBRESP_PROMISC;
2862 changed &= FIF_PROMISC_IN_BSS |
2868 FIF_BCN_PRBRESP_PROMISC;
2870 wl->filter_flags = *fflags;
2872 if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
2873 b43legacy_adjust_opmode(dev);
2874 spin_unlock_irqrestore(&wl->irq_lock, flags);
2877 /* Locking: wl->mutex */
2878 static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
2880 struct b43legacy_wl *wl = dev->wl;
2881 unsigned long flags;
2883 if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
2886 /* Disable and sync interrupts. We must do this before than
2887 * setting the status to INITIALIZED, as the interrupt handler
2888 * won't care about IRQs then. */
2889 spin_lock_irqsave(&wl->irq_lock, flags);
2890 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
2891 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
2892 spin_unlock_irqrestore(&wl->irq_lock, flags);
2893 b43legacy_synchronize_irq(dev);
2895 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
2897 mutex_unlock(&wl->mutex);
2898 /* Must unlock as it would otherwise deadlock. No races here.
2899 * Cancel the possibly running self-rearming periodic work. */
2900 cancel_delayed_work_sync(&dev->periodic_work);
2901 mutex_lock(&wl->mutex);
2903 ieee80211_stop_queues(wl->hw); /* FIXME this could cause a deadlock */
2905 b43legacy_mac_suspend(dev);
2906 free_irq(dev->dev->irq, dev);
2907 b43legacydbg(wl, "Wireless interface stopped\n");
2910 /* Locking: wl->mutex */
2911 static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
2915 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
2917 drain_txstatus_queue(dev);
2918 err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
2919 IRQF_SHARED, KBUILD_MODNAME, dev);
2921 b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
2925 /* We are ready to run. */
2926 b43legacy_set_status(dev, B43legacy_STAT_STARTED);
2928 /* Start data flow (TX/RX) */
2929 b43legacy_mac_enable(dev);
2930 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
2932 /* Start maintenance work */
2933 b43legacy_periodic_tasks_setup(dev);
2935 b43legacydbg(dev->wl, "Wireless interface started\n");
2940 /* Get PHY and RADIO versioning numbers */
2941 static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
2943 struct b43legacy_phy *phy = &dev->phy;
2951 int unsupported = 0;
2953 /* Get PHY versioning */
2954 tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
2955 analog_type = (tmp & B43legacy_PHYVER_ANALOG)
2956 >> B43legacy_PHYVER_ANALOG_SHIFT;
2957 phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
2958 phy_rev = (tmp & B43legacy_PHYVER_VERSION);
2960 case B43legacy_PHYTYPE_B:
2961 if (phy_rev != 2 && phy_rev != 4
2962 && phy_rev != 6 && phy_rev != 7)
2965 case B43legacy_PHYTYPE_G:
2973 b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
2974 "(Analog %u, Type %u, Revision %u)\n",
2975 analog_type, phy_type, phy_rev);
2978 b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
2979 analog_type, phy_type, phy_rev);
2982 /* Get RADIO versioning */
2983 if (dev->dev->bus->chip_id == 0x4317) {
2984 if (dev->dev->bus->chip_rev == 0)
2986 else if (dev->dev->bus->chip_rev == 1)
2991 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,