blob: 2422a5dab1d2b89cdd891b5f283bf988c80934c6 [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>
37#include <linux/version.h>
38#include <linux/firmware.h>
39#include <linux/wireless.h>
40#include <linux/workqueue.h>
41#include <linux/skbuff.h>
42#include <linux/dma-mapping.h>
43#include <net/dst.h>
44#include <asm/unaligned.h>
45
46#include "b43legacy.h"
47#include "main.h"
48#include "debugfs.h"
49#include "phy.h"
50#include "dma.h"
51#include "pio.h"
52#include "sysfs.h"
53#include "xmit.h"
54#include "radio.h"
55
56
57MODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
58MODULE_AUTHOR("Martin Langer");
59MODULE_AUTHOR("Stefano Brivio");
60MODULE_AUTHOR("Michael Buesch");
61MODULE_LICENSE("GPL");
62
Stefano Brivio1a1c3602008-02-12 03:09:00 +010063MODULE_FIRMWARE(B43legacy_SUPPORTED_FIRMWARE_ID);
64
Larry Finger75388ac2007-09-25 16:46:54 -070065#if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
66static int modparam_pio;
67module_param_named(pio, modparam_pio, int, 0444);
68MODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
69#elif defined(CONFIG_B43LEGACY_DMA)
70# define modparam_pio 0
71#elif defined(CONFIG_B43LEGACY_PIO)
72# define modparam_pio 1
73#endif
74
75static int modparam_bad_frames_preempt;
76module_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
77MODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
78 " Preemption");
79
Larry Finger75388ac2007-09-25 16:46:54 -070080static char modparam_fwpostfix[16];
81module_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
82MODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
83
Larry Finger75388ac2007-09-25 16:46:54 -070084/* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
85static const struct ssb_device_id b43legacy_ssb_tbl[] = {
86 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
87 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
88 SSB_DEVTABLE_END
89};
90MODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
91
92
93/* Channel and ratetables are shared for all devices.
94 * They can't be const, because ieee80211 puts some precalculated
95 * data in there. This data is the same for all devices, so we don't
96 * get concurrency issues */
97#define RATETAB_ENT(_rateid, _flags) \
Johannes Berg8318d782008-01-24 19:38:38 +010098 { \
99 .bitrate = B43legacy_RATE_TO_100KBPS(_rateid), \
100 .hw_value = (_rateid), \
101 .flags = (_flags), \
Larry Finger75388ac2007-09-25 16:46:54 -0700102 }
Johannes Berg8318d782008-01-24 19:38:38 +0100103/*
104 * NOTE: When changing this, sync with xmit.c's
105 * b43legacy_plcp_get_bitrate_idx_* functions!
106 */
Larry Finger75388ac2007-09-25 16:46:54 -0700107static struct ieee80211_rate __b43legacy_ratetable[] = {
Johannes Berg8318d782008-01-24 19:38:38 +0100108 RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
109 RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
110 RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
111 RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
112 RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
113 RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
114 RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
115 RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
116 RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
117 RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
118 RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
119 RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
Larry Finger75388ac2007-09-25 16:46:54 -0700120};
Larry Finger75388ac2007-09-25 16:46:54 -0700121#define b43legacy_b_ratetable (__b43legacy_ratetable + 0)
122#define b43legacy_b_ratetable_size 4
123#define b43legacy_g_ratetable (__b43legacy_ratetable + 0)
124#define b43legacy_g_ratetable_size 12
125
126#define CHANTAB_ENT(_chanid, _freq) \
127 { \
Johannes Berg8318d782008-01-24 19:38:38 +0100128 .center_freq = (_freq), \
129 .hw_value = (_chanid), \
Larry Finger75388ac2007-09-25 16:46:54 -0700130 }
131static struct ieee80211_channel b43legacy_bg_chantable[] = {
132 CHANTAB_ENT(1, 2412),
133 CHANTAB_ENT(2, 2417),
134 CHANTAB_ENT(3, 2422),
135 CHANTAB_ENT(4, 2427),
136 CHANTAB_ENT(5, 2432),
137 CHANTAB_ENT(6, 2437),
138 CHANTAB_ENT(7, 2442),
139 CHANTAB_ENT(8, 2447),
140 CHANTAB_ENT(9, 2452),
141 CHANTAB_ENT(10, 2457),
142 CHANTAB_ENT(11, 2462),
143 CHANTAB_ENT(12, 2467),
144 CHANTAB_ENT(13, 2472),
145 CHANTAB_ENT(14, 2484),
146};
Johannes Berg8318d782008-01-24 19:38:38 +0100147
148static struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
149 .channels = b43legacy_bg_chantable,
150 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
151 .bitrates = b43legacy_b_ratetable,
152 .n_bitrates = b43legacy_b_ratetable_size,
153};
154
155static struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
156 .channels = b43legacy_bg_chantable,
157 .n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
158 .bitrates = b43legacy_g_ratetable,
159 .n_bitrates = b43legacy_g_ratetable_size,
160};
Larry Finger75388ac2007-09-25 16:46:54 -0700161
162static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
163static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
164static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
165static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
166
167
168static int b43legacy_ratelimit(struct b43legacy_wl *wl)
169{
170 if (!wl || !wl->current_dev)
171 return 1;
172 if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
173 return 1;
174 /* We are up and running.
175 * Ratelimit the messages to avoid DoS over the net. */
176 return net_ratelimit();
177}
178
179void b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
180{
181 va_list args;
182
183 if (!b43legacy_ratelimit(wl))
184 return;
185 va_start(args, fmt);
186 printk(KERN_INFO "b43legacy-%s: ",
187 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
188 vprintk(fmt, args);
189 va_end(args);
190}
191
192void b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
193{
194 va_list args;
195
196 if (!b43legacy_ratelimit(wl))
197 return;
198 va_start(args, fmt);
199 printk(KERN_ERR "b43legacy-%s ERROR: ",
200 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
201 vprintk(fmt, args);
202 va_end(args);
203}
204
205void b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
206{
207 va_list args;
208
209 if (!b43legacy_ratelimit(wl))
210 return;
211 va_start(args, fmt);
212 printk(KERN_WARNING "b43legacy-%s warning: ",
213 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
214 vprintk(fmt, args);
215 va_end(args);
216}
217
218#if B43legacy_DEBUG
219void b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
220{
221 va_list args;
222
223 va_start(args, fmt);
224 printk(KERN_DEBUG "b43legacy-%s debug: ",
225 (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
226 vprintk(fmt, args);
227 va_end(args);
228}
229#endif /* DEBUG */
230
231static void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
232 u32 val)
233{
234 u32 status;
235
236 B43legacy_WARN_ON(offset % 4 != 0);
237
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100238 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
239 if (status & B43legacy_MACCTL_BE)
Larry Finger75388ac2007-09-25 16:46:54 -0700240 val = swab32(val);
241
242 b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
243 mmiowb();
244 b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
245}
246
247static inline
248void b43legacy_shm_control_word(struct b43legacy_wldev *dev,
249 u16 routing, u16 offset)
250{
251 u32 control;
252
253 /* "offset" is the WORD offset. */
254
255 control = routing;
256 control <<= 16;
257 control |= offset;
258 b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
259}
260
261u32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
262 u16 routing, u16 offset)
263{
264 u32 ret;
265
266 if (routing == B43legacy_SHM_SHARED) {
267 B43legacy_WARN_ON((offset & 0x0001) != 0);
268 if (offset & 0x0003) {
269 /* Unaligned access */
270 b43legacy_shm_control_word(dev, routing, offset >> 2);
271 ret = b43legacy_read16(dev,
272 B43legacy_MMIO_SHM_DATA_UNALIGNED);
273 ret <<= 16;
274 b43legacy_shm_control_word(dev, routing,
275 (offset >> 2) + 1);
276 ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
277
278 return ret;
279 }
280 offset >>= 2;
281 }
282 b43legacy_shm_control_word(dev, routing, offset);
283 ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
284
285 return ret;
286}
287
288u16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
289 u16 routing, u16 offset)
290{
291 u16 ret;
292
293 if (routing == B43legacy_SHM_SHARED) {
294 B43legacy_WARN_ON((offset & 0x0001) != 0);
295 if (offset & 0x0003) {
296 /* Unaligned access */
297 b43legacy_shm_control_word(dev, routing, offset >> 2);
298 ret = b43legacy_read16(dev,
299 B43legacy_MMIO_SHM_DATA_UNALIGNED);
300
301 return ret;
302 }
303 offset >>= 2;
304 }
305 b43legacy_shm_control_word(dev, routing, offset);
306 ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
307
308 return ret;
309}
310
311void b43legacy_shm_write32(struct b43legacy_wldev *dev,
312 u16 routing, u16 offset,
313 u32 value)
314{
315 if (routing == B43legacy_SHM_SHARED) {
316 B43legacy_WARN_ON((offset & 0x0001) != 0);
317 if (offset & 0x0003) {
318 /* Unaligned access */
319 b43legacy_shm_control_word(dev, routing, offset >> 2);
320 mmiowb();
321 b43legacy_write16(dev,
322 B43legacy_MMIO_SHM_DATA_UNALIGNED,
323 (value >> 16) & 0xffff);
324 mmiowb();
325 b43legacy_shm_control_word(dev, routing,
326 (offset >> 2) + 1);
327 mmiowb();
328 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
329 value & 0xffff);
330 return;
331 }
332 offset >>= 2;
333 }
334 b43legacy_shm_control_word(dev, routing, offset);
335 mmiowb();
336 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
337}
338
339void b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
340 u16 value)
341{
342 if (routing == B43legacy_SHM_SHARED) {
343 B43legacy_WARN_ON((offset & 0x0001) != 0);
344 if (offset & 0x0003) {
345 /* Unaligned access */
346 b43legacy_shm_control_word(dev, routing, offset >> 2);
347 mmiowb();
348 b43legacy_write16(dev,
349 B43legacy_MMIO_SHM_DATA_UNALIGNED,
350 value);
351 return;
352 }
353 offset >>= 2;
354 }
355 b43legacy_shm_control_word(dev, routing, offset);
356 mmiowb();
357 b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
358}
359
360/* Read HostFlags */
361u32 b43legacy_hf_read(struct b43legacy_wldev *dev)
362{
363 u32 ret;
364
365 ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
366 B43legacy_SHM_SH_HOSTFHI);
367 ret <<= 16;
368 ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
369 B43legacy_SHM_SH_HOSTFLO);
370
371 return ret;
372}
373
374/* Write HostFlags */
375void b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
376{
377 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
378 B43legacy_SHM_SH_HOSTFLO,
379 (value & 0x0000FFFF));
380 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
381 B43legacy_SHM_SH_HOSTFHI,
382 ((value & 0xFFFF0000) >> 16));
383}
384
385void b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
386{
387 /* We need to be careful. As we read the TSF from multiple
388 * registers, we should take care of register overflows.
389 * In theory, the whole tsf read process should be atomic.
390 * We try to be atomic here, by restaring the read process,
391 * if any of the high registers changed (overflew).
392 */
393 if (dev->dev->id.revision >= 3) {
394 u32 low;
395 u32 high;
396 u32 high2;
397
398 do {
399 high = b43legacy_read32(dev,
400 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
401 low = b43legacy_read32(dev,
402 B43legacy_MMIO_REV3PLUS_TSF_LOW);
403 high2 = b43legacy_read32(dev,
404 B43legacy_MMIO_REV3PLUS_TSF_HIGH);
405 } while (unlikely(high != high2));
406
407 *tsf = high;
408 *tsf <<= 32;
409 *tsf |= low;
410 } else {
411 u64 tmp;
412 u16 v0;
413 u16 v1;
414 u16 v2;
415 u16 v3;
416 u16 test1;
417 u16 test2;
418 u16 test3;
419
420 do {
421 v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
422 v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
423 v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
424 v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
425
426 test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
427 test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
428 test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
429 } while (v3 != test3 || v2 != test2 || v1 != test1);
430
431 *tsf = v3;
432 *tsf <<= 48;
433 tmp = v2;
434 tmp <<= 32;
435 *tsf |= tmp;
436 tmp = v1;
437 tmp <<= 16;
438 *tsf |= tmp;
439 *tsf |= v0;
440 }
441}
442
443static void b43legacy_time_lock(struct b43legacy_wldev *dev)
444{
445 u32 status;
446
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100447 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
448 status |= B43legacy_MACCTL_TBTTHOLD;
449 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
Larry Finger75388ac2007-09-25 16:46:54 -0700450 mmiowb();
451}
452
453static void b43legacy_time_unlock(struct b43legacy_wldev *dev)
454{
455 u32 status;
456
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100457 status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
458 status &= ~B43legacy_MACCTL_TBTTHOLD;
459 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
Larry Finger75388ac2007-09-25 16:46:54 -0700460}
461
462static void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
463{
464 /* Be careful with the in-progress timer.
465 * First zero out the low register, so we have a full
466 * register-overflow duration to complete the operation.
467 */
468 if (dev->dev->id.revision >= 3) {
469 u32 lo = (tsf & 0x00000000FFFFFFFFULL);
470 u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
471
472 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
473 mmiowb();
474 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
475 hi);
476 mmiowb();
477 b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
478 lo);
479 } else {
480 u16 v0 = (tsf & 0x000000000000FFFFULL);
481 u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
482 u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
483 u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
484
485 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
486 mmiowb();
487 b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
488 mmiowb();
489 b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
490 mmiowb();
491 b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
492 mmiowb();
493 b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
494 }
495}
496
497void b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
498{
499 b43legacy_time_lock(dev);
500 b43legacy_tsf_write_locked(dev, tsf);
501 b43legacy_time_unlock(dev);
502}
503
504static
505void b43legacy_macfilter_set(struct b43legacy_wldev *dev,
506 u16 offset, const u8 *mac)
507{
508 static const u8 zero_addr[ETH_ALEN] = { 0 };
509 u16 data;
510
511 if (!mac)
512 mac = zero_addr;
513
514 offset |= 0x0020;
515 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
516
517 data = mac[0];
518 data |= mac[1] << 8;
519 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
520 data = mac[2];
521 data |= mac[3] << 8;
522 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
523 data = mac[4];
524 data |= mac[5] << 8;
525 b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
526}
527
528static void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
529{
530 static const u8 zero_addr[ETH_ALEN] = { 0 };
531 const u8 *mac = dev->wl->mac_addr;
532 const u8 *bssid = dev->wl->bssid;
533 u8 mac_bssid[ETH_ALEN * 2];
534 int i;
535 u32 tmp;
536
537 if (!bssid)
538 bssid = zero_addr;
539 if (!mac)
540 mac = zero_addr;
541
542 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
543
544 memcpy(mac_bssid, mac, ETH_ALEN);
545 memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
546
547 /* Write our MAC address and BSSID to template ram */
548 for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
549 tmp = (u32)(mac_bssid[i + 0]);
550 tmp |= (u32)(mac_bssid[i + 1]) << 8;
551 tmp |= (u32)(mac_bssid[i + 2]) << 16;
552 tmp |= (u32)(mac_bssid[i + 3]) << 24;
553 b43legacy_ram_write(dev, 0x20 + i, tmp);
554 b43legacy_ram_write(dev, 0x78 + i, tmp);
555 b43legacy_ram_write(dev, 0x478 + i, tmp);
556 }
557}
558
Johannes Berg4150c572007-09-17 01:29:23 -0400559static void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
Larry Finger75388ac2007-09-25 16:46:54 -0700560{
Larry Finger75388ac2007-09-25 16:46:54 -0700561 b43legacy_write_mac_bssid_templates(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400562 b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
563 dev->wl->mac_addr);
Larry Finger75388ac2007-09-25 16:46:54 -0700564}
565
566static void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
567 u16 slot_time)
568{
569 /* slot_time is in usec. */
570 if (dev->phy.type != B43legacy_PHYTYPE_G)
571 return;
572 b43legacy_write16(dev, 0x684, 510 + slot_time);
573 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
574 slot_time);
575}
576
577static void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
578{
579 b43legacy_set_slot_time(dev, 9);
580 dev->short_slot = 1;
581}
582
583static void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
584{
585 b43legacy_set_slot_time(dev, 20);
586 dev->short_slot = 0;
587}
588
589/* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
590 * Returns the _previously_ enabled IRQ mask.
591 */
592static inline u32 b43legacy_interrupt_enable(struct b43legacy_wldev *dev,
593 u32 mask)
594{
595 u32 old_mask;
596
597 old_mask = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
598 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, old_mask |
599 mask);
600
601 return old_mask;
602}
603
604/* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
605 * Returns the _previously_ enabled IRQ mask.
606 */
607static inline u32 b43legacy_interrupt_disable(struct b43legacy_wldev *dev,
608 u32 mask)
609{
610 u32 old_mask;
611
612 old_mask = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
613 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
614
615 return old_mask;
616}
617
618/* Synchronize IRQ top- and bottom-half.
619 * IRQs must be masked before calling this.
620 * This must not be called with the irq_lock held.
621 */
622static void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
623{
624 synchronize_irq(dev->dev->irq);
625 tasklet_kill(&dev->isr_tasklet);
626}
627
628/* DummyTransmission function, as documented on
629 * http://bcm-specs.sipsolutions.net/DummyTransmission
630 */
631void b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
632{
633 struct b43legacy_phy *phy = &dev->phy;
634 unsigned int i;
635 unsigned int max_loop;
636 u16 value;
637 u32 buffer[5] = {
638 0x00000000,
639 0x00D40000,
640 0x00000000,
641 0x01000000,
642 0x00000000,
643 };
644
645 switch (phy->type) {
646 case B43legacy_PHYTYPE_B:
647 case B43legacy_PHYTYPE_G:
648 max_loop = 0xFA;
649 buffer[0] = 0x000B846E;
650 break;
651 default:
652 B43legacy_BUG_ON(1);
653 return;
654 }
655
656 for (i = 0; i < 5; i++)
657 b43legacy_ram_write(dev, i * 4, buffer[i]);
658
659 /* dummy read follows */
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100660 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
Larry Finger75388ac2007-09-25 16:46:54 -0700661
662 b43legacy_write16(dev, 0x0568, 0x0000);
663 b43legacy_write16(dev, 0x07C0, 0x0000);
664 b43legacy_write16(dev, 0x050C, 0x0000);
665 b43legacy_write16(dev, 0x0508, 0x0000);
666 b43legacy_write16(dev, 0x050A, 0x0000);
667 b43legacy_write16(dev, 0x054C, 0x0000);
668 b43legacy_write16(dev, 0x056A, 0x0014);
669 b43legacy_write16(dev, 0x0568, 0x0826);
670 b43legacy_write16(dev, 0x0500, 0x0000);
671 b43legacy_write16(dev, 0x0502, 0x0030);
672
673 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
674 b43legacy_radio_write16(dev, 0x0051, 0x0017);
675 for (i = 0x00; i < max_loop; i++) {
676 value = b43legacy_read16(dev, 0x050E);
677 if (value & 0x0080)
678 break;
679 udelay(10);
680 }
681 for (i = 0x00; i < 0x0A; i++) {
682 value = b43legacy_read16(dev, 0x050E);
683 if (value & 0x0400)
684 break;
685 udelay(10);
686 }
687 for (i = 0x00; i < 0x0A; i++) {
688 value = b43legacy_read16(dev, 0x0690);
689 if (!(value & 0x0100))
690 break;
691 udelay(10);
692 }
693 if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
694 b43legacy_radio_write16(dev, 0x0051, 0x0037);
695}
696
697/* Turn the Analog ON/OFF */
698static void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
699{
700 b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
701}
702
703void b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
704{
705 u32 tmslow;
706 u32 macctl;
707
708 flags |= B43legacy_TMSLOW_PHYCLKEN;
709 flags |= B43legacy_TMSLOW_PHYRESET;
710 ssb_device_enable(dev->dev, flags);
711 msleep(2); /* Wait for the PLL to turn on. */
712
713 /* Now take the PHY out of Reset again */
714 tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
715 tmslow |= SSB_TMSLOW_FGC;
716 tmslow &= ~B43legacy_TMSLOW_PHYRESET;
717 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
718 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
719 msleep(1);
720 tmslow &= ~SSB_TMSLOW_FGC;
721 ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
722 ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
723 msleep(1);
724
725 /* Turn Analog ON */
726 b43legacy_switch_analog(dev, 1);
727
728 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
729 macctl &= ~B43legacy_MACCTL_GMODE;
730 if (flags & B43legacy_TMSLOW_GMODE) {
731 macctl |= B43legacy_MACCTL_GMODE;
732 dev->phy.gmode = 1;
733 } else
734 dev->phy.gmode = 0;
735 macctl |= B43legacy_MACCTL_IHR_ENABLED;
736 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
737}
738
739static void handle_irq_transmit_status(struct b43legacy_wldev *dev)
740{
741 u32 v0;
742 u32 v1;
743 u16 tmp;
744 struct b43legacy_txstatus stat;
745
746 while (1) {
747 v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
748 if (!(v0 & 0x00000001))
749 break;
750 v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
751
752 stat.cookie = (v0 >> 16);
753 stat.seq = (v1 & 0x0000FFFF);
754 stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
755 tmp = (v0 & 0x0000FFFF);
756 stat.frame_count = ((tmp & 0xF000) >> 12);
757 stat.rts_count = ((tmp & 0x0F00) >> 8);
758 stat.supp_reason = ((tmp & 0x001C) >> 2);
759 stat.pm_indicated = !!(tmp & 0x0080);
760 stat.intermediate = !!(tmp & 0x0040);
761 stat.for_ampdu = !!(tmp & 0x0020);
762 stat.acked = !!(tmp & 0x0002);
763
764 b43legacy_handle_txstatus(dev, &stat);
765 }
766}
767
768static void drain_txstatus_queue(struct b43legacy_wldev *dev)
769{
770 u32 dummy;
771
772 if (dev->dev->id.revision < 5)
773 return;
774 /* Read all entries from the microcode TXstatus FIFO
775 * and throw them away.
776 */
777 while (1) {
778 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
779 if (!(dummy & 0x00000001))
780 break;
781 dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
782 }
783}
784
785static u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
786{
787 u32 val = 0;
788
789 val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
790 val <<= 16;
791 val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
792
793 return val;
794}
795
796static void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
797{
798 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
799 (jssi & 0x0000FFFF));
800 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
801 (jssi & 0xFFFF0000) >> 16);
802}
803
804static void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
805{
806 b43legacy_jssi_write(dev, 0x7F7F7F7F);
Stefano Brivioe78c9d22008-01-23 14:48:50 +0100807 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100808 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
809 | B43legacy_MACCMD_BGNOISE);
Larry Finger75388ac2007-09-25 16:46:54 -0700810 B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
811 dev->phy.channel);
812}
813
814static void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
815{
816 /* Top half of Link Quality calculation. */
817
818 if (dev->noisecalc.calculation_running)
819 return;
820 dev->noisecalc.channel_at_start = dev->phy.channel;
821 dev->noisecalc.calculation_running = 1;
822 dev->noisecalc.nr_samples = 0;
823
824 b43legacy_generate_noise_sample(dev);
825}
826
827static void handle_irq_noise(struct b43legacy_wldev *dev)
828{
829 struct b43legacy_phy *phy = &dev->phy;
830 u16 tmp;
831 u8 noise[4];
832 u8 i;
833 u8 j;
834 s32 average;
835
836 /* Bottom half of Link Quality calculation. */
837
838 B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
839 if (dev->noisecalc.channel_at_start != phy->channel)
840 goto drop_calculation;
841 *((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
842 if (noise[0] == 0x7F || noise[1] == 0x7F ||
843 noise[2] == 0x7F || noise[3] == 0x7F)
844 goto generate_new;
845
846 /* Get the noise samples. */
847 B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
848 i = dev->noisecalc.nr_samples;
849 noise[0] = limit_value(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
850 noise[1] = limit_value(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
851 noise[2] = limit_value(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
852 noise[3] = limit_value(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
853 dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
854 dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
855 dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
856 dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
857 dev->noisecalc.nr_samples++;
858 if (dev->noisecalc.nr_samples == 8) {
859 /* Calculate the Link Quality by the noise samples. */
860 average = 0;
861 for (i = 0; i < 8; i++) {
862 for (j = 0; j < 4; j++)
863 average += dev->noisecalc.samples[i][j];
864 }
865 average /= (8 * 4);
866 average *= 125;
867 average += 64;
868 average /= 128;
869 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
870 0x40C);
871 tmp = (tmp / 128) & 0x1F;
872 if (tmp >= 8)
873 average += 2;
874 else
875 average -= 25;
876 if (tmp == 8)
877 average -= 72;
878 else
879 average -= 48;
880
881 dev->stats.link_noise = average;
882drop_calculation:
883 dev->noisecalc.calculation_running = 0;
884 return;
885 }
886generate_new:
887 b43legacy_generate_noise_sample(dev);
888}
889
890static void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
891{
892 if (b43legacy_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
893 /* TODO: PS TBTT */
894 } else {
895 if (1/*FIXME: the last PSpoll frame was sent successfully */)
896 b43legacy_power_saving_ctl_bits(dev, -1, -1);
897 }
Larry Finger75388ac2007-09-25 16:46:54 -0700898 if (b43legacy_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100899 dev->dfq_valid = 1;
Larry Finger75388ac2007-09-25 16:46:54 -0700900}
901
902static void handle_irq_atim_end(struct b43legacy_wldev *dev)
903{
Stefano Brivioeed0fd22008-02-08 06:31:10 +0100904 if (dev->dfq_valid) {
905 b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
906 b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
907 | B43legacy_MACCMD_DFQ_VALID);
908 dev->dfq_valid = 0;
909 }
Larry Finger75388ac2007-09-25 16:46:54 -0700910}
911
912static void handle_irq_pmq(struct b43legacy_wldev *dev)
913{
914 u32 tmp;
915
916 /* TODO: AP mode. */
917
918 while (1) {
919 tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
920 if (!(tmp & 0x00000008))
921 break;
922 }
923 /* 16bit write is odd, but correct. */
924 b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
925}
926
927static void b43legacy_write_template_common(struct b43legacy_wldev *dev,
928 const u8 *data, u16 size,
929 u16 ram_offset,
930 u16 shm_size_offset, u8 rate)
931{
932 u32 i;
933 u32 tmp;
934 struct b43legacy_plcp_hdr4 plcp;
935
936 plcp.data = 0;
937 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
938 b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
939 ram_offset += sizeof(u32);
940 /* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
941 * So leave the first two bytes of the next write blank.
942 */
943 tmp = (u32)(data[0]) << 16;
944 tmp |= (u32)(data[1]) << 24;
945 b43legacy_ram_write(dev, ram_offset, tmp);
946 ram_offset += sizeof(u32);
947 for (i = 2; i < size; i += sizeof(u32)) {
948 tmp = (u32)(data[i + 0]);
949 if (i + 1 < size)
950 tmp |= (u32)(data[i + 1]) << 8;
951 if (i + 2 < size)
952 tmp |= (u32)(data[i + 2]) << 16;
953 if (i + 3 < size)
954 tmp |= (u32)(data[i + 3]) << 24;
955 b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
956 }
957 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
958 size + sizeof(struct b43legacy_plcp_hdr6));
959}
960
961static void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
962 u16 ram_offset,
963 u16 shm_size_offset, u8 rate)
964{
Larry Finger75388ac2007-09-25 16:46:54 -0700965
Stefano Brivioa2971702008-02-08 06:31:25 +0100966 unsigned int i, len, variable_len;
967 const struct ieee80211_mgmt *bcn;
968 const u8 *ie;
969 bool tim_found = 0;
970
971 bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
972 len = min((size_t)dev->wl->current_beacon->len,
Larry Finger75388ac2007-09-25 16:46:54 -0700973 0x200 - sizeof(struct b43legacy_plcp_hdr6));
Stefano Brivioa2971702008-02-08 06:31:25 +0100974
975 b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
Larry Finger75388ac2007-09-25 16:46:54 -0700976 shm_size_offset, rate);
Stefano Brivioa2971702008-02-08 06:31:25 +0100977
978 /* Find the position of the TIM and the DTIM_period value
979 * and write them to SHM. */
980 ie = bcn->u.beacon.variable;
981 variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
982 for (i = 0; i < variable_len - 2; ) {
983 uint8_t ie_id, ie_len;
984
985 ie_id = ie[i];
986 ie_len = ie[i + 1];
987 if (ie_id == 5) {
988 u16 tim_position;
989 u16 dtim_period;
990 /* This is the TIM Information Element */
991
992 /* Check whether the ie_len is in the beacon data range. */
993 if (variable_len < ie_len + 2 + i)
994 break;
995 /* A valid TIM is at least 4 bytes long. */
996 if (ie_len < 4)
997 break;
998 tim_found = 1;
999
1000 tim_position = sizeof(struct b43legacy_plcp_hdr6);
1001 tim_position += offsetof(struct ieee80211_mgmt,
1002 u.beacon.variable);
1003 tim_position += i;
1004
1005 dtim_period = ie[i + 3];
1006
1007 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1008 B43legacy_SHM_SH_TIMPOS, tim_position);
1009 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1010 B43legacy_SHM_SH_DTIMP, dtim_period);
1011 break;
1012 }
1013 i += ie_len + 2;
1014 }
1015 if (!tim_found) {
1016 b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
1017 "beacon template packet. AP or IBSS operation "
1018 "may be broken.\n");
1019 }
Larry Finger75388ac2007-09-25 16:46:54 -07001020}
1021
1022static void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
1023 u16 shm_offset, u16 size,
Johannes Berg8318d782008-01-24 19:38:38 +01001024 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001025{
1026 struct b43legacy_plcp_hdr4 plcp;
1027 u32 tmp;
1028 __le16 dur;
1029
1030 plcp.data = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001031 b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->bitrate);
Larry Finger75388ac2007-09-25 16:46:54 -07001032 dur = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01001033 dev->wl->vif,
Larry Finger75388ac2007-09-25 16:46:54 -07001034 size,
Johannes Berg8318d782008-01-24 19:38:38 +01001035 rate);
Larry Finger75388ac2007-09-25 16:46:54 -07001036 /* Write PLCP in two parts and timing for packet transfer */
1037 tmp = le32_to_cpu(plcp.data);
1038 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
1039 tmp & 0xFFFF);
1040 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
1041 tmp >> 16);
1042 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
1043 le16_to_cpu(dur));
1044}
1045
1046/* Instead of using custom probe response template, this function
1047 * just patches custom beacon template by:
1048 * 1) Changing packet type
1049 * 2) Patching duration field
1050 * 3) Stripping TIM
1051 */
Stefano Brivioa2971702008-02-08 06:31:25 +01001052static const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
1053 u16 *dest_size,
1054 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001055{
1056 const u8 *src_data;
1057 u8 *dest_data;
Stefano Brivioa2971702008-02-08 06:31:25 +01001058 u16 src_size, elem_size, src_pos, dest_pos;
Larry Finger75388ac2007-09-25 16:46:54 -07001059 __le16 dur;
1060 struct ieee80211_hdr *hdr;
Stefano Brivioa2971702008-02-08 06:31:25 +01001061 size_t ie_start;
Larry Finger75388ac2007-09-25 16:46:54 -07001062
Stefano Brivioa2971702008-02-08 06:31:25 +01001063 src_size = dev->wl->current_beacon->len;
1064 src_data = (const u8 *)dev->wl->current_beacon->data;
Larry Finger75388ac2007-09-25 16:46:54 -07001065
Stefano Brivioa2971702008-02-08 06:31:25 +01001066 /* Get the start offset of the variable IEs in the packet. */
1067 ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
1068 B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
1069 u.beacon.variable));
1070
Stefano Brivio4688be32008-02-08 06:31:39 +01001071 if (B43legacy_WARN_ON(src_size < ie_start))
Larry Finger75388ac2007-09-25 16:46:54 -07001072 return NULL;
Larry Finger75388ac2007-09-25 16:46:54 -07001073
1074 dest_data = kmalloc(src_size, GFP_ATOMIC);
1075 if (unlikely(!dest_data))
1076 return NULL;
1077
Stefano Brivioa2971702008-02-08 06:31:25 +01001078 /* Copy the static data and all Information Elements, except the TIM. */
1079 memcpy(dest_data, src_data, ie_start);
1080 src_pos = ie_start;
1081 dest_pos = ie_start;
1082 for ( ; src_pos < src_size - 2; src_pos += elem_size) {
Larry Finger75388ac2007-09-25 16:46:54 -07001083 elem_size = src_data[src_pos + 1] + 2;
Stefano Brivioa2971702008-02-08 06:31:25 +01001084 if (src_data[src_pos] == 5) {
1085 /* This is the TIM. */
1086 continue;
Larry Finger75388ac2007-09-25 16:46:54 -07001087 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001088 memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
1089 dest_pos += elem_size;
Larry Finger75388ac2007-09-25 16:46:54 -07001090 }
1091 *dest_size = dest_pos;
1092 hdr = (struct ieee80211_hdr *)dest_data;
1093
1094 /* Set the frame control. */
1095 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1096 IEEE80211_STYPE_PROBE_RESP);
1097 dur = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01001098 dev->wl->vif,
Larry Finger75388ac2007-09-25 16:46:54 -07001099 *dest_size,
Johannes Berg8318d782008-01-24 19:38:38 +01001100 rate);
Larry Finger75388ac2007-09-25 16:46:54 -07001101 hdr->duration_id = dur;
1102
1103 return dest_data;
1104}
1105
1106static void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
1107 u16 ram_offset,
Johannes Berg8318d782008-01-24 19:38:38 +01001108 u16 shm_size_offset,
1109 struct ieee80211_rate *rate)
Larry Finger75388ac2007-09-25 16:46:54 -07001110{
Stefano Brivioa2971702008-02-08 06:31:25 +01001111 const u8 *probe_resp_data;
Larry Finger75388ac2007-09-25 16:46:54 -07001112 u16 size;
1113
Stefano Brivioa2971702008-02-08 06:31:25 +01001114 size = dev->wl->current_beacon->len;
Larry Finger75388ac2007-09-25 16:46:54 -07001115 probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
1116 if (unlikely(!probe_resp_data))
1117 return;
1118
1119 /* Looks like PLCP headers plus packet timings are stored for
1120 * all possible basic rates
1121 */
1122 b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001123 &b43legacy_b_ratetable[0]);
Larry Finger75388ac2007-09-25 16:46:54 -07001124 b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001125 &b43legacy_b_ratetable[1]);
Larry Finger75388ac2007-09-25 16:46:54 -07001126 b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001127 &b43legacy_b_ratetable[2]);
Larry Finger75388ac2007-09-25 16:46:54 -07001128 b43legacy_write_probe_resp_plcp(dev, 0x350, size,
Johannes Berg8318d782008-01-24 19:38:38 +01001129 &b43legacy_b_ratetable[3]);
Larry Finger75388ac2007-09-25 16:46:54 -07001130
1131 size = min((size_t)size,
1132 0x200 - sizeof(struct b43legacy_plcp_hdr6));
1133 b43legacy_write_template_common(dev, probe_resp_data,
1134 size, ram_offset,
Johannes Berg8318d782008-01-24 19:38:38 +01001135 shm_size_offset, rate->bitrate);
Larry Finger75388ac2007-09-25 16:46:54 -07001136 kfree(probe_resp_data);
1137}
1138
Stefano Brivioa2971702008-02-08 06:31:25 +01001139/* Asynchronously update the packet templates in template RAM.
1140 * Locking: Requires wl->irq_lock to be locked. */
1141static void b43legacy_update_templates(struct b43legacy_wl *wl,
1142 struct sk_buff *beacon)
Larry Finger75388ac2007-09-25 16:46:54 -07001143{
Stefano Brivioa2971702008-02-08 06:31:25 +01001144 /* This is the top half of the ansynchronous beacon update. The bottom
1145 * half is the beacon IRQ. Beacon update must be asynchronous to avoid
1146 * sending an invalid beacon. This can happen for example, if the
1147 * firmware transmits a beacon while we are updating it. */
Larry Finger75388ac2007-09-25 16:46:54 -07001148
Stefano Brivioa2971702008-02-08 06:31:25 +01001149 if (wl->current_beacon)
1150 dev_kfree_skb_any(wl->current_beacon);
1151 wl->current_beacon = beacon;
1152 wl->beacon0_uploaded = 0;
1153 wl->beacon1_uploaded = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07001154}
1155
1156static void b43legacy_set_ssid(struct b43legacy_wldev *dev,
1157 const u8 *ssid, u8 ssid_len)
1158{
1159 u32 tmp;
1160 u16 i;
1161 u16 len;
1162
1163 len = min((u16)ssid_len, (u16)0x100);
1164 for (i = 0; i < len; i += sizeof(u32)) {
1165 tmp = (u32)(ssid[i + 0]);
1166 if (i + 1 < len)
1167 tmp |= (u32)(ssid[i + 1]) << 8;
1168 if (i + 2 < len)
1169 tmp |= (u32)(ssid[i + 2]) << 16;
1170 if (i + 3 < len)
1171 tmp |= (u32)(ssid[i + 3]) << 24;
1172 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
1173 0x380 + i, tmp);
1174 }
1175 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
1176 0x48, len);
1177}
1178
1179static void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
1180 u16 beacon_int)
1181{
1182 b43legacy_time_lock(dev);
1183 if (dev->dev->id.revision >= 3)
1184 b43legacy_write32(dev, 0x188, (beacon_int << 16));
1185 else {
1186 b43legacy_write16(dev, 0x606, (beacon_int >> 6));
1187 b43legacy_write16(dev, 0x610, beacon_int);
1188 }
1189 b43legacy_time_unlock(dev);
1190}
1191
1192static void handle_irq_beacon(struct b43legacy_wldev *dev)
1193{
Stefano Brivioa2971702008-02-08 06:31:25 +01001194 struct b43legacy_wl *wl = dev->wl;
1195 u32 cmd;
Larry Finger75388ac2007-09-25 16:46:54 -07001196
Stefano Brivioa2971702008-02-08 06:31:25 +01001197 if (!b43legacy_is_mode(wl, IEEE80211_IF_TYPE_AP))
Larry Finger75388ac2007-09-25 16:46:54 -07001198 return;
1199
Stefano Brivioa2971702008-02-08 06:31:25 +01001200 /* This is the bottom half of the asynchronous beacon update. */
Larry Finger75388ac2007-09-25 16:46:54 -07001201
Stefano Brivioa2971702008-02-08 06:31:25 +01001202 cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
1203 if (!(cmd & B43legacy_MACCMD_BEACON0_VALID)) {
1204 if (!wl->beacon0_uploaded) {
1205 b43legacy_write_beacon_template(dev, 0x68,
1206 B43legacy_SHM_SH_BTL0,
1207 B43legacy_CCK_RATE_1MB);
1208 b43legacy_write_probe_resp_template(dev, 0x268,
1209 B43legacy_SHM_SH_PRTLEN,
1210 &__b43legacy_ratetable[3]);
1211 wl->beacon0_uploaded = 1;
1212 }
1213 cmd |= B43legacy_MACCMD_BEACON0_VALID;
Larry Finger75388ac2007-09-25 16:46:54 -07001214 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001215 if (!(cmd & B43legacy_MACCMD_BEACON1_VALID)) {
1216 if (!wl->beacon1_uploaded) {
1217 b43legacy_write_beacon_template(dev, 0x468,
1218 B43legacy_SHM_SH_BTL1,
1219 B43legacy_CCK_RATE_1MB);
1220 wl->beacon1_uploaded = 1;
1221 }
1222 cmd |= B43legacy_MACCMD_BEACON1_VALID;
Larry Finger75388ac2007-09-25 16:46:54 -07001223 }
Stefano Brivioa2971702008-02-08 06:31:25 +01001224 b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
Larry Finger75388ac2007-09-25 16:46:54 -07001225}
1226
1227static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
1228{
1229}
1230
1231/* Interrupt handler bottom-half */
1232static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
1233{
1234 u32 reason;
1235 u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
1236 u32 merged_dma_reason = 0;
1237 int i;
Larry Finger75388ac2007-09-25 16:46:54 -07001238 unsigned long flags;
1239
1240 spin_lock_irqsave(&dev->wl->irq_lock, flags);
1241
1242 B43legacy_WARN_ON(b43legacy_status(dev) <
1243 B43legacy_STAT_INITIALIZED);
1244
1245 reason = dev->irq_reason;
1246 for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
1247 dma_reason[i] = dev->dma_reason[i];
1248 merged_dma_reason |= dma_reason[i];
1249 }
1250
1251 if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
1252 b43legacyerr(dev->wl, "MAC transmission error\n");
1253
Stefano Brivioa293ee92007-11-24 23:35:25 +01001254 if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
Larry Finger75388ac2007-09-25 16:46:54 -07001255 b43legacyerr(dev->wl, "PHY transmission error\n");
Stefano Brivioa293ee92007-11-24 23:35:25 +01001256 rmb();
1257 if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
1258 b43legacyerr(dev->wl, "Too many PHY TX errors, "
1259 "restarting the controller\n");
1260 b43legacy_controller_restart(dev, "PHY TX errors");
1261 }
1262 }
Larry Finger75388ac2007-09-25 16:46:54 -07001263
1264 if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
1265 B43legacy_DMAIRQ_NONFATALMASK))) {
1266 if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
1267 b43legacyerr(dev->wl, "Fatal DMA error: "
1268 "0x%08X, 0x%08X, 0x%08X, "
1269 "0x%08X, 0x%08X, 0x%08X\n",
1270 dma_reason[0], dma_reason[1],
1271 dma_reason[2], dma_reason[3],
1272 dma_reason[4], dma_reason[5]);
1273 b43legacy_controller_restart(dev, "DMA error");
1274 mmiowb();
1275 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1276 return;
1277 }
1278 if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
1279 b43legacyerr(dev->wl, "DMA error: "
1280 "0x%08X, 0x%08X, 0x%08X, "
1281 "0x%08X, 0x%08X, 0x%08X\n",
1282 dma_reason[0], dma_reason[1],
1283 dma_reason[2], dma_reason[3],
1284 dma_reason[4], dma_reason[5]);
1285 }
1286
1287 if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
1288 handle_irq_ucode_debug(dev);
1289 if (reason & B43legacy_IRQ_TBTT_INDI)
1290 handle_irq_tbtt_indication(dev);
1291 if (reason & B43legacy_IRQ_ATIM_END)
1292 handle_irq_atim_end(dev);
1293 if (reason & B43legacy_IRQ_BEACON)
1294 handle_irq_beacon(dev);
1295 if (reason & B43legacy_IRQ_PMQ)
1296 handle_irq_pmq(dev);
1297 if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK)
1298 ;/*TODO*/
1299 if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
1300 handle_irq_noise(dev);
1301
1302 /* Check the DMA reason registers for received data. */
1303 if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
1304 if (b43legacy_using_pio(dev))
1305 b43legacy_pio_rx(dev->pio.queue0);
1306 else
1307 b43legacy_dma_rx(dev->dma.rx_ring0);
Larry Finger75388ac2007-09-25 16:46:54 -07001308 }
1309 B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
1310 B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
1311 if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
1312 if (b43legacy_using_pio(dev))
1313 b43legacy_pio_rx(dev->pio.queue3);
1314 else
1315 b43legacy_dma_rx(dev->dma.rx_ring3);
Larry Finger75388ac2007-09-25 16:46:54 -07001316 }
1317 B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
1318 B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
1319
Larry Fingerba48f7b2007-10-12 23:04:51 -05001320 if (reason & B43legacy_IRQ_TX_OK)
Larry Finger75388ac2007-09-25 16:46:54 -07001321 handle_irq_transmit_status(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07001322
Larry Finger75388ac2007-09-25 16:46:54 -07001323 b43legacy_interrupt_enable(dev, dev->irq_savedstate);
1324 mmiowb();
1325 spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
1326}
1327
1328static void pio_irq_workaround(struct b43legacy_wldev *dev,
1329 u16 base, int queueidx)
1330{
1331 u16 rxctl;
1332
1333 rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
1334 if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
1335 dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
1336 else
1337 dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
1338}
1339
1340static void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
1341{
1342 if (b43legacy_using_pio(dev) &&
1343 (dev->dev->id.revision < 3) &&
1344 (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
1345 /* Apply a PIO specific workaround to the dma_reasons */
1346 pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
1347 pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
1348 pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
1349 pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
1350 }
1351
1352 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
1353
1354 b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
1355 dev->dma_reason[0]);
1356 b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
1357 dev->dma_reason[1]);
1358 b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
1359 dev->dma_reason[2]);
1360 b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
1361 dev->dma_reason[3]);
1362 b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
1363 dev->dma_reason[4]);
1364 b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
1365 dev->dma_reason[5]);
1366}
1367
1368/* Interrupt handler top-half */
1369static irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
1370{
1371 irqreturn_t ret = IRQ_NONE;
1372 struct b43legacy_wldev *dev = dev_id;
1373 u32 reason;
1374
1375 if (!dev)
1376 return IRQ_NONE;
1377
1378 spin_lock(&dev->wl->irq_lock);
1379
1380 if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
1381 goto out;
1382 reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1383 if (reason == 0xffffffff) /* shared IRQ */
1384 goto out;
1385 ret = IRQ_HANDLED;
1386 reason &= b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK);
1387 if (!reason)
1388 goto out;
1389
1390 dev->dma_reason[0] = b43legacy_read32(dev,
1391 B43legacy_MMIO_DMA0_REASON)
1392 & 0x0001DC00;
1393 dev->dma_reason[1] = b43legacy_read32(dev,
1394 B43legacy_MMIO_DMA1_REASON)
1395 & 0x0000DC00;
1396 dev->dma_reason[2] = b43legacy_read32(dev,
1397 B43legacy_MMIO_DMA2_REASON)
1398 & 0x0000DC00;
1399 dev->dma_reason[3] = b43legacy_read32(dev,
1400 B43legacy_MMIO_DMA3_REASON)
1401 & 0x0001DC00;
1402 dev->dma_reason[4] = b43legacy_read32(dev,
1403 B43legacy_MMIO_DMA4_REASON)
1404 & 0x0000DC00;
1405 dev->dma_reason[5] = b43legacy_read32(dev,
1406 B43legacy_MMIO_DMA5_REASON)
1407 & 0x0000DC00;
1408
1409 b43legacy_interrupt_ack(dev, reason);
1410 /* disable all IRQs. They are enabled again in the bottom half. */
1411 dev->irq_savedstate = b43legacy_interrupt_disable(dev,
1412 B43legacy_IRQ_ALL);
1413 /* save the reason code and call our bottom half. */
1414 dev->irq_reason = reason;
1415 tasklet_schedule(&dev->isr_tasklet);
1416out:
1417 mmiowb();
1418 spin_unlock(&dev->wl->irq_lock);
1419
1420 return ret;
1421}
1422
1423static void b43legacy_release_firmware(struct b43legacy_wldev *dev)
1424{
1425 release_firmware(dev->fw.ucode);
1426 dev->fw.ucode = NULL;
1427 release_firmware(dev->fw.pcm);
1428 dev->fw.pcm = NULL;
1429 release_firmware(dev->fw.initvals);
1430 dev->fw.initvals = NULL;
1431 release_firmware(dev->fw.initvals_band);
1432 dev->fw.initvals_band = NULL;
1433}
1434
1435static void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
1436{
1437 b43legacyerr(wl, "You must go to http://linuxwireless.org/en/users/"
Stefano Brivio354807e2007-11-19 20:21:31 +01001438 "Drivers/b43#devicefirmware "
Larry Finger75388ac2007-09-25 16:46:54 -07001439 "and download the correct firmware (version 3).\n");
1440}
1441
1442static int do_request_fw(struct b43legacy_wldev *dev,
1443 const char *name,
1444 const struct firmware **fw)
1445{
1446 char path[sizeof(modparam_fwpostfix) + 32];
1447 struct b43legacy_fw_header *hdr;
1448 u32 size;
1449 int err;
1450
1451 if (!name)
1452 return 0;
1453
1454 snprintf(path, ARRAY_SIZE(path),
1455 "b43legacy%s/%s.fw",
1456 modparam_fwpostfix, name);
1457 err = request_firmware(fw, path, dev->dev->dev);
1458 if (err) {
1459 b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
1460 "or load failed.\n", path);
1461 return err;
1462 }
1463 if ((*fw)->size < sizeof(struct b43legacy_fw_header))
1464 goto err_format;
1465 hdr = (struct b43legacy_fw_header *)((*fw)->data);
1466 switch (hdr->type) {
1467 case B43legacy_FW_TYPE_UCODE:
1468 case B43legacy_FW_TYPE_PCM:
1469 size = be32_to_cpu(hdr->size);
1470 if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
1471 goto err_format;
1472 /* fallthrough */
1473 case B43legacy_FW_TYPE_IV:
1474 if (hdr->ver != 1)
1475 goto err_format;
1476 break;
1477 default:
1478 goto err_format;
1479 }
1480
1481 return err;
1482
1483err_format:
1484 b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
1485 return -EPROTO;
1486}
1487
1488static int b43legacy_request_firmware(struct b43legacy_wldev *dev)
1489{
1490 struct b43legacy_firmware *fw = &dev->fw;
1491 const u8 rev = dev->dev->id.revision;
1492 const char *filename;
1493 u32 tmshigh;
1494 int err;
1495
1496 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
1497 if (!fw->ucode) {
1498 if (rev == 2)
1499 filename = "ucode2";
1500 else if (rev == 4)
1501 filename = "ucode4";
1502 else
1503 filename = "ucode5";
1504 err = do_request_fw(dev, filename, &fw->ucode);
1505 if (err)
1506 goto err_load;
1507 }
1508 if (!fw->pcm) {
1509 if (rev < 5)
1510 filename = "pcm4";
1511 else
1512 filename = "pcm5";
1513 err = do_request_fw(dev, filename, &fw->pcm);
1514 if (err)
1515 goto err_load;
1516 }
1517 if (!fw->initvals) {
1518 switch (dev->phy.type) {
1519 case B43legacy_PHYTYPE_G:
1520 if ((rev >= 5) && (rev <= 10))
1521 filename = "b0g0initvals5";
1522 else if (rev == 2 || rev == 4)
1523 filename = "b0g0initvals2";
1524 else
1525 goto err_no_initvals;
1526 break;
1527 default:
1528 goto err_no_initvals;
1529 }
1530 err = do_request_fw(dev, filename, &fw->initvals);
1531 if (err)
1532 goto err_load;
1533 }
1534 if (!fw->initvals_band) {
1535 switch (dev->phy.type) {
1536 case B43legacy_PHYTYPE_G:
1537 if ((rev >= 5) && (rev <= 10))
1538 filename = "b0g0bsinitvals5";
1539 else if (rev >= 11)
1540 filename = NULL;
1541 else if (rev == 2 || rev == 4)
1542 filename = NULL;
1543 else
1544 goto err_no_initvals;
1545 break;
1546 default:
1547 goto err_no_initvals;
1548 }
1549 err = do_request_fw(dev, filename, &fw->initvals_band);
1550 if (err)
1551 goto err_load;
1552 }
1553
1554 return 0;
1555
1556err_load:
1557 b43legacy_print_fw_helptext(dev->wl);
1558 goto error;
1559
1560err_no_initvals:
1561 err = -ENODEV;
1562 b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
1563 "core rev %u\n", dev->phy.type, rev);
1564 goto error;
1565
1566error:
1567 b43legacy_release_firmware(dev);
1568 return err;
1569}
1570
1571static int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
1572{
1573 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1574 const __be32 *data;
1575 unsigned int i;
1576 unsigned int len;
1577 u16 fwrev;
1578 u16 fwpatch;
1579 u16 fwdate;
1580 u16 fwtime;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001581 u32 tmp, macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07001582 int err = 0;
1583
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001584 /* Jump the microcode PSM to offset 0 */
1585 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1586 B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
1587 macctl |= B43legacy_MACCTL_PSM_JMP0;
1588 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1589 /* Zero out all microcode PSM registers and shared memory. */
1590 for (i = 0; i < 64; i++)
1591 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
1592 for (i = 0; i < 4096; i += 2)
1593 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
1594
Larry Finger75388ac2007-09-25 16:46:54 -07001595 /* Upload Microcode. */
1596 data = (__be32 *) (dev->fw.ucode->data + hdr_len);
1597 len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
1598 b43legacy_shm_control_word(dev,
1599 B43legacy_SHM_UCODE |
1600 B43legacy_SHM_AUTOINC_W,
1601 0x0000);
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 if (dev->fw.pcm) {
1609 /* Upload PCM data. */
1610 data = (__be32 *) (dev->fw.pcm->data + hdr_len);
1611 len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
1612 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
1613 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
1614 /* No need for autoinc bit in SHM_HW */
1615 b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
1616 for (i = 0; i < len; i++) {
1617 b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
1618 be32_to_cpu(data[i]));
1619 udelay(10);
1620 }
1621 }
1622
1623 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1624 B43legacy_IRQ_ALL);
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001625
1626 /* Start the microcode PSM */
1627 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1628 macctl &= ~B43legacy_MACCTL_PSM_JMP0;
1629 macctl |= B43legacy_MACCTL_PSM_RUN;
1630 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
Larry Finger75388ac2007-09-25 16:46:54 -07001631
1632 /* Wait for the microcode to load and respond */
1633 i = 0;
1634 while (1) {
1635 tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1636 if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
1637 break;
1638 i++;
1639 if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
1640 b43legacyerr(dev->wl, "Microcode not responding\n");
1641 b43legacy_print_fw_helptext(dev->wl);
1642 err = -ENODEV;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001643 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001644 }
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001645 msleep_interruptible(50);
1646 if (signal_pending(current)) {
1647 err = -EINTR;
1648 goto error;
1649 }
Larry Finger75388ac2007-09-25 16:46:54 -07001650 }
1651 /* dummy read follows */
1652 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1653
1654 /* Get and check the revisions. */
1655 fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1656 B43legacy_SHM_SH_UCODEREV);
1657 fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1658 B43legacy_SHM_SH_UCODEPATCH);
1659 fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1660 B43legacy_SHM_SH_UCODEDATE);
1661 fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
1662 B43legacy_SHM_SH_UCODETIME);
1663
1664 if (fwrev > 0x128) {
1665 b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
1666 " Only firmware from binary drivers version 3.x"
1667 " is supported. You must change your firmware"
1668 " files.\n");
1669 b43legacy_print_fw_helptext(dev->wl);
Larry Finger75388ac2007-09-25 16:46:54 -07001670 err = -EOPNOTSUPP;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001671 goto error;
Larry Finger75388ac2007-09-25 16:46:54 -07001672 }
Stefano Briviocfbc35b2008-02-12 03:09:10 +01001673 b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
1674 "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
1675 (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
1676 (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
1677 fwtime & 0x1F);
Larry Finger75388ac2007-09-25 16:46:54 -07001678
1679 dev->fw.rev = fwrev;
1680 dev->fw.patch = fwpatch;
1681
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001682 return 0;
1683
1684error:
1685 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1686 macctl &= ~B43legacy_MACCTL_PSM_RUN;
1687 macctl |= B43legacy_MACCTL_PSM_JMP0;
1688 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
1689
Larry Finger75388ac2007-09-25 16:46:54 -07001690 return err;
1691}
1692
1693static int b43legacy_write_initvals(struct b43legacy_wldev *dev,
1694 const struct b43legacy_iv *ivals,
1695 size_t count,
1696 size_t array_size)
1697{
1698 const struct b43legacy_iv *iv;
1699 u16 offset;
1700 size_t i;
1701 bool bit32;
1702
1703 BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
1704 iv = ivals;
1705 for (i = 0; i < count; i++) {
1706 if (array_size < sizeof(iv->offset_size))
1707 goto err_format;
1708 array_size -= sizeof(iv->offset_size);
1709 offset = be16_to_cpu(iv->offset_size);
1710 bit32 = !!(offset & B43legacy_IV_32BIT);
1711 offset &= B43legacy_IV_OFFSET_MASK;
1712 if (offset >= 0x1000)
1713 goto err_format;
1714 if (bit32) {
1715 u32 value;
1716
1717 if (array_size < sizeof(iv->data.d32))
1718 goto err_format;
1719 array_size -= sizeof(iv->data.d32);
1720
1721 value = be32_to_cpu(get_unaligned(&iv->data.d32));
1722 b43legacy_write32(dev, offset, value);
1723
1724 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1725 sizeof(__be16) +
1726 sizeof(__be32));
1727 } else {
1728 u16 value;
1729
1730 if (array_size < sizeof(iv->data.d16))
1731 goto err_format;
1732 array_size -= sizeof(iv->data.d16);
1733
1734 value = be16_to_cpu(iv->data.d16);
1735 b43legacy_write16(dev, offset, value);
1736
1737 iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
1738 sizeof(__be16) +
1739 sizeof(__be16));
1740 }
1741 }
1742 if (array_size)
1743 goto err_format;
1744
1745 return 0;
1746
1747err_format:
1748 b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
1749 b43legacy_print_fw_helptext(dev->wl);
1750
1751 return -EPROTO;
1752}
1753
1754static int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
1755{
1756 const size_t hdr_len = sizeof(struct b43legacy_fw_header);
1757 const struct b43legacy_fw_header *hdr;
1758 struct b43legacy_firmware *fw = &dev->fw;
1759 const struct b43legacy_iv *ivals;
1760 size_t count;
1761 int err;
1762
1763 hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
1764 ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
1765 count = be32_to_cpu(hdr->size);
1766 err = b43legacy_write_initvals(dev, ivals, count,
1767 fw->initvals->size - hdr_len);
1768 if (err)
1769 goto out;
1770 if (fw->initvals_band) {
1771 hdr = (const struct b43legacy_fw_header *)
1772 (fw->initvals_band->data);
1773 ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
1774 + hdr_len);
1775 count = be32_to_cpu(hdr->size);
1776 err = b43legacy_write_initvals(dev, ivals, count,
1777 fw->initvals_band->size - hdr_len);
1778 if (err)
1779 goto out;
1780 }
1781out:
1782
1783 return err;
1784}
1785
1786/* Initialize the GPIOs
1787 * http://bcm-specs.sipsolutions.net/GPIO
1788 */
1789static int b43legacy_gpio_init(struct b43legacy_wldev *dev)
1790{
1791 struct ssb_bus *bus = dev->dev->bus;
1792 struct ssb_device *gpiodev, *pcidev = NULL;
1793 u32 mask;
1794 u32 set;
1795
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001796 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001797 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001798 B43legacy_MMIO_MACCTL)
Larry Finger75388ac2007-09-25 16:46:54 -07001799 & 0xFFFF3FFF);
1800
Larry Finger75388ac2007-09-25 16:46:54 -07001801 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1802 b43legacy_read16(dev,
1803 B43legacy_MMIO_GPIO_MASK)
1804 | 0x000F);
1805
1806 mask = 0x0000001F;
1807 set = 0x0000000F;
1808 if (dev->dev->bus->chip_id == 0x4301) {
1809 mask |= 0x0060;
1810 set |= 0x0060;
1811 }
Larry Finger7797aa32007-11-09 16:57:34 -06001812 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
Larry Finger75388ac2007-09-25 16:46:54 -07001813 b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
1814 b43legacy_read16(dev,
1815 B43legacy_MMIO_GPIO_MASK)
1816 | 0x0200);
1817 mask |= 0x0200;
1818 set |= 0x0200;
1819 }
1820 if (dev->dev->id.revision >= 2)
1821 mask |= 0x0010; /* FIXME: This is redundant. */
1822
1823#ifdef CONFIG_SSB_DRIVER_PCICORE
1824 pcidev = bus->pcicore.dev;
1825#endif
1826 gpiodev = bus->chipco.dev ? : pcidev;
1827 if (!gpiodev)
1828 return 0;
1829 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
1830 (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
1831 & mask) | set);
1832
1833 return 0;
1834}
1835
1836/* Turn off all GPIO stuff. Call this on module unload, for example. */
1837static void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
1838{
1839 struct ssb_bus *bus = dev->dev->bus;
1840 struct ssb_device *gpiodev, *pcidev = NULL;
1841
1842#ifdef CONFIG_SSB_DRIVER_PCICORE
1843 pcidev = bus->pcicore.dev;
1844#endif
1845 gpiodev = bus->chipco.dev ? : pcidev;
1846 if (!gpiodev)
1847 return;
1848 ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
1849}
1850
1851/* http://bcm-specs.sipsolutions.net/EnableMac */
1852void b43legacy_mac_enable(struct b43legacy_wldev *dev)
1853{
1854 dev->mac_suspended--;
1855 B43legacy_WARN_ON(dev->mac_suspended < 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001856 B43legacy_WARN_ON(irqs_disabled());
Larry Finger75388ac2007-09-25 16:46:54 -07001857 if (dev->mac_suspended == 0) {
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001858 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001859 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001860 B43legacy_MMIO_MACCTL)
1861 | B43legacy_MACCTL_ENABLED);
Larry Finger75388ac2007-09-25 16:46:54 -07001862 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
1863 B43legacy_IRQ_MAC_SUSPENDED);
1864 /* the next two are dummy reads */
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001865 b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
Larry Finger75388ac2007-09-25 16:46:54 -07001866 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
1867 b43legacy_power_saving_ctl_bits(dev, -1, -1);
Larry Fingerf34eb692007-10-11 00:05:57 -05001868
1869 /* Re-enable IRQs. */
1870 spin_lock_irq(&dev->wl->irq_lock);
1871 b43legacy_interrupt_enable(dev, dev->irq_savedstate);
1872 spin_unlock_irq(&dev->wl->irq_lock);
Larry Finger75388ac2007-09-25 16:46:54 -07001873 }
1874}
1875
1876/* http://bcm-specs.sipsolutions.net/SuspendMAC */
1877void b43legacy_mac_suspend(struct b43legacy_wldev *dev)
1878{
1879 int i;
1880 u32 tmp;
1881
Larry Fingerf34eb692007-10-11 00:05:57 -05001882 might_sleep();
1883 B43legacy_WARN_ON(irqs_disabled());
Larry Finger75388ac2007-09-25 16:46:54 -07001884 B43legacy_WARN_ON(dev->mac_suspended < 0);
Larry Fingerf34eb692007-10-11 00:05:57 -05001885
Larry Finger75388ac2007-09-25 16:46:54 -07001886 if (dev->mac_suspended == 0) {
Larry Fingerf34eb692007-10-11 00:05:57 -05001887 /* Mask IRQs before suspending MAC. Otherwise
1888 * the MAC stays busy and won't suspend. */
1889 spin_lock_irq(&dev->wl->irq_lock);
1890 tmp = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
1891 spin_unlock_irq(&dev->wl->irq_lock);
1892 b43legacy_synchronize_irq(dev);
1893 dev->irq_savedstate = tmp;
1894
Larry Finger75388ac2007-09-25 16:46:54 -07001895 b43legacy_power_saving_ctl_bits(dev, -1, 1);
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001896 b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
Larry Finger75388ac2007-09-25 16:46:54 -07001897 b43legacy_read32(dev,
Stefano Brivioe78c9d22008-01-23 14:48:50 +01001898 B43legacy_MMIO_MACCTL)
1899 & ~B43legacy_MACCTL_ENABLED);
Larry Finger75388ac2007-09-25 16:46:54 -07001900 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
Larry Fingerf34eb692007-10-11 00:05:57 -05001901 for (i = 40; i; i--) {
Larry Finger75388ac2007-09-25 16:46:54 -07001902 tmp = b43legacy_read32(dev,
1903 B43legacy_MMIO_GEN_IRQ_REASON);
1904 if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
1905 goto out;
Larry Fingerf34eb692007-10-11 00:05:57 -05001906 msleep(1);
Larry Finger75388ac2007-09-25 16:46:54 -07001907 }
1908 b43legacyerr(dev->wl, "MAC suspend failed\n");
1909 }
1910out:
1911 dev->mac_suspended++;
1912}
1913
1914static void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
1915{
1916 struct b43legacy_wl *wl = dev->wl;
1917 u32 ctl;
1918 u16 cfp_pretbtt;
1919
1920 ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
1921 /* Reset status to STA infrastructure mode. */
1922 ctl &= ~B43legacy_MACCTL_AP;
1923 ctl &= ~B43legacy_MACCTL_KEEP_CTL;
1924 ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
1925 ctl &= ~B43legacy_MACCTL_KEEP_BAD;
1926 ctl &= ~B43legacy_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04001927 ctl &= ~B43legacy_MACCTL_BEACPROMISC;
Larry Finger75388ac2007-09-25 16:46:54 -07001928 ctl |= B43legacy_MACCTL_INFRA;
1929
Johannes Berg4150c572007-09-17 01:29:23 -04001930 if (b43legacy_is_mode(wl, IEEE80211_IF_TYPE_AP))
1931 ctl |= B43legacy_MACCTL_AP;
1932 else if (b43legacy_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
1933 ctl &= ~B43legacy_MACCTL_INFRA;
1934
1935 if (wl->filter_flags & FIF_CONTROL)
Larry Finger75388ac2007-09-25 16:46:54 -07001936 ctl |= B43legacy_MACCTL_KEEP_CTL;
Johannes Berg4150c572007-09-17 01:29:23 -04001937 if (wl->filter_flags & FIF_FCSFAIL)
1938 ctl |= B43legacy_MACCTL_KEEP_BAD;
1939 if (wl->filter_flags & FIF_PLCPFAIL)
1940 ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
1941 if (wl->filter_flags & FIF_PROMISC_IN_BSS)
Larry Finger75388ac2007-09-25 16:46:54 -07001942 ctl |= B43legacy_MACCTL_PROMISC;
Johannes Berg4150c572007-09-17 01:29:23 -04001943 if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
1944 ctl |= B43legacy_MACCTL_BEACPROMISC;
1945
Larry Finger75388ac2007-09-25 16:46:54 -07001946 /* Workaround: On old hardware the HW-MAC-address-filter
1947 * doesn't work properly, so always run promisc in filter
1948 * it in software. */
1949 if (dev->dev->id.revision <= 4)
1950 ctl |= B43legacy_MACCTL_PROMISC;
1951
1952 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
1953
1954 cfp_pretbtt = 2;
1955 if ((ctl & B43legacy_MACCTL_INFRA) &&
1956 !(ctl & B43legacy_MACCTL_AP)) {
1957 if (dev->dev->bus->chip_id == 0x4306 &&
1958 dev->dev->bus->chip_rev == 3)
1959 cfp_pretbtt = 100;
1960 else
1961 cfp_pretbtt = 50;
1962 }
1963 b43legacy_write16(dev, 0x612, cfp_pretbtt);
1964}
1965
1966static void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
1967 u16 rate,
1968 int is_ofdm)
1969{
1970 u16 offset;
1971
1972 if (is_ofdm) {
1973 offset = 0x480;
1974 offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
1975 } else {
1976 offset = 0x4C0;
1977 offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
1978 }
1979 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
1980 b43legacy_shm_read16(dev,
1981 B43legacy_SHM_SHARED, offset));
1982}
1983
1984static void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
1985{
1986 switch (dev->phy.type) {
1987 case B43legacy_PHYTYPE_G:
1988 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
1989 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
1990 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
1991 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
1992 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
1993 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
1994 b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
1995 /* fallthrough */
1996 case B43legacy_PHYTYPE_B:
1997 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
1998 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
1999 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
2000 b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
2001 break;
2002 default:
2003 B43legacy_BUG_ON(1);
2004 }
2005}
2006
2007/* Set the TX-Antenna for management frames sent by firmware. */
2008static void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
2009 int antenna)
2010{
2011 u16 ant = 0;
2012 u16 tmp;
2013
2014 switch (antenna) {
2015 case B43legacy_ANTENNA0:
2016 ant |= B43legacy_TX4_PHY_ANT0;
2017 break;
2018 case B43legacy_ANTENNA1:
2019 ant |= B43legacy_TX4_PHY_ANT1;
2020 break;
2021 case B43legacy_ANTENNA_AUTO:
2022 ant |= B43legacy_TX4_PHY_ANTLAST;
2023 break;
2024 default:
2025 B43legacy_BUG_ON(1);
2026 }
2027
2028 /* FIXME We also need to set the other flags of the PHY control
2029 * field somewhere. */
2030
2031 /* For Beacons */
2032 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2033 B43legacy_SHM_SH_BEACPHYCTL);
2034 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2035 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2036 B43legacy_SHM_SH_BEACPHYCTL, tmp);
2037 /* For ACK/CTS */
2038 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2039 B43legacy_SHM_SH_ACKCTSPHYCTL);
2040 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2041 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2042 B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
2043 /* For Probe Resposes */
2044 tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2045 B43legacy_SHM_SH_PRPHYCTL);
2046 tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
2047 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
2048 B43legacy_SHM_SH_PRPHYCTL, tmp);
2049}
2050
2051/* This is the opposite of b43legacy_chip_init() */
2052static void b43legacy_chip_exit(struct b43legacy_wldev *dev)
2053{
Larry Finger93bb7f32007-10-10 22:44:22 -05002054 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07002055 b43legacy_gpio_cleanup(dev);
2056 /* firmware is released later */
2057}
2058
2059/* Initialize the chip
2060 * http://bcm-specs.sipsolutions.net/ChipInit
2061 */
2062static int b43legacy_chip_init(struct b43legacy_wldev *dev)
2063{
2064 struct b43legacy_phy *phy = &dev->phy;
2065 int err;
2066 int tmp;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002067 u32 value32, macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07002068 u16 value16;
2069
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002070 /* Initialize the MAC control */
2071 macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
2072 if (dev->phy.gmode)
2073 macctl |= B43legacy_MACCTL_GMODE;
2074 macctl |= B43legacy_MACCTL_INFRA;
2075 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
Larry Finger75388ac2007-09-25 16:46:54 -07002076
2077 err = b43legacy_request_firmware(dev);
2078 if (err)
2079 goto out;
2080 err = b43legacy_upload_microcode(dev);
2081 if (err)
2082 goto out; /* firmware is released later */
2083
2084 err = b43legacy_gpio_init(dev);
2085 if (err)
2086 goto out; /* firmware is released later */
Larry Fingerba48f7b2007-10-12 23:04:51 -05002087
Larry Finger75388ac2007-09-25 16:46:54 -07002088 err = b43legacy_upload_initvals(dev);
2089 if (err)
Larry Finger4ad36d72007-12-16 19:21:06 +01002090 goto err_gpio_clean;
Larry Finger75388ac2007-09-25 16:46:54 -07002091 b43legacy_radio_turn_on(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002092
2093 b43legacy_write16(dev, 0x03E6, 0x0000);
2094 err = b43legacy_phy_init(dev);
2095 if (err)
2096 goto err_radio_off;
2097
2098 /* Select initial Interference Mitigation. */
2099 tmp = phy->interfmode;
2100 phy->interfmode = B43legacy_INTERFMODE_NONE;
2101 b43legacy_radio_set_interference_mitigation(dev, tmp);
2102
2103 b43legacy_phy_set_antenna_diversity(dev);
2104 b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
2105
2106 if (phy->type == B43legacy_PHYTYPE_B) {
2107 value16 = b43legacy_read16(dev, 0x005E);
2108 value16 |= 0x0004;
2109 b43legacy_write16(dev, 0x005E, value16);
2110 }
2111 b43legacy_write32(dev, 0x0100, 0x01000000);
2112 if (dev->dev->id.revision < 5)
2113 b43legacy_write32(dev, 0x010C, 0x01000000);
2114
Stefano Brivioe78c9d22008-01-23 14:48:50 +01002115 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2116 value32 &= ~B43legacy_MACCTL_INFRA;
2117 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
2118 value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2119 value32 |= B43legacy_MACCTL_INFRA;
2120 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
Larry Finger75388ac2007-09-25 16:46:54 -07002121
Larry Finger75388ac2007-09-25 16:46:54 -07002122 if (b43legacy_using_pio(dev)) {
2123 b43legacy_write32(dev, 0x0210, 0x00000100);
2124 b43legacy_write32(dev, 0x0230, 0x00000100);
2125 b43legacy_write32(dev, 0x0250, 0x00000100);
2126 b43legacy_write32(dev, 0x0270, 0x00000100);
2127 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
2128 0x0000);
2129 }
2130
2131 /* Probe Response Timeout value */
2132 /* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
2133 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
2134
2135 /* Initially set the wireless operation mode. */
2136 b43legacy_adjust_opmode(dev);
2137
2138 if (dev->dev->id.revision < 3) {
2139 b43legacy_write16(dev, 0x060E, 0x0000);
2140 b43legacy_write16(dev, 0x0610, 0x8000);
2141 b43legacy_write16(dev, 0x0604, 0x0000);
2142 b43legacy_write16(dev, 0x0606, 0x0200);
2143 } else {
2144 b43legacy_write32(dev, 0x0188, 0x80000000);
2145 b43legacy_write32(dev, 0x018C, 0x02000000);
2146 }
2147 b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
2148 b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
2149 b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
2150 b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
2151 b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
2152 b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
2153 b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
2154
2155 value32 = ssb_read32(dev->dev, SSB_TMSLOW);
2156 value32 |= 0x00100000;
2157 ssb_write32(dev->dev, SSB_TMSLOW, value32);
2158
2159 b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
2160 dev->dev->bus->chipco.fast_pwrup_delay);
2161
Stefano Brivioa293ee92007-11-24 23:35:25 +01002162 /* PHY TX errors counter. */
2163 atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2164
Larry Finger75388ac2007-09-25 16:46:54 -07002165 B43legacy_WARN_ON(err != 0);
2166 b43legacydbg(dev->wl, "Chip initialized\n");
2167out:
2168 return err;
2169
2170err_radio_off:
Larry Finger93bb7f32007-10-10 22:44:22 -05002171 b43legacy_radio_turn_off(dev, 1);
Larry Finger4ad36d72007-12-16 19:21:06 +01002172err_gpio_clean:
Larry Finger75388ac2007-09-25 16:46:54 -07002173 b43legacy_gpio_cleanup(dev);
2174 goto out;
2175}
2176
2177static void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
2178{
2179 struct b43legacy_phy *phy = &dev->phy;
2180
2181 if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
2182 return;
2183
2184 b43legacy_mac_suspend(dev);
2185 b43legacy_phy_lo_g_measure(dev);
2186 b43legacy_mac_enable(dev);
2187}
2188
2189static void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
2190{
2191 b43legacy_phy_lo_mark_all_unused(dev);
Larry Finger7797aa32007-11-09 16:57:34 -06002192 if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
Larry Finger75388ac2007-09-25 16:46:54 -07002193 b43legacy_mac_suspend(dev);
2194 b43legacy_calc_nrssi_slope(dev);
2195 b43legacy_mac_enable(dev);
2196 }
2197}
2198
2199static void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
2200{
2201 /* Update device statistics. */
2202 b43legacy_calculate_link_quality(dev);
2203}
2204
2205static void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
2206{
2207 b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
Stefano Brivioa293ee92007-11-24 23:35:25 +01002208
2209 atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
2210 wmb();
Larry Finger75388ac2007-09-25 16:46:54 -07002211}
2212
Larry Finger75388ac2007-09-25 16:46:54 -07002213static void do_periodic_work(struct b43legacy_wldev *dev)
2214{
2215 unsigned int state;
2216
2217 state = dev->periodic_state;
Larry Finger6be50832007-10-10 22:48:17 -05002218 if (state % 8 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002219 b43legacy_periodic_every120sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002220 if (state % 4 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002221 b43legacy_periodic_every60sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002222 if (state % 2 == 0)
Larry Finger75388ac2007-09-25 16:46:54 -07002223 b43legacy_periodic_every30sec(dev);
Larry Finger6be50832007-10-10 22:48:17 -05002224 b43legacy_periodic_every15sec(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002225}
2226
Larry Fingerf34eb692007-10-11 00:05:57 -05002227/* Periodic work locking policy:
2228 * The whole periodic work handler is protected by
2229 * wl->mutex. If another lock is needed somewhere in the
2230 * pwork callchain, it's aquired in-place, where it's needed.
Larry Finger75388ac2007-09-25 16:46:54 -07002231 */
Larry Finger75388ac2007-09-25 16:46:54 -07002232static void b43legacy_periodic_work_handler(struct work_struct *work)
2233{
Larry Fingerf34eb692007-10-11 00:05:57 -05002234 struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
2235 periodic_work.work);
2236 struct b43legacy_wl *wl = dev->wl;
Larry Finger75388ac2007-09-25 16:46:54 -07002237 unsigned long delay;
Larry Finger75388ac2007-09-25 16:46:54 -07002238
Larry Fingerf34eb692007-10-11 00:05:57 -05002239 mutex_lock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07002240
2241 if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
2242 goto out;
2243 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
2244 goto out_requeue;
2245
Larry Fingerf34eb692007-10-11 00:05:57 -05002246 do_periodic_work(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002247
Larry Finger75388ac2007-09-25 16:46:54 -07002248 dev->periodic_state++;
2249out_requeue:
2250 if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
2251 delay = msecs_to_jiffies(50);
2252 else
Larry Finger6be50832007-10-10 22:48:17 -05002253 delay = round_jiffies_relative(HZ * 15);
Larry Fingerf34eb692007-10-11 00:05:57 -05002254 queue_delayed_work(wl->hw->workqueue, &dev->periodic_work, delay);
Larry Finger75388ac2007-09-25 16:46:54 -07002255out:
Larry Fingerf34eb692007-10-11 00:05:57 -05002256 mutex_unlock(&wl->mutex);
Larry Finger75388ac2007-09-25 16:46:54 -07002257}
2258
2259static void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
2260{
2261 struct delayed_work *work = &dev->periodic_work;
2262
2263 dev->periodic_state = 0;
2264 INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
2265 queue_delayed_work(dev->wl->hw->workqueue, work, 0);
2266}
2267
2268/* Validate access to the chip (SHM) */
2269static int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
2270{
2271 u32 value;
2272 u32 shm_backup;
2273
2274 shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
2275 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
2276 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2277 0xAA5555AA)
2278 goto error;
2279 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
2280 if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
2281 0x55AAAA55)
2282 goto error;
2283 b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
2284
2285 value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2286 if ((value | B43legacy_MACCTL_GMODE) !=
2287 (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
2288 goto error;
2289
2290 value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
2291 if (value)
2292 goto error;
2293
2294 return 0;
2295error:
2296 b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
2297 return -ENODEV;
2298}
2299
2300static void b43legacy_security_init(struct b43legacy_wldev *dev)
2301{
2302 dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
2303 B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
2304 dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
2305 0x0056);
2306 /* KTP is a word address, but we address SHM bytewise.
2307 * So multiply by two.
2308 */
2309 dev->ktp *= 2;
2310 if (dev->dev->id.revision >= 5)
2311 /* Number of RCMTA address slots */
2312 b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
2313 dev->max_nr_keys - 8);
2314}
2315
2316static int b43legacy_rng_read(struct hwrng *rng, u32 *data)
2317{
2318 struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
2319 unsigned long flags;
2320
2321 /* Don't take wl->mutex here, as it could deadlock with
2322 * hwrng internal locking. It's not needed to take
2323 * wl->mutex here, anyway. */
2324
2325 spin_lock_irqsave(&wl->irq_lock, flags);
2326 *data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
2327 spin_unlock_irqrestore(&wl->irq_lock, flags);
2328
2329 return (sizeof(u16));
2330}
2331
2332static void b43legacy_rng_exit(struct b43legacy_wl *wl)
2333{
2334 if (wl->rng_initialized)
2335 hwrng_unregister(&wl->rng);
2336}
2337
2338static int b43legacy_rng_init(struct b43legacy_wl *wl)
2339{
2340 int err;
2341
2342 snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
2343 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
2344 wl->rng.name = wl->rng_name;
2345 wl->rng.data_read = b43legacy_rng_read;
2346 wl->rng.priv = (unsigned long)wl;
2347 wl->rng_initialized = 1;
2348 err = hwrng_register(&wl->rng);
2349 if (err) {
2350 wl->rng_initialized = 0;
2351 b43legacyerr(wl, "Failed to register the random "
2352 "number generator (%d)\n", err);
2353 }
2354
2355 return err;
2356}
2357
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002358static int b43legacy_op_tx(struct ieee80211_hw *hw,
2359 struct sk_buff *skb,
2360 struct ieee80211_tx_control *ctl)
Larry Finger75388ac2007-09-25 16:46:54 -07002361{
2362 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2363 struct b43legacy_wldev *dev = wl->current_dev;
2364 int err = -ENODEV;
2365 unsigned long flags;
2366
2367 if (unlikely(!dev))
2368 goto out;
2369 if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
2370 goto out;
2371 /* DMA-TX is done without a global lock. */
2372 if (b43legacy_using_pio(dev)) {
2373 spin_lock_irqsave(&wl->irq_lock, flags);
2374 err = b43legacy_pio_tx(dev, skb, ctl);
2375 spin_unlock_irqrestore(&wl->irq_lock, flags);
2376 } else
2377 err = b43legacy_dma_tx(dev, skb, ctl);
2378out:
2379 if (unlikely(err))
2380 return NETDEV_TX_BUSY;
2381 return NETDEV_TX_OK;
2382}
2383
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002384static int b43legacy_op_conf_tx(struct ieee80211_hw *hw,
2385 int queue,
2386 const struct ieee80211_tx_queue_params *params)
Larry Finger75388ac2007-09-25 16:46:54 -07002387{
2388 return 0;
2389}
2390
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002391static int b43legacy_op_get_tx_stats(struct ieee80211_hw *hw,
2392 struct ieee80211_tx_queue_stats *stats)
Larry Finger75388ac2007-09-25 16:46:54 -07002393{
2394 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2395 struct b43legacy_wldev *dev = wl->current_dev;
2396 unsigned long flags;
2397 int err = -ENODEV;
2398
2399 if (!dev)
2400 goto out;
2401 spin_lock_irqsave(&wl->irq_lock, flags);
2402 if (likely(b43legacy_status(dev) >= B43legacy_STAT_STARTED)) {
2403 if (b43legacy_using_pio(dev))
2404 b43legacy_pio_get_tx_stats(dev, stats);
2405 else
2406 b43legacy_dma_get_tx_stats(dev, stats);
2407 err = 0;
2408 }
2409 spin_unlock_irqrestore(&wl->irq_lock, flags);
2410out:
2411 return err;
2412}
2413
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002414static int b43legacy_op_get_stats(struct ieee80211_hw *hw,
2415 struct ieee80211_low_level_stats *stats)
Larry Finger75388ac2007-09-25 16:46:54 -07002416{
2417 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2418 unsigned long flags;
2419
2420 spin_lock_irqsave(&wl->irq_lock, flags);
2421 memcpy(stats, &wl->ieee_stats, sizeof(*stats));
2422 spin_unlock_irqrestore(&wl->irq_lock, flags);
2423
2424 return 0;
2425}
2426
2427static const char *phymode_to_string(unsigned int phymode)
2428{
2429 switch (phymode) {
2430 case B43legacy_PHYMODE_B:
2431 return "B";
2432 case B43legacy_PHYMODE_G:
2433 return "G";
2434 default:
2435 B43legacy_BUG_ON(1);
2436 }
2437 return "";
2438}
2439
2440static int find_wldev_for_phymode(struct b43legacy_wl *wl,
2441 unsigned int phymode,
2442 struct b43legacy_wldev **dev,
2443 bool *gmode)
2444{
2445 struct b43legacy_wldev *d;
2446
2447 list_for_each_entry(d, &wl->devlist, list) {
2448 if (d->phy.possible_phymodes & phymode) {
2449 /* Ok, this device supports the PHY-mode.
2450 * Set the gmode bit. */
2451 *gmode = 1;
2452 *dev = d;
2453
2454 return 0;
2455 }
2456 }
2457
2458 return -ESRCH;
2459}
2460
2461static void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
2462{
2463 struct ssb_device *sdev = dev->dev;
2464 u32 tmslow;
2465
2466 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2467 tmslow &= ~B43legacy_TMSLOW_GMODE;
2468 tmslow |= B43legacy_TMSLOW_PHYRESET;
2469 tmslow |= SSB_TMSLOW_FGC;
2470 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2471 msleep(1);
2472
2473 tmslow = ssb_read32(sdev, SSB_TMSLOW);
2474 tmslow &= ~SSB_TMSLOW_FGC;
2475 tmslow |= B43legacy_TMSLOW_PHYRESET;
2476 ssb_write32(sdev, SSB_TMSLOW, tmslow);
2477 msleep(1);
2478}
2479
2480/* Expects wl->mutex locked */
2481static int b43legacy_switch_phymode(struct b43legacy_wl *wl,
2482 unsigned int new_mode)
2483{
2484 struct b43legacy_wldev *up_dev;
2485 struct b43legacy_wldev *down_dev;
2486 int err;
2487 bool gmode = 0;
2488 int prev_status;
2489
2490 err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
2491 if (err) {
2492 b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
2493 phymode_to_string(new_mode));
2494 return err;
2495 }
2496 if ((up_dev == wl->current_dev) &&
2497 (!!wl->current_dev->phy.gmode == !!gmode))
2498 /* This device is already running. */
2499 return 0;
2500 b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
2501 phymode_to_string(new_mode));
2502 down_dev = wl->current_dev;
2503
2504 prev_status = b43legacy_status(down_dev);
2505 /* Shutdown the currently running core. */
2506 if (prev_status >= B43legacy_STAT_STARTED)
2507 b43legacy_wireless_core_stop(down_dev);
2508 if (prev_status >= B43legacy_STAT_INITIALIZED)
2509 b43legacy_wireless_core_exit(down_dev);
2510
2511 if (down_dev != up_dev)
2512 /* We switch to a different core, so we put PHY into
2513 * RESET on the old core. */
2514 b43legacy_put_phy_into_reset(down_dev);
2515
2516 /* Now start the new core. */
2517 up_dev->phy.gmode = gmode;
2518 if (prev_status >= B43legacy_STAT_INITIALIZED) {
2519 err = b43legacy_wireless_core_init(up_dev);
2520 if (err) {
2521 b43legacyerr(wl, "Fatal: Could not initialize device"
2522 " for newly selected %s-PHY mode\n",
2523 phymode_to_string(new_mode));
2524 goto init_failure;
2525 }
2526 }
2527 if (prev_status >= B43legacy_STAT_STARTED) {
2528 err = b43legacy_wireless_core_start(up_dev);
2529 if (err) {
2530 b43legacyerr(wl, "Fatal: Coult not start device for "
2531 "newly selected %s-PHY mode\n",
2532 phymode_to_string(new_mode));
2533 b43legacy_wireless_core_exit(up_dev);
2534 goto init_failure;
2535 }
2536 }
2537 B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
2538
2539 b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
2540
2541 wl->current_dev = up_dev;
2542
2543 return 0;
2544init_failure:
2545 /* Whoops, failed to init the new core. No core is operating now. */
2546 wl->current_dev = NULL;
2547 return err;
2548}
2549
2550static int b43legacy_antenna_from_ieee80211(u8 antenna)
2551{
2552 switch (antenna) {
2553 case 0: /* default/diversity */
2554 return B43legacy_ANTENNA_DEFAULT;
2555 case 1: /* Antenna 0 */
2556 return B43legacy_ANTENNA0;
2557 case 2: /* Antenna 1 */
2558 return B43legacy_ANTENNA1;
2559 default:
2560 return B43legacy_ANTENNA_DEFAULT;
2561 }
2562}
2563
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002564static int b43legacy_op_dev_config(struct ieee80211_hw *hw,
2565 struct ieee80211_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07002566{
2567 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2568 struct b43legacy_wldev *dev;
2569 struct b43legacy_phy *phy;
2570 unsigned long flags;
2571 unsigned int new_phymode = 0xFFFF;
2572 int antenna_tx;
2573 int antenna_rx;
2574 int err = 0;
2575 u32 savedirqs;
2576
2577 antenna_tx = b43legacy_antenna_from_ieee80211(conf->antenna_sel_tx);
2578 antenna_rx = b43legacy_antenna_from_ieee80211(conf->antenna_sel_rx);
2579
2580 mutex_lock(&wl->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01002581 dev = wl->current_dev;
2582 phy = &dev->phy;
Larry Finger75388ac2007-09-25 16:46:54 -07002583
2584 /* Switch the PHY mode (if necessary). */
Johannes Berg8318d782008-01-24 19:38:38 +01002585 switch (conf->channel->band) {
2586 case IEEE80211_BAND_2GHZ:
2587 if (phy->type == B43legacy_PHYTYPE_B)
2588 new_phymode = B43legacy_PHYMODE_B;
2589 else
2590 new_phymode = B43legacy_PHYMODE_G;
Larry Finger75388ac2007-09-25 16:46:54 -07002591 break;
2592 default:
2593 B43legacy_WARN_ON(1);
2594 }
2595 err = b43legacy_switch_phymode(wl, new_phymode);
2596 if (err)
2597 goto out_unlock_mutex;
Larry Finger75388ac2007-09-25 16:46:54 -07002598
2599 /* Disable IRQs while reconfiguring the device.
2600 * This makes it possible to drop the spinlock throughout
2601 * the reconfiguration process. */
2602 spin_lock_irqsave(&wl->irq_lock, flags);
2603 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
2604 spin_unlock_irqrestore(&wl->irq_lock, flags);
2605 goto out_unlock_mutex;
2606 }
2607 savedirqs = b43legacy_interrupt_disable(dev, B43legacy_IRQ_ALL);
2608 spin_unlock_irqrestore(&wl->irq_lock, flags);
2609 b43legacy_synchronize_irq(dev);
2610
2611 /* Switch to the requested channel.
2612 * The firmware takes care of races with the TX handler. */
Johannes Berg8318d782008-01-24 19:38:38 +01002613 if (conf->channel->hw_value != phy->channel)
2614 b43legacy_radio_selectchannel(dev, conf->channel->hw_value, 0);
Larry Finger75388ac2007-09-25 16:46:54 -07002615
2616 /* Enable/Disable ShortSlot timing. */
2617 if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME))
2618 != dev->short_slot) {
2619 B43legacy_WARN_ON(phy->type != B43legacy_PHYTYPE_G);
2620 if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
2621 b43legacy_short_slot_timing_enable(dev);
2622 else
2623 b43legacy_short_slot_timing_disable(dev);
2624 }
2625
Johannes Berg5be3bda2007-11-24 21:11:09 +01002626 dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2627
Larry Finger75388ac2007-09-25 16:46:54 -07002628 /* Adjust the desired TX power level. */
2629 if (conf->power_level != 0) {
2630 if (conf->power_level != phy->power_level) {
2631 phy->power_level = conf->power_level;
2632 b43legacy_phy_xmitpower(dev);
2633 }
2634 }
2635
2636 /* Antennas for RX and management frame TX. */
2637 b43legacy_mgmtframe_txantenna(dev, antenna_tx);
2638
2639 /* Update templates for AP mode. */
2640 if (b43legacy_is_mode(wl, IEEE80211_IF_TYPE_AP))
2641 b43legacy_set_beacon_int(dev, conf->beacon_int);
2642
2643
Larry Finger42a91742007-09-20 21:11:02 -05002644 if (!!conf->radio_enabled != phy->radio_on) {
2645 if (conf->radio_enabled) {
2646 b43legacy_radio_turn_on(dev);
2647 b43legacyinfo(dev->wl, "Radio turned on by software\n");
2648 if (!dev->radio_hw_enable)
2649 b43legacyinfo(dev->wl, "The hardware RF-kill"
2650 " button still turns the radio"
2651 " physically off. Press the"
2652 " button to turn it on.\n");
2653 } else {
Larry Finger93bb7f32007-10-10 22:44:22 -05002654 b43legacy_radio_turn_off(dev, 0);
Larry Finger42a91742007-09-20 21:11:02 -05002655 b43legacyinfo(dev->wl, "Radio turned off by"
2656 " software\n");
2657 }
2658 }
2659
Larry Finger75388ac2007-09-25 16:46:54 -07002660 spin_lock_irqsave(&wl->irq_lock, flags);
2661 b43legacy_interrupt_enable(dev, savedirqs);
2662 mmiowb();
2663 spin_unlock_irqrestore(&wl->irq_lock, flags);
2664out_unlock_mutex:
2665 mutex_unlock(&wl->mutex);
2666
2667 return err;
2668}
2669
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002670static void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
2671 unsigned int changed,
2672 unsigned int *fflags,
2673 int mc_count,
2674 struct dev_addr_list *mc_list)
Larry Finger75388ac2007-09-25 16:46:54 -07002675{
2676 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2677 struct b43legacy_wldev *dev = wl->current_dev;
2678 unsigned long flags;
2679
Johannes Berg4150c572007-09-17 01:29:23 -04002680 if (!dev) {
2681 *fflags = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002682 return;
Larry Finger75388ac2007-09-25 16:46:54 -07002683 }
Johannes Berg4150c572007-09-17 01:29:23 -04002684
2685 spin_lock_irqsave(&wl->irq_lock, flags);
2686 *fflags &= FIF_PROMISC_IN_BSS |
2687 FIF_ALLMULTI |
2688 FIF_FCSFAIL |
2689 FIF_PLCPFAIL |
2690 FIF_CONTROL |
2691 FIF_OTHER_BSS |
2692 FIF_BCN_PRBRESP_PROMISC;
2693
2694 changed &= FIF_PROMISC_IN_BSS |
2695 FIF_ALLMULTI |
2696 FIF_FCSFAIL |
2697 FIF_PLCPFAIL |
2698 FIF_CONTROL |
2699 FIF_OTHER_BSS |
2700 FIF_BCN_PRBRESP_PROMISC;
2701
2702 wl->filter_flags = *fflags;
2703
2704 if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
2705 b43legacy_adjust_opmode(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002706 spin_unlock_irqrestore(&wl->irq_lock, flags);
2707}
2708
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002709static int b43legacy_op_config_interface(struct ieee80211_hw *hw,
Johannes Berg32bfd352007-12-19 01:31:26 +01002710 struct ieee80211_vif *vif,
Stefano Brivio33a3dc92007-11-06 22:48:25 +01002711 struct ieee80211_if_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07002712{
2713 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
2714 struct b43legacy_wldev *dev = wl->current_dev;
2715 unsigned long flags;
2716
2717 if (!dev)
2718 return -ENODEV;
2719 mutex_lock(&wl->mutex);
2720 spin_lock_irqsave(&wl->irq_lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01002721 B43legacy_WARN_ON(wl->vif != vif);
Johannes Berg4150c572007-09-17 01:29:23 -04002722 if (conf->bssid)
2723 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
2724 else
2725 memset(wl->bssid, 0, ETH_ALEN);
2726 if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
2727 if (b43legacy_is_mode(wl, IEEE80211_IF_TYPE_AP)) {
2728 B43legacy_WARN_ON(conf->type != IEEE80211_IF_TYPE_AP);
2729 b43legacy_set_ssid(dev, conf->ssid, conf->ssid_len);
2730 if (conf->beacon)
Stefano Brivioa2971702008-02-08 06:31:25 +01002731 b43legacy_update_templates(wl, conf->beacon);
Larry Finger75388ac2007-09-25 16:46:54 -07002732 }
Johannes Berg4150c572007-09-17 01:29:23 -04002733 b43legacy_write_mac_bssid_templates(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07002734 }
2735 spin_unlock_irqrestore(&wl->irq_lock, flags);
2736 mutex_unlock(&wl->mutex);
2737
2738 return 0;
2739}
2740
2741/* Locking: wl->mutex */
2742static void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
2743{
2744 struct b43legacy_wl *wl = dev->wl;
2745 unsigned long flags;
2746
2747 if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
2748 return;
Stefano Brivio440cb582007-11-07 18:33:37 +01002749
2750 /* Disable and sync interrupts. We must do this before than
2751 * setting the status to INITIALIZED, as the interrupt handler
2752 * won't care about IRQs then. */
2753 spin_lock_irqsave(&wl->irq_lock, flags);
2754 dev->irq_savedstate = b43legacy_interrupt_disable(dev,
2755 B43legacy_IRQ_ALL);
2756 b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
2757 spin_unlock_irqrestore(&wl->irq_lock, flags);
2758 b43legacy_synchronize_irq(dev);
2759
Larry Finger75388ac2007-09-25 16:46:54 -07002760 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
2761
2762 mutex_unlock(&wl->mutex);
2763 /* Must unlock as it would otherwise deadlock. No races here.
2764 * Cancel the possibly running self-rearming periodic work. */
2765 cancel_delayed_work_sync(&dev->periodic_work);
2766 mutex_lock(&wl->mutex);
2767
2768 ieee80211_stop_queues(wl->hw); /* FIXME this could cause a deadlock */
2769
Larry Finger75388ac2007-09-25 16:46:54 -07002770 b43legacy_mac_suspend(dev);
2771 free_irq(dev->dev->irq, dev);
2772 b43legacydbg(wl, "Wireless interface stopped\n");
2773}
2774
2775/* Locking: wl->mutex */
2776static int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
2777{
2778 int err;
2779
2780 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
2781
2782 drain_txstatus_queue(dev);
2783 err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
2784 IRQF_SHARED, KBUILD_MODNAME, dev);
2785 if (err) {
2786 b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
2787 dev->dev->irq);
2788 goto out;
2789 }
2790 /* We are ready to run. */
2791 b43legacy_set_status(dev, B43legacy_STAT_STARTED);
2792
2793 /* Start data flow (TX/RX) */
2794 b43legacy_mac_enable(dev);
2795 b43legacy_interrupt_enable(dev, dev->irq_savedstate);
2796 ieee80211_start_queues(dev->wl->hw);
2797
2798 /* Start maintenance work */
2799 b43legacy_periodic_tasks_setup(dev);
2800
2801 b43legacydbg(dev->wl, "Wireless interface started\n");
2802out:
2803 return err;
2804}
2805
2806/* Get PHY and RADIO versioning numbers */
2807static int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
2808{
2809 struct b43legacy_phy *phy = &dev->phy;
2810 u32 tmp;
2811 u8 analog_type;
2812 u8 phy_type;
2813 u8 phy_rev;
2814 u16 radio_manuf;
2815 u16 radio_ver;
2816 u16 radio_rev;
2817 int unsupported = 0;
2818
2819 /* Get PHY versioning */
2820 tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
2821 analog_type = (tmp & B43legacy_PHYVER_ANALOG)
2822 >> B43legacy_PHYVER_ANALOG_SHIFT;
2823 phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
2824 phy_rev = (tmp & B43legacy_PHYVER_VERSION);
2825 switch (phy_type) {
2826 case B43legacy_PHYTYPE_B:
2827 if (phy_rev != 2 && phy_rev != 4
2828 && phy_rev != 6 && phy_rev != 7)
2829 unsupported = 1;
2830 break;
2831 case B43legacy_PHYTYPE_G:
2832 if (phy_rev > 8)
2833 unsupported = 1;
2834 break;
2835 default:
2836 unsupported = 1;
2837 };
2838 if (unsupported) {
2839 b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
2840 "(Analog %u, Type %u, Revision %u)\n",
2841 analog_type, phy_type, phy_rev);
2842 return -EOPNOTSUPP;
2843 }
2844 b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
2845 analog_type, phy_type, phy_rev);
2846
2847
2848 /* Get RADIO versioning */
2849 if (dev->dev->bus->chip_id == 0x4317) {
2850 if (dev->dev->bus->chip_rev == 0)
2851 tmp = 0x3205017F;
2852 else if (dev->dev->bus->chip_rev == 1)
2853 tmp = 0x4205017F;
2854 else
2855 tmp = 0x5205017F;
2856 } else {
2857 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
2858 B43legacy_RADIOCTL_ID);
2859 tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
2860 tmp <<= 16;
2861 b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
2862 B43legacy_RADIOCTL_ID);
2863 tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
2864 }
2865 radio_manuf = (tmp & 0x00000FFF);
2866 radio_ver = (tmp & 0x0FFFF000) >> 12;
2867 radio_rev = (tmp & 0xF0000000) >> 28;
2868 switch (phy_type) {
2869 case B43legacy_PHYTYPE_B:
2870 if ((radio_ver & 0xFFF0) != 0x2050)
2871 unsupported = 1;
2872 break;
2873 case B43legacy_PHYTYPE_G:
2874 if (radio_ver != 0x2050)
2875 unsupported = 1;
2876 break;
2877 default:
2878 B43legacy_BUG_ON(1);
2879 }
2880 if (unsupported) {
2881 b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
2882 "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
2883 radio_manuf, radio_ver, radio_rev);
2884 return -EOPNOTSUPP;
2885 }
2886 b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
2887 " Revision %u\n", radio_manuf, radio_ver, radio_rev);
2888
2889
2890 phy->radio_manuf = radio_manuf;
2891 phy->radio_ver = radio_ver;
2892 phy->radio_rev = radio_rev;
2893
2894 phy->analog = analog_type;
2895 phy->type = phy_type;
2896 phy->rev = phy_rev;
2897
2898 return 0;
2899}
2900
2901static void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
2902 struct b43legacy_phy *phy)
2903{
2904 struct b43legacy_lopair *lo;
2905 int i;
2906
2907 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
2908 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
2909
Larry Finger1065de12007-09-20 20:10:07 -05002910 /* Assume the radio is enabled. If it's not enabled, the state will
2911 * immediately get fixed on the first periodic work run. */
2912 dev->radio_hw_enable = 1;
Larry Finger75388ac2007-09-25 16:46:54 -07002913
2914 phy->savedpctlreg = 0xFFFF;
2915 phy->aci_enable = 0;
2916 phy->aci_wlan_automatic = 0;
2917 phy->aci_hw_rssi = 0;
2918
2919 lo = phy->_lo_pairs;
2920 if (lo)
2921 memset(lo, 0, sizeof(struct b43legacy_lopair) *
2922 B43legacy_LO_COUNT);
2923 phy->max_lb_gain = 0;
2924 phy->trsw_rx_gain = 0;
2925
2926 /* Set default attenuation values. */
2927 phy->bbatt = b43legacy_default_baseband_attenuation(dev);
2928 phy->rfatt = b43legacy_default_radio_attenuation(dev);
2929 phy->txctl1 = b43legacy_default_txctl1(dev);
2930 phy->txpwr_offset = 0;
2931
2932 /* NRSSI */
2933 phy->nrssislope = 0;
2934 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
2935 phy->nrssi[i] = -1000;
2936 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
2937 phy->nrssi_lt[i] = i;
2938
2939 phy->lofcal = 0xFFFF;
2940 phy->initval = 0xFFFF;
2941
Larry Finger75388ac2007-09-25 16:46:54 -07002942 phy->interfmode = B43legacy_INTERFMODE_NONE;
2943 phy->channel = 0xFF;
2944}
2945
2946static void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
2947{
2948 /* Flags */
Stefano Brivioeed0fd22008-02-08 06:31:10 +01002949 dev->dfq_valid = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07002950
2951 /* Stats */
2952 memset(&dev->stats, 0, sizeof(dev->stats));
2953
2954 setup_struct_phy_for_init(dev, &dev->phy);
2955
2956 /* IRQ related flags */
2957 dev->irq_reason = 0;
2958 memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
2959 dev->irq_savedstate = B43legacy_IRQ_MASKTEMPLATE;
2960
2961 dev->mac_suspended = 1;
2962
2963 /* Noise calculation context */
2964 memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
2965}
2966
2967static void b43legacy_imcfglo_timeouts_workaround(struct b43legacy_wldev *dev)
2968{
2969#ifdef CONFIG_SSB_DRIVER_PCICORE
2970 struct ssb_bus *bus = dev->dev->bus;
2971 u32 tmp;
2972
2973 if (bus->pcicore.dev &&
2974 bus->pcicore.dev->id.coreid == SSB_DEV_PCI &&
2975 bus->pcicore.dev->id.revision <= 5) {
2976 /* IMCFGLO timeouts workaround. */
2977 tmp = ssb_read32(dev->dev, SSB_IMCFGLO);
2978 tmp &= ~SSB_IMCFGLO_REQTO;
2979 tmp &= ~SSB_IMCFGLO_SERTO;
2980 switch (bus->bustype) {
2981 case SSB_BUSTYPE_PCI:
2982 case SSB_BUSTYPE_PCMCIA:
2983 tmp |= 0x32;
2984 break;
2985 case SSB_BUSTYPE_SSB:
2986 tmp |= 0x53;
2987 break;
2988 }
2989 ssb_write32(dev->dev, SSB_IMCFGLO, tmp);
2990 }
2991#endif /* CONFIG_SSB_DRIVER_PCICORE */
2992}
2993
Stefano Brivio0a6e1be2007-11-06 22:48:12 +01002994/* Write the short and long frame retry limit values. */
2995static void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
2996 unsigned int short_retry,
2997 unsigned int long_retry)
2998{
2999 /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
3000 * the chip-internal counter. */
3001 short_retry = min(short_retry, (unsigned int)0xF);
3002 long_retry = min(long_retry, (unsigned int)0xF);
3003
3004 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
3005 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
3006}
3007
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003008static void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
3009 bool idle) {
3010 u16 pu_delay = 1050;
3011
3012 if (b43legacy_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS) || idle)
3013 pu_delay = 500;
3014 if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
3015 pu_delay = max(pu_delay, (u16)2400);
3016
3017 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3018 B43legacy_SHM_SH_SPUWKUP, pu_delay);
3019}
3020
3021/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
3022static void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
3023{
3024 u16 pretbtt;
3025
3026 /* The time value is in microseconds. */
3027 if (b43legacy_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
3028 pretbtt = 2;
3029 else
3030 pretbtt = 250;
3031 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3032 B43legacy_SHM_SH_PRETBTT, pretbtt);
3033 b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
3034}
3035
Larry Finger75388ac2007-09-25 16:46:54 -07003036/* Shutdown a wireless core */
3037/* Locking: wl->mutex */
3038static void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
3039{
3040 struct b43legacy_wl *wl = dev->wl;
3041 struct b43legacy_phy *phy = &dev->phy;
Stefano Brivioe78c9d22008-01-23 14:48:50 +01003042 u32 macctl;
Larry Finger75388ac2007-09-25 16:46:54 -07003043
3044 B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
3045 if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
3046 return;
3047 b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
3048
Stefano Brivioe78c9d22008-01-23 14:48:50 +01003049 /* Stop the microcode PSM. */
3050 macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
3051 macctl &= ~B43legacy_MACCTL_PSM_RUN;
3052 macctl |= B43legacy_MACCTL_PSM_JMP0;
3053 b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
3054
Larry Finger75388ac2007-09-25 16:46:54 -07003055 mutex_unlock(&wl->mutex);
3056 /* Must unlock as it would otherwise deadlock. No races here.
3057 * Cancel possibly pending workqueues. */
3058 cancel_work_sync(&dev->restart_work);
3059 mutex_lock(&wl->mutex);
3060
Larry Finger4ad36d72007-12-16 19:21:06 +01003061 b43legacy_leds_exit(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003062 b43legacy_rng_exit(dev->wl);
3063 b43legacy_pio_free(dev);
3064 b43legacy_dma_free(dev);
3065 b43legacy_chip_exit(dev);
Larry Finger93bb7f32007-10-10 22:44:22 -05003066 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003067 b43legacy_switch_analog(dev, 0);
3068 if (phy->dyn_tssi_tbl)
3069 kfree(phy->tssi2dbm);
3070 kfree(phy->lo_control);
3071 phy->lo_control = NULL;
Stefano Brivioa2971702008-02-08 06:31:25 +01003072 if (dev->wl->current_beacon) {
3073 dev_kfree_skb_any(dev->wl->current_beacon);
3074 dev->wl->current_beacon = NULL;
3075 }
3076
Larry Finger75388ac2007-09-25 16:46:54 -07003077 ssb_device_disable(dev->dev, 0);
3078 ssb_bus_may_powerdown(dev->dev->bus);
3079}
3080
3081static void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
3082{
3083 struct b43legacy_phy *phy = &dev->phy;
3084 int i;
3085
3086 /* Set default attenuation values. */
3087 phy->bbatt = b43legacy_default_baseband_attenuation(dev);
3088 phy->rfatt = b43legacy_default_radio_attenuation(dev);
3089 phy->txctl1 = b43legacy_default_txctl1(dev);
3090 phy->txctl2 = 0xFFFF;
3091 phy->txpwr_offset = 0;
3092
3093 /* NRSSI */
3094 phy->nrssislope = 0;
3095 for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
3096 phy->nrssi[i] = -1000;
3097 for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
3098 phy->nrssi_lt[i] = i;
3099
3100 phy->lofcal = 0xFFFF;
3101 phy->initval = 0xFFFF;
3102
3103 phy->aci_enable = 0;
3104 phy->aci_wlan_automatic = 0;
3105 phy->aci_hw_rssi = 0;
3106
3107 phy->antenna_diversity = 0xFFFF;
3108 memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
3109 memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
3110
3111 /* Flags */
3112 phy->calibrated = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07003113
3114 if (phy->_lo_pairs)
3115 memset(phy->_lo_pairs, 0,
3116 sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
3117 memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
3118}
3119
3120/* Initialize a wireless core */
3121static int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
3122{
3123 struct b43legacy_wl *wl = dev->wl;
3124 struct ssb_bus *bus = dev->dev->bus;
3125 struct b43legacy_phy *phy = &dev->phy;
3126 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
3127 int err;
3128 u32 hf;
3129 u32 tmp;
3130
3131 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
3132
3133 err = ssb_bus_powerup(bus, 0);
3134 if (err)
3135 goto out;
3136 if (!ssb_device_is_enabled(dev->dev)) {
3137 tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
3138 b43legacy_wireless_core_reset(dev, tmp);
3139 }
3140
3141 if ((phy->type == B43legacy_PHYTYPE_B) ||
3142 (phy->type == B43legacy_PHYTYPE_G)) {
3143 phy->_lo_pairs = kzalloc(sizeof(struct b43legacy_lopair)
3144 * B43legacy_LO_COUNT,
3145 GFP_KERNEL);
3146 if (!phy->_lo_pairs)
3147 return -ENOMEM;
3148 }
3149 setup_struct_wldev_for_init(dev);
3150
3151 err = b43legacy_phy_init_tssi2dbm_table(dev);
3152 if (err)
3153 goto err_kfree_lo_control;
3154
3155 /* Enable IRQ routing to this device. */
3156 ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
3157
3158 b43legacy_imcfglo_timeouts_workaround(dev);
3159 prepare_phy_data_for_init(dev);
3160 b43legacy_phy_calibrate(dev);
3161 err = b43legacy_chip_init(dev);
3162 if (err)
3163 goto err_kfree_tssitbl;
3164 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3165 B43legacy_SHM_SH_WLCOREREV,
3166 dev->dev->id.revision);
3167 hf = b43legacy_hf_read(dev);
3168 if (phy->type == B43legacy_PHYTYPE_G) {
3169 hf |= B43legacy_HF_SYMW;
3170 if (phy->rev == 1)
3171 hf |= B43legacy_HF_GDCW;
Larry Finger7797aa32007-11-09 16:57:34 -06003172 if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
Larry Finger75388ac2007-09-25 16:46:54 -07003173 hf |= B43legacy_HF_OFDMPABOOST;
3174 } else if (phy->type == B43legacy_PHYTYPE_B) {
3175 hf |= B43legacy_HF_SYMW;
3176 if (phy->rev >= 2 && phy->radio_ver == 0x2050)
3177 hf &= ~B43legacy_HF_GDCW;
3178 }
3179 b43legacy_hf_write(dev, hf);
3180
Stefano Brivio0a6e1be2007-11-06 22:48:12 +01003181 b43legacy_set_retry_limits(dev,
3182 B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
3183 B43legacy_DEFAULT_LONG_RETRY_LIMIT);
Larry Finger75388ac2007-09-25 16:46:54 -07003184
3185 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3186 0x0044, 3);
3187 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3188 0x0046, 2);
3189
3190 /* Disable sending probe responses from firmware.
3191 * Setting the MaxTime to one usec will always trigger
3192 * a timeout, so we never send any probe resp.
3193 * A timeout of zero is infinite. */
3194 b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3195 B43legacy_SHM_SH_PRMAXTIME, 1);
3196
3197 b43legacy_rate_memory_init(dev);
3198
3199 /* Minimum Contention Window */
3200 if (phy->type == B43legacy_PHYTYPE_B)
3201 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3202 0x0003, 31);
3203 else
3204 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3205 0x0003, 15);
3206 /* Maximum Contention Window */
3207 b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
3208 0x0004, 1023);
3209
3210 do {
3211 if (b43legacy_using_pio(dev))
3212 err = b43legacy_pio_init(dev);
3213 else {
3214 err = b43legacy_dma_init(dev);
3215 if (!err)
3216 b43legacy_qos_init(dev);
3217 }
3218 } while (err == -EAGAIN);
3219 if (err)
3220 goto err_chip_exit;
3221
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003222 b43legacy_set_synth_pu_delay(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003223
3224 ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
Johannes Berg4150c572007-09-17 01:29:23 -04003225 b43legacy_upload_card_macaddress(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003226 b43legacy_security_init(dev);
3227 b43legacy_rng_init(wl);
3228
3229 b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
3230
Larry Finger4ad36d72007-12-16 19:21:06 +01003231 b43legacy_leds_init(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003232out:
3233 return err;
3234
3235err_chip_exit:
3236 b43legacy_chip_exit(dev);
3237err_kfree_tssitbl:
3238 if (phy->dyn_tssi_tbl)
3239 kfree(phy->tssi2dbm);
3240err_kfree_lo_control:
3241 kfree(phy->lo_control);
3242 phy->lo_control = NULL;
3243 ssb_bus_may_powerdown(bus);
3244 B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
3245 return err;
3246}
3247
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003248static int b43legacy_op_add_interface(struct ieee80211_hw *hw,
3249 struct ieee80211_if_init_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07003250{
3251 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3252 struct b43legacy_wldev *dev;
3253 unsigned long flags;
3254 int err = -EOPNOTSUPP;
Johannes Berg4150c572007-09-17 01:29:23 -04003255
3256 /* TODO: allow WDS/AP devices to coexist */
3257
3258 if (conf->type != IEEE80211_IF_TYPE_AP &&
3259 conf->type != IEEE80211_IF_TYPE_STA &&
3260 conf->type != IEEE80211_IF_TYPE_WDS &&
3261 conf->type != IEEE80211_IF_TYPE_IBSS)
3262 return -EOPNOTSUPP;
Larry Finger75388ac2007-09-25 16:46:54 -07003263
3264 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04003265 if (wl->operating)
Larry Finger75388ac2007-09-25 16:46:54 -07003266 goto out_mutex_unlock;
3267
3268 b43legacydbg(wl, "Adding Interface type %d\n", conf->type);
3269
3270 dev = wl->current_dev;
Johannes Berg4150c572007-09-17 01:29:23 -04003271 wl->operating = 1;
Johannes Berg32bfd352007-12-19 01:31:26 +01003272 wl->vif = conf->vif;
Johannes Berg4150c572007-09-17 01:29:23 -04003273 wl->if_type = conf->type;
3274 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
Larry Finger75388ac2007-09-25 16:46:54 -07003275
3276 spin_lock_irqsave(&wl->irq_lock, flags);
Larry Finger75388ac2007-09-25 16:46:54 -07003277 b43legacy_adjust_opmode(dev);
Stefano Brivio3e2c40e2008-04-14 00:57:03 +02003278 b43legacy_set_pretbtt(dev);
3279 b43legacy_set_synth_pu_delay(dev, 0);
Johannes Berg4150c572007-09-17 01:29:23 -04003280 b43legacy_upload_card_macaddress(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003281 spin_unlock_irqrestore(&wl->irq_lock, flags);
3282
3283 err = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04003284 out_mutex_unlock:
Larry Finger75388ac2007-09-25 16:46:54 -07003285 mutex_unlock(&wl->mutex);
3286
3287 return err;
3288}
3289
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003290static void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
3291 struct ieee80211_if_init_conf *conf)
Larry Finger75388ac2007-09-25 16:46:54 -07003292{
3293 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
Johannes Berg4150c572007-09-17 01:29:23 -04003294 struct b43legacy_wldev *dev = wl->current_dev;
Larry Finger75388ac2007-09-25 16:46:54 -07003295 unsigned long flags;
3296
3297 b43legacydbg(wl, "Removing Interface type %d\n", conf->type);
3298
3299 mutex_lock(&wl->mutex);
Johannes Berg4150c572007-09-17 01:29:23 -04003300
3301 B43legacy_WARN_ON(!wl->operating);
Johannes Berg32bfd352007-12-19 01:31:26 +01003302 B43legacy_WARN_ON(wl->vif != conf->vif);
3303 wl->vif = NULL;
Johannes Berg4150c572007-09-17 01:29:23 -04003304
3305 wl->operating = 0;
3306
3307 spin_lock_irqsave(&wl->irq_lock, flags);
3308 b43legacy_adjust_opmode(dev);
3309 memset(wl->mac_addr, 0, ETH_ALEN);
3310 b43legacy_upload_card_macaddress(dev);
3311 spin_unlock_irqrestore(&wl->irq_lock, flags);
3312
3313 mutex_unlock(&wl->mutex);
3314}
3315
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003316static int b43legacy_op_start(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04003317{
3318 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3319 struct b43legacy_wldev *dev = wl->current_dev;
3320 int did_init = 0;
Larry Finger208eec82007-10-14 16:04:30 -05003321 int err = 0;
Michael Buesch8712f272008-01-23 12:15:38 +01003322 bool do_rfkill_exit = 0;
Johannes Berg4150c572007-09-17 01:29:23 -04003323
Larry Finger4ad36d72007-12-16 19:21:06 +01003324 /* First register RFkill.
3325 * LEDs that are registered later depend on it. */
3326 b43legacy_rfkill_init(dev);
3327
Stefano Brivioada50732008-02-02 19:15:57 +01003328 /* Kill all old instance specific information to make sure
3329 * the card won't use it in the short timeframe between start
3330 * and mac80211 reconfiguring it. */
3331 memset(wl->bssid, 0, ETH_ALEN);
3332 memset(wl->mac_addr, 0, ETH_ALEN);
3333 wl->filter_flags = 0;
3334
Johannes Berg4150c572007-09-17 01:29:23 -04003335 mutex_lock(&wl->mutex);
3336
3337 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
3338 err = b43legacy_wireless_core_init(dev);
Michael Buesch8712f272008-01-23 12:15:38 +01003339 if (err) {
3340 do_rfkill_exit = 1;
Johannes Berg4150c572007-09-17 01:29:23 -04003341 goto out_mutex_unlock;
Michael Buesch8712f272008-01-23 12:15:38 +01003342 }
Johannes Berg4150c572007-09-17 01:29:23 -04003343 did_init = 1;
Larry Finger75388ac2007-09-25 16:46:54 -07003344 }
3345
Johannes Berg4150c572007-09-17 01:29:23 -04003346 if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
3347 err = b43legacy_wireless_core_start(dev);
3348 if (err) {
3349 if (did_init)
3350 b43legacy_wireless_core_exit(dev);
Michael Buesch8712f272008-01-23 12:15:38 +01003351 do_rfkill_exit = 1;
Johannes Berg4150c572007-09-17 01:29:23 -04003352 goto out_mutex_unlock;
3353 }
Larry Finger75388ac2007-09-25 16:46:54 -07003354 }
Johannes Berg4150c572007-09-17 01:29:23 -04003355
3356out_mutex_unlock:
3357 mutex_unlock(&wl->mutex);
3358
Michael Buesch8712f272008-01-23 12:15:38 +01003359 if (do_rfkill_exit)
3360 b43legacy_rfkill_exit(dev);
3361
Johannes Berg4150c572007-09-17 01:29:23 -04003362 return err;
3363}
3364
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003365static void b43legacy_op_stop(struct ieee80211_hw *hw)
Johannes Berg4150c572007-09-17 01:29:23 -04003366{
3367 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3368 struct b43legacy_wldev *dev = wl->current_dev;
3369
Larry Finger4ad36d72007-12-16 19:21:06 +01003370 b43legacy_rfkill_exit(dev);
3371
Johannes Berg4150c572007-09-17 01:29:23 -04003372 mutex_lock(&wl->mutex);
3373 if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
3374 b43legacy_wireless_core_stop(dev);
3375 b43legacy_wireless_core_exit(dev);
Larry Finger75388ac2007-09-25 16:46:54 -07003376 mutex_unlock(&wl->mutex);
3377}
3378
Stefano Brivio0a6e1be2007-11-06 22:48:12 +01003379static int b43legacy_op_set_retry_limit(struct ieee80211_hw *hw,
3380 u32 short_retry_limit,
3381 u32 long_retry_limit)
3382{
3383 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3384 struct b43legacy_wldev *dev;
3385 int err = 0;
3386
3387 mutex_lock(&wl->mutex);
3388 dev = wl->current_dev;
3389 if (unlikely(!dev ||
3390 (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED))) {
3391 err = -ENODEV;
3392 goto out_unlock;
3393 }
3394 b43legacy_set_retry_limits(dev, short_retry_limit, long_retry_limit);
3395out_unlock:
3396 mutex_unlock(&wl->mutex);
3397
3398 return err;
3399}
Larry Finger75388ac2007-09-25 16:46:54 -07003400
Stefano Brivioa2971702008-02-08 06:31:25 +01003401static int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
3402 int aid, int set)
3403{
3404 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3405 struct sk_buff *beacon;
3406 unsigned long flags;
3407
3408 /* We could modify the existing beacon and set the aid bit in the TIM
3409 * field, but that would probably require resizing and moving of data
3410 * within the beacon template. Simply request a new beacon and let
3411 * mac80211 do the hard work. */
3412 beacon = ieee80211_beacon_get(hw, wl->vif, NULL);
3413 if (unlikely(!beacon))
3414 return -ENOMEM;
3415 spin_lock_irqsave(&wl->irq_lock, flags);
3416 b43legacy_update_templates(wl, beacon);
3417 spin_unlock_irqrestore(&wl->irq_lock, flags);
3418
3419 return 0;
3420}
3421
3422static int b43legacy_op_ibss_beacon_update(struct ieee80211_hw *hw,
3423 struct sk_buff *beacon,
3424 struct ieee80211_tx_control *ctl)
3425{
3426 struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
3427 unsigned long flags;
3428
3429 spin_lock_irqsave(&wl->irq_lock, flags);
3430 b43legacy_update_templates(wl, beacon);
3431 spin_unlock_irqrestore(&wl->irq_lock, flags);
3432
3433 return 0;
3434}
3435
Larry Finger75388ac2007-09-25 16:46:54 -07003436static const struct ieee80211_ops b43legacy_hw_ops = {
Stefano Brivio33a3dc92007-11-06 22:48:25 +01003437 .tx = b43legacy_op_tx,
3438 .conf_tx = b43legacy_op_conf_tx,
3439 .add_interface = b43legacy_op_add_interface,
3440 .remove_interface = b43legacy_op_remove_interface,
3441 .config = b43legacy_op_dev_config,
3442 .config_interface = b43legacy_op_config_interface,
3443 .configure_filter = b43legacy_op_configure_filter,
3444 .get_stats = b43legacy_op_get_stats,
3445 .get_tx_stats = b43legacy_op_get_tx_stats,
3446 .start = b43legacy_op_start,
3447 .stop = b43legacy_op_stop,
Stefano Brivio0a6e1be2007-11-06 22:48:12 +01003448 .set_retry_limit = b43legacy_op_set_retry_limit,
Stefano Brivioa2971702008-02-08 06:31:25 +01003449 .set_tim = b43legacy_op_beacon_set_tim,
3450 .beacon_update = b43legacy_op_ibss_beacon_update,
Larry Finger75388ac2007-09-25 16:46:54 -07003451};
3452
3453/* Hard-reset the chip. Do not call this directly.
3454 * Use b43legacy_controller_restart()
3455 */
3456static void b43legacy_chip_reset(struct work_struct *work)
3457{
3458 struct b43legacy_wldev *dev =
3459 container_of(work, struct b43legacy_wldev, restart_work);
3460 struct b43legacy_wl *wl = dev->wl;
3461 int err = 0;
3462 int prev_status;
3463
3464 mutex_lock(&wl->mutex);
3465
3466 prev_status = b43legacy_status(dev);
3467 /* Bring the device down... */
3468 if (prev_status >= B43legacy_STAT_STARTED)
3469 b43legacy_wireless_core_stop(dev);
3470 if (prev_status >= B43legacy_STAT_INITIALIZED)
3471 b43legacy_wireless_core_exit(dev);
3472
3473 /* ...and up again. */
3474 if (prev_status >= B43legacy_STAT_INITIALIZED) {
3475 err = b43legacy_wireless_core_init(dev);
3476 if (err)
3477 goto out;
3478 }
3479 if (prev_status >= B43legacy_STAT_STARTED) {
3480 err = b43legacy_wireless_core_start(dev);
3481 if (err) {
3482 b43legacy_wireless_core_exit(dev);
3483 goto out;
3484 }
3485 }
3486out:
3487 mutex_unlock(&wl->mutex);
3488 if (err)
3489 b43legacyerr(wl, "Controller restart FAILED\n");
3490 else
3491 b43legacyinfo(wl, "Controller restarted\n");
3492}
3493
3494static int b43legacy_setup_modes(struct b43legacy_wldev *dev,
3495 int have_bphy,
3496 int have_gphy)
3497{
3498 struct ieee80211_hw *hw = dev->wl->hw;
Larry Finger75388ac2007-09-25 16:46:54 -07003499 struct b43legacy_phy *phy = &dev->phy;
Larry Finger75388ac2007-09-25 16:46:54 -07003500
3501 phy->possible_phymodes = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003502 if (have_bphy) {
3503 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3504 &b43legacy_band_2GHz_BPHY;
3505 phy->possible_phymodes |= B43legacy_PHYMODE_B;
3506 }
Larry Finger75388ac2007-09-25 16:46:54 -07003507
Johannes Berg8318d782008-01-24 19:38:38 +01003508 if (have_gphy) {
3509 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
3510 &b43legacy_band_2GHz_GPHY;
3511 phy->possible_phymodes |= B43legacy_PHYMODE_G;
Larry Finger75388ac2007-09-25 16:46:54 -07003512 }
3513
3514 return 0;
3515}
3516
3517static void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
3518{
3519 /* We release firmware that late to not be required to re-request
3520 * is all the time when we reinit the core. */
3521 b43legacy_release_firmware(dev);
3522}
3523
3524static int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
3525{
3526 struct b43legacy_wl *wl = dev->wl;
3527 struct ssb_bus *bus = dev->dev->bus;
3528 struct pci_dev *pdev = bus->host_pci;
3529 int err;
3530 int have_bphy = 0;
3531 int have_gphy = 0;
3532 u32 tmp;
3533
3534 /* Do NOT do any device initialization here.
3535 * Do it in wireless_core_init() instead.
3536 * This function is for gathering basic information about the HW, only.
3537 * Also some structs may be set up here. But most likely you want to
3538 * have that in core_init(), too.
3539 */
3540
3541 err = ssb_bus_powerup(bus, 0);
3542 if (err) {
3543 b43legacyerr(wl, "Bus powerup failed\n");
3544 goto out;
3545 }
3546 /* Get the PHY type. */
3547 if (dev->dev->id.revision >= 5) {
3548 u32 tmshigh;
3549
3550 tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
3551 have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
3552 if (!have_gphy)
3553 have_bphy = 1;
3554 } else if (dev->dev->id.revision == 4)
3555 have_gphy = 1;
3556 else
3557 have_bphy = 1;
3558
Larry Finger75388ac2007-09-25 16:46:54 -07003559 dev->phy.gmode = (have_gphy || have_bphy);
3560 tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
3561 b43legacy_wireless_core_reset(dev, tmp);
3562
3563 err = b43legacy_phy_versioning(dev);
3564 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003565 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003566 /* Check if this device supports multiband. */
3567 if (!pdev ||
3568 (pdev->device != 0x4312 &&
3569 pdev->device != 0x4319 &&
3570 pdev->device != 0x4324)) {
3571 /* No multiband support. */
3572 have_bphy = 0;
3573 have_gphy = 0;
3574 switch (dev->phy.type) {
3575 case B43legacy_PHYTYPE_B:
3576 have_bphy = 1;
3577 break;
3578 case B43legacy_PHYTYPE_G:
3579 have_gphy = 1;
3580 break;
3581 default:
3582 B43legacy_BUG_ON(1);
3583 }
3584 }
3585 dev->phy.gmode = (have_gphy || have_bphy);
3586 tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
3587 b43legacy_wireless_core_reset(dev, tmp);
3588
3589 err = b43legacy_validate_chipaccess(dev);
3590 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003591 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003592 err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
3593 if (err)
Larry Fingerba48f7b2007-10-12 23:04:51 -05003594 goto err_powerdown;
Larry Finger75388ac2007-09-25 16:46:54 -07003595
3596 /* Now set some default "current_dev" */
3597 if (!wl->current_dev)
3598 wl->current_dev = dev;
3599 INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
3600
Larry Finger93bb7f32007-10-10 22:44:22 -05003601 b43legacy_radio_turn_off(dev, 1);
Larry Finger75388ac2007-09-25 16:46:54 -07003602 b43legacy_switch_analog(dev, 0);
3603 ssb_device_disable(dev->dev, 0);
3604 ssb_bus_may_powerdown(bus);
3605
3606out:
3607 return err;
3608
Larry Finger75388ac2007-09-25 16:46:54 -07003609err_powerdown:
3610 ssb_bus_may_powerdown(bus);
3611 return err;
3612}
3613
3614static void b43legacy_one_core_detach(struct ssb_device *dev)
3615{
3616 struct b43legacy_wldev *wldev;
3617 struct b43legacy_wl *wl;
3618
3619 wldev = ssb_get_drvdata(dev);
3620 wl = wldev->wl;
3621 cancel_work_sync(&wldev->restart_work);
3622 b43legacy_debugfs_remove_device(wldev);
3623 b43legacy_wireless_core_detach(wldev);
3624 list_del(&wldev->list);
3625 wl->nr_devs--;
3626 ssb_set_drvdata(dev, NULL);
3627 kfree(wldev);
3628}
3629
3630static int b43legacy_one_core_attach(struct ssb_device *dev,
3631 struct b43legacy_wl *wl)
3632{
3633 struct b43legacy_wldev *wldev;
3634 struct pci_dev *pdev;
3635 int err = -ENOMEM;
3636
3637 if (!list_empty(&wl->devlist)) {
3638 /* We are not the first core on this chip. */
3639 pdev = dev->bus->host_pci;
3640 /* Only special chips support more than one wireless
3641 * core, although some of the other chips have more than
3642 * one wireless core as well. Check for this and
3643 * bail out early.
3644 */
3645 if (!pdev ||
3646 ((pdev->device != 0x4321) &&
3647 (pdev->device != 0x4313) &&
3648 (pdev->device != 0x431A))) {
3649 b43legacydbg(wl, "Ignoring unconnected 802.11 core\n");
3650 return -ENODEV;
3651 }
3652 }
3653
3654 wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
3655 if (!wldev)
3656 goto out;
3657
3658 wldev->dev = dev;
3659 wldev->wl = wl;
3660 b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
3661 wldev->bad_frames_preempt = modparam_bad_frames_preempt;
3662 tasklet_init(&wldev->isr_tasklet,
3663 (void (*)(unsigned long))b43legacy_interrupt_tasklet,
3664 (unsigned long)wldev);
3665 if (modparam_pio)
3666 wldev->__using_pio = 1;
3667 INIT_LIST_HEAD(&wldev->list);
3668
3669 err = b43legacy_wireless_core_attach(wldev);
3670 if (err)
3671 goto err_kfree_wldev;
3672
3673 list_add(&wldev->list, &wl->devlist);
3674 wl->nr_devs++;
3675 ssb_set_drvdata(dev, wldev);
3676 b43legacy_debugfs_add_device(wldev);
3677out:
3678 return err;
3679
3680err_kfree_wldev:
3681 kfree(wldev);
3682 return err;
3683}
3684
3685static void b43legacy_sprom_fixup(struct ssb_bus *bus)
3686{
3687 /* boardflags workarounds */
3688 if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
3689 bus->boardinfo.type == 0x4E &&
3690 bus->boardinfo.rev > 0x40)
Larry Finger7797aa32007-11-09 16:57:34 -06003691 bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
Larry Finger75388ac2007-09-25 16:46:54 -07003692}
3693
3694static void b43legacy_wireless_exit(struct ssb_device *dev,
3695 struct b43legacy_wl *wl)
3696{
3697 struct ieee80211_hw *hw = wl->hw;
3698
3699 ssb_set_devtypedata(dev, NULL);
3700 ieee80211_free_hw(hw);
3701}
3702
3703static int b43legacy_wireless_init(struct ssb_device *dev)
3704{
3705 struct ssb_sprom *sprom = &dev->bus->sprom;
3706 struct ieee80211_hw *hw;
3707 struct b43legacy_wl *wl;
3708 int err = -ENOMEM;
3709
3710 b43legacy_sprom_fixup(dev->bus);
3711
3712 hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
3713 if (!hw) {
3714 b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
3715 goto out;
3716 }
3717
3718 /* fill hw info */
3719 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
3720 IEEE80211_HW_RX_INCLUDES_FCS;
3721 hw->max_signal = 100;
3722 hw->max_rssi = -110;
3723 hw->max_noise = -110;
3724 hw->queues = 1; /* FIXME: hardware has more queues */
3725 SET_IEEE80211_DEV(hw, dev->dev);
Larry Finger7797aa32007-11-09 16:57:34 -06003726 if (is_valid_ether_addr(sprom->et1mac))
3727 SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
Larry Finger75388ac2007-09-25 16:46:54 -07003728 else
Larry Finger7797aa32007-11-09 16:57:34 -06003729 SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
Larry Finger75388ac2007-09-25 16:46:54 -07003730
3731 /* Get and initialize struct b43legacy_wl */
3732 wl = hw_to_b43legacy_wl(hw);
3733 memset(wl, 0, sizeof(*wl));
3734 wl->hw = hw;
3735 spin_lock_init(&wl->irq_lock);
3736 spin_lock_init(&wl->leds_lock);
3737 mutex_init(&wl->mutex);
3738 INIT_LIST_HEAD(&wl->devlist);
3739
3740 ssb_set_devtypedata(dev, wl);
3741 b43legacyinfo(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
3742 err = 0;
3743out:
3744 return err;
3745}
3746
3747static int b43legacy_probe(struct ssb_device *dev,
3748 const struct ssb_device_id *id)
3749{
3750 struct b43legacy_wl *wl;
3751 int err;
3752 int first = 0;
3753
3754 wl = ssb_get_devtypedata(dev);
3755 if (!wl) {
3756 /* Probing the first core - setup common struct b43legacy_wl */
3757 first = 1;
3758 err = b43legacy_wireless_init(dev);
3759 if (err)
3760 goto out;
3761 wl = ssb_get_devtypedata(dev);
3762 B43legacy_WARN_ON(!wl);
3763 }
3764 err = b43legacy_one_core_attach(dev, wl);
3765 if (err)
3766 goto err_wireless_exit;
3767
3768 if (first) {
3769 err = ieee80211_register_hw(wl->hw);
3770 if (err)
3771 goto err_one_core_detach;
3772 }
3773
3774out:
3775 return err;
3776
3777err_one_core_detach:
3778 b43legacy_one_core_detach(dev);
3779err_wireless_exit:
3780 if (first)
3781 b43legacy_wireless_exit(dev, wl);
3782 return err;
3783}
3784
3785static void b43legacy_remove(struct ssb_device *dev)
3786{
3787 struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
3788 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3789
3790 B43legacy_WARN_ON(!wl);
3791 if (wl->current_dev == wldev)
3792 ieee80211_unregister_hw(wl->hw);
3793
3794 b43legacy_one_core_detach(dev);
3795
3796 if (list_empty(&wl->devlist))
3797 /* Last core on the chip unregistered.
3798 * We can destroy common struct b43legacy_wl.
3799 */
3800 b43legacy_wireless_exit(dev, wl);
3801}
3802
3803/* Perform a hardware reset. This can be called from any context. */
3804void b43legacy_controller_restart(struct b43legacy_wldev *dev,
3805 const char *reason)
3806{
3807 /* Must avoid requeueing, if we are in shutdown. */
3808 if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
3809 return;
3810 b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
3811 queue_work(dev->wl->hw->workqueue, &dev->restart_work);
3812}
3813
3814#ifdef CONFIG_PM
3815
3816static int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
3817{
3818 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3819 struct b43legacy_wl *wl = wldev->wl;
3820
3821 b43legacydbg(wl, "Suspending...\n");
3822
3823 mutex_lock(&wl->mutex);
3824 wldev->suspend_init_status = b43legacy_status(wldev);
3825 if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
3826 b43legacy_wireless_core_stop(wldev);
3827 if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
3828 b43legacy_wireless_core_exit(wldev);
3829 mutex_unlock(&wl->mutex);
3830
3831 b43legacydbg(wl, "Device suspended.\n");
3832
3833 return 0;
3834}
3835
3836static int b43legacy_resume(struct ssb_device *dev)
3837{
3838 struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
3839 struct b43legacy_wl *wl = wldev->wl;
3840 int err = 0;
3841
3842 b43legacydbg(wl, "Resuming...\n");
3843
3844 mutex_lock(&wl->mutex);
3845 if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
3846 err = b43legacy_wireless_core_init(wldev);
3847 if (err) {
3848 b43legacyerr(wl, "Resume failed at core init\n");
3849 goto out;
3850 }
3851 }
3852 if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
3853 err = b43legacy_wireless_core_start(wldev);
3854 if (err) {
3855 b43legacy_wireless_core_exit(wldev);
3856 b43legacyerr(wl, "Resume failed at core start\n");
3857 goto out;
3858 }
3859 }
3860 mutex_unlock(&wl->mutex);
3861
3862 b43legacydbg(wl, "Device resumed.\n");
3863out:
3864 return err;
3865}
3866
3867#else /* CONFIG_PM */
3868# define b43legacy_suspend NULL
3869# define b43legacy_resume NULL
3870#endif /* CONFIG_PM */
3871
3872static struct ssb_driver b43legacy_ssb_driver = {
3873 .name = KBUILD_MODNAME,
3874 .id_table = b43legacy_ssb_tbl,
3875 .probe = b43legacy_probe,
3876 .remove = b43legacy_remove,
3877 .suspend = b43legacy_suspend,
3878 .resume = b43legacy_resume,
3879};
3880
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003881static void b43legacy_print_driverinfo(void)
3882{
3883 const char *feat_pci = "", *feat_leds = "", *feat_rfkill = "",
3884 *feat_pio = "", *feat_dma = "";
3885
3886#ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
3887 feat_pci = "P";
3888#endif
3889#ifdef CONFIG_B43LEGACY_LEDS
3890 feat_leds = "L";
3891#endif
3892#ifdef CONFIG_B43LEGACY_RFKILL
3893 feat_rfkill = "R";
3894#endif
3895#ifdef CONFIG_B43LEGACY_PIO
3896 feat_pio = "I";
3897#endif
3898#ifdef CONFIG_B43LEGACY_DMA
3899 feat_dma = "D";
3900#endif
Michael Bueschc256e052008-03-04 20:31:13 +01003901 printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003902 "[ Features: %s%s%s%s%s, Firmware-ID: "
3903 B43legacy_SUPPORTED_FIRMWARE_ID " ]\n",
3904 feat_pci, feat_leds, feat_rfkill, feat_pio, feat_dma);
3905}
3906
Larry Finger75388ac2007-09-25 16:46:54 -07003907static int __init b43legacy_init(void)
3908{
3909 int err;
3910
3911 b43legacy_debugfs_init();
3912
3913 err = ssb_driver_register(&b43legacy_ssb_driver);
3914 if (err)
3915 goto err_dfs_exit;
3916
Stefano Brivio6fff1c62008-02-09 07:20:43 +01003917 b43legacy_print_driverinfo();
3918
Larry Finger75388ac2007-09-25 16:46:54 -07003919 return err;
3920
3921err_dfs_exit:
3922 b43legacy_debugfs_exit();
3923 return err;
3924}
3925
3926static void __exit b43legacy_exit(void)
3927{
3928 ssb_driver_unregister(&b43legacy_ssb_driver);
3929 b43legacy_debugfs_exit();
3930}
3931
3932module_init(b43legacy_init)
3933module_exit(b43legacy_exit)