b43: Fix ofdmtab write regression
[linux-2.6/sactl.git] / drivers / net / wireless / b43 / main.c
blobd7ea671394a8cb84204a2d2be6ef5a415cb9412d
1 /*
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/version.h>
37 #include <linux/firmware.h>
38 #include <linux/wireless.h>
39 #include <linux/workqueue.h>
40 #include <linux/skbuff.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/unaligned.h>
44 #include "b43.h"
45 #include "main.h"
46 #include "debugfs.h"
47 #include "phy.h"
48 #include "dma.h"
49 #include "pio.h"
50 #include "sysfs.h"
51 #include "xmit.h"
52 #include "lo.h"
53 #include "pcmcia.h"
55 MODULE_DESCRIPTION("Broadcom B43 wireless driver");
56 MODULE_AUTHOR("Martin Langer");
57 MODULE_AUTHOR("Stefano Brivio");
58 MODULE_AUTHOR("Michael Buesch");
59 MODULE_LICENSE("GPL");
61 extern char *nvram_get(char *name);
63 #if defined(CONFIG_B43_DMA) && defined(CONFIG_B43_PIO)
64 static int modparam_pio;
65 module_param_named(pio, modparam_pio, int, 0444);
66 MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
67 #elif defined(CONFIG_B43_DMA)
68 # define modparam_pio 0
69 #elif defined(CONFIG_B43_PIO)
70 # define modparam_pio 1
71 #endif
73 static int modparam_bad_frames_preempt;
74 module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
75 MODULE_PARM_DESC(bad_frames_preempt,
76 "enable(1) / disable(0) Bad Frames Preemption");
78 static char modparam_fwpostfix[16];
79 module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
80 MODULE_PARM_DESC(fwpostfix, "Postfix for the .fw files to load.");
82 static int modparam_hwpctl;
83 module_param_named(hwpctl, modparam_hwpctl, int, 0444);
84 MODULE_PARM_DESC(hwpctl, "Enable hardware-side power control (default off)");
86 static int modparam_nohwcrypt;
87 module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
88 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
90 static const struct ssb_device_id b43_ssb_tbl[] = {
91 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
92 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 6),
93 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 7),
94 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 9),
95 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
96 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
97 SSB_DEVTABLE_END
100 MODULE_DEVICE_TABLE(ssb, b43_ssb_tbl);
102 /* Channel and ratetables are shared for all devices.
103 * They can't be const, because ieee80211 puts some precalculated
104 * data in there. This data is the same for all devices, so we don't
105 * get concurrency issues */
106 #define RATETAB_ENT(_rateid, _flags) \
108 .rate = B43_RATE_TO_BASE100KBPS(_rateid), \
109 .val = (_rateid), \
110 .val2 = (_rateid), \
111 .flags = (_flags), \
113 static struct ieee80211_rate __b43_ratetable[] = {
114 RATETAB_ENT(B43_CCK_RATE_1MB, IEEE80211_RATE_CCK),
115 RATETAB_ENT(B43_CCK_RATE_2MB, IEEE80211_RATE_CCK_2),
116 RATETAB_ENT(B43_CCK_RATE_5MB, IEEE80211_RATE_CCK_2),
117 RATETAB_ENT(B43_CCK_RATE_11MB, IEEE80211_RATE_CCK_2),
118 RATETAB_ENT(B43_OFDM_RATE_6MB, IEEE80211_RATE_OFDM),
119 RATETAB_ENT(B43_OFDM_RATE_9MB, IEEE80211_RATE_OFDM),
120 RATETAB_ENT(B43_OFDM_RATE_12MB, IEEE80211_RATE_OFDM),
121 RATETAB_ENT(B43_OFDM_RATE_18MB, IEEE80211_RATE_OFDM),
122 RATETAB_ENT(B43_OFDM_RATE_24MB, IEEE80211_RATE_OFDM),
123 RATETAB_ENT(B43_OFDM_RATE_36MB, IEEE80211_RATE_OFDM),
124 RATETAB_ENT(B43_OFDM_RATE_48MB, IEEE80211_RATE_OFDM),
125 RATETAB_ENT(B43_OFDM_RATE_54MB, IEEE80211_RATE_OFDM),
128 #define b43_a_ratetable (__b43_ratetable + 4)
129 #define b43_a_ratetable_size 8
130 #define b43_b_ratetable (__b43_ratetable + 0)
131 #define b43_b_ratetable_size 4
132 #define b43_g_ratetable (__b43_ratetable + 0)
133 #define b43_g_ratetable_size 12
135 #define CHANTAB_ENT(_chanid, _freq) \
137 .chan = (_chanid), \
138 .freq = (_freq), \
139 .val = (_chanid), \
140 .flag = IEEE80211_CHAN_W_SCAN | \
141 IEEE80211_CHAN_W_ACTIVE_SCAN | \
142 IEEE80211_CHAN_W_IBSS, \
143 .power_level = 0xFF, \
144 .antenna_max = 0xFF, \
146 static struct ieee80211_channel b43_bg_chantable[] = {
147 CHANTAB_ENT(1, 2412),
148 CHANTAB_ENT(2, 2417),
149 CHANTAB_ENT(3, 2422),
150 CHANTAB_ENT(4, 2427),
151 CHANTAB_ENT(5, 2432),
152 CHANTAB_ENT(6, 2437),
153 CHANTAB_ENT(7, 2442),
154 CHANTAB_ENT(8, 2447),
155 CHANTAB_ENT(9, 2452),
156 CHANTAB_ENT(10, 2457),
157 CHANTAB_ENT(11, 2462),
158 CHANTAB_ENT(12, 2467),
159 CHANTAB_ENT(13, 2472),
160 CHANTAB_ENT(14, 2484),
163 #define b43_bg_chantable_size ARRAY_SIZE(b43_bg_chantable)
164 static struct ieee80211_channel b43_a_chantable[] = {
165 CHANTAB_ENT(36, 5180),
166 CHANTAB_ENT(40, 5200),
167 CHANTAB_ENT(44, 5220),
168 CHANTAB_ENT(48, 5240),
169 CHANTAB_ENT(52, 5260),
170 CHANTAB_ENT(56, 5280),
171 CHANTAB_ENT(60, 5300),
172 CHANTAB_ENT(64, 5320),
173 CHANTAB_ENT(149, 5745),
174 CHANTAB_ENT(153, 5765),
175 CHANTAB_ENT(157, 5785),
176 CHANTAB_ENT(161, 5805),
177 CHANTAB_ENT(165, 5825),
180 #define b43_a_chantable_size ARRAY_SIZE(b43_a_chantable)
182 static void b43_wireless_core_exit(struct b43_wldev *dev);
183 static int b43_wireless_core_init(struct b43_wldev *dev);
184 static void b43_wireless_core_stop(struct b43_wldev *dev);
185 static int b43_wireless_core_start(struct b43_wldev *dev);
187 static int b43_ratelimit(struct b43_wl *wl)
189 if (!wl || !wl->current_dev)
190 return 1;
191 if (b43_status(wl->current_dev) < B43_STAT_STARTED)
192 return 1;
193 /* We are up and running.
194 * Ratelimit the messages to avoid DoS over the net. */
195 return net_ratelimit();
198 void b43info(struct b43_wl *wl, const char *fmt, ...)
200 va_list args;
202 if (!b43_ratelimit(wl))
203 return;
204 va_start(args, fmt);
205 printk(KERN_INFO "b43-%s: ",
206 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
207 vprintk(fmt, args);
208 va_end(args);
211 void b43err(struct b43_wl *wl, const char *fmt, ...)
213 va_list args;
215 if (!b43_ratelimit(wl))
216 return;
217 va_start(args, fmt);
218 printk(KERN_ERR "b43-%s ERROR: ",
219 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
220 vprintk(fmt, args);
221 va_end(args);
224 void b43warn(struct b43_wl *wl, const char *fmt, ...)
226 va_list args;
228 if (!b43_ratelimit(wl))
229 return;
230 va_start(args, fmt);
231 printk(KERN_WARNING "b43-%s warning: ",
232 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
233 vprintk(fmt, args);
234 va_end(args);
237 #if B43_DEBUG
238 void b43dbg(struct b43_wl *wl, const char *fmt, ...)
240 va_list args;
242 va_start(args, fmt);
243 printk(KERN_DEBUG "b43-%s debug: ",
244 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
245 vprintk(fmt, args);
246 va_end(args);
248 #endif /* DEBUG */
250 static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
252 u32 macctl;
254 B43_WARN_ON(offset % 4 != 0);
256 macctl = b43_read32(dev, B43_MMIO_MACCTL);
257 if (macctl & B43_MACCTL_BE)
258 val = swab32(val);
260 b43_write32(dev, B43_MMIO_RAM_CONTROL, offset);
261 mmiowb();
262 b43_write32(dev, B43_MMIO_RAM_DATA, val);
265 static inline
266 void b43_shm_control_word(struct b43_wldev *dev, u16 routing, u16 offset)
268 u32 control;
270 /* "offset" is the WORD offset. */
272 control = routing;
273 control <<= 16;
274 control |= offset;
275 b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
278 u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
280 u32 ret;
282 if (routing == B43_SHM_SHARED) {
283 B43_WARN_ON(offset & 0x0001);
284 if (offset & 0x0003) {
285 /* Unaligned access */
286 b43_shm_control_word(dev, routing, offset >> 2);
287 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
288 ret <<= 16;
289 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
290 ret |= b43_read16(dev, B43_MMIO_SHM_DATA);
292 return ret;
294 offset >>= 2;
296 b43_shm_control_word(dev, routing, offset);
297 ret = b43_read32(dev, B43_MMIO_SHM_DATA);
299 return ret;
302 u16 b43_shm_read16(struct b43_wldev * dev, u16 routing, u16 offset)
304 u16 ret;
306 if (routing == B43_SHM_SHARED) {
307 B43_WARN_ON(offset & 0x0001);
308 if (offset & 0x0003) {
309 /* Unaligned access */
310 b43_shm_control_word(dev, routing, offset >> 2);
311 ret = b43_read16(dev, B43_MMIO_SHM_DATA_UNALIGNED);
313 return ret;
315 offset >>= 2;
317 b43_shm_control_word(dev, routing, offset);
318 ret = b43_read16(dev, B43_MMIO_SHM_DATA);
320 return ret;
323 void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
325 if (routing == B43_SHM_SHARED) {
326 B43_WARN_ON(offset & 0x0001);
327 if (offset & 0x0003) {
328 /* Unaligned access */
329 b43_shm_control_word(dev, routing, offset >> 2);
330 mmiowb();
331 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED,
332 (value >> 16) & 0xffff);
333 mmiowb();
334 b43_shm_control_word(dev, routing, (offset >> 2) + 1);
335 mmiowb();
336 b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
337 return;
339 offset >>= 2;
341 b43_shm_control_word(dev, routing, offset);
342 mmiowb();
343 b43_write32(dev, B43_MMIO_SHM_DATA, value);
346 void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
348 if (routing == B43_SHM_SHARED) {
349 B43_WARN_ON(offset & 0x0001);
350 if (offset & 0x0003) {
351 /* Unaligned access */
352 b43_shm_control_word(dev, routing, offset >> 2);
353 mmiowb();
354 b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
355 return;
357 offset >>= 2;
359 b43_shm_control_word(dev, routing, offset);
360 mmiowb();
361 b43_write16(dev, B43_MMIO_SHM_DATA, value);
364 /* Read HostFlags */
365 u32 b43_hf_read(struct b43_wldev * dev)
367 u32 ret;
369 ret = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI);
370 ret <<= 16;
371 ret |= b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFLO);
373 return ret;
376 /* Write HostFlags */
377 void b43_hf_write(struct b43_wldev *dev, u32 value)
379 b43_shm_write16(dev, B43_SHM_SHARED,
380 B43_SHM_SH_HOSTFLO, (value & 0x0000FFFF));
381 b43_shm_write16(dev, B43_SHM_SHARED,
382 B43_SHM_SH_HOSTFHI, ((value & 0xFFFF0000) >> 16));
385 void b43_tsf_read(struct b43_wldev *dev, u64 * tsf)
387 /* We need to be careful. As we read the TSF from multiple
388 * registers, we should take care of register overflows.
389 * In theory, the whole tsf read process should be atomic.
390 * We try to be atomic here, by restaring the read process,
391 * if any of the high registers changed (overflew).
393 if (dev->dev->id.revision >= 3) {
394 u32 low, high, high2;
396 do {
397 high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
398 low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
399 high2 = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
400 } while (unlikely(high != high2));
402 *tsf = high;
403 *tsf <<= 32;
404 *tsf |= low;
405 } else {
406 u64 tmp;
407 u16 v0, v1, v2, v3;
408 u16 test1, test2, test3;
410 do {
411 v3 = b43_read16(dev, B43_MMIO_TSF_3);
412 v2 = b43_read16(dev, B43_MMIO_TSF_2);
413 v1 = b43_read16(dev, B43_MMIO_TSF_1);
414 v0 = b43_read16(dev, B43_MMIO_TSF_0);
416 test3 = b43_read16(dev, B43_MMIO_TSF_3);
417 test2 = b43_read16(dev, B43_MMIO_TSF_2);
418 test1 = b43_read16(dev, B43_MMIO_TSF_1);
419 } while (v3 != test3 || v2 != test2 || v1 != test1);
421 *tsf = v3;
422 *tsf <<= 48;
423 tmp = v2;
424 tmp <<= 32;
425 *tsf |= tmp;
426 tmp = v1;
427 tmp <<= 16;
428 *tsf |= tmp;
429 *tsf |= v0;
433 static void b43_time_lock(struct b43_wldev *dev)
435 u32 macctl;
437 macctl = b43_read32(dev, B43_MMIO_MACCTL);
438 macctl |= B43_MACCTL_TBTTHOLD;
439 b43_write32(dev, B43_MMIO_MACCTL, macctl);
440 /* Commit the write */
441 b43_read32(dev, B43_MMIO_MACCTL);
444 static void b43_time_unlock(struct b43_wldev *dev)
446 u32 macctl;
448 macctl = b43_read32(dev, B43_MMIO_MACCTL);
449 macctl &= ~B43_MACCTL_TBTTHOLD;
450 b43_write32(dev, B43_MMIO_MACCTL, macctl);
451 /* Commit the write */
452 b43_read32(dev, B43_MMIO_MACCTL);
455 static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
457 /* Be careful with the in-progress timer.
458 * First zero out the low register, so we have a full
459 * register-overflow duration to complete the operation.
461 if (dev->dev->id.revision >= 3) {
462 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
463 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
465 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, 0);
466 mmiowb();
467 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, hi);
468 mmiowb();
469 b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, lo);
470 } else {
471 u16 v0 = (tsf & 0x000000000000FFFFULL);
472 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
473 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
474 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
476 b43_write16(dev, B43_MMIO_TSF_0, 0);
477 mmiowb();
478 b43_write16(dev, B43_MMIO_TSF_3, v3);
479 mmiowb();
480 b43_write16(dev, B43_MMIO_TSF_2, v2);
481 mmiowb();
482 b43_write16(dev, B43_MMIO_TSF_1, v1);
483 mmiowb();
484 b43_write16(dev, B43_MMIO_TSF_0, v0);
488 void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
490 b43_time_lock(dev);
491 b43_tsf_write_locked(dev, tsf);
492 b43_time_unlock(dev);
495 static
496 void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 * mac)
498 static const u8 zero_addr[ETH_ALEN] = { 0 };
499 u16 data;
501 if (!mac)
502 mac = zero_addr;
504 offset |= 0x0020;
505 b43_write16(dev, B43_MMIO_MACFILTER_CONTROL, offset);
507 data = mac[0];
508 data |= mac[1] << 8;
509 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
510 data = mac[2];
511 data |= mac[3] << 8;
512 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
513 data = mac[4];
514 data |= mac[5] << 8;
515 b43_write16(dev, B43_MMIO_MACFILTER_DATA, data);
518 static void b43_write_mac_bssid_templates(struct b43_wldev *dev)
520 const u8 *mac;
521 const u8 *bssid;
522 u8 mac_bssid[ETH_ALEN * 2];
523 int i;
524 u32 tmp;
526 bssid = dev->wl->bssid;
527 mac = dev->wl->mac_addr;
529 b43_macfilter_set(dev, B43_MACFILTER_BSSID, bssid);
531 memcpy(mac_bssid, mac, ETH_ALEN);
532 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
534 /* Write our MAC address and BSSID to template ram */
535 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
536 tmp = (u32) (mac_bssid[i + 0]);
537 tmp |= (u32) (mac_bssid[i + 1]) << 8;
538 tmp |= (u32) (mac_bssid[i + 2]) << 16;
539 tmp |= (u32) (mac_bssid[i + 3]) << 24;
540 b43_ram_write(dev, 0x20 + i, tmp);
544 static void b43_upload_card_macaddress(struct b43_wldev *dev)
546 b43_write_mac_bssid_templates(dev);
547 b43_macfilter_set(dev, B43_MACFILTER_SELF, dev->wl->mac_addr);
550 static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
552 /* slot_time is in usec. */
553 if (dev->phy.type != B43_PHYTYPE_G)
554 return;
555 b43_write16(dev, 0x684, 510 + slot_time);
556 b43_shm_write16(dev, B43_SHM_SHARED, 0x0010, slot_time);
559 static void b43_short_slot_timing_enable(struct b43_wldev *dev)
561 b43_set_slot_time(dev, 9);
562 dev->short_slot = 1;
565 static void b43_short_slot_timing_disable(struct b43_wldev *dev)
567 b43_set_slot_time(dev, 20);
568 dev->short_slot = 0;
571 /* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
572 * Returns the _previously_ enabled IRQ mask.
574 static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
576 u32 old_mask;
578 old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
579 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
581 return old_mask;
584 /* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
585 * Returns the _previously_ enabled IRQ mask.
587 static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
589 u32 old_mask;
591 old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
592 b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
594 return old_mask;
597 /* Synchronize IRQ top- and bottom-half.
598 * IRQs must be masked before calling this.
599 * This must not be called with the irq_lock held.
601 static void b43_synchronize_irq(struct b43_wldev *dev)
603 synchronize_irq(dev->dev->irq);
604 tasklet_kill(&dev->isr_tasklet);
607 /* DummyTransmission function, as documented on
608 * http://bcm-specs.sipsolutions.net/DummyTransmission
610 void b43_dummy_transmission(struct b43_wldev *dev)
612 struct b43_phy *phy = &dev->phy;
613 unsigned int i, max_loop;
614 u16 value;
615 u32 buffer[5] = {
616 0x00000000,
617 0x00D40000,
618 0x00000000,
619 0x01000000,
620 0x00000000,
623 switch (phy->type) {
624 case B43_PHYTYPE_A:
625 max_loop = 0x1E;
626 buffer[0] = 0x000201CC;
627 break;
628 case B43_PHYTYPE_B:
629 case B43_PHYTYPE_G:
630 max_loop = 0xFA;
631 buffer[0] = 0x000B846E;
632 break;
633 default:
634 B43_WARN_ON(1);
635 return;
638 for (i = 0; i < 5; i++)
639 b43_ram_write(dev, i * 4, buffer[i]);
641 /* Commit writes */
642 b43_read32(dev, B43_MMIO_MACCTL);
644 b43_write16(dev, 0x0568, 0x0000);
645 b43_write16(dev, 0x07C0, 0x0000);
646 value = ((phy->type == B43_PHYTYPE_A) ? 1 : 0);
647 b43_write16(dev, 0x050C, value);
648 b43_write16(dev, 0x0508, 0x0000);
649 b43_write16(dev, 0x050A, 0x0000);
650 b43_write16(dev, 0x054C, 0x0000);
651 b43_write16(dev, 0x056A, 0x0014);
652 b43_write16(dev, 0x0568, 0x0826);
653 b43_write16(dev, 0x0500, 0x0000);
654 b43_write16(dev, 0x0502, 0x0030);
656 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
657 b43_radio_write16(dev, 0x0051, 0x0017);
658 for (i = 0x00; i < max_loop; i++) {
659 value = b43_read16(dev, 0x050E);
660 if (value & 0x0080)
661 break;
662 udelay(10);
664 for (i = 0x00; i < 0x0A; i++) {
665 value = b43_read16(dev, 0x050E);
666 if (value & 0x0400)
667 break;
668 udelay(10);
670 for (i = 0x00; i < 0x0A; i++) {
671 value = b43_read16(dev, 0x0690);
672 if (!(value & 0x0100))
673 break;
674 udelay(10);
676 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
677 b43_radio_write16(dev, 0x0051, 0x0037);
680 static void key_write(struct b43_wldev *dev,
681 u8 index, u8 algorithm, const u8 * key)
683 unsigned int i;
684 u32 offset;
685 u16 value;
686 u16 kidx;
688 /* Key index/algo block */
689 kidx = b43_kidx_to_fw(dev, index);
690 value = ((kidx << 4) | algorithm);
691 b43_shm_write16(dev, B43_SHM_SHARED,
692 B43_SHM_SH_KEYIDXBLOCK + (kidx * 2), value);
694 /* Write the key to the Key Table Pointer offset */
695 offset = dev->ktp + (index * B43_SEC_KEYSIZE);
696 for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
697 value = key[i];
698 value |= (u16) (key[i + 1]) << 8;
699 b43_shm_write16(dev, B43_SHM_SHARED, offset + i, value);
703 static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
705 u32 addrtmp[2] = { 0, 0, };
706 u8 per_sta_keys_start = 8;
708 if (b43_new_kidx_api(dev))
709 per_sta_keys_start = 4;
711 B43_WARN_ON(index < per_sta_keys_start);
712 /* We have two default TX keys and possibly two default RX keys.
713 * Physical mac 0 is mapped to physical key 4 or 8, depending
714 * on the firmware version.
715 * So we must adjust the index here.
717 index -= per_sta_keys_start;
719 if (addr) {
720 addrtmp[0] = addr[0];
721 addrtmp[0] |= ((u32) (addr[1]) << 8);
722 addrtmp[0] |= ((u32) (addr[2]) << 16);
723 addrtmp[0] |= ((u32) (addr[3]) << 24);
724 addrtmp[1] = addr[4];
725 addrtmp[1] |= ((u32) (addr[5]) << 8);
728 if (dev->dev->id.revision >= 5) {
729 /* Receive match transmitter address mechanism */
730 b43_shm_write32(dev, B43_SHM_RCMTA,
731 (index * 2) + 0, addrtmp[0]);
732 b43_shm_write16(dev, B43_SHM_RCMTA,
733 (index * 2) + 1, addrtmp[1]);
734 } else {
735 /* RXE (Receive Engine) and
736 * PSM (Programmable State Machine) mechanism
738 if (index < 8) {
739 /* TODO write to RCM 16, 19, 22 and 25 */
740 } else {
741 b43_shm_write32(dev, B43_SHM_SHARED,
742 B43_SHM_SH_PSM + (index * 6) + 0,
743 addrtmp[0]);
744 b43_shm_write16(dev, B43_SHM_SHARED,
745 B43_SHM_SH_PSM + (index * 6) + 4,
746 addrtmp[1]);
751 static void do_key_write(struct b43_wldev *dev,
752 u8 index, u8 algorithm,
753 const u8 * key, size_t key_len, const u8 * mac_addr)
755 u8 buf[B43_SEC_KEYSIZE] = { 0, };
756 u8 per_sta_keys_start = 8;
758 if (b43_new_kidx_api(dev))
759 per_sta_keys_start = 4;
761 B43_WARN_ON(index >= dev->max_nr_keys);
762 B43_WARN_ON(key_len > B43_SEC_KEYSIZE);
764 if (index >= per_sta_keys_start)
765 keymac_write(dev, index, NULL); /* First zero out mac. */
766 if (key)
767 memcpy(buf, key, key_len);
768 key_write(dev, index, algorithm, buf);
769 if (index >= per_sta_keys_start)
770 keymac_write(dev, index, mac_addr);
772 dev->key[index].algorithm = algorithm;
775 static int b43_key_write(struct b43_wldev *dev,
776 int index, u8 algorithm,
777 const u8 * key, size_t key_len,
778 const u8 * mac_addr,
779 struct ieee80211_key_conf *keyconf)
781 int i;
782 int sta_keys_start;
784 if (key_len > B43_SEC_KEYSIZE)
785 return -EINVAL;
786 for (i = 0; i < dev->max_nr_keys; i++) {
787 /* Check that we don't already have this key. */
788 B43_WARN_ON(dev->key[i].keyconf == keyconf);
790 if (index < 0) {
791 /* Either pairwise key or address is 00:00:00:00:00:00
792 * for transmit-only keys. Search the index. */
793 if (b43_new_kidx_api(dev))
794 sta_keys_start = 4;
795 else
796 sta_keys_start = 8;
797 for (i = sta_keys_start; i < dev->max_nr_keys; i++) {
798 if (!dev->key[i].keyconf) {
799 /* found empty */
800 index = i;
801 break;
804 if (index < 0) {
805 b43err(dev->wl, "Out of hardware key memory\n");
806 return -ENOSPC;
808 } else
809 B43_WARN_ON(index > 3);
811 do_key_write(dev, index, algorithm, key, key_len, mac_addr);
812 if ((index <= 3) && !b43_new_kidx_api(dev)) {
813 /* Default RX key */
814 B43_WARN_ON(mac_addr);
815 do_key_write(dev, index + 4, algorithm, key, key_len, NULL);
817 keyconf->hw_key_idx = index;
818 dev->key[index].keyconf = keyconf;
820 return 0;
823 static int b43_key_clear(struct b43_wldev *dev, int index)
825 if (B43_WARN_ON((index < 0) || (index >= dev->max_nr_keys)))
826 return -EINVAL;
827 do_key_write(dev, index, B43_SEC_ALGO_NONE,
828 NULL, B43_SEC_KEYSIZE, NULL);
829 if ((index <= 3) && !b43_new_kidx_api(dev)) {
830 do_key_write(dev, index + 4, B43_SEC_ALGO_NONE,
831 NULL, B43_SEC_KEYSIZE, NULL);
833 dev->key[index].keyconf = NULL;
835 return 0;
838 static void b43_clear_keys(struct b43_wldev *dev)
840 int i;
842 for (i = 0; i < dev->max_nr_keys; i++)
843 b43_key_clear(dev, i);
846 void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
848 u32 macctl;
849 u16 ucstat;
850 bool hwps;
851 bool awake;
852 int i;
854 B43_WARN_ON((ps_flags & B43_PS_ENABLED) &&
855 (ps_flags & B43_PS_DISABLED));
856 B43_WARN_ON((ps_flags & B43_PS_AWAKE) && (ps_flags & B43_PS_ASLEEP));
858 if (ps_flags & B43_PS_ENABLED) {
859 hwps = 1;
860 } else if (ps_flags & B43_PS_DISABLED) {
861 hwps = 0;
862 } else {
863 //TODO: If powersave is not off and FIXME is not set and we are not in adhoc
864 // and thus is not an AP and we are associated, set bit 25
866 if (ps_flags & B43_PS_AWAKE) {
867 awake = 1;
868 } else if (ps_flags & B43_PS_ASLEEP) {
869 awake = 0;
870 } else {
871 //TODO: If the device is awake or this is an AP, or we are scanning, or FIXME,
872 // or we are associated, or FIXME, or the latest PS-Poll packet sent was
873 // successful, set bit26
876 /* FIXME: For now we force awake-on and hwps-off */
877 hwps = 0;
878 awake = 1;
880 macctl = b43_read32(dev, B43_MMIO_MACCTL);
881 if (hwps)
882 macctl |= B43_MACCTL_HWPS;
883 else
884 macctl &= ~B43_MACCTL_HWPS;
885 if (awake)
886 macctl |= B43_MACCTL_AWAKE;
887 else
888 macctl &= ~B43_MACCTL_AWAKE;
889 b43_write32(dev, B43_MMIO_MACCTL, macctl);
890 /* Commit write */
891 b43_read32(dev, B43_MMIO_MACCTL);
892 if (awake && dev->dev->id.revision >= 5) {
893 /* Wait for the microcode to wake up. */
894 for (i = 0; i < 100; i++) {
895 ucstat = b43_shm_read16(dev, B43_SHM_SHARED,
896 B43_SHM_SH_UCODESTAT);
897 if (ucstat != B43_SHM_SH_UCODESTAT_SLEEP)
898 break;
899 udelay(10);
904 /* Turn the Analog ON/OFF */
905 static void b43_switch_analog(struct b43_wldev *dev, int on)
907 b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
910 void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
912 u32 tmslow;
913 u32 macctl;
915 flags |= B43_TMSLOW_PHYCLKEN;
916 flags |= B43_TMSLOW_PHYRESET;
917 ssb_device_enable(dev->dev, flags);
918 msleep(2); /* Wait for the PLL to turn on. */
920 /* Now take the PHY out of Reset again */
921 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
922 tmslow |= SSB_TMSLOW_FGC;
923 tmslow &= ~B43_TMSLOW_PHYRESET;
924 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
925 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
926 msleep(1);
927 tmslow &= ~SSB_TMSLOW_FGC;
928 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
929 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
930 msleep(1);
932 /* Turn Analog ON */
933 b43_switch_analog(dev, 1);
935 macctl = b43_read32(dev, B43_MMIO_MACCTL);
936 macctl &= ~B43_MACCTL_GMODE;
937 if (flags & B43_TMSLOW_GMODE)
938 macctl |= B43_MACCTL_GMODE;
939 macctl |= B43_MACCTL_IHR_ENABLED;
940 b43_write32(dev, B43_MMIO_MACCTL, macctl);
943 static void handle_irq_transmit_status(struct b43_wldev *dev)
945 u32 v0, v1;
946 u16 tmp;
947 struct b43_txstatus stat;
949 while (1) {
950 v0 = b43_read32(dev, B43_MMIO_XMITSTAT_0);
951 if (!(v0 & 0x00000001))
952 break;
953 v1 = b43_read32(dev, B43_MMIO_XMITSTAT_1);
955 stat.cookie = (v0 >> 16);
956 stat.seq = (v1 & 0x0000FFFF);
957 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
958 tmp = (v0 & 0x0000FFFF);
959 stat.frame_count = ((tmp & 0xF000) >> 12);
960 stat.rts_count = ((tmp & 0x0F00) >> 8);
961 stat.supp_reason = ((tmp & 0x001C) >> 2);
962 stat.pm_indicated = !!(tmp & 0x0080);
963 stat.intermediate = !!(tmp & 0x0040);
964 stat.for_ampdu = !!(tmp & 0x0020);
965 stat.acked = !!(tmp & 0x0002);
967 b43_handle_txstatus(dev, &stat);
971 static void drain_txstatus_queue(struct b43_wldev *dev)
973 u32 dummy;
975 if (dev->dev->id.revision < 5)
976 return;
977 /* Read all entries from the microcode TXstatus FIFO
978 * and throw them away.
980 while (1) {
981 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_0);
982 if (!(dummy & 0x00000001))
983 break;
984 dummy = b43_read32(dev, B43_MMIO_XMITSTAT_1);
988 static u32 b43_jssi_read(struct b43_wldev *dev)
990 u32 val = 0;
992 val = b43_shm_read16(dev, B43_SHM_SHARED, 0x08A);
993 val <<= 16;
994 val |= b43_shm_read16(dev, B43_SHM_SHARED, 0x088);
996 return val;
999 static void b43_jssi_write(struct b43_wldev *dev, u32 jssi)
1001 b43_shm_write16(dev, B43_SHM_SHARED, 0x088, (jssi & 0x0000FFFF));
1002 b43_shm_write16(dev, B43_SHM_SHARED, 0x08A, (jssi & 0xFFFF0000) >> 16);
1005 static void b43_generate_noise_sample(struct b43_wldev *dev)
1007 b43_jssi_write(dev, 0x7F7F7F7F);
1008 b43_write32(dev, B43_MMIO_STATUS2_BITFIELD,
1009 b43_read32(dev, B43_MMIO_STATUS2_BITFIELD)
1010 | (1 << 4));
1011 B43_WARN_ON(dev->noisecalc.channel_at_start != dev->phy.channel);
1014 static void b43_calculate_link_quality(struct b43_wldev *dev)
1016 /* Top half of Link Quality calculation. */
1018 if (dev->noisecalc.calculation_running)
1019 return;
1020 dev->noisecalc.channel_at_start = dev->phy.channel;
1021 dev->noisecalc.calculation_running = 1;
1022 dev->noisecalc.nr_samples = 0;
1024 b43_generate_noise_sample(dev);
1027 static void handle_irq_noise(struct b43_wldev *dev)
1029 struct b43_phy *phy = &dev->phy;
1030 u16 tmp;
1031 u8 noise[4];
1032 u8 i, j;
1033 s32 average;
1035 /* Bottom half of Link Quality calculation. */
1037 B43_WARN_ON(!dev->noisecalc.calculation_running);
1038 if (dev->noisecalc.channel_at_start != phy->channel)
1039 goto drop_calculation;
1040 *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
1041 if (noise[0] == 0x7F || noise[1] == 0x7F ||
1042 noise[2] == 0x7F || noise[3] == 0x7F)
1043 goto generate_new;
1045 /* Get the noise samples. */
1046 B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
1047 i = dev->noisecalc.nr_samples;
1048 noise[0] = limit_value(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1049 noise[1] = limit_value(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1050 noise[2] = limit_value(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1051 noise[3] = limit_value(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
1052 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
1053 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
1054 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
1055 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
1056 dev->noisecalc.nr_samples++;
1057 if (dev->noisecalc.nr_samples == 8) {
1058 /* Calculate the Link Quality by the noise samples. */
1059 average = 0;
1060 for (i = 0; i < 8; i++) {
1061 for (j = 0; j < 4; j++)
1062 average += dev->noisecalc.samples[i][j];
1064 average /= (8 * 4);
1065 average *= 125;
1066 average += 64;
1067 average /= 128;
1068 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x40C);
1069 tmp = (tmp / 128) & 0x1F;
1070 if (tmp >= 8)
1071 average += 2;
1072 else
1073 average -= 25;
1074 if (tmp == 8)
1075 average -= 72;
1076 else
1077 average -= 48;
1079 dev->stats.link_noise = average;
1080 drop_calculation:
1081 dev->noisecalc.calculation_running = 0;
1082 return;
1084 generate_new:
1085 b43_generate_noise_sample(dev);
1088 static void handle_irq_tbtt_indication(struct b43_wldev *dev)
1090 if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
1091 ///TODO: PS TBTT
1092 } else {
1093 if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
1094 b43_power_saving_ctl_bits(dev, 0);
1096 dev->reg124_set_0x4 = 0;
1097 if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
1098 dev->reg124_set_0x4 = 1;
1101 static void handle_irq_atim_end(struct b43_wldev *dev)
1103 if (!dev->reg124_set_0x4 /*FIXME rename this variable */ )
1104 return;
1105 b43_write32(dev, B43_MMIO_STATUS2_BITFIELD,
1106 b43_read32(dev, B43_MMIO_STATUS2_BITFIELD)
1107 | 0x4);
1110 static void handle_irq_pmq(struct b43_wldev *dev)
1112 u32 tmp;
1114 //TODO: AP mode.
1116 while (1) {
1117 tmp = b43_read32(dev, B43_MMIO_PS_STATUS);
1118 if (!(tmp & 0x00000008))
1119 break;
1121 /* 16bit write is odd, but correct. */
1122 b43_write16(dev, B43_MMIO_PS_STATUS, 0x0002);
1125 static void b43_write_template_common(struct b43_wldev *dev,
1126 const u8 * data, u16 size,
1127 u16 ram_offset,
1128 u16 shm_size_offset, u8 rate)
1130 u32 i, tmp;
1131 struct b43_plcp_hdr4 plcp;
1133 plcp.data = 0;
1134 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1135 b43_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
1136 ram_offset += sizeof(u32);
1137 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
1138 * So leave the first two bytes of the next write blank.
1140 tmp = (u32) (data[0]) << 16;
1141 tmp |= (u32) (data[1]) << 24;
1142 b43_ram_write(dev, ram_offset, tmp);
1143 ram_offset += sizeof(u32);
1144 for (i = 2; i < size; i += sizeof(u32)) {
1145 tmp = (u32) (data[i + 0]);
1146 if (i + 1 < size)
1147 tmp |= (u32) (data[i + 1]) << 8;
1148 if (i + 2 < size)
1149 tmp |= (u32) (data[i + 2]) << 16;
1150 if (i + 3 < size)
1151 tmp |= (u32) (data[i + 3]) << 24;
1152 b43_ram_write(dev, ram_offset + i - 2, tmp);
1154 b43_shm_write16(dev, B43_SHM_SHARED, shm_size_offset,
1155 size + sizeof(struct b43_plcp_hdr6));
1158 static void b43_write_beacon_template(struct b43_wldev *dev,
1159 u16 ram_offset,
1160 u16 shm_size_offset, u8 rate)
1162 int len;
1163 const u8 *data;
1165 B43_WARN_ON(!dev->cached_beacon);
1166 len = min((size_t) dev->cached_beacon->len,
1167 0x200 - sizeof(struct b43_plcp_hdr6));
1168 data = (const u8 *)(dev->cached_beacon->data);
1169 b43_write_template_common(dev, data,
1170 len, ram_offset, shm_size_offset, rate);
1173 static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
1174 u16 shm_offset, u16 size, u8 rate)
1176 struct b43_plcp_hdr4 plcp;
1177 u32 tmp;
1178 __le16 dur;
1180 plcp.data = 0;
1181 b43_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
1182 dur = ieee80211_generic_frame_duration(dev->wl->hw,
1183 dev->wl->if_id, size,
1184 B43_RATE_TO_BASE100KBPS(rate));
1185 /* Write PLCP in two parts and timing for packet transfer */
1186 tmp = le32_to_cpu(plcp.data);
1187 b43_shm_write16(dev, B43_SHM_SHARED, shm_offset, tmp & 0xFFFF);
1188 b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 2, tmp >> 16);
1189 b43_shm_write16(dev, B43_SHM_SHARED, shm_offset + 6, le16_to_cpu(dur));
1192 /* Instead of using custom probe response template, this function
1193 * just patches custom beacon template by:
1194 * 1) Changing packet type
1195 * 2) Patching duration field
1196 * 3) Stripping TIM
1198 static u8 *b43_generate_probe_resp(struct b43_wldev *dev,
1199 u16 * dest_size, u8 rate)
1201 const u8 *src_data;
1202 u8 *dest_data;
1203 u16 src_size, elem_size, src_pos, dest_pos;
1204 __le16 dur;
1205 struct ieee80211_hdr *hdr;
1207 B43_WARN_ON(!dev->cached_beacon);
1208 src_size = dev->cached_beacon->len;
1209 src_data = (const u8 *)dev->cached_beacon->data;
1211 if (unlikely(src_size < 0x24)) {
1212 b43dbg(dev->wl, "b43_generate_probe_resp: " "invalid beacon\n");
1213 return NULL;
1216 dest_data = kmalloc(src_size, GFP_ATOMIC);
1217 if (unlikely(!dest_data))
1218 return NULL;
1220 /* 0x24 is offset of first variable-len Information-Element
1221 * in beacon frame.
1223 memcpy(dest_data, src_data, 0x24);
1224 src_pos = dest_pos = 0x24;
1225 for (; src_pos < src_size - 2; src_pos += elem_size) {
1226 elem_size = src_data[src_pos + 1] + 2;
1227 if (src_data[src_pos] != 0x05) { /* TIM */
1228 memcpy(dest_data + dest_pos, src_data + src_pos,
1229 elem_size);
1230 dest_pos += elem_size;
1233 *dest_size = dest_pos;
1234 hdr = (struct ieee80211_hdr *)dest_data;
1236 /* Set the frame control. */
1237 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1238 IEEE80211_STYPE_PROBE_RESP);
1239 dur = ieee80211_generic_frame_duration(dev->wl->hw,
1240 dev->wl->if_id, *dest_size,
1241 B43_RATE_TO_BASE100KBPS(rate));
1242 hdr->duration_id = dur;
1244 return dest_data;
1247 static void b43_write_probe_resp_template(struct b43_wldev *dev,
1248 u16 ram_offset,
1249 u16 shm_size_offset, u8 rate)
1251 u8 *probe_resp_data;
1252 u16 size;
1254 B43_WARN_ON(!dev->cached_beacon);
1255 size = dev->cached_beacon->len;
1256 probe_resp_data = b43_generate_probe_resp(dev, &size, rate);
1257 if (unlikely(!probe_resp_data))
1258 return;
1260 /* Looks like PLCP headers plus packet timings are stored for
1261 * all possible basic rates
1263 b43_write_probe_resp_plcp(dev, 0x31A, size, B43_CCK_RATE_1MB);
1264 b43_write_probe_resp_plcp(dev, 0x32C, size, B43_CCK_RATE_2MB);
1265 b43_write_probe_resp_plcp(dev, 0x33E, size, B43_CCK_RATE_5MB);
1266 b43_write_probe_resp_plcp(dev, 0x350, size, B43_CCK_RATE_11MB);
1268 size = min((size_t) size, 0x200 - sizeof(struct b43_plcp_hdr6));
1269 b43_write_template_common(dev, probe_resp_data,
1270 size, ram_offset, shm_size_offset, rate);
1271 kfree(probe_resp_data);
1274 static int b43_refresh_cached_beacon(struct b43_wldev *dev,
1275 struct sk_buff *beacon)
1277 if (dev->cached_beacon)
1278 kfree_skb(dev->cached_beacon);
1279 dev->cached_beacon = beacon;
1281 return 0;
1284 static void b43_update_templates(struct b43_wldev *dev)
1286 u32 status;
1288 B43_WARN_ON(!dev->cached_beacon);
1290 b43_write_beacon_template(dev, 0x68, 0x18, B43_CCK_RATE_1MB);
1291 b43_write_beacon_template(dev, 0x468, 0x1A, B43_CCK_RATE_1MB);
1292 b43_write_probe_resp_template(dev, 0x268, 0x4A, B43_CCK_RATE_11MB);
1294 status = b43_read32(dev, B43_MMIO_STATUS2_BITFIELD);
1295 status |= 0x03;
1296 b43_write32(dev, B43_MMIO_STATUS2_BITFIELD, status);
1299 static void b43_refresh_templates(struct b43_wldev *dev, struct sk_buff *beacon)
1301 int err;
1303 err = b43_refresh_cached_beacon(dev, beacon);
1304 if (unlikely(err))
1305 return;
1306 b43_update_templates(dev);
1309 static void b43_set_ssid(struct b43_wldev *dev, const u8 * ssid, u8 ssid_len)
1311 u32 tmp;
1312 u16 i, len;
1314 len = min((u16) ssid_len, (u16) 0x100);
1315 for (i = 0; i < len; i += sizeof(u32)) {
1316 tmp = (u32) (ssid[i + 0]);
1317 if (i + 1 < len)
1318 tmp |= (u32) (ssid[i + 1]) << 8;
1319 if (i + 2 < len)
1320 tmp |= (u32) (ssid[i + 2]) << 16;
1321 if (i + 3 < len)
1322 tmp |= (u32) (ssid[i + 3]) << 24;
1323 b43_shm_write32(dev, B43_SHM_SHARED, 0x380 + i, tmp);
1325 b43_shm_write16(dev, B43_SHM_SHARED, 0x48, len);
1328 static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
1330 b43_time_lock(dev);
1331 if (dev->dev->id.revision >= 3) {
1332 b43_write32(dev, 0x188, (beacon_int << 16));
1333 } else {
1334 b43_write16(dev, 0x606, (beacon_int >> 6));
1335 b43_write16(dev, 0x610, beacon_int);
1337 b43_time_unlock(dev);
1340 static void handle_irq_beacon(struct b43_wldev *dev)
1342 u32 status;
1344 if (!b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP))
1345 return;
1347 dev->irq_savedstate &= ~B43_IRQ_BEACON;
1348 status = b43_read32(dev, B43_MMIO_STATUS2_BITFIELD);
1350 if (!dev->cached_beacon || ((status & 0x1) && (status & 0x2))) {
1351 /* ACK beacon IRQ. */
1352 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
1353 dev->irq_savedstate |= B43_IRQ_BEACON;
1354 if (dev->cached_beacon)
1355 kfree_skb(dev->cached_beacon);
1356 dev->cached_beacon = NULL;
1357 return;
1359 if (!(status & 0x1)) {
1360 b43_write_beacon_template(dev, 0x68, 0x18, B43_CCK_RATE_1MB);
1361 status |= 0x1;
1362 b43_write32(dev, B43_MMIO_STATUS2_BITFIELD, status);
1364 if (!(status & 0x2)) {
1365 b43_write_beacon_template(dev, 0x468, 0x1A, B43_CCK_RATE_1MB);
1366 status |= 0x2;
1367 b43_write32(dev, B43_MMIO_STATUS2_BITFIELD, status);
1371 static void handle_irq_ucode_debug(struct b43_wldev *dev)
1373 //TODO
1376 /* Interrupt handler bottom-half */
1377 static void b43_interrupt_tasklet(struct b43_wldev *dev)
1379 u32 reason;
1380 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1381 u32 merged_dma_reason = 0;
1382 int i;
1383 unsigned long flags;
1385 spin_lock_irqsave(&dev->wl->irq_lock, flags);
1387 B43_WARN_ON(b43_status(dev) != B43_STAT_STARTED);
1389 reason = dev->irq_reason;
1390 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1391 dma_reason[i] = dev->dma_reason[i];
1392 merged_dma_reason |= dma_reason[i];
1395 if (unlikely(reason & B43_IRQ_MAC_TXERR))
1396 b43err(dev->wl, "MAC transmission error\n");
1398 if (unlikely(reason & B43_IRQ_PHY_TXERR)) {
1399 b43err(dev->wl, "PHY transmission error\n");
1400 rmb();
1401 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1402 atomic_set(&dev->phy.txerr_cnt,
1403 B43_PHY_TX_BADNESS_LIMIT);
1404 b43err(dev->wl, "Too many PHY TX errors, "
1405 "restarting the controller\n");
1406 b43_controller_restart(dev, "PHY TX errors");
1410 if (unlikely(merged_dma_reason & (B43_DMAIRQ_FATALMASK |
1411 B43_DMAIRQ_NONFATALMASK))) {
1412 if (merged_dma_reason & B43_DMAIRQ_FATALMASK) {
1413 b43err(dev->wl, "Fatal DMA error: "
1414 "0x%08X, 0x%08X, 0x%08X, "
1415 "0x%08X, 0x%08X, 0x%08X\n",
1416 dma_reason[0], dma_reason[1],
1417 dma_reason[2], dma_reason[3],
1418 dma_reason[4], dma_reason[5]);
1419 b43_controller_restart(dev, "DMA error");
1420 mmiowb();
1421 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1422 return;
1424 if (merged_dma_reason & B43_DMAIRQ_NONFATALMASK) {
1425 b43err(dev->wl, "DMA error: "
1426 "0x%08X, 0x%08X, 0x%08X, "
1427 "0x%08X, 0x%08X, 0x%08X\n",
1428 dma_reason[0], dma_reason[1],
1429 dma_reason[2], dma_reason[3],
1430 dma_reason[4], dma_reason[5]);
1434 if (unlikely(reason & B43_IRQ_UCODE_DEBUG))
1435 handle_irq_ucode_debug(dev);
1436 if (reason & B43_IRQ_TBTT_INDI)
1437 handle_irq_tbtt_indication(dev);
1438 if (reason & B43_IRQ_ATIM_END)
1439 handle_irq_atim_end(dev);
1440 if (reason & B43_IRQ_BEACON)
1441 handle_irq_beacon(dev);
1442 if (reason & B43_IRQ_PMQ)
1443 handle_irq_pmq(dev);
1444 if (reason & B43_IRQ_TXFIFO_FLUSH_OK)
1445 ;/* TODO */
1446 if (reason & B43_IRQ_NOISESAMPLE_OK)
1447 handle_irq_noise(dev);
1449 /* Check the DMA reason registers for received data. */
1450 if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
1451 if (b43_using_pio(dev))
1452 b43_pio_rx(dev->pio.queue0);
1453 else
1454 b43_dma_rx(dev->dma.rx_ring0);
1456 B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
1457 B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
1458 if (dma_reason[3] & B43_DMAIRQ_RX_DONE) {
1459 if (b43_using_pio(dev))
1460 b43_pio_rx(dev->pio.queue3);
1461 else
1462 b43_dma_rx(dev->dma.rx_ring3);
1464 B43_WARN_ON(dma_reason[4] & B43_DMAIRQ_RX_DONE);
1465 B43_WARN_ON(dma_reason[5] & B43_DMAIRQ_RX_DONE);
1467 if (reason & B43_IRQ_TX_OK)
1468 handle_irq_transmit_status(dev);
1470 b43_interrupt_enable(dev, dev->irq_savedstate);
1471 mmiowb();
1472 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1475 static void pio_irq_workaround(struct b43_wldev *dev, u16 base, int queueidx)
1477 u16 rxctl;
1479 rxctl = b43_read16(dev, base + B43_PIO_RXCTL);
1480 if (rxctl & B43_PIO_RXCTL_DATAAVAILABLE)
1481 dev->dma_reason[queueidx] |= B43_DMAIRQ_RX_DONE;
1482 else
1483 dev->dma_reason[queueidx] &= ~B43_DMAIRQ_RX_DONE;
1486 static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
1488 if (b43_using_pio(dev) &&
1489 (dev->dev->id.revision < 3) &&
1490 (!(reason & B43_IRQ_PIO_WORKAROUND))) {
1491 /* Apply a PIO specific workaround to the dma_reasons */
1492 pio_irq_workaround(dev, B43_MMIO_PIO1_BASE, 0);
1493 pio_irq_workaround(dev, B43_MMIO_PIO2_BASE, 1);
1494 pio_irq_workaround(dev, B43_MMIO_PIO3_BASE, 2);
1495 pio_irq_workaround(dev, B43_MMIO_PIO4_BASE, 3);
1498 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, reason);
1500 b43_write32(dev, B43_MMIO_DMA0_REASON, dev->dma_reason[0]);
1501 b43_write32(dev, B43_MMIO_DMA1_REASON, dev->dma_reason[1]);
1502 b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
1503 b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
1504 b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
1505 b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
1508 /* Interrupt handler top-half */
1509 static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
1511 irqreturn_t ret = IRQ_NONE;
1512 struct b43_wldev *dev = dev_id;
1513 u32 reason;
1515 if (!dev)
1516 return IRQ_NONE;
1518 spin_lock(&dev->wl->irq_lock);
1520 if (b43_status(dev) < B43_STAT_STARTED)
1521 goto out;
1522 reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1523 if (reason == 0xffffffff) /* shared IRQ */
1524 goto out;
1525 ret = IRQ_HANDLED;
1526 reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
1527 if (!reason)
1528 goto out;
1530 dev->dma_reason[0] = b43_read32(dev, B43_MMIO_DMA0_REASON)
1531 & 0x0001DC00;
1532 dev->dma_reason[1] = b43_read32(dev, B43_MMIO_DMA1_REASON)
1533 & 0x0000DC00;
1534 dev->dma_reason[2] = b43_read32(dev, B43_MMIO_DMA2_REASON)
1535 & 0x0000DC00;
1536 dev->dma_reason[3] = b43_read32(dev, B43_MMIO_DMA3_REASON)
1537 & 0x0001DC00;
1538 dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
1539 & 0x0000DC00;
1540 dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
1541 & 0x0000DC00;
1543 b43_interrupt_ack(dev, reason);
1544 /* disable all IRQs. They are enabled again in the bottom half. */
1545 dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
1546 /* save the reason code and call our bottom half. */
1547 dev->irq_reason = reason;
1548 tasklet_schedule(&dev->isr_tasklet);
1549 out:
1550 mmiowb();
1551 spin_unlock(&dev->wl->irq_lock);
1553 return ret;
1556 static void b43_release_firmware(struct b43_wldev *dev)
1558 release_firmware(dev->fw.ucode);
1559 dev->fw.ucode = NULL;
1560 release_firmware(dev->fw.pcm);
1561 dev->fw.pcm = NULL;
1562 release_firmware(dev->fw.initvals);
1563 dev->fw.initvals = NULL;
1564 release_firmware(dev->fw.initvals_band);
1565 dev->fw.initvals_band = NULL;
1568 static void b43_print_fw_helptext(struct b43_wl *wl)
1570 b43err(wl, "You must go to "
1571 "http://linuxwireless.org/en/users/Drivers/b43#devicefirmware "
1572 "and download the correct firmware (version 4).\n");
1575 static int do_request_fw(struct b43_wldev *dev,
1576 const char *name,
1577 const struct firmware **fw)
1579 char path[sizeof(modparam_fwpostfix) + 32];
1580 struct b43_fw_header *hdr;
1581 u32 size;
1582 int err;
1584 if (!name)
1585 return 0;
1587 snprintf(path, ARRAY_SIZE(path),
1588 "b43%s/%s.fw",
1589 modparam_fwpostfix, name);
1590 err = request_firmware(fw, path, dev->dev->dev);
1591 if (err) {
1592 b43err(dev->wl, "Firmware file \"%s\" not found "
1593 "or load failed.\n", path);
1594 return err;
1596 if ((*fw)->size < sizeof(struct b43_fw_header))
1597 goto err_format;
1598 hdr = (struct b43_fw_header *)((*fw)->data);
1599 switch (hdr->type) {
1600 case B43_FW_TYPE_UCODE:
1601 case B43_FW_TYPE_PCM:
1602 size = be32_to_cpu(hdr->size);
1603 if (size != (*fw)->size - sizeof(struct b43_fw_header))
1604 goto err_format;
1605 /* fallthrough */
1606 case B43_FW_TYPE_IV:
1607 if (hdr->ver != 1)
1608 goto err_format;
1609 break;
1610 default:
1611 goto err_format;
1614 return err;
1616 err_format:
1617 b43err(dev->wl, "Firmware file \"%s\" format error.\n", path);
1618 return -EPROTO;
1621 static int b43_request_firmware(struct b43_wldev *dev)
1623 struct b43_firmware *fw = &dev->fw;
1624 const u8 rev = dev->dev->id.revision;
1625 const char *filename;
1626 u32 tmshigh;
1627 int err;
1629 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
1630 if (!fw->ucode) {
1631 if ((rev >= 5) && (rev <= 10))
1632 filename = "ucode5";
1633 else if ((rev >= 11) && (rev <= 12))
1634 filename = "ucode11";
1635 else if (rev >= 13)
1636 filename = "ucode13";
1637 else
1638 goto err_no_ucode;
1639 err = do_request_fw(dev, filename, &fw->ucode);
1640 if (err)
1641 goto err_load;
1643 if (!fw->pcm) {
1644 if ((rev >= 5) && (rev <= 10))
1645 filename = "pcm5";
1646 else if (rev >= 11)
1647 filename = NULL;
1648 else
1649 goto err_no_pcm;
1650 err = do_request_fw(dev, filename, &fw->pcm);
1651 if (err)
1652 goto err_load;
1654 if (!fw->initvals) {
1655 switch (dev->phy.type) {
1656 case B43_PHYTYPE_A:
1657 if ((rev >= 5) && (rev <= 10)) {
1658 if (tmshigh & B43_TMSHIGH_GPHY)
1659 filename = "a0g1initvals5";
1660 else
1661 filename = "a0g0initvals5";
1662 } else
1663 goto err_no_initvals;
1664 break;
1665 case B43_PHYTYPE_G:
1666 if ((rev >= 5) && (rev <= 10))
1667 filename = "b0g0initvals5";
1668 else if (rev >= 13)
1669 filename = "lp0initvals13";
1670 else
1671 goto err_no_initvals;
1672 break;
1673 default:
1674 goto err_no_initvals;
1676 err = do_request_fw(dev, filename, &fw->initvals);
1677 if (err)
1678 goto err_load;
1680 if (!fw->initvals_band) {
1681 switch (dev->phy.type) {
1682 case B43_PHYTYPE_A:
1683 if ((rev >= 5) && (rev <= 10)) {
1684 if (tmshigh & B43_TMSHIGH_GPHY)
1685 filename = "a0g1bsinitvals5";
1686 else
1687 filename = "a0g0bsinitvals5";
1688 } else if (rev >= 11)
1689 filename = NULL;
1690 else
1691 goto err_no_initvals;
1692 break;
1693 case B43_PHYTYPE_G:
1694 if ((rev >= 5) && (rev <= 10))
1695 filename = "b0g0bsinitvals5";
1696 else if (rev >= 11)
1697 filename = NULL;
1698 else
1699 goto err_no_initvals;
1700 break;
1701 default:
1702 goto err_no_initvals;
1704 err = do_request_fw(dev, filename, &fw->initvals_band);
1705 if (err)
1706 goto err_load;
1709 return 0;
1711 err_load:
1712 b43_print_fw_helptext(dev->wl);
1713 goto error;
1715 err_no_ucode:
1716 err = -ENODEV;
1717 b43err(dev->wl, "No microcode available for core rev %u\n", rev);
1718 goto error;
1720 err_no_pcm:
1721 err = -ENODEV;
1722 b43err(dev->wl, "No PCM available for core rev %u\n", rev);
1723 goto error;
1725 err_no_initvals:
1726 err = -ENODEV;
1727 b43err(dev->wl, "No Initial Values firmware file for PHY %u, "
1728 "core rev %u\n", dev->phy.type, rev);
1729 goto error;
1731 error:
1732 b43_release_firmware(dev);
1733 return err;
1736 static int b43_upload_microcode(struct b43_wldev *dev)
1738 const size_t hdr_len = sizeof(struct b43_fw_header);
1739 const __be32 *data;
1740 unsigned int i, len;
1741 u16 fwrev, fwpatch, fwdate, fwtime;
1742 u32 tmp;
1743 int err = 0;
1745 /* Upload Microcode. */
1746 data = (__be32 *) (dev->fw.ucode->data + hdr_len);
1747 len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
1748 b43_shm_control_word(dev, B43_SHM_UCODE | B43_SHM_AUTOINC_W, 0x0000);
1749 for (i = 0; i < len; i++) {
1750 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
1751 udelay(10);
1754 if (dev->fw.pcm) {
1755 /* Upload PCM data. */
1756 data = (__be32 *) (dev->fw.pcm->data + hdr_len);
1757 len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
1758 b43_shm_control_word(dev, B43_SHM_HW, 0x01EA);
1759 b43_write32(dev, B43_MMIO_SHM_DATA, 0x00004000);
1760 /* No need for autoinc bit in SHM_HW */
1761 b43_shm_control_word(dev, B43_SHM_HW, 0x01EB);
1762 for (i = 0; i < len; i++) {
1763 b43_write32(dev, B43_MMIO_SHM_DATA, be32_to_cpu(data[i]));
1764 udelay(10);
1768 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_ALL);
1769 b43_write32(dev, B43_MMIO_MACCTL,
1770 B43_MACCTL_PSM_RUN |
1771 B43_MACCTL_IHR_ENABLED | B43_MACCTL_INFRA);
1773 /* Wait for the microcode to load and respond */
1774 i = 0;
1775 while (1) {
1776 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1777 if (tmp == B43_IRQ_MAC_SUSPENDED)
1778 break;
1779 i++;
1780 if (i >= 50) {
1781 b43err(dev->wl, "Microcode not responding\n");
1782 b43_print_fw_helptext(dev->wl);
1783 err = -ENODEV;
1784 goto out;
1786 udelay(10);
1788 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON); /* dummy read */
1790 /* Get and check the revisions. */
1791 fwrev = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEREV);
1792 fwpatch = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEPATCH);
1793 fwdate = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODEDATE);
1794 fwtime = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_UCODETIME);
1796 if (fwrev <= 0x128) {
1797 b43err(dev->wl, "YOUR FIRMWARE IS TOO OLD. Firmware from "
1798 "binary drivers older than version 4.x is unsupported. "
1799 "You must upgrade your firmware files.\n");
1800 b43_print_fw_helptext(dev->wl);
1801 b43_write32(dev, B43_MMIO_MACCTL, 0);
1802 err = -EOPNOTSUPP;
1803 goto out;
1805 b43dbg(dev->wl, "Loading firmware version %u.%u "
1806 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
1807 fwrev, fwpatch,
1808 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
1809 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
1811 dev->fw.rev = fwrev;
1812 dev->fw.patch = fwpatch;
1814 out:
1815 return err;
1818 static int b43_write_initvals(struct b43_wldev *dev,
1819 const struct b43_iv *ivals,
1820 size_t count,
1821 size_t array_size)
1823 const struct b43_iv *iv;
1824 u16 offset;
1825 size_t i;
1826 bool bit32;
1828 BUILD_BUG_ON(sizeof(struct b43_iv) != 6);
1829 iv = ivals;
1830 for (i = 0; i < count; i++) {
1831 if (array_size < sizeof(iv->offset_size))
1832 goto err_format;
1833 array_size -= sizeof(iv->offset_size);
1834 offset = be16_to_cpu(iv->offset_size);
1835 bit32 = !!(offset & B43_IV_32BIT);
1836 offset &= B43_IV_OFFSET_MASK;
1837 if (offset >= 0x1000)
1838 goto err_format;
1839 if (bit32) {
1840 u32 value;
1842 if (array_size < sizeof(iv->data.d32))
1843 goto err_format;
1844 array_size -= sizeof(iv->data.d32);
1846 value = be32_to_cpu(get_unaligned(&iv->data.d32));
1847 b43_write32(dev, offset, value);
1849 iv = (const struct b43_iv *)((const uint8_t *)iv +
1850 sizeof(__be16) +
1851 sizeof(__be32));
1852 } else {
1853 u16 value;
1855 if (array_size < sizeof(iv->data.d16))
1856 goto err_format;
1857 array_size -= sizeof(iv->data.d16);
1859 value = be16_to_cpu(iv->data.d16);
1860 b43_write16(dev, offset, value);
1862 iv = (const struct b43_iv *)((const uint8_t *)iv +
1863 sizeof(__be16) +
1864 sizeof(__be16));
1867 if (array_size)
1868 goto err_format;
1870 return 0;
1872 err_format:
1873 b43err(dev->wl, "Initial Values Firmware file-format error.\n");
1874 b43_print_fw_helptext(dev->wl);
1876 return -EPROTO;
1879 static int b43_upload_initvals(struct b43_wldev *dev)
1881 const size_t hdr_len = sizeof(struct b43_fw_header);
1882 const struct b43_fw_header *hdr;
1883 struct b43_firmware *fw = &dev->fw;
1884 const struct b43_iv *ivals;
1885 size_t count;
1886 int err;
1888 hdr = (const struct b43_fw_header *)(fw->initvals->data);
1889 ivals = (const struct b43_iv *)(fw->initvals->data + hdr_len);
1890 count = be32_to_cpu(hdr->size);
1891 err = b43_write_initvals(dev, ivals, count,
1892 fw->initvals->size - hdr_len);
1893 if (err)
1894 goto out;
1895 if (fw->initvals_band) {
1896 hdr = (const struct b43_fw_header *)(fw->initvals_band->data);
1897 ivals = (const struct b43_iv *)(fw->initvals_band->data + hdr_len);
1898 count = be32_to_cpu(hdr->size);
1899 err = b43_write_initvals(dev, ivals, count,
1900 fw->initvals_band->size - hdr_len);
1901 if (err)
1902 goto out;
1904 out:
1906 return err;
1909 /* Initialize the GPIOs
1910 * http://bcm-specs.sipsolutions.net/GPIO
1912 static int b43_gpio_init(struct b43_wldev *dev)
1914 struct ssb_bus *bus = dev->dev->bus;
1915 struct ssb_device *gpiodev, *pcidev = NULL;
1916 u32 mask, set;
1918 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
1919 & ~B43_MACCTL_GPOUTSMSK);
1921 b43_write16(dev, B43_MMIO_GPIO_MASK, b43_read16(dev, B43_MMIO_GPIO_MASK)
1922 | 0x000F);
1924 mask = 0x0000001F;
1925 set = 0x0000000F;
1926 if (dev->dev->bus->chip_id == 0x4301) {
1927 mask |= 0x0060;
1928 set |= 0x0060;
1930 if (0 /* FIXME: conditional unknown */ ) {
1931 b43_write16(dev, B43_MMIO_GPIO_MASK,
1932 b43_read16(dev, B43_MMIO_GPIO_MASK)
1933 | 0x0100);
1934 mask |= 0x0180;
1935 set |= 0x0180;
1937 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_PACTRL) {
1938 b43_write16(dev, B43_MMIO_GPIO_MASK,
1939 b43_read16(dev, B43_MMIO_GPIO_MASK)
1940 | 0x0200);
1941 mask |= 0x0200;
1942 set |= 0x0200;
1944 if (dev->dev->id.revision >= 2)
1945 mask |= 0x0010; /* FIXME: This is redundant. */
1947 #ifdef CONFIG_SSB_DRIVER_PCICORE
1948 pcidev = bus->pcicore.dev;
1949 #endif
1950 gpiodev = bus->chipco.dev ? : pcidev;
1951 if (!gpiodev)
1952 return 0;
1953 ssb_write32(gpiodev, B43_GPIO_CONTROL,
1954 (ssb_read32(gpiodev, B43_GPIO_CONTROL)
1955 & mask) | set);
1957 return 0;
1960 /* Turn off all GPIO stuff. Call this on module unload, for example. */
1961 static void b43_gpio_cleanup(struct b43_wldev *dev)
1963 struct ssb_bus *bus = dev->dev->bus;
1964 struct ssb_device *gpiodev, *pcidev = NULL;
1966 #ifdef CONFIG_SSB_DRIVER_PCICORE
1967 pcidev = bus->pcicore.dev;
1968 #endif
1969 gpiodev = bus->chipco.dev ? : pcidev;
1970 if (!gpiodev)
1971 return;
1972 ssb_write32(gpiodev, B43_GPIO_CONTROL, 0);
1975 /* http://bcm-specs.sipsolutions.net/EnableMac */
1976 void b43_mac_enable(struct b43_wldev *dev)
1978 dev->mac_suspended--;
1979 B43_WARN_ON(dev->mac_suspended < 0);
1980 B43_WARN_ON(irqs_disabled());
1981 if (dev->mac_suspended == 0) {
1982 b43_write32(dev, B43_MMIO_MACCTL,
1983 b43_read32(dev, B43_MMIO_MACCTL)
1984 | B43_MACCTL_ENABLED);
1985 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON,
1986 B43_IRQ_MAC_SUSPENDED);
1987 /* Commit writes */
1988 b43_read32(dev, B43_MMIO_MACCTL);
1989 b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
1990 b43_power_saving_ctl_bits(dev, 0);
1992 /* Re-enable IRQs. */
1993 spin_lock_irq(&dev->wl->irq_lock);
1994 b43_interrupt_enable(dev, dev->irq_savedstate);
1995 spin_unlock_irq(&dev->wl->irq_lock);
1999 /* http://bcm-specs.sipsolutions.net/SuspendMAC */
2000 void b43_mac_suspend(struct b43_wldev *dev)
2002 int i;
2003 u32 tmp;
2005 might_sleep();
2006 B43_WARN_ON(irqs_disabled());
2007 B43_WARN_ON(dev->mac_suspended < 0);
2009 if (dev->mac_suspended == 0) {
2010 /* Mask IRQs before suspending MAC. Otherwise
2011 * the MAC stays busy and won't suspend. */
2012 spin_lock_irq(&dev->wl->irq_lock);
2013 tmp = b43_interrupt_disable(dev, B43_IRQ_ALL);
2014 spin_unlock_irq(&dev->wl->irq_lock);
2015 b43_synchronize_irq(dev);
2016 dev->irq_savedstate = tmp;
2018 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
2019 b43_write32(dev, B43_MMIO_MACCTL,
2020 b43_read32(dev, B43_MMIO_MACCTL)
2021 & ~B43_MACCTL_ENABLED);
2022 /* force pci to flush the write */
2023 b43_read32(dev, B43_MMIO_MACCTL);
2024 for (i = 40; i; i--) {
2025 tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2026 if (tmp & B43_IRQ_MAC_SUSPENDED)
2027 goto out;
2028 msleep(1);
2030 b43err(dev->wl, "MAC suspend failed\n");
2032 out:
2033 dev->mac_suspended++;
2036 static void b43_adjust_opmode(struct b43_wldev *dev)
2038 struct b43_wl *wl = dev->wl;
2039 u32 ctl;
2040 u16 cfp_pretbtt;
2042 ctl = b43_read32(dev, B43_MMIO_MACCTL);
2043 /* Reset status to STA infrastructure mode. */
2044 ctl &= ~B43_MACCTL_AP;
2045 ctl &= ~B43_MACCTL_KEEP_CTL;
2046 ctl &= ~B43_MACCTL_KEEP_BADPLCP;
2047 ctl &= ~B43_MACCTL_KEEP_BAD;
2048 ctl &= ~B43_MACCTL_PROMISC;
2049 ctl &= ~B43_MACCTL_BEACPROMISC;
2050 ctl |= B43_MACCTL_INFRA;
2052 if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
2053 ctl |= B43_MACCTL_AP;
2054 else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
2055 ctl &= ~B43_MACCTL_INFRA;
2057 if (wl->filter_flags & FIF_CONTROL)
2058 ctl |= B43_MACCTL_KEEP_CTL;
2059 if (wl->filter_flags & FIF_FCSFAIL)
2060 ctl |= B43_MACCTL_KEEP_BAD;
2061 if (wl->filter_flags & FIF_PLCPFAIL)
2062 ctl |= B43_MACCTL_KEEP_BADPLCP;
2063 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
2064 ctl |= B43_MACCTL_PROMISC;
2065 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
2066 ctl |= B43_MACCTL_BEACPROMISC;
2068 /* Workaround: On old hardware the HW-MAC-address-filter
2069 * doesn't work properly, so always run promisc in filter
2070 * it in software. */
2071 if (dev->dev->id.revision <= 4)
2072 ctl |= B43_MACCTL_PROMISC;
2074 b43_write32(dev, B43_MMIO_MACCTL, ctl);
2076 cfp_pretbtt = 2;
2077 if ((ctl & B43_MACCTL_INFRA) && !(ctl & B43_MACCTL_AP)) {
2078 if (dev->dev->bus->chip_id == 0x4306 &&
2079 dev->dev->bus->chip_rev == 3)
2080 cfp_pretbtt = 100;
2081 else
2082 cfp_pretbtt = 50;
2084 b43_write16(dev, 0x612, cfp_pretbtt);
2087 static void b43_rate_memory_write(struct b43_wldev *dev, u16 rate, int is_ofdm)
2089 u16 offset;
2091 if (is_ofdm) {
2092 offset = 0x480;
2093 offset += (b43_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
2094 } else {
2095 offset = 0x4C0;
2096 offset += (b43_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
2098 b43_shm_write16(dev, B43_SHM_SHARED, offset + 0x20,
2099 b43_shm_read16(dev, B43_SHM_SHARED, offset));
2102 static void b43_rate_memory_init(struct b43_wldev *dev)
2104 switch (dev->phy.type) {
2105 case B43_PHYTYPE_A:
2106 case B43_PHYTYPE_G:
2107 b43_rate_memory_write(dev, B43_OFDM_RATE_6MB, 1);
2108 b43_rate_memory_write(dev, B43_OFDM_RATE_12MB, 1);
2109 b43_rate_memory_write(dev, B43_OFDM_RATE_18MB, 1);
2110 b43_rate_memory_write(dev, B43_OFDM_RATE_24MB, 1);
2111 b43_rate_memory_write(dev, B43_OFDM_RATE_36MB, 1);
2112 b43_rate_memory_write(dev, B43_OFDM_RATE_48MB, 1);
2113 b43_rate_memory_write(dev, B43_OFDM_RATE_54MB, 1);
2114 if (dev->phy.type == B43_PHYTYPE_A)
2115 break;
2116 /* fallthrough */
2117 case B43_PHYTYPE_B:
2118 b43_rate_memory_write(dev, B43_CCK_RATE_1MB, 0);
2119 b43_rate_memory_write(dev, B43_CCK_RATE_2MB, 0);
2120 b43_rate_memory_write(dev, B43_CCK_RATE_5MB, 0);
2121 b43_rate_memory_write(dev, B43_CCK_RATE_11MB, 0);
2122 break;
2123 default:
2124 B43_WARN_ON(1);
2128 /* Set the TX-Antenna for management frames sent by firmware. */
2129 static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
2131 u16 ant = 0;
2132 u16 tmp;
2134 switch (antenna) {
2135 case B43_ANTENNA0:
2136 ant |= B43_TX4_PHY_ANT0;
2137 break;
2138 case B43_ANTENNA1:
2139 ant |= B43_TX4_PHY_ANT1;
2140 break;
2141 case B43_ANTENNA_AUTO:
2142 ant |= B43_TX4_PHY_ANTLAST;
2143 break;
2144 default:
2145 B43_WARN_ON(1);
2148 /* FIXME We also need to set the other flags of the PHY control field somewhere. */
2150 /* For Beacons */
2151 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
2152 tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
2153 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, tmp);
2154 /* For ACK/CTS */
2155 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
2156 tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
2157 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, tmp);
2158 /* For Probe Resposes */
2159 tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL);
2160 tmp = (tmp & ~B43_TX4_PHY_ANT) | ant;
2161 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, tmp);
2164 /* This is the opposite of b43_chip_init() */
2165 static void b43_chip_exit(struct b43_wldev *dev)
2167 b43_radio_turn_off(dev, 1);
2168 b43_gpio_cleanup(dev);
2169 /* firmware is released later */
2172 /* Initialize the chip
2173 * http://bcm-specs.sipsolutions.net/ChipInit
2175 static int b43_chip_init(struct b43_wldev *dev)
2177 struct b43_phy *phy = &dev->phy;
2178 int err, tmp;
2179 u32 value32;
2180 u16 value16;
2182 b43_write32(dev, B43_MMIO_MACCTL,
2183 B43_MACCTL_PSM_JMP0 | B43_MACCTL_IHR_ENABLED);
2185 err = b43_request_firmware(dev);
2186 if (err)
2187 goto out;
2188 err = b43_upload_microcode(dev);
2189 if (err)
2190 goto out; /* firmware is released later */
2192 err = b43_gpio_init(dev);
2193 if (err)
2194 goto out; /* firmware is released later */
2196 err = b43_upload_initvals(dev);
2197 if (err)
2198 goto err_gpio_clean;
2199 b43_radio_turn_on(dev);
2201 b43_write16(dev, 0x03E6, 0x0000);
2202 err = b43_phy_init(dev);
2203 if (err)
2204 goto err_radio_off;
2206 /* Select initial Interference Mitigation. */
2207 tmp = phy->interfmode;
2208 phy->interfmode = B43_INTERFMODE_NONE;
2209 b43_radio_set_interference_mitigation(dev, tmp);
2211 b43_set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
2212 b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
2214 if (phy->type == B43_PHYTYPE_B) {
2215 value16 = b43_read16(dev, 0x005E);
2216 value16 |= 0x0004;
2217 b43_write16(dev, 0x005E, value16);
2219 b43_write32(dev, 0x0100, 0x01000000);
2220 if (dev->dev->id.revision < 5)
2221 b43_write32(dev, 0x010C, 0x01000000);
2223 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2224 & ~B43_MACCTL_INFRA);
2225 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
2226 | B43_MACCTL_INFRA);
2228 if (b43_using_pio(dev)) {
2229 b43_write32(dev, 0x0210, 0x00000100);
2230 b43_write32(dev, 0x0230, 0x00000100);
2231 b43_write32(dev, 0x0250, 0x00000100);
2232 b43_write32(dev, 0x0270, 0x00000100);
2233 b43_shm_write16(dev, B43_SHM_SHARED, 0x0034, 0x0000);
2236 /* Probe Response Timeout value */
2237 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2238 b43_shm_write16(dev, B43_SHM_SHARED, 0x0074, 0x0000);
2240 /* Initially set the wireless operation mode. */
2241 b43_adjust_opmode(dev);
2243 if (dev->dev->id.revision < 3) {
2244 b43_write16(dev, 0x060E, 0x0000);
2245 b43_write16(dev, 0x0610, 0x8000);
2246 b43_write16(dev, 0x0604, 0x0000);
2247 b43_write16(dev, 0x0606, 0x0200);
2248 } else {
2249 b43_write32(dev, 0x0188, 0x80000000);
2250 b43_write32(dev, 0x018C, 0x02000000);
2252 b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, 0x00004000);
2253 b43_write32(dev, B43_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2254 b43_write32(dev, B43_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2255 b43_write32(dev, B43_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2256 b43_write32(dev, B43_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2257 b43_write32(dev, B43_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2258 b43_write32(dev, B43_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2260 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
2261 value32 |= 0x00100000;
2262 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2264 b43_write16(dev, B43_MMIO_POWERUP_DELAY,
2265 dev->dev->bus->chipco.fast_pwrup_delay);
2267 err = 0;
2268 b43dbg(dev->wl, "Chip initialized\n");
2269 out:
2270 return err;
2272 err_radio_off:
2273 b43_radio_turn_off(dev, 1);
2274 err_gpio_clean:
2275 b43_gpio_cleanup(dev);
2276 return err;
2279 static void b43_periodic_every120sec(struct b43_wldev *dev)
2281 struct b43_phy *phy = &dev->phy;
2283 if (phy->type != B43_PHYTYPE_G || phy->rev < 2)
2284 return;
2286 b43_mac_suspend(dev);
2287 b43_lo_g_measure(dev);
2288 b43_mac_enable(dev);
2289 if (b43_has_hardware_pctl(phy))
2290 b43_lo_g_ctl_mark_all_unused(dev);
2293 static void b43_periodic_every60sec(struct b43_wldev *dev)
2295 struct b43_phy *phy = &dev->phy;
2297 if (!b43_has_hardware_pctl(phy))
2298 b43_lo_g_ctl_mark_all_unused(dev);
2299 if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI) {
2300 b43_mac_suspend(dev);
2301 b43_calc_nrssi_slope(dev);
2302 if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 8)) {
2303 u8 old_chan = phy->channel;
2305 /* VCO Calibration */
2306 if (old_chan >= 8)
2307 b43_radio_selectchannel(dev, 1, 0);
2308 else
2309 b43_radio_selectchannel(dev, 13, 0);
2310 b43_radio_selectchannel(dev, old_chan, 0);
2312 b43_mac_enable(dev);
2316 static void b43_periodic_every30sec(struct b43_wldev *dev)
2318 /* Update device statistics. */
2319 b43_calculate_link_quality(dev);
2322 static void b43_periodic_every15sec(struct b43_wldev *dev)
2324 struct b43_phy *phy = &dev->phy;
2326 if (phy->type == B43_PHYTYPE_G) {
2327 //TODO: update_aci_moving_average
2328 if (phy->aci_enable && phy->aci_wlan_automatic) {
2329 b43_mac_suspend(dev);
2330 if (!phy->aci_enable && 1 /*TODO: not scanning? */ ) {
2331 if (0 /*TODO: bunch of conditions */ ) {
2332 b43_radio_set_interference_mitigation
2333 (dev, B43_INTERFMODE_MANUALWLAN);
2335 } else if (1 /*TODO*/) {
2337 if ((aci_average > 1000) && !(b43_radio_aci_scan(dev))) {
2338 b43_radio_set_interference_mitigation(dev,
2339 B43_INTERFMODE_NONE);
2343 b43_mac_enable(dev);
2344 } else if (phy->interfmode == B43_INTERFMODE_NONWLAN &&
2345 phy->rev == 1) {
2346 //TODO: implement rev1 workaround
2349 b43_phy_xmitpower(dev); //FIXME: unless scanning?
2350 //TODO for APHY (temperature?)
2352 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
2353 wmb();
2356 static void do_periodic_work(struct b43_wldev *dev)
2358 unsigned int state;
2360 state = dev->periodic_state;
2361 if (state % 8 == 0)
2362 b43_periodic_every120sec(dev);
2363 if (state % 4 == 0)
2364 b43_periodic_every60sec(dev);
2365 if (state % 2 == 0)
2366 b43_periodic_every30sec(dev);
2367 b43_periodic_every15sec(dev);
2370 /* Periodic work locking policy:
2371 * The whole periodic work handler is protected by
2372 * wl->mutex. If another lock is needed somewhere in the
2373 * pwork callchain, it's aquired in-place, where it's needed.
2375 static void b43_periodic_work_handler(struct work_struct *work)
2377 struct b43_wldev *dev = container_of(work, struct b43_wldev,
2378 periodic_work.work);
2379 struct b43_wl *wl = dev->wl;
2380 unsigned long delay;
2382 mutex_lock(&wl->mutex);
2384 if (unlikely(b43_status(dev) != B43_STAT_STARTED))
2385 goto out;
2386 if (b43_debug(dev, B43_DBG_PWORK_STOP))
2387 goto out_requeue;
2389 do_periodic_work(dev);
2391 dev->periodic_state++;
2392 out_requeue:
2393 if (b43_debug(dev, B43_DBG_PWORK_FAST))
2394 delay = msecs_to_jiffies(50);
2395 else
2396 delay = round_jiffies_relative(HZ * 15);
2397 queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
2398 out:
2399 mutex_unlock(&wl->mutex);
2402 static void b43_periodic_tasks_setup(struct b43_wldev *dev)
2404 struct delayed_work *work = &dev->periodic_work;
2406 dev->periodic_state = 0;
2407 INIT_DELAYED_WORK(work, b43_periodic_work_handler);
2408 queue_delayed_work(dev->wl->hw->workqueue, work, 0);
2411 /* Validate access to the chip (SHM) */
2412 static int b43_validate_chipaccess(struct b43_wldev *dev)
2414 u32 value;
2415 u32 shm_backup;
2417 shm_backup = b43_shm_read32(dev, B43_SHM_SHARED, 0);
2418 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0xAA5555AA);
2419 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0xAA5555AA)
2420 goto error;
2421 b43_shm_write32(dev, B43_SHM_SHARED, 0, 0x55AAAA55);
2422 if (b43_shm_read32(dev, B43_SHM_SHARED, 0) != 0x55AAAA55)
2423 goto error;
2424 b43_shm_write32(dev, B43_SHM_SHARED, 0, shm_backup);
2426 value = b43_read32(dev, B43_MMIO_MACCTL);
2427 if ((value | B43_MACCTL_GMODE) !=
2428 (B43_MACCTL_GMODE | B43_MACCTL_IHR_ENABLED))
2429 goto error;
2431 value = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
2432 if (value)
2433 goto error;
2435 return 0;
2436 error:
2437 b43err(dev->wl, "Failed to validate the chipaccess\n");
2438 return -ENODEV;
2441 static void b43_security_init(struct b43_wldev *dev)
2443 dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
2444 B43_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
2445 dev->ktp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_KTP);
2446 /* KTP is a word address, but we address SHM bytewise.
2447 * So multiply by two.
2449 dev->ktp *= 2;
2450 if (dev->dev->id.revision >= 5) {
2451 /* Number of RCMTA address slots */
2452 b43_write16(dev, B43_MMIO_RCMTA_COUNT, dev->max_nr_keys - 8);
2454 b43_clear_keys(dev);
2457 static int b43_rng_read(struct hwrng *rng, u32 * data)
2459 struct b43_wl *wl = (struct b43_wl *)rng->priv;
2460 unsigned long flags;
2462 /* Don't take wl->mutex here, as it could deadlock with
2463 * hwrng internal locking. It's not needed to take
2464 * wl->mutex here, anyway. */
2466 spin_lock_irqsave(&wl->irq_lock, flags);
2467 *data = b43_read16(wl->current_dev, B43_MMIO_RNG);
2468 spin_unlock_irqrestore(&wl->irq_lock, flags);
2470 return (sizeof(u16));
2473 static void b43_rng_exit(struct b43_wl *wl)
2475 if (wl->rng_initialized)
2476 hwrng_unregister(&wl->rng);
2479 static int b43_rng_init(struct b43_wl *wl)
2481 int err;
2483 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
2484 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
2485 wl->rng.name = wl->rng_name;
2486 wl->rng.data_read = b43_rng_read;
2487 wl->rng.priv = (unsigned long)wl;
2488 wl->rng_initialized = 1;
2489 err = hwrng_register(&wl->rng);
2490 if (err) {
2491 wl->rng_initialized = 0;
2492 b43err(wl, "Failed to register the random "
2493 "number generator (%d)\n", err);
2496 return err;
2499 static int b43_op_tx(struct ieee80211_hw *hw,
2500 struct sk_buff *skb,
2501 struct ieee80211_tx_control *ctl)
2503 struct b43_wl *wl = hw_to_b43_wl(hw);
2504 struct b43_wldev *dev = wl->current_dev;
2505 int err = -ENODEV;
2506 unsigned long flags;
2508 if (unlikely(!dev))
2509 goto out;
2510 if (unlikely(b43_status(dev) < B43_STAT_STARTED))
2511 goto out;
2512 /* DMA-TX is done without a global lock. */
2513 if (b43_using_pio(dev)) {
2514 spin_lock_irqsave(&wl->irq_lock, flags);
2515 err = b43_pio_tx(dev, skb, ctl);
2516 spin_unlock_irqrestore(&wl->irq_lock, flags);
2517 } else
2518 err = b43_dma_tx(dev, skb, ctl);
2519 out:
2520 if (unlikely(err))
2521 return NETDEV_TX_BUSY;
2522 return NETDEV_TX_OK;
2525 static int b43_op_conf_tx(struct ieee80211_hw *hw,
2526 int queue,
2527 const struct ieee80211_tx_queue_params *params)
2529 return 0;
2532 static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
2533 struct ieee80211_tx_queue_stats *stats)
2535 struct b43_wl *wl = hw_to_b43_wl(hw);
2536 struct b43_wldev *dev = wl->current_dev;
2537 unsigned long flags;
2538 int err = -ENODEV;
2540 if (!dev)
2541 goto out;
2542 spin_lock_irqsave(&wl->irq_lock, flags);
2543 if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
2544 if (b43_using_pio(dev))
2545 b43_pio_get_tx_stats(dev, stats);
2546 else
2547 b43_dma_get_tx_stats(dev, stats);
2548 err = 0;
2550 spin_unlock_irqrestore(&wl->irq_lock, flags);
2551 out:
2552 return err;
2555 static int b43_op_get_stats(struct ieee80211_hw *hw,
2556 struct ieee80211_low_level_stats *stats)
2558 struct b43_wl *wl = hw_to_b43_wl(hw);
2559 unsigned long flags;
2561 spin_lock_irqsave(&wl->irq_lock, flags);
2562 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
2563 spin_unlock_irqrestore(&wl->irq_lock, flags);
2565 return 0;
2568 static const char *phymode_to_string(unsigned int phymode)
2570 switch (phymode) {
2571 case B43_PHYMODE_A:
2572 return "A";
2573 case B43_PHYMODE_B:
2574 return "B";
2575 case B43_PHYMODE_G:
2576 return "G";
2577 default:
2578 B43_WARN_ON(1);
2580 return "";
2583 static int find_wldev_for_phymode(struct b43_wl *wl,
2584 unsigned int phymode,
2585 struct b43_wldev **dev, bool * gmode)
2587 struct b43_wldev *d;
2589 list_for_each_entry(d, &wl->devlist, list) {
2590 if (d->phy.possible_phymodes & phymode) {
2591 /* Ok, this device supports the PHY-mode.
2592 * Now figure out how the gmode bit has to be
2593 * set to support it. */
2594 if (phymode == B43_PHYMODE_A)
2595 *gmode = 0;
2596 else
2597 *gmode = 1;
2598 *dev = d;
2600 return 0;
2604 return -ESRCH;
2607 static void b43_put_phy_into_reset(struct b43_wldev *dev)
2609 struct ssb_device *sdev = dev->dev;
2610 u32 tmslow;
2612 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2613 tmslow &= ~B43_TMSLOW_GMODE;
2614 tmslow |= B43_TMSLOW_PHYRESET;
2615 tmslow |= SSB_TMSLOW_FGC;
2616 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2617 msleep(1);
2619 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2620 tmslow &= ~SSB_TMSLOW_FGC;
2621 tmslow |= B43_TMSLOW_PHYRESET;
2622 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2623 msleep(1);
2626 /* Expects wl->mutex locked */
2627 static int b43_switch_phymode(struct b43_wl *wl, unsigned int new_mode)
2629 struct b43_wldev *up_dev;
2630 struct b43_wldev *down_dev;
2631 int err;
2632 bool gmode = 0;
2633 int prev_status;
2635 err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
2636 if (err) {
2637 b43err(wl, "Could not find a device for %s-PHY mode\n",
2638 phymode_to_string(new_mode));
2639 return err;
2641 if ((up_dev == wl->current_dev) &&
2642 (!!wl->current_dev->phy.gmode == !!gmode)) {
2643 /* This device is already running. */
2644 return 0;
2646 b43dbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
2647 phymode_to_string(new_mode));
2648 down_dev = wl->current_dev;
2650 prev_status = b43_status(down_dev);
2651 /* Shutdown the currently running core. */
2652 if (prev_status >= B43_STAT_STARTED)
2653 b43_wireless_core_stop(down_dev);
2654 if (prev_status >= B43_STAT_INITIALIZED)
2655 b43_wireless_core_exit(down_dev);
2657 if (down_dev != up_dev) {
2658 /* We switch to a different core, so we put PHY into
2659 * RESET on the old core. */
2660 b43_put_phy_into_reset(down_dev);
2663 /* Now start the new core. */
2664 up_dev->phy.gmode = gmode;
2665 if (prev_status >= B43_STAT_INITIALIZED) {
2666 err = b43_wireless_core_init(up_dev);
2667 if (err) {
2668 b43err(wl, "Fatal: Could not initialize device for "
2669 "newly selected %s-PHY mode\n",
2670 phymode_to_string(new_mode));
2671 goto init_failure;
2674 if (prev_status >= B43_STAT_STARTED) {
2675 err = b43_wireless_core_start(up_dev);
2676 if (err) {
2677 b43err(wl, "Fatal: Coult not start device for "
2678 "newly selected %s-PHY mode\n",
2679 phymode_to_string(new_mode));
2680 b43_wireless_core_exit(up_dev);
2681 goto init_failure;
2684 B43_WARN_ON(b43_status(up_dev) != prev_status);
2686 wl->current_dev = up_dev;
2688 return 0;
2689 init_failure:
2690 /* Whoops, failed to init the new core. No core is operating now. */
2691 wl->current_dev = NULL;
2692 return err;
2695 static int b43_antenna_from_ieee80211(u8 antenna)
2697 switch (antenna) {
2698 case 0: /* default/diversity */
2699 return B43_ANTENNA_DEFAULT;
2700 case 1: /* Antenna 0 */
2701 return B43_ANTENNA0;
2702 case 2: /* Antenna 1 */
2703 return B43_ANTENNA1;
2704 default:
2705 return B43_ANTENNA_DEFAULT;
2709 static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
2711 struct b43_wl *wl = hw_to_b43_wl(hw);
2712 struct b43_wldev *dev;
2713 struct b43_phy *phy;
2714 unsigned long flags;
2715 unsigned int new_phymode = 0xFFFF;
2716 int antenna_tx;
2717 int antenna_rx;
2718 int err = 0;
2719 u32 savedirqs;
2721 antenna_tx = b43_antenna_from_ieee80211(conf->antenna_sel_tx);
2722 antenna_rx = b43_antenna_from_ieee80211(conf->antenna_sel_rx);
2724 mutex_lock(&wl->mutex);
2726 /* Switch the PHY mode (if necessary). */
2727 switch (conf->phymode) {
2728 case MODE_IEEE80211A:
2729 new_phymode = B43_PHYMODE_A;
2730 break;
2731 case MODE_IEEE80211B:
2732 new_phymode = B43_PHYMODE_B;
2733 break;
2734 case MODE_IEEE80211G:
2735 new_phymode = B43_PHYMODE_G;
2736 break;
2737 default:
2738 B43_WARN_ON(1);
2740 err = b43_switch_phymode(wl, new_phymode);
2741 if (err)
2742 goto out_unlock_mutex;
2743 dev = wl->current_dev;
2744 phy = &dev->phy;
2746 /* Disable IRQs while reconfiguring the device.
2747 * This makes it possible to drop the spinlock throughout
2748 * the reconfiguration process. */
2749 spin_lock_irqsave(&wl->irq_lock, flags);
2750 if (b43_status(dev) < B43_STAT_STARTED) {
2751 spin_unlock_irqrestore(&wl->irq_lock, flags);
2752 goto out_unlock_mutex;
2754 savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
2755 spin_unlock_irqrestore(&wl->irq_lock, flags);
2756 b43_synchronize_irq(dev);
2758 /* Switch to the requested channel.
2759 * The firmware takes care of races with the TX handler. */
2760 if (conf->channel_val != phy->channel)
2761 b43_radio_selectchannel(dev, conf->channel_val, 0);
2763 /* Enable/Disable ShortSlot timing. */
2764 if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
2765 dev->short_slot) {
2766 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
2767 if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
2768 b43_short_slot_timing_enable(dev);
2769 else
2770 b43_short_slot_timing_disable(dev);
2773 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2775 /* Adjust the desired TX power level. */
2776 if (conf->power_level != 0) {
2777 if (conf->power_level != phy->power_level) {
2778 phy->power_level = conf->power_level;
2779 b43_phy_xmitpower(dev);
2783 /* Antennas for RX and management frame TX. */
2784 b43_mgmtframe_txantenna(dev, antenna_tx);
2785 b43_set_rx_antenna(dev, antenna_rx);
2787 /* Update templates for AP mode. */
2788 if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
2789 b43_set_beacon_int(dev, conf->beacon_int);
2791 if (!!conf->radio_enabled != phy->radio_on) {
2792 if (conf->radio_enabled) {
2793 b43_radio_turn_on(dev);
2794 b43info(dev->wl, "Radio turned on by software\n");
2795 if (!dev->radio_hw_enable) {
2796 b43info(dev->wl, "The hardware RF-kill button "
2797 "still turns the radio physically off. "
2798 "Press the button to turn it on.\n");
2800 } else {
2801 b43_radio_turn_off(dev, 0);
2802 b43info(dev->wl, "Radio turned off by software\n");
2806 spin_lock_irqsave(&wl->irq_lock, flags);
2807 b43_interrupt_enable(dev, savedirqs);
2808 mmiowb();
2809 spin_unlock_irqrestore(&wl->irq_lock, flags);
2810 out_unlock_mutex:
2811 mutex_unlock(&wl->mutex);
2813 return err;
2816 static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2817 const u8 *local_addr, const u8 *addr,
2818 struct ieee80211_key_conf *key)
2820 struct b43_wl *wl = hw_to_b43_wl(hw);
2821 struct b43_wldev *dev;
2822 unsigned long flags;
2823 u8 algorithm;
2824 u8 index;
2825 int err;
2826 DECLARE_MAC_BUF(mac);
2828 if (modparam_nohwcrypt)
2829 return -ENOSPC; /* User disabled HW-crypto */
2831 mutex_lock(&wl->mutex);
2832 spin_lock_irqsave(&wl->irq_lock, flags);
2834 dev = wl->current_dev;
2835 err = -ENODEV;
2836 if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
2837 goto out_unlock;
2839 err = -EINVAL;
2840 switch (key->alg) {
2841 case ALG_WEP:
2842 if (key->keylen == 5)
2843 algorithm = B43_SEC_ALGO_WEP40;
2844 else
2845 algorithm = B43_SEC_ALGO_WEP104;
2846 break;
2847 case ALG_TKIP:
2848 algorithm = B43_SEC_ALGO_TKIP;
2849 break;
2850 case ALG_CCMP:
2851 algorithm = B43_SEC_ALGO_AES;
2852 break;
2853 default:
2854 B43_WARN_ON(1);
2855 goto out_unlock;
2857 index = (u8) (key->keyidx);
2858 if (index > 3)
2859 goto out_unlock;
2861 switch (cmd) {
2862 case SET_KEY:
2863 if (algorithm == B43_SEC_ALGO_TKIP) {
2864 /* FIXME: No TKIP hardware encryption for now. */
2865 err = -EOPNOTSUPP;
2866 goto out_unlock;
2869 if (is_broadcast_ether_addr(addr)) {
2870 /* addr is FF:FF:FF:FF:FF:FF for default keys */
2871 err = b43_key_write(dev, index, algorithm,
2872 key->key, key->keylen, NULL, key);
2873 } else {
2875 * either pairwise key or address is 00:00:00:00:00:00
2876 * for transmit-only keys
2878 err = b43_key_write(dev, -1, algorithm,
2879 key->key, key->keylen, addr, key);
2881 if (err)
2882 goto out_unlock;
2884 if (algorithm == B43_SEC_ALGO_WEP40 ||
2885 algorithm == B43_SEC_ALGO_WEP104) {
2886 b43_hf_write(dev, b43_hf_read(dev) | B43_HF_USEDEFKEYS);
2887 } else {
2888 b43_hf_write(dev,
2889 b43_hf_read(dev) & ~B43_HF_USEDEFKEYS);
2891 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2892 break;
2893 case DISABLE_KEY: {
2894 err = b43_key_clear(dev, key->hw_key_idx);
2895 if (err)
2896 goto out_unlock;
2897 break;
2899 default:
2900 B43_WARN_ON(1);
2902 out_unlock:
2903 spin_unlock_irqrestore(&wl->irq_lock, flags);
2904 mutex_unlock(&wl->mutex);
2905 if (!err) {
2906 b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
2907 "mac: %s\n",
2908 cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
2909 print_mac(mac, addr));
2911 return err;
2914 static void b43_op_configure_filter(struct ieee80211_hw *hw,
2915 unsigned int changed, unsigned int *fflags,
2916 int mc_count, struct dev_addr_list *mc_list)
2918 struct b43_wl *wl = hw_to_b43_wl(hw);
2919 struct b43_wldev *dev = wl->current_dev;
2920 unsigned long flags;
2922 if (!dev) {
2923 *fflags = 0;
2924 return;
2927 spin_lock_irqsave(&wl->irq_lock, flags);
2928 *fflags &= FIF_PROMISC_IN_BSS |
2929 FIF_ALLMULTI |
2930 FIF_FCSFAIL |
2931 FIF_PLCPFAIL |
2932 FIF_CONTROL |
2933 FIF_OTHER_BSS |
2934 FIF_BCN_PRBRESP_PROMISC;
2936 changed &= FIF_PROMISC_IN_BSS |
2937 FIF_ALLMULTI |
2938 FIF_FCSFAIL |
2939 FIF_PLCPFAIL |
2940 FIF_CONTROL |
2941 FIF_OTHER_BSS |
2942 FIF_BCN_PRBRESP_PROMISC;
2944 wl->filter_flags = *fflags;
2946 if (changed && b43_status(dev) >= B43_STAT_INITIALIZED)
2947 b43_adjust_opmode(dev);
2948 spin_unlock_irqrestore(&wl->irq_lock, flags);
2951 static int b43_op_config_interface(struct ieee80211_hw *hw,
2952 int if_id,
2953 struct ieee80211_if_conf *conf)
2955 struct b43_wl *wl = hw_to_b43_wl(hw);
2956 struct b43_wldev *dev = wl->current_dev;
2957 unsigned long flags;
2959 if (!dev)
2960 return -ENODEV;
2961 mutex_lock(&wl->mutex);
2962 spin_lock_irqsave(&wl->irq_lock, flags);
2963 B43_WARN_ON(wl->if_id != if_id);
2964 if (conf->bssid)
2965 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
2966 else
2967 memset(wl->bssid, 0, ETH_ALEN);
2968 if (b43_status(dev) >= B43_STAT_INITIALIZED) {
2969 if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP)) {
2970 B43_WARN_ON(conf->type != IEEE80211_IF_TYPE_AP);
2971 b43_set_ssid(dev, conf->ssid, conf->ssid_len);
2972 if (conf->beacon)
2973 b43_refresh_templates(dev, conf->beacon);
2975 b43_write_mac_bssid_templates(dev);
2977 spin_unlock_irqrestore(&wl->irq_lock, flags);
2978 mutex_unlock(&wl->mutex);
2980 return 0;
2983 /* Locking: wl->mutex */
2984 static void b43_wireless_core_stop(struct b43_wldev *dev)
2986 struct b43_wl *wl = dev->wl;
2987 unsigned long flags;
2989 if (b43_status(dev) < B43_STAT_STARTED)
2990 return;
2992 /* Disable and sync interrupts. We must do this before than
2993 * setting the status to INITIALIZED, as the interrupt handler
2994 * won't care about IRQs then. */
2995 spin_lock_irqsave(&wl->irq_lock, flags);
2996 dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
2997 b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
2998 spin_unlock_irqrestore(&wl->irq_lock, flags);
2999 b43_synchronize_irq(dev);
3001 b43_set_status(dev, B43_STAT_INITIALIZED);
3003 mutex_unlock(&wl->mutex);
3004 /* Must unlock as it would otherwise deadlock. No races here.
3005 * Cancel the possibly running self-rearming periodic work. */
3006 cancel_delayed_work_sync(&dev->periodic_work);
3007 mutex_lock(&wl->mutex);
3009 ieee80211_stop_queues(wl->hw); //FIXME this could cause a deadlock, as mac80211 seems buggy.
3011 b43_mac_suspend(dev);
3012 free_irq(dev->dev->irq, dev);
3013 b43dbg(wl, "Wireless interface stopped\n");
3016 /* Locking: wl->mutex */
3017 static int b43_wireless_core_start(struct b43_wldev *dev)
3019 int err;
3021 B43_WARN_ON(b43_status(dev) != B43_STAT_INITIALIZED);
3023 drain_txstatus_queue(dev);
3024 err = request_irq(dev->dev->irq, b43_interrupt_handler,
3025 IRQF_SHARED, KBUILD_MODNAME, dev);
3026 if (err) {
3027 b43err(dev->wl, "Cannot request IRQ-%d\n", dev->dev->irq);
3028 goto out;
3031 /* We are ready to run. */
3032 b43_set_status(dev, B43_STAT_STARTED);
3034 /* Start data flow (TX/RX). */
3035 b43_mac_enable(dev);
3036 b43_interrupt_enable(dev, dev->irq_savedstate);
3037 ieee80211_start_queues(dev->wl->hw);
3039 /* Start maintainance work */
3040 b43_periodic_tasks_setup(dev);
3042 b43dbg(dev->wl, "Wireless interface started\n");
3043 out:
3044 return err;
3047 /* Get PHY and RADIO versioning numbers */
3048 static int b43_phy_versioning(struct b43_wldev *dev)
3050 struct b43_phy *phy = &dev->phy;
3051 u32 tmp;
3052 u8 analog_type;
3053 u8 phy_type;
3054 u8 phy_rev;
3055 u16 radio_manuf;
3056 u16 radio_ver;
3057 u16 radio_rev;
3058 int unsupported = 0;
3060 /* Get PHY versioning */
3061 tmp = b43_read16(dev, B43_MMIO_PHY_VER);
3062 analog_type = (tmp & B43_PHYVER_ANALOG) >> B43_PHYVER_ANALOG_SHIFT;
3063 phy_type = (tmp & B43_PHYVER_TYPE) >> B43_PHYVER_TYPE_SHIFT;
3064 phy_rev = (tmp & B43_PHYVER_VERSION);
3065 switch (phy_type) {
3066 case B43_PHYTYPE_A:
3067 if (phy_rev >= 4)
3068 unsupported = 1;
3069 break;
3070 case B43_PHYTYPE_B:
3071 if (phy_rev != 2 && phy_rev != 4 && phy_rev != 6
3072 && phy_rev != 7)
3073 unsupported = 1;
3074 break;
3075 case B43_PHYTYPE_G:
3076 if (phy_rev > 9)
3077 unsupported = 1;
3078 break;
3079 default:
3080 unsupported = 1;
3082 if (unsupported) {
3083 b43err(dev->wl, "FOUND UNSUPPORTED PHY "
3084 "(Analog %u, Type %u, Revision %u)\n",
3085 analog_type, phy_type, phy_rev);
3086 return -EOPNOTSUPP;
3088 b43dbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
3089 analog_type, phy_type, phy_rev);
3091 /* Get RADIO versioning */
3092 if (dev->dev->bus->chip_id == 0x4317) {
3093 if (dev->dev->bus->chip_rev == 0)
3094 tmp = 0x3205017F;
3095 else if (dev->dev->bus->chip_rev == 1)
3096 tmp = 0x4205017F;
3097 else
3098 tmp = 0x5205017F;
3099 } else {
3100 b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
3101 tmp = b43_read16(dev, B43_MMIO_RADIO_DATA_HIGH);
3102 tmp <<= 16;
3103 b43_write16(dev, B43_MMIO_RADIO_CONTROL, B43_RADIOCTL_ID);
3104 tmp |= b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
3106 radio_manuf = (tmp & 0x00000FFF);
3107 radio_ver = (tmp & 0x0FFFF000) >> 12;
3108 radio_rev = (tmp & 0xF0000000) >> 28;
3109 switch (phy_type) {
3110 case B43_PHYTYPE_A:
3111 if (radio_ver != 0x2060)
3112 unsupported = 1;
3113 if (radio_rev != 1)
3114 unsupported = 1;
3115 if (radio_manuf != 0x17F)
3116 unsupported = 1;
3117 break;
3118 case B43_PHYTYPE_B:
3119 if ((radio_ver & 0xFFF0) != 0x2050)
3120 unsupported = 1;
3121 break;
3122 case B43_PHYTYPE_G:
3123 if (radio_ver != 0x2050)
3124 unsupported = 1;
3125 break;
3126 default:
3127 B43_WARN_ON(1);
3129 if (unsupported) {
3130 b43err(dev->wl, "FOUND UNSUPPORTED RADIO "
3131 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
3132 radio_manuf, radio_ver, radio_rev);
3133 return -EOPNOTSUPP;
3135 b43dbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X, Revision %u\n",
3136 radio_manuf, radio_ver, radio_rev);
3138 phy->radio_manuf = radio_manuf;
3139 phy->radio_ver = radio_ver;
3140 phy->radio_rev = radio_rev;
3142 phy->analog = analog_type;
3143 phy->type = phy_type;
3144 phy->rev = phy_rev;
3146 return 0;
3149 static void setup_struct_phy_for_init(struct b43_wldev *dev,
3150 struct b43_phy *phy)
3152 struct b43_txpower_lo_control *lo;
3153 int i;
3155 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
3156 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
3158 /* Flags */
3159 phy->locked = 0;
3161 phy->aci_enable = 0;
3162 phy->aci_wlan_automatic = 0;
3163 phy->aci_hw_rssi = 0;
3165 phy->radio_off_context.valid = 0;
3167 lo = phy->lo_control;
3168 if (lo) {
3169 memset(lo, 0, sizeof(*(phy->lo_control)));
3170 lo->rebuild = 1;
3171 lo->tx_bias = 0xFF;
3173 phy->max_lb_gain = 0;
3174 phy->trsw_rx_gain = 0;
3175 phy->txpwr_offset = 0;
3177 /* NRSSI */
3178 phy->nrssislope = 0;
3179 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
3180 phy->nrssi[i] = -1000;
3181 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
3182 phy->nrssi_lt[i] = i;
3184 phy->lofcal = 0xFFFF;
3185 phy->initval = 0xFFFF;
3187 spin_lock_init(&phy->lock);
3188 phy->interfmode = B43_INTERFMODE_NONE;
3189 phy->channel = 0xFF;
3191 phy->hardware_power_control = !!modparam_hwpctl;
3193 /* PHY TX errors counter. */
3194 atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
3196 /* OFDM-table address caching. */
3197 phy->ofdmtab_addr_direction = B43_OFDMTAB_DIRECTION_UNKNOWN;
3200 static void setup_struct_wldev_for_init(struct b43_wldev *dev)
3202 /* Flags */
3203 dev->reg124_set_0x4 = 0;
3204 /* Assume the radio is enabled. If it's not enabled, the state will
3205 * immediately get fixed on the first periodic work run. */
3206 dev->radio_hw_enable = 1;
3208 /* Stats */
3209 memset(&dev->stats, 0, sizeof(dev->stats));
3211 setup_struct_phy_for_init(dev, &dev->phy);
3213 /* IRQ related flags */
3214 dev->irq_reason = 0;
3215 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
3216 dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
3218 dev->mac_suspended = 1;
3220 /* Noise calculation context */
3221 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
3224 static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
3226 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
3227 u32 hf;
3229 if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
3230 return;
3231 if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
3232 return;
3234 hf = b43_hf_read(dev);
3235 if (sprom->boardflags_lo & B43_BFL_BTCMOD)
3236 hf |= B43_HF_BTCOEXALT;
3237 else
3238 hf |= B43_HF_BTCOEX;
3239 b43_hf_write(dev, hf);
3240 //TODO
3243 static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
3244 { //TODO
3247 static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
3249 #ifdef CONFIG_SSB_DRIVER_PCICORE
3250 struct ssb_bus *bus = dev->dev->bus;
3251 u32 tmp;
3253 if (bus->pcicore.dev &&
3254 bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
3255 bus->pcicore.dev->id.revision <= 5) {
3256 /* IMCFGLO timeouts workaround. */
3257 tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
3258 tmp &= ~SSB_IMCFGLO_REQTO;
3259 tmp &= ~SSB_IMCFGLO_SERTO;
3260 switch (bus->bustype) {
3261 case SSB_BUSTYPE_PCI:
3262 case SSB_BUSTYPE_PCMCIA:
3263 tmp |= 0x32;
3264 break;
3265 case SSB_BUSTYPE_SSB:
3266 tmp |= 0x53;
3267 break;
3269 ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
3271 #endif /* CONFIG_SSB_DRIVER_PCICORE */
3274 /* Write the short and long frame retry limit values. */
3275 static void b43_set_retry_limits(struct b43_wldev *dev,
3276 unsigned int short_retry,
3277 unsigned int long_retry)
3279 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3280 * the chip-internal counter. */
3281 short_retry = min(short_retry, (unsigned int)0xF);
3282 long_retry = min(long_retry, (unsigned int)0xF);
3284 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
3285 short_retry);
3286 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
3287 long_retry);
3290 /* Shutdown a wireless core */
3291 /* Locking: wl->mutex */
3292 static void b43_wireless_core_exit(struct b43_wldev *dev)
3294 struct b43_phy *phy = &dev->phy;
3296 B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
3297 if (b43_status(dev) != B43_STAT_INITIALIZED)
3298 return;
3299 b43_set_status(dev, B43_STAT_UNINIT);
3301 b43_leds_exit(dev);
3302 b43_rng_exit(dev->wl);
3303 b43_pio_free(dev);
3304 b43_dma_free(dev);
3305 b43_chip_exit(dev);
3306 b43_radio_turn_off(dev, 1);
3307 b43_switch_analog(dev, 0);
3308 if (phy->dyn_tssi_tbl)
3309 kfree(phy->tssi2dbm);
3310 kfree(phy->lo_control);
3311 phy->lo_control = NULL;
3312 ssb_device_disable(dev->dev, 0);
3313 ssb_bus_may_powerdown(dev->dev->bus);
3316 /* Initialize a wireless core */
3317 static int b43_wireless_core_init(struct b43_wldev *dev)
3319 struct b43_wl *wl = dev->wl;
3320 struct ssb_bus *bus = dev->dev->bus;
3321 struct ssb_sprom *sprom = &bus->sprom;
3322 struct b43_phy *phy = &dev->phy;
3323 int err;
3324 u32 hf, tmp;
3326 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
3328 err = ssb_bus_powerup(bus, 0);
3329 if (err)
3330 goto out;
3331 if (!ssb_device_is_enabled(dev->dev)) {
3332 tmp = phy->gmode ? B43_TMSLOW_GMODE : 0;
3333 b43_wireless_core_reset(dev, tmp);
3336 if ((phy->type == B43_PHYTYPE_B) || (phy->type == B43_PHYTYPE_G)) {
3337 phy->lo_control =
3338 kzalloc(sizeof(*(phy->lo_control)), GFP_KERNEL);
3339 if (!phy->lo_control) {
3340 err = -ENOMEM;
3341 goto err_busdown;
3344 setup_struct_wldev_for_init(dev);
3346 err = b43_phy_init_tssi2dbm_table(dev);
3347 if (err)
3348 goto err_kfree_lo_control;
3350 /* Enable IRQ routing to this device. */
3351 ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
3353 b43_imcfglo_timeouts_workaround(dev);
3354 b43_bluetooth_coext_disable(dev);
3355 b43_phy_early_init(dev);
3356 err = b43_chip_init(dev);
3357 if (err)
3358 goto err_kfree_tssitbl;
3359 b43_shm_write16(dev, B43_SHM_SHARED,
3360 B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
3361 hf = b43_hf_read(dev);
3362 if (phy->type == B43_PHYTYPE_G) {
3363 hf |= B43_HF_SYMW;
3364 if (phy->rev == 1)
3365 hf |= B43_HF_GDCW;
3366 if (sprom->boardflags_lo & B43_BFL_PACTRL)
3367 hf |= B43_HF_OFDMPABOOST;
3368 } else if (phy->type == B43_PHYTYPE_B) {
3369 hf |= B43_HF_SYMW;
3370 if (phy->rev >= 2 && phy->radio_ver == 0x2050)
3371 hf &= ~B43_HF_GDCW;
3373 b43_hf_write(dev, hf);
3375 b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
3376 B43_DEFAULT_LONG_RETRY_LIMIT);
3377 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SFFBLIM, 3);
3378 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_LFFBLIM, 2);
3380 /* Disable sending probe responses from firmware.
3381 * Setting the MaxTime to one usec will always trigger
3382 * a timeout, so we never send any probe resp.
3383 * A timeout of zero is infinite. */
3384 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
3386 b43_rate_memory_init(dev);
3388 /* Minimum Contention Window */
3389 if (phy->type == B43_PHYTYPE_B) {
3390 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0x1F);
3391 } else {
3392 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MINCONT, 0xF);
3394 /* Maximum Contention Window */
3395 b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
3397 do {
3398 if (b43_using_pio(dev)) {
3399 err = b43_pio_init(dev);
3400 } else {
3401 err = b43_dma_init(dev);
3402 if (!err)
3403 b43_qos_init(dev);
3405 } while (err == -EAGAIN);
3406 if (err)
3407 goto err_chip_exit;
3409 //FIXME
3410 #if 1
3411 b43_write16(dev, 0x0612, 0x0050);
3412 b43_shm_write16(dev, B43_SHM_SHARED, 0x0416, 0x0050);
3413 b43_shm_write16(dev, B43_SHM_SHARED, 0x0414, 0x01F4);
3414 #endif
3416 b43_bluetooth_coext_enable(dev);
3418 ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
3419 memset(wl->bssid, 0, ETH_ALEN);
3420 memset(wl->mac_addr, 0, ETH_ALEN);
3421 b43_upload_card_macaddress(dev);
3422 b43_security_init(dev);
3423 b43_rng_init(wl);
3425 b43_set_status(dev, B43_STAT_INITIALIZED);
3427 b43_leds_init(dev);
3428 out:
3429 return err;
3431 err_chip_exit:
3432 b43_chip_exit(dev);
3433 err_kfree_tssitbl:
3434 if (phy->dyn_tssi_tbl)
3435 kfree(phy->tssi2dbm);
3436 err_kfree_lo_control:
3437 kfree(phy->lo_control);
3438 phy->lo_control = NULL;
3439 err_busdown:
3440 ssb_bus_may_powerdown(bus);
3441 B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
3442 return err;
3445 static int b43_op_add_interface(struct ieee80211_hw *hw,
3446 struct ieee80211_if_init_conf *conf)
3448 struct b43_wl *wl = hw_to_b43_wl(hw);
3449 struct b43_wldev *dev;
3450 unsigned long flags;
3451 int err = -EOPNOTSUPP;
3453 /* TODO: allow WDS/AP devices to coexist */
3455 if (conf->type != IEEE80211_IF_TYPE_AP &&
3456 conf->type != IEEE80211_IF_TYPE_STA &&
3457 conf->type != IEEE80211_IF_TYPE_WDS &&
3458 conf->type != IEEE80211_IF_TYPE_IBSS)
3459 return -EOPNOTSUPP;
3461 mutex_lock(&wl->mutex);
3462 if (wl->operating)
3463 goto out_mutex_unlock;
3465 b43dbg(wl, "Adding Interface type %d\n", conf->type);
3467 dev = wl->current_dev;
3468 wl->operating = 1;
3469 wl->if_id = conf->if_id;
3470 wl->if_type = conf->type;
3471 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
3473 spin_lock_irqsave(&wl->irq_lock, flags);
3474 b43_adjust_opmode(dev);
3475 b43_upload_card_macaddress(dev);
3476 spin_unlock_irqrestore(&wl->irq_lock, flags);
3478 err = 0;
3479 out_mutex_unlock:
3480 mutex_unlock(&wl->mutex);
3482 return err;
3485 static void b43_op_remove_interface(struct ieee80211_hw *hw,
3486 struct ieee80211_if_init_conf *conf)
3488 struct b43_wl *wl = hw_to_b43_wl(hw);
3489 struct b43_wldev *dev = wl->current_dev;
3490 unsigned long flags;
3492 b43dbg(wl, "Removing Interface type %d\n", conf->type);
3494 mutex_lock(&wl->mutex);
3496 B43_WARN_ON(!wl->operating);
3497 B43_WARN_ON(wl->if_id != conf->if_id);
3499 wl->operating = 0;
3501 spin_lock_irqsave(&wl->irq_lock, flags);
3502 b43_adjust_opmode(dev);
3503 memset(wl->mac_addr, 0, ETH_ALEN);
3504 b43_upload_card_macaddress(dev);
3505 spin_unlock_irqrestore(&wl->irq_lock, flags);
3507 mutex_unlock(&wl->mutex);
3510 static int b43_op_start(struct ieee80211_hw *hw)
3512 struct b43_wl *wl = hw_to_b43_wl(hw);
3513 struct b43_wldev *dev = wl->current_dev;
3514 int did_init = 0;
3515 int err = 0;
3517 /* First register RFkill.
3518 * LEDs that are registered later depend on it. */
3519 b43_rfkill_init(dev);
3521 mutex_lock(&wl->mutex);
3523 if (b43_status(dev) < B43_STAT_INITIALIZED) {
3524 err = b43_wireless_core_init(dev);
3525 if (err)
3526 goto out_mutex_unlock;
3527 did_init = 1;
3530 if (b43_status(dev) < B43_STAT_STARTED) {
3531 err = b43_wireless_core_start(dev);
3532 if (err) {
3533 if (did_init)
3534 b43_wireless_core_exit(dev);
3535 goto out_mutex_unlock;
3539 out_mutex_unlock:
3540 mutex_unlock(&wl->mutex);
3542 return err;
3545 static void b43_op_stop(struct ieee80211_hw *hw)
3547 struct b43_wl *wl = hw_to_b43_wl(hw);
3548 struct b43_wldev *dev = wl->current_dev;
3550 b43_rfkill_exit(dev);
3552 mutex_lock(&wl->mutex);
3553 if (b43_status(dev) >= B43_STAT_STARTED)
3554 b43_wireless_core_stop(dev);
3555 b43_wireless_core_exit(dev);
3556 mutex_unlock(&wl->mutex);
3559 static int b43_op_set_retry_limit(struct ieee80211_hw *hw,
3560 u32 short_retry_limit, u32 long_retry_limit)
3562 struct b43_wl *wl = hw_to_b43_wl(hw);
3563 struct b43_wldev *dev;
3564 int err = 0;
3566 mutex_lock(&wl->mutex);
3567 dev = wl->current_dev;
3568 if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED))) {
3569 err = -ENODEV;
3570 goto out_unlock;
3572 b43_set_retry_limits(dev, short_retry_limit, long_retry_limit);
3573 out_unlock:
3574 mutex_unlock(&wl->mutex);
3576 return err;
3579 static const struct ieee80211_ops b43_hw_ops = {
3580 .tx = b43_op_tx,
3581 .conf_tx = b43_op_conf_tx,
3582 .add_interface = b43_op_add_interface,
3583 .remove_interface = b43_op_remove_interface,
3584 .config = b43_op_config,
3585 .config_interface = b43_op_config_interface,
3586 .configure_filter = b43_op_configure_filter,
3587 .set_key = b43_op_set_key,
3588 .get_stats = b43_op_get_stats,
3589 .get_tx_stats = b43_op_get_tx_stats,
3590 .start = b43_op_start,
3591 .stop = b43_op_stop,
3592 .set_retry_limit = b43_op_set_retry_limit,
3595 /* Hard-reset the chip. Do not call this directly.
3596 * Use b43_controller_restart()
3598 static void b43_chip_reset(struct work_struct *work)
3600 struct b43_wldev *dev =
3601 container_of(work, struct b43_wldev, restart_work);
3602 struct b43_wl *wl = dev->wl;
3603 int err = 0;
3604 int prev_status;
3606 mutex_lock(&wl->mutex);
3608 prev_status = b43_status(dev);
3609 /* Bring the device down... */
3610 if (prev_status >= B43_STAT_STARTED)
3611 b43_wireless_core_stop(dev);
3612 if (prev_status >= B43_STAT_INITIALIZED)
3613 b43_wireless_core_exit(dev);
3615 /* ...and up again. */
3616 if (prev_status >= B43_STAT_INITIALIZED) {
3617 err = b43_wireless_core_init(dev);
3618 if (err)
3619 goto out;
3621 if (prev_status >= B43_STAT_STARTED) {
3622 err = b43_wireless_core_start(dev);
3623 if (err) {
3624 b43_wireless_core_exit(dev);
3625 goto out;
3628 out:
3629 mutex_unlock(&wl->mutex);
3630 if (err)
3631 b43err(wl, "Controller restart FAILED\n");
3632 else
3633 b43info(wl, "Controller restarted\n");
3636 static int b43_setup_modes(struct b43_wldev *dev,
3637 int have_aphy, int have_bphy, int have_gphy)
3639 struct ieee80211_hw *hw = dev->wl->hw;
3640 struct ieee80211_hw_mode *mode;
3641 struct b43_phy *phy = &dev->phy;
3642 int cnt = 0;
3643 int err;
3645 /*FIXME: Don't tell ieee80211 about an A-PHY, because we currently don't support A-PHY. */
3646 have_aphy = 0;
3648 phy->possible_phymodes = 0;
3649 for (; 1; cnt++) {
3650 if (have_aphy) {
3651 B43_WARN_ON(cnt >= B43_MAX_PHYHWMODES);
3652 mode = &phy->hwmodes[cnt];
3654 mode->mode = MODE_IEEE80211A;
3655 mode->num_channels = b43_a_chantable_size;
3656 mode->channels = b43_a_chantable;
3657 mode->num_rates = b43_a_ratetable_size;
3658 mode->rates = b43_a_ratetable;
3659 err = ieee80211_register_hwmode(hw, mode);
3660 if (err)
3661 return err;
3663 phy->possible_phymodes |= B43_PHYMODE_A;
3664 have_aphy = 0;
3665 continue;
3667 if (have_bphy) {
3668 B43_WARN_ON(cnt >= B43_MAX_PHYHWMODES);
3669 mode = &phy->hwmodes[cnt];
3671 mode->mode = MODE_IEEE80211B;
3672 mode->num_channels = b43_bg_chantable_size;
3673 mode->channels = b43_bg_chantable;
3674 mode->num_rates = b43_b_ratetable_size;
3675 mode->rates = b43_b_ratetable;
3676 err = ieee80211_register_hwmode(hw, mode);
3677 if (err)
3678 return err;
3680 phy->possible_phymodes |= B43_PHYMODE_B;
3681 have_bphy = 0;
3682 continue;
3684 if (have_gphy) {
3685 B43_WARN_ON(cnt >= B43_MAX_PHYHWMODES);
3686 mode = &phy->hwmodes[cnt];
3688 mode->mode = MODE_IEEE80211G;
3689 mode->num_channels = b43_bg_chantable_size;
3690 mode->channels = b43_bg_chantable;
3691 mode->num_rates = b43_g_ratetable_size;
3692 mode->rates = b43_g_ratetable;
3693 err = ieee80211_register_hwmode(hw, mode);
3694 if (err)
3695 return err;
3697 phy->possible_phymodes |= B43_PHYMODE_G;
3698 have_gphy = 0;
3699 continue;
3701 break;
3704 return 0;
3707 static void b43_wireless_core_detach(struct b43_wldev *dev)
3709 /* We release firmware that late to not be required to re-request
3710 * is all the time when we reinit the core. */
3711 b43_release_firmware(dev);
3714 static int b43_wireless_core_attach(struct b43_wldev *dev)
3716 struct b43_wl *wl = dev->wl;
3717 struct ssb_bus *bus = dev->dev->bus;
3718 struct pci_dev *pdev = bus->host_pci;
3719 int err;
3720 int have_aphy = 0, have_bphy = 0, have_gphy = 0;
3721 u32 tmp;
3723 /* Do NOT do any device initialization here.
3724 * Do it in wireless_core_init() instead.
3725 * This function is for gathering basic information about the HW, only.
3726 * Also some structs may be set up here. But most likely you want to have
3727 * that in core_init(), too.
3730 err = ssb_bus_powerup(bus, 0);
3731 if (err) {
3732 b43err(wl, "Bus powerup failed\n");
3733 goto out;
3735 /* Get the PHY type. */
3736 if (dev->dev->id.revision >= 5) {
3737 u32 tmshigh;
3739 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
3740 have_aphy = !!(tmshigh & B43_TMSHIGH_APHY);
3741 have_gphy = !!(tmshigh & B43_TMSHIGH_GPHY);
3742 if (!have_aphy && !have_gphy)
3743 have_bphy = 1;
3744 } else if (dev->dev->id.revision == 4) {
3745 have_gphy = 1;
3746 have_aphy = 1;
3747 } else
3748 have_bphy = 1;
3750 dev->phy.gmode = (have_gphy || have_bphy);
3751 tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
3752 b43_wireless_core_reset(dev, tmp);
3754 err = b43_phy_versioning(dev);
3755 if (err)
3756 goto err_powerdown;
3757 /* Check if this device supports multiband. */
3758 if (!pdev ||
3759 (pdev->device != 0x4312 &&
3760 pdev->device != 0x4319 && pdev->device != 0x4324)) {
3761 /* No multiband support. */
3762 have_aphy = 0;
3763 have_bphy = 0;
3764 have_gphy = 0;
3765 switch (dev->phy.type) {
3766 case B43_PHYTYPE_A:
3767 have_aphy = 1;
3768 break;
3769 case B43_PHYTYPE_B:
3770 have_bphy = 1;
3771 break;
3772 case B43_PHYTYPE_G:
3773 have_gphy = 1;
3774 break;
3775 default:
3776 B43_WARN_ON(1);
3779 dev->phy.gmode = (have_gphy || have_bphy);
3780 tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
3781 b43_wireless_core_reset(dev, tmp);
3783 err = b43_validate_chipaccess(dev);
3784 if (err)
3785 goto err_powerdown;
3786 err = b43_setup_modes(dev, have_aphy, have_bphy, have_gphy);
3787 if (err)
3788 goto err_powerdown;
3790 /* Now set some default "current_dev" */
3791 if (!wl->current_dev)
3792 wl->current_dev = dev;
3793 INIT_WORK(&dev->restart_work, b43_chip_reset);
3795 b43_radio_turn_off(dev, 1);
3796 b43_switch_analog(dev, 0);
3797 ssb_device_disable(dev->dev, 0);
3798 ssb_bus_may_powerdown(bus);
3800 out:
3801 return err;
3803 err_powerdown:
3804 ssb_bus_may_powerdown(bus);
3805 return err;
3808 static void b43_one_core_detach(struct ssb_device *dev)
3810 struct b43_wldev *wldev;
3811 struct b43_wl *wl;
3813 wldev = ssb_get_drvdata(dev);
3814 wl = wldev->wl;
3815 cancel_work_sync(&wldev->restart_work);
3816 b43_debugfs_remove_device(wldev);
3817 b43_wireless_core_detach(wldev);
3818 list_del(&wldev->list);
3819 wl->nr_devs--;
3820 ssb_set_drvdata(dev, NULL);
3821 kfree(wldev);
3824 static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
3826 struct b43_wldev *wldev;
3827 struct pci_dev *pdev;
3828 int err = -ENOMEM;
3830 if (!list_empty(&wl->devlist)) {
3831 /* We are not the first core on this chip. */
3832 pdev = dev->bus->host_pci;
3833 /* Only special chips support more than one wireless
3834 * core, although some of the other chips have more than
3835 * one wireless core as well. Check for this and
3836 * bail out early.
3838 if (!pdev ||
3839 ((pdev->device != 0x4321) &&
3840 (pdev->device != 0x4313) && (pdev->device != 0x431A))) {
3841 b43dbg(wl, "Ignoring unconnected 802.11 core\n");
3842 return -ENODEV;
3846 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
3847 if (!wldev)
3848 goto out;
3850 wldev->dev = dev;
3851 wldev->wl = wl;
3852 b43_set_status(wldev, B43_STAT_UNINIT);
3853 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
3854 tasklet_init(&wldev->isr_tasklet,
3855 (void (*)(unsigned long))b43_interrupt_tasklet,
3856 (unsigned long)wldev);
3857 if (modparam_pio)
3858 wldev->__using_pio = 1;
3859 INIT_LIST_HEAD(&wldev->list);
3861 err = b43_wireless_core_attach(wldev);
3862 if (err)
3863 goto err_kfree_wldev;
3865 list_add(&wldev->list, &wl->devlist);
3866 wl->nr_devs++;
3867 ssb_set_drvdata(dev, wldev);
3868 b43_debugfs_add_device(wldev);
3870 out:
3871 return err;
3873 err_kfree_wldev:
3874 kfree(wldev);
3875 return err;
3878 static void b43_sprom_fixup(struct ssb_bus *bus)
3880 /* boardflags workarounds */
3881 if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
3882 bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
3883 bus->sprom.boardflags_lo |= B43_BFL_BTCOEXIST;
3884 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
3885 bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
3886 bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
3888 /* Handle case when gain is not set in sprom */
3889 if (bus->sprom.antenna_gain_a == 0xFF)
3890 bus->sprom.antenna_gain_a = 2;
3891 if (bus->sprom.antenna_gain_bg == 0xFF)
3892 bus->sprom.antenna_gain_bg = 2;
3894 /* Convert Antennagain values to Q5.2 */
3895 bus->sprom.antenna_gain_a <<= 2;
3896 bus->sprom.antenna_gain_bg <<= 2;
3899 static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
3901 struct ieee80211_hw *hw = wl->hw;
3903 ssb_set_devtypedata(dev, NULL);
3904 ieee80211_free_hw(hw);
3907 static int b43_wireless_init(struct ssb_device *dev)
3909 struct ssb_sprom *sprom = &dev->bus->sprom;
3910 struct ieee80211_hw *hw;
3911 struct b43_wl *wl;
3912 int err = -ENOMEM;
3914 b43_sprom_fixup(dev->bus);
3916 hw = ieee80211_alloc_hw(sizeof(*wl), &b43_hw_ops);
3917 if (!hw) {
3918 b43err(NULL, "Could not allocate ieee80211 device\n");
3919 goto out;
3922 /* fill hw info */
3923 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
3924 IEEE80211_HW_RX_INCLUDES_FCS;
3925 hw->max_signal = 100;
3926 hw->max_rssi = -110;
3927 hw->max_noise = -110;
3928 hw->queues = 1; /* FIXME: hardware has more queues */
3929 SET_IEEE80211_DEV(hw, dev->dev);
3930 if (is_valid_ether_addr(sprom->et1mac))
3931 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
3932 else
3933 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
3935 /* Get and initialize struct b43_wl */
3936 wl = hw_to_b43_wl(hw);
3937 memset(wl, 0, sizeof(*wl));
3938 wl->hw = hw;
3939 spin_lock_init(&wl->irq_lock);
3940 spin_lock_init(&wl->leds_lock);
3941 mutex_init(&wl->mutex);
3942 INIT_LIST_HEAD(&wl->devlist);
3944 ssb_set_devtypedata(dev, wl);
3945 b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
3946 err = 0;
3947 out:
3948 return err;
3951 static int b43_probe(struct ssb_device *dev, const struct ssb_device_id *id)
3953 struct b43_wl *wl;
3954 int err;
3955 int first = 0;
3957 wl = ssb_get_devtypedata(dev);
3958 if (!wl) {
3959 /* Probing the first core. Must setup common struct b43_wl */
3960 first = 1;
3961 err = b43_wireless_init(dev);
3962 if (err)
3963 goto out;
3964 wl = ssb_get_devtypedata(dev);
3965 B43_WARN_ON(!wl);
3967 err = b43_one_core_attach(dev, wl);
3968 if (err)
3969 goto err_wireless_exit;
3971 if (first) {
3972 err = ieee80211_register_hw(wl->hw);
3973 if (err)
3974 goto err_one_core_detach;
3977 out:
3978 return err;
3980 err_one_core_detach:
3981 b43_one_core_detach(dev);
3982 err_wireless_exit:
3983 if (first)
3984 b43_wireless_exit(dev, wl);
3985 return err;
3988 static void b43_remove(struct ssb_device *dev)
3990 struct b43_wl *wl = ssb_get_devtypedata(dev);
3991 struct b43_wldev *wldev = ssb_get_drvdata(dev);
3993 B43_WARN_ON(!wl);
3994 if (wl->current_dev == wldev)
3995 ieee80211_unregister_hw(wl->hw);
3997 b43_one_core_detach(dev);
3999 if (list_empty(&wl->devlist)) {
4000 /* Last core on the chip unregistered.
4001 * We can destroy common struct b43_wl.
4003 b43_wireless_exit(dev, wl);
4007 /* Perform a hardware reset. This can be called from any context. */
4008 void b43_controller_restart(struct b43_wldev *dev, const char *reason)
4010 /* Must avoid requeueing, if we are in shutdown. */
4011 if (b43_status(dev) < B43_STAT_INITIALIZED)
4012 return;
4013 b43info(dev->wl, "Controller RESET (%s) ...\n", reason);
4014 queue_work(dev->wl->hw->workqueue, &dev->restart_work);
4017 #ifdef CONFIG_PM
4019 static int b43_suspend(struct ssb_device *dev, pm_message_t state)
4021 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4022 struct b43_wl *wl = wldev->wl;
4024 b43dbg(wl, "Suspending...\n");
4026 mutex_lock(&wl->mutex);
4027 wldev->suspend_init_status = b43_status(wldev);
4028 if (wldev->suspend_init_status >= B43_STAT_STARTED)
4029 b43_wireless_core_stop(wldev);
4030 if (wldev->suspend_init_status >= B43_STAT_INITIALIZED)
4031 b43_wireless_core_exit(wldev);
4032 mutex_unlock(&wl->mutex);
4034 b43dbg(wl, "Device suspended.\n");
4036 return 0;
4039 static int b43_resume(struct ssb_device *dev)
4041 struct b43_wldev *wldev = ssb_get_drvdata(dev);
4042 struct b43_wl *wl = wldev->wl;
4043 int err = 0;
4045 b43dbg(wl, "Resuming...\n");
4047 mutex_lock(&wl->mutex);
4048 if (wldev->suspend_init_status >= B43_STAT_INITIALIZED) {
4049 err = b43_wireless_core_init(wldev);
4050 if (err) {
4051 b43err(wl, "Resume failed at core init\n");
4052 goto out;
4055 if (wldev->suspend_init_status >= B43_STAT_STARTED) {
4056 err = b43_wireless_core_start(wldev);
4057 if (err) {
4058 b43_wireless_core_exit(wldev);
4059 b43err(wl, "Resume failed at core start\n");
4060 goto out;
4063 mutex_unlock(&wl->mutex);
4065 b43dbg(wl, "Device resumed.\n");
4066 out:
4067 return err;
4070 #else /* CONFIG_PM */
4071 # define b43_suspend NULL
4072 # define b43_resume NULL
4073 #endif /* CONFIG_PM */
4075 static struct ssb_driver b43_ssb_driver = {
4076 .name = KBUILD_MODNAME,
4077 .id_table = b43_ssb_tbl,
4078 .probe = b43_probe,
4079 .remove = b43_remove,
4080 .suspend = b43_suspend,
4081 .resume = b43_resume,
4084 static int __init b43_init(void)
4086 int err;
4088 b43_debugfs_init();
4089 err = b43_pcmcia_init();
4090 if (err)
4091 goto err_dfs_exit;
4092 err = ssb_driver_register(&b43_ssb_driver);
4093 if (err)
4094 goto err_pcmcia_exit;
4096 return err;
4098 err_pcmcia_exit:
4099 b43_pcmcia_exit();
4100 err_dfs_exit:
4101 b43_debugfs_exit();
4102 return err;
4105 static void __exit b43_exit(void)
4107 ssb_driver_unregister(&b43_ssb_driver);
4108 b43_pcmcia_exit();
4109 b43_debugfs_exit();
4112 module_init(b43_init)
4113 module_exit(b43_exit)