blob: 6c8eb4d2519ad5da678bc41e0bd88ccc6074f236 [file] [log] [blame]
Larry Finger75388ac2007-09-25 16:46:54 -07001/*
2 *
3 * Broadcom B43legacy wireless driver
4 *
5 * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio6fff1c62008-02-09 07:20:43 +01006 * Copyright (c) 2005-2008 Stefano Brivio <stefano.brivio@polimi.it>
Larry Finger75388ac2007-09-25 16:46:54 -07007 * Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
8 * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
9 * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
10 * Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
11 *
12 * Some parts of the code in this file are derived from the ipw2200
13 * driver Copyright(c) 2003 - 2004 Intel Corporation.
14
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; see the file COPYING. If not, write to
27 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
29 *
30 */
31
32#include <linux/delay.h>
33#include <linux/init.h>
34#include <linux/moduleparam.h>
35#include <linux/if_arp.h>
36#include <linux/etherdevice.h>
Larry Finger75388ac2007-09-25 16:46:54 -070037#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 <net/dst.h>
43#include <asm/unaligned.h>
44
45#include "b43legacy.h"
46#include "main.h"
47#include "debugfs.h"
48#include "phy.h"
49#include "dma.h"
50#include "pio.h"
51#include "sysfs.h"
52#include "xmit.h"
53#include "radio.h"
54
55
56MODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
57MODULE_AUTHOR("Martin Langer");
58MODULE_AUTHOR("Stefano Brivio");
59MODULE_AUTHOR("Michael Buesch");
60MODULE_LICENSE("GPL");
61
Stefano Brivio1a1c3602008-02-12 03:09:00 +010062MODULE_FIRMWARE(B43legacy_SUPPORTED_FIRMWARE_ID);
63
Larry Finger75388ac2007-09-25 16:46:54 -070064#if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
65static int modparam_pio;
66module_param_named(pio, modparam_pio, int, 0444);
67MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
68#elif defined(CONFIG_B43LEGACY_DMA)
69# define modparam_pio 0
70#elif defined(CONFIG_B43LEGACY_PIO)
71# define modparam_pio 1
72#endif
73
74static int modparam_bad_frames_preempt;
75module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
76MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
77 " Preemption");
78
Larry Finger75388ac2007-09-25 16:46:54 -070079static char modparam_fwpostfix[16];
80module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
81MODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
82
Larry Finger75388ac2007-09-25 16:46:54 -070083/* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
84static const struct ssb_device_id b43legacy_ssb_tbl[] = {
85 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
86 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
87 SSB_DEVTABLE_END
88};
89MODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
90
91
92/* Channel and ratetables are shared for all devices.
93 * They can't be const, because ieee80211 puts some precalculated
94 * data in there. This data is the same for all devices, so we don't
95 * get concurrency issues */
96#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +010097 { \
98 .bitrate = B43legacy_RATE_TO_100KBPS(_rateid), \
99 .hw_value = (_rateid), \
100 .flags = (_flags), \
Larry Finger75388ac2007-09-25 16:46:54 -0700101 }
Johannes Berg8318d782008-01-24 19:38:38 +0100102/*
103 * NOTE: When changing this, sync with xmit.c's
104 * b43legacy_plcp_get_bitrate_idx_* functions!
105 */
Larry Finger75388ac2007-09-25 16:46:54 -0700106static struct ieee80211_rate __b43legacy_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100107 RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
108 RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
109 RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
110 RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
111 RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
112 RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
113 RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
114 RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
115 RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
116 RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
117 RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
118 RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
Larry Finger75388ac2007-09-25 16:46:54 -0700119};
Larry Finger75388ac2007-09-25 16:46:54 -0700120#define b43legacy_b_ratetable (__b43legacy_ratetable + 0)
121#define b43legacy_b_ratetable_size 4
122#define b43legacy_g_ratetable (__b43legacy_ratetable + 0)
123#define b43legacy_g_ratetable_size 12
124
125#define CHANTAB_ENT(_chanid, _freq) \
126 { \
Johannes Berg8318d782008-01-24 19:38:38 +0100127 .center_freq = (_freq), \
128 .hw_value = (_chanid), \
Larry Finger75388ac2007-09-25 16:46:54 -0700129 }
130static struct ieee80211_channel b43legacy_bg_chantable[] = {
131 CHANTAB_ENT(1, 2412),
132 CHANTAB_ENT(2, 2417),
133 CHANTAB_ENT(3, 2422),
134 CHANTAB_ENT(4, 2427),
135 CHANTAB_ENT(5, 2432),
136 CHANTAB_ENT(6, 2437),
137 CHANTAB_ENT(7, 2442),
138 CHANTAB_ENT(8, 2447),
139 CHANTAB_ENT(9, 2452),
140 CHANTAB_ENT(10, 2457),
141 CHANTAB_ENT(11, 2462),
142 CHANTAB_ENT(12, 2467),
143 CHANTAB_ENT(13, 2472),
144 CHANTAB_ENT(14, 2484),
145};
Johannes Berg8318d782008-01-24 19:38:38 +0100146
147static struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
148 .channels = b43legacy_bg_chantable,
149 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
150 .bitrates = b43legacy_b_ratetable,
151 .n_bitrates = b43legacy_b_ratetable_size,
152};
153
154static struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
155 .channels = b43legacy_bg_chantable,
156 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
157 .bitrates = b43legacy_g_ratetable,
158 .n_bitrates = b43legacy_g_ratetable_size,
159};
Larry Finger75388ac2007-09-25 16:46:54 -0700160
161static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
162static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
163static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
164static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
165
166
167static int b43legacy_ratelimit(struct b43legacy_wl *wl)
168{
169 if (!wl || !wl->current_dev)
170 return 1;
171 if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
172 return 1;
173 /* We are up and running.
174 * Ratelimit the messages to avoid DoS over the net. */
175 return net_ratelimit();
176}
177
178void b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
179{
180 va_list args;
181
182 if (!b43legacy_ratelimit(wl))
183 return;
184 va_start(args, fmt);
185 printk(KERN_INFO "b43legacy-%s: ",
186 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
187 vprintk(fmt, args);
188 va_end(args);
189}
190
191void b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
192{
193 va_list args;
194
195 if (!b43legacy_ratelimit(wl))
196 return;
197 va_start(args, fmt);
198 printk(KERN_ERR "b43legacy-%s ERROR: ",
199 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
200 vprintk(fmt, args);
201 va_end(args);
202}
203
204void b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
205{
206 va_list args;
207
208 if (!b43legacy_ratelimit(wl))
209 return;
210 va_start(args, fmt);
211 printk(KERN_WARNING "b43legacy-%s warning: ",
212 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
213 vprintk(fmt, args);
214 va_end(args);
215}
216
217#if B43legacy_DEBUG
218void b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
219{
220 va_list args;
221
222 va_start(args, fmt);
223 printk(KERN_DEBUG "b43legacy-%s debug: ",
224 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
225 vprintk(fmt, args);
226 va_end(args);
227}
228#endif /* DEBUG */
229
230static void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
231 u32 val)
232{
233 u32 status;
234
235 B43legacy_WARN_ON(offset % 4 != 0);
236
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100237 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
238 if (status & B43legacy_MACCTL_BE)
Larry Finger75388ac2007-09-25 16:46:54 -0700239 val = swab32(val);
240
241 b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
242 mmiowb();
243 b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
244}
245
246static inline
247void b43legacy_shm_control_word(struct b43legacy_wldev *dev,
248 u16 routing, u16 offset)
249{
250 u32 control;
251
252 /* "offset" is the WORD offset. */
253
254 control = routing;
255 control <<= 16;
256 control |= offset;
257 b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
258}
259
260u32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
261 u16 routing, u16 offset)
262{
263 u32 ret;
264
265 if (routing == B43legacy_SHM_SHARED) {
266 B43legacy_WARN_ON((offset & 0x0001) != 0);
267 if (offset & 0x0003) {
268 /* Unaligned access */
269 b43legacy_shm_control_word(dev, routing, offset >> 2);
270 ret = b43legacy_read16(dev,
271 B43legacy_MMIO_SHM_DATA_UNALIGNED);
272 ret <<= 16;
273 b43legacy_shm_control_word(dev, routing,
274 (offset >> 2) + 1);
275 ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
276
277 return ret;
278 }
279 offset >>= 2;
280 }
281 b43legacy_shm_control_word(dev, routing, offset);
282 ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
283
284 return ret;
285}
286
287u16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
288 u16 routing, u16 offset)
289{
290 u16 ret;
291
292 if (routing == B43legacy_SHM_SHARED) {
293 B43legacy_WARN_ON((offset & 0x0001) != 0);
294 if (offset & 0x0003) {
295 /* Unaligned access */
296 b43legacy_shm_control_word(dev, routing, offset >> 2);
297 ret = b43legacy_read16(dev,
298 B43legacy_MMIO_SHM_DATA_UNALIGNED);
299
300 return ret;
301 }
302 offset >>= 2;
303 }
304 b43legacy_shm_control_word(dev, routing, offset);
305 ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
306
307 return ret;
308}
309
310void b43legacy_shm_write32(struct b43legacy_wldev *dev,
311 u16 routing, u16 offset,
312 u32 value)
313{
314 if (routing == B43legacy_SHM_SHARED) {
315 B43legacy_WARN_ON((offset & 0x0001) != 0);
316 if (offset & 0x0003) {
317 /* Unaligned access */
318 b43legacy_shm_control_word(dev, routing, offset >> 2);
319 mmiowb();
320 b43legacy_write16(dev,
321 B43legacy_MMIO_SHM_DATA_UNALIGNED,
322 (value >> 16) & 0xffff);
323 mmiowb();
324 b43legacy_shm_control_word(dev, routing,
325 (offset >> 2) + 1);
326 mmiowb();
327 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
328 value & 0xffff);
329 return;
330 }
331 offset >>= 2;
332 }
333 b43legacy_shm_control_word(dev, routing, offset);
334 mmiowb();
335 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
336}
337
338void b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
339 u16 value)
340{
341 if (routing == B43legacy_SHM_SHARED) {
342 B43legacy_WARN_ON((offset & 0x0001) != 0);
343 if (offset & 0x0003) {
344 /* Unaligned access */
345 b43legacy_shm_control_word(dev, routing, offset >> 2);
346 mmiowb();
347 b43legacy_write16(dev,
348 B43legacy_MMIO_SHM_DATA_UNALIGNED,
349 value);
350 return;
351 }
352 offset >>= 2;
353 }
354 b43legacy_shm_control_word(dev, routing, offset);
355 mmiowb();
356 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
357}
358
359/* Read HostFlags */
360u32 b43legacy_hf_read(struct b43legacy_wldev *dev)
361{
362 u32 ret;
363
364 ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
365 B43legacy_SHM_SH_HOSTFHI);
366 ret <<= 16;
367 ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
368 B43legacy_SHM_SH_HOSTFLO);
369
370 return ret;
371}
372
373/* Write HostFlags */
374void b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
375{
376 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
377 B43legacy_SHM_SH_HOSTFLO,
378 (value & 0x0000FFFF));
379 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
380 B43legacy_SHM_SH_HOSTFHI,
381 ((value & 0xFFFF0000) >> 16));
382}
383
384void b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
385{
386 /* We need to be careful. As we read the TSF from multiple
387 * registers, we should take care of register overflows.
388 * In theory, the whole tsf read process should be atomic.
389 * We try to be atomic here, by restaring the read process,
390 * if any of the high registers changed (overflew).
391 */
392 if (dev->dev->id.revision >= 3) {
393 u32 low;
394 u32 high;
395 u32 high2;
396
397 do {
398 high = b43legacy_read32(dev,
399 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
400 low = b43legacy_read32(dev,
401 B43legacy_MMIO_REV3PLUS_TSF_LOW);
402 high2 = b43legacy_read32(dev,
403 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
404 } while (unlikely(high != high2));
405
406 *tsf = high;
407 *tsf <<= 32;
408 *tsf |= low;
409 } else {
410 u64 tmp;
411 u16 v0;
412 u16 v1;
413 u16 v2;
414 u16 v3;
415 u16 test1;
416 u16 test2;
417 u16 test3;
418
419 do {
420 v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
421 v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
422 v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
423 v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
424
425 test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
426 test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
427 test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
428 } while (v3 != test3 || v2 != test2 || v1 != test1);
429
430 *tsf = v3;
431 *tsf <<= 48;
432 tmp = v2;
433 tmp <<= 32;
434 *tsf |= tmp;
435 tmp = v1;
436 tmp <<= 16;
437 *tsf |= tmp;
438 *tsf |= v0;
439 }
440}
441
442static void b43legacy_time_lock(struct b43legacy_wldev *dev)
443{
444 u32 status;
445
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100446 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
447 status |= B43legacy_MACCTL_TBTTHOLD;
448 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
Larry Finger75388ac2007-09-25 16:46:54 -0700449 mmiowb();
450}
451
452static void b43legacy_time_unlock(struct b43legacy_wldev *dev)
453{
454 u32 status;
455
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100456 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
457 status &= ~B43legacy_MACCTL_TBTTHOLD;
458 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
Larry Finger75388ac2007-09-25 16:46:54 -0700459}
460
461static void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
462{
463 /* Be careful with the in-progress timer.
464 * First zero out the low register, so we have a full
465 * register-overflow duration to complete the operation.
466 */
467 if (dev->dev->id.revision >= 3) {
468 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
469 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
470
471 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
472 mmiowb();
473 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
474 hi);
475 mmiowb();
476 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
477 lo);
478 } else {
479 u16 v0 = (tsf & 0x000000000000FFFFULL);
480 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
481 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
482 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
483
484 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
485 mmiowb();
486 b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
487 mmiowb();
488 b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
489 mmiowb();
490 b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
491 mmiowb();
492 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
493 }
494}
495
496void b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
497{
498 b43legacy_time_lock(dev);
499 b43legacy_tsf_write_locked(dev, tsf);
500 b43legacy_time_unlock(dev);
501}
502
503static
504void b43legacy_macfilter_set(struct b43legacy_wldev *dev,
505 u16 offset, const u8 *mac)
506{
507 static const u8 zero_addr[ETH_ALEN] = { 0 };
508 u16 data;
509
510 if (!mac)
511 mac = zero_addr;
512
513 offset |= 0x0020;
514 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
515
516 data = mac[0];
517 data |= mac[1] << 8;
518 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
519 data = mac[2];
520 data |= mac[3] << 8;
521 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
522 data = mac[4];
523 data |= mac[5] << 8;
524 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
525}
526
527static void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
528{
529 static const u8 zero_addr[ETH_ALEN] = { 0 };
530 const u8 *mac = dev->wl->mac_addr;
531 const u8 *bssid = dev->wl->bssid;
532 u8 mac_bssid[ETH_ALEN * 2];
533 int i;
534 u32 tmp;
535
536 if (!bssid)
537 bssid = zero_addr;
538 if (!mac)
539 mac = zero_addr;
540
541 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
542
543 memcpy(mac_bssid, mac, ETH_ALEN);
544 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
545
546 /* Write our MAC address and BSSID to template ram */
547 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
548 tmp = (u32)(mac_bssid[i + 0]);
549 tmp |= (u32)(mac_bssid[i + 1]) << 8;
550 tmp |= (u32)(mac_bssid[i + 2]) << 16;
551 tmp |= (u32)(mac_bssid[i + 3]) << 24;
552 b43legacy_ram_write(dev, 0x20 + i, tmp);
553 b43legacy_ram_write(dev, 0x78 + i, tmp);
554 b43legacy_ram_write(dev, 0x478 + i, tmp);
555 }
556}
557
Johannes Berg4150c572007-09-17 01:29:23 -0400558static void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
Larry Finger75388ac2007-09-25 16:46:54 -0700559{
Larry Finger75388ac2007-09-25 16:46:54 -0700560 b43legacy_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400561 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
562 dev->wl->mac_addr);
Larry Finger75388ac2007-09-25 16:46:54 -0700563}
564
565static void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
566 u16 slot_time)
567{
568 /* slot_time is in usec. */
569 if (dev->phy.type != B43legacy_PHYTYPE_G)
570 return;
571 b43legacy_write16(dev, 0x684, 510 + slot_time);
572 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
573 slot_time);
574}
575
576static void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
577{
578 b43legacy_set_slot_time(dev, 9);
579 dev->short_slot = 1;
580}
581
582static void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
583{
584 b43legacy_set_slot_time(dev, 20);
585 dev->short_slot = 0;
586}
587
588/* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
589 * Returns the _previously_ enabled IRQ mask.
590 */
591static inline u32 b43legacy_interrupt_enable(struct b43legacy_wldev *dev,
592 u32 mask)
593{
594 u32 old_mask;
595
596 old_mask = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
597 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, old_mask |
598 mask);
599
600 return old_mask;
601}
602
603/* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
604 * Returns the _previously_ enabled IRQ mask.
605 */
606static inline u32 b43legacy_interrupt_disable(struct b43legacy_wldev *dev,
607 u32 mask)
608{
609 u32 old_mask;
610
611 old_mask = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
612 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
613
614 return old_mask;
615}
616
617/* Synchronize IRQ top- and bottom-half.
618 * IRQs must be masked before calling this.
619 * This must not be called with the irq_lock held.
620 */
621static void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
622{
623 synchronize_irq(dev->dev->irq);
624 tasklet_kill(&dev->isr_tasklet);
625}
626
627/* DummyTransmission function, as documented on
628 * http://bcm-specs.sipsolutions.net/DummyTransmission
629 */
630void b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
631{
632 struct b43legacy_phy *phy = &dev->phy;
633 unsigned int i;
634 unsigned int max_loop;
635 u16 value;
636 u32 buffer[5] = {
637 0x00000000,
638 0x00D40000,
639 0x00000000,
640 0x01000000,
641 0x00000000,
642 };
643
644 switch (phy->type) {
645 case B43legacy_PHYTYPE_B:
646 case B43legacy_PHYTYPE_G:
647 max_loop = 0xFA;
648 buffer[0] = 0x000B846E;
649 break;
650 default:
651 B43legacy_BUG_ON(1);
652 return;
653 }
654
655 for (i = 0; i < 5; i++)
656 b43legacy_ram_write(dev, i * 4, buffer[i]);
657
658 /* dummy read follows */
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100659 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
Larry Finger75388ac2007-09-25 16:46:54 -0700660
661 b43legacy_write16(dev, 0x0568, 0x0000);
662 b43legacy_write16(dev, 0x07C0, 0x0000);
663 b43legacy_write16(dev, 0x050C, 0x0000);
664 b43legacy_write16(dev, 0x0508, 0x0000);
665 b43legacy_write16(dev, 0x050A, 0x0000);
666 b43legacy_write16(dev, 0x054C, 0x0000);
667 b43legacy_write16(dev, 0x056A, 0x0014);
668 b43legacy_write16(dev, 0x0568, 0x0826);
669 b43legacy_write16(dev, 0x0500, 0x0000);
670 b43legacy_write16(dev, 0x0502, 0x0030);
671
672 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
673 b43legacy_radio_write16(dev, 0x0051, 0x0017);
674 for (i = 0x00; i < max_loop; i++) {
675 value = b43legacy_read16(dev, 0x050E);
676 if (value & 0x0080)
677 break;
678 udelay(10);
679 }
680 for (i = 0x00; i < 0x0A; i++) {
681 value = b43legacy_read16(dev, 0x050E);
682 if (value & 0x0400)
683 break;
684 udelay(10);
685 }
686 for (i = 0x00; i < 0x0A; i++) {
687 value = b43legacy_read16(dev, 0x0690);
688 if (!(value & 0x0100))
689 break;
690 udelay(10);
691 }
692 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
693 b43legacy_radio_write16(dev, 0x0051, 0x0037);
694}
695
696/* Turn the Analog ON/OFF */
697static void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
698{
699 b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
700}
701
702void b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
703{
704 u32 tmslow;
705 u32 macctl;
706
707 flags |= B43legacy_TMSLOW_PHYCLKEN;
708 flags |= B43legacy_TMSLOW_PHYRESET;
709 ssb_device_enable(dev->dev, flags);
710 msleep(2); /* Wait for the PLL to turn on. */
711
712 /* Now take the PHY out of Reset again */
713 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
714 tmslow |= SSB_TMSLOW_FGC;
715 tmslow &= ~B43legacy_TMSLOW_PHYRESET;
716 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
717 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
718 msleep(1);
719 tmslow &= ~SSB_TMSLOW_FGC;
720 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
721 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
722 msleep(1);
723
724 /* Turn Analog ON */
725 b43legacy_switch_analog(dev, 1);
726
727 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
728 macctl &= ~B43legacy_MACCTL_GMODE;
729 if (flags & B43legacy_TMSLOW_GMODE) {
730 macctl |= B43legacy_MACCTL_GMODE;
731 dev->phy.gmode = 1;
732 } else
733 dev->phy.gmode = 0;
734 macctl |= B43legacy_MACCTL_IHR_ENABLED;
735 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
736}
737
738static void handle_irq_transmit_status(struct b43legacy_wldev *dev)
739{
740 u32 v0;
741 u32 v1;
742 u16 tmp;
743 struct b43legacy_txstatus stat;
744
745 while (1) {
746 v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
747 if (!(v0 & 0x00000001))
748 break;
749 v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
750
751 stat.cookie = (v0 >> 16);
752 stat.seq = (v1 & 0x0000FFFF);
753 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
754 tmp = (v0 & 0x0000FFFF);
755 stat.frame_count = ((tmp & 0xF000) >> 12);
756 stat.rts_count = ((tmp & 0x0F00) >> 8);
757 stat.supp_reason = ((tmp & 0x001C) >> 2);
758 stat.pm_indicated = !!(tmp & 0x0080);
759 stat.intermediate = !!(tmp & 0x0040);
760 stat.for_ampdu = !!(tmp & 0x0020);
761 stat.acked = !!(tmp & 0x0002);
762
763 b43legacy_handle_txstatus(dev, &stat);
764 }
765}
766
767static void drain_txstatus_queue(struct b43legacy_wldev *dev)
768{
769 u32 dummy;
770
771 if (dev->dev->id.revision < 5)
772 return;
773 /* Read all entries from the microcode TXstatus FIFO
774 * and throw them away.
775 */
776 while (1) {
777 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
778 if (!(dummy & 0x00000001))
779 break;
780 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
781 }
782}
783
784static u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
785{
786 u32 val = 0;
787
788 val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
789 val <<= 16;
790 val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
791
792 return val;
793}
794
795static void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
796{
797 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
798 (jssi & 0x0000FFFF));
799 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
800 (jssi & 0xFFFF0000) >> 16);
801}
802
803static void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
804{
805 b43legacy_jssi_write(dev, 0x7F7F7F7F);
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100806 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100807 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
808 | B43legacy_MACCMD_BGNOISE);
Larry Finger75388ac2007-09-25 16:46:54 -0700809 B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
810 dev->phy.channel);
811}
812
813static void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
814{
815 /* Top half of Link Quality calculation. */
816
817 if (dev->noisecalc.calculation_running)
818 return;
819 dev->noisecalc.channel_at_start = dev->phy.channel;
820 dev->noisecalc.calculation_running = 1;
821 dev->noisecalc.nr_samples = 0;
822
823 b43legacy_generate_noise_sample(dev);
824}
825
826static void handle_irq_noise(struct b43legacy_wldev *dev)
827{
828 struct b43legacy_phy *phy = &dev->phy;
829 u16 tmp;
830 u8 noise[4];
831 u8 i;
832 u8 j;
833 s32 average;
834
835 /* Bottom half of Link Quality calculation. */
836
837 B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
838 if (dev->noisecalc.channel_at_start != phy->channel)
839 goto drop_calculation;
840 *((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
841 if (noise[0] == 0x7F || noise[1] == 0x7F ||
842 noise[2] == 0x7F || noise[3] == 0x7F)
843 goto generate_new;
844
845 /* Get the noise samples. */
846 B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
847 i = dev->noisecalc.nr_samples;
Harvey Harrisonca216142008-05-02 13:47:49 -0700848 noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
849 noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
850 noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
851 noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
Larry Finger75388ac2007-09-25 16:46:54 -0700852 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
853 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
854 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
855 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
856 dev->noisecalc.nr_samples++;
857 if (dev->noisecalc.nr_samples == 8) {
858 /* Calculate the Link Quality by the noise samples. */
859 average = 0;
860 for (i = 0; i < 8; i++) {
861 for (j = 0; j < 4; j++)
862 average += dev->noisecalc.samples[i][j];
863 }
864 average /= (8 * 4);
865 average *= 125;
866 average += 64;
867 average /= 128;
868 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
869 0x40C);
870 tmp = (tmp / 128) & 0x1F;
871 if (tmp >= 8)
872 average += 2;
873 else
874 average -= 25;
875 if (tmp == 8)
876 average -= 72;
877 else
878 average -= 48;
879
880 dev->stats.link_noise = average;
881drop_calculation:
882 dev->noisecalc.calculation_running = 0;
883 return;
884 }
885generate_new:
886 b43legacy_generate_noise_sample(dev);
887}
888
889static void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
890{
Johannes Berg05c914f2008-09-11 00:01:58 +0200891 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
Larry Finger75388ac2007-09-25 16:46:54 -0700892 /* TODO: PS TBTT */
893 } else {
894 if (1/*FIXME: the last PSpoll frame was sent successfully */)
895 b43legacy_power_saving_ctl_bits(dev, -1, -1);
896 }
Johannes Berg05c914f2008-09-11 00:01:58 +0200897 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100898 dev->dfq_valid = 1;
Larry Finger75388ac2007-09-25 16:46:54 -0700899}
900
901static void handle_irq_atim_end(struct b43legacy_wldev *dev)
902{
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100903 if (dev->dfq_valid) {
904 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
905 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
906 | B43legacy_MACCMD_DFQ_VALID);
907 dev->dfq_valid = 0;
908 }
Larry Finger75388ac2007-09-25 16:46:54 -0700909}
910
911static void handle_irq_pmq(struct b43legacy_wldev *dev)
912{
913 u32 tmp;
914
915 /* TODO: AP mode. */
916
917 while (1) {
918 tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
919 if (!(tmp & 0x00000008))
920 break;
921 }
922 /* 16bit write is odd, but correct. */
923 b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
924}
925
926static void b43legacy_write_template_common(struct b43legacy_wldev *dev,
927 const u8 *data, u16 size,
928 u16 ram_offset,
929 u16 shm_size_offset, u8 rate)
930{
931 u32 i;
932 u32 tmp;
933 struct b43legacy_plcp_hdr4 plcp;
934
935 plcp.data = 0;
936 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
937 b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
938 ram_offset += sizeof(u32);
939 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
940 * So leave the first two bytes of the next write blank.
941 */
942 tmp = (u32)(data[0]) << 16;
943 tmp |= (u32)(data[1]) << 24;
944 b43legacy_ram_write(dev, ram_offset, tmp);
945 ram_offset += sizeof(u32);
946 for (i = 2; i < size; i += sizeof(u32)) {
947 tmp = (u32)(data[i + 0]);
948 if (i + 1 < size)
949 tmp |= (u32)(data[i + 1]) << 8;
950 if (i + 2 < size)
951 tmp |= (u32)(data[i + 2]) << 16;
952 if (i + 3 < size)
953 tmp |= (u32)(data[i + 3]) << 24;
954 b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
955 }
956 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
957 size + sizeof(struct b43legacy_plcp_hdr6));
958}
959
960static void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
961 u16 ram_offset,
962 u16 shm_size_offset, u8 rate)
963{
Larry Finger75388ac2007-09-25 16:46:54 -0700964
Stefano Brivioa2971702008-02-08 06:31:25 +0100965 unsigned int i, len, variable_len;
966 const struct ieee80211_mgmt *bcn;
967 const u8 *ie;
968 bool tim_found = 0;
969
970 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
971 len = min((size_t)dev->wl->current_beacon->len,
Larry Finger75388ac2007-09-25 16:46:54 -0700972 0x200 - sizeof(struct b43legacy_plcp_hdr6));
Stefano Brivioa2971702008-02-08 06:31:25 +0100973
974 b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
Larry Finger75388ac2007-09-25 16:46:54 -0700975 shm_size_offset, rate);
Stefano Brivioa2971702008-02-08 06:31:25 +0100976
977 /* Find the position of the TIM and the DTIM_period value
978 * and write them to SHM. */
979 ie = bcn->u.beacon.variable;
980 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
981 for (i = 0; i < variable_len - 2; ) {
982 uint8_t ie_id, ie_len;
983
984 ie_id = ie[i];
985 ie_len = ie[i + 1];
986 if (ie_id == 5) {
987 u16 tim_position;
988 u16 dtim_period;
989 /* This is the TIM Information Element */
990
991 /* Check whether the ie_len is in the beacon data range. */
992 if (variable_len < ie_len + 2 + i)
993 break;
994 /* A valid TIM is at least 4 bytes long. */
995 if (ie_len < 4)
996 break;
997 tim_found = 1;
998
999 tim_position = sizeof(struct b43legacy_plcp_hdr6);
1000 tim_position += offsetof(struct ieee80211_mgmt,
1001 u.beacon.variable);
1002 tim_position += i;
1003
1004 dtim_period = ie[i + 3];
1005
1006 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1007 B43legacy_SHM_SH_TIMPOS, tim_position);
1008 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1009 B43legacy_SHM_SH_DTIMP, dtim_period);
1010 break;
1011 }
1012 i += ie_len + 2;
1013 }
1014 if (!tim_found) {
1015 b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
1016 "beacon template packet. AP or IBSS operation "
1017 "may be broken.\n");
1018 }
Larry Finger75388ac2007-09-25 16:46:54 -07001019}
1020
1021static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
1022 u16 shm_offset, u16 size,
Johannes Berg8318d782008-01-24 19:38:38 +01001023 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001024{
1025 struct b43legacy_plcp_hdr4 plcp;
1026 u32 tmp;
1027 __le16 dur;
1028
1029 plcp.data = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001030 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->bitrate);
Larry Finger75388ac2007-09-25 16:46:54 -07001031 dur = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01001032 dev->wl->vif,
Larry Finger75388ac2007-09-25 16:46:54 -07001033 size,
Johannes Berg8318d782008-01-24 19:38:38 +01001034 rate);
Larry Finger75388ac2007-09-25 16:46:54 -07001035 /* Write PLCP in two parts and timing for packet transfer */
1036 tmp = le32_to_cpu(plcp.data);
1037 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
1038 tmp & 0xFFFF);
1039 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
1040 tmp >> 16);
1041 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
1042 le16_to_cpu(dur));
1043}
1044
1045/* Instead of using custom probe response template, this function
1046 * just patches custom beacon template by:
1047 * 1) Changing packet type
1048 * 2) Patching duration field
1049 * 3) Stripping TIM
1050 */
Stefano Brivioa2971702008-02-08 06:31:25 +01001051static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
1052 u16 *dest_size,
1053 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001054{
1055 const u8 *src_data;
1056 u8 *dest_data;
Stefano Brivioa2971702008-02-08 06:31:25 +01001057 u16 src_size, elem_size, src_pos, dest_pos;
Larry Finger75388ac2007-09-25 16:46:54 -07001058 __le16 dur;
1059 struct ieee80211_hdr *hdr;
Stefano Brivioa2971702008-02-08 06:31:25 +01001060 size_t ie_start;
Larry Finger75388ac2007-09-25 16:46:54 -07001061
Stefano Brivioa2971702008-02-08 06:31:25 +01001062 src_size = dev->wl->current_beacon->len;
1063 src_data = (const u8 *)dev->wl->current_beacon->data;
Larry Finger75388ac2007-09-25 16:46:54 -07001064
Stefano Brivioa2971702008-02-08 06:31:25 +01001065 /* Get the start offset of the variable IEs in the packet. */
1066 ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
1067 B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
1068 u.beacon.variable));
1069
Stefano Brivio4688be32008-02-08 06:31:39 +01001070 if (B43legacy_WARN_ON(src_size < ie_start))
Larry Finger75388ac2007-09-25 16:46:54 -07001071 return NULL;
Larry Finger75388ac2007-09-25 16:46:54 -07001072
1073 dest_data = kmalloc(src_size, GFP_ATOMIC);
1074 if (unlikely(!dest_data))
1075 return NULL;
1076
Stefano Brivioa2971702008-02-08 06:31:25 +01001077 /* Copy the static data and all Information Elements, except the TIM. */
1078 memcpy(dest_data, src_data, ie_start);
1079 src_pos = ie_start;
1080 dest_pos = ie_start;
1081 for ( ; src_pos < src_size - 2; src_pos += elem_size) {
Larry Finger75388ac2007-09-25 16:46:54 -07001082 elem_size = src_data[src_pos + 1] + 2;
Stefano Brivioa2971702008-02-08 06:31:25 +01001083 if (src_data[src_pos] == 5) {
1084 /* This is the TIM. */
1085 continue;
Larry Finger75388ac2007-09-25 16:46:54 -07001086 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001087 memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
1088 dest_pos += elem_size;
Larry Finger75388ac2007-09-25 16:46:54 -07001089 }
1090 *dest_size = dest_pos;
1091 hdr = (struct ieee80211_hdr *)dest_data;
1092
1093 /* Set the frame control. */
1094 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1095 IEEE80211_STYPE_PROBE_RESP);
1096 dur = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01001097 dev->wl->vif,
Larry Finger75388ac2007-09-25 16:46:54 -07001098 *dest_size,
Johannes Berg8318d782008-01-24 19:38:38 +01001099 rate);
Larry Finger75388ac2007-09-25 16:46:54 -07001100 hdr->duration_id = dur;
1101
1102 return dest_data;
1103}
1104
1105static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
1106 u16 ram_offset,
Johannes Berg8318d782008-01-24 19:38:38 +01001107 u16 shm_size_offset,
1108 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001109{
Stefano Brivioa2971702008-02-08 06:31:25 +01001110 const u8 *probe_resp_data;
Larry Finger75388ac2007-09-25 16:46:54 -07001111 u16 size;
1112
Stefano Brivioa2971702008-02-08 06:31:25 +01001113 size = dev->wl->current_beacon->len;
Larry Finger75388ac2007-09-25 16:46:54 -07001114 probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
1115 if (unlikely(!probe_resp_data))
1116 return;
1117
1118 /* Looks like PLCP headers plus packet timings are stored for
1119 * all possible basic rates
1120 */
1121 b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001122 &b43legacy_b_ratetable[0]);
Larry Finger75388ac2007-09-25 16:46:54 -07001123 b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001124 &b43legacy_b_ratetable[1]);
Larry Finger75388ac2007-09-25 16:46:54 -07001125 b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001126 &b43legacy_b_ratetable[2]);
Larry Finger75388ac2007-09-25 16:46:54 -07001127 b43legacy_write_probe_resp_plcp(dev, 0x350, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001128 &b43legacy_b_ratetable[3]);
Larry Finger75388ac2007-09-25 16:46:54 -07001129
1130 size = min((size_t)size,
1131 0x200 - sizeof(struct b43legacy_plcp_hdr6));
1132 b43legacy_write_template_common(dev, probe_resp_data,
1133 size, ram_offset,
Johannes Berg8318d782008-01-24 19:38:38 +01001134 shm_size_offset, rate->bitrate);
Larry Finger75388ac2007-09-25 16:46:54 -07001135 kfree(probe_resp_data);
1136}
1137
Stefano Brivioa2971702008-02-08 06:31:25 +01001138/* Asynchronously update the packet templates in template RAM.
1139 * Locking: Requires wl->irq_lock to be locked. */
Johannes Berg9d139c82008-07-09 14:40:37 +02001140static void b43legacy_update_templates(struct b43legacy_wl *wl)
Larry Finger75388ac2007-09-25 16:46:54 -07001141{
Johannes Berg9d139c82008-07-09 14:40:37 +02001142 struct sk_buff *beacon;
Stefano Brivioa2971702008-02-08 06:31:25 +01001143 /* This is the top half of the ansynchronous beacon update. The bottom
1144 * half is the beacon IRQ. Beacon update must be asynchronous to avoid
1145 * sending an invalid beacon. This can happen for example, if the
1146 * firmware transmits a beacon while we are updating it. */
Larry Finger75388ac2007-09-25 16:46:54 -07001147
Johannes Berg9d139c82008-07-09 14:40:37 +02001148 /* We could modify the existing beacon and set the aid bit in the TIM
1149 * field, but that would probably require resizing and moving of data
1150 * within the beacon template. Simply request a new beacon and let
1151 * mac80211 do the hard work. */
1152 beacon = ieee80211_beacon_get(wl->hw, wl->vif);
1153 if (unlikely(!beacon))
1154 return;
1155
Stefano Brivioa2971702008-02-08 06:31:25 +01001156 if (wl->current_beacon)
1157 dev_kfree_skb_any(wl->current_beacon);
1158 wl->current_beacon = beacon;
1159 wl->beacon0_uploaded = 0;
1160 wl->beacon1_uploaded = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07001161}
1162
Larry Finger75388ac2007-09-25 16:46:54 -07001163static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
1164 u16 beacon_int)
1165{
1166 b43legacy_time_lock(dev);
1167 if (dev->dev->id.revision >= 3)
1168 b43legacy_write32(dev, 0x188, (beacon_int << 16));
1169 else {
1170 b43legacy_write16(dev, 0x606, (beacon_int >> 6));
1171 b43legacy_write16(dev, 0x610, beacon_int);
1172 }
1173 b43legacy_time_unlock(dev);
1174}
1175
1176static void handle_irq_beacon(struct b43legacy_wldev *dev)
1177{
Stefano Brivioa2971702008-02-08 06:31:25 +01001178 struct b43legacy_wl *wl = dev->wl;
1179 u32 cmd;
Larry Finger75388ac2007-09-25 16:46:54 -07001180
Johannes Berg05c914f2008-09-11 00:01:58 +02001181 if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
Larry Finger75388ac2007-09-25 16:46:54 -07001182 return;
1183
Stefano Brivioa2971702008-02-08 06:31:25 +01001184 /* This is the bottom half of the asynchronous beacon update. */
Larry Finger75388ac2007-09-25 16:46:54 -07001185
Stefano Brivioa2971702008-02-08 06:31:25 +01001186 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1187 if (!(cmd & B43legacy_MACCMD_BEACON0_VALID)) {
1188 if (!wl->beacon0_uploaded) {
1189 b43legacy_write_beacon_template(dev, 0x68,
1190 B43legacy_SHM_SH_BTL0,
1191 B43legacy_CCK_RATE_1MB);
1192 b43legacy_write_probe_resp_template(dev, 0x268,
1193 B43legacy_SHM_SH_PRTLEN,
1194 &__b43legacy_ratetable[3]);
1195 wl->beacon0_uploaded = 1;
1196 }
1197 cmd |= B43legacy_MACCMD_BEACON0_VALID;
Larry Finger75388ac2007-09-25 16:46:54 -07001198 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001199 if (!(cmd & B43legacy_MACCMD_BEACON1_VALID)) {
1200 if (!wl->beacon1_uploaded) {
1201 b43legacy_write_beacon_template(dev, 0x468,
1202 B43legacy_SHM_SH_BTL1,
1203 B43legacy_CCK_RATE_1MB);
1204 wl->beacon1_uploaded = 1;
1205 }
1206 cmd |= B43legacy_MACCMD_BEACON1_VALID;
Larry Finger75388ac2007-09-25 16:46:54 -07001207 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001208 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
Larry Finger75388ac2007-09-25 16:46:54 -07001209}
1210
1211static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
1212{
1213}
1214
1215/* Interrupt handler bottom-half */
1216static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
1217{
1218 u32 reason;
1219 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1220 u32 merged_dma_reason = 0;
1221 int i;
Larry Finger75388ac2007-09-25 16:46:54 -07001222 unsigned long flags;
1223
1224 spin_lock_irqsave(&dev->wl->irq_lock, flags);
1225
1226 B43legacy_WARN_ON(b43legacy_status(dev) <
1227 B43legacy_STAT_INITIALIZED);
1228
1229 reason = dev->irq_reason;
1230 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1231 dma_reason[i] = dev->dma_reason[i];
1232 merged_dma_reason |= dma_reason[i];
1233 }
1234
1235 if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
1236 b43legacyerr(dev->wl, "MAC transmission error\n");
1237
Stefano Brivioa293ee92007-11-24 23:35:25 +01001238 if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
Larry Finger75388ac2007-09-25 16:46:54 -07001239 b43legacyerr(dev->wl, "PHY transmission error\n");
Stefano Brivioa293ee92007-11-24 23:35:25 +01001240 rmb();
1241 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1242 b43legacyerr(dev->wl, "Too many PHY TX errors, "
1243 "restarting the controller\n");
1244 b43legacy_controller_restart(dev, "PHY TX errors");
1245 }
1246 }
Larry Finger75388ac2007-09-25 16:46:54 -07001247
1248 if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
1249 B43legacy_DMAIRQ_NONFATALMASK))) {
1250 if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
1251 b43legacyerr(dev->wl, "Fatal DMA error: "
1252 "0x%08X, 0x%08X, 0x%08X, "
1253 "0x%08X, 0x%08X, 0x%08X\n",
1254 dma_reason[0], dma_reason[1],
1255 dma_reason[2], dma_reason[3],
1256 dma_reason[4], dma_reason[5]);
1257 b43legacy_controller_restart(dev, "DMA error");
1258 mmiowb();
1259 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1260 return;
1261 }
1262 if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
1263 b43legacyerr(dev->wl, "DMA error: "
1264 "0x%08X, 0x%08X, 0x%08X, "
1265 "0x%08X, 0x%08X, 0x%08X\n",
1266 dma_reason[0], dma_reason[1],
1267 dma_reason[2], dma_reason[3],
1268 dma_reason[4], dma_reason[5]);
1269 }
1270
1271 if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
1272 handle_irq_ucode_debug(dev);
1273 if (reason & B43legacy_IRQ_TBTT_INDI)
1274 handle_irq_tbtt_indication(dev);
1275 if (reason & B43legacy_IRQ_ATIM_END)
1276 handle_irq_atim_end(dev);
1277 if (reason & B43legacy_IRQ_BEACON)
1278 handle_irq_beacon(dev);
1279 if (reason & B43legacy_IRQ_PMQ)
1280 handle_irq_pmq(dev);
1281 if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
1282 ;/*TODO*/
1283 if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
1284 handle_irq_noise(dev);
1285
1286 /* Check the DMA reason registers for received data. */
1287 if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
1288 if (b43legacy_using_pio(dev))
1289 b43legacy_pio_rx(dev->pio.queue0);
1290 else
1291 b43legacy_dma_rx(dev->dma.rx_ring0);
Larry Finger75388ac2007-09-25 16:46:54 -07001292 }
1293 B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
1294 B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
1295 if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
1296 if (b43legacy_using_pio(dev))
1297 b43legacy_pio_rx(dev->pio.queue3);
1298 else
1299 b43legacy_dma_rx(dev->dma.rx_ring3);
Larry Finger75388ac2007-09-25 16:46:54 -07001300 }
1301 B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
1302 B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
1303
Larry Fingerba48f7b2007-10-12 23:04:51 -05001304 if (reason & B43legacy_IRQ_TX_OK)
Larry Finger75388ac2007-09-25 16:46:54 -07001305 handle_irq_transmit_status(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07001306
Larry Finger75388ac2007-09-25 16:46:54 -07001307 b43legacy_interrupt_enable(dev, dev->irq_savedstate);
1308 mmiowb();
1309 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1310}
1311
1312static void pio_irq_workaround(struct b43legacy_wldev *dev,
1313 u16 base, int queueidx)
1314{
1315 u16 rxctl;
1316
1317 rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
1318 if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
1319 dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
1320 else
1321 dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
1322}
1323
1324static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
1325{
1326 if (b43legacy_using_pio(dev) &&
1327 (dev->dev->id.revision < 3) &&
1328 (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
1329 /* Apply a PIO specific workaround to the dma_reasons */
1330 pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
1331 pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
1332 pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
1333 pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
1334 }
1335
1336 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
1337
1338 b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
1339 dev->dma_reason[0]);
1340 b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
1341 dev->dma_reason[1]);
1342 b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
1343 dev->dma_reason[2]);
1344 b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
1345 dev->dma_reason[3]);
1346 b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
1347 dev->dma_reason[4]);
1348 b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
1349 dev->dma_reason[5]);
1350}
1351
1352/* Interrupt handler top-half */
1353static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
1354{
1355 irqreturn_t ret = IRQ_NONE;
1356 struct b43legacy_wldev *dev = dev_id;
1357 u32 reason;
1358
1359 if (!dev)
1360 return IRQ_NONE;
1361
1362 spin_lock(&dev->wl->irq_lock);
1363
1364 if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
1365 goto out;
1366 reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1367 if (reason == 0xffffffff) /* shared IRQ */
1368 goto out;
1369 ret = IRQ_HANDLED;
1370 reason &= b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
1371 if (!reason)
1372 goto out;
1373
1374 dev->dma_reason[0] = b43legacy_read32(dev,
1375 B43legacy_MMIO_DMA0_REASON)
1376 & 0x0001DC00;
1377 dev->dma_reason[1] = b43legacy_read32(dev,
1378 B43legacy_MMIO_DMA1_REASON)
1379 & 0x0000DC00;
1380 dev->dma_reason[2] = b43legacy_read32(dev,
1381 B43legacy_MMIO_DMA2_REASON)
1382 & 0x0000DC00;
1383 dev->dma_reason[3] = b43legacy_read32(dev,
1384 B43legacy_MMIO_DMA3_REASON)
1385 & 0x0001DC00;
1386 dev->dma_reason[4] = b43legacy_read32(dev,
1387 B43legacy_MMIO_DMA4_REASON)
1388 & 0x0000DC00;
1389 dev->dma_reason[5] = b43legacy_read32(dev,
1390 B43legacy_MMIO_DMA5_REASON)
1391 & 0x0000DC00;
1392
1393 b43legacy_interrupt_ack(dev, reason);
1394 /* disable all IRQs. They are enabled again in the bottom half. */
1395 dev->irq_savedstate = b43legacy_interrupt_disable(dev,
1396 B43legacy_IRQ_ALL);
1397 /* save the reason code and call our bottom half. */
1398 dev->irq_reason = reason;
1399 tasklet_schedule(&dev->isr_tasklet);
1400out:
1401 mmiowb();
1402 spin_unlock(&dev->wl->irq_lock);
1403
1404 return ret;
1405}
1406
1407static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
1408{
1409 release_firmware(dev->fw.ucode);
1410 dev->fw.ucode = NULL;
1411 release_firmware(dev->fw.pcm);
1412 dev->fw.pcm = NULL;
1413 release_firmware(dev->fw.initvals);
1414 dev->fw.initvals = NULL;
1415 release_firmware(dev->fw.initvals_band);
1416 dev->fw.initvals_band = NULL;
1417}
1418
1419static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
1420{
1421 b43legacyerr(wl, "You must go to http://linuxwireless.org/en/users/"
Stefano Brivio354807e2007-11-19 20:21:31 +01001422 "Drivers/b43#devicefirmware "
Larry Finger75388ac2007-09-25 16:46:54 -07001423 "and download the correct firmware (version 3).\n");
1424}
1425
1426static int do_request_fw(struct b43legacy_wldev *dev,
1427 const char *name,
1428 const struct firmware **fw)
1429{
1430 char path[sizeof(modparam_fwpostfix) + 32];
1431 struct b43legacy_fw_header *hdr;
1432 u32 size;
1433 int err;
1434
1435 if (!name)
1436 return 0;
1437
1438 snprintf(path, ARRAY_SIZE(path),
1439 "b43legacy%s/%s.fw",
1440 modparam_fwpostfix, name);
1441 err = request_firmware(fw, path, dev->dev->dev);
1442 if (err) {
1443 b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
1444 "or load failed.\n", path);
1445 return err;
1446 }
1447 if ((*fw)->size < sizeof(struct b43legacy_fw_header))
1448 goto err_format;
1449 hdr = (struct b43legacy_fw_header *)((*fw)->data);
1450 switch (hdr->type) {
1451 case B43legacy_FW_TYPE_UCODE:
1452 case B43legacy_FW_TYPE_PCM:
1453 size = be32_to_cpu(hdr->size);
1454 if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
1455 goto err_format;
1456 /* fallthrough */
1457 case B43legacy_FW_TYPE_IV:
1458 if (hdr->ver != 1)
1459 goto err_format;
1460 break;
1461 default:
1462 goto err_format;
1463 }
1464
1465 return err;
1466
1467err_format:
1468 b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
1469 return -EPROTO;
1470}
1471
1472static int b43legacy_request_firmware(struct b43legacy_wldev *dev)
1473{
1474 struct b43legacy_firmware *fw = &dev->fw;
1475 const u8 rev = dev->dev->id.revision;
1476 const char *filename;
1477 u32 tmshigh;
1478 int err;
1479
1480 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
1481 if (!fw->ucode) {
1482 if (rev == 2)
1483 filename = "ucode2";
1484 else if (rev == 4)
1485 filename = "ucode4";
1486 else
1487 filename = "ucode5";
1488 err = do_request_fw(dev, filename, &fw->ucode);
1489 if (err)
1490 goto err_load;
1491 }
1492 if (!fw->pcm) {
1493 if (rev < 5)
1494 filename = "pcm4";
1495 else
1496 filename = "pcm5";
1497 err = do_request_fw(dev, filename, &fw->pcm);
1498 if (err)
1499 goto err_load;
1500 }
1501 if (!fw->initvals) {
1502 switch (dev->phy.type) {
Stefano Brivio385f8482008-04-06 17:10:53 +02001503 case B43legacy_PHYTYPE_B:
Larry Finger75388ac2007-09-25 16:46:54 -07001504 case B43legacy_PHYTYPE_G:
1505 if ((rev >= 5) && (rev <= 10))
1506 filename = "b0g0initvals5";
1507 else if (rev == 2 || rev == 4)
1508 filename = "b0g0initvals2";
1509 else
1510 goto err_no_initvals;
1511 break;
1512 default:
1513 goto err_no_initvals;
1514 }
1515 err = do_request_fw(dev, filename, &fw->initvals);
1516 if (err)
1517 goto err_load;
1518 }
1519 if (!fw->initvals_band) {
1520 switch (dev->phy.type) {
Stefano Brivio385f8482008-04-06 17:10:53 +02001521 case B43legacy_PHYTYPE_B:
Larry Finger75388ac2007-09-25 16:46:54 -07001522 case B43legacy_PHYTYPE_G:
1523 if ((rev >= 5) && (rev <= 10))
1524 filename = "b0g0bsinitvals5";
1525 else if (rev >= 11)
1526 filename = NULL;
1527 else if (rev == 2 || rev == 4)
1528 filename = NULL;
1529 else
1530 goto err_no_initvals;
1531 break;
1532 default:
1533 goto err_no_initvals;
1534 }
1535 err = do_request_fw(dev, filename, &fw->initvals_band);
1536 if (err)
1537 goto err_load;
1538 }
1539
1540 return 0;
1541
1542err_load:
1543 b43legacy_print_fw_helptext(dev->wl);
1544 goto error;
1545
1546err_no_initvals:
1547 err = -ENODEV;
1548 b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
1549 "core rev %u\n", dev->phy.type, rev);
1550 goto error;
1551
1552error:
1553 b43legacy_release_firmware(dev);
1554 return err;
1555}
1556
1557static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
1558{
1559 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1560 const __be32 *data;
1561 unsigned int i;
1562 unsigned int len;
1563 u16 fwrev;
1564 u16 fwpatch;
1565 u16 fwdate;
1566 u16 fwtime;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001567 u32 tmp, macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07001568 int err = 0;
1569
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001570 /* Jump the microcode PSM to offset 0 */
1571 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1572 B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
1573 macctl |= B43legacy_MACCTL_PSM_JMP0;
1574 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1575 /* Zero out all microcode PSM registers and shared memory. */
1576 for (i = 0; i < 64; i++)
1577 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
1578 for (i = 0; i < 4096; i += 2)
1579 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
1580
Larry Finger75388ac2007-09-25 16:46:54 -07001581 /* Upload Microcode. */
1582 data = (__be32 *) (dev->fw.ucode->data + hdr_len);
1583 len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
1584 b43legacy_shm_control_word(dev,
1585 B43legacy_SHM_UCODE |
1586 B43legacy_SHM_AUTOINC_W,
1587 0x0000);
1588 for (i = 0; i < len; i++) {
1589 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1590 be32_to_cpu(data[i]));
1591 udelay(10);
1592 }
1593
1594 if (dev->fw.pcm) {
1595 /* Upload PCM data. */
1596 data = (__be32 *) (dev->fw.pcm->data + hdr_len);
1597 len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
1598 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
1599 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
1600 /* No need for autoinc bit in SHM_HW */
1601 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
1602 for (i = 0; i < len; i++) {
1603 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1604 be32_to_cpu(data[i]));
1605 udelay(10);
1606 }
1607 }
1608
1609 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1610 B43legacy_IRQ_ALL);
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001611
1612 /* Start the microcode PSM */
1613 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1614 macctl &= ~B43legacy_MACCTL_PSM_JMP0;
1615 macctl |= B43legacy_MACCTL_PSM_RUN;
1616 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
Larry Finger75388ac2007-09-25 16:46:54 -07001617
1618 /* Wait for the microcode to load and respond */
1619 i = 0;
1620 while (1) {
1621 tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1622 if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
1623 break;
1624 i++;
1625 if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
1626 b43legacyerr(dev->wl, "Microcode not responding\n");
1627 b43legacy_print_fw_helptext(dev->wl);
1628 err = -ENODEV;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001629 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001630 }
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001631 msleep_interruptible(50);
1632 if (signal_pending(current)) {
1633 err = -EINTR;
1634 goto error;
1635 }
Larry Finger75388ac2007-09-25 16:46:54 -07001636 }
1637 /* dummy read follows */
1638 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1639
1640 /* Get and check the revisions. */
1641 fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1642 B43legacy_SHM_SH_UCODEREV);
1643 fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1644 B43legacy_SHM_SH_UCODEPATCH);
1645 fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1646 B43legacy_SHM_SH_UCODEDATE);
1647 fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1648 B43legacy_SHM_SH_UCODETIME);
1649
1650 if (fwrev > 0x128) {
1651 b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
1652 " Only firmware from binary drivers version 3.x"
1653 " is supported. You must change your firmware"
1654 " files.\n");
1655 b43legacy_print_fw_helptext(dev->wl);
Larry Finger75388ac2007-09-25 16:46:54 -07001656 err = -EOPNOTSUPP;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001657 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001658 }
Stefano Briviocfbc35b2008-02-12 03:09:10 +01001659 b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
1660 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
1661 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
1662 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
1663 fwtime & 0x1F);
Larry Finger75388ac2007-09-25 16:46:54 -07001664
1665 dev->fw.rev = fwrev;
1666 dev->fw.patch = fwpatch;
1667
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001668 return 0;
1669
1670error:
1671 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1672 macctl &= ~B43legacy_MACCTL_PSM_RUN;
1673 macctl |= B43legacy_MACCTL_PSM_JMP0;
1674 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1675
Larry Finger75388ac2007-09-25 16:46:54 -07001676 return err;
1677}
1678
1679static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
1680 const struct b43legacy_iv *ivals,
1681 size_t count,
1682 size_t array_size)
1683{
1684 const struct b43legacy_iv *iv;
1685 u16 offset;
1686 size_t i;
1687 bool bit32;
1688
1689 BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
1690 iv = ivals;
1691 for (i = 0; i < count; i++) {
1692 if (array_size < sizeof(iv->offset_size))
1693 goto err_format;
1694 array_size -= sizeof(iv->offset_size);
1695 offset = be16_to_cpu(iv->offset_size);
1696 bit32 = !!(offset & B43legacy_IV_32BIT);
1697 offset &= B43legacy_IV_OFFSET_MASK;
1698 if (offset >= 0x1000)
1699 goto err_format;
1700 if (bit32) {
1701 u32 value;
1702
1703 if (array_size < sizeof(iv->data.d32))
1704 goto err_format;
1705 array_size -= sizeof(iv->data.d32);
1706
Harvey Harrison533dd1b2008-04-29 01:03:36 -07001707 value = get_unaligned_be32(&iv->data.d32);
Larry Finger75388ac2007-09-25 16:46:54 -07001708 b43legacy_write32(dev, offset, value);
1709
1710 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1711 sizeof(__be16) +
1712 sizeof(__be32));
1713 } else {
1714 u16 value;
1715
1716 if (array_size < sizeof(iv->data.d16))
1717 goto err_format;
1718 array_size -= sizeof(iv->data.d16);
1719
1720 value = be16_to_cpu(iv->data.d16);
1721 b43legacy_write16(dev, offset, value);
1722
1723 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1724 sizeof(__be16) +
1725 sizeof(__be16));
1726 }
1727 }
1728 if (array_size)
1729 goto err_format;
1730
1731 return 0;
1732
1733err_format:
1734 b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
1735 b43legacy_print_fw_helptext(dev->wl);
1736
1737 return -EPROTO;
1738}
1739
1740static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
1741{
1742 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1743 const struct b43legacy_fw_header *hdr;
1744 struct b43legacy_firmware *fw = &dev->fw;
1745 const struct b43legacy_iv *ivals;
1746 size_t count;
1747 int err;
1748
1749 hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
1750 ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
1751 count = be32_to_cpu(hdr->size);
1752 err = b43legacy_write_initvals(dev, ivals, count,
1753 fw->initvals->size - hdr_len);
1754 if (err)
1755 goto out;
1756 if (fw->initvals_band) {
1757 hdr = (const struct b43legacy_fw_header *)
1758 (fw->initvals_band->data);
1759 ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
1760 + hdr_len);
1761 count = be32_to_cpu(hdr->size);
1762 err = b43legacy_write_initvals(dev, ivals, count,
1763 fw->initvals_band->size - hdr_len);
1764 if (err)
1765 goto out;
1766 }
1767out:
1768
1769 return err;
1770}
1771
1772/* Initialize the GPIOs
1773 * http://bcm-specs.sipsolutions.net/GPIO
1774 */
1775static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
1776{
1777 struct ssb_bus *bus = dev->dev->bus;
1778 struct ssb_device *gpiodev, *pcidev = NULL;
1779 u32 mask;
1780 u32 set;
1781
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001782 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001783 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001784 B43legacy_MMIO_MACCTL)
Larry Finger75388ac2007-09-25 16:46:54 -07001785 & 0xFFFF3FFF);
1786
Larry Finger75388ac2007-09-25 16:46:54 -07001787 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1788 b43legacy_read16(dev,
1789 B43legacy_MMIO_GPIO_MASK)
1790 | 0x000F);
1791
1792 mask = 0x0000001F;
1793 set = 0x0000000F;
1794 if (dev->dev->bus->chip_id == 0x4301) {
1795 mask |= 0x0060;
1796 set |= 0x0060;
1797 }
Larry Finger7797aa32007-11-09 16:57:34 -06001798 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
Larry Finger75388ac2007-09-25 16:46:54 -07001799 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1800 b43legacy_read16(dev,
1801 B43legacy_MMIO_GPIO_MASK)
1802 | 0x0200);
1803 mask |= 0x0200;
1804 set |= 0x0200;
1805 }
1806 if (dev->dev->id.revision >= 2)
1807 mask |= 0x0010; /* FIXME: This is redundant. */
1808
1809#ifdef CONFIG_SSB_DRIVER_PCICORE
1810 pcidev = bus->pcicore.dev;
1811#endif
1812 gpiodev = bus->chipco.dev ? : pcidev;
1813 if (!gpiodev)
1814 return 0;
1815 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
1816 (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
1817 & mask) | set);
1818
1819 return 0;
1820}
1821
1822/* Turn off all GPIO stuff. Call this on module unload, for example. */
1823static void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
1824{
1825 struct ssb_bus *bus = dev->dev->bus;
1826 struct ssb_device *gpiodev, *pcidev = NULL;
1827
1828#ifdef CONFIG_SSB_DRIVER_PCICORE
1829 pcidev = bus->pcicore.dev;
1830#endif
1831 gpiodev = bus->chipco.dev ? : pcidev;
1832 if (!gpiodev)
1833 return;
1834 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
1835}
1836
1837/* http://bcm-specs.sipsolutions.net/EnableMac */
1838void b43legacy_mac_enable(struct b43legacy_wldev *dev)
1839{
1840 dev->mac_suspended--;
1841 B43legacy_WARN_ON(dev->mac_suspended < 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001842 B43legacy_WARN_ON(irqs_disabled());
Larry Finger75388ac2007-09-25 16:46:54 -07001843 if (dev->mac_suspended == 0) {
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001844 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001845 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001846 B43legacy_MMIO_MACCTL)
1847 | B43legacy_MACCTL_ENABLED);
Larry Finger75388ac2007-09-25 16:46:54 -07001848 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1849 B43legacy_IRQ_MAC_SUSPENDED);
1850 /* the next two are dummy reads */
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001851 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
Larry Finger75388ac2007-09-25 16:46:54 -07001852 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1853 b43legacy_power_saving_ctl_bits(dev, -1, -1);
Larry Fingerf34eb692007-10-11 00:05:57 -05001854
1855 /* Re-enable IRQs. */
1856 spin_lock_irq(&dev->wl->irq_lock);
1857 b43legacy_interrupt_enable(dev, dev->irq_savedstate);
1858 spin_unlock_irq(&dev->wl->irq_lock);
Larry Finger75388ac2007-09-25 16:46:54 -07001859 }
1860}
1861
1862/* http://bcm-specs.sipsolutions.net/SuspendMAC */
1863void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
1864{
1865 int i;
1866 u32 tmp;
1867
Larry Fingerf34eb692007-10-11 00:05:57 -05001868 might_sleep();
1869 B43legacy_WARN_ON(irqs_disabled());
Larry Finger75388ac2007-09-25 16:46:54 -07001870 B43legacy_WARN_ON(dev->mac_suspended < 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001871
Larry Finger75388ac2007-09-25 16:46:54 -07001872 if (dev->mac_suspended == 0) {
Larry Fingerf34eb692007-10-11 00:05:57 -05001873 /* Mask IRQs before suspending MAC. Otherwise
1874 * the MAC stays busy and won't suspend. */
1875 spin_lock_irq(&dev->wl->irq_lock);
1876 tmp = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
1877 spin_unlock_irq(&dev->wl->irq_lock);
1878 b43legacy_synchronize_irq(dev);
1879 dev->irq_savedstate = tmp;
1880
Larry Finger75388ac2007-09-25 16:46:54 -07001881 b43legacy_power_saving_ctl_bits(dev, -1, 1);
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001882 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001883 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001884 B43legacy_MMIO_MACCTL)
1885 & ~B43legacy_MACCTL_ENABLED);
Larry Finger75388ac2007-09-25 16:46:54 -07001886 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
Larry Fingerf34eb692007-10-11 00:05:57 -05001887 for (i = 40; i; i--) {
Larry Finger75388ac2007-09-25 16:46:54 -07001888 tmp = b43legacy_read32(dev,
1889 B43legacy_MMIO_GEN_IRQ_REASON);
1890 if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
1891 goto out;
Larry Fingerf34eb692007-10-11 00:05:57 -05001892 msleep(1);
Larry Finger75388ac2007-09-25 16:46:54 -07001893 }
1894 b43legacyerr(dev->wl, "MAC suspend failed\n");
1895 }
1896out:
1897 dev->mac_suspended++;
1898}
1899
1900static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
1901{
1902 struct b43legacy_wl *wl = dev->wl;
1903 u32 ctl;
1904 u16 cfp_pretbtt;
1905
1906 ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1907 /* Reset status to STA infrastructure mode. */
1908 ctl &= ~B43legacy_MACCTL_AP;
1909 ctl &= ~B43legacy_MACCTL_KEEP_CTL;
1910 ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
1911 ctl &= ~B43legacy_MACCTL_KEEP_BAD;
1912 ctl &= ~B43legacy_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04001913 ctl &= ~B43legacy_MACCTL_BEACPROMISC;
Larry Finger75388ac2007-09-25 16:46:54 -07001914 ctl |= B43legacy_MACCTL_INFRA;
1915
Johannes Berg05c914f2008-09-11 00:01:58 +02001916 if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
Johannes Berg4150c572007-09-17 01:29:23 -04001917 ctl |= B43legacy_MACCTL_AP;
Johannes Berg05c914f2008-09-11 00:01:58 +02001918 else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
Johannes Berg4150c572007-09-17 01:29:23 -04001919 ctl &= ~B43legacy_MACCTL_INFRA;
1920
1921 if (wl->filter_flags & FIF_CONTROL)
Larry Finger75388ac2007-09-25 16:46:54 -07001922 ctl |= B43legacy_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04001923 if (wl->filter_flags & FIF_FCSFAIL)
1924 ctl |= B43legacy_MACCTL_KEEP_BAD;
1925 if (wl->filter_flags & FIF_PLCPFAIL)
1926 ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
1927 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Larry Finger75388ac2007-09-25 16:46:54 -07001928 ctl |= B43legacy_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04001929 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
1930 ctl |= B43legacy_MACCTL_BEACPROMISC;
1931
Larry Finger75388ac2007-09-25 16:46:54 -07001932 /* Workaround: On old hardware the HW-MAC-address-filter
1933 * doesn't work properly, so always run promisc in filter
1934 * it in software. */
1935 if (dev->dev->id.revision <= 4)
1936 ctl |= B43legacy_MACCTL_PROMISC;
1937
1938 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
1939
1940 cfp_pretbtt = 2;
1941 if ((ctl & B43legacy_MACCTL_INFRA) &&
1942 !(ctl & B43legacy_MACCTL_AP)) {
1943 if (dev->dev->bus->chip_id == 0x4306 &&
1944 dev->dev->bus->chip_rev == 3)
1945 cfp_pretbtt = 100;
1946 else
1947 cfp_pretbtt = 50;
1948 }
1949 b43legacy_write16(dev, 0x612, cfp_pretbtt);
1950}
1951
1952static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
1953 u16 rate,
1954 int is_ofdm)
1955{
1956 u16 offset;
1957
1958 if (is_ofdm) {
1959 offset = 0x480;
1960 offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
1961 } else {
1962 offset = 0x4C0;
1963 offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
1964 }
1965 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
1966 b43legacy_shm_read16(dev,
1967 B43legacy_SHM_SHARED, offset));
1968}
1969
1970static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
1971{
1972 switch (dev->phy.type) {
1973 case B43legacy_PHYTYPE_G:
1974 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
1975 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
1976 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
1977 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
1978 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
1979 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
1980 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
1981 /* fallthrough */
1982 case B43legacy_PHYTYPE_B:
1983 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
1984 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
1985 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
1986 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
1987 break;
1988 default:
1989 B43legacy_BUG_ON(1);
1990 }
1991}
1992
1993/* Set the TX-Antenna for management frames sent by firmware. */
1994static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
1995 int antenna)
1996{
1997 u16 ant = 0;
1998 u16 tmp;
1999
2000 switch (antenna) {
2001 case B43legacy_ANTENNA0:
2002 ant |= B43legacy_TX4_PHY_ANT0;
2003 break;
2004 case B43legacy_ANTENNA1:
2005 ant |= B43legacy_TX4_PHY_ANT1;
2006 break;
2007 case B43legacy_ANTENNA_AUTO:
2008 ant |= B43legacy_TX4_PHY_ANTLAST;
2009 break;
2010 default:
2011 B43legacy_BUG_ON(1);
2012 }
2013
2014 /* FIXME We also need to set the other flags of the PHY control
2015 * field somewhere. */
2016
2017 /* For Beacons */
2018 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2019 B43legacy_SHM_SH_BEACPHYCTL);
2020 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2021 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2022 B43legacy_SHM_SH_BEACPHYCTL, tmp);
2023 /* For ACK/CTS */
2024 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2025 B43legacy_SHM_SH_ACKCTSPHYCTL);
2026 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2027 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2028 B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
2029 /* For Probe Resposes */
2030 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2031 B43legacy_SHM_SH_PRPHYCTL);
2032 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2033 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2034 B43legacy_SHM_SH_PRPHYCTL, tmp);
2035}
2036
2037/* This is the opposite of b43legacy_chip_init() */
2038static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
2039{
Larry Finger93bb7f32007-10-10 22:44:22 -05002040 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07002041 b43legacy_gpio_cleanup(dev);
2042 /* firmware is released later */
2043}
2044
2045/* Initialize the chip
2046 * http://bcm-specs.sipsolutions.net/ChipInit
2047 */
2048static int b43legacy_chip_init(struct b43legacy_wldev *dev)
2049{
2050 struct b43legacy_phy *phy = &dev->phy;
2051 int err;
2052 int tmp;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002053 u32 value32, macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07002054 u16 value16;
2055
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002056 /* Initialize the MAC control */
2057 macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
2058 if (dev->phy.gmode)
2059 macctl |= B43legacy_MACCTL_GMODE;
2060 macctl |= B43legacy_MACCTL_INFRA;
2061 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
Larry Finger75388ac2007-09-25 16:46:54 -07002062
2063 err = b43legacy_request_firmware(dev);
2064 if (err)
2065 goto out;
2066 err = b43legacy_upload_microcode(dev);
2067 if (err)
2068 goto out; /* firmware is released later */
2069
2070 err = b43legacy_gpio_init(dev);
2071 if (err)
2072 goto out; /* firmware is released later */
Larry Fingerba48f7b2007-10-12 23:04:51 -05002073
Larry Finger75388ac2007-09-25 16:46:54 -07002074 err = b43legacy_upload_initvals(dev);
2075 if (err)
Larry Finger4ad36d72007-12-16 19:21:06 +01002076 goto err_gpio_clean;
Larry Finger75388ac2007-09-25 16:46:54 -07002077 b43legacy_radio_turn_on(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002078
2079 b43legacy_write16(dev, 0x03E6, 0x0000);
2080 err = b43legacy_phy_init(dev);
2081 if (err)
2082 goto err_radio_off;
2083
2084 /* Select initial Interference Mitigation. */
2085 tmp = phy->interfmode;
2086 phy->interfmode = B43legacy_INTERFMODE_NONE;
2087 b43legacy_radio_set_interference_mitigation(dev, tmp);
2088
2089 b43legacy_phy_set_antenna_diversity(dev);
2090 b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
2091
2092 if (phy->type == B43legacy_PHYTYPE_B) {
2093 value16 = b43legacy_read16(dev, 0x005E);
2094 value16 |= 0x0004;
2095 b43legacy_write16(dev, 0x005E, value16);
2096 }
2097 b43legacy_write32(dev, 0x0100, 0x01000000);
2098 if (dev->dev->id.revision < 5)
2099 b43legacy_write32(dev, 0x010C, 0x01000000);
2100
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002101 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2102 value32 &= ~B43legacy_MACCTL_INFRA;
2103 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
2104 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2105 value32 |= B43legacy_MACCTL_INFRA;
2106 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
Larry Finger75388ac2007-09-25 16:46:54 -07002107
Larry Finger75388ac2007-09-25 16:46:54 -07002108 if (b43legacy_using_pio(dev)) {
2109 b43legacy_write32(dev, 0x0210, 0x00000100);
2110 b43legacy_write32(dev, 0x0230, 0x00000100);
2111 b43legacy_write32(dev, 0x0250, 0x00000100);
2112 b43legacy_write32(dev, 0x0270, 0x00000100);
2113 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
2114 0x0000);
2115 }
2116
2117 /* Probe Response Timeout value */
2118 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2119 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
2120
2121 /* Initially set the wireless operation mode. */
2122 b43legacy_adjust_opmode(dev);
2123
2124 if (dev->dev->id.revision < 3) {
2125 b43legacy_write16(dev, 0x060E, 0x0000);
2126 b43legacy_write16(dev, 0x0610, 0x8000);
2127 b43legacy_write16(dev, 0x0604, 0x0000);
2128 b43legacy_write16(dev, 0x0606, 0x0200);
2129 } else {
2130 b43legacy_write32(dev, 0x0188, 0x80000000);
2131 b43legacy_write32(dev, 0x018C, 0x02000000);
2132 }
2133 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
2134 b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2135 b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2136 b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2137 b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2138 b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2139 b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2140
2141 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
2142 value32 |= 0x00100000;
2143 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2144
2145 b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
2146 dev->dev->bus->chipco.fast_pwrup_delay);
2147
Stefano Brivioa293ee92007-11-24 23:35:25 +01002148 /* PHY TX errors counter. */
2149 atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2150
Larry Finger75388ac2007-09-25 16:46:54 -07002151 B43legacy_WARN_ON(err != 0);
2152 b43legacydbg(dev->wl, "Chip initialized\n");
2153out:
2154 return err;
2155
2156err_radio_off:
Larry Finger93bb7f32007-10-10 22:44:22 -05002157 b43legacy_radio_turn_off(dev, 1);
Larry Finger4ad36d72007-12-16 19:21:06 +01002158err_gpio_clean:
Larry Finger75388ac2007-09-25 16:46:54 -07002159 b43legacy_gpio_cleanup(dev);
2160 goto out;
2161}
2162
2163static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
2164{
2165 struct b43legacy_phy *phy = &dev->phy;
2166
2167 if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
2168 return;
2169
2170 b43legacy_mac_suspend(dev);
2171 b43legacy_phy_lo_g_measure(dev);
2172 b43legacy_mac_enable(dev);
2173}
2174
2175static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
2176{
2177 b43legacy_phy_lo_mark_all_unused(dev);
Larry Finger7797aa32007-11-09 16:57:34 -06002178 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
Larry Finger75388ac2007-09-25 16:46:54 -07002179 b43legacy_mac_suspend(dev);
2180 b43legacy_calc_nrssi_slope(dev);
2181 b43legacy_mac_enable(dev);
2182 }
2183}
2184
2185static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
2186{
2187 /* Update device statistics. */
2188 b43legacy_calculate_link_quality(dev);
2189}
2190
2191static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
2192{
2193 b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
Stefano Brivioa293ee92007-11-24 23:35:25 +01002194
2195 atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2196 wmb();
Larry Finger75388ac2007-09-25 16:46:54 -07002197}
2198
Larry Finger75388ac2007-09-25 16:46:54 -07002199static void do_periodic_work(struct b43legacy_wldev *dev)
2200{
2201 unsigned int state;
2202
2203 state = dev->periodic_state;
Larry Finger6be50832007-10-10 22:48:17 -05002204 if (state % 8 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002205 b43legacy_periodic_every120sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002206 if (state % 4 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002207 b43legacy_periodic_every60sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002208 if (state % 2 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002209 b43legacy_periodic_every30sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002210 b43legacy_periodic_every15sec(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002211}
2212
Larry Fingerf34eb692007-10-11 00:05:57 -05002213/* Periodic work locking policy:
2214 * The whole periodic work handler is protected by
2215 * wl->mutex. If another lock is needed somewhere in the
2216 * pwork callchain, it's aquired in-place, where it's needed.
Larry Finger75388ac2007-09-25 16:46:54 -07002217 */
Larry Finger75388ac2007-09-25 16:46:54 -07002218static void b43legacy_periodic_work_handler(struct work_struct *work)
2219{
Larry Fingerf34eb692007-10-11 00:05:57 -05002220 struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
2221 periodic_work.work);
2222 struct b43legacy_wl *wl = dev->wl;
Larry Finger75388ac2007-09-25 16:46:54 -07002223 unsigned long delay;
Larry Finger75388ac2007-09-25 16:46:54 -07002224
Larry Fingerf34eb692007-10-11 00:05:57 -05002225 mutex_lock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07002226
2227 if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
2228 goto out;
2229 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
2230 goto out_requeue;
2231
Larry Fingerf34eb692007-10-11 00:05:57 -05002232 do_periodic_work(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002233
Larry Finger75388ac2007-09-25 16:46:54 -07002234 dev->periodic_state++;
2235out_requeue:
2236 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
2237 delay = msecs_to_jiffies(50);
2238 else
Larry Finger6be50832007-10-10 22:48:17 -05002239 delay = round_jiffies_relative(HZ * 15);
Larry Fingerf34eb692007-10-11 00:05:57 -05002240 queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
Larry Finger75388ac2007-09-25 16:46:54 -07002241out:
Larry Fingerf34eb692007-10-11 00:05:57 -05002242 mutex_unlock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07002243}
2244
2245static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
2246{
2247 struct delayed_work *work = &dev->periodic_work;
2248
2249 dev->periodic_state = 0;
2250 INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
2251 queue_delayed_work(dev->wl->hw->workqueue, work, 0);
2252}
2253
2254/* Validate access to the chip (SHM) */
2255static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
2256{
2257 u32 value;
2258 u32 shm_backup;
2259
2260 shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
2261 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
2262 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2263 0xAA5555AA)
2264 goto error;
2265 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
2266 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2267 0x55AAAA55)
2268 goto error;
2269 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
2270
2271 value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2272 if ((value | B43legacy_MACCTL_GMODE) !=
2273 (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
2274 goto error;
2275
2276 value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
2277 if (value)
2278 goto error;
2279
2280 return 0;
2281error:
2282 b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
2283 return -ENODEV;
2284}
2285
2286static void b43legacy_security_init(struct b43legacy_wldev *dev)
2287{
2288 dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
2289 B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
2290 dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2291 0x0056);
2292 /* KTP is a word address, but we address SHM bytewise.
2293 * So multiply by two.
2294 */
2295 dev->ktp *= 2;
2296 if (dev->dev->id.revision >= 5)
2297 /* Number of RCMTA address slots */
2298 b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
2299 dev->max_nr_keys - 8);
2300}
2301
2302static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
2303{
2304 struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
2305 unsigned long flags;
2306
2307 /* Don't take wl->mutex here, as it could deadlock with
2308 * hwrng internal locking. It's not needed to take
2309 * wl->mutex here, anyway. */
2310
2311 spin_lock_irqsave(&wl->irq_lock, flags);
2312 *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
2313 spin_unlock_irqrestore(&wl->irq_lock, flags);
2314
2315 return (sizeof(u16));
2316}
2317
2318static void b43legacy_rng_exit(struct b43legacy_wl *wl)
2319{
2320 if (wl->rng_initialized)
2321 hwrng_unregister(&wl->rng);
2322}
2323
2324static int b43legacy_rng_init(struct b43legacy_wl *wl)
2325{
2326 int err;
2327
2328 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
2329 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
2330 wl->rng.name = wl->rng_name;
2331 wl->rng.data_read = b43legacy_rng_read;
2332 wl->rng.priv = (unsigned long)wl;
2333 wl->rng_initialized = 1;
2334 err = hwrng_register(&wl->rng);
2335 if (err) {
2336 wl->rng_initialized = 0;
2337 b43legacyerr(wl, "Failed to register the random "
2338 "number generator (%d)\n", err);
2339 }
2340
2341 return err;
2342}
2343
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002344static int b43legacy_op_tx(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02002345 struct sk_buff *skb)
Larry Finger75388ac2007-09-25 16:46:54 -07002346{
2347 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2348 struct b43legacy_wldev *dev = wl->current_dev;
2349 int err = -ENODEV;
2350 unsigned long flags;
2351
2352 if (unlikely(!dev))
2353 goto out;
2354 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
2355 goto out;
2356 /* DMA-TX is done without a global lock. */
2357 if (b43legacy_using_pio(dev)) {
2358 spin_lock_irqsave(&wl->irq_lock, flags);
Johannes Berge039fa42008-05-15 12:55:29 +02002359 err = b43legacy_pio_tx(dev, skb);
Larry Finger75388ac2007-09-25 16:46:54 -07002360 spin_unlock_irqrestore(&wl->irq_lock, flags);
2361 } else
Johannes Berge039fa42008-05-15 12:55:29 +02002362 err = b43legacy_dma_tx(dev, skb);
Larry Finger75388ac2007-09-25 16:46:54 -07002363out:
Michael Buesch664f2002008-06-15 15:27:49 +02002364 if (unlikely(err)) {
2365 /* Drop the packet. */
2366 dev_kfree_skb_any(skb);
2367 }
Larry Finger75388ac2007-09-25 16:46:54 -07002368 return NETDEV_TX_OK;
2369}
2370
Johannes Berge100bb62008-04-30 18:51:21 +02002371static int b43legacy_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002372 const struct ieee80211_tx_queue_params *params)
Larry Finger75388ac2007-09-25 16:46:54 -07002373{
2374 return 0;
2375}
2376
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002377static int b43legacy_op_get_tx_stats(struct ieee80211_hw *hw,
2378 struct ieee80211_tx_queue_stats *stats)
Larry Finger75388ac2007-09-25 16:46:54 -07002379{
2380 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2381 struct b43legacy_wldev *dev = wl->current_dev;
2382 unsigned long flags;
2383 int err = -ENODEV;
2384
2385 if (!dev)
2386 goto out;
2387 spin_lock_irqsave(&wl->irq_lock, flags);
2388 if (likely(b43legacy_status(dev) >= B43legacy_STAT_STARTED)) {
2389 if (b43legacy_using_pio(dev))
2390 b43legacy_pio_get_tx_stats(dev, stats);
2391 else
2392 b43legacy_dma_get_tx_stats(dev, stats);
2393 err = 0;
2394 }
2395 spin_unlock_irqrestore(&wl->irq_lock, flags);
2396out:
2397 return err;
2398}
2399
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002400static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
2401 struct ieee80211_low_level_stats *stats)
Larry Finger75388ac2007-09-25 16:46:54 -07002402{
2403 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2404 unsigned long flags;
2405
2406 spin_lock_irqsave(&wl->irq_lock, flags);
2407 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
2408 spin_unlock_irqrestore(&wl->irq_lock, flags);
2409
2410 return 0;
2411}
2412
2413static const char *phymode_to_string(unsigned int phymode)
2414{
2415 switch (phymode) {
2416 case B43legacy_PHYMODE_B:
2417 return "B";
2418 case B43legacy_PHYMODE_G:
2419 return "G";
2420 default:
2421 B43legacy_BUG_ON(1);
2422 }
2423 return "";
2424}
2425
2426static int find_wldev_for_phymode(struct b43legacy_wl *wl,
2427 unsigned int phymode,
2428 struct b43legacy_wldev **dev,
2429 bool *gmode)
2430{
2431 struct b43legacy_wldev *d;
2432
2433 list_for_each_entry(d, &wl->devlist, list) {
2434 if (d->phy.possible_phymodes & phymode) {
2435 /* Ok, this device supports the PHY-mode.
2436 * Set the gmode bit. */
2437 *gmode = 1;
2438 *dev = d;
2439
2440 return 0;
2441 }
2442 }
2443
2444 return -ESRCH;
2445}
2446
2447static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
2448{
2449 struct ssb_device *sdev = dev->dev;
2450 u32 tmslow;
2451
2452 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2453 tmslow &= ~B43legacy_TMSLOW_GMODE;
2454 tmslow |= B43legacy_TMSLOW_PHYRESET;
2455 tmslow |= SSB_TMSLOW_FGC;
2456 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2457 msleep(1);
2458
2459 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2460 tmslow &= ~SSB_TMSLOW_FGC;
2461 tmslow |= B43legacy_TMSLOW_PHYRESET;
2462 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2463 msleep(1);
2464}
2465
2466/* Expects wl->mutex locked */
2467static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
2468 unsigned int new_mode)
2469{
2470 struct b43legacy_wldev *up_dev;
2471 struct b43legacy_wldev *down_dev;
2472 int err;
2473 bool gmode = 0;
2474 int prev_status;
2475
2476 err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
2477 if (err) {
2478 b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
2479 phymode_to_string(new_mode));
2480 return err;
2481 }
2482 if ((up_dev == wl->current_dev) &&
2483 (!!wl->current_dev->phy.gmode == !!gmode))
2484 /* This device is already running. */
2485 return 0;
2486 b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
2487 phymode_to_string(new_mode));
2488 down_dev = wl->current_dev;
2489
2490 prev_status = b43legacy_status(down_dev);
2491 /* Shutdown the currently running core. */
2492 if (prev_status >= B43legacy_STAT_STARTED)
2493 b43legacy_wireless_core_stop(down_dev);
2494 if (prev_status >= B43legacy_STAT_INITIALIZED)
2495 b43legacy_wireless_core_exit(down_dev);
2496
2497 if (down_dev != up_dev)
2498 /* We switch to a different core, so we put PHY into
2499 * RESET on the old core. */
2500 b43legacy_put_phy_into_reset(down_dev);
2501
2502 /* Now start the new core. */
2503 up_dev->phy.gmode = gmode;
2504 if (prev_status >= B43legacy_STAT_INITIALIZED) {
2505 err = b43legacy_wireless_core_init(up_dev);
2506 if (err) {
2507 b43legacyerr(wl, "Fatal: Could not initialize device"
2508 " for newly selected %s-PHY mode\n",
2509 phymode_to_string(new_mode));
2510 goto init_failure;
2511 }
2512 }
2513 if (prev_status >= B43legacy_STAT_STARTED) {
2514 err = b43legacy_wireless_core_start(up_dev);
2515 if (err) {
2516 b43legacyerr(wl, "Fatal: Coult not start device for "
2517 "newly selected %s-PHY mode\n",
2518 phymode_to_string(new_mode));
2519 b43legacy_wireless_core_exit(up_dev);
2520 goto init_failure;
2521 }
2522 }
2523 B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
2524
2525 b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
2526
2527 wl->current_dev = up_dev;
2528
2529 return 0;
2530init_failure:
2531 /* Whoops, failed to init the new core. No core is operating now. */
2532 wl->current_dev = NULL;
2533 return err;
2534}
2535
Johannes Berg9124b072008-10-14 19:17:54 +02002536/* Write the short and long frame retry limit values. */
2537static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
2538 unsigned int short_retry,
2539 unsigned int long_retry)
2540{
2541 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
2542 * the chip-internal counter. */
2543 short_retry = min(short_retry, (unsigned int)0xF);
2544 long_retry = min(long_retry, (unsigned int)0xF);
2545
2546 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
2547 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
2548}
2549
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002550static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
Johannes Berge8975582008-10-09 12:18:51 +02002551 u32 changed)
Larry Finger75388ac2007-09-25 16:46:54 -07002552{
2553 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2554 struct b43legacy_wldev *dev;
2555 struct b43legacy_phy *phy;
Johannes Berge8975582008-10-09 12:18:51 +02002556 struct ieee80211_conf *conf = &hw->conf;
Larry Finger75388ac2007-09-25 16:46:54 -07002557 unsigned long flags;
2558 unsigned int new_phymode = 0xFFFF;
2559 int antenna_tx;
2560 int antenna_rx;
2561 int err = 0;
2562 u32 savedirqs;
2563
Johannes Berg0f4ac382008-10-09 12:18:04 +02002564 antenna_tx = B43legacy_ANTENNA_DEFAULT;
2565 antenna_rx = B43legacy_ANTENNA_DEFAULT;
Larry Finger75388ac2007-09-25 16:46:54 -07002566
2567 mutex_lock(&wl->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002568 dev = wl->current_dev;
2569 phy = &dev->phy;
Larry Finger75388ac2007-09-25 16:46:54 -07002570
Johannes Berg9124b072008-10-14 19:17:54 +02002571 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
2572 b43legacy_set_retry_limits(dev,
2573 conf->short_frame_max_tx_count,
2574 conf->long_frame_max_tx_count);
2575 changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
2576 if (!changed)
2577 goto out_unlock_mutex;
2578
Larry Finger75388ac2007-09-25 16:46:54 -07002579 /* Switch the PHY mode (if necessary). */
Johannes Berg8318d782008-01-24 19:38:38 +01002580 switch (conf->channel->band) {
2581 case IEEE80211_BAND_2GHZ:
2582 if (phy->type == B43legacy_PHYTYPE_B)
2583 new_phymode = B43legacy_PHYMODE_B;
2584 else
2585 new_phymode = B43legacy_PHYMODE_G;
Larry Finger75388ac2007-09-25 16:46:54 -07002586 break;
2587 default:
2588 B43legacy_WARN_ON(1);
2589 }
2590 err = b43legacy_switch_phymode(wl, new_phymode);
2591 if (err)
2592 goto out_unlock_mutex;
Larry Finger75388ac2007-09-25 16:46:54 -07002593
2594 /* Disable IRQs while reconfiguring the device.
2595 * This makes it possible to drop the spinlock throughout
2596 * the reconfiguration process. */
2597 spin_lock_irqsave(&wl->irq_lock, flags);
2598 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
2599 spin_unlock_irqrestore(&wl->irq_lock, flags);
2600 goto out_unlock_mutex;
2601 }
2602 savedirqs = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
2603 spin_unlock_irqrestore(&wl->irq_lock, flags);
2604 b43legacy_synchronize_irq(dev);
2605
2606 /* Switch to the requested channel.
2607 * The firmware takes care of races with the TX handler. */
Johannes Berg8318d782008-01-24 19:38:38 +01002608 if (conf->channel->hw_value != phy->channel)
2609 b43legacy_radio_selectchannel(dev, conf->channel->hw_value, 0);
Larry Finger75388ac2007-09-25 16:46:54 -07002610
2611 /* Enable/Disable ShortSlot timing. */
2612 if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME))
2613 != dev->short_slot) {
2614 B43legacy_WARN_ON(phy->type != B43legacy_PHYTYPE_G);
2615 if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
2616 b43legacy_short_slot_timing_enable(dev);
2617 else
2618 b43legacy_short_slot_timing_disable(dev);
2619 }
2620
Johannes Berg5be3bda2007-11-24 21:11:09 +01002621 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2622
Larry Finger75388ac2007-09-25 16:46:54 -07002623 /* Adjust the desired TX power level. */
2624 if (conf->power_level != 0) {
2625 if (conf->power_level != phy->power_level) {
2626 phy->power_level = conf->power_level;
2627 b43legacy_phy_xmitpower(dev);
2628 }
2629 }
2630
2631 /* Antennas for RX and management frame TX. */
2632 b43legacy_mgmtframe_txantenna(dev, antenna_tx);
2633
2634 /* Update templates for AP mode. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002635 if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
Larry Finger75388ac2007-09-25 16:46:54 -07002636 b43legacy_set_beacon_int(dev, conf->beacon_int);
2637
2638
Larry Finger42a91742007-09-20 21:11:02 -05002639 if (!!conf->radio_enabled != phy->radio_on) {
2640 if (conf->radio_enabled) {
2641 b43legacy_radio_turn_on(dev);
2642 b43legacyinfo(dev->wl, "Radio turned on by software\n");
2643 if (!dev->radio_hw_enable)
2644 b43legacyinfo(dev->wl, "The hardware RF-kill"
2645 " button still turns the radio"
2646 " physically off. Press the"
2647 " button to turn it on.\n");
2648 } else {
Larry Finger93bb7f32007-10-10 22:44:22 -05002649 b43legacy_radio_turn_off(dev, 0);
Larry Finger42a91742007-09-20 21:11:02 -05002650 b43legacyinfo(dev->wl, "Radio turned off by"
2651 " software\n");
2652 }
2653 }
2654
Larry Finger75388ac2007-09-25 16:46:54 -07002655 spin_lock_irqsave(&wl->irq_lock, flags);
2656 b43legacy_interrupt_enable(dev, savedirqs);
2657 mmiowb();
2658 spin_unlock_irqrestore(&wl->irq_lock, flags);
2659out_unlock_mutex:
2660 mutex_unlock(&wl->mutex);
2661
2662 return err;
2663}
2664
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002665static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
2666 unsigned int changed,
2667 unsigned int *fflags,
2668 int mc_count,
2669 struct dev_addr_list *mc_list)
Larry Finger75388ac2007-09-25 16:46:54 -07002670{
2671 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2672 struct b43legacy_wldev *dev = wl->current_dev;
2673 unsigned long flags;
2674
Johannes Berg4150c572007-09-17 01:29:23 -04002675 if (!dev) {
2676 *fflags = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002677 return;
Larry Finger75388ac2007-09-25 16:46:54 -07002678 }
Johannes Berg4150c572007-09-17 01:29:23 -04002679
2680 spin_lock_irqsave(&wl->irq_lock, flags);
2681 *fflags &= FIF_PROMISC_IN_BSS |
2682 FIF_ALLMULTI |
2683 FIF_FCSFAIL |
2684 FIF_PLCPFAIL |
2685 FIF_CONTROL |
2686 FIF_OTHER_BSS |
2687 FIF_BCN_PRBRESP_PROMISC;
2688
2689 changed &= FIF_PROMISC_IN_BSS |
2690 FIF_ALLMULTI |
2691 FIF_FCSFAIL |
2692 FIF_PLCPFAIL |
2693 FIF_CONTROL |
2694 FIF_OTHER_BSS |
2695 FIF_BCN_PRBRESP_PROMISC;
2696
2697 wl->filter_flags = *fflags;
2698
2699 if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
2700 b43legacy_adjust_opmode(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002701 spin_unlock_irqrestore(&wl->irq_lock, flags);
2702}
2703
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002704static int b43legacy_op_config_interface(struct ieee80211_hw *hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01002705 struct ieee80211_vif *vif,
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002706 struct ieee80211_if_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07002707{
2708 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2709 struct b43legacy_wldev *dev = wl->current_dev;
2710 unsigned long flags;
2711
2712 if (!dev)
2713 return -ENODEV;
2714 mutex_lock(&wl->mutex);
2715 spin_lock_irqsave(&wl->irq_lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01002716 B43legacy_WARN_ON(wl->vif != vif);
Johannes Berg4150c572007-09-17 01:29:23 -04002717 if (conf->bssid)
2718 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
2719 else
2720 memset(wl->bssid, 0, ETH_ALEN);
2721 if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002722 if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP)) {
2723 B43legacy_WARN_ON(vif->type != NL80211_IFTYPE_AP);
Johannes Berg9d139c82008-07-09 14:40:37 +02002724 if (conf->changed & IEEE80211_IFCC_BEACON)
2725 b43legacy_update_templates(wl);
Johannes Berg05c914f2008-09-11 00:01:58 +02002726 } else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
Johannes Berg9d139c82008-07-09 14:40:37 +02002727 if (conf->changed & IEEE80211_IFCC_BEACON)
2728 b43legacy_update_templates(wl);
Larry Finger75388ac2007-09-25 16:46:54 -07002729 }
Johannes Berg4150c572007-09-17 01:29:23 -04002730 b43legacy_write_mac_bssid_templates(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002731 }
2732 spin_unlock_irqrestore(&wl->irq_lock, flags);
2733 mutex_unlock(&wl->mutex);
2734
2735 return 0;
2736}
2737
2738/* Locking: wl->mutex */
2739static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
2740{
2741 struct b43legacy_wl *wl = dev->wl;
2742 unsigned long flags;
2743
2744 if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
2745 return;
Stefano Brivio440cb582007-11-07 18:33:37 +01002746
2747 /* Disable and sync interrupts. We must do this before than
2748 * setting the status to INITIALIZED, as the interrupt handler
2749 * won't care about IRQs then. */
2750 spin_lock_irqsave(&wl->irq_lock, flags);
2751 dev->irq_savedstate = b43legacy_interrupt_disable(dev,
2752 B43legacy_IRQ_ALL);
2753 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
2754 spin_unlock_irqrestore(&wl->irq_lock, flags);
2755 b43legacy_synchronize_irq(dev);
2756
Larry Finger75388ac2007-09-25 16:46:54 -07002757 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
2758
2759 mutex_unlock(&wl->mutex);
2760 /* Must unlock as it would otherwise deadlock. No races here.
2761 * Cancel the possibly running self-rearming periodic work. */
2762 cancel_delayed_work_sync(&dev->periodic_work);
2763 mutex_lock(&wl->mutex);
2764
2765 ieee80211_stop_queues(wl->hw); /* FIXME this could cause a deadlock */
2766
Larry Finger75388ac2007-09-25 16:46:54 -07002767 b43legacy_mac_suspend(dev);
2768 free_irq(dev->dev->irq, dev);
2769 b43legacydbg(wl, "Wireless interface stopped\n");
2770}
2771
2772/* Locking: wl->mutex */
2773static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
2774{
2775 int err;
2776
2777 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
2778
2779 drain_txstatus_queue(dev);
2780 err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
2781 IRQF_SHARED, KBUILD_MODNAME, dev);
2782 if (err) {
2783 b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
2784 dev->dev->irq);
2785 goto out;
2786 }
2787 /* We are ready to run. */
2788 b43legacy_set_status(dev, B43legacy_STAT_STARTED);
2789
2790 /* Start data flow (TX/RX) */
2791 b43legacy_mac_enable(dev);
2792 b43legacy_interrupt_enable(dev, dev->irq_savedstate);
Larry Finger75388ac2007-09-25 16:46:54 -07002793
2794 /* Start maintenance work */
2795 b43legacy_periodic_tasks_setup(dev);
2796
2797 b43legacydbg(dev->wl, "Wireless interface started\n");
2798out:
2799 return err;
2800}
2801
2802/* Get PHY and RADIO versioning numbers */
2803static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
2804{
2805 struct b43legacy_phy *phy = &dev->phy;
2806 u32 tmp;
2807 u8 analog_type;
2808 u8 phy_type;
2809 u8 phy_rev;
2810 u16 radio_manuf;
2811 u16 radio_ver;
2812 u16 radio_rev;
2813 int unsupported = 0;
2814
2815 /* Get PHY versioning */
2816 tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
2817 analog_type = (tmp & B43legacy_PHYVER_ANALOG)
2818 >> B43legacy_PHYVER_ANALOG_SHIFT;
2819 phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
2820 phy_rev = (tmp & B43legacy_PHYVER_VERSION);
2821 switch (phy_type) {
2822 case B43legacy_PHYTYPE_B:
2823 if (phy_rev != 2 && phy_rev != 4
2824 && phy_rev != 6 && phy_rev != 7)
2825 unsupported = 1;
2826 break;
2827 case B43legacy_PHYTYPE_G:
2828 if (phy_rev > 8)
2829 unsupported = 1;
2830 break;
2831 default:
2832 unsupported = 1;
2833 };
2834 if (unsupported) {
2835 b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
2836 "(Analog %u, Type %u, Revision %u)\n",
2837 analog_type, phy_type, phy_rev);
2838 return -EOPNOTSUPP;
2839 }
2840 b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
2841 analog_type, phy_type, phy_rev);
2842
2843
2844 /* Get RADIO versioning */
2845 if (dev->dev->bus->chip_id == 0x4317) {
2846 if (dev->dev->bus->chip_rev == 0)
2847 tmp = 0x3205017F;
2848 else if (dev->dev->bus->chip_rev == 1)
2849 tmp = 0x4205017F;
2850 else
2851 tmp = 0x5205017F;
2852 } else {
2853 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
2854 B43legacy_RADIOCTL_ID);
2855 tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
2856 tmp <<= 16;
2857 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
2858 B43legacy_RADIOCTL_ID);
2859 tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
2860 }
2861 radio_manuf = (tmp & 0x00000FFF);
2862 radio_ver = (tmp & 0x0FFFF000) >> 12;
2863 radio_rev = (tmp & 0xF0000000) >> 28;
2864 switch (phy_type) {
2865 case B43legacy_PHYTYPE_B:
2866 if ((radio_ver & 0xFFF0) != 0x2050)
2867 unsupported = 1;
2868 break;
2869 case B43legacy_PHYTYPE_G:
2870 if (radio_ver != 0x2050)
2871 unsupported = 1;
2872 break;
2873 default:
2874 B43legacy_BUG_ON(1);
2875 }
2876 if (unsupported) {
2877 b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
2878 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
2879 radio_manuf, radio_ver, radio_rev);
2880 return -EOPNOTSUPP;
2881 }
2882 b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
2883 " Revision %u\n", radio_manuf, radio_ver, radio_rev);
2884
2885
2886 phy->radio_manuf = radio_manuf;
2887 phy->radio_ver = radio_ver;
2888 phy->radio_rev = radio_rev;
2889
2890 phy->analog = analog_type;
2891 phy->type = phy_type;
2892 phy->rev = phy_rev;
2893
2894 return 0;
2895}
2896
2897static void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
2898 struct b43legacy_phy *phy)
2899{
2900 struct b43legacy_lopair *lo;
2901 int i;
2902
2903 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
2904 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
2905
Larry Finger1065de12007-09-20 20:10:07 -05002906 /* Assume the radio is enabled. If it's not enabled, the state will
2907 * immediately get fixed on the first periodic work run. */
2908 dev->radio_hw_enable = 1;
Larry Finger75388ac2007-09-25 16:46:54 -07002909
2910 phy->savedpctlreg = 0xFFFF;
2911 phy->aci_enable = 0;
2912 phy->aci_wlan_automatic = 0;
2913 phy->aci_hw_rssi = 0;
2914
2915 lo = phy->_lo_pairs;
2916 if (lo)
2917 memset(lo, 0, sizeof(struct b43legacy_lopair) *
2918 B43legacy_LO_COUNT);
2919 phy->max_lb_gain = 0;
2920 phy->trsw_rx_gain = 0;
2921
2922 /* Set default attenuation values. */
2923 phy->bbatt = b43legacy_default_baseband_attenuation(dev);
2924 phy->rfatt = b43legacy_default_radio_attenuation(dev);
2925 phy->txctl1 = b43legacy_default_txctl1(dev);
2926 phy->txpwr_offset = 0;
2927
2928 /* NRSSI */
2929 phy->nrssislope = 0;
2930 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
2931 phy->nrssi[i] = -1000;
2932 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
2933 phy->nrssi_lt[i] = i;
2934
2935 phy->lofcal = 0xFFFF;
2936 phy->initval = 0xFFFF;
2937
Larry Finger75388ac2007-09-25 16:46:54 -07002938 phy->interfmode = B43legacy_INTERFMODE_NONE;
2939 phy->channel = 0xFF;
2940}
2941
2942static void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
2943{
2944 /* Flags */
Stefano Brivioeed0fd22008-02-08 06:31:10 +01002945 dev->dfq_valid = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002946
2947 /* Stats */
2948 memset(&dev->stats, 0, sizeof(dev->stats));
2949
2950 setup_struct_phy_for_init(dev, &dev->phy);
2951
2952 /* IRQ related flags */
2953 dev->irq_reason = 0;
2954 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
2955 dev->irq_savedstate = B43legacy_IRQ_MASKTEMPLATE;
2956
2957 dev->mac_suspended = 1;
2958
2959 /* Noise calculation context */
2960 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
2961}
2962
2963static void b43legacy_imcfglo_timeouts_workaround(struct b43legacy_wldev *dev)
2964{
2965#ifdef CONFIG_SSB_DRIVER_PCICORE
2966 struct ssb_bus *bus = dev->dev->bus;
2967 u32 tmp;
2968
2969 if (bus->pcicore.dev &&
2970 bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
2971 bus->pcicore.dev->id.revision <= 5) {
2972 /* IMCFGLO timeouts workaround. */
2973 tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
2974 tmp &= ~SSB_IMCFGLO_REQTO;
2975 tmp &= ~SSB_IMCFGLO_SERTO;
2976 switch (bus->bustype) {
2977 case SSB_BUSTYPE_PCI:
2978 case SSB_BUSTYPE_PCMCIA:
2979 tmp |= 0x32;
2980 break;
2981 case SSB_BUSTYPE_SSB:
2982 tmp |= 0x53;
2983 break;
2984 }
2985 ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
2986 }
2987#endif /* CONFIG_SSB_DRIVER_PCICORE */
2988}
2989
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02002990static void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
2991 bool idle) {
2992 u16 pu_delay = 1050;
2993
Johannes Berg05c914f2008-09-11 00:01:58 +02002994 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02002995 pu_delay = 500;
2996 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
2997 pu_delay = max(pu_delay, (u16)2400);
2998
2999 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3000 B43legacy_SHM_SH_SPUWKUP, pu_delay);
3001}
3002
3003/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
3004static void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
3005{
3006 u16 pretbtt;
3007
3008 /* The time value is in microseconds. */
Johannes Berg05c914f2008-09-11 00:01:58 +02003009 if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003010 pretbtt = 2;
3011 else
3012 pretbtt = 250;
3013 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3014 B43legacy_SHM_SH_PRETBTT, pretbtt);
3015 b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
3016}
3017
Larry Finger75388ac2007-09-25 16:46:54 -07003018/* Shutdown a wireless core */
3019/* Locking: wl->mutex */
3020static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
3021{
Larry Finger75388ac2007-09-25 16:46:54 -07003022 struct b43legacy_phy *phy = &dev->phy;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01003023 u32 macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07003024
3025 B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
3026 if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
3027 return;
3028 b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
3029
Stefano Brivioe78c9d22008-01-23 14:48:50 +01003030 /* Stop the microcode PSM. */
3031 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
3032 macctl &= ~B43legacy_MACCTL_PSM_RUN;
3033 macctl |= B43legacy_MACCTL_PSM_JMP0;
3034 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
3035
Larry Finger4ad36d72007-12-16 19:21:06 +01003036 b43legacy_leds_exit(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003037 b43legacy_rng_exit(dev->wl);
3038 b43legacy_pio_free(dev);
3039 b43legacy_dma_free(dev);
3040 b43legacy_chip_exit(dev);
Larry Finger93bb7f32007-10-10 22:44:22 -05003041 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003042 b43legacy_switch_analog(dev, 0);
3043 if (phy->dyn_tssi_tbl)
3044 kfree(phy->tssi2dbm);
3045 kfree(phy->lo_control);
3046 phy->lo_control = NULL;
Stefano Brivioa2971702008-02-08 06:31:25 +01003047 if (dev->wl->current_beacon) {
3048 dev_kfree_skb_any(dev->wl->current_beacon);
3049 dev->wl->current_beacon = NULL;
3050 }
3051
Larry Finger75388ac2007-09-25 16:46:54 -07003052 ssb_device_disable(dev->dev, 0);
3053 ssb_bus_may_powerdown(dev->dev->bus);
3054}
3055
3056static void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
3057{
3058 struct b43legacy_phy *phy = &dev->phy;
3059 int i;
3060
3061 /* Set default attenuation values. */
3062 phy->bbatt = b43legacy_default_baseband_attenuation(dev);
3063 phy->rfatt = b43legacy_default_radio_attenuation(dev);
3064 phy->txctl1 = b43legacy_default_txctl1(dev);
3065 phy->txctl2 = 0xFFFF;
3066 phy->txpwr_offset = 0;
3067
3068 /* NRSSI */
3069 phy->nrssislope = 0;
3070 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
3071 phy->nrssi[i] = -1000;
3072 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
3073 phy->nrssi_lt[i] = i;
3074
3075 phy->lofcal = 0xFFFF;
3076 phy->initval = 0xFFFF;
3077
3078 phy->aci_enable = 0;
3079 phy->aci_wlan_automatic = 0;
3080 phy->aci_hw_rssi = 0;
3081
3082 phy->antenna_diversity = 0xFFFF;
3083 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
3084 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
3085
3086 /* Flags */
3087 phy->calibrated = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07003088
3089 if (phy->_lo_pairs)
3090 memset(phy->_lo_pairs, 0,
3091 sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
3092 memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
3093}
3094
3095/* Initialize a wireless core */
3096static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
3097{
3098 struct b43legacy_wl *wl = dev->wl;
3099 struct ssb_bus *bus = dev->dev->bus;
3100 struct b43legacy_phy *phy = &dev->phy;
3101 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
3102 int err;
3103 u32 hf;
3104 u32 tmp;
3105
3106 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
3107
3108 err = ssb_bus_powerup(bus, 0);
3109 if (err)
3110 goto out;
3111 if (!ssb_device_is_enabled(dev->dev)) {
3112 tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
3113 b43legacy_wireless_core_reset(dev, tmp);
3114 }
3115
3116 if ((phy->type == B43legacy_PHYTYPE_B) ||
3117 (phy->type == B43legacy_PHYTYPE_G)) {
3118 phy->_lo_pairs = kzalloc(sizeof(struct b43legacy_lopair)
3119 * B43legacy_LO_COUNT,
3120 GFP_KERNEL);
3121 if (!phy->_lo_pairs)
3122 return -ENOMEM;
3123 }
3124 setup_struct_wldev_for_init(dev);
3125
3126 err = b43legacy_phy_init_tssi2dbm_table(dev);
3127 if (err)
3128 goto err_kfree_lo_control;
3129
3130 /* Enable IRQ routing to this device. */
3131 ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
3132
3133 b43legacy_imcfglo_timeouts_workaround(dev);
3134 prepare_phy_data_for_init(dev);
3135 b43legacy_phy_calibrate(dev);
3136 err = b43legacy_chip_init(dev);
3137 if (err)
3138 goto err_kfree_tssitbl;
3139 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3140 B43legacy_SHM_SH_WLCOREREV,
3141 dev->dev->id.revision);
3142 hf = b43legacy_hf_read(dev);
3143 if (phy->type == B43legacy_PHYTYPE_G) {
3144 hf |= B43legacy_HF_SYMW;
3145 if (phy->rev == 1)
3146 hf |= B43legacy_HF_GDCW;
Larry Finger7797aa32007-11-09 16:57:34 -06003147 if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
Larry Finger75388ac2007-09-25 16:46:54 -07003148 hf |= B43legacy_HF_OFDMPABOOST;
3149 } else if (phy->type == B43legacy_PHYTYPE_B) {
3150 hf |= B43legacy_HF_SYMW;
3151 if (phy->rev >= 2 && phy->radio_ver == 0x2050)
3152 hf &= ~B43legacy_HF_GDCW;
3153 }
3154 b43legacy_hf_write(dev, hf);
3155
Stefano Brivio0a6e1be2007-11-06 22:48:12 +01003156 b43legacy_set_retry_limits(dev,
3157 B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
3158 B43legacy_DEFAULT_LONG_RETRY_LIMIT);
Larry Finger75388ac2007-09-25 16:46:54 -07003159
3160 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3161 0x0044, 3);
3162 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3163 0x0046, 2);
3164
3165 /* Disable sending probe responses from firmware.
3166 * Setting the MaxTime to one usec will always trigger
3167 * a timeout, so we never send any probe resp.
3168 * A timeout of zero is infinite. */
3169 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3170 B43legacy_SHM_SH_PRMAXTIME, 1);
3171
3172 b43legacy_rate_memory_init(dev);
3173
3174 /* Minimum Contention Window */
3175 if (phy->type == B43legacy_PHYTYPE_B)
3176 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3177 0x0003, 31);
3178 else
3179 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3180 0x0003, 15);
3181 /* Maximum Contention Window */
3182 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3183 0x0004, 1023);
3184
3185 do {
3186 if (b43legacy_using_pio(dev))
3187 err = b43legacy_pio_init(dev);
3188 else {
3189 err = b43legacy_dma_init(dev);
3190 if (!err)
3191 b43legacy_qos_init(dev);
3192 }
3193 } while (err == -EAGAIN);
3194 if (err)
3195 goto err_chip_exit;
3196
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003197 b43legacy_set_synth_pu_delay(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003198
3199 ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
Johannes Berg4150c572007-09-17 01:29:23 -04003200 b43legacy_upload_card_macaddress(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003201 b43legacy_security_init(dev);
3202 b43legacy_rng_init(wl);
3203
3204 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
3205
Larry Finger4ad36d72007-12-16 19:21:06 +01003206 b43legacy_leds_init(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003207out:
3208 return err;
3209
3210err_chip_exit:
3211 b43legacy_chip_exit(dev);
3212err_kfree_tssitbl:
3213 if (phy->dyn_tssi_tbl)
3214 kfree(phy->tssi2dbm);
3215err_kfree_lo_control:
3216 kfree(phy->lo_control);
3217 phy->lo_control = NULL;
3218 ssb_bus_may_powerdown(bus);
3219 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
3220 return err;
3221}
3222
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003223static int b43legacy_op_add_interface(struct ieee80211_hw *hw,
3224 struct ieee80211_if_init_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07003225{
3226 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3227 struct b43legacy_wldev *dev;
3228 unsigned long flags;
3229 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04003230
3231 /* TODO: allow WDS/AP devices to coexist */
3232
Johannes Berg05c914f2008-09-11 00:01:58 +02003233 if (conf->type != NL80211_IFTYPE_AP &&
3234 conf->type != NL80211_IFTYPE_STATION &&
3235 conf->type != NL80211_IFTYPE_WDS &&
3236 conf->type != NL80211_IFTYPE_ADHOC)
Johannes Berg4150c572007-09-17 01:29:23 -04003237 return -EOPNOTSUPP;
Larry Finger75388ac2007-09-25 16:46:54 -07003238
3239 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04003240 if (wl->operating)
Larry Finger75388ac2007-09-25 16:46:54 -07003241 goto out_mutex_unlock;
3242
3243 b43legacydbg(wl, "Adding Interface type %d\n", conf->type);
3244
3245 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04003246 wl->operating = 1;
Johannes Berg32bfd352007-12-19 01:31:26 +01003247 wl->vif = conf->vif;
Johannes Berg4150c572007-09-17 01:29:23 -04003248 wl->if_type = conf->type;
3249 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
Larry Finger75388ac2007-09-25 16:46:54 -07003250
3251 spin_lock_irqsave(&wl->irq_lock, flags);
Larry Finger75388ac2007-09-25 16:46:54 -07003252 b43legacy_adjust_opmode(dev);
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003253 b43legacy_set_pretbtt(dev);
3254 b43legacy_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04003255 b43legacy_upload_card_macaddress(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003256 spin_unlock_irqrestore(&wl->irq_lock, flags);
3257
3258 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04003259 out_mutex_unlock:
Larry Finger75388ac2007-09-25 16:46:54 -07003260 mutex_unlock(&wl->mutex);
3261
3262 return err;
3263}
3264
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003265static void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
3266 struct ieee80211_if_init_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07003267{
3268 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04003269 struct b43legacy_wldev *dev = wl->current_dev;
Larry Finger75388ac2007-09-25 16:46:54 -07003270 unsigned long flags;
3271
3272 b43legacydbg(wl, "Removing Interface type %d\n", conf->type);
3273
3274 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04003275
3276 B43legacy_WARN_ON(!wl->operating);
Johannes Berg32bfd352007-12-19 01:31:26 +01003277 B43legacy_WARN_ON(wl->vif != conf->vif);
3278 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04003279
3280 wl->operating = 0;
3281
3282 spin_lock_irqsave(&wl->irq_lock, flags);
3283 b43legacy_adjust_opmode(dev);
3284 memset(wl->mac_addr, 0, ETH_ALEN);
3285 b43legacy_upload_card_macaddress(dev);
3286 spin_unlock_irqrestore(&wl->irq_lock, flags);
3287
3288 mutex_unlock(&wl->mutex);
3289}
3290
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003291static int b43legacy_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04003292{
3293 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3294 struct b43legacy_wldev *dev = wl->current_dev;
3295 int did_init = 0;
Larry Finger208eec82007-10-14 16:04:30 -05003296 int err = 0;
Michael Buesch8712f272008-01-23 12:15:38 +01003297 bool do_rfkill_exit = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04003298
Larry Finger4ad36d72007-12-16 19:21:06 +01003299 /* First register RFkill.
3300 * LEDs that are registered later depend on it. */
3301 b43legacy_rfkill_init(dev);
3302
Stefano Brivioada50732008-02-02 19:15:57 +01003303 /* Kill all old instance specific information to make sure
3304 * the card won't use it in the short timeframe between start
3305 * and mac80211 reconfiguring it. */
3306 memset(wl->bssid, 0, ETH_ALEN);
3307 memset(wl->mac_addr, 0, ETH_ALEN);
3308 wl->filter_flags = 0;
3309
Johannes Berg4150c572007-09-17 01:29:23 -04003310 mutex_lock(&wl->mutex);
3311
3312 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
3313 err = b43legacy_wireless_core_init(dev);
Michael Buesch8712f272008-01-23 12:15:38 +01003314 if (err) {
3315 do_rfkill_exit = 1;
Johannes Berg4150c572007-09-17 01:29:23 -04003316 goto out_mutex_unlock;
Michael Buesch8712f272008-01-23 12:15:38 +01003317 }
Johannes Berg4150c572007-09-17 01:29:23 -04003318 did_init = 1;
Larry Finger75388ac2007-09-25 16:46:54 -07003319 }
3320
Johannes Berg4150c572007-09-17 01:29:23 -04003321 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
3322 err = b43legacy_wireless_core_start(dev);
3323 if (err) {
3324 if (did_init)
3325 b43legacy_wireless_core_exit(dev);
Michael Buesch8712f272008-01-23 12:15:38 +01003326 do_rfkill_exit = 1;
Johannes Berg4150c572007-09-17 01:29:23 -04003327 goto out_mutex_unlock;
3328 }
Larry Finger75388ac2007-09-25 16:46:54 -07003329 }
Johannes Berg4150c572007-09-17 01:29:23 -04003330
3331out_mutex_unlock:
3332 mutex_unlock(&wl->mutex);
3333
Michael Buesch8712f272008-01-23 12:15:38 +01003334 if (do_rfkill_exit)
3335 b43legacy_rfkill_exit(dev);
3336
Johannes Berg4150c572007-09-17 01:29:23 -04003337 return err;
3338}
3339
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003340static void b43legacy_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04003341{
3342 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3343 struct b43legacy_wldev *dev = wl->current_dev;
3344
Larry Finger4ad36d72007-12-16 19:21:06 +01003345 b43legacy_rfkill_exit(dev);
3346
Johannes Berg4150c572007-09-17 01:29:23 -04003347 mutex_lock(&wl->mutex);
3348 if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
3349 b43legacy_wireless_core_stop(dev);
3350 b43legacy_wireless_core_exit(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003351 mutex_unlock(&wl->mutex);
3352}
3353
Stefano Brivioa2971702008-02-08 06:31:25 +01003354static int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
Johannes Berg17741cd2008-09-11 00:02:02 +02003355 struct ieee80211_sta *sta, bool set)
Stefano Brivioa2971702008-02-08 06:31:25 +01003356{
3357 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
Stefano Brivioa2971702008-02-08 06:31:25 +01003358 unsigned long flags;
3359
3360 spin_lock_irqsave(&wl->irq_lock, flags);
Johannes Berg9d139c82008-07-09 14:40:37 +02003361 b43legacy_update_templates(wl);
Stefano Brivioa2971702008-02-08 06:31:25 +01003362 spin_unlock_irqrestore(&wl->irq_lock, flags);
3363
3364 return 0;
3365}
3366
Larry Finger75388ac2007-09-25 16:46:54 -07003367static const struct ieee80211_ops b43legacy_hw_ops = {
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003368 .tx = b43legacy_op_tx,
3369 .conf_tx = b43legacy_op_conf_tx,
3370 .add_interface = b43legacy_op_add_interface,
3371 .remove_interface = b43legacy_op_remove_interface,
3372 .config = b43legacy_op_dev_config,
3373 .config_interface = b43legacy_op_config_interface,
3374 .configure_filter = b43legacy_op_configure_filter,
3375 .get_stats = b43legacy_op_get_stats,
3376 .get_tx_stats = b43legacy_op_get_tx_stats,
3377 .start = b43legacy_op_start,
3378 .stop = b43legacy_op_stop,
Stefano Brivioa2971702008-02-08 06:31:25 +01003379 .set_tim = b43legacy_op_beacon_set_tim,
Larry Finger75388ac2007-09-25 16:46:54 -07003380};
3381
3382/* Hard-reset the chip. Do not call this directly.
3383 * Use b43legacy_controller_restart()
3384 */
3385static void b43legacy_chip_reset(struct work_struct *work)
3386{
3387 struct b43legacy_wldev *dev =
3388 container_of(work, struct b43legacy_wldev, restart_work);
3389 struct b43legacy_wl *wl = dev->wl;
3390 int err = 0;
3391 int prev_status;
3392
3393 mutex_lock(&wl->mutex);
3394
3395 prev_status = b43legacy_status(dev);
3396 /* Bring the device down... */
3397 if (prev_status >= B43legacy_STAT_STARTED)
3398 b43legacy_wireless_core_stop(dev);
3399 if (prev_status >= B43legacy_STAT_INITIALIZED)
3400 b43legacy_wireless_core_exit(dev);
3401
3402 /* ...and up again. */
3403 if (prev_status >= B43legacy_STAT_INITIALIZED) {
3404 err = b43legacy_wireless_core_init(dev);
3405 if (err)
3406 goto out;
3407 }
3408 if (prev_status >= B43legacy_STAT_STARTED) {
3409 err = b43legacy_wireless_core_start(dev);
3410 if (err) {
3411 b43legacy_wireless_core_exit(dev);
3412 goto out;
3413 }
3414 }
3415out:
Michael Buesch48e6c512008-05-22 17:06:36 +02003416 if (err)
3417 wl->current_dev = NULL; /* Failed to init the dev. */
Larry Finger75388ac2007-09-25 16:46:54 -07003418 mutex_unlock(&wl->mutex);
3419 if (err)
3420 b43legacyerr(wl, "Controller restart FAILED\n");
3421 else
3422 b43legacyinfo(wl, "Controller restarted\n");
3423}
3424
3425static int b43legacy_setup_modes(struct b43legacy_wldev *dev,
3426 int have_bphy,
3427 int have_gphy)
3428{
3429 struct ieee80211_hw *hw = dev->wl->hw;
Larry Finger75388ac2007-09-25 16:46:54 -07003430 struct b43legacy_phy *phy = &dev->phy;
Larry Finger75388ac2007-09-25 16:46:54 -07003431
3432 phy->possible_phymodes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003433 if (have_bphy) {
3434 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3435 &b43legacy_band_2GHz_BPHY;
3436 phy->possible_phymodes |= B43legacy_PHYMODE_B;
3437 }
Larry Finger75388ac2007-09-25 16:46:54 -07003438
Johannes Berg8318d782008-01-24 19:38:38 +01003439 if (have_gphy) {
3440 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3441 &b43legacy_band_2GHz_GPHY;
3442 phy->possible_phymodes |= B43legacy_PHYMODE_G;
Larry Finger75388ac2007-09-25 16:46:54 -07003443 }
3444
3445 return 0;
3446}
3447
3448static void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
3449{
3450 /* We release firmware that late to not be required to re-request
3451 * is all the time when we reinit the core. */
3452 b43legacy_release_firmware(dev);
3453}
3454
3455static int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
3456{
3457 struct b43legacy_wl *wl = dev->wl;
3458 struct ssb_bus *bus = dev->dev->bus;
3459 struct pci_dev *pdev = bus->host_pci;
3460 int err;
3461 int have_bphy = 0;
3462 int have_gphy = 0;
3463 u32 tmp;
3464
3465 /* Do NOT do any device initialization here.
3466 * Do it in wireless_core_init() instead.
3467 * This function is for gathering basic information about the HW, only.
3468 * Also some structs may be set up here. But most likely you want to
3469 * have that in core_init(), too.
3470 */
3471
3472 err = ssb_bus_powerup(bus, 0);
3473 if (err) {
3474 b43legacyerr(wl, "Bus powerup failed\n");
3475 goto out;
3476 }
3477 /* Get the PHY type. */
3478 if (dev->dev->id.revision >= 5) {
3479 u32 tmshigh;
3480
3481 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
3482 have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
3483 if (!have_gphy)
3484 have_bphy = 1;
3485 } else if (dev->dev->id.revision == 4)
3486 have_gphy = 1;
3487 else
3488 have_bphy = 1;
3489
Larry Finger75388ac2007-09-25 16:46:54 -07003490 dev->phy.gmode = (have_gphy || have_bphy);
3491 tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
3492 b43legacy_wireless_core_reset(dev, tmp);
3493
3494 err = b43legacy_phy_versioning(dev);
3495 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003496 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003497 /* Check if this device supports multiband. */
3498 if (!pdev ||
3499 (pdev->device != 0x4312 &&
3500 pdev->device != 0x4319 &&
3501 pdev->device != 0x4324)) {
3502 /* No multiband support. */
3503 have_bphy = 0;
3504 have_gphy = 0;
3505 switch (dev->phy.type) {
3506 case B43legacy_PHYTYPE_B:
3507 have_bphy = 1;
3508 break;
3509 case B43legacy_PHYTYPE_G:
3510 have_gphy = 1;
3511 break;
3512 default:
3513 B43legacy_BUG_ON(1);
3514 }
3515 }
3516 dev->phy.gmode = (have_gphy || have_bphy);
3517 tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
3518 b43legacy_wireless_core_reset(dev, tmp);
3519
3520 err = b43legacy_validate_chipaccess(dev);
3521 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003522 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003523 err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
3524 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003525 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003526
3527 /* Now set some default "current_dev" */
3528 if (!wl->current_dev)
3529 wl->current_dev = dev;
3530 INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
3531
Larry Finger93bb7f32007-10-10 22:44:22 -05003532 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003533 b43legacy_switch_analog(dev, 0);
3534 ssb_device_disable(dev->dev, 0);
3535 ssb_bus_may_powerdown(bus);
3536
3537out:
3538 return err;
3539
Larry Finger75388ac2007-09-25 16:46:54 -07003540err_powerdown:
3541 ssb_bus_may_powerdown(bus);
3542 return err;
3543}
3544
3545static void b43legacy_one_core_detach(struct ssb_device *dev)
3546{
3547 struct b43legacy_wldev *wldev;
3548 struct b43legacy_wl *wl;
3549
Michael Buesch48e6c512008-05-22 17:06:36 +02003550 /* Do not cancel ieee80211-workqueue based work here.
3551 * See comment in b43legacy_remove(). */
3552
Larry Finger75388ac2007-09-25 16:46:54 -07003553 wldev = ssb_get_drvdata(dev);
3554 wl = wldev->wl;
Larry Finger75388ac2007-09-25 16:46:54 -07003555 b43legacy_debugfs_remove_device(wldev);
3556 b43legacy_wireless_core_detach(wldev);
3557 list_del(&wldev->list);
3558 wl->nr_devs--;
3559 ssb_set_drvdata(dev, NULL);
3560 kfree(wldev);
3561}
3562
3563static int b43legacy_one_core_attach(struct ssb_device *dev,
3564 struct b43legacy_wl *wl)
3565{
3566 struct b43legacy_wldev *wldev;
3567 struct pci_dev *pdev;
3568 int err = -ENOMEM;
3569
3570 if (!list_empty(&wl->devlist)) {
3571 /* We are not the first core on this chip. */
3572 pdev = dev->bus->host_pci;
3573 /* Only special chips support more than one wireless
3574 * core, although some of the other chips have more than
3575 * one wireless core as well. Check for this and
3576 * bail out early.
3577 */
3578 if (!pdev ||
3579 ((pdev->device != 0x4321) &&
3580 (pdev->device != 0x4313) &&
3581 (pdev->device != 0x431A))) {
3582 b43legacydbg(wl, "Ignoring unconnected 802.11 core\n");
3583 return -ENODEV;
3584 }
3585 }
3586
3587 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
3588 if (!wldev)
3589 goto out;
3590
3591 wldev->dev = dev;
3592 wldev->wl = wl;
3593 b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
3594 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
3595 tasklet_init(&wldev->isr_tasklet,
3596 (void (*)(unsigned long))b43legacy_interrupt_tasklet,
3597 (unsigned long)wldev);
3598 if (modparam_pio)
3599 wldev->__using_pio = 1;
3600 INIT_LIST_HEAD(&wldev->list);
3601
3602 err = b43legacy_wireless_core_attach(wldev);
3603 if (err)
3604 goto err_kfree_wldev;
3605
3606 list_add(&wldev->list, &wl->devlist);
3607 wl->nr_devs++;
3608 ssb_set_drvdata(dev, wldev);
3609 b43legacy_debugfs_add_device(wldev);
3610out:
3611 return err;
3612
3613err_kfree_wldev:
3614 kfree(wldev);
3615 return err;
3616}
3617
3618static void b43legacy_sprom_fixup(struct ssb_bus *bus)
3619{
3620 /* boardflags workarounds */
3621 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
3622 bus->boardinfo.type == 0x4E &&
3623 bus->boardinfo.rev > 0x40)
Larry Finger7797aa32007-11-09 16:57:34 -06003624 bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
Larry Finger75388ac2007-09-25 16:46:54 -07003625}
3626
3627static void b43legacy_wireless_exit(struct ssb_device *dev,
3628 struct b43legacy_wl *wl)
3629{
3630 struct ieee80211_hw *hw = wl->hw;
3631
3632 ssb_set_devtypedata(dev, NULL);
3633 ieee80211_free_hw(hw);
3634}
3635
3636static int b43legacy_wireless_init(struct ssb_device *dev)
3637{
3638 struct ssb_sprom *sprom = &dev->bus->sprom;
3639 struct ieee80211_hw *hw;
3640 struct b43legacy_wl *wl;
3641 int err = -ENOMEM;
3642
3643 b43legacy_sprom_fixup(dev->bus);
3644
3645 hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
3646 if (!hw) {
3647 b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
3648 goto out;
3649 }
3650
3651 /* fill hw info */
Johannes Berg605a0bd2008-07-15 10:10:01 +02003652 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Bruno Randolf566bfe52008-05-08 19:15:40 +02003653 IEEE80211_HW_SIGNAL_DBM |
3654 IEEE80211_HW_NOISE_DBM;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07003655 hw->wiphy->interface_modes =
3656 BIT(NL80211_IFTYPE_AP) |
3657 BIT(NL80211_IFTYPE_STATION) |
3658 BIT(NL80211_IFTYPE_WDS) |
3659 BIT(NL80211_IFTYPE_ADHOC);
Larry Finger75388ac2007-09-25 16:46:54 -07003660 hw->queues = 1; /* FIXME: hardware has more queues */
Johannes Berge6a98542008-10-21 12:40:02 +02003661 hw->max_rates = 2;
Larry Finger75388ac2007-09-25 16:46:54 -07003662 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger7797aa32007-11-09 16:57:34 -06003663 if (is_valid_ether_addr(sprom->et1mac))
3664 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Larry Finger75388ac2007-09-25 16:46:54 -07003665 else
Larry Finger7797aa32007-11-09 16:57:34 -06003666 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Larry Finger75388ac2007-09-25 16:46:54 -07003667
3668 /* Get and initialize struct b43legacy_wl */
3669 wl = hw_to_b43legacy_wl(hw);
3670 memset(wl, 0, sizeof(*wl));
3671 wl->hw = hw;
3672 spin_lock_init(&wl->irq_lock);
3673 spin_lock_init(&wl->leds_lock);
3674 mutex_init(&wl->mutex);
3675 INIT_LIST_HEAD(&wl->devlist);
3676
3677 ssb_set_devtypedata(dev, wl);
3678 b43legacyinfo(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
3679 err = 0;
3680out:
3681 return err;
3682}
3683
3684static int b43legacy_probe(struct ssb_device *dev,
3685 const struct ssb_device_id *id)
3686{
3687 struct b43legacy_wl *wl;
3688 int err;
3689 int first = 0;
3690
3691 wl = ssb_get_devtypedata(dev);
3692 if (!wl) {
3693 /* Probing the first core - setup common struct b43legacy_wl */
3694 first = 1;
3695 err = b43legacy_wireless_init(dev);
3696 if (err)
3697 goto out;
3698 wl = ssb_get_devtypedata(dev);
3699 B43legacy_WARN_ON(!wl);
3700 }
3701 err = b43legacy_one_core_attach(dev, wl);
3702 if (err)
3703 goto err_wireless_exit;
3704
3705 if (first) {
3706 err = ieee80211_register_hw(wl->hw);
3707 if (err)
3708 goto err_one_core_detach;
3709 }
3710
3711out:
3712 return err;
3713
3714err_one_core_detach:
3715 b43legacy_one_core_detach(dev);
3716err_wireless_exit:
3717 if (first)
3718 b43legacy_wireless_exit(dev, wl);
3719 return err;
3720}
3721
3722static void b43legacy_remove(struct ssb_device *dev)
3723{
3724 struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
3725 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3726
Michael Buesch48e6c512008-05-22 17:06:36 +02003727 /* We must cancel any work here before unregistering from ieee80211,
3728 * as the ieee80211 unreg will destroy the workqueue. */
3729 cancel_work_sync(&wldev->restart_work);
3730
Larry Finger75388ac2007-09-25 16:46:54 -07003731 B43legacy_WARN_ON(!wl);
3732 if (wl->current_dev == wldev)
3733 ieee80211_unregister_hw(wl->hw);
3734
3735 b43legacy_one_core_detach(dev);
3736
3737 if (list_empty(&wl->devlist))
3738 /* Last core on the chip unregistered.
3739 * We can destroy common struct b43legacy_wl.
3740 */
3741 b43legacy_wireless_exit(dev, wl);
3742}
3743
3744/* Perform a hardware reset. This can be called from any context. */
3745void b43legacy_controller_restart(struct b43legacy_wldev *dev,
3746 const char *reason)
3747{
3748 /* Must avoid requeueing, if we are in shutdown. */
3749 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
3750 return;
3751 b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
3752 queue_work(dev->wl->hw->workqueue, &dev->restart_work);
3753}
3754
3755#ifdef CONFIG_PM
3756
3757static int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
3758{
3759 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3760 struct b43legacy_wl *wl = wldev->wl;
3761
3762 b43legacydbg(wl, "Suspending...\n");
3763
3764 mutex_lock(&wl->mutex);
3765 wldev->suspend_init_status = b43legacy_status(wldev);
3766 if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
3767 b43legacy_wireless_core_stop(wldev);
3768 if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
3769 b43legacy_wireless_core_exit(wldev);
3770 mutex_unlock(&wl->mutex);
3771
3772 b43legacydbg(wl, "Device suspended.\n");
3773
3774 return 0;
3775}
3776
3777static int b43legacy_resume(struct ssb_device *dev)
3778{
3779 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3780 struct b43legacy_wl *wl = wldev->wl;
3781 int err = 0;
3782
3783 b43legacydbg(wl, "Resuming...\n");
3784
3785 mutex_lock(&wl->mutex);
3786 if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
3787 err = b43legacy_wireless_core_init(wldev);
3788 if (err) {
3789 b43legacyerr(wl, "Resume failed at core init\n");
3790 goto out;
3791 }
3792 }
3793 if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
3794 err = b43legacy_wireless_core_start(wldev);
3795 if (err) {
3796 b43legacy_wireless_core_exit(wldev);
3797 b43legacyerr(wl, "Resume failed at core start\n");
3798 goto out;
3799 }
3800 }
Larry Finger75388ac2007-09-25 16:46:54 -07003801
3802 b43legacydbg(wl, "Device resumed.\n");
3803out:
Julia Lawall41048632008-07-21 11:29:34 +02003804 mutex_unlock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07003805 return err;
3806}
3807
3808#else /* CONFIG_PM */
3809# define b43legacy_suspend NULL
3810# define b43legacy_resume NULL
3811#endif /* CONFIG_PM */
3812
3813static struct ssb_driver b43legacy_ssb_driver = {
3814 .name = KBUILD_MODNAME,
3815 .id_table = b43legacy_ssb_tbl,
3816 .probe = b43legacy_probe,
3817 .remove = b43legacy_remove,
3818 .suspend = b43legacy_suspend,
3819 .resume = b43legacy_resume,
3820};
3821
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003822static void b43legacy_print_driverinfo(void)
3823{
3824 const char *feat_pci = "", *feat_leds = "", *feat_rfkill = "",
3825 *feat_pio = "", *feat_dma = "";
3826
3827#ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
3828 feat_pci = "P";
3829#endif
3830#ifdef CONFIG_B43LEGACY_LEDS
3831 feat_leds = "L";
3832#endif
3833#ifdef CONFIG_B43LEGACY_RFKILL
3834 feat_rfkill = "R";
3835#endif
3836#ifdef CONFIG_B43LEGACY_PIO
3837 feat_pio = "I";
3838#endif
3839#ifdef CONFIG_B43LEGACY_DMA
3840 feat_dma = "D";
3841#endif
Michael Bueschc256e052008-03-04 20:31:13 +01003842 printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003843 "[ Features: %s%s%s%s%s, Firmware-ID: "
3844 B43legacy_SUPPORTED_FIRMWARE_ID " ]\n",
3845 feat_pci, feat_leds, feat_rfkill, feat_pio, feat_dma);
3846}
3847
Larry Finger75388ac2007-09-25 16:46:54 -07003848static int __init b43legacy_init(void)
3849{
3850 int err;
3851
3852 b43legacy_debugfs_init();
3853
3854 err = ssb_driver_register(&b43legacy_ssb_driver);
3855 if (err)
3856 goto err_dfs_exit;
3857
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003858 b43legacy_print_driverinfo();
3859
Larry Finger75388ac2007-09-25 16:46:54 -07003860 return err;
3861
3862err_dfs_exit:
3863 b43legacy_debugfs_exit();
3864 return err;
3865}
3866
3867static void __exit b43legacy_exit(void)
3868{
3869 ssb_driver_unregister(&b43legacy_ssb_driver);
3870 b43legacy_debugfs_exit();
3871}
3872
3873module_init(b43legacy_init)
3874module_exit(b43legacy_exit)