18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *  Broadcom B43legacy wireless driver
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
78c2ecf20Sopenharmony_ci *  Copyright (c) 2005-2008 Stefano Brivio <stefano.brivio@polimi.it>
88c2ecf20Sopenharmony_ci *  Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch>
98c2ecf20Sopenharmony_ci *  Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
108c2ecf20Sopenharmony_ci *  Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
118c2ecf20Sopenharmony_ci *  Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *  Some parts of the code in this file are derived from the ipw2200
148c2ecf20Sopenharmony_ci *  driver  Copyright(c) 2003 - 2004 Intel Corporation.
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
228c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
238c2ecf20Sopenharmony_ci#include <linux/firmware.h>
248c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
258c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
268c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
278c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
288c2ecf20Sopenharmony_ci#include <linux/slab.h>
298c2ecf20Sopenharmony_ci#include <net/dst.h>
308c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "b43legacy.h"
338c2ecf20Sopenharmony_ci#include "main.h"
348c2ecf20Sopenharmony_ci#include "debugfs.h"
358c2ecf20Sopenharmony_ci#include "phy.h"
368c2ecf20Sopenharmony_ci#include "dma.h"
378c2ecf20Sopenharmony_ci#include "pio.h"
388c2ecf20Sopenharmony_ci#include "sysfs.h"
398c2ecf20Sopenharmony_ci#include "xmit.h"
408c2ecf20Sopenharmony_ci#include "radio.h"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Broadcom B43legacy wireless driver");
448c2ecf20Sopenharmony_ciMODULE_AUTHOR("Martin Langer");
458c2ecf20Sopenharmony_ciMODULE_AUTHOR("Stefano Brivio");
468c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Buesch");
478c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciMODULE_FIRMWARE("b43legacy/ucode2.fw");
508c2ecf20Sopenharmony_ciMODULE_FIRMWARE("b43legacy/ucode4.fw");
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#if defined(CONFIG_B43LEGACY_DMA) && defined(CONFIG_B43LEGACY_PIO)
538c2ecf20Sopenharmony_cistatic int modparam_pio;
548c2ecf20Sopenharmony_cimodule_param_named(pio, modparam_pio, int, 0444);
558c2ecf20Sopenharmony_ciMODULE_PARM_DESC(pio, "enable(1) / disable(0) PIO mode");
568c2ecf20Sopenharmony_ci#elif defined(CONFIG_B43LEGACY_DMA)
578c2ecf20Sopenharmony_ci# define modparam_pio	0
588c2ecf20Sopenharmony_ci#elif defined(CONFIG_B43LEGACY_PIO)
598c2ecf20Sopenharmony_ci# define modparam_pio	1
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int modparam_bad_frames_preempt;
638c2ecf20Sopenharmony_cimodule_param_named(bad_frames_preempt, modparam_bad_frames_preempt, int, 0444);
648c2ecf20Sopenharmony_ciMODULE_PARM_DESC(bad_frames_preempt, "enable(1) / disable(0) Bad Frames"
658c2ecf20Sopenharmony_ci		 " Preemption");
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic char modparam_fwpostfix[16];
688c2ecf20Sopenharmony_cimodule_param_string(fwpostfix, modparam_fwpostfix, 16, 0444);
698c2ecf20Sopenharmony_ciMODULE_PARM_DESC(fwpostfix, "Postfix for the firmware files to load.");
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* The following table supports BCM4301, BCM4303 and BCM4306/2 devices. */
728c2ecf20Sopenharmony_cistatic const struct ssb_device_id b43legacy_ssb_tbl[] = {
738c2ecf20Sopenharmony_ci	SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 2),
748c2ecf20Sopenharmony_ci	SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 4),
758c2ecf20Sopenharmony_ci	{},
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(ssb, b43legacy_ssb_tbl);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* Channel and ratetables are shared for all devices.
818c2ecf20Sopenharmony_ci * They can't be const, because ieee80211 puts some precalculated
828c2ecf20Sopenharmony_ci * data in there. This data is the same for all devices, so we don't
838c2ecf20Sopenharmony_ci * get concurrency issues */
848c2ecf20Sopenharmony_ci#define RATETAB_ENT(_rateid, _flags) \
858c2ecf20Sopenharmony_ci	{								\
868c2ecf20Sopenharmony_ci		.bitrate	= B43legacy_RATE_TO_100KBPS(_rateid),	\
878c2ecf20Sopenharmony_ci		.hw_value	= (_rateid),				\
888c2ecf20Sopenharmony_ci		.flags		= (_flags),				\
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci/*
918c2ecf20Sopenharmony_ci * NOTE: When changing this, sync with xmit.c's
928c2ecf20Sopenharmony_ci *	 b43legacy_plcp_get_bitrate_idx_* functions!
938c2ecf20Sopenharmony_ci */
948c2ecf20Sopenharmony_cistatic struct ieee80211_rate __b43legacy_ratetable[] = {
958c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_CCK_RATE_1MB, 0),
968c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_CCK_RATE_2MB, IEEE80211_RATE_SHORT_PREAMBLE),
978c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_CCK_RATE_5MB, IEEE80211_RATE_SHORT_PREAMBLE),
988c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_CCK_RATE_11MB, IEEE80211_RATE_SHORT_PREAMBLE),
998c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_6MB, 0),
1008c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_9MB, 0),
1018c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_12MB, 0),
1028c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_18MB, 0),
1038c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_24MB, 0),
1048c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_36MB, 0),
1058c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_48MB, 0),
1068c2ecf20Sopenharmony_ci	RATETAB_ENT(B43legacy_OFDM_RATE_54MB, 0),
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci#define b43legacy_b_ratetable		(__b43legacy_ratetable + 0)
1098c2ecf20Sopenharmony_ci#define b43legacy_b_ratetable_size	4
1108c2ecf20Sopenharmony_ci#define b43legacy_g_ratetable		(__b43legacy_ratetable + 0)
1118c2ecf20Sopenharmony_ci#define b43legacy_g_ratetable_size	12
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define CHANTAB_ENT(_chanid, _freq) \
1148c2ecf20Sopenharmony_ci	{							\
1158c2ecf20Sopenharmony_ci		.center_freq	= (_freq),			\
1168c2ecf20Sopenharmony_ci		.hw_value	= (_chanid),			\
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_cistatic struct ieee80211_channel b43legacy_bg_chantable[] = {
1198c2ecf20Sopenharmony_ci	CHANTAB_ENT(1, 2412),
1208c2ecf20Sopenharmony_ci	CHANTAB_ENT(2, 2417),
1218c2ecf20Sopenharmony_ci	CHANTAB_ENT(3, 2422),
1228c2ecf20Sopenharmony_ci	CHANTAB_ENT(4, 2427),
1238c2ecf20Sopenharmony_ci	CHANTAB_ENT(5, 2432),
1248c2ecf20Sopenharmony_ci	CHANTAB_ENT(6, 2437),
1258c2ecf20Sopenharmony_ci	CHANTAB_ENT(7, 2442),
1268c2ecf20Sopenharmony_ci	CHANTAB_ENT(8, 2447),
1278c2ecf20Sopenharmony_ci	CHANTAB_ENT(9, 2452),
1288c2ecf20Sopenharmony_ci	CHANTAB_ENT(10, 2457),
1298c2ecf20Sopenharmony_ci	CHANTAB_ENT(11, 2462),
1308c2ecf20Sopenharmony_ci	CHANTAB_ENT(12, 2467),
1318c2ecf20Sopenharmony_ci	CHANTAB_ENT(13, 2472),
1328c2ecf20Sopenharmony_ci	CHANTAB_ENT(14, 2484),
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic struct ieee80211_supported_band b43legacy_band_2GHz_BPHY = {
1368c2ecf20Sopenharmony_ci	.channels = b43legacy_bg_chantable,
1378c2ecf20Sopenharmony_ci	.n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
1388c2ecf20Sopenharmony_ci	.bitrates = b43legacy_b_ratetable,
1398c2ecf20Sopenharmony_ci	.n_bitrates = b43legacy_b_ratetable_size,
1408c2ecf20Sopenharmony_ci};
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic struct ieee80211_supported_band b43legacy_band_2GHz_GPHY = {
1438c2ecf20Sopenharmony_ci	.channels = b43legacy_bg_chantable,
1448c2ecf20Sopenharmony_ci	.n_channels = ARRAY_SIZE(b43legacy_bg_chantable),
1458c2ecf20Sopenharmony_ci	.bitrates = b43legacy_g_ratetable,
1468c2ecf20Sopenharmony_ci	.n_bitrates = b43legacy_g_ratetable_size,
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev);
1508c2ecf20Sopenharmony_cistatic int b43legacy_wireless_core_init(struct b43legacy_wldev *dev);
1518c2ecf20Sopenharmony_cistatic void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev);
1528c2ecf20Sopenharmony_cistatic int b43legacy_wireless_core_start(struct b43legacy_wldev *dev);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic int b43legacy_ratelimit(struct b43legacy_wl *wl)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	if (!wl || !wl->current_dev)
1588c2ecf20Sopenharmony_ci		return 1;
1598c2ecf20Sopenharmony_ci	if (b43legacy_status(wl->current_dev) < B43legacy_STAT_STARTED)
1608c2ecf20Sopenharmony_ci		return 1;
1618c2ecf20Sopenharmony_ci	/* We are up and running.
1628c2ecf20Sopenharmony_ci	 * Ratelimit the messages to avoid DoS over the net. */
1638c2ecf20Sopenharmony_ci	return net_ratelimit();
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_civoid b43legacyinfo(struct b43legacy_wl *wl, const char *fmt, ...)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct va_format vaf;
1698c2ecf20Sopenharmony_ci	va_list args;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (!b43legacy_ratelimit(wl))
1728c2ecf20Sopenharmony_ci		return;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	va_start(args, fmt);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
1778c2ecf20Sopenharmony_ci	vaf.va = &args;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	printk(KERN_INFO "b43legacy-%s: %pV",
1808c2ecf20Sopenharmony_ci	       (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	va_end(args);
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_civoid b43legacyerr(struct b43legacy_wl *wl, const char *fmt, ...)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct va_format vaf;
1888c2ecf20Sopenharmony_ci	va_list args;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	if (!b43legacy_ratelimit(wl))
1918c2ecf20Sopenharmony_ci		return;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	va_start(args, fmt);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
1968c2ecf20Sopenharmony_ci	vaf.va = &args;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	printk(KERN_ERR "b43legacy-%s ERROR: %pV",
1998c2ecf20Sopenharmony_ci	       (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	va_end(args);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_civoid b43legacywarn(struct b43legacy_wl *wl, const char *fmt, ...)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	struct va_format vaf;
2078c2ecf20Sopenharmony_ci	va_list args;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (!b43legacy_ratelimit(wl))
2108c2ecf20Sopenharmony_ci		return;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	va_start(args, fmt);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
2158c2ecf20Sopenharmony_ci	vaf.va = &args;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	printk(KERN_WARNING "b43legacy-%s warning: %pV",
2188c2ecf20Sopenharmony_ci	       (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	va_end(args);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci#if B43legacy_DEBUG
2248c2ecf20Sopenharmony_civoid b43legacydbg(struct b43legacy_wl *wl, const char *fmt, ...)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	struct va_format vaf;
2278c2ecf20Sopenharmony_ci	va_list args;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	va_start(args, fmt);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	vaf.fmt = fmt;
2328c2ecf20Sopenharmony_ci	vaf.va = &args;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "b43legacy-%s debug: %pV",
2358c2ecf20Sopenharmony_ci	       (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan", &vaf);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	va_end(args);
2388c2ecf20Sopenharmony_ci}
2398c2ecf20Sopenharmony_ci#endif /* DEBUG */
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic void b43legacy_ram_write(struct b43legacy_wldev *dev, u16 offset,
2428c2ecf20Sopenharmony_ci				u32 val)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	u32 status;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(offset % 4 != 0);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
2498c2ecf20Sopenharmony_ci	if (status & B43legacy_MACCTL_BE)
2508c2ecf20Sopenharmony_ci		val = swab32(val);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_RAM_CONTROL, offset);
2538c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_RAM_DATA, val);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic inline
2578c2ecf20Sopenharmony_civoid b43legacy_shm_control_word(struct b43legacy_wldev *dev,
2588c2ecf20Sopenharmony_ci				u16 routing, u16 offset)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	u32 control;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* "offset" is the WORD offset. */
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	control = routing;
2658c2ecf20Sopenharmony_ci	control <<= 16;
2668c2ecf20Sopenharmony_ci	control |= offset;
2678c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_SHM_CONTROL, control);
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ciu32 b43legacy_shm_read32(struct b43legacy_wldev *dev,
2718c2ecf20Sopenharmony_ci		       u16 routing, u16 offset)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	u32 ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (routing == B43legacy_SHM_SHARED) {
2768c2ecf20Sopenharmony_ci		B43legacy_WARN_ON((offset & 0x0001) != 0);
2778c2ecf20Sopenharmony_ci		if (offset & 0x0003) {
2788c2ecf20Sopenharmony_ci			/* Unaligned access */
2798c2ecf20Sopenharmony_ci			b43legacy_shm_control_word(dev, routing, offset >> 2);
2808c2ecf20Sopenharmony_ci			ret = b43legacy_read16(dev,
2818c2ecf20Sopenharmony_ci				B43legacy_MMIO_SHM_DATA_UNALIGNED);
2828c2ecf20Sopenharmony_ci			ret <<= 16;
2838c2ecf20Sopenharmony_ci			b43legacy_shm_control_word(dev, routing,
2848c2ecf20Sopenharmony_ci						     (offset >> 2) + 1);
2858c2ecf20Sopenharmony_ci			ret |= b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci			return ret;
2888c2ecf20Sopenharmony_ci		}
2898c2ecf20Sopenharmony_ci		offset >>= 2;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci	b43legacy_shm_control_word(dev, routing, offset);
2928c2ecf20Sopenharmony_ci	ret = b43legacy_read32(dev, B43legacy_MMIO_SHM_DATA);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return ret;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciu16 b43legacy_shm_read16(struct b43legacy_wldev *dev,
2988c2ecf20Sopenharmony_ci			   u16 routing, u16 offset)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	u16 ret;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (routing == B43legacy_SHM_SHARED) {
3038c2ecf20Sopenharmony_ci		B43legacy_WARN_ON((offset & 0x0001) != 0);
3048c2ecf20Sopenharmony_ci		if (offset & 0x0003) {
3058c2ecf20Sopenharmony_ci			/* Unaligned access */
3068c2ecf20Sopenharmony_ci			b43legacy_shm_control_word(dev, routing, offset >> 2);
3078c2ecf20Sopenharmony_ci			ret = b43legacy_read16(dev,
3088c2ecf20Sopenharmony_ci					     B43legacy_MMIO_SHM_DATA_UNALIGNED);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci			return ret;
3118c2ecf20Sopenharmony_ci		}
3128c2ecf20Sopenharmony_ci		offset >>= 2;
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci	b43legacy_shm_control_word(dev, routing, offset);
3158c2ecf20Sopenharmony_ci	ret = b43legacy_read16(dev, B43legacy_MMIO_SHM_DATA);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return ret;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_civoid b43legacy_shm_write32(struct b43legacy_wldev *dev,
3218c2ecf20Sopenharmony_ci			   u16 routing, u16 offset,
3228c2ecf20Sopenharmony_ci			   u32 value)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	if (routing == B43legacy_SHM_SHARED) {
3258c2ecf20Sopenharmony_ci		B43legacy_WARN_ON((offset & 0x0001) != 0);
3268c2ecf20Sopenharmony_ci		if (offset & 0x0003) {
3278c2ecf20Sopenharmony_ci			/* Unaligned access */
3288c2ecf20Sopenharmony_ci			b43legacy_shm_control_word(dev, routing, offset >> 2);
3298c2ecf20Sopenharmony_ci			b43legacy_write16(dev,
3308c2ecf20Sopenharmony_ci					  B43legacy_MMIO_SHM_DATA_UNALIGNED,
3318c2ecf20Sopenharmony_ci					  (value >> 16) & 0xffff);
3328c2ecf20Sopenharmony_ci			b43legacy_shm_control_word(dev, routing,
3338c2ecf20Sopenharmony_ci						   (offset >> 2) + 1);
3348c2ecf20Sopenharmony_ci			b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA,
3358c2ecf20Sopenharmony_ci					  value & 0xffff);
3368c2ecf20Sopenharmony_ci			return;
3378c2ecf20Sopenharmony_ci		}
3388c2ecf20Sopenharmony_ci		offset >>= 2;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci	b43legacy_shm_control_word(dev, routing, offset);
3418c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, value);
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_civoid b43legacy_shm_write16(struct b43legacy_wldev *dev, u16 routing, u16 offset,
3458c2ecf20Sopenharmony_ci			   u16 value)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	if (routing == B43legacy_SHM_SHARED) {
3488c2ecf20Sopenharmony_ci		B43legacy_WARN_ON((offset & 0x0001) != 0);
3498c2ecf20Sopenharmony_ci		if (offset & 0x0003) {
3508c2ecf20Sopenharmony_ci			/* Unaligned access */
3518c2ecf20Sopenharmony_ci			b43legacy_shm_control_word(dev, routing, offset >> 2);
3528c2ecf20Sopenharmony_ci			b43legacy_write16(dev,
3538c2ecf20Sopenharmony_ci					  B43legacy_MMIO_SHM_DATA_UNALIGNED,
3548c2ecf20Sopenharmony_ci					  value);
3558c2ecf20Sopenharmony_ci			return;
3568c2ecf20Sopenharmony_ci		}
3578c2ecf20Sopenharmony_ci		offset >>= 2;
3588c2ecf20Sopenharmony_ci	}
3598c2ecf20Sopenharmony_ci	b43legacy_shm_control_word(dev, routing, offset);
3608c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_SHM_DATA, value);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci/* Read HostFlags */
3648c2ecf20Sopenharmony_ciu32 b43legacy_hf_read(struct b43legacy_wldev *dev)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	u32 ret;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	ret = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
3698c2ecf20Sopenharmony_ci				   B43legacy_SHM_SH_HOSTFHI);
3708c2ecf20Sopenharmony_ci	ret <<= 16;
3718c2ecf20Sopenharmony_ci	ret |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
3728c2ecf20Sopenharmony_ci				    B43legacy_SHM_SH_HOSTFLO);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	return ret;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci/* Write HostFlags */
3788c2ecf20Sopenharmony_civoid b43legacy_hf_write(struct b43legacy_wldev *dev, u32 value)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3818c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_HOSTFLO,
3828c2ecf20Sopenharmony_ci			      (value & 0x0000FFFF));
3838c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
3848c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_HOSTFHI,
3858c2ecf20Sopenharmony_ci			      ((value & 0xFFFF0000) >> 16));
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_civoid b43legacy_tsf_read(struct b43legacy_wldev *dev, u64 *tsf)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	/* We need to be careful. As we read the TSF from multiple
3918c2ecf20Sopenharmony_ci	 * registers, we should take care of register overflows.
3928c2ecf20Sopenharmony_ci	 * In theory, the whole tsf read process should be atomic.
3938c2ecf20Sopenharmony_ci	 * We try to be atomic here, by restaring the read process,
3948c2ecf20Sopenharmony_ci	 * if any of the high registers changed (overflew).
3958c2ecf20Sopenharmony_ci	 */
3968c2ecf20Sopenharmony_ci	if (dev->dev->id.revision >= 3) {
3978c2ecf20Sopenharmony_ci		u32 low;
3988c2ecf20Sopenharmony_ci		u32 high;
3998c2ecf20Sopenharmony_ci		u32 high2;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci		do {
4028c2ecf20Sopenharmony_ci			high = b43legacy_read32(dev,
4038c2ecf20Sopenharmony_ci					B43legacy_MMIO_REV3PLUS_TSF_HIGH);
4048c2ecf20Sopenharmony_ci			low = b43legacy_read32(dev,
4058c2ecf20Sopenharmony_ci					B43legacy_MMIO_REV3PLUS_TSF_LOW);
4068c2ecf20Sopenharmony_ci			high2 = b43legacy_read32(dev,
4078c2ecf20Sopenharmony_ci					B43legacy_MMIO_REV3PLUS_TSF_HIGH);
4088c2ecf20Sopenharmony_ci		} while (unlikely(high != high2));
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci		*tsf = high;
4118c2ecf20Sopenharmony_ci		*tsf <<= 32;
4128c2ecf20Sopenharmony_ci		*tsf |= low;
4138c2ecf20Sopenharmony_ci	} else {
4148c2ecf20Sopenharmony_ci		u64 tmp;
4158c2ecf20Sopenharmony_ci		u16 v0;
4168c2ecf20Sopenharmony_ci		u16 v1;
4178c2ecf20Sopenharmony_ci		u16 v2;
4188c2ecf20Sopenharmony_ci		u16 v3;
4198c2ecf20Sopenharmony_ci		u16 test1;
4208c2ecf20Sopenharmony_ci		u16 test2;
4218c2ecf20Sopenharmony_ci		u16 test3;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci		do {
4248c2ecf20Sopenharmony_ci			v3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
4258c2ecf20Sopenharmony_ci			v2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
4268c2ecf20Sopenharmony_ci			v1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
4278c2ecf20Sopenharmony_ci			v0 = b43legacy_read16(dev, B43legacy_MMIO_TSF_0);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci			test3 = b43legacy_read16(dev, B43legacy_MMIO_TSF_3);
4308c2ecf20Sopenharmony_ci			test2 = b43legacy_read16(dev, B43legacy_MMIO_TSF_2);
4318c2ecf20Sopenharmony_ci			test1 = b43legacy_read16(dev, B43legacy_MMIO_TSF_1);
4328c2ecf20Sopenharmony_ci		} while (v3 != test3 || v2 != test2 || v1 != test1);
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		*tsf = v3;
4358c2ecf20Sopenharmony_ci		*tsf <<= 48;
4368c2ecf20Sopenharmony_ci		tmp = v2;
4378c2ecf20Sopenharmony_ci		tmp <<= 32;
4388c2ecf20Sopenharmony_ci		*tsf |= tmp;
4398c2ecf20Sopenharmony_ci		tmp = v1;
4408c2ecf20Sopenharmony_ci		tmp <<= 16;
4418c2ecf20Sopenharmony_ci		*tsf |= tmp;
4428c2ecf20Sopenharmony_ci		*tsf |= v0;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic void b43legacy_time_lock(struct b43legacy_wldev *dev)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	u32 status;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
4518c2ecf20Sopenharmony_ci	status |= B43legacy_MACCTL_TBTTHOLD;
4528c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic void b43legacy_time_unlock(struct b43legacy_wldev *dev)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	u32 status;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
4608c2ecf20Sopenharmony_ci	status &= ~B43legacy_MACCTL_TBTTHOLD;
4618c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic void b43legacy_tsf_write_locked(struct b43legacy_wldev *dev, u64 tsf)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	/* Be careful with the in-progress timer.
4678c2ecf20Sopenharmony_ci	 * First zero out the low register, so we have a full
4688c2ecf20Sopenharmony_ci	 * register-overflow duration to complete the operation.
4698c2ecf20Sopenharmony_ci	 */
4708c2ecf20Sopenharmony_ci	if (dev->dev->id.revision >= 3) {
4718c2ecf20Sopenharmony_ci		u32 lo = (tsf & 0x00000000FFFFFFFFULL);
4728c2ecf20Sopenharmony_ci		u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW, 0);
4758c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_HIGH,
4768c2ecf20Sopenharmony_ci				    hi);
4778c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_REV3PLUS_TSF_LOW,
4788c2ecf20Sopenharmony_ci				    lo);
4798c2ecf20Sopenharmony_ci	} else {
4808c2ecf20Sopenharmony_ci		u16 v0 = (tsf & 0x000000000000FFFFULL);
4818c2ecf20Sopenharmony_ci		u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
4828c2ecf20Sopenharmony_ci		u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
4838c2ecf20Sopenharmony_ci		u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_TSF_0, 0);
4868c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_TSF_3, v3);
4878c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_TSF_2, v2);
4888c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_TSF_1, v1);
4898c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_TSF_0, v0);
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_civoid b43legacy_tsf_write(struct b43legacy_wldev *dev, u64 tsf)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	b43legacy_time_lock(dev);
4968c2ecf20Sopenharmony_ci	b43legacy_tsf_write_locked(dev, tsf);
4978c2ecf20Sopenharmony_ci	b43legacy_time_unlock(dev);
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic
5018c2ecf20Sopenharmony_civoid b43legacy_macfilter_set(struct b43legacy_wldev *dev,
5028c2ecf20Sopenharmony_ci			     u16 offset, const u8 *mac)
5038c2ecf20Sopenharmony_ci{
5048c2ecf20Sopenharmony_ci	static const u8 zero_addr[ETH_ALEN] = { 0 };
5058c2ecf20Sopenharmony_ci	u16 data;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	if (!mac)
5088c2ecf20Sopenharmony_ci		mac = zero_addr;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	offset |= 0x0020;
5118c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_CONTROL, offset);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	data = mac[0];
5148c2ecf20Sopenharmony_ci	data |= mac[1] << 8;
5158c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
5168c2ecf20Sopenharmony_ci	data = mac[2];
5178c2ecf20Sopenharmony_ci	data |= mac[3] << 8;
5188c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
5198c2ecf20Sopenharmony_ci	data = mac[4];
5208c2ecf20Sopenharmony_ci	data |= mac[5] << 8;
5218c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_MACFILTER_DATA, data);
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic void b43legacy_write_mac_bssid_templates(struct b43legacy_wldev *dev)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	static const u8 zero_addr[ETH_ALEN] = { 0 };
5278c2ecf20Sopenharmony_ci	const u8 *mac = dev->wl->mac_addr;
5288c2ecf20Sopenharmony_ci	const u8 *bssid = dev->wl->bssid;
5298c2ecf20Sopenharmony_ci	u8 mac_bssid[ETH_ALEN * 2];
5308c2ecf20Sopenharmony_ci	int i;
5318c2ecf20Sopenharmony_ci	u32 tmp;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	if (!bssid)
5348c2ecf20Sopenharmony_ci		bssid = zero_addr;
5358c2ecf20Sopenharmony_ci	if (!mac)
5368c2ecf20Sopenharmony_ci		mac = zero_addr;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	b43legacy_macfilter_set(dev, B43legacy_MACFILTER_BSSID, bssid);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	memcpy(mac_bssid, mac, ETH_ALEN);
5418c2ecf20Sopenharmony_ci	memcpy(mac_bssid + ETH_ALEN, bssid, ETH_ALEN);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	/* Write our MAC address and BSSID to template ram */
5448c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mac_bssid); i += sizeof(u32)) {
5458c2ecf20Sopenharmony_ci		tmp =  (u32)(mac_bssid[i + 0]);
5468c2ecf20Sopenharmony_ci		tmp |= (u32)(mac_bssid[i + 1]) << 8;
5478c2ecf20Sopenharmony_ci		tmp |= (u32)(mac_bssid[i + 2]) << 16;
5488c2ecf20Sopenharmony_ci		tmp |= (u32)(mac_bssid[i + 3]) << 24;
5498c2ecf20Sopenharmony_ci		b43legacy_ram_write(dev, 0x20 + i, tmp);
5508c2ecf20Sopenharmony_ci		b43legacy_ram_write(dev, 0x78 + i, tmp);
5518c2ecf20Sopenharmony_ci		b43legacy_ram_write(dev, 0x478 + i, tmp);
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic void b43legacy_upload_card_macaddress(struct b43legacy_wldev *dev)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	b43legacy_write_mac_bssid_templates(dev);
5588c2ecf20Sopenharmony_ci	b43legacy_macfilter_set(dev, B43legacy_MACFILTER_SELF,
5598c2ecf20Sopenharmony_ci				dev->wl->mac_addr);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cistatic void b43legacy_set_slot_time(struct b43legacy_wldev *dev,
5638c2ecf20Sopenharmony_ci				    u16 slot_time)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	/* slot_time is in usec. */
5668c2ecf20Sopenharmony_ci	if (dev->phy.type != B43legacy_PHYTYPE_G)
5678c2ecf20Sopenharmony_ci		return;
5688c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x684, 510 + slot_time);
5698c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0010,
5708c2ecf20Sopenharmony_ci			      slot_time);
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic void b43legacy_short_slot_timing_enable(struct b43legacy_wldev *dev)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	b43legacy_set_slot_time(dev, 9);
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic void b43legacy_short_slot_timing_disable(struct b43legacy_wldev *dev)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	b43legacy_set_slot_time(dev, 20);
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci/* Synchronize IRQ top- and bottom-half.
5848c2ecf20Sopenharmony_ci * IRQs must be masked before calling this.
5858c2ecf20Sopenharmony_ci * This must not be called with the irq_lock held.
5868c2ecf20Sopenharmony_ci */
5878c2ecf20Sopenharmony_cistatic void b43legacy_synchronize_irq(struct b43legacy_wldev *dev)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	synchronize_irq(dev->dev->irq);
5908c2ecf20Sopenharmony_ci	tasklet_kill(&dev->isr_tasklet);
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci/* DummyTransmission function, as documented on
5948c2ecf20Sopenharmony_ci * https://bcm-specs.sipsolutions.net/DummyTransmission
5958c2ecf20Sopenharmony_ci */
5968c2ecf20Sopenharmony_civoid b43legacy_dummy_transmission(struct b43legacy_wldev *dev)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
5998c2ecf20Sopenharmony_ci	unsigned int i;
6008c2ecf20Sopenharmony_ci	unsigned int max_loop;
6018c2ecf20Sopenharmony_ci	u16 value;
6028c2ecf20Sopenharmony_ci	u32 buffer[5] = {
6038c2ecf20Sopenharmony_ci		0x00000000,
6048c2ecf20Sopenharmony_ci		0x00D40000,
6058c2ecf20Sopenharmony_ci		0x00000000,
6068c2ecf20Sopenharmony_ci		0x01000000,
6078c2ecf20Sopenharmony_ci		0x00000000,
6088c2ecf20Sopenharmony_ci	};
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	switch (phy->type) {
6118c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_B:
6128c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_G:
6138c2ecf20Sopenharmony_ci		max_loop = 0xFA;
6148c2ecf20Sopenharmony_ci		buffer[0] = 0x000B846E;
6158c2ecf20Sopenharmony_ci		break;
6168c2ecf20Sopenharmony_ci	default:
6178c2ecf20Sopenharmony_ci		B43legacy_BUG_ON(1);
6188c2ecf20Sopenharmony_ci		return;
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	for (i = 0; i < 5; i++)
6228c2ecf20Sopenharmony_ci		b43legacy_ram_write(dev, i * 4, buffer[i]);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	/* dummy read follows */
6258c2ecf20Sopenharmony_ci	b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x0568, 0x0000);
6288c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x07C0, 0x0000);
6298c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x050C, 0x0000);
6308c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x0508, 0x0000);
6318c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x050A, 0x0000);
6328c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x054C, 0x0000);
6338c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x056A, 0x0014);
6348c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x0568, 0x0826);
6358c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x0500, 0x0000);
6368c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x0502, 0x0030);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
6398c2ecf20Sopenharmony_ci		b43legacy_radio_write16(dev, 0x0051, 0x0017);
6408c2ecf20Sopenharmony_ci	for (i = 0x00; i < max_loop; i++) {
6418c2ecf20Sopenharmony_ci		value = b43legacy_read16(dev, 0x050E);
6428c2ecf20Sopenharmony_ci		if (value & 0x0080)
6438c2ecf20Sopenharmony_ci			break;
6448c2ecf20Sopenharmony_ci		udelay(10);
6458c2ecf20Sopenharmony_ci	}
6468c2ecf20Sopenharmony_ci	for (i = 0x00; i < 0x0A; i++) {
6478c2ecf20Sopenharmony_ci		value = b43legacy_read16(dev, 0x050E);
6488c2ecf20Sopenharmony_ci		if (value & 0x0400)
6498c2ecf20Sopenharmony_ci			break;
6508c2ecf20Sopenharmony_ci		udelay(10);
6518c2ecf20Sopenharmony_ci	}
6528c2ecf20Sopenharmony_ci	for (i = 0x00; i < 0x0A; i++) {
6538c2ecf20Sopenharmony_ci		value = b43legacy_read16(dev, 0x0690);
6548c2ecf20Sopenharmony_ci		if (!(value & 0x0100))
6558c2ecf20Sopenharmony_ci			break;
6568c2ecf20Sopenharmony_ci		udelay(10);
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci	if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
6598c2ecf20Sopenharmony_ci		b43legacy_radio_write16(dev, 0x0051, 0x0037);
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci/* Turn the Analog ON/OFF */
6638c2ecf20Sopenharmony_cistatic void b43legacy_switch_analog(struct b43legacy_wldev *dev, int on)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_PHY0, on ? 0 : 0xF4);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_civoid b43legacy_wireless_core_reset(struct b43legacy_wldev *dev, u32 flags)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	u32 tmslow;
6718c2ecf20Sopenharmony_ci	u32 macctl;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	flags |= B43legacy_TMSLOW_PHYCLKEN;
6748c2ecf20Sopenharmony_ci	flags |= B43legacy_TMSLOW_PHYRESET;
6758c2ecf20Sopenharmony_ci	ssb_device_enable(dev->dev, flags);
6768c2ecf20Sopenharmony_ci	msleep(2); /* Wait for the PLL to turn on. */
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	/* Now take the PHY out of Reset again */
6798c2ecf20Sopenharmony_ci	tmslow = ssb_read32(dev->dev, SSB_TMSLOW);
6808c2ecf20Sopenharmony_ci	tmslow |= SSB_TMSLOW_FGC;
6818c2ecf20Sopenharmony_ci	tmslow &= ~B43legacy_TMSLOW_PHYRESET;
6828c2ecf20Sopenharmony_ci	ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
6838c2ecf20Sopenharmony_ci	ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
6848c2ecf20Sopenharmony_ci	msleep(1);
6858c2ecf20Sopenharmony_ci	tmslow &= ~SSB_TMSLOW_FGC;
6868c2ecf20Sopenharmony_ci	ssb_write32(dev->dev, SSB_TMSLOW, tmslow);
6878c2ecf20Sopenharmony_ci	ssb_read32(dev->dev, SSB_TMSLOW); /* flush */
6888c2ecf20Sopenharmony_ci	msleep(1);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/* Turn Analog ON */
6918c2ecf20Sopenharmony_ci	b43legacy_switch_analog(dev, 1);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
6948c2ecf20Sopenharmony_ci	macctl &= ~B43legacy_MACCTL_GMODE;
6958c2ecf20Sopenharmony_ci	if (flags & B43legacy_TMSLOW_GMODE) {
6968c2ecf20Sopenharmony_ci		macctl |= B43legacy_MACCTL_GMODE;
6978c2ecf20Sopenharmony_ci		dev->phy.gmode = true;
6988c2ecf20Sopenharmony_ci	} else
6998c2ecf20Sopenharmony_ci		dev->phy.gmode = false;
7008c2ecf20Sopenharmony_ci	macctl |= B43legacy_MACCTL_IHR_ENABLED;
7018c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic void handle_irq_transmit_status(struct b43legacy_wldev *dev)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	u32 v0;
7078c2ecf20Sopenharmony_ci	u32 v1;
7088c2ecf20Sopenharmony_ci	u16 tmp;
7098c2ecf20Sopenharmony_ci	struct b43legacy_txstatus stat;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	while (1) {
7128c2ecf20Sopenharmony_ci		v0 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
7138c2ecf20Sopenharmony_ci		if (!(v0 & 0x00000001))
7148c2ecf20Sopenharmony_ci			break;
7158c2ecf20Sopenharmony_ci		v1 = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci		stat.cookie = (v0 >> 16);
7188c2ecf20Sopenharmony_ci		stat.seq = (v1 & 0x0000FFFF);
7198c2ecf20Sopenharmony_ci		stat.phy_stat = ((v1 & 0x00FF0000) >> 16);
7208c2ecf20Sopenharmony_ci		tmp = (v0 & 0x0000FFFF);
7218c2ecf20Sopenharmony_ci		stat.frame_count = ((tmp & 0xF000) >> 12);
7228c2ecf20Sopenharmony_ci		stat.rts_count = ((tmp & 0x0F00) >> 8);
7238c2ecf20Sopenharmony_ci		stat.supp_reason = ((tmp & 0x001C) >> 2);
7248c2ecf20Sopenharmony_ci		stat.pm_indicated = !!(tmp & 0x0080);
7258c2ecf20Sopenharmony_ci		stat.intermediate = !!(tmp & 0x0040);
7268c2ecf20Sopenharmony_ci		stat.for_ampdu = !!(tmp & 0x0020);
7278c2ecf20Sopenharmony_ci		stat.acked = !!(tmp & 0x0002);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci		b43legacy_handle_txstatus(dev, &stat);
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic void drain_txstatus_queue(struct b43legacy_wldev *dev)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	u32 dummy;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	if (dev->dev->id.revision < 5)
7388c2ecf20Sopenharmony_ci		return;
7398c2ecf20Sopenharmony_ci	/* Read all entries from the microcode TXstatus FIFO
7408c2ecf20Sopenharmony_ci	 * and throw them away.
7418c2ecf20Sopenharmony_ci	 */
7428c2ecf20Sopenharmony_ci	while (1) {
7438c2ecf20Sopenharmony_ci		dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_0);
7448c2ecf20Sopenharmony_ci		if (!(dummy & 0x00000001))
7458c2ecf20Sopenharmony_ci			break;
7468c2ecf20Sopenharmony_ci		dummy = b43legacy_read32(dev, B43legacy_MMIO_XMITSTAT_1);
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic u32 b43legacy_jssi_read(struct b43legacy_wldev *dev)
7518c2ecf20Sopenharmony_ci{
7528c2ecf20Sopenharmony_ci	u32 val = 0;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	val = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x40A);
7558c2ecf20Sopenharmony_ci	val <<= 16;
7568c2ecf20Sopenharmony_ci	val |= b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x408);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	return val;
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic void b43legacy_jssi_write(struct b43legacy_wldev *dev, u32 jssi)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x408,
7648c2ecf20Sopenharmony_ci			      (jssi & 0x0000FFFF));
7658c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x40A,
7668c2ecf20Sopenharmony_ci			      (jssi & 0xFFFF0000) >> 16);
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic void b43legacy_generate_noise_sample(struct b43legacy_wldev *dev)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	b43legacy_jssi_write(dev, 0x7F7F7F7F);
7728c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
7738c2ecf20Sopenharmony_ci			  b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
7748c2ecf20Sopenharmony_ci			  | B43legacy_MACCMD_BGNOISE);
7758c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dev->noisecalc.channel_at_start !=
7768c2ecf20Sopenharmony_ci			    dev->phy.channel);
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_cistatic void b43legacy_calculate_link_quality(struct b43legacy_wldev *dev)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	/* Top half of Link Quality calculation. */
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (dev->noisecalc.calculation_running)
7848c2ecf20Sopenharmony_ci		return;
7858c2ecf20Sopenharmony_ci	dev->noisecalc.channel_at_start = dev->phy.channel;
7868c2ecf20Sopenharmony_ci	dev->noisecalc.calculation_running = true;
7878c2ecf20Sopenharmony_ci	dev->noisecalc.nr_samples = 0;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	b43legacy_generate_noise_sample(dev);
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_cistatic void handle_irq_noise(struct b43legacy_wldev *dev)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
7958c2ecf20Sopenharmony_ci	u16 tmp;
7968c2ecf20Sopenharmony_ci	u8 noise[4];
7978c2ecf20Sopenharmony_ci	u8 i;
7988c2ecf20Sopenharmony_ci	u8 j;
7998c2ecf20Sopenharmony_ci	s32 average;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	/* Bottom half of Link Quality calculation. */
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(!dev->noisecalc.calculation_running);
8048c2ecf20Sopenharmony_ci	if (dev->noisecalc.channel_at_start != phy->channel)
8058c2ecf20Sopenharmony_ci		goto drop_calculation;
8068c2ecf20Sopenharmony_ci	*((__le32 *)noise) = cpu_to_le32(b43legacy_jssi_read(dev));
8078c2ecf20Sopenharmony_ci	if (noise[0] == 0x7F || noise[1] == 0x7F ||
8088c2ecf20Sopenharmony_ci	    noise[2] == 0x7F || noise[3] == 0x7F)
8098c2ecf20Sopenharmony_ci		goto generate_new;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	/* Get the noise samples. */
8128c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dev->noisecalc.nr_samples >= 8);
8138c2ecf20Sopenharmony_ci	i = dev->noisecalc.nr_samples;
8148c2ecf20Sopenharmony_ci	noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
8158c2ecf20Sopenharmony_ci	noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
8168c2ecf20Sopenharmony_ci	noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
8178c2ecf20Sopenharmony_ci	noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
8188c2ecf20Sopenharmony_ci	dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
8198c2ecf20Sopenharmony_ci	dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
8208c2ecf20Sopenharmony_ci	dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
8218c2ecf20Sopenharmony_ci	dev->noisecalc.samples[i][3] = phy->nrssi_lt[noise[3]];
8228c2ecf20Sopenharmony_ci	dev->noisecalc.nr_samples++;
8238c2ecf20Sopenharmony_ci	if (dev->noisecalc.nr_samples == 8) {
8248c2ecf20Sopenharmony_ci		/* Calculate the Link Quality by the noise samples. */
8258c2ecf20Sopenharmony_ci		average = 0;
8268c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++) {
8278c2ecf20Sopenharmony_ci			for (j = 0; j < 4; j++)
8288c2ecf20Sopenharmony_ci				average += dev->noisecalc.samples[i][j];
8298c2ecf20Sopenharmony_ci		}
8308c2ecf20Sopenharmony_ci		average /= (8 * 4);
8318c2ecf20Sopenharmony_ci		average *= 125;
8328c2ecf20Sopenharmony_ci		average += 64;
8338c2ecf20Sopenharmony_ci		average /= 128;
8348c2ecf20Sopenharmony_ci		tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
8358c2ecf20Sopenharmony_ci					     0x40C);
8368c2ecf20Sopenharmony_ci		tmp = (tmp / 128) & 0x1F;
8378c2ecf20Sopenharmony_ci		if (tmp >= 8)
8388c2ecf20Sopenharmony_ci			average += 2;
8398c2ecf20Sopenharmony_ci		else
8408c2ecf20Sopenharmony_ci			average -= 25;
8418c2ecf20Sopenharmony_ci		if (tmp == 8)
8428c2ecf20Sopenharmony_ci			average -= 72;
8438c2ecf20Sopenharmony_ci		else
8448c2ecf20Sopenharmony_ci			average -= 48;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		dev->stats.link_noise = average;
8478c2ecf20Sopenharmony_cidrop_calculation:
8488c2ecf20Sopenharmony_ci		dev->noisecalc.calculation_running = false;
8498c2ecf20Sopenharmony_ci		return;
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_cigenerate_new:
8528c2ecf20Sopenharmony_ci	b43legacy_generate_noise_sample(dev);
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cistatic void handle_irq_tbtt_indication(struct b43legacy_wldev *dev)
8568c2ecf20Sopenharmony_ci{
8578c2ecf20Sopenharmony_ci	if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
8588c2ecf20Sopenharmony_ci		/* TODO: PS TBTT */
8598c2ecf20Sopenharmony_ci	} else {
8608c2ecf20Sopenharmony_ci		if (1/*FIXME: the last PSpoll frame was sent successfully */)
8618c2ecf20Sopenharmony_ci			b43legacy_power_saving_ctl_bits(dev, -1, -1);
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci	if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
8648c2ecf20Sopenharmony_ci		dev->dfq_valid = true;
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_cistatic void handle_irq_atim_end(struct b43legacy_wldev *dev)
8688c2ecf20Sopenharmony_ci{
8698c2ecf20Sopenharmony_ci	if (dev->dfq_valid) {
8708c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_MACCMD,
8718c2ecf20Sopenharmony_ci				  b43legacy_read32(dev, B43legacy_MMIO_MACCMD)
8728c2ecf20Sopenharmony_ci				  | B43legacy_MACCMD_DFQ_VALID);
8738c2ecf20Sopenharmony_ci		dev->dfq_valid = false;
8748c2ecf20Sopenharmony_ci	}
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_cistatic void handle_irq_pmq(struct b43legacy_wldev *dev)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	u32 tmp;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	/* TODO: AP mode. */
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	while (1) {
8848c2ecf20Sopenharmony_ci		tmp = b43legacy_read32(dev, B43legacy_MMIO_PS_STATUS);
8858c2ecf20Sopenharmony_ci		if (!(tmp & 0x00000008))
8868c2ecf20Sopenharmony_ci			break;
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci	/* 16bit write is odd, but correct. */
8898c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_PS_STATUS, 0x0002);
8908c2ecf20Sopenharmony_ci}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_cistatic void b43legacy_write_template_common(struct b43legacy_wldev *dev,
8938c2ecf20Sopenharmony_ci					    const u8 *data, u16 size,
8948c2ecf20Sopenharmony_ci					    u16 ram_offset,
8958c2ecf20Sopenharmony_ci					    u16 shm_size_offset, u8 rate)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	u32 i;
8988c2ecf20Sopenharmony_ci	u32 tmp;
8998c2ecf20Sopenharmony_ci	struct b43legacy_plcp_hdr4 plcp;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	plcp.data = 0;
9028c2ecf20Sopenharmony_ci	b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate);
9038c2ecf20Sopenharmony_ci	b43legacy_ram_write(dev, ram_offset, le32_to_cpu(plcp.data));
9048c2ecf20Sopenharmony_ci	ram_offset += sizeof(u32);
9058c2ecf20Sopenharmony_ci	/* The PLCP is 6 bytes long, but we only wrote 4 bytes, yet.
9068c2ecf20Sopenharmony_ci	 * So leave the first two bytes of the next write blank.
9078c2ecf20Sopenharmony_ci	 */
9088c2ecf20Sopenharmony_ci	tmp = (u32)(data[0]) << 16;
9098c2ecf20Sopenharmony_ci	tmp |= (u32)(data[1]) << 24;
9108c2ecf20Sopenharmony_ci	b43legacy_ram_write(dev, ram_offset, tmp);
9118c2ecf20Sopenharmony_ci	ram_offset += sizeof(u32);
9128c2ecf20Sopenharmony_ci	for (i = 2; i < size; i += sizeof(u32)) {
9138c2ecf20Sopenharmony_ci		tmp = (u32)(data[i + 0]);
9148c2ecf20Sopenharmony_ci		if (i + 1 < size)
9158c2ecf20Sopenharmony_ci			tmp |= (u32)(data[i + 1]) << 8;
9168c2ecf20Sopenharmony_ci		if (i + 2 < size)
9178c2ecf20Sopenharmony_ci			tmp |= (u32)(data[i + 2]) << 16;
9188c2ecf20Sopenharmony_ci		if (i + 3 < size)
9198c2ecf20Sopenharmony_ci			tmp |= (u32)(data[i + 3]) << 24;
9208c2ecf20Sopenharmony_ci		b43legacy_ram_write(dev, ram_offset + i - 2, tmp);
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_size_offset,
9238c2ecf20Sopenharmony_ci			      size + sizeof(struct b43legacy_plcp_hdr6));
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci/* Convert a b43legacy antenna number value to the PHY TX control value. */
9278c2ecf20Sopenharmony_cistatic u16 b43legacy_antenna_to_phyctl(int antenna)
9288c2ecf20Sopenharmony_ci{
9298c2ecf20Sopenharmony_ci	switch (antenna) {
9308c2ecf20Sopenharmony_ci	case B43legacy_ANTENNA0:
9318c2ecf20Sopenharmony_ci		return B43legacy_TX4_PHY_ANT0;
9328c2ecf20Sopenharmony_ci	case B43legacy_ANTENNA1:
9338c2ecf20Sopenharmony_ci		return B43legacy_TX4_PHY_ANT1;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci	return B43legacy_TX4_PHY_ANTLAST;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_cistatic void b43legacy_write_beacon_template(struct b43legacy_wldev *dev,
9398c2ecf20Sopenharmony_ci					    u16 ram_offset,
9408c2ecf20Sopenharmony_ci					    u16 shm_size_offset)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	unsigned int i, len, variable_len;
9448c2ecf20Sopenharmony_ci	const struct ieee80211_mgmt *bcn;
9458c2ecf20Sopenharmony_ci	const u8 *ie;
9468c2ecf20Sopenharmony_ci	bool tim_found = false;
9478c2ecf20Sopenharmony_ci	unsigned int rate;
9488c2ecf20Sopenharmony_ci	u16 ctl;
9498c2ecf20Sopenharmony_ci	int antenna;
9508c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
9538c2ecf20Sopenharmony_ci	len = min_t(size_t, dev->wl->current_beacon->len,
9548c2ecf20Sopenharmony_ci		  0x200 - sizeof(struct b43legacy_plcp_hdr6));
9558c2ecf20Sopenharmony_ci	rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	b43legacy_write_template_common(dev, (const u8 *)bcn, len, ram_offset,
9588c2ecf20Sopenharmony_ci					shm_size_offset, rate);
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	/* Write the PHY TX control parameters. */
9618c2ecf20Sopenharmony_ci	antenna = B43legacy_ANTENNA_DEFAULT;
9628c2ecf20Sopenharmony_ci	antenna = b43legacy_antenna_to_phyctl(antenna);
9638c2ecf20Sopenharmony_ci	ctl = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
9648c2ecf20Sopenharmony_ci				   B43legacy_SHM_SH_BEACPHYCTL);
9658c2ecf20Sopenharmony_ci	/* We can't send beacons with short preamble. Would get PHY errors. */
9668c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_TX4_PHY_SHORTPRMBL;
9678c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_TX4_PHY_ANT;
9688c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_TX4_PHY_ENC;
9698c2ecf20Sopenharmony_ci	ctl |= antenna;
9708c2ecf20Sopenharmony_ci	ctl |= B43legacy_TX4_PHY_ENC_CCK;
9718c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
9728c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_BEACPHYCTL, ctl);
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	/* Find the position of the TIM and the DTIM_period value
9758c2ecf20Sopenharmony_ci	 * and write them to SHM. */
9768c2ecf20Sopenharmony_ci	ie = bcn->u.beacon.variable;
9778c2ecf20Sopenharmony_ci	variable_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
9788c2ecf20Sopenharmony_ci	for (i = 0; i < variable_len - 2; ) {
9798c2ecf20Sopenharmony_ci		uint8_t ie_id, ie_len;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci		ie_id = ie[i];
9828c2ecf20Sopenharmony_ci		ie_len = ie[i + 1];
9838c2ecf20Sopenharmony_ci		if (ie_id == 5) {
9848c2ecf20Sopenharmony_ci			u16 tim_position;
9858c2ecf20Sopenharmony_ci			u16 dtim_period;
9868c2ecf20Sopenharmony_ci			/* This is the TIM Information Element */
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci			/* Check whether the ie_len is in the beacon data range. */
9898c2ecf20Sopenharmony_ci			if (variable_len < ie_len + 2 + i)
9908c2ecf20Sopenharmony_ci				break;
9918c2ecf20Sopenharmony_ci			/* A valid TIM is at least 4 bytes long. */
9928c2ecf20Sopenharmony_ci			if (ie_len < 4)
9938c2ecf20Sopenharmony_ci				break;
9948c2ecf20Sopenharmony_ci			tim_found = true;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci			tim_position = sizeof(struct b43legacy_plcp_hdr6);
9978c2ecf20Sopenharmony_ci			tim_position += offsetof(struct ieee80211_mgmt,
9988c2ecf20Sopenharmony_ci						 u.beacon.variable);
9998c2ecf20Sopenharmony_ci			tim_position += i;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci			dtim_period = ie[i + 3];
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci			b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
10048c2ecf20Sopenharmony_ci					B43legacy_SHM_SH_TIMPOS, tim_position);
10058c2ecf20Sopenharmony_ci			b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
10068c2ecf20Sopenharmony_ci					B43legacy_SHM_SH_DTIMP, dtim_period);
10078c2ecf20Sopenharmony_ci			break;
10088c2ecf20Sopenharmony_ci		}
10098c2ecf20Sopenharmony_ci		i += ie_len + 2;
10108c2ecf20Sopenharmony_ci	}
10118c2ecf20Sopenharmony_ci	if (!tim_found) {
10128c2ecf20Sopenharmony_ci		b43legacywarn(dev->wl, "Did not find a valid TIM IE in the "
10138c2ecf20Sopenharmony_ci			      "beacon template packet. AP or IBSS operation "
10148c2ecf20Sopenharmony_ci			      "may be broken.\n");
10158c2ecf20Sopenharmony_ci	} else
10168c2ecf20Sopenharmony_ci		b43legacydbg(dev->wl, "Updated beacon template\n");
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_cistatic void b43legacy_write_probe_resp_plcp(struct b43legacy_wldev *dev,
10208c2ecf20Sopenharmony_ci					    u16 shm_offset, u16 size,
10218c2ecf20Sopenharmony_ci					    struct ieee80211_rate *rate)
10228c2ecf20Sopenharmony_ci{
10238c2ecf20Sopenharmony_ci	struct b43legacy_plcp_hdr4 plcp;
10248c2ecf20Sopenharmony_ci	u32 tmp;
10258c2ecf20Sopenharmony_ci	__le16 dur;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	plcp.data = 0;
10288c2ecf20Sopenharmony_ci	b43legacy_generate_plcp_hdr(&plcp, size + FCS_LEN, rate->hw_value);
10298c2ecf20Sopenharmony_ci	dur = ieee80211_generic_frame_duration(dev->wl->hw,
10308c2ecf20Sopenharmony_ci					       dev->wl->vif,
10318c2ecf20Sopenharmony_ci					       NL80211_BAND_2GHZ,
10328c2ecf20Sopenharmony_ci					       size,
10338c2ecf20Sopenharmony_ci					       rate);
10348c2ecf20Sopenharmony_ci	/* Write PLCP in two parts and timing for packet transfer */
10358c2ecf20Sopenharmony_ci	tmp = le32_to_cpu(plcp.data);
10368c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset,
10378c2ecf20Sopenharmony_ci			      tmp & 0xFFFF);
10388c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 2,
10398c2ecf20Sopenharmony_ci			      tmp >> 16);
10408c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, shm_offset + 6,
10418c2ecf20Sopenharmony_ci			      le16_to_cpu(dur));
10428c2ecf20Sopenharmony_ci}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci/* Instead of using custom probe response template, this function
10458c2ecf20Sopenharmony_ci * just patches custom beacon template by:
10468c2ecf20Sopenharmony_ci * 1) Changing packet type
10478c2ecf20Sopenharmony_ci * 2) Patching duration field
10488c2ecf20Sopenharmony_ci * 3) Stripping TIM
10498c2ecf20Sopenharmony_ci */
10508c2ecf20Sopenharmony_cistatic const u8 *b43legacy_generate_probe_resp(struct b43legacy_wldev *dev,
10518c2ecf20Sopenharmony_ci					       u16 *dest_size,
10528c2ecf20Sopenharmony_ci					       struct ieee80211_rate *rate)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	const u8 *src_data;
10558c2ecf20Sopenharmony_ci	u8 *dest_data;
10568c2ecf20Sopenharmony_ci	u16 src_size, elem_size, src_pos, dest_pos;
10578c2ecf20Sopenharmony_ci	__le16 dur;
10588c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr;
10598c2ecf20Sopenharmony_ci	size_t ie_start;
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	src_size = dev->wl->current_beacon->len;
10628c2ecf20Sopenharmony_ci	src_data = (const u8 *)dev->wl->current_beacon->data;
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	/* Get the start offset of the variable IEs in the packet. */
10658c2ecf20Sopenharmony_ci	ie_start = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
10668c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(ie_start != offsetof(struct ieee80211_mgmt,
10678c2ecf20Sopenharmony_ci					       u.beacon.variable));
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	if (B43legacy_WARN_ON(src_size < ie_start))
10708c2ecf20Sopenharmony_ci		return NULL;
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	dest_data = kmalloc(src_size, GFP_ATOMIC);
10738c2ecf20Sopenharmony_ci	if (unlikely(!dest_data))
10748c2ecf20Sopenharmony_ci		return NULL;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	/* Copy the static data and all Information Elements, except the TIM. */
10778c2ecf20Sopenharmony_ci	memcpy(dest_data, src_data, ie_start);
10788c2ecf20Sopenharmony_ci	src_pos = ie_start;
10798c2ecf20Sopenharmony_ci	dest_pos = ie_start;
10808c2ecf20Sopenharmony_ci	for ( ; src_pos < src_size - 2; src_pos += elem_size) {
10818c2ecf20Sopenharmony_ci		elem_size = src_data[src_pos + 1] + 2;
10828c2ecf20Sopenharmony_ci		if (src_data[src_pos] == 5) {
10838c2ecf20Sopenharmony_ci			/* This is the TIM. */
10848c2ecf20Sopenharmony_ci			continue;
10858c2ecf20Sopenharmony_ci		}
10868c2ecf20Sopenharmony_ci		memcpy(dest_data + dest_pos, src_data + src_pos, elem_size);
10878c2ecf20Sopenharmony_ci		dest_pos += elem_size;
10888c2ecf20Sopenharmony_ci	}
10898c2ecf20Sopenharmony_ci	*dest_size = dest_pos;
10908c2ecf20Sopenharmony_ci	hdr = (struct ieee80211_hdr *)dest_data;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	/* Set the frame control. */
10938c2ecf20Sopenharmony_ci	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
10948c2ecf20Sopenharmony_ci					 IEEE80211_STYPE_PROBE_RESP);
10958c2ecf20Sopenharmony_ci	dur = ieee80211_generic_frame_duration(dev->wl->hw,
10968c2ecf20Sopenharmony_ci					       dev->wl->vif,
10978c2ecf20Sopenharmony_ci					       NL80211_BAND_2GHZ,
10988c2ecf20Sopenharmony_ci					       *dest_size,
10998c2ecf20Sopenharmony_ci					       rate);
11008c2ecf20Sopenharmony_ci	hdr->duration_id = dur;
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	return dest_data;
11038c2ecf20Sopenharmony_ci}
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_cistatic void b43legacy_write_probe_resp_template(struct b43legacy_wldev *dev,
11068c2ecf20Sopenharmony_ci						u16 ram_offset,
11078c2ecf20Sopenharmony_ci						u16 shm_size_offset,
11088c2ecf20Sopenharmony_ci						struct ieee80211_rate *rate)
11098c2ecf20Sopenharmony_ci{
11108c2ecf20Sopenharmony_ci	const u8 *probe_resp_data;
11118c2ecf20Sopenharmony_ci	u16 size;
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	size = dev->wl->current_beacon->len;
11148c2ecf20Sopenharmony_ci	probe_resp_data = b43legacy_generate_probe_resp(dev, &size, rate);
11158c2ecf20Sopenharmony_ci	if (unlikely(!probe_resp_data))
11168c2ecf20Sopenharmony_ci		return;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	/* Looks like PLCP headers plus packet timings are stored for
11198c2ecf20Sopenharmony_ci	 * all possible basic rates
11208c2ecf20Sopenharmony_ci	 */
11218c2ecf20Sopenharmony_ci	b43legacy_write_probe_resp_plcp(dev, 0x31A, size,
11228c2ecf20Sopenharmony_ci					&b43legacy_b_ratetable[0]);
11238c2ecf20Sopenharmony_ci	b43legacy_write_probe_resp_plcp(dev, 0x32C, size,
11248c2ecf20Sopenharmony_ci					&b43legacy_b_ratetable[1]);
11258c2ecf20Sopenharmony_ci	b43legacy_write_probe_resp_plcp(dev, 0x33E, size,
11268c2ecf20Sopenharmony_ci					&b43legacy_b_ratetable[2]);
11278c2ecf20Sopenharmony_ci	b43legacy_write_probe_resp_plcp(dev, 0x350, size,
11288c2ecf20Sopenharmony_ci					&b43legacy_b_ratetable[3]);
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	size = min_t(size_t, size,
11318c2ecf20Sopenharmony_ci		   0x200 - sizeof(struct b43legacy_plcp_hdr6));
11328c2ecf20Sopenharmony_ci	b43legacy_write_template_common(dev, probe_resp_data,
11338c2ecf20Sopenharmony_ci					size, ram_offset,
11348c2ecf20Sopenharmony_ci					shm_size_offset, rate->hw_value);
11358c2ecf20Sopenharmony_ci	kfree(probe_resp_data);
11368c2ecf20Sopenharmony_ci}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_cistatic void b43legacy_upload_beacon0(struct b43legacy_wldev *dev)
11398c2ecf20Sopenharmony_ci{
11408c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	if (wl->beacon0_uploaded)
11438c2ecf20Sopenharmony_ci		return;
11448c2ecf20Sopenharmony_ci	b43legacy_write_beacon_template(dev, 0x68, 0x18);
11458c2ecf20Sopenharmony_ci	/* FIXME: Probe resp upload doesn't really belong here,
11468c2ecf20Sopenharmony_ci	 *        but we don't use that feature anyway. */
11478c2ecf20Sopenharmony_ci	b43legacy_write_probe_resp_template(dev, 0x268, 0x4A,
11488c2ecf20Sopenharmony_ci				      &__b43legacy_ratetable[3]);
11498c2ecf20Sopenharmony_ci	wl->beacon0_uploaded = true;
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_cistatic void b43legacy_upload_beacon1(struct b43legacy_wldev *dev)
11538c2ecf20Sopenharmony_ci{
11548c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	if (wl->beacon1_uploaded)
11578c2ecf20Sopenharmony_ci		return;
11588c2ecf20Sopenharmony_ci	b43legacy_write_beacon_template(dev, 0x468, 0x1A);
11598c2ecf20Sopenharmony_ci	wl->beacon1_uploaded = true;
11608c2ecf20Sopenharmony_ci}
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_cistatic void handle_irq_beacon(struct b43legacy_wldev *dev)
11638c2ecf20Sopenharmony_ci{
11648c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
11658c2ecf20Sopenharmony_ci	u32 cmd, beacon0_valid, beacon1_valid;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	if (!b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
11688c2ecf20Sopenharmony_ci		return;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	/* This is the bottom half of the asynchronous beacon update. */
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	/* Ignore interrupt in the future. */
11738c2ecf20Sopenharmony_ci	dev->irq_mask &= ~B43legacy_IRQ_BEACON;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
11768c2ecf20Sopenharmony_ci	beacon0_valid = (cmd & B43legacy_MACCMD_BEACON0_VALID);
11778c2ecf20Sopenharmony_ci	beacon1_valid = (cmd & B43legacy_MACCMD_BEACON1_VALID);
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	/* Schedule interrupt manually, if busy. */
11808c2ecf20Sopenharmony_ci	if (beacon0_valid && beacon1_valid) {
11818c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, B43legacy_IRQ_BEACON);
11828c2ecf20Sopenharmony_ci		dev->irq_mask |= B43legacy_IRQ_BEACON;
11838c2ecf20Sopenharmony_ci		return;
11848c2ecf20Sopenharmony_ci	}
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	if (unlikely(wl->beacon_templates_virgin)) {
11878c2ecf20Sopenharmony_ci		/* We never uploaded a beacon before.
11888c2ecf20Sopenharmony_ci		 * Upload both templates now, but only mark one valid. */
11898c2ecf20Sopenharmony_ci		wl->beacon_templates_virgin = false;
11908c2ecf20Sopenharmony_ci		b43legacy_upload_beacon0(dev);
11918c2ecf20Sopenharmony_ci		b43legacy_upload_beacon1(dev);
11928c2ecf20Sopenharmony_ci		cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
11938c2ecf20Sopenharmony_ci		cmd |= B43legacy_MACCMD_BEACON0_VALID;
11948c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
11958c2ecf20Sopenharmony_ci	} else {
11968c2ecf20Sopenharmony_ci		if (!beacon0_valid) {
11978c2ecf20Sopenharmony_ci			b43legacy_upload_beacon0(dev);
11988c2ecf20Sopenharmony_ci			cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
11998c2ecf20Sopenharmony_ci			cmd |= B43legacy_MACCMD_BEACON0_VALID;
12008c2ecf20Sopenharmony_ci			b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
12018c2ecf20Sopenharmony_ci		} else if (!beacon1_valid) {
12028c2ecf20Sopenharmony_ci			b43legacy_upload_beacon1(dev);
12038c2ecf20Sopenharmony_ci			cmd = b43legacy_read32(dev, B43legacy_MMIO_MACCMD);
12048c2ecf20Sopenharmony_ci			cmd |= B43legacy_MACCMD_BEACON1_VALID;
12058c2ecf20Sopenharmony_ci			b43legacy_write32(dev, B43legacy_MMIO_MACCMD, cmd);
12068c2ecf20Sopenharmony_ci		}
12078c2ecf20Sopenharmony_ci	}
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic void b43legacy_beacon_update_trigger_work(struct work_struct *work)
12118c2ecf20Sopenharmony_ci{
12128c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
12138c2ecf20Sopenharmony_ci					 beacon_update_trigger);
12148c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev;
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
12178c2ecf20Sopenharmony_ci	dev = wl->current_dev;
12188c2ecf20Sopenharmony_ci	if (likely(dev && (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED))) {
12198c2ecf20Sopenharmony_ci		spin_lock_irq(&wl->irq_lock);
12208c2ecf20Sopenharmony_ci		/* Update beacon right away or defer to IRQ. */
12218c2ecf20Sopenharmony_ci		handle_irq_beacon(dev);
12228c2ecf20Sopenharmony_ci		/* The handler might have updated the IRQ mask. */
12238c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
12248c2ecf20Sopenharmony_ci				  dev->irq_mask);
12258c2ecf20Sopenharmony_ci		spin_unlock_irq(&wl->irq_lock);
12268c2ecf20Sopenharmony_ci	}
12278c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
12288c2ecf20Sopenharmony_ci}
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci/* Asynchronously update the packet templates in template RAM.
12318c2ecf20Sopenharmony_ci * Locking: Requires wl->irq_lock to be locked. */
12328c2ecf20Sopenharmony_cistatic void b43legacy_update_templates(struct b43legacy_wl *wl)
12338c2ecf20Sopenharmony_ci{
12348c2ecf20Sopenharmony_ci	struct sk_buff *beacon;
12358c2ecf20Sopenharmony_ci	/* This is the top half of the ansynchronous beacon update. The bottom
12368c2ecf20Sopenharmony_ci	 * half is the beacon IRQ. Beacon update must be asynchronous to avoid
12378c2ecf20Sopenharmony_ci	 * sending an invalid beacon. This can happen for example, if the
12388c2ecf20Sopenharmony_ci	 * firmware transmits a beacon while we are updating it. */
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	/* We could modify the existing beacon and set the aid bit in the TIM
12418c2ecf20Sopenharmony_ci	 * field, but that would probably require resizing and moving of data
12428c2ecf20Sopenharmony_ci	 * within the beacon template. Simply request a new beacon and let
12438c2ecf20Sopenharmony_ci	 * mac80211 do the hard work. */
12448c2ecf20Sopenharmony_ci	beacon = ieee80211_beacon_get(wl->hw, wl->vif);
12458c2ecf20Sopenharmony_ci	if (unlikely(!beacon))
12468c2ecf20Sopenharmony_ci		return;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	if (wl->current_beacon)
12498c2ecf20Sopenharmony_ci		dev_kfree_skb_any(wl->current_beacon);
12508c2ecf20Sopenharmony_ci	wl->current_beacon = beacon;
12518c2ecf20Sopenharmony_ci	wl->beacon0_uploaded = false;
12528c2ecf20Sopenharmony_ci	wl->beacon1_uploaded = false;
12538c2ecf20Sopenharmony_ci	ieee80211_queue_work(wl->hw, &wl->beacon_update_trigger);
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_cistatic void b43legacy_set_beacon_int(struct b43legacy_wldev *dev,
12578c2ecf20Sopenharmony_ci				     u16 beacon_int)
12588c2ecf20Sopenharmony_ci{
12598c2ecf20Sopenharmony_ci	b43legacy_time_lock(dev);
12608c2ecf20Sopenharmony_ci	if (dev->dev->id.revision >= 3) {
12618c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_REP,
12628c2ecf20Sopenharmony_ci				 (beacon_int << 16));
12638c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_TSF_CFP_START,
12648c2ecf20Sopenharmony_ci				 (beacon_int << 10));
12658c2ecf20Sopenharmony_ci	} else {
12668c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x606, (beacon_int >> 6));
12678c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x610, beacon_int);
12688c2ecf20Sopenharmony_ci	}
12698c2ecf20Sopenharmony_ci	b43legacy_time_unlock(dev);
12708c2ecf20Sopenharmony_ci	b43legacydbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
12718c2ecf20Sopenharmony_ci}
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_cistatic void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci}
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci/* Interrupt handler bottom-half */
12788c2ecf20Sopenharmony_cistatic void b43legacy_interrupt_tasklet(struct tasklet_struct *t)
12798c2ecf20Sopenharmony_ci{
12808c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = from_tasklet(dev, t, isr_tasklet);
12818c2ecf20Sopenharmony_ci	u32 reason;
12828c2ecf20Sopenharmony_ci	u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
12838c2ecf20Sopenharmony_ci	u32 merged_dma_reason = 0;
12848c2ecf20Sopenharmony_ci	int i;
12858c2ecf20Sopenharmony_ci	unsigned long flags;
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->wl->irq_lock, flags);
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(b43legacy_status(dev) <
12908c2ecf20Sopenharmony_ci			  B43legacy_STAT_INITIALIZED);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	reason = dev->irq_reason;
12938c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(dma_reason); i++) {
12948c2ecf20Sopenharmony_ci		dma_reason[i] = dev->dma_reason[i];
12958c2ecf20Sopenharmony_ci		merged_dma_reason |= dma_reason[i];
12968c2ecf20Sopenharmony_ci	}
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	if (unlikely(reason & B43legacy_IRQ_MAC_TXERR))
12998c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "MAC transmission error\n");
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	if (unlikely(reason & B43legacy_IRQ_PHY_TXERR)) {
13028c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "PHY transmission error\n");
13038c2ecf20Sopenharmony_ci		rmb();
13048c2ecf20Sopenharmony_ci		if (unlikely(atomic_dec_and_test(&dev->phy.txerr_cnt))) {
13058c2ecf20Sopenharmony_ci			b43legacyerr(dev->wl, "Too many PHY TX errors, "
13068c2ecf20Sopenharmony_ci					      "restarting the controller\n");
13078c2ecf20Sopenharmony_ci			b43legacy_controller_restart(dev, "PHY TX errors");
13088c2ecf20Sopenharmony_ci		}
13098c2ecf20Sopenharmony_ci	}
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	if (unlikely(merged_dma_reason & (B43legacy_DMAIRQ_FATALMASK |
13128c2ecf20Sopenharmony_ci					  B43legacy_DMAIRQ_NONFATALMASK))) {
13138c2ecf20Sopenharmony_ci		if (merged_dma_reason & B43legacy_DMAIRQ_FATALMASK) {
13148c2ecf20Sopenharmony_ci			b43legacyerr(dev->wl, "Fatal DMA error: "
13158c2ecf20Sopenharmony_ci			       "0x%08X, 0x%08X, 0x%08X, "
13168c2ecf20Sopenharmony_ci			       "0x%08X, 0x%08X, 0x%08X\n",
13178c2ecf20Sopenharmony_ci			       dma_reason[0], dma_reason[1],
13188c2ecf20Sopenharmony_ci			       dma_reason[2], dma_reason[3],
13198c2ecf20Sopenharmony_ci			       dma_reason[4], dma_reason[5]);
13208c2ecf20Sopenharmony_ci			b43legacy_controller_restart(dev, "DMA error");
13218c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
13228c2ecf20Sopenharmony_ci			return;
13238c2ecf20Sopenharmony_ci		}
13248c2ecf20Sopenharmony_ci		if (merged_dma_reason & B43legacy_DMAIRQ_NONFATALMASK)
13258c2ecf20Sopenharmony_ci			b43legacyerr(dev->wl, "DMA error: "
13268c2ecf20Sopenharmony_ci			       "0x%08X, 0x%08X, 0x%08X, "
13278c2ecf20Sopenharmony_ci			       "0x%08X, 0x%08X, 0x%08X\n",
13288c2ecf20Sopenharmony_ci			       dma_reason[0], dma_reason[1],
13298c2ecf20Sopenharmony_ci			       dma_reason[2], dma_reason[3],
13308c2ecf20Sopenharmony_ci			       dma_reason[4], dma_reason[5]);
13318c2ecf20Sopenharmony_ci	}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	if (unlikely(reason & B43legacy_IRQ_UCODE_DEBUG))
13348c2ecf20Sopenharmony_ci		handle_irq_ucode_debug(dev);
13358c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_TBTT_INDI)
13368c2ecf20Sopenharmony_ci		handle_irq_tbtt_indication(dev);
13378c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_ATIM_END)
13388c2ecf20Sopenharmony_ci		handle_irq_atim_end(dev);
13398c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_BEACON)
13408c2ecf20Sopenharmony_ci		handle_irq_beacon(dev);
13418c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_PMQ)
13428c2ecf20Sopenharmony_ci		handle_irq_pmq(dev);
13438c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_TXFIFO_FLUSH_OK) {
13448c2ecf20Sopenharmony_ci		;/*TODO*/
13458c2ecf20Sopenharmony_ci	}
13468c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_NOISESAMPLE_OK)
13478c2ecf20Sopenharmony_ci		handle_irq_noise(dev);
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	/* Check the DMA reason registers for received data. */
13508c2ecf20Sopenharmony_ci	if (dma_reason[0] & B43legacy_DMAIRQ_RX_DONE) {
13518c2ecf20Sopenharmony_ci		if (b43legacy_using_pio(dev))
13528c2ecf20Sopenharmony_ci			b43legacy_pio_rx(dev->pio.queue0);
13538c2ecf20Sopenharmony_ci		else
13548c2ecf20Sopenharmony_ci			b43legacy_dma_rx(dev->dma.rx_ring0);
13558c2ecf20Sopenharmony_ci	}
13568c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dma_reason[1] & B43legacy_DMAIRQ_RX_DONE);
13578c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dma_reason[2] & B43legacy_DMAIRQ_RX_DONE);
13588c2ecf20Sopenharmony_ci	if (dma_reason[3] & B43legacy_DMAIRQ_RX_DONE) {
13598c2ecf20Sopenharmony_ci		if (b43legacy_using_pio(dev))
13608c2ecf20Sopenharmony_ci			b43legacy_pio_rx(dev->pio.queue3);
13618c2ecf20Sopenharmony_ci		else
13628c2ecf20Sopenharmony_ci			b43legacy_dma_rx(dev->dma.rx_ring3);
13638c2ecf20Sopenharmony_ci	}
13648c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dma_reason[4] & B43legacy_DMAIRQ_RX_DONE);
13658c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dma_reason[5] & B43legacy_DMAIRQ_RX_DONE);
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	if (reason & B43legacy_IRQ_TX_OK)
13688c2ecf20Sopenharmony_ci		handle_irq_transmit_status(dev);
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
13718c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
13728c2ecf20Sopenharmony_ci}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_cistatic void pio_irq_workaround(struct b43legacy_wldev *dev,
13758c2ecf20Sopenharmony_ci			       u16 base, int queueidx)
13768c2ecf20Sopenharmony_ci{
13778c2ecf20Sopenharmony_ci	u16 rxctl;
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	rxctl = b43legacy_read16(dev, base + B43legacy_PIO_RXCTL);
13808c2ecf20Sopenharmony_ci	if (rxctl & B43legacy_PIO_RXCTL_DATAAVAILABLE)
13818c2ecf20Sopenharmony_ci		dev->dma_reason[queueidx] |= B43legacy_DMAIRQ_RX_DONE;
13828c2ecf20Sopenharmony_ci	else
13838c2ecf20Sopenharmony_ci		dev->dma_reason[queueidx] &= ~B43legacy_DMAIRQ_RX_DONE;
13848c2ecf20Sopenharmony_ci}
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_cistatic void b43legacy_interrupt_ack(struct b43legacy_wldev *dev, u32 reason)
13878c2ecf20Sopenharmony_ci{
13888c2ecf20Sopenharmony_ci	if (b43legacy_using_pio(dev) &&
13898c2ecf20Sopenharmony_ci	    (dev->dev->id.revision < 3) &&
13908c2ecf20Sopenharmony_ci	    (!(reason & B43legacy_IRQ_PIO_WORKAROUND))) {
13918c2ecf20Sopenharmony_ci		/* Apply a PIO specific workaround to the dma_reasons */
13928c2ecf20Sopenharmony_ci		pio_irq_workaround(dev, B43legacy_MMIO_PIO1_BASE, 0);
13938c2ecf20Sopenharmony_ci		pio_irq_workaround(dev, B43legacy_MMIO_PIO2_BASE, 1);
13948c2ecf20Sopenharmony_ci		pio_irq_workaround(dev, B43legacy_MMIO_PIO3_BASE, 2);
13958c2ecf20Sopenharmony_ci		pio_irq_workaround(dev, B43legacy_MMIO_PIO4_BASE, 3);
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, reason);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA0_REASON,
14018c2ecf20Sopenharmony_ci			  dev->dma_reason[0]);
14028c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA1_REASON,
14038c2ecf20Sopenharmony_ci			  dev->dma_reason[1]);
14048c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA2_REASON,
14058c2ecf20Sopenharmony_ci			  dev->dma_reason[2]);
14068c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA3_REASON,
14078c2ecf20Sopenharmony_ci			  dev->dma_reason[3]);
14088c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA4_REASON,
14098c2ecf20Sopenharmony_ci			  dev->dma_reason[4]);
14108c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA5_REASON,
14118c2ecf20Sopenharmony_ci			  dev->dma_reason[5]);
14128c2ecf20Sopenharmony_ci}
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci/* Interrupt handler top-half */
14158c2ecf20Sopenharmony_cistatic irqreturn_t b43legacy_interrupt_handler(int irq, void *dev_id)
14168c2ecf20Sopenharmony_ci{
14178c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_NONE;
14188c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = dev_id;
14198c2ecf20Sopenharmony_ci	u32 reason;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(!dev);
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	spin_lock(&dev->wl->irq_lock);
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	if (unlikely(b43legacy_status(dev) < B43legacy_STAT_STARTED))
14268c2ecf20Sopenharmony_ci		/* This can only happen on shared IRQ lines. */
14278c2ecf20Sopenharmony_ci		goto out;
14288c2ecf20Sopenharmony_ci	reason = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
14298c2ecf20Sopenharmony_ci	if (reason == 0xffffffff) /* shared IRQ */
14308c2ecf20Sopenharmony_ci		goto out;
14318c2ecf20Sopenharmony_ci	ret = IRQ_HANDLED;
14328c2ecf20Sopenharmony_ci	reason &= dev->irq_mask;
14338c2ecf20Sopenharmony_ci	if (!reason)
14348c2ecf20Sopenharmony_ci		goto out;
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	dev->dma_reason[0] = b43legacy_read32(dev,
14378c2ecf20Sopenharmony_ci					      B43legacy_MMIO_DMA0_REASON)
14388c2ecf20Sopenharmony_ci					      & 0x0001DC00;
14398c2ecf20Sopenharmony_ci	dev->dma_reason[1] = b43legacy_read32(dev,
14408c2ecf20Sopenharmony_ci					      B43legacy_MMIO_DMA1_REASON)
14418c2ecf20Sopenharmony_ci					      & 0x0000DC00;
14428c2ecf20Sopenharmony_ci	dev->dma_reason[2] = b43legacy_read32(dev,
14438c2ecf20Sopenharmony_ci					      B43legacy_MMIO_DMA2_REASON)
14448c2ecf20Sopenharmony_ci					      & 0x0000DC00;
14458c2ecf20Sopenharmony_ci	dev->dma_reason[3] = b43legacy_read32(dev,
14468c2ecf20Sopenharmony_ci					      B43legacy_MMIO_DMA3_REASON)
14478c2ecf20Sopenharmony_ci					      & 0x0001DC00;
14488c2ecf20Sopenharmony_ci	dev->dma_reason[4] = b43legacy_read32(dev,
14498c2ecf20Sopenharmony_ci					      B43legacy_MMIO_DMA4_REASON)
14508c2ecf20Sopenharmony_ci					      & 0x0000DC00;
14518c2ecf20Sopenharmony_ci	dev->dma_reason[5] = b43legacy_read32(dev,
14528c2ecf20Sopenharmony_ci					      B43legacy_MMIO_DMA5_REASON)
14538c2ecf20Sopenharmony_ci					      & 0x0000DC00;
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	b43legacy_interrupt_ack(dev, reason);
14568c2ecf20Sopenharmony_ci	/* Disable all IRQs. They are enabled again in the bottom half. */
14578c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
14588c2ecf20Sopenharmony_ci	/* Save the reason code and call our bottom half. */
14598c2ecf20Sopenharmony_ci	dev->irq_reason = reason;
14608c2ecf20Sopenharmony_ci	tasklet_schedule(&dev->isr_tasklet);
14618c2ecf20Sopenharmony_ciout:
14628c2ecf20Sopenharmony_ci	spin_unlock(&dev->wl->irq_lock);
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	return ret;
14658c2ecf20Sopenharmony_ci}
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_cistatic void b43legacy_release_firmware(struct b43legacy_wldev *dev)
14688c2ecf20Sopenharmony_ci{
14698c2ecf20Sopenharmony_ci	release_firmware(dev->fw.ucode);
14708c2ecf20Sopenharmony_ci	dev->fw.ucode = NULL;
14718c2ecf20Sopenharmony_ci	release_firmware(dev->fw.pcm);
14728c2ecf20Sopenharmony_ci	dev->fw.pcm = NULL;
14738c2ecf20Sopenharmony_ci	release_firmware(dev->fw.initvals);
14748c2ecf20Sopenharmony_ci	dev->fw.initvals = NULL;
14758c2ecf20Sopenharmony_ci	release_firmware(dev->fw.initvals_band);
14768c2ecf20Sopenharmony_ci	dev->fw.initvals_band = NULL;
14778c2ecf20Sopenharmony_ci}
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_cistatic void b43legacy_print_fw_helptext(struct b43legacy_wl *wl)
14808c2ecf20Sopenharmony_ci{
14818c2ecf20Sopenharmony_ci	b43legacyerr(wl, "You must go to https://wireless.wiki.kernel.org/en/"
14828c2ecf20Sopenharmony_ci		     "users/Drivers/b43#devicefirmware "
14838c2ecf20Sopenharmony_ci		     "and download the correct firmware (version 3).\n");
14848c2ecf20Sopenharmony_ci}
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_cistatic void b43legacy_fw_cb(const struct firmware *firmware, void *context)
14878c2ecf20Sopenharmony_ci{
14888c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = context;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci	dev->fwp = firmware;
14918c2ecf20Sopenharmony_ci	complete(&dev->fw_load_complete);
14928c2ecf20Sopenharmony_ci}
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_cistatic int do_request_fw(struct b43legacy_wldev *dev,
14958c2ecf20Sopenharmony_ci			 const char *name,
14968c2ecf20Sopenharmony_ci			 const struct firmware **fw, bool async)
14978c2ecf20Sopenharmony_ci{
14988c2ecf20Sopenharmony_ci	char path[sizeof(modparam_fwpostfix) + 32];
14998c2ecf20Sopenharmony_ci	struct b43legacy_fw_header *hdr;
15008c2ecf20Sopenharmony_ci	u32 size;
15018c2ecf20Sopenharmony_ci	int err;
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	if (!name)
15048c2ecf20Sopenharmony_ci		return 0;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	snprintf(path, ARRAY_SIZE(path),
15078c2ecf20Sopenharmony_ci		 "b43legacy%s/%s.fw",
15088c2ecf20Sopenharmony_ci		 modparam_fwpostfix, name);
15098c2ecf20Sopenharmony_ci	b43legacyinfo(dev->wl, "Loading firmware %s\n", path);
15108c2ecf20Sopenharmony_ci	if (async) {
15118c2ecf20Sopenharmony_ci		init_completion(&dev->fw_load_complete);
15128c2ecf20Sopenharmony_ci		err = request_firmware_nowait(THIS_MODULE, 1, path,
15138c2ecf20Sopenharmony_ci					      dev->dev->dev, GFP_KERNEL,
15148c2ecf20Sopenharmony_ci					      dev, b43legacy_fw_cb);
15158c2ecf20Sopenharmony_ci		if (err) {
15168c2ecf20Sopenharmony_ci			b43legacyerr(dev->wl, "Unable to load firmware\n");
15178c2ecf20Sopenharmony_ci			return err;
15188c2ecf20Sopenharmony_ci		}
15198c2ecf20Sopenharmony_ci		/* stall here until fw ready */
15208c2ecf20Sopenharmony_ci		wait_for_completion(&dev->fw_load_complete);
15218c2ecf20Sopenharmony_ci		if (!dev->fwp)
15228c2ecf20Sopenharmony_ci			err = -EINVAL;
15238c2ecf20Sopenharmony_ci		*fw = dev->fwp;
15248c2ecf20Sopenharmony_ci	} else {
15258c2ecf20Sopenharmony_ci		err = request_firmware(fw, path, dev->dev->dev);
15268c2ecf20Sopenharmony_ci	}
15278c2ecf20Sopenharmony_ci	if (err) {
15288c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "Firmware file \"%s\" not found "
15298c2ecf20Sopenharmony_ci		       "or load failed.\n", path);
15308c2ecf20Sopenharmony_ci		return err;
15318c2ecf20Sopenharmony_ci	}
15328c2ecf20Sopenharmony_ci	if ((*fw)->size < sizeof(struct b43legacy_fw_header))
15338c2ecf20Sopenharmony_ci		goto err_format;
15348c2ecf20Sopenharmony_ci	hdr = (struct b43legacy_fw_header *)((*fw)->data);
15358c2ecf20Sopenharmony_ci	switch (hdr->type) {
15368c2ecf20Sopenharmony_ci	case B43legacy_FW_TYPE_UCODE:
15378c2ecf20Sopenharmony_ci	case B43legacy_FW_TYPE_PCM:
15388c2ecf20Sopenharmony_ci		size = be32_to_cpu(hdr->size);
15398c2ecf20Sopenharmony_ci		if (size != (*fw)->size - sizeof(struct b43legacy_fw_header))
15408c2ecf20Sopenharmony_ci			goto err_format;
15418c2ecf20Sopenharmony_ci		fallthrough;
15428c2ecf20Sopenharmony_ci	case B43legacy_FW_TYPE_IV:
15438c2ecf20Sopenharmony_ci		if (hdr->ver != 1)
15448c2ecf20Sopenharmony_ci			goto err_format;
15458c2ecf20Sopenharmony_ci		break;
15468c2ecf20Sopenharmony_ci	default:
15478c2ecf20Sopenharmony_ci		goto err_format;
15488c2ecf20Sopenharmony_ci	}
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	return err;
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_cierr_format:
15538c2ecf20Sopenharmony_ci	b43legacyerr(dev->wl, "Firmware file \"%s\" format error.\n", path);
15548c2ecf20Sopenharmony_ci	return -EPROTO;
15558c2ecf20Sopenharmony_ci}
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_cistatic int b43legacy_one_core_attach(struct ssb_device *dev,
15588c2ecf20Sopenharmony_ci				     struct b43legacy_wl *wl);
15598c2ecf20Sopenharmony_cistatic void b43legacy_one_core_detach(struct ssb_device *dev);
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_cistatic void b43legacy_request_firmware(struct work_struct *work)
15628c2ecf20Sopenharmony_ci{
15638c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = container_of(work,
15648c2ecf20Sopenharmony_ci				  struct b43legacy_wl, firmware_load);
15658c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = wl->current_dev;
15668c2ecf20Sopenharmony_ci	struct b43legacy_firmware *fw = &dev->fw;
15678c2ecf20Sopenharmony_ci	const u8 rev = dev->dev->id.revision;
15688c2ecf20Sopenharmony_ci	const char *filename;
15698c2ecf20Sopenharmony_ci	int err;
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci	if (!fw->ucode) {
15728c2ecf20Sopenharmony_ci		if (rev == 2)
15738c2ecf20Sopenharmony_ci			filename = "ucode2";
15748c2ecf20Sopenharmony_ci		else if (rev == 4)
15758c2ecf20Sopenharmony_ci			filename = "ucode4";
15768c2ecf20Sopenharmony_ci		else
15778c2ecf20Sopenharmony_ci			filename = "ucode5";
15788c2ecf20Sopenharmony_ci		err = do_request_fw(dev, filename, &fw->ucode, true);
15798c2ecf20Sopenharmony_ci		if (err)
15808c2ecf20Sopenharmony_ci			goto err_load;
15818c2ecf20Sopenharmony_ci	}
15828c2ecf20Sopenharmony_ci	if (!fw->pcm) {
15838c2ecf20Sopenharmony_ci		if (rev < 5)
15848c2ecf20Sopenharmony_ci			filename = "pcm4";
15858c2ecf20Sopenharmony_ci		else
15868c2ecf20Sopenharmony_ci			filename = "pcm5";
15878c2ecf20Sopenharmony_ci		err = do_request_fw(dev, filename, &fw->pcm, false);
15888c2ecf20Sopenharmony_ci		if (err)
15898c2ecf20Sopenharmony_ci			goto err_load;
15908c2ecf20Sopenharmony_ci	}
15918c2ecf20Sopenharmony_ci	if (!fw->initvals) {
15928c2ecf20Sopenharmony_ci		switch (dev->phy.type) {
15938c2ecf20Sopenharmony_ci		case B43legacy_PHYTYPE_B:
15948c2ecf20Sopenharmony_ci		case B43legacy_PHYTYPE_G:
15958c2ecf20Sopenharmony_ci			if ((rev >= 5) && (rev <= 10))
15968c2ecf20Sopenharmony_ci				filename = "b0g0initvals5";
15978c2ecf20Sopenharmony_ci			else if (rev == 2 || rev == 4)
15988c2ecf20Sopenharmony_ci				filename = "b0g0initvals2";
15998c2ecf20Sopenharmony_ci			else
16008c2ecf20Sopenharmony_ci				goto err_no_initvals;
16018c2ecf20Sopenharmony_ci			break;
16028c2ecf20Sopenharmony_ci		default:
16038c2ecf20Sopenharmony_ci			goto err_no_initvals;
16048c2ecf20Sopenharmony_ci		}
16058c2ecf20Sopenharmony_ci		err = do_request_fw(dev, filename, &fw->initvals, false);
16068c2ecf20Sopenharmony_ci		if (err)
16078c2ecf20Sopenharmony_ci			goto err_load;
16088c2ecf20Sopenharmony_ci	}
16098c2ecf20Sopenharmony_ci	if (!fw->initvals_band) {
16108c2ecf20Sopenharmony_ci		switch (dev->phy.type) {
16118c2ecf20Sopenharmony_ci		case B43legacy_PHYTYPE_B:
16128c2ecf20Sopenharmony_ci		case B43legacy_PHYTYPE_G:
16138c2ecf20Sopenharmony_ci			if ((rev >= 5) && (rev <= 10))
16148c2ecf20Sopenharmony_ci				filename = "b0g0bsinitvals5";
16158c2ecf20Sopenharmony_ci			else if (rev >= 11)
16168c2ecf20Sopenharmony_ci				filename = NULL;
16178c2ecf20Sopenharmony_ci			else if (rev == 2 || rev == 4)
16188c2ecf20Sopenharmony_ci				filename = NULL;
16198c2ecf20Sopenharmony_ci			else
16208c2ecf20Sopenharmony_ci				goto err_no_initvals;
16218c2ecf20Sopenharmony_ci			break;
16228c2ecf20Sopenharmony_ci		default:
16238c2ecf20Sopenharmony_ci			goto err_no_initvals;
16248c2ecf20Sopenharmony_ci		}
16258c2ecf20Sopenharmony_ci		err = do_request_fw(dev, filename, &fw->initvals_band, false);
16268c2ecf20Sopenharmony_ci		if (err)
16278c2ecf20Sopenharmony_ci			goto err_load;
16288c2ecf20Sopenharmony_ci	}
16298c2ecf20Sopenharmony_ci	err = ieee80211_register_hw(wl->hw);
16308c2ecf20Sopenharmony_ci	if (err)
16318c2ecf20Sopenharmony_ci		goto err_one_core_detach;
16328c2ecf20Sopenharmony_ci	return;
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_cierr_one_core_detach:
16358c2ecf20Sopenharmony_ci	b43legacy_one_core_detach(dev->dev);
16368c2ecf20Sopenharmony_ci	goto error;
16378c2ecf20Sopenharmony_ci
16388c2ecf20Sopenharmony_cierr_load:
16398c2ecf20Sopenharmony_ci	b43legacy_print_fw_helptext(dev->wl);
16408c2ecf20Sopenharmony_ci	goto error;
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_cierr_no_initvals:
16438c2ecf20Sopenharmony_ci	err = -ENODEV;
16448c2ecf20Sopenharmony_ci	b43legacyerr(dev->wl, "No Initial Values firmware file for PHY %u, "
16458c2ecf20Sopenharmony_ci	       "core rev %u\n", dev->phy.type, rev);
16468c2ecf20Sopenharmony_ci	goto error;
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_cierror:
16498c2ecf20Sopenharmony_ci	b43legacy_release_firmware(dev);
16508c2ecf20Sopenharmony_ci	return;
16518c2ecf20Sopenharmony_ci}
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_cistatic int b43legacy_upload_microcode(struct b43legacy_wldev *dev)
16548c2ecf20Sopenharmony_ci{
16558c2ecf20Sopenharmony_ci	struct wiphy *wiphy = dev->wl->hw->wiphy;
16568c2ecf20Sopenharmony_ci	const size_t hdr_len = sizeof(struct b43legacy_fw_header);
16578c2ecf20Sopenharmony_ci	const __be32 *data;
16588c2ecf20Sopenharmony_ci	unsigned int i;
16598c2ecf20Sopenharmony_ci	unsigned int len;
16608c2ecf20Sopenharmony_ci	u16 fwrev;
16618c2ecf20Sopenharmony_ci	u16 fwpatch;
16628c2ecf20Sopenharmony_ci	u16 fwdate;
16638c2ecf20Sopenharmony_ci	u16 fwtime;
16648c2ecf20Sopenharmony_ci	u32 tmp, macctl;
16658c2ecf20Sopenharmony_ci	int err = 0;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	/* Jump the microcode PSM to offset 0 */
16688c2ecf20Sopenharmony_ci	macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
16698c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(macctl & B43legacy_MACCTL_PSM_RUN);
16708c2ecf20Sopenharmony_ci	macctl |= B43legacy_MACCTL_PSM_JMP0;
16718c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
16728c2ecf20Sopenharmony_ci	/* Zero out all microcode PSM registers and shared memory. */
16738c2ecf20Sopenharmony_ci	for (i = 0; i < 64; i++)
16748c2ecf20Sopenharmony_ci		b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, i, 0);
16758c2ecf20Sopenharmony_ci	for (i = 0; i < 4096; i += 2)
16768c2ecf20Sopenharmony_ci		b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, i, 0);
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	/* Upload Microcode. */
16798c2ecf20Sopenharmony_ci	data = (__be32 *) (dev->fw.ucode->data + hdr_len);
16808c2ecf20Sopenharmony_ci	len = (dev->fw.ucode->size - hdr_len) / sizeof(__be32);
16818c2ecf20Sopenharmony_ci	b43legacy_shm_control_word(dev,
16828c2ecf20Sopenharmony_ci				   B43legacy_SHM_UCODE |
16838c2ecf20Sopenharmony_ci				   B43legacy_SHM_AUTOINC_W,
16848c2ecf20Sopenharmony_ci				   0x0000);
16858c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
16868c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
16878c2ecf20Sopenharmony_ci				    be32_to_cpu(data[i]));
16888c2ecf20Sopenharmony_ci		udelay(10);
16898c2ecf20Sopenharmony_ci	}
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_ci	if (dev->fw.pcm) {
16928c2ecf20Sopenharmony_ci		/* Upload PCM data. */
16938c2ecf20Sopenharmony_ci		data = (__be32 *) (dev->fw.pcm->data + hdr_len);
16948c2ecf20Sopenharmony_ci		len = (dev->fw.pcm->size - hdr_len) / sizeof(__be32);
16958c2ecf20Sopenharmony_ci		b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EA);
16968c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA, 0x00004000);
16978c2ecf20Sopenharmony_ci		/* No need for autoinc bit in SHM_HW */
16988c2ecf20Sopenharmony_ci		b43legacy_shm_control_word(dev, B43legacy_SHM_HW, 0x01EB);
16998c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++) {
17008c2ecf20Sopenharmony_ci			b43legacy_write32(dev, B43legacy_MMIO_SHM_DATA,
17018c2ecf20Sopenharmony_ci					  be32_to_cpu(data[i]));
17028c2ecf20Sopenharmony_ci			udelay(10);
17038c2ecf20Sopenharmony_ci		}
17048c2ecf20Sopenharmony_ci	}
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
17078c2ecf20Sopenharmony_ci			  B43legacy_IRQ_ALL);
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	/* Start the microcode PSM */
17108c2ecf20Sopenharmony_ci	macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
17118c2ecf20Sopenharmony_ci	macctl &= ~B43legacy_MACCTL_PSM_JMP0;
17128c2ecf20Sopenharmony_ci	macctl |= B43legacy_MACCTL_PSM_RUN;
17138c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	/* Wait for the microcode to load and respond */
17168c2ecf20Sopenharmony_ci	i = 0;
17178c2ecf20Sopenharmony_ci	while (1) {
17188c2ecf20Sopenharmony_ci		tmp = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
17198c2ecf20Sopenharmony_ci		if (tmp == B43legacy_IRQ_MAC_SUSPENDED)
17208c2ecf20Sopenharmony_ci			break;
17218c2ecf20Sopenharmony_ci		i++;
17228c2ecf20Sopenharmony_ci		if (i >= B43legacy_IRQWAIT_MAX_RETRIES) {
17238c2ecf20Sopenharmony_ci			b43legacyerr(dev->wl, "Microcode not responding\n");
17248c2ecf20Sopenharmony_ci			b43legacy_print_fw_helptext(dev->wl);
17258c2ecf20Sopenharmony_ci			err = -ENODEV;
17268c2ecf20Sopenharmony_ci			goto error;
17278c2ecf20Sopenharmony_ci		}
17288c2ecf20Sopenharmony_ci		msleep_interruptible(50);
17298c2ecf20Sopenharmony_ci		if (signal_pending(current)) {
17308c2ecf20Sopenharmony_ci			err = -EINTR;
17318c2ecf20Sopenharmony_ci			goto error;
17328c2ecf20Sopenharmony_ci		}
17338c2ecf20Sopenharmony_ci	}
17348c2ecf20Sopenharmony_ci	/* dummy read follows */
17358c2ecf20Sopenharmony_ci	b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci	/* Get and check the revisions. */
17388c2ecf20Sopenharmony_ci	fwrev = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
17398c2ecf20Sopenharmony_ci				     B43legacy_SHM_SH_UCODEREV);
17408c2ecf20Sopenharmony_ci	fwpatch = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
17418c2ecf20Sopenharmony_ci				       B43legacy_SHM_SH_UCODEPATCH);
17428c2ecf20Sopenharmony_ci	fwdate = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
17438c2ecf20Sopenharmony_ci				      B43legacy_SHM_SH_UCODEDATE);
17448c2ecf20Sopenharmony_ci	fwtime = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
17458c2ecf20Sopenharmony_ci				      B43legacy_SHM_SH_UCODETIME);
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	if (fwrev > 0x128) {
17488c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "YOU ARE TRYING TO LOAD V4 FIRMWARE."
17498c2ecf20Sopenharmony_ci			     " Only firmware from binary drivers version 3.x"
17508c2ecf20Sopenharmony_ci			     " is supported. You must change your firmware"
17518c2ecf20Sopenharmony_ci			     " files.\n");
17528c2ecf20Sopenharmony_ci		b43legacy_print_fw_helptext(dev->wl);
17538c2ecf20Sopenharmony_ci		err = -EOPNOTSUPP;
17548c2ecf20Sopenharmony_ci		goto error;
17558c2ecf20Sopenharmony_ci	}
17568c2ecf20Sopenharmony_ci	b43legacyinfo(dev->wl, "Loading firmware version 0x%X, patch level %u "
17578c2ecf20Sopenharmony_ci		      "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n", fwrev, fwpatch,
17588c2ecf20Sopenharmony_ci		      (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
17598c2ecf20Sopenharmony_ci		      (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F,
17608c2ecf20Sopenharmony_ci		      fwtime & 0x1F);
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	dev->fw.rev = fwrev;
17638c2ecf20Sopenharmony_ci	dev->fw.patch = fwpatch;
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), "%u.%u",
17668c2ecf20Sopenharmony_ci			dev->fw.rev, dev->fw.patch);
17678c2ecf20Sopenharmony_ci	wiphy->hw_version = dev->dev->id.coreid;
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci	return 0;
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_cierror:
17728c2ecf20Sopenharmony_ci	macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
17738c2ecf20Sopenharmony_ci	macctl &= ~B43legacy_MACCTL_PSM_RUN;
17748c2ecf20Sopenharmony_ci	macctl |= B43legacy_MACCTL_PSM_JMP0;
17758c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci	return err;
17788c2ecf20Sopenharmony_ci}
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_cistatic int b43legacy_write_initvals(struct b43legacy_wldev *dev,
17818c2ecf20Sopenharmony_ci				    const struct b43legacy_iv *ivals,
17828c2ecf20Sopenharmony_ci				    size_t count,
17838c2ecf20Sopenharmony_ci				    size_t array_size)
17848c2ecf20Sopenharmony_ci{
17858c2ecf20Sopenharmony_ci	const struct b43legacy_iv *iv;
17868c2ecf20Sopenharmony_ci	u16 offset;
17878c2ecf20Sopenharmony_ci	size_t i;
17888c2ecf20Sopenharmony_ci	bool bit32;
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct b43legacy_iv) != 6);
17918c2ecf20Sopenharmony_ci	iv = ivals;
17928c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
17938c2ecf20Sopenharmony_ci		if (array_size < sizeof(iv->offset_size))
17948c2ecf20Sopenharmony_ci			goto err_format;
17958c2ecf20Sopenharmony_ci		array_size -= sizeof(iv->offset_size);
17968c2ecf20Sopenharmony_ci		offset = be16_to_cpu(iv->offset_size);
17978c2ecf20Sopenharmony_ci		bit32 = !!(offset & B43legacy_IV_32BIT);
17988c2ecf20Sopenharmony_ci		offset &= B43legacy_IV_OFFSET_MASK;
17998c2ecf20Sopenharmony_ci		if (offset >= 0x1000)
18008c2ecf20Sopenharmony_ci			goto err_format;
18018c2ecf20Sopenharmony_ci		if (bit32) {
18028c2ecf20Sopenharmony_ci			u32 value;
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci			if (array_size < sizeof(iv->data.d32))
18058c2ecf20Sopenharmony_ci				goto err_format;
18068c2ecf20Sopenharmony_ci			array_size -= sizeof(iv->data.d32);
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ci			value = get_unaligned_be32(&iv->data.d32);
18098c2ecf20Sopenharmony_ci			b43legacy_write32(dev, offset, value);
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ci			iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
18128c2ecf20Sopenharmony_ci							sizeof(__be16) +
18138c2ecf20Sopenharmony_ci							sizeof(__be32));
18148c2ecf20Sopenharmony_ci		} else {
18158c2ecf20Sopenharmony_ci			u16 value;
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci			if (array_size < sizeof(iv->data.d16))
18188c2ecf20Sopenharmony_ci				goto err_format;
18198c2ecf20Sopenharmony_ci			array_size -= sizeof(iv->data.d16);
18208c2ecf20Sopenharmony_ci
18218c2ecf20Sopenharmony_ci			value = be16_to_cpu(iv->data.d16);
18228c2ecf20Sopenharmony_ci			b43legacy_write16(dev, offset, value);
18238c2ecf20Sopenharmony_ci
18248c2ecf20Sopenharmony_ci			iv = (const struct b43legacy_iv *)((const uint8_t *)iv +
18258c2ecf20Sopenharmony_ci							sizeof(__be16) +
18268c2ecf20Sopenharmony_ci							sizeof(__be16));
18278c2ecf20Sopenharmony_ci		}
18288c2ecf20Sopenharmony_ci	}
18298c2ecf20Sopenharmony_ci	if (array_size)
18308c2ecf20Sopenharmony_ci		goto err_format;
18318c2ecf20Sopenharmony_ci
18328c2ecf20Sopenharmony_ci	return 0;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_cierr_format:
18358c2ecf20Sopenharmony_ci	b43legacyerr(dev->wl, "Initial Values Firmware file-format error.\n");
18368c2ecf20Sopenharmony_ci	b43legacy_print_fw_helptext(dev->wl);
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	return -EPROTO;
18398c2ecf20Sopenharmony_ci}
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_cistatic int b43legacy_upload_initvals(struct b43legacy_wldev *dev)
18428c2ecf20Sopenharmony_ci{
18438c2ecf20Sopenharmony_ci	const size_t hdr_len = sizeof(struct b43legacy_fw_header);
18448c2ecf20Sopenharmony_ci	const struct b43legacy_fw_header *hdr;
18458c2ecf20Sopenharmony_ci	struct b43legacy_firmware *fw = &dev->fw;
18468c2ecf20Sopenharmony_ci	const struct b43legacy_iv *ivals;
18478c2ecf20Sopenharmony_ci	size_t count;
18488c2ecf20Sopenharmony_ci	int err;
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	hdr = (const struct b43legacy_fw_header *)(fw->initvals->data);
18518c2ecf20Sopenharmony_ci	ivals = (const struct b43legacy_iv *)(fw->initvals->data + hdr_len);
18528c2ecf20Sopenharmony_ci	count = be32_to_cpu(hdr->size);
18538c2ecf20Sopenharmony_ci	err = b43legacy_write_initvals(dev, ivals, count,
18548c2ecf20Sopenharmony_ci				 fw->initvals->size - hdr_len);
18558c2ecf20Sopenharmony_ci	if (err)
18568c2ecf20Sopenharmony_ci		goto out;
18578c2ecf20Sopenharmony_ci	if (fw->initvals_band) {
18588c2ecf20Sopenharmony_ci		hdr = (const struct b43legacy_fw_header *)
18598c2ecf20Sopenharmony_ci		      (fw->initvals_band->data);
18608c2ecf20Sopenharmony_ci		ivals = (const struct b43legacy_iv *)(fw->initvals_band->data
18618c2ecf20Sopenharmony_ci			+ hdr_len);
18628c2ecf20Sopenharmony_ci		count = be32_to_cpu(hdr->size);
18638c2ecf20Sopenharmony_ci		err = b43legacy_write_initvals(dev, ivals, count,
18648c2ecf20Sopenharmony_ci					 fw->initvals_band->size - hdr_len);
18658c2ecf20Sopenharmony_ci		if (err)
18668c2ecf20Sopenharmony_ci			goto out;
18678c2ecf20Sopenharmony_ci	}
18688c2ecf20Sopenharmony_ciout:
18698c2ecf20Sopenharmony_ci
18708c2ecf20Sopenharmony_ci	return err;
18718c2ecf20Sopenharmony_ci}
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci/* Initialize the GPIOs
18748c2ecf20Sopenharmony_ci * https://bcm-specs.sipsolutions.net/GPIO
18758c2ecf20Sopenharmony_ci */
18768c2ecf20Sopenharmony_cistatic int b43legacy_gpio_init(struct b43legacy_wldev *dev)
18778c2ecf20Sopenharmony_ci{
18788c2ecf20Sopenharmony_ci	struct ssb_bus *bus = dev->dev->bus;
18798c2ecf20Sopenharmony_ci	struct ssb_device *gpiodev, *pcidev = NULL;
18808c2ecf20Sopenharmony_ci	u32 mask;
18818c2ecf20Sopenharmony_ci	u32 set;
18828c2ecf20Sopenharmony_ci
18838c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
18848c2ecf20Sopenharmony_ci			  b43legacy_read32(dev,
18858c2ecf20Sopenharmony_ci			  B43legacy_MMIO_MACCTL)
18868c2ecf20Sopenharmony_ci			  & 0xFFFF3FFF);
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
18898c2ecf20Sopenharmony_ci			  b43legacy_read16(dev,
18908c2ecf20Sopenharmony_ci			  B43legacy_MMIO_GPIO_MASK)
18918c2ecf20Sopenharmony_ci			  | 0x000F);
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_ci	mask = 0x0000001F;
18948c2ecf20Sopenharmony_ci	set = 0x0000000F;
18958c2ecf20Sopenharmony_ci	if (dev->dev->bus->chip_id == 0x4301) {
18968c2ecf20Sopenharmony_ci		mask |= 0x0060;
18978c2ecf20Sopenharmony_ci		set |= 0x0060;
18988c2ecf20Sopenharmony_ci	}
18998c2ecf20Sopenharmony_ci	if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL) {
19008c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_GPIO_MASK,
19018c2ecf20Sopenharmony_ci				  b43legacy_read16(dev,
19028c2ecf20Sopenharmony_ci				  B43legacy_MMIO_GPIO_MASK)
19038c2ecf20Sopenharmony_ci				  | 0x0200);
19048c2ecf20Sopenharmony_ci		mask |= 0x0200;
19058c2ecf20Sopenharmony_ci		set |= 0x0200;
19068c2ecf20Sopenharmony_ci	}
19078c2ecf20Sopenharmony_ci	if (dev->dev->id.revision >= 2)
19088c2ecf20Sopenharmony_ci		mask  |= 0x0010; /* FIXME: This is redundant. */
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_DRIVER_PCICORE
19118c2ecf20Sopenharmony_ci	pcidev = bus->pcicore.dev;
19128c2ecf20Sopenharmony_ci#endif
19138c2ecf20Sopenharmony_ci	gpiodev = bus->chipco.dev ? : pcidev;
19148c2ecf20Sopenharmony_ci	if (!gpiodev)
19158c2ecf20Sopenharmony_ci		return 0;
19168c2ecf20Sopenharmony_ci	ssb_write32(gpiodev, B43legacy_GPIO_CONTROL,
19178c2ecf20Sopenharmony_ci		    (ssb_read32(gpiodev, B43legacy_GPIO_CONTROL)
19188c2ecf20Sopenharmony_ci		     & ~mask) | set);
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	return 0;
19218c2ecf20Sopenharmony_ci}
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci/* Turn off all GPIO stuff. Call this on module unload, for example. */
19248c2ecf20Sopenharmony_cistatic void b43legacy_gpio_cleanup(struct b43legacy_wldev *dev)
19258c2ecf20Sopenharmony_ci{
19268c2ecf20Sopenharmony_ci	struct ssb_bus *bus = dev->dev->bus;
19278c2ecf20Sopenharmony_ci	struct ssb_device *gpiodev, *pcidev = NULL;
19288c2ecf20Sopenharmony_ci
19298c2ecf20Sopenharmony_ci#ifdef CONFIG_SSB_DRIVER_PCICORE
19308c2ecf20Sopenharmony_ci	pcidev = bus->pcicore.dev;
19318c2ecf20Sopenharmony_ci#endif
19328c2ecf20Sopenharmony_ci	gpiodev = bus->chipco.dev ? : pcidev;
19338c2ecf20Sopenharmony_ci	if (!gpiodev)
19348c2ecf20Sopenharmony_ci		return;
19358c2ecf20Sopenharmony_ci	ssb_write32(gpiodev, B43legacy_GPIO_CONTROL, 0);
19368c2ecf20Sopenharmony_ci}
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci/* http://bcm-specs.sipsolutions.net/EnableMac */
19398c2ecf20Sopenharmony_civoid b43legacy_mac_enable(struct b43legacy_wldev *dev)
19408c2ecf20Sopenharmony_ci{
19418c2ecf20Sopenharmony_ci	dev->mac_suspended--;
19428c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dev->mac_suspended < 0);
19438c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(irqs_disabled());
19448c2ecf20Sopenharmony_ci	if (dev->mac_suspended == 0) {
19458c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
19468c2ecf20Sopenharmony_ci				  b43legacy_read32(dev,
19478c2ecf20Sopenharmony_ci				  B43legacy_MMIO_MACCTL)
19488c2ecf20Sopenharmony_ci				  | B43legacy_MACCTL_ENABLED);
19498c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON,
19508c2ecf20Sopenharmony_ci				  B43legacy_IRQ_MAC_SUSPENDED);
19518c2ecf20Sopenharmony_ci		/* the next two are dummy reads */
19528c2ecf20Sopenharmony_ci		b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
19538c2ecf20Sopenharmony_ci		b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
19548c2ecf20Sopenharmony_ci		b43legacy_power_saving_ctl_bits(dev, -1, -1);
19558c2ecf20Sopenharmony_ci
19568c2ecf20Sopenharmony_ci		/* Re-enable IRQs. */
19578c2ecf20Sopenharmony_ci		spin_lock_irq(&dev->wl->irq_lock);
19588c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK,
19598c2ecf20Sopenharmony_ci				  dev->irq_mask);
19608c2ecf20Sopenharmony_ci		spin_unlock_irq(&dev->wl->irq_lock);
19618c2ecf20Sopenharmony_ci	}
19628c2ecf20Sopenharmony_ci}
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci/* https://bcm-specs.sipsolutions.net/SuspendMAC */
19658c2ecf20Sopenharmony_civoid b43legacy_mac_suspend(struct b43legacy_wldev *dev)
19668c2ecf20Sopenharmony_ci{
19678c2ecf20Sopenharmony_ci	int i;
19688c2ecf20Sopenharmony_ci	u32 tmp;
19698c2ecf20Sopenharmony_ci
19708c2ecf20Sopenharmony_ci	might_sleep();
19718c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(irqs_disabled());
19728c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dev->mac_suspended < 0);
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci	if (dev->mac_suspended == 0) {
19758c2ecf20Sopenharmony_ci		/* Mask IRQs before suspending MAC. Otherwise
19768c2ecf20Sopenharmony_ci		 * the MAC stays busy and won't suspend. */
19778c2ecf20Sopenharmony_ci		spin_lock_irq(&dev->wl->irq_lock);
19788c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
19798c2ecf20Sopenharmony_ci		spin_unlock_irq(&dev->wl->irq_lock);
19808c2ecf20Sopenharmony_ci		b43legacy_synchronize_irq(dev);
19818c2ecf20Sopenharmony_ci
19828c2ecf20Sopenharmony_ci		b43legacy_power_saving_ctl_bits(dev, -1, 1);
19838c2ecf20Sopenharmony_ci		b43legacy_write32(dev, B43legacy_MMIO_MACCTL,
19848c2ecf20Sopenharmony_ci				  b43legacy_read32(dev,
19858c2ecf20Sopenharmony_ci				  B43legacy_MMIO_MACCTL)
19868c2ecf20Sopenharmony_ci				  & ~B43legacy_MACCTL_ENABLED);
19878c2ecf20Sopenharmony_ci		b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
19888c2ecf20Sopenharmony_ci		for (i = 40; i; i--) {
19898c2ecf20Sopenharmony_ci			tmp = b43legacy_read32(dev,
19908c2ecf20Sopenharmony_ci					       B43legacy_MMIO_GEN_IRQ_REASON);
19918c2ecf20Sopenharmony_ci			if (tmp & B43legacy_IRQ_MAC_SUSPENDED)
19928c2ecf20Sopenharmony_ci				goto out;
19938c2ecf20Sopenharmony_ci			msleep(1);
19948c2ecf20Sopenharmony_ci		}
19958c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "MAC suspend failed\n");
19968c2ecf20Sopenharmony_ci	}
19978c2ecf20Sopenharmony_ciout:
19988c2ecf20Sopenharmony_ci	dev->mac_suspended++;
19998c2ecf20Sopenharmony_ci}
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_cistatic void b43legacy_adjust_opmode(struct b43legacy_wldev *dev)
20028c2ecf20Sopenharmony_ci{
20038c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
20048c2ecf20Sopenharmony_ci	u32 ctl;
20058c2ecf20Sopenharmony_ci	u16 cfp_pretbtt;
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	ctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
20088c2ecf20Sopenharmony_ci	/* Reset status to STA infrastructure mode. */
20098c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_MACCTL_AP;
20108c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_MACCTL_KEEP_CTL;
20118c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_MACCTL_KEEP_BADPLCP;
20128c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_MACCTL_KEEP_BAD;
20138c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_MACCTL_PROMISC;
20148c2ecf20Sopenharmony_ci	ctl &= ~B43legacy_MACCTL_BEACPROMISC;
20158c2ecf20Sopenharmony_ci	ctl |= B43legacy_MACCTL_INFRA;
20168c2ecf20Sopenharmony_ci
20178c2ecf20Sopenharmony_ci	if (b43legacy_is_mode(wl, NL80211_IFTYPE_AP))
20188c2ecf20Sopenharmony_ci		ctl |= B43legacy_MACCTL_AP;
20198c2ecf20Sopenharmony_ci	else if (b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC))
20208c2ecf20Sopenharmony_ci		ctl &= ~B43legacy_MACCTL_INFRA;
20218c2ecf20Sopenharmony_ci
20228c2ecf20Sopenharmony_ci	if (wl->filter_flags & FIF_CONTROL)
20238c2ecf20Sopenharmony_ci		ctl |= B43legacy_MACCTL_KEEP_CTL;
20248c2ecf20Sopenharmony_ci	if (wl->filter_flags & FIF_FCSFAIL)
20258c2ecf20Sopenharmony_ci		ctl |= B43legacy_MACCTL_KEEP_BAD;
20268c2ecf20Sopenharmony_ci	if (wl->filter_flags & FIF_PLCPFAIL)
20278c2ecf20Sopenharmony_ci		ctl |= B43legacy_MACCTL_KEEP_BADPLCP;
20288c2ecf20Sopenharmony_ci	if (wl->filter_flags & FIF_BCN_PRBRESP_PROMISC)
20298c2ecf20Sopenharmony_ci		ctl |= B43legacy_MACCTL_BEACPROMISC;
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	/* Workaround: On old hardware the HW-MAC-address-filter
20328c2ecf20Sopenharmony_ci	 * doesn't work properly, so always run promisc in filter
20338c2ecf20Sopenharmony_ci	 * it in software. */
20348c2ecf20Sopenharmony_ci	if (dev->dev->id.revision <= 4)
20358c2ecf20Sopenharmony_ci		ctl |= B43legacy_MACCTL_PROMISC;
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, ctl);
20388c2ecf20Sopenharmony_ci
20398c2ecf20Sopenharmony_ci	cfp_pretbtt = 2;
20408c2ecf20Sopenharmony_ci	if ((ctl & B43legacy_MACCTL_INFRA) &&
20418c2ecf20Sopenharmony_ci	    !(ctl & B43legacy_MACCTL_AP)) {
20428c2ecf20Sopenharmony_ci		if (dev->dev->bus->chip_id == 0x4306 &&
20438c2ecf20Sopenharmony_ci		    dev->dev->bus->chip_rev == 3)
20448c2ecf20Sopenharmony_ci			cfp_pretbtt = 100;
20458c2ecf20Sopenharmony_ci		else
20468c2ecf20Sopenharmony_ci			cfp_pretbtt = 50;
20478c2ecf20Sopenharmony_ci	}
20488c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x612, cfp_pretbtt);
20498c2ecf20Sopenharmony_ci}
20508c2ecf20Sopenharmony_ci
20518c2ecf20Sopenharmony_cistatic void b43legacy_rate_memory_write(struct b43legacy_wldev *dev,
20528c2ecf20Sopenharmony_ci					u16 rate,
20538c2ecf20Sopenharmony_ci					int is_ofdm)
20548c2ecf20Sopenharmony_ci{
20558c2ecf20Sopenharmony_ci	u16 offset;
20568c2ecf20Sopenharmony_ci
20578c2ecf20Sopenharmony_ci	if (is_ofdm) {
20588c2ecf20Sopenharmony_ci		offset = 0x480;
20598c2ecf20Sopenharmony_ci		offset += (b43legacy_plcp_get_ratecode_ofdm(rate) & 0x000F) * 2;
20608c2ecf20Sopenharmony_ci	} else {
20618c2ecf20Sopenharmony_ci		offset = 0x4C0;
20628c2ecf20Sopenharmony_ci		offset += (b43legacy_plcp_get_ratecode_cck(rate) & 0x000F) * 2;
20638c2ecf20Sopenharmony_ci	}
20648c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, offset + 0x20,
20658c2ecf20Sopenharmony_ci			      b43legacy_shm_read16(dev,
20668c2ecf20Sopenharmony_ci			      B43legacy_SHM_SHARED, offset));
20678c2ecf20Sopenharmony_ci}
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_cistatic void b43legacy_rate_memory_init(struct b43legacy_wldev *dev)
20708c2ecf20Sopenharmony_ci{
20718c2ecf20Sopenharmony_ci	switch (dev->phy.type) {
20728c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_G:
20738c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_6MB, 1);
20748c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_12MB, 1);
20758c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_18MB, 1);
20768c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_24MB, 1);
20778c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_36MB, 1);
20788c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_48MB, 1);
20798c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_OFDM_RATE_54MB, 1);
20808c2ecf20Sopenharmony_ci		fallthrough;
20818c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_B:
20828c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_1MB, 0);
20838c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_2MB, 0);
20848c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_5MB, 0);
20858c2ecf20Sopenharmony_ci		b43legacy_rate_memory_write(dev, B43legacy_CCK_RATE_11MB, 0);
20868c2ecf20Sopenharmony_ci		break;
20878c2ecf20Sopenharmony_ci	default:
20888c2ecf20Sopenharmony_ci		B43legacy_BUG_ON(1);
20898c2ecf20Sopenharmony_ci	}
20908c2ecf20Sopenharmony_ci}
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci/* Set the TX-Antenna for management frames sent by firmware. */
20938c2ecf20Sopenharmony_cistatic void b43legacy_mgmtframe_txantenna(struct b43legacy_wldev *dev,
20948c2ecf20Sopenharmony_ci					  int antenna)
20958c2ecf20Sopenharmony_ci{
20968c2ecf20Sopenharmony_ci	u16 ant = 0;
20978c2ecf20Sopenharmony_ci	u16 tmp;
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_ci	switch (antenna) {
21008c2ecf20Sopenharmony_ci	case B43legacy_ANTENNA0:
21018c2ecf20Sopenharmony_ci		ant |= B43legacy_TX4_PHY_ANT0;
21028c2ecf20Sopenharmony_ci		break;
21038c2ecf20Sopenharmony_ci	case B43legacy_ANTENNA1:
21048c2ecf20Sopenharmony_ci		ant |= B43legacy_TX4_PHY_ANT1;
21058c2ecf20Sopenharmony_ci		break;
21068c2ecf20Sopenharmony_ci	case B43legacy_ANTENNA_AUTO:
21078c2ecf20Sopenharmony_ci		ant |= B43legacy_TX4_PHY_ANTLAST;
21088c2ecf20Sopenharmony_ci		break;
21098c2ecf20Sopenharmony_ci	default:
21108c2ecf20Sopenharmony_ci		B43legacy_BUG_ON(1);
21118c2ecf20Sopenharmony_ci	}
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_ci	/* FIXME We also need to set the other flags of the PHY control
21148c2ecf20Sopenharmony_ci	 * field somewhere. */
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci	/* For Beacons */
21178c2ecf20Sopenharmony_ci	tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
21188c2ecf20Sopenharmony_ci				   B43legacy_SHM_SH_BEACPHYCTL);
21198c2ecf20Sopenharmony_ci	tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
21208c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
21218c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_BEACPHYCTL, tmp);
21228c2ecf20Sopenharmony_ci	/* For ACK/CTS */
21238c2ecf20Sopenharmony_ci	tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
21248c2ecf20Sopenharmony_ci				   B43legacy_SHM_SH_ACKCTSPHYCTL);
21258c2ecf20Sopenharmony_ci	tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
21268c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
21278c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_ACKCTSPHYCTL, tmp);
21288c2ecf20Sopenharmony_ci	/* For Probe Resposes */
21298c2ecf20Sopenharmony_ci	tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
21308c2ecf20Sopenharmony_ci				   B43legacy_SHM_SH_PRPHYCTL);
21318c2ecf20Sopenharmony_ci	tmp = (tmp & ~B43legacy_TX4_PHY_ANT) | ant;
21328c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
21338c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_PRPHYCTL, tmp);
21348c2ecf20Sopenharmony_ci}
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci/* This is the opposite of b43legacy_chip_init() */
21378c2ecf20Sopenharmony_cistatic void b43legacy_chip_exit(struct b43legacy_wldev *dev)
21388c2ecf20Sopenharmony_ci{
21398c2ecf20Sopenharmony_ci	b43legacy_radio_turn_off(dev, 1);
21408c2ecf20Sopenharmony_ci	b43legacy_gpio_cleanup(dev);
21418c2ecf20Sopenharmony_ci	/* firmware is released later */
21428c2ecf20Sopenharmony_ci}
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci/* Initialize the chip
21458c2ecf20Sopenharmony_ci * https://bcm-specs.sipsolutions.net/ChipInit
21468c2ecf20Sopenharmony_ci */
21478c2ecf20Sopenharmony_cistatic int b43legacy_chip_init(struct b43legacy_wldev *dev)
21488c2ecf20Sopenharmony_ci{
21498c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
21508c2ecf20Sopenharmony_ci	int err;
21518c2ecf20Sopenharmony_ci	int tmp;
21528c2ecf20Sopenharmony_ci	u32 value32, macctl;
21538c2ecf20Sopenharmony_ci	u16 value16;
21548c2ecf20Sopenharmony_ci
21558c2ecf20Sopenharmony_ci	/* Initialize the MAC control */
21568c2ecf20Sopenharmony_ci	macctl = B43legacy_MACCTL_IHR_ENABLED | B43legacy_MACCTL_SHM_ENABLED;
21578c2ecf20Sopenharmony_ci	if (dev->phy.gmode)
21588c2ecf20Sopenharmony_ci		macctl |= B43legacy_MACCTL_GMODE;
21598c2ecf20Sopenharmony_ci	macctl |= B43legacy_MACCTL_INFRA;
21608c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
21618c2ecf20Sopenharmony_ci
21628c2ecf20Sopenharmony_ci	err = b43legacy_upload_microcode(dev);
21638c2ecf20Sopenharmony_ci	if (err)
21648c2ecf20Sopenharmony_ci		goto out; /* firmware is released later */
21658c2ecf20Sopenharmony_ci
21668c2ecf20Sopenharmony_ci	err = b43legacy_gpio_init(dev);
21678c2ecf20Sopenharmony_ci	if (err)
21688c2ecf20Sopenharmony_ci		goto out; /* firmware is released later */
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	err = b43legacy_upload_initvals(dev);
21718c2ecf20Sopenharmony_ci	if (err)
21728c2ecf20Sopenharmony_ci		goto err_gpio_clean;
21738c2ecf20Sopenharmony_ci	b43legacy_radio_turn_on(dev);
21748c2ecf20Sopenharmony_ci
21758c2ecf20Sopenharmony_ci	b43legacy_write16(dev, 0x03E6, 0x0000);
21768c2ecf20Sopenharmony_ci	err = b43legacy_phy_init(dev);
21778c2ecf20Sopenharmony_ci	if (err)
21788c2ecf20Sopenharmony_ci		goto err_radio_off;
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	/* Select initial Interference Mitigation. */
21818c2ecf20Sopenharmony_ci	tmp = phy->interfmode;
21828c2ecf20Sopenharmony_ci	phy->interfmode = B43legacy_INTERFMODE_NONE;
21838c2ecf20Sopenharmony_ci	b43legacy_radio_set_interference_mitigation(dev, tmp);
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci	b43legacy_phy_set_antenna_diversity(dev);
21868c2ecf20Sopenharmony_ci	b43legacy_mgmtframe_txantenna(dev, B43legacy_ANTENNA_DEFAULT);
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_ci	if (phy->type == B43legacy_PHYTYPE_B) {
21898c2ecf20Sopenharmony_ci		value16 = b43legacy_read16(dev, 0x005E);
21908c2ecf20Sopenharmony_ci		value16 |= 0x0004;
21918c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x005E, value16);
21928c2ecf20Sopenharmony_ci	}
21938c2ecf20Sopenharmony_ci	b43legacy_write32(dev, 0x0100, 0x01000000);
21948c2ecf20Sopenharmony_ci	if (dev->dev->id.revision < 5)
21958c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x010C, 0x01000000);
21968c2ecf20Sopenharmony_ci
21978c2ecf20Sopenharmony_ci	value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
21988c2ecf20Sopenharmony_ci	value32 &= ~B43legacy_MACCTL_INFRA;
21998c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
22008c2ecf20Sopenharmony_ci	value32 = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
22018c2ecf20Sopenharmony_ci	value32 |= B43legacy_MACCTL_INFRA;
22028c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, value32);
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	if (b43legacy_using_pio(dev)) {
22058c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x0210, 0x00000100);
22068c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x0230, 0x00000100);
22078c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x0250, 0x00000100);
22088c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x0270, 0x00000100);
22098c2ecf20Sopenharmony_ci		b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0034,
22108c2ecf20Sopenharmony_ci				      0x0000);
22118c2ecf20Sopenharmony_ci	}
22128c2ecf20Sopenharmony_ci
22138c2ecf20Sopenharmony_ci	/* Probe Response Timeout value */
22148c2ecf20Sopenharmony_ci	/* FIXME: Default to 0, has to be set by ioctl probably... :-/ */
22158c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED, 0x0074, 0x0000);
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	/* Initially set the wireless operation mode. */
22188c2ecf20Sopenharmony_ci	b43legacy_adjust_opmode(dev);
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci	if (dev->dev->id.revision < 3) {
22218c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x060E, 0x0000);
22228c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x0610, 0x8000);
22238c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x0604, 0x0000);
22248c2ecf20Sopenharmony_ci		b43legacy_write16(dev, 0x0606, 0x0200);
22258c2ecf20Sopenharmony_ci	} else {
22268c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x0188, 0x80000000);
22278c2ecf20Sopenharmony_ci		b43legacy_write32(dev, 0x018C, 0x02000000);
22288c2ecf20Sopenharmony_ci	}
22298c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_REASON, 0x00004000);
22308c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA0_IRQ_MASK, 0x0001DC00);
22318c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA1_IRQ_MASK, 0x0000DC00);
22328c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA2_IRQ_MASK, 0x0000DC00);
22338c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA3_IRQ_MASK, 0x0001DC00);
22348c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA4_IRQ_MASK, 0x0000DC00);
22358c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_DMA5_IRQ_MASK, 0x0000DC00);
22368c2ecf20Sopenharmony_ci
22378c2ecf20Sopenharmony_ci	value32 = ssb_read32(dev->dev, SSB_TMSLOW);
22388c2ecf20Sopenharmony_ci	value32 |= B43legacy_TMSLOW_MACPHYCLKEN;
22398c2ecf20Sopenharmony_ci	ssb_write32(dev->dev, SSB_TMSLOW, value32);
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_POWERUP_DELAY,
22428c2ecf20Sopenharmony_ci			  dev->dev->bus->chipco.fast_pwrup_delay);
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_ci	/* PHY TX errors counter. */
22458c2ecf20Sopenharmony_ci	atomic_set(&phy->txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(err != 0);
22488c2ecf20Sopenharmony_ci	b43legacydbg(dev->wl, "Chip initialized\n");
22498c2ecf20Sopenharmony_ciout:
22508c2ecf20Sopenharmony_ci	return err;
22518c2ecf20Sopenharmony_ci
22528c2ecf20Sopenharmony_cierr_radio_off:
22538c2ecf20Sopenharmony_ci	b43legacy_radio_turn_off(dev, 1);
22548c2ecf20Sopenharmony_cierr_gpio_clean:
22558c2ecf20Sopenharmony_ci	b43legacy_gpio_cleanup(dev);
22568c2ecf20Sopenharmony_ci	goto out;
22578c2ecf20Sopenharmony_ci}
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_cistatic void b43legacy_periodic_every120sec(struct b43legacy_wldev *dev)
22608c2ecf20Sopenharmony_ci{
22618c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
22628c2ecf20Sopenharmony_ci
22638c2ecf20Sopenharmony_ci	if (phy->type != B43legacy_PHYTYPE_G || phy->rev < 2)
22648c2ecf20Sopenharmony_ci		return;
22658c2ecf20Sopenharmony_ci
22668c2ecf20Sopenharmony_ci	b43legacy_mac_suspend(dev);
22678c2ecf20Sopenharmony_ci	b43legacy_phy_lo_g_measure(dev);
22688c2ecf20Sopenharmony_ci	b43legacy_mac_enable(dev);
22698c2ecf20Sopenharmony_ci}
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_cistatic void b43legacy_periodic_every60sec(struct b43legacy_wldev *dev)
22728c2ecf20Sopenharmony_ci{
22738c2ecf20Sopenharmony_ci	b43legacy_phy_lo_mark_all_unused(dev);
22748c2ecf20Sopenharmony_ci	if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
22758c2ecf20Sopenharmony_ci		b43legacy_mac_suspend(dev);
22768c2ecf20Sopenharmony_ci		b43legacy_calc_nrssi_slope(dev);
22778c2ecf20Sopenharmony_ci		b43legacy_mac_enable(dev);
22788c2ecf20Sopenharmony_ci	}
22798c2ecf20Sopenharmony_ci}
22808c2ecf20Sopenharmony_ci
22818c2ecf20Sopenharmony_cistatic void b43legacy_periodic_every30sec(struct b43legacy_wldev *dev)
22828c2ecf20Sopenharmony_ci{
22838c2ecf20Sopenharmony_ci	/* Update device statistics. */
22848c2ecf20Sopenharmony_ci	b43legacy_calculate_link_quality(dev);
22858c2ecf20Sopenharmony_ci}
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_cistatic void b43legacy_periodic_every15sec(struct b43legacy_wldev *dev)
22888c2ecf20Sopenharmony_ci{
22898c2ecf20Sopenharmony_ci	b43legacy_phy_xmitpower(dev); /* FIXME: unless scanning? */
22908c2ecf20Sopenharmony_ci
22918c2ecf20Sopenharmony_ci	atomic_set(&dev->phy.txerr_cnt, B43legacy_PHY_TX_BADNESS_LIMIT);
22928c2ecf20Sopenharmony_ci	wmb();
22938c2ecf20Sopenharmony_ci}
22948c2ecf20Sopenharmony_ci
22958c2ecf20Sopenharmony_cistatic void do_periodic_work(struct b43legacy_wldev *dev)
22968c2ecf20Sopenharmony_ci{
22978c2ecf20Sopenharmony_ci	unsigned int state;
22988c2ecf20Sopenharmony_ci
22998c2ecf20Sopenharmony_ci	state = dev->periodic_state;
23008c2ecf20Sopenharmony_ci	if (state % 8 == 0)
23018c2ecf20Sopenharmony_ci		b43legacy_periodic_every120sec(dev);
23028c2ecf20Sopenharmony_ci	if (state % 4 == 0)
23038c2ecf20Sopenharmony_ci		b43legacy_periodic_every60sec(dev);
23048c2ecf20Sopenharmony_ci	if (state % 2 == 0)
23058c2ecf20Sopenharmony_ci		b43legacy_periodic_every30sec(dev);
23068c2ecf20Sopenharmony_ci	b43legacy_periodic_every15sec(dev);
23078c2ecf20Sopenharmony_ci}
23088c2ecf20Sopenharmony_ci
23098c2ecf20Sopenharmony_ci/* Periodic work locking policy:
23108c2ecf20Sopenharmony_ci * 	The whole periodic work handler is protected by
23118c2ecf20Sopenharmony_ci * 	wl->mutex. If another lock is needed somewhere in the
23128c2ecf20Sopenharmony_ci * 	pwork callchain, it's acquired in-place, where it's needed.
23138c2ecf20Sopenharmony_ci */
23148c2ecf20Sopenharmony_cistatic void b43legacy_periodic_work_handler(struct work_struct *work)
23158c2ecf20Sopenharmony_ci{
23168c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = container_of(work, struct b43legacy_wldev,
23178c2ecf20Sopenharmony_ci					     periodic_work.work);
23188c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
23198c2ecf20Sopenharmony_ci	unsigned long delay;
23208c2ecf20Sopenharmony_ci
23218c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
23228c2ecf20Sopenharmony_ci
23238c2ecf20Sopenharmony_ci	if (unlikely(b43legacy_status(dev) != B43legacy_STAT_STARTED))
23248c2ecf20Sopenharmony_ci		goto out;
23258c2ecf20Sopenharmony_ci	if (b43legacy_debug(dev, B43legacy_DBG_PWORK_STOP))
23268c2ecf20Sopenharmony_ci		goto out_requeue;
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci	do_periodic_work(dev);
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	dev->periodic_state++;
23318c2ecf20Sopenharmony_ciout_requeue:
23328c2ecf20Sopenharmony_ci	if (b43legacy_debug(dev, B43legacy_DBG_PWORK_FAST))
23338c2ecf20Sopenharmony_ci		delay = msecs_to_jiffies(50);
23348c2ecf20Sopenharmony_ci	else
23358c2ecf20Sopenharmony_ci		delay = round_jiffies_relative(HZ * 15);
23368c2ecf20Sopenharmony_ci	ieee80211_queue_delayed_work(wl->hw, &dev->periodic_work, delay);
23378c2ecf20Sopenharmony_ciout:
23388c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
23398c2ecf20Sopenharmony_ci}
23408c2ecf20Sopenharmony_ci
23418c2ecf20Sopenharmony_cistatic void b43legacy_periodic_tasks_setup(struct b43legacy_wldev *dev)
23428c2ecf20Sopenharmony_ci{
23438c2ecf20Sopenharmony_ci	struct delayed_work *work = &dev->periodic_work;
23448c2ecf20Sopenharmony_ci
23458c2ecf20Sopenharmony_ci	dev->periodic_state = 0;
23468c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(work, b43legacy_periodic_work_handler);
23478c2ecf20Sopenharmony_ci	ieee80211_queue_delayed_work(dev->wl->hw, work, 0);
23488c2ecf20Sopenharmony_ci}
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci/* Validate access to the chip (SHM) */
23518c2ecf20Sopenharmony_cistatic int b43legacy_validate_chipaccess(struct b43legacy_wldev *dev)
23528c2ecf20Sopenharmony_ci{
23538c2ecf20Sopenharmony_ci	u32 value;
23548c2ecf20Sopenharmony_ci	u32 shm_backup;
23558c2ecf20Sopenharmony_ci
23568c2ecf20Sopenharmony_ci	shm_backup = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0);
23578c2ecf20Sopenharmony_ci	b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0xAA5555AA);
23588c2ecf20Sopenharmony_ci	if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
23598c2ecf20Sopenharmony_ci				 0xAA5555AA)
23608c2ecf20Sopenharmony_ci		goto error;
23618c2ecf20Sopenharmony_ci	b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, 0x55AAAA55);
23628c2ecf20Sopenharmony_ci	if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED, 0) !=
23638c2ecf20Sopenharmony_ci				 0x55AAAA55)
23648c2ecf20Sopenharmony_ci		goto error;
23658c2ecf20Sopenharmony_ci	b43legacy_shm_write32(dev, B43legacy_SHM_SHARED, 0, shm_backup);
23668c2ecf20Sopenharmony_ci
23678c2ecf20Sopenharmony_ci	value = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
23688c2ecf20Sopenharmony_ci	if ((value | B43legacy_MACCTL_GMODE) !=
23698c2ecf20Sopenharmony_ci	    (B43legacy_MACCTL_GMODE | B43legacy_MACCTL_IHR_ENABLED))
23708c2ecf20Sopenharmony_ci		goto error;
23718c2ecf20Sopenharmony_ci
23728c2ecf20Sopenharmony_ci	value = b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_REASON);
23738c2ecf20Sopenharmony_ci	if (value)
23748c2ecf20Sopenharmony_ci		goto error;
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci	return 0;
23778c2ecf20Sopenharmony_cierror:
23788c2ecf20Sopenharmony_ci	b43legacyerr(dev->wl, "Failed to validate the chipaccess\n");
23798c2ecf20Sopenharmony_ci	return -ENODEV;
23808c2ecf20Sopenharmony_ci}
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_cistatic void b43legacy_security_init(struct b43legacy_wldev *dev)
23838c2ecf20Sopenharmony_ci{
23848c2ecf20Sopenharmony_ci	dev->max_nr_keys = (dev->dev->id.revision >= 5) ? 58 : 20;
23858c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(dev->max_nr_keys > ARRAY_SIZE(dev->key));
23868c2ecf20Sopenharmony_ci	dev->ktp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
23878c2ecf20Sopenharmony_ci					0x0056);
23888c2ecf20Sopenharmony_ci	/* KTP is a word address, but we address SHM bytewise.
23898c2ecf20Sopenharmony_ci	 * So multiply by two.
23908c2ecf20Sopenharmony_ci	 */
23918c2ecf20Sopenharmony_ci	dev->ktp *= 2;
23928c2ecf20Sopenharmony_ci	if (dev->dev->id.revision >= 5)
23938c2ecf20Sopenharmony_ci		/* Number of RCMTA address slots */
23948c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_RCMTA_COUNT,
23958c2ecf20Sopenharmony_ci				  dev->max_nr_keys - 8);
23968c2ecf20Sopenharmony_ci}
23978c2ecf20Sopenharmony_ci
23988c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_HWRNG
23998c2ecf20Sopenharmony_cistatic int b43legacy_rng_read(struct hwrng *rng, u32 *data)
24008c2ecf20Sopenharmony_ci{
24018c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = (struct b43legacy_wl *)rng->priv;
24028c2ecf20Sopenharmony_ci	unsigned long flags;
24038c2ecf20Sopenharmony_ci
24048c2ecf20Sopenharmony_ci	/* Don't take wl->mutex here, as it could deadlock with
24058c2ecf20Sopenharmony_ci	 * hwrng internal locking. It's not needed to take
24068c2ecf20Sopenharmony_ci	 * wl->mutex here, anyway. */
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
24098c2ecf20Sopenharmony_ci	*data = b43legacy_read16(wl->current_dev, B43legacy_MMIO_RNG);
24108c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
24118c2ecf20Sopenharmony_ci
24128c2ecf20Sopenharmony_ci	return (sizeof(u16));
24138c2ecf20Sopenharmony_ci}
24148c2ecf20Sopenharmony_ci#endif
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_cistatic void b43legacy_rng_exit(struct b43legacy_wl *wl)
24178c2ecf20Sopenharmony_ci{
24188c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_HWRNG
24198c2ecf20Sopenharmony_ci	if (wl->rng_initialized)
24208c2ecf20Sopenharmony_ci		hwrng_unregister(&wl->rng);
24218c2ecf20Sopenharmony_ci#endif
24228c2ecf20Sopenharmony_ci}
24238c2ecf20Sopenharmony_ci
24248c2ecf20Sopenharmony_cistatic int b43legacy_rng_init(struct b43legacy_wl *wl)
24258c2ecf20Sopenharmony_ci{
24268c2ecf20Sopenharmony_ci	int err = 0;
24278c2ecf20Sopenharmony_ci
24288c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_HWRNG
24298c2ecf20Sopenharmony_ci	snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
24308c2ecf20Sopenharmony_ci		 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
24318c2ecf20Sopenharmony_ci	wl->rng.name = wl->rng_name;
24328c2ecf20Sopenharmony_ci	wl->rng.data_read = b43legacy_rng_read;
24338c2ecf20Sopenharmony_ci	wl->rng.priv = (unsigned long)wl;
24348c2ecf20Sopenharmony_ci	wl->rng_initialized = 1;
24358c2ecf20Sopenharmony_ci	err = hwrng_register(&wl->rng);
24368c2ecf20Sopenharmony_ci	if (err) {
24378c2ecf20Sopenharmony_ci		wl->rng_initialized = 0;
24388c2ecf20Sopenharmony_ci		b43legacyerr(wl, "Failed to register the random "
24398c2ecf20Sopenharmony_ci		       "number generator (%d)\n", err);
24408c2ecf20Sopenharmony_ci	}
24418c2ecf20Sopenharmony_ci
24428c2ecf20Sopenharmony_ci#endif
24438c2ecf20Sopenharmony_ci	return err;
24448c2ecf20Sopenharmony_ci}
24458c2ecf20Sopenharmony_ci
24468c2ecf20Sopenharmony_cistatic void b43legacy_tx_work(struct work_struct *work)
24478c2ecf20Sopenharmony_ci{
24488c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = container_of(work, struct b43legacy_wl,
24498c2ecf20Sopenharmony_ci				  tx_work);
24508c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev;
24518c2ecf20Sopenharmony_ci	struct sk_buff *skb;
24528c2ecf20Sopenharmony_ci	int queue_num;
24538c2ecf20Sopenharmony_ci	int err = 0;
24548c2ecf20Sopenharmony_ci
24558c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
24568c2ecf20Sopenharmony_ci	dev = wl->current_dev;
24578c2ecf20Sopenharmony_ci	if (unlikely(!dev || b43legacy_status(dev) < B43legacy_STAT_STARTED)) {
24588c2ecf20Sopenharmony_ci		mutex_unlock(&wl->mutex);
24598c2ecf20Sopenharmony_ci		return;
24608c2ecf20Sopenharmony_ci	}
24618c2ecf20Sopenharmony_ci
24628c2ecf20Sopenharmony_ci	for (queue_num = 0; queue_num < B43legacy_QOS_QUEUE_NUM; queue_num++) {
24638c2ecf20Sopenharmony_ci		while (skb_queue_len(&wl->tx_queue[queue_num])) {
24648c2ecf20Sopenharmony_ci			skb = skb_dequeue(&wl->tx_queue[queue_num]);
24658c2ecf20Sopenharmony_ci			if (b43legacy_using_pio(dev))
24668c2ecf20Sopenharmony_ci				err = b43legacy_pio_tx(dev, skb);
24678c2ecf20Sopenharmony_ci			else
24688c2ecf20Sopenharmony_ci				err = b43legacy_dma_tx(dev, skb);
24698c2ecf20Sopenharmony_ci			if (err == -ENOSPC) {
24708c2ecf20Sopenharmony_ci				wl->tx_queue_stopped[queue_num] = 1;
24718c2ecf20Sopenharmony_ci				ieee80211_stop_queue(wl->hw, queue_num);
24728c2ecf20Sopenharmony_ci				skb_queue_head(&wl->tx_queue[queue_num], skb);
24738c2ecf20Sopenharmony_ci				break;
24748c2ecf20Sopenharmony_ci			}
24758c2ecf20Sopenharmony_ci			if (unlikely(err))
24768c2ecf20Sopenharmony_ci				dev_kfree_skb(skb); /* Drop it */
24778c2ecf20Sopenharmony_ci			err = 0;
24788c2ecf20Sopenharmony_ci		}
24798c2ecf20Sopenharmony_ci
24808c2ecf20Sopenharmony_ci		if (!err)
24818c2ecf20Sopenharmony_ci			wl->tx_queue_stopped[queue_num] = 0;
24828c2ecf20Sopenharmony_ci	}
24838c2ecf20Sopenharmony_ci
24848c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
24858c2ecf20Sopenharmony_ci}
24868c2ecf20Sopenharmony_ci
24878c2ecf20Sopenharmony_cistatic void b43legacy_op_tx(struct ieee80211_hw *hw,
24888c2ecf20Sopenharmony_ci			    struct ieee80211_tx_control *control,
24898c2ecf20Sopenharmony_ci			    struct sk_buff *skb)
24908c2ecf20Sopenharmony_ci{
24918c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
24928c2ecf20Sopenharmony_ci
24938c2ecf20Sopenharmony_ci	if (unlikely(skb->len < 2 + 2 + 6)) {
24948c2ecf20Sopenharmony_ci		/* Too short, this can't be a valid frame. */
24958c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
24968c2ecf20Sopenharmony_ci		return;
24978c2ecf20Sopenharmony_ci	}
24988c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags);
24998c2ecf20Sopenharmony_ci
25008c2ecf20Sopenharmony_ci	skb_queue_tail(&wl->tx_queue[skb->queue_mapping], skb);
25018c2ecf20Sopenharmony_ci	if (!wl->tx_queue_stopped[skb->queue_mapping])
25028c2ecf20Sopenharmony_ci		ieee80211_queue_work(wl->hw, &wl->tx_work);
25038c2ecf20Sopenharmony_ci	else
25048c2ecf20Sopenharmony_ci		ieee80211_stop_queue(wl->hw, skb->queue_mapping);
25058c2ecf20Sopenharmony_ci}
25068c2ecf20Sopenharmony_ci
25078c2ecf20Sopenharmony_cistatic int b43legacy_op_conf_tx(struct ieee80211_hw *hw,
25088c2ecf20Sopenharmony_ci				struct ieee80211_vif *vif, u16 queue,
25098c2ecf20Sopenharmony_ci				const struct ieee80211_tx_queue_params *params)
25108c2ecf20Sopenharmony_ci{
25118c2ecf20Sopenharmony_ci	return 0;
25128c2ecf20Sopenharmony_ci}
25138c2ecf20Sopenharmony_ci
25148c2ecf20Sopenharmony_cistatic int b43legacy_op_get_stats(struct ieee80211_hw *hw,
25158c2ecf20Sopenharmony_ci				  struct ieee80211_low_level_stats *stats)
25168c2ecf20Sopenharmony_ci{
25178c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
25188c2ecf20Sopenharmony_ci	unsigned long flags;
25198c2ecf20Sopenharmony_ci
25208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
25218c2ecf20Sopenharmony_ci	memcpy(stats, &wl->ieee_stats, sizeof(*stats));
25228c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
25238c2ecf20Sopenharmony_ci
25248c2ecf20Sopenharmony_ci	return 0;
25258c2ecf20Sopenharmony_ci}
25268c2ecf20Sopenharmony_ci
25278c2ecf20Sopenharmony_cistatic const char *phymode_to_string(unsigned int phymode)
25288c2ecf20Sopenharmony_ci{
25298c2ecf20Sopenharmony_ci	switch (phymode) {
25308c2ecf20Sopenharmony_ci	case B43legacy_PHYMODE_B:
25318c2ecf20Sopenharmony_ci		return "B";
25328c2ecf20Sopenharmony_ci	case B43legacy_PHYMODE_G:
25338c2ecf20Sopenharmony_ci		return "G";
25348c2ecf20Sopenharmony_ci	default:
25358c2ecf20Sopenharmony_ci		B43legacy_BUG_ON(1);
25368c2ecf20Sopenharmony_ci	}
25378c2ecf20Sopenharmony_ci	return "";
25388c2ecf20Sopenharmony_ci}
25398c2ecf20Sopenharmony_ci
25408c2ecf20Sopenharmony_cistatic int find_wldev_for_phymode(struct b43legacy_wl *wl,
25418c2ecf20Sopenharmony_ci				  unsigned int phymode,
25428c2ecf20Sopenharmony_ci				  struct b43legacy_wldev **dev,
25438c2ecf20Sopenharmony_ci				  bool *gmode)
25448c2ecf20Sopenharmony_ci{
25458c2ecf20Sopenharmony_ci	struct b43legacy_wldev *d;
25468c2ecf20Sopenharmony_ci
25478c2ecf20Sopenharmony_ci	list_for_each_entry(d, &wl->devlist, list) {
25488c2ecf20Sopenharmony_ci		if (d->phy.possible_phymodes & phymode) {
25498c2ecf20Sopenharmony_ci			/* Ok, this device supports the PHY-mode.
25508c2ecf20Sopenharmony_ci			 * Set the gmode bit. */
25518c2ecf20Sopenharmony_ci			*gmode = true;
25528c2ecf20Sopenharmony_ci			*dev = d;
25538c2ecf20Sopenharmony_ci
25548c2ecf20Sopenharmony_ci			return 0;
25558c2ecf20Sopenharmony_ci		}
25568c2ecf20Sopenharmony_ci	}
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_ci	return -ESRCH;
25598c2ecf20Sopenharmony_ci}
25608c2ecf20Sopenharmony_ci
25618c2ecf20Sopenharmony_cistatic void b43legacy_put_phy_into_reset(struct b43legacy_wldev *dev)
25628c2ecf20Sopenharmony_ci{
25638c2ecf20Sopenharmony_ci	struct ssb_device *sdev = dev->dev;
25648c2ecf20Sopenharmony_ci	u32 tmslow;
25658c2ecf20Sopenharmony_ci
25668c2ecf20Sopenharmony_ci	tmslow = ssb_read32(sdev, SSB_TMSLOW);
25678c2ecf20Sopenharmony_ci	tmslow &= ~B43legacy_TMSLOW_GMODE;
25688c2ecf20Sopenharmony_ci	tmslow |= B43legacy_TMSLOW_PHYRESET;
25698c2ecf20Sopenharmony_ci	tmslow |= SSB_TMSLOW_FGC;
25708c2ecf20Sopenharmony_ci	ssb_write32(sdev, SSB_TMSLOW, tmslow);
25718c2ecf20Sopenharmony_ci	msleep(1);
25728c2ecf20Sopenharmony_ci
25738c2ecf20Sopenharmony_ci	tmslow = ssb_read32(sdev, SSB_TMSLOW);
25748c2ecf20Sopenharmony_ci	tmslow &= ~SSB_TMSLOW_FGC;
25758c2ecf20Sopenharmony_ci	tmslow |= B43legacy_TMSLOW_PHYRESET;
25768c2ecf20Sopenharmony_ci	ssb_write32(sdev, SSB_TMSLOW, tmslow);
25778c2ecf20Sopenharmony_ci	msleep(1);
25788c2ecf20Sopenharmony_ci}
25798c2ecf20Sopenharmony_ci
25808c2ecf20Sopenharmony_ci/* Expects wl->mutex locked */
25818c2ecf20Sopenharmony_cistatic int b43legacy_switch_phymode(struct b43legacy_wl *wl,
25828c2ecf20Sopenharmony_ci				      unsigned int new_mode)
25838c2ecf20Sopenharmony_ci{
25848c2ecf20Sopenharmony_ci	struct b43legacy_wldev *up_dev;
25858c2ecf20Sopenharmony_ci	struct b43legacy_wldev *down_dev;
25868c2ecf20Sopenharmony_ci	int err;
25878c2ecf20Sopenharmony_ci	bool gmode = false;
25888c2ecf20Sopenharmony_ci	int prev_status;
25898c2ecf20Sopenharmony_ci
25908c2ecf20Sopenharmony_ci	err = find_wldev_for_phymode(wl, new_mode, &up_dev, &gmode);
25918c2ecf20Sopenharmony_ci	if (err) {
25928c2ecf20Sopenharmony_ci		b43legacyerr(wl, "Could not find a device for %s-PHY mode\n",
25938c2ecf20Sopenharmony_ci		       phymode_to_string(new_mode));
25948c2ecf20Sopenharmony_ci		return err;
25958c2ecf20Sopenharmony_ci	}
25968c2ecf20Sopenharmony_ci	if ((up_dev == wl->current_dev) &&
25978c2ecf20Sopenharmony_ci	    (!!wl->current_dev->phy.gmode == !!gmode))
25988c2ecf20Sopenharmony_ci		/* This device is already running. */
25998c2ecf20Sopenharmony_ci		return 0;
26008c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Reconfiguring PHYmode to %s-PHY\n",
26018c2ecf20Sopenharmony_ci	       phymode_to_string(new_mode));
26028c2ecf20Sopenharmony_ci	down_dev = wl->current_dev;
26038c2ecf20Sopenharmony_ci
26048c2ecf20Sopenharmony_ci	prev_status = b43legacy_status(down_dev);
26058c2ecf20Sopenharmony_ci	/* Shutdown the currently running core. */
26068c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_STARTED)
26078c2ecf20Sopenharmony_ci		b43legacy_wireless_core_stop(down_dev);
26088c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_INITIALIZED)
26098c2ecf20Sopenharmony_ci		b43legacy_wireless_core_exit(down_dev);
26108c2ecf20Sopenharmony_ci
26118c2ecf20Sopenharmony_ci	if (down_dev != up_dev)
26128c2ecf20Sopenharmony_ci		/* We switch to a different core, so we put PHY into
26138c2ecf20Sopenharmony_ci		 * RESET on the old core. */
26148c2ecf20Sopenharmony_ci		b43legacy_put_phy_into_reset(down_dev);
26158c2ecf20Sopenharmony_ci
26168c2ecf20Sopenharmony_ci	/* Now start the new core. */
26178c2ecf20Sopenharmony_ci	up_dev->phy.gmode = gmode;
26188c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_INITIALIZED) {
26198c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_init(up_dev);
26208c2ecf20Sopenharmony_ci		if (err) {
26218c2ecf20Sopenharmony_ci			b43legacyerr(wl, "Fatal: Could not initialize device"
26228c2ecf20Sopenharmony_ci				     " for newly selected %s-PHY mode\n",
26238c2ecf20Sopenharmony_ci				     phymode_to_string(new_mode));
26248c2ecf20Sopenharmony_ci			goto init_failure;
26258c2ecf20Sopenharmony_ci		}
26268c2ecf20Sopenharmony_ci	}
26278c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_STARTED) {
26288c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_start(up_dev);
26298c2ecf20Sopenharmony_ci		if (err) {
26308c2ecf20Sopenharmony_ci			b43legacyerr(wl, "Fatal: Could not start device for "
26318c2ecf20Sopenharmony_ci			       "newly selected %s-PHY mode\n",
26328c2ecf20Sopenharmony_ci			       phymode_to_string(new_mode));
26338c2ecf20Sopenharmony_ci			b43legacy_wireless_core_exit(up_dev);
26348c2ecf20Sopenharmony_ci			goto init_failure;
26358c2ecf20Sopenharmony_ci		}
26368c2ecf20Sopenharmony_ci	}
26378c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(b43legacy_status(up_dev) != prev_status);
26388c2ecf20Sopenharmony_ci
26398c2ecf20Sopenharmony_ci	b43legacy_shm_write32(up_dev, B43legacy_SHM_SHARED, 0x003E, 0);
26408c2ecf20Sopenharmony_ci
26418c2ecf20Sopenharmony_ci	wl->current_dev = up_dev;
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_ci	return 0;
26448c2ecf20Sopenharmony_ciinit_failure:
26458c2ecf20Sopenharmony_ci	/* Whoops, failed to init the new core. No core is operating now. */
26468c2ecf20Sopenharmony_ci	wl->current_dev = NULL;
26478c2ecf20Sopenharmony_ci	return err;
26488c2ecf20Sopenharmony_ci}
26498c2ecf20Sopenharmony_ci
26508c2ecf20Sopenharmony_ci/* Write the short and long frame retry limit values. */
26518c2ecf20Sopenharmony_cistatic void b43legacy_set_retry_limits(struct b43legacy_wldev *dev,
26528c2ecf20Sopenharmony_ci				       unsigned int short_retry,
26538c2ecf20Sopenharmony_ci				       unsigned int long_retry)
26548c2ecf20Sopenharmony_ci{
26558c2ecf20Sopenharmony_ci	/* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
26568c2ecf20Sopenharmony_ci	 * the chip-internal counter. */
26578c2ecf20Sopenharmony_ci	short_retry = min(short_retry, (unsigned int)0xF);
26588c2ecf20Sopenharmony_ci	long_retry = min(long_retry, (unsigned int)0xF);
26598c2ecf20Sopenharmony_ci
26608c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0006, short_retry);
26618c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS, 0x0007, long_retry);
26628c2ecf20Sopenharmony_ci}
26638c2ecf20Sopenharmony_ci
26648c2ecf20Sopenharmony_cistatic int b43legacy_op_dev_config(struct ieee80211_hw *hw,
26658c2ecf20Sopenharmony_ci				   u32 changed)
26668c2ecf20Sopenharmony_ci{
26678c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
26688c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev;
26698c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy;
26708c2ecf20Sopenharmony_ci	struct ieee80211_conf *conf = &hw->conf;
26718c2ecf20Sopenharmony_ci	unsigned long flags;
26728c2ecf20Sopenharmony_ci	unsigned int new_phymode = 0xFFFF;
26738c2ecf20Sopenharmony_ci	int antenna_tx;
26748c2ecf20Sopenharmony_ci	int err = 0;
26758c2ecf20Sopenharmony_ci
26768c2ecf20Sopenharmony_ci	antenna_tx = B43legacy_ANTENNA_DEFAULT;
26778c2ecf20Sopenharmony_ci
26788c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
26798c2ecf20Sopenharmony_ci	dev = wl->current_dev;
26808c2ecf20Sopenharmony_ci	phy = &dev->phy;
26818c2ecf20Sopenharmony_ci
26828c2ecf20Sopenharmony_ci	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
26838c2ecf20Sopenharmony_ci		b43legacy_set_retry_limits(dev,
26848c2ecf20Sopenharmony_ci					   conf->short_frame_max_tx_count,
26858c2ecf20Sopenharmony_ci					   conf->long_frame_max_tx_count);
26868c2ecf20Sopenharmony_ci	changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
26878c2ecf20Sopenharmony_ci	if (!changed)
26888c2ecf20Sopenharmony_ci		goto out_unlock_mutex;
26898c2ecf20Sopenharmony_ci
26908c2ecf20Sopenharmony_ci	/* Switch the PHY mode (if necessary). */
26918c2ecf20Sopenharmony_ci	switch (conf->chandef.chan->band) {
26928c2ecf20Sopenharmony_ci	case NL80211_BAND_2GHZ:
26938c2ecf20Sopenharmony_ci		if (phy->type == B43legacy_PHYTYPE_B)
26948c2ecf20Sopenharmony_ci			new_phymode = B43legacy_PHYMODE_B;
26958c2ecf20Sopenharmony_ci		else
26968c2ecf20Sopenharmony_ci			new_phymode = B43legacy_PHYMODE_G;
26978c2ecf20Sopenharmony_ci		break;
26988c2ecf20Sopenharmony_ci	default:
26998c2ecf20Sopenharmony_ci		B43legacy_WARN_ON(1);
27008c2ecf20Sopenharmony_ci	}
27018c2ecf20Sopenharmony_ci	err = b43legacy_switch_phymode(wl, new_phymode);
27028c2ecf20Sopenharmony_ci	if (err)
27038c2ecf20Sopenharmony_ci		goto out_unlock_mutex;
27048c2ecf20Sopenharmony_ci
27058c2ecf20Sopenharmony_ci	/* Disable IRQs while reconfiguring the device.
27068c2ecf20Sopenharmony_ci	 * This makes it possible to drop the spinlock throughout
27078c2ecf20Sopenharmony_ci	 * the reconfiguration process. */
27088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
27098c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
27108c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&wl->irq_lock, flags);
27118c2ecf20Sopenharmony_ci		goto out_unlock_mutex;
27128c2ecf20Sopenharmony_ci	}
27138c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
27148c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
27158c2ecf20Sopenharmony_ci	b43legacy_synchronize_irq(dev);
27168c2ecf20Sopenharmony_ci
27178c2ecf20Sopenharmony_ci	/* Switch to the requested channel.
27188c2ecf20Sopenharmony_ci	 * The firmware takes care of races with the TX handler. */
27198c2ecf20Sopenharmony_ci	if (conf->chandef.chan->hw_value != phy->channel)
27208c2ecf20Sopenharmony_ci		b43legacy_radio_selectchannel(dev, conf->chandef.chan->hw_value,
27218c2ecf20Sopenharmony_ci					      0);
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci	dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_MONITOR);
27248c2ecf20Sopenharmony_ci
27258c2ecf20Sopenharmony_ci	/* Adjust the desired TX power level. */
27268c2ecf20Sopenharmony_ci	if (conf->power_level != 0) {
27278c2ecf20Sopenharmony_ci		if (conf->power_level != phy->power_level) {
27288c2ecf20Sopenharmony_ci			phy->power_level = conf->power_level;
27298c2ecf20Sopenharmony_ci			b43legacy_phy_xmitpower(dev);
27308c2ecf20Sopenharmony_ci		}
27318c2ecf20Sopenharmony_ci	}
27328c2ecf20Sopenharmony_ci
27338c2ecf20Sopenharmony_ci	/* Antennas for RX and management frame TX. */
27348c2ecf20Sopenharmony_ci	b43legacy_mgmtframe_txantenna(dev, antenna_tx);
27358c2ecf20Sopenharmony_ci
27368c2ecf20Sopenharmony_ci	if (wl->radio_enabled != phy->radio_on) {
27378c2ecf20Sopenharmony_ci		if (wl->radio_enabled) {
27388c2ecf20Sopenharmony_ci			b43legacy_radio_turn_on(dev);
27398c2ecf20Sopenharmony_ci			b43legacyinfo(dev->wl, "Radio turned on by software\n");
27408c2ecf20Sopenharmony_ci			if (!dev->radio_hw_enable)
27418c2ecf20Sopenharmony_ci				b43legacyinfo(dev->wl, "The hardware RF-kill"
27428c2ecf20Sopenharmony_ci					      " button still turns the radio"
27438c2ecf20Sopenharmony_ci					      " physically off. Press the"
27448c2ecf20Sopenharmony_ci					      " button to turn it on.\n");
27458c2ecf20Sopenharmony_ci		} else {
27468c2ecf20Sopenharmony_ci			b43legacy_radio_turn_off(dev, 0);
27478c2ecf20Sopenharmony_ci			b43legacyinfo(dev->wl, "Radio turned off by"
27488c2ecf20Sopenharmony_ci				      " software\n");
27498c2ecf20Sopenharmony_ci		}
27508c2ecf20Sopenharmony_ci	}
27518c2ecf20Sopenharmony_ci
27528c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
27538c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
27548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
27558c2ecf20Sopenharmony_ciout_unlock_mutex:
27568c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
27578c2ecf20Sopenharmony_ci
27588c2ecf20Sopenharmony_ci	return err;
27598c2ecf20Sopenharmony_ci}
27608c2ecf20Sopenharmony_ci
27618c2ecf20Sopenharmony_cistatic void b43legacy_update_basic_rates(struct b43legacy_wldev *dev, u32 brates)
27628c2ecf20Sopenharmony_ci{
27638c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband =
27648c2ecf20Sopenharmony_ci		dev->wl->hw->wiphy->bands[NL80211_BAND_2GHZ];
27658c2ecf20Sopenharmony_ci	struct ieee80211_rate *rate;
27668c2ecf20Sopenharmony_ci	int i;
27678c2ecf20Sopenharmony_ci	u16 basic, direct, offset, basic_offset, rateptr;
27688c2ecf20Sopenharmony_ci
27698c2ecf20Sopenharmony_ci	for (i = 0; i < sband->n_bitrates; i++) {
27708c2ecf20Sopenharmony_ci		rate = &sband->bitrates[i];
27718c2ecf20Sopenharmony_ci
27728c2ecf20Sopenharmony_ci		if (b43legacy_is_cck_rate(rate->hw_value)) {
27738c2ecf20Sopenharmony_ci			direct = B43legacy_SHM_SH_CCKDIRECT;
27748c2ecf20Sopenharmony_ci			basic = B43legacy_SHM_SH_CCKBASIC;
27758c2ecf20Sopenharmony_ci			offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
27768c2ecf20Sopenharmony_ci			offset &= 0xF;
27778c2ecf20Sopenharmony_ci		} else {
27788c2ecf20Sopenharmony_ci			direct = B43legacy_SHM_SH_OFDMDIRECT;
27798c2ecf20Sopenharmony_ci			basic = B43legacy_SHM_SH_OFDMBASIC;
27808c2ecf20Sopenharmony_ci			offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
27818c2ecf20Sopenharmony_ci			offset &= 0xF;
27828c2ecf20Sopenharmony_ci		}
27838c2ecf20Sopenharmony_ci
27848c2ecf20Sopenharmony_ci		rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
27858c2ecf20Sopenharmony_ci
27868c2ecf20Sopenharmony_ci		if (b43legacy_is_cck_rate(rate->hw_value)) {
27878c2ecf20Sopenharmony_ci			basic_offset = b43legacy_plcp_get_ratecode_cck(rate->hw_value);
27888c2ecf20Sopenharmony_ci			basic_offset &= 0xF;
27898c2ecf20Sopenharmony_ci		} else {
27908c2ecf20Sopenharmony_ci			basic_offset = b43legacy_plcp_get_ratecode_ofdm(rate->hw_value);
27918c2ecf20Sopenharmony_ci			basic_offset &= 0xF;
27928c2ecf20Sopenharmony_ci		}
27938c2ecf20Sopenharmony_ci
27948c2ecf20Sopenharmony_ci		/*
27958c2ecf20Sopenharmony_ci		 * Get the pointer that we need to point to
27968c2ecf20Sopenharmony_ci		 * from the direct map
27978c2ecf20Sopenharmony_ci		 */
27988c2ecf20Sopenharmony_ci		rateptr = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
27998c2ecf20Sopenharmony_ci					       direct + 2 * basic_offset);
28008c2ecf20Sopenharmony_ci		/* and write it to the basic map */
28018c2ecf20Sopenharmony_ci		b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
28028c2ecf20Sopenharmony_ci				      basic + 2 * offset, rateptr);
28038c2ecf20Sopenharmony_ci	}
28048c2ecf20Sopenharmony_ci}
28058c2ecf20Sopenharmony_ci
28068c2ecf20Sopenharmony_cistatic void b43legacy_op_bss_info_changed(struct ieee80211_hw *hw,
28078c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif,
28088c2ecf20Sopenharmony_ci				    struct ieee80211_bss_conf *conf,
28098c2ecf20Sopenharmony_ci				    u32 changed)
28108c2ecf20Sopenharmony_ci{
28118c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
28128c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev;
28138c2ecf20Sopenharmony_ci	unsigned long flags;
28148c2ecf20Sopenharmony_ci
28158c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
28168c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(wl->vif != vif);
28178c2ecf20Sopenharmony_ci
28188c2ecf20Sopenharmony_ci	dev = wl->current_dev;
28198c2ecf20Sopenharmony_ci
28208c2ecf20Sopenharmony_ci	/* Disable IRQs while reconfiguring the device.
28218c2ecf20Sopenharmony_ci	 * This makes it possible to drop the spinlock throughout
28228c2ecf20Sopenharmony_ci	 * the reconfiguration process. */
28238c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
28248c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
28258c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&wl->irq_lock, flags);
28268c2ecf20Sopenharmony_ci		goto out_unlock_mutex;
28278c2ecf20Sopenharmony_ci	}
28288c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
28298c2ecf20Sopenharmony_ci
28308c2ecf20Sopenharmony_ci	if (changed & BSS_CHANGED_BSSID) {
28318c2ecf20Sopenharmony_ci		b43legacy_synchronize_irq(dev);
28328c2ecf20Sopenharmony_ci
28338c2ecf20Sopenharmony_ci		if (conf->bssid)
28348c2ecf20Sopenharmony_ci			memcpy(wl->bssid, conf->bssid, ETH_ALEN);
28358c2ecf20Sopenharmony_ci		else
28368c2ecf20Sopenharmony_ci			eth_zero_addr(wl->bssid);
28378c2ecf20Sopenharmony_ci	}
28388c2ecf20Sopenharmony_ci
28398c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED) {
28408c2ecf20Sopenharmony_ci		if (changed & BSS_CHANGED_BEACON &&
28418c2ecf20Sopenharmony_ci		    (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
28428c2ecf20Sopenharmony_ci		     b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
28438c2ecf20Sopenharmony_ci			b43legacy_update_templates(wl);
28448c2ecf20Sopenharmony_ci
28458c2ecf20Sopenharmony_ci		if (changed & BSS_CHANGED_BSSID)
28468c2ecf20Sopenharmony_ci			b43legacy_write_mac_bssid_templates(dev);
28478c2ecf20Sopenharmony_ci	}
28488c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
28498c2ecf20Sopenharmony_ci
28508c2ecf20Sopenharmony_ci	b43legacy_mac_suspend(dev);
28518c2ecf20Sopenharmony_ci
28528c2ecf20Sopenharmony_ci	if (changed & BSS_CHANGED_BEACON_INT &&
28538c2ecf20Sopenharmony_ci	    (b43legacy_is_mode(wl, NL80211_IFTYPE_AP) ||
28548c2ecf20Sopenharmony_ci	     b43legacy_is_mode(wl, NL80211_IFTYPE_ADHOC)))
28558c2ecf20Sopenharmony_ci		b43legacy_set_beacon_int(dev, conf->beacon_int);
28568c2ecf20Sopenharmony_ci
28578c2ecf20Sopenharmony_ci	if (changed & BSS_CHANGED_BASIC_RATES)
28588c2ecf20Sopenharmony_ci		b43legacy_update_basic_rates(dev, conf->basic_rates);
28598c2ecf20Sopenharmony_ci
28608c2ecf20Sopenharmony_ci	if (changed & BSS_CHANGED_ERP_SLOT) {
28618c2ecf20Sopenharmony_ci		if (conf->use_short_slot)
28628c2ecf20Sopenharmony_ci			b43legacy_short_slot_timing_enable(dev);
28638c2ecf20Sopenharmony_ci		else
28648c2ecf20Sopenharmony_ci			b43legacy_short_slot_timing_disable(dev);
28658c2ecf20Sopenharmony_ci	}
28668c2ecf20Sopenharmony_ci
28678c2ecf20Sopenharmony_ci	b43legacy_mac_enable(dev);
28688c2ecf20Sopenharmony_ci
28698c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
28708c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
28718c2ecf20Sopenharmony_ci	/* XXX: why? */
28728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
28738c2ecf20Sopenharmony_ci out_unlock_mutex:
28748c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
28758c2ecf20Sopenharmony_ci}
28768c2ecf20Sopenharmony_ci
28778c2ecf20Sopenharmony_cistatic void b43legacy_op_configure_filter(struct ieee80211_hw *hw,
28788c2ecf20Sopenharmony_ci					  unsigned int changed,
28798c2ecf20Sopenharmony_ci					  unsigned int *fflags,u64 multicast)
28808c2ecf20Sopenharmony_ci{
28818c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
28828c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = wl->current_dev;
28838c2ecf20Sopenharmony_ci	unsigned long flags;
28848c2ecf20Sopenharmony_ci
28858c2ecf20Sopenharmony_ci	if (!dev) {
28868c2ecf20Sopenharmony_ci		*fflags = 0;
28878c2ecf20Sopenharmony_ci		return;
28888c2ecf20Sopenharmony_ci	}
28898c2ecf20Sopenharmony_ci
28908c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
28918c2ecf20Sopenharmony_ci	*fflags &= FIF_ALLMULTI |
28928c2ecf20Sopenharmony_ci		  FIF_FCSFAIL |
28938c2ecf20Sopenharmony_ci		  FIF_PLCPFAIL |
28948c2ecf20Sopenharmony_ci		  FIF_CONTROL |
28958c2ecf20Sopenharmony_ci		  FIF_OTHER_BSS |
28968c2ecf20Sopenharmony_ci		  FIF_BCN_PRBRESP_PROMISC;
28978c2ecf20Sopenharmony_ci
28988c2ecf20Sopenharmony_ci	changed &= FIF_ALLMULTI |
28998c2ecf20Sopenharmony_ci		   FIF_FCSFAIL |
29008c2ecf20Sopenharmony_ci		   FIF_PLCPFAIL |
29018c2ecf20Sopenharmony_ci		   FIF_CONTROL |
29028c2ecf20Sopenharmony_ci		   FIF_OTHER_BSS |
29038c2ecf20Sopenharmony_ci		   FIF_BCN_PRBRESP_PROMISC;
29048c2ecf20Sopenharmony_ci
29058c2ecf20Sopenharmony_ci	wl->filter_flags = *fflags;
29068c2ecf20Sopenharmony_ci
29078c2ecf20Sopenharmony_ci	if (changed && b43legacy_status(dev) >= B43legacy_STAT_INITIALIZED)
29088c2ecf20Sopenharmony_ci		b43legacy_adjust_opmode(dev);
29098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
29108c2ecf20Sopenharmony_ci}
29118c2ecf20Sopenharmony_ci
29128c2ecf20Sopenharmony_ci/* Locking: wl->mutex */
29138c2ecf20Sopenharmony_cistatic void b43legacy_wireless_core_stop(struct b43legacy_wldev *dev)
29148c2ecf20Sopenharmony_ci{
29158c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
29168c2ecf20Sopenharmony_ci	unsigned long flags;
29178c2ecf20Sopenharmony_ci	int queue_num;
29188c2ecf20Sopenharmony_ci
29198c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) < B43legacy_STAT_STARTED)
29208c2ecf20Sopenharmony_ci		return;
29218c2ecf20Sopenharmony_ci
29228c2ecf20Sopenharmony_ci	/* Disable and sync interrupts. We must do this before than
29238c2ecf20Sopenharmony_ci	 * setting the status to INITIALIZED, as the interrupt handler
29248c2ecf20Sopenharmony_ci	 * won't care about IRQs then. */
29258c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
29268c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, 0);
29278c2ecf20Sopenharmony_ci	b43legacy_read32(dev, B43legacy_MMIO_GEN_IRQ_MASK); /* flush */
29288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
29298c2ecf20Sopenharmony_ci	b43legacy_synchronize_irq(dev);
29308c2ecf20Sopenharmony_ci
29318c2ecf20Sopenharmony_ci	b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
29328c2ecf20Sopenharmony_ci
29338c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
29348c2ecf20Sopenharmony_ci	/* Must unlock as it would otherwise deadlock. No races here.
29358c2ecf20Sopenharmony_ci	 * Cancel the possibly running self-rearming periodic work. */
29368c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&dev->periodic_work);
29378c2ecf20Sopenharmony_ci	cancel_work_sync(&wl->tx_work);
29388c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
29398c2ecf20Sopenharmony_ci
29408c2ecf20Sopenharmony_ci	/* Drain all TX queues. */
29418c2ecf20Sopenharmony_ci	for (queue_num = 0; queue_num < B43legacy_QOS_QUEUE_NUM; queue_num++) {
29428c2ecf20Sopenharmony_ci		while (skb_queue_len(&wl->tx_queue[queue_num]))
29438c2ecf20Sopenharmony_ci			dev_kfree_skb(skb_dequeue(&wl->tx_queue[queue_num]));
29448c2ecf20Sopenharmony_ci	}
29458c2ecf20Sopenharmony_ci
29468c2ecf20Sopenharmony_cib43legacy_mac_suspend(dev);
29478c2ecf20Sopenharmony_ci	free_irq(dev->dev->irq, dev);
29488c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Wireless interface stopped\n");
29498c2ecf20Sopenharmony_ci}
29508c2ecf20Sopenharmony_ci
29518c2ecf20Sopenharmony_ci/* Locking: wl->mutex */
29528c2ecf20Sopenharmony_cistatic int b43legacy_wireless_core_start(struct b43legacy_wldev *dev)
29538c2ecf20Sopenharmony_ci{
29548c2ecf20Sopenharmony_ci	int err;
29558c2ecf20Sopenharmony_ci
29568c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_INITIALIZED);
29578c2ecf20Sopenharmony_ci
29588c2ecf20Sopenharmony_ci	drain_txstatus_queue(dev);
29598c2ecf20Sopenharmony_ci	err = request_irq(dev->dev->irq, b43legacy_interrupt_handler,
29608c2ecf20Sopenharmony_ci			  IRQF_SHARED, KBUILD_MODNAME, dev);
29618c2ecf20Sopenharmony_ci	if (err) {
29628c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "Cannot request IRQ-%d\n",
29638c2ecf20Sopenharmony_ci		       dev->dev->irq);
29648c2ecf20Sopenharmony_ci		goto out;
29658c2ecf20Sopenharmony_ci	}
29668c2ecf20Sopenharmony_ci	/* We are ready to run. */
29678c2ecf20Sopenharmony_ci	ieee80211_wake_queues(dev->wl->hw);
29688c2ecf20Sopenharmony_ci	b43legacy_set_status(dev, B43legacy_STAT_STARTED);
29698c2ecf20Sopenharmony_ci
29708c2ecf20Sopenharmony_ci	/* Start data flow (TX/RX) */
29718c2ecf20Sopenharmony_ci	b43legacy_mac_enable(dev);
29728c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_GEN_IRQ_MASK, dev->irq_mask);
29738c2ecf20Sopenharmony_ci
29748c2ecf20Sopenharmony_ci	/* Start maintenance work */
29758c2ecf20Sopenharmony_ci	b43legacy_periodic_tasks_setup(dev);
29768c2ecf20Sopenharmony_ci
29778c2ecf20Sopenharmony_ci	b43legacydbg(dev->wl, "Wireless interface started\n");
29788c2ecf20Sopenharmony_ciout:
29798c2ecf20Sopenharmony_ci	return err;
29808c2ecf20Sopenharmony_ci}
29818c2ecf20Sopenharmony_ci
29828c2ecf20Sopenharmony_ci/* Get PHY and RADIO versioning numbers */
29838c2ecf20Sopenharmony_cistatic int b43legacy_phy_versioning(struct b43legacy_wldev *dev)
29848c2ecf20Sopenharmony_ci{
29858c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
29868c2ecf20Sopenharmony_ci	u32 tmp;
29878c2ecf20Sopenharmony_ci	u8 analog_type;
29888c2ecf20Sopenharmony_ci	u8 phy_type;
29898c2ecf20Sopenharmony_ci	u8 phy_rev;
29908c2ecf20Sopenharmony_ci	u16 radio_manuf;
29918c2ecf20Sopenharmony_ci	u16 radio_ver;
29928c2ecf20Sopenharmony_ci	u16 radio_rev;
29938c2ecf20Sopenharmony_ci	int unsupported = 0;
29948c2ecf20Sopenharmony_ci
29958c2ecf20Sopenharmony_ci	/* Get PHY versioning */
29968c2ecf20Sopenharmony_ci	tmp = b43legacy_read16(dev, B43legacy_MMIO_PHY_VER);
29978c2ecf20Sopenharmony_ci	analog_type = (tmp & B43legacy_PHYVER_ANALOG)
29988c2ecf20Sopenharmony_ci		      >> B43legacy_PHYVER_ANALOG_SHIFT;
29998c2ecf20Sopenharmony_ci	phy_type = (tmp & B43legacy_PHYVER_TYPE) >> B43legacy_PHYVER_TYPE_SHIFT;
30008c2ecf20Sopenharmony_ci	phy_rev = (tmp & B43legacy_PHYVER_VERSION);
30018c2ecf20Sopenharmony_ci	switch (phy_type) {
30028c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_B:
30038c2ecf20Sopenharmony_ci		if (phy_rev != 2 && phy_rev != 4
30048c2ecf20Sopenharmony_ci		    && phy_rev != 6 && phy_rev != 7)
30058c2ecf20Sopenharmony_ci			unsupported = 1;
30068c2ecf20Sopenharmony_ci		break;
30078c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_G:
30088c2ecf20Sopenharmony_ci		if (phy_rev > 8)
30098c2ecf20Sopenharmony_ci			unsupported = 1;
30108c2ecf20Sopenharmony_ci		break;
30118c2ecf20Sopenharmony_ci	default:
30128c2ecf20Sopenharmony_ci		unsupported = 1;
30138c2ecf20Sopenharmony_ci	}
30148c2ecf20Sopenharmony_ci	if (unsupported) {
30158c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "FOUND UNSUPPORTED PHY "
30168c2ecf20Sopenharmony_ci		       "(Analog %u, Type %u, Revision %u)\n",
30178c2ecf20Sopenharmony_ci		       analog_type, phy_type, phy_rev);
30188c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
30198c2ecf20Sopenharmony_ci	}
30208c2ecf20Sopenharmony_ci	b43legacydbg(dev->wl, "Found PHY: Analog %u, Type %u, Revision %u\n",
30218c2ecf20Sopenharmony_ci	       analog_type, phy_type, phy_rev);
30228c2ecf20Sopenharmony_ci
30238c2ecf20Sopenharmony_ci
30248c2ecf20Sopenharmony_ci	/* Get RADIO versioning */
30258c2ecf20Sopenharmony_ci	if (dev->dev->bus->chip_id == 0x4317) {
30268c2ecf20Sopenharmony_ci		if (dev->dev->bus->chip_rev == 0)
30278c2ecf20Sopenharmony_ci			tmp = 0x3205017F;
30288c2ecf20Sopenharmony_ci		else if (dev->dev->bus->chip_rev == 1)
30298c2ecf20Sopenharmony_ci			tmp = 0x4205017F;
30308c2ecf20Sopenharmony_ci		else
30318c2ecf20Sopenharmony_ci			tmp = 0x5205017F;
30328c2ecf20Sopenharmony_ci	} else {
30338c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
30348c2ecf20Sopenharmony_ci				  B43legacy_RADIOCTL_ID);
30358c2ecf20Sopenharmony_ci		tmp = b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_HIGH);
30368c2ecf20Sopenharmony_ci		tmp <<= 16;
30378c2ecf20Sopenharmony_ci		b43legacy_write16(dev, B43legacy_MMIO_RADIO_CONTROL,
30388c2ecf20Sopenharmony_ci				  B43legacy_RADIOCTL_ID);
30398c2ecf20Sopenharmony_ci		tmp |= b43legacy_read16(dev, B43legacy_MMIO_RADIO_DATA_LOW);
30408c2ecf20Sopenharmony_ci	}
30418c2ecf20Sopenharmony_ci	radio_manuf = (tmp & 0x00000FFF);
30428c2ecf20Sopenharmony_ci	radio_ver = (tmp & 0x0FFFF000) >> 12;
30438c2ecf20Sopenharmony_ci	radio_rev = (tmp & 0xF0000000) >> 28;
30448c2ecf20Sopenharmony_ci	switch (phy_type) {
30458c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_B:
30468c2ecf20Sopenharmony_ci		if ((radio_ver & 0xFFF0) != 0x2050)
30478c2ecf20Sopenharmony_ci			unsupported = 1;
30488c2ecf20Sopenharmony_ci		break;
30498c2ecf20Sopenharmony_ci	case B43legacy_PHYTYPE_G:
30508c2ecf20Sopenharmony_ci		if (radio_ver != 0x2050)
30518c2ecf20Sopenharmony_ci			unsupported = 1;
30528c2ecf20Sopenharmony_ci		break;
30538c2ecf20Sopenharmony_ci	default:
30548c2ecf20Sopenharmony_ci		B43legacy_BUG_ON(1);
30558c2ecf20Sopenharmony_ci	}
30568c2ecf20Sopenharmony_ci	if (unsupported) {
30578c2ecf20Sopenharmony_ci		b43legacyerr(dev->wl, "FOUND UNSUPPORTED RADIO "
30588c2ecf20Sopenharmony_ci		       "(Manuf 0x%X, Version 0x%X, Revision %u)\n",
30598c2ecf20Sopenharmony_ci		       radio_manuf, radio_ver, radio_rev);
30608c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
30618c2ecf20Sopenharmony_ci	}
30628c2ecf20Sopenharmony_ci	b43legacydbg(dev->wl, "Found Radio: Manuf 0x%X, Version 0x%X,"
30638c2ecf20Sopenharmony_ci		     " Revision %u\n", radio_manuf, radio_ver, radio_rev);
30648c2ecf20Sopenharmony_ci
30658c2ecf20Sopenharmony_ci
30668c2ecf20Sopenharmony_ci	phy->radio_manuf = radio_manuf;
30678c2ecf20Sopenharmony_ci	phy->radio_ver = radio_ver;
30688c2ecf20Sopenharmony_ci	phy->radio_rev = radio_rev;
30698c2ecf20Sopenharmony_ci
30708c2ecf20Sopenharmony_ci	phy->analog = analog_type;
30718c2ecf20Sopenharmony_ci	phy->type = phy_type;
30728c2ecf20Sopenharmony_ci	phy->rev = phy_rev;
30738c2ecf20Sopenharmony_ci
30748c2ecf20Sopenharmony_ci	return 0;
30758c2ecf20Sopenharmony_ci}
30768c2ecf20Sopenharmony_ci
30778c2ecf20Sopenharmony_cistatic void setup_struct_phy_for_init(struct b43legacy_wldev *dev,
30788c2ecf20Sopenharmony_ci				      struct b43legacy_phy *phy)
30798c2ecf20Sopenharmony_ci{
30808c2ecf20Sopenharmony_ci	struct b43legacy_lopair *lo;
30818c2ecf20Sopenharmony_ci	int i;
30828c2ecf20Sopenharmony_ci
30838c2ecf20Sopenharmony_ci	memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
30848c2ecf20Sopenharmony_ci	memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
30858c2ecf20Sopenharmony_ci
30868c2ecf20Sopenharmony_ci	/* Assume the radio is enabled. If it's not enabled, the state will
30878c2ecf20Sopenharmony_ci	 * immediately get fixed on the first periodic work run. */
30888c2ecf20Sopenharmony_ci	dev->radio_hw_enable = true;
30898c2ecf20Sopenharmony_ci
30908c2ecf20Sopenharmony_ci	phy->savedpctlreg = 0xFFFF;
30918c2ecf20Sopenharmony_ci	phy->aci_enable = false;
30928c2ecf20Sopenharmony_ci	phy->aci_wlan_automatic = false;
30938c2ecf20Sopenharmony_ci	phy->aci_hw_rssi = false;
30948c2ecf20Sopenharmony_ci
30958c2ecf20Sopenharmony_ci	lo = phy->_lo_pairs;
30968c2ecf20Sopenharmony_ci	if (lo)
30978c2ecf20Sopenharmony_ci		memset(lo, 0, sizeof(struct b43legacy_lopair) *
30988c2ecf20Sopenharmony_ci				     B43legacy_LO_COUNT);
30998c2ecf20Sopenharmony_ci	phy->max_lb_gain = 0;
31008c2ecf20Sopenharmony_ci	phy->trsw_rx_gain = 0;
31018c2ecf20Sopenharmony_ci
31028c2ecf20Sopenharmony_ci	/* Set default attenuation values. */
31038c2ecf20Sopenharmony_ci	phy->bbatt = b43legacy_default_baseband_attenuation(dev);
31048c2ecf20Sopenharmony_ci	phy->rfatt = b43legacy_default_radio_attenuation(dev);
31058c2ecf20Sopenharmony_ci	phy->txctl1 = b43legacy_default_txctl1(dev);
31068c2ecf20Sopenharmony_ci	phy->txpwr_offset = 0;
31078c2ecf20Sopenharmony_ci
31088c2ecf20Sopenharmony_ci	/* NRSSI */
31098c2ecf20Sopenharmony_ci	phy->nrssislope = 0;
31108c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
31118c2ecf20Sopenharmony_ci		phy->nrssi[i] = -1000;
31128c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
31138c2ecf20Sopenharmony_ci		phy->nrssi_lt[i] = i;
31148c2ecf20Sopenharmony_ci
31158c2ecf20Sopenharmony_ci	phy->lofcal = 0xFFFF;
31168c2ecf20Sopenharmony_ci	phy->initval = 0xFFFF;
31178c2ecf20Sopenharmony_ci
31188c2ecf20Sopenharmony_ci	phy->interfmode = B43legacy_INTERFMODE_NONE;
31198c2ecf20Sopenharmony_ci	phy->channel = 0xFF;
31208c2ecf20Sopenharmony_ci}
31218c2ecf20Sopenharmony_ci
31228c2ecf20Sopenharmony_cistatic void setup_struct_wldev_for_init(struct b43legacy_wldev *dev)
31238c2ecf20Sopenharmony_ci{
31248c2ecf20Sopenharmony_ci	/* Flags */
31258c2ecf20Sopenharmony_ci	dev->dfq_valid = false;
31268c2ecf20Sopenharmony_ci
31278c2ecf20Sopenharmony_ci	/* Stats */
31288c2ecf20Sopenharmony_ci	memset(&dev->stats, 0, sizeof(dev->stats));
31298c2ecf20Sopenharmony_ci
31308c2ecf20Sopenharmony_ci	setup_struct_phy_for_init(dev, &dev->phy);
31318c2ecf20Sopenharmony_ci
31328c2ecf20Sopenharmony_ci	/* IRQ related flags */
31338c2ecf20Sopenharmony_ci	dev->irq_reason = 0;
31348c2ecf20Sopenharmony_ci	memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
31358c2ecf20Sopenharmony_ci	dev->irq_mask = B43legacy_IRQ_MASKTEMPLATE;
31368c2ecf20Sopenharmony_ci
31378c2ecf20Sopenharmony_ci	dev->mac_suspended = 1;
31388c2ecf20Sopenharmony_ci
31398c2ecf20Sopenharmony_ci	/* Noise calculation context */
31408c2ecf20Sopenharmony_ci	memset(&dev->noisecalc, 0, sizeof(dev->noisecalc));
31418c2ecf20Sopenharmony_ci}
31428c2ecf20Sopenharmony_ci
31438c2ecf20Sopenharmony_cistatic void b43legacy_set_synth_pu_delay(struct b43legacy_wldev *dev,
31448c2ecf20Sopenharmony_ci					  bool idle) {
31458c2ecf20Sopenharmony_ci	u16 pu_delay = 1050;
31468c2ecf20Sopenharmony_ci
31478c2ecf20Sopenharmony_ci	if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
31488c2ecf20Sopenharmony_ci		pu_delay = 500;
31498c2ecf20Sopenharmony_ci	if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
31508c2ecf20Sopenharmony_ci		pu_delay = max(pu_delay, (u16)2400);
31518c2ecf20Sopenharmony_ci
31528c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
31538c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_SPUWKUP, pu_delay);
31548c2ecf20Sopenharmony_ci}
31558c2ecf20Sopenharmony_ci
31568c2ecf20Sopenharmony_ci/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
31578c2ecf20Sopenharmony_cistatic void b43legacy_set_pretbtt(struct b43legacy_wldev *dev)
31588c2ecf20Sopenharmony_ci{
31598c2ecf20Sopenharmony_ci	u16 pretbtt;
31608c2ecf20Sopenharmony_ci
31618c2ecf20Sopenharmony_ci	/* The time value is in microseconds. */
31628c2ecf20Sopenharmony_ci	if (b43legacy_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
31638c2ecf20Sopenharmony_ci		pretbtt = 2;
31648c2ecf20Sopenharmony_ci	else
31658c2ecf20Sopenharmony_ci		pretbtt = 250;
31668c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
31678c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_PRETBTT, pretbtt);
31688c2ecf20Sopenharmony_ci	b43legacy_write16(dev, B43legacy_MMIO_TSF_CFP_PRETBTT, pretbtt);
31698c2ecf20Sopenharmony_ci}
31708c2ecf20Sopenharmony_ci
31718c2ecf20Sopenharmony_ci/* Shutdown a wireless core */
31728c2ecf20Sopenharmony_ci/* Locking: wl->mutex */
31738c2ecf20Sopenharmony_cistatic void b43legacy_wireless_core_exit(struct b43legacy_wldev *dev)
31748c2ecf20Sopenharmony_ci{
31758c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
31768c2ecf20Sopenharmony_ci	u32 macctl;
31778c2ecf20Sopenharmony_ci
31788c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(b43legacy_status(dev) > B43legacy_STAT_INITIALIZED);
31798c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) != B43legacy_STAT_INITIALIZED)
31808c2ecf20Sopenharmony_ci		return;
31818c2ecf20Sopenharmony_ci	b43legacy_set_status(dev, B43legacy_STAT_UNINIT);
31828c2ecf20Sopenharmony_ci
31838c2ecf20Sopenharmony_ci	/* Stop the microcode PSM. */
31848c2ecf20Sopenharmony_ci	macctl = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
31858c2ecf20Sopenharmony_ci	macctl &= ~B43legacy_MACCTL_PSM_RUN;
31868c2ecf20Sopenharmony_ci	macctl |= B43legacy_MACCTL_PSM_JMP0;
31878c2ecf20Sopenharmony_ci	b43legacy_write32(dev, B43legacy_MMIO_MACCTL, macctl);
31888c2ecf20Sopenharmony_ci
31898c2ecf20Sopenharmony_ci	b43legacy_leds_exit(dev);
31908c2ecf20Sopenharmony_ci	b43legacy_rng_exit(dev->wl);
31918c2ecf20Sopenharmony_ci	b43legacy_pio_free(dev);
31928c2ecf20Sopenharmony_ci	b43legacy_dma_free(dev);
31938c2ecf20Sopenharmony_ci	b43legacy_chip_exit(dev);
31948c2ecf20Sopenharmony_ci	b43legacy_radio_turn_off(dev, 1);
31958c2ecf20Sopenharmony_ci	b43legacy_switch_analog(dev, 0);
31968c2ecf20Sopenharmony_ci	if (phy->dyn_tssi_tbl)
31978c2ecf20Sopenharmony_ci		kfree(phy->tssi2dbm);
31988c2ecf20Sopenharmony_ci	kfree(phy->lo_control);
31998c2ecf20Sopenharmony_ci	phy->lo_control = NULL;
32008c2ecf20Sopenharmony_ci	if (dev->wl->current_beacon) {
32018c2ecf20Sopenharmony_ci		dev_kfree_skb_any(dev->wl->current_beacon);
32028c2ecf20Sopenharmony_ci		dev->wl->current_beacon = NULL;
32038c2ecf20Sopenharmony_ci	}
32048c2ecf20Sopenharmony_ci
32058c2ecf20Sopenharmony_ci	ssb_device_disable(dev->dev, 0);
32068c2ecf20Sopenharmony_ci	ssb_bus_may_powerdown(dev->dev->bus);
32078c2ecf20Sopenharmony_ci}
32088c2ecf20Sopenharmony_ci
32098c2ecf20Sopenharmony_cistatic void prepare_phy_data_for_init(struct b43legacy_wldev *dev)
32108c2ecf20Sopenharmony_ci{
32118c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
32128c2ecf20Sopenharmony_ci	int i;
32138c2ecf20Sopenharmony_ci
32148c2ecf20Sopenharmony_ci	/* Set default attenuation values. */
32158c2ecf20Sopenharmony_ci	phy->bbatt = b43legacy_default_baseband_attenuation(dev);
32168c2ecf20Sopenharmony_ci	phy->rfatt = b43legacy_default_radio_attenuation(dev);
32178c2ecf20Sopenharmony_ci	phy->txctl1 = b43legacy_default_txctl1(dev);
32188c2ecf20Sopenharmony_ci	phy->txctl2 = 0xFFFF;
32198c2ecf20Sopenharmony_ci	phy->txpwr_offset = 0;
32208c2ecf20Sopenharmony_ci
32218c2ecf20Sopenharmony_ci	/* NRSSI */
32228c2ecf20Sopenharmony_ci	phy->nrssislope = 0;
32238c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
32248c2ecf20Sopenharmony_ci		phy->nrssi[i] = -1000;
32258c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
32268c2ecf20Sopenharmony_ci		phy->nrssi_lt[i] = i;
32278c2ecf20Sopenharmony_ci
32288c2ecf20Sopenharmony_ci	phy->lofcal = 0xFFFF;
32298c2ecf20Sopenharmony_ci	phy->initval = 0xFFFF;
32308c2ecf20Sopenharmony_ci
32318c2ecf20Sopenharmony_ci	phy->aci_enable = false;
32328c2ecf20Sopenharmony_ci	phy->aci_wlan_automatic = false;
32338c2ecf20Sopenharmony_ci	phy->aci_hw_rssi = false;
32348c2ecf20Sopenharmony_ci
32358c2ecf20Sopenharmony_ci	phy->antenna_diversity = 0xFFFF;
32368c2ecf20Sopenharmony_ci	memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
32378c2ecf20Sopenharmony_ci	memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
32388c2ecf20Sopenharmony_ci
32398c2ecf20Sopenharmony_ci	/* Flags */
32408c2ecf20Sopenharmony_ci	phy->calibrated = 0;
32418c2ecf20Sopenharmony_ci
32428c2ecf20Sopenharmony_ci	if (phy->_lo_pairs)
32438c2ecf20Sopenharmony_ci		memset(phy->_lo_pairs, 0,
32448c2ecf20Sopenharmony_ci		       sizeof(struct b43legacy_lopair) * B43legacy_LO_COUNT);
32458c2ecf20Sopenharmony_ci	memset(phy->loopback_gain, 0, sizeof(phy->loopback_gain));
32468c2ecf20Sopenharmony_ci}
32478c2ecf20Sopenharmony_ci
32488c2ecf20Sopenharmony_ci/* Initialize a wireless core */
32498c2ecf20Sopenharmony_cistatic int b43legacy_wireless_core_init(struct b43legacy_wldev *dev)
32508c2ecf20Sopenharmony_ci{
32518c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
32528c2ecf20Sopenharmony_ci	struct ssb_bus *bus = dev->dev->bus;
32538c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
32548c2ecf20Sopenharmony_ci	struct ssb_sprom *sprom = &dev->dev->bus->sprom;
32558c2ecf20Sopenharmony_ci	int err;
32568c2ecf20Sopenharmony_ci	u32 hf;
32578c2ecf20Sopenharmony_ci	u32 tmp;
32588c2ecf20Sopenharmony_ci
32598c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
32608c2ecf20Sopenharmony_ci
32618c2ecf20Sopenharmony_ci	err = ssb_bus_powerup(bus, 0);
32628c2ecf20Sopenharmony_ci	if (err)
32638c2ecf20Sopenharmony_ci		goto out;
32648c2ecf20Sopenharmony_ci	if (!ssb_device_is_enabled(dev->dev)) {
32658c2ecf20Sopenharmony_ci		tmp = phy->gmode ? B43legacy_TMSLOW_GMODE : 0;
32668c2ecf20Sopenharmony_ci		b43legacy_wireless_core_reset(dev, tmp);
32678c2ecf20Sopenharmony_ci	}
32688c2ecf20Sopenharmony_ci
32698c2ecf20Sopenharmony_ci	if ((phy->type == B43legacy_PHYTYPE_B) ||
32708c2ecf20Sopenharmony_ci	    (phy->type == B43legacy_PHYTYPE_G)) {
32718c2ecf20Sopenharmony_ci		phy->_lo_pairs = kcalloc(B43legacy_LO_COUNT,
32728c2ecf20Sopenharmony_ci					 sizeof(struct b43legacy_lopair),
32738c2ecf20Sopenharmony_ci					 GFP_KERNEL);
32748c2ecf20Sopenharmony_ci		if (!phy->_lo_pairs)
32758c2ecf20Sopenharmony_ci			return -ENOMEM;
32768c2ecf20Sopenharmony_ci	}
32778c2ecf20Sopenharmony_ci	setup_struct_wldev_for_init(dev);
32788c2ecf20Sopenharmony_ci
32798c2ecf20Sopenharmony_ci	err = b43legacy_phy_init_tssi2dbm_table(dev);
32808c2ecf20Sopenharmony_ci	if (err)
32818c2ecf20Sopenharmony_ci		goto err_kfree_lo_control;
32828c2ecf20Sopenharmony_ci
32838c2ecf20Sopenharmony_ci	/* Enable IRQ routing to this device. */
32848c2ecf20Sopenharmony_ci	ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
32858c2ecf20Sopenharmony_ci
32868c2ecf20Sopenharmony_ci	prepare_phy_data_for_init(dev);
32878c2ecf20Sopenharmony_ci	b43legacy_phy_calibrate(dev);
32888c2ecf20Sopenharmony_ci	err = b43legacy_chip_init(dev);
32898c2ecf20Sopenharmony_ci	if (err)
32908c2ecf20Sopenharmony_ci		goto err_kfree_tssitbl;
32918c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
32928c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_WLCOREREV,
32938c2ecf20Sopenharmony_ci			      dev->dev->id.revision);
32948c2ecf20Sopenharmony_ci	hf = b43legacy_hf_read(dev);
32958c2ecf20Sopenharmony_ci	if (phy->type == B43legacy_PHYTYPE_G) {
32968c2ecf20Sopenharmony_ci		hf |= B43legacy_HF_SYMW;
32978c2ecf20Sopenharmony_ci		if (phy->rev == 1)
32988c2ecf20Sopenharmony_ci			hf |= B43legacy_HF_GDCW;
32998c2ecf20Sopenharmony_ci		if (sprom->boardflags_lo & B43legacy_BFL_PACTRL)
33008c2ecf20Sopenharmony_ci			hf |= B43legacy_HF_OFDMPABOOST;
33018c2ecf20Sopenharmony_ci	} else if (phy->type == B43legacy_PHYTYPE_B) {
33028c2ecf20Sopenharmony_ci		hf |= B43legacy_HF_SYMW;
33038c2ecf20Sopenharmony_ci		if (phy->rev >= 2 && phy->radio_ver == 0x2050)
33048c2ecf20Sopenharmony_ci			hf &= ~B43legacy_HF_GDCW;
33058c2ecf20Sopenharmony_ci	}
33068c2ecf20Sopenharmony_ci	b43legacy_hf_write(dev, hf);
33078c2ecf20Sopenharmony_ci
33088c2ecf20Sopenharmony_ci	b43legacy_set_retry_limits(dev,
33098c2ecf20Sopenharmony_ci				   B43legacy_DEFAULT_SHORT_RETRY_LIMIT,
33108c2ecf20Sopenharmony_ci				   B43legacy_DEFAULT_LONG_RETRY_LIMIT);
33118c2ecf20Sopenharmony_ci
33128c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
33138c2ecf20Sopenharmony_ci			      0x0044, 3);
33148c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
33158c2ecf20Sopenharmony_ci			      0x0046, 2);
33168c2ecf20Sopenharmony_ci
33178c2ecf20Sopenharmony_ci	/* Disable sending probe responses from firmware.
33188c2ecf20Sopenharmony_ci	 * Setting the MaxTime to one usec will always trigger
33198c2ecf20Sopenharmony_ci	 * a timeout, so we never send any probe resp.
33208c2ecf20Sopenharmony_ci	 * A timeout of zero is infinite. */
33218c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_SHARED,
33228c2ecf20Sopenharmony_ci			      B43legacy_SHM_SH_PRMAXTIME, 1);
33238c2ecf20Sopenharmony_ci
33248c2ecf20Sopenharmony_ci	b43legacy_rate_memory_init(dev);
33258c2ecf20Sopenharmony_ci
33268c2ecf20Sopenharmony_ci	/* Minimum Contention Window */
33278c2ecf20Sopenharmony_ci	if (phy->type == B43legacy_PHYTYPE_B)
33288c2ecf20Sopenharmony_ci		b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
33298c2ecf20Sopenharmony_ci				      0x0003, 31);
33308c2ecf20Sopenharmony_ci	else
33318c2ecf20Sopenharmony_ci		b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
33328c2ecf20Sopenharmony_ci				      0x0003, 15);
33338c2ecf20Sopenharmony_ci	/* Maximum Contention Window */
33348c2ecf20Sopenharmony_ci	b43legacy_shm_write16(dev, B43legacy_SHM_WIRELESS,
33358c2ecf20Sopenharmony_ci			      0x0004, 1023);
33368c2ecf20Sopenharmony_ci
33378c2ecf20Sopenharmony_ci	do {
33388c2ecf20Sopenharmony_ci		if (b43legacy_using_pio(dev))
33398c2ecf20Sopenharmony_ci			err = b43legacy_pio_init(dev);
33408c2ecf20Sopenharmony_ci		else {
33418c2ecf20Sopenharmony_ci			err = b43legacy_dma_init(dev);
33428c2ecf20Sopenharmony_ci			if (!err)
33438c2ecf20Sopenharmony_ci				b43legacy_qos_init(dev);
33448c2ecf20Sopenharmony_ci		}
33458c2ecf20Sopenharmony_ci	} while (err == -EAGAIN);
33468c2ecf20Sopenharmony_ci	if (err)
33478c2ecf20Sopenharmony_ci		goto err_chip_exit;
33488c2ecf20Sopenharmony_ci
33498c2ecf20Sopenharmony_ci	b43legacy_set_synth_pu_delay(dev, 1);
33508c2ecf20Sopenharmony_ci
33518c2ecf20Sopenharmony_ci	ssb_bus_powerup(bus, 1); /* Enable dynamic PCTL */
33528c2ecf20Sopenharmony_ci	b43legacy_upload_card_macaddress(dev);
33538c2ecf20Sopenharmony_ci	b43legacy_security_init(dev);
33548c2ecf20Sopenharmony_ci	b43legacy_rng_init(wl);
33558c2ecf20Sopenharmony_ci
33568c2ecf20Sopenharmony_ci	ieee80211_wake_queues(dev->wl->hw);
33578c2ecf20Sopenharmony_ci	b43legacy_set_status(dev, B43legacy_STAT_INITIALIZED);
33588c2ecf20Sopenharmony_ci
33598c2ecf20Sopenharmony_ci	b43legacy_leds_init(dev);
33608c2ecf20Sopenharmony_ciout:
33618c2ecf20Sopenharmony_ci	return err;
33628c2ecf20Sopenharmony_ci
33638c2ecf20Sopenharmony_cierr_chip_exit:
33648c2ecf20Sopenharmony_ci	b43legacy_chip_exit(dev);
33658c2ecf20Sopenharmony_cierr_kfree_tssitbl:
33668c2ecf20Sopenharmony_ci	if (phy->dyn_tssi_tbl)
33678c2ecf20Sopenharmony_ci		kfree(phy->tssi2dbm);
33688c2ecf20Sopenharmony_cierr_kfree_lo_control:
33698c2ecf20Sopenharmony_ci	kfree(phy->lo_control);
33708c2ecf20Sopenharmony_ci	phy->lo_control = NULL;
33718c2ecf20Sopenharmony_ci	ssb_bus_may_powerdown(bus);
33728c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(b43legacy_status(dev) != B43legacy_STAT_UNINIT);
33738c2ecf20Sopenharmony_ci	return err;
33748c2ecf20Sopenharmony_ci}
33758c2ecf20Sopenharmony_ci
33768c2ecf20Sopenharmony_cistatic int b43legacy_op_add_interface(struct ieee80211_hw *hw,
33778c2ecf20Sopenharmony_ci				      struct ieee80211_vif *vif)
33788c2ecf20Sopenharmony_ci{
33798c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
33808c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev;
33818c2ecf20Sopenharmony_ci	unsigned long flags;
33828c2ecf20Sopenharmony_ci	int err = -EOPNOTSUPP;
33838c2ecf20Sopenharmony_ci
33848c2ecf20Sopenharmony_ci	/* TODO: allow WDS/AP devices to coexist */
33858c2ecf20Sopenharmony_ci
33868c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_AP &&
33878c2ecf20Sopenharmony_ci	    vif->type != NL80211_IFTYPE_STATION &&
33888c2ecf20Sopenharmony_ci	    vif->type != NL80211_IFTYPE_WDS &&
33898c2ecf20Sopenharmony_ci	    vif->type != NL80211_IFTYPE_ADHOC)
33908c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
33918c2ecf20Sopenharmony_ci
33928c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
33938c2ecf20Sopenharmony_ci	if (wl->operating)
33948c2ecf20Sopenharmony_ci		goto out_mutex_unlock;
33958c2ecf20Sopenharmony_ci
33968c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Adding Interface type %d\n", vif->type);
33978c2ecf20Sopenharmony_ci
33988c2ecf20Sopenharmony_ci	dev = wl->current_dev;
33998c2ecf20Sopenharmony_ci	wl->operating = true;
34008c2ecf20Sopenharmony_ci	wl->vif = vif;
34018c2ecf20Sopenharmony_ci	wl->if_type = vif->type;
34028c2ecf20Sopenharmony_ci	memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
34038c2ecf20Sopenharmony_ci
34048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
34058c2ecf20Sopenharmony_ci	b43legacy_adjust_opmode(dev);
34068c2ecf20Sopenharmony_ci	b43legacy_set_pretbtt(dev);
34078c2ecf20Sopenharmony_ci	b43legacy_set_synth_pu_delay(dev, 0);
34088c2ecf20Sopenharmony_ci	b43legacy_upload_card_macaddress(dev);
34098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
34108c2ecf20Sopenharmony_ci
34118c2ecf20Sopenharmony_ci	err = 0;
34128c2ecf20Sopenharmony_ci out_mutex_unlock:
34138c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
34148c2ecf20Sopenharmony_ci
34158c2ecf20Sopenharmony_ci	return err;
34168c2ecf20Sopenharmony_ci}
34178c2ecf20Sopenharmony_ci
34188c2ecf20Sopenharmony_cistatic void b43legacy_op_remove_interface(struct ieee80211_hw *hw,
34198c2ecf20Sopenharmony_ci					  struct ieee80211_vif *vif)
34208c2ecf20Sopenharmony_ci{
34218c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
34228c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = wl->current_dev;
34238c2ecf20Sopenharmony_ci	unsigned long flags;
34248c2ecf20Sopenharmony_ci
34258c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Removing Interface type %d\n", vif->type);
34268c2ecf20Sopenharmony_ci
34278c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
34288c2ecf20Sopenharmony_ci
34298c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(!wl->operating);
34308c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(wl->vif != vif);
34318c2ecf20Sopenharmony_ci	wl->vif = NULL;
34328c2ecf20Sopenharmony_ci
34338c2ecf20Sopenharmony_ci	wl->operating = false;
34348c2ecf20Sopenharmony_ci
34358c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
34368c2ecf20Sopenharmony_ci	b43legacy_adjust_opmode(dev);
34378c2ecf20Sopenharmony_ci	eth_zero_addr(wl->mac_addr);
34388c2ecf20Sopenharmony_ci	b43legacy_upload_card_macaddress(dev);
34398c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
34408c2ecf20Sopenharmony_ci
34418c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
34428c2ecf20Sopenharmony_ci}
34438c2ecf20Sopenharmony_ci
34448c2ecf20Sopenharmony_cistatic int b43legacy_op_start(struct ieee80211_hw *hw)
34458c2ecf20Sopenharmony_ci{
34468c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
34478c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = wl->current_dev;
34488c2ecf20Sopenharmony_ci	int did_init = 0;
34498c2ecf20Sopenharmony_ci	int err = 0;
34508c2ecf20Sopenharmony_ci
34518c2ecf20Sopenharmony_ci	/* Kill all old instance specific information to make sure
34528c2ecf20Sopenharmony_ci	 * the card won't use it in the short timeframe between start
34538c2ecf20Sopenharmony_ci	 * and mac80211 reconfiguring it. */
34548c2ecf20Sopenharmony_ci	eth_zero_addr(wl->bssid);
34558c2ecf20Sopenharmony_ci	eth_zero_addr(wl->mac_addr);
34568c2ecf20Sopenharmony_ci	wl->filter_flags = 0;
34578c2ecf20Sopenharmony_ci	wl->beacon0_uploaded = false;
34588c2ecf20Sopenharmony_ci	wl->beacon1_uploaded = false;
34598c2ecf20Sopenharmony_ci	wl->beacon_templates_virgin = true;
34608c2ecf20Sopenharmony_ci	wl->radio_enabled = true;
34618c2ecf20Sopenharmony_ci
34628c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
34638c2ecf20Sopenharmony_ci
34648c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED) {
34658c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_init(dev);
34668c2ecf20Sopenharmony_ci		if (err)
34678c2ecf20Sopenharmony_ci			goto out_mutex_unlock;
34688c2ecf20Sopenharmony_ci		did_init = 1;
34698c2ecf20Sopenharmony_ci	}
34708c2ecf20Sopenharmony_ci
34718c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) < B43legacy_STAT_STARTED) {
34728c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_start(dev);
34738c2ecf20Sopenharmony_ci		if (err) {
34748c2ecf20Sopenharmony_ci			if (did_init)
34758c2ecf20Sopenharmony_ci				b43legacy_wireless_core_exit(dev);
34768c2ecf20Sopenharmony_ci			goto out_mutex_unlock;
34778c2ecf20Sopenharmony_ci		}
34788c2ecf20Sopenharmony_ci	}
34798c2ecf20Sopenharmony_ci
34808c2ecf20Sopenharmony_ci	wiphy_rfkill_start_polling(hw->wiphy);
34818c2ecf20Sopenharmony_ci
34828c2ecf20Sopenharmony_ciout_mutex_unlock:
34838c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
34848c2ecf20Sopenharmony_ci
34858c2ecf20Sopenharmony_ci	return err;
34868c2ecf20Sopenharmony_ci}
34878c2ecf20Sopenharmony_ci
34888c2ecf20Sopenharmony_cistatic void b43legacy_op_stop(struct ieee80211_hw *hw)
34898c2ecf20Sopenharmony_ci{
34908c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
34918c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = wl->current_dev;
34928c2ecf20Sopenharmony_ci
34938c2ecf20Sopenharmony_ci	cancel_work_sync(&(wl->beacon_update_trigger));
34948c2ecf20Sopenharmony_ci
34958c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
34968c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) >= B43legacy_STAT_STARTED)
34978c2ecf20Sopenharmony_ci		b43legacy_wireless_core_stop(dev);
34988c2ecf20Sopenharmony_ci	b43legacy_wireless_core_exit(dev);
34998c2ecf20Sopenharmony_ci	wl->radio_enabled = false;
35008c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
35018c2ecf20Sopenharmony_ci}
35028c2ecf20Sopenharmony_ci
35038c2ecf20Sopenharmony_cistatic int b43legacy_op_beacon_set_tim(struct ieee80211_hw *hw,
35048c2ecf20Sopenharmony_ci				       struct ieee80211_sta *sta, bool set)
35058c2ecf20Sopenharmony_ci{
35068c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
35078c2ecf20Sopenharmony_ci	unsigned long flags;
35088c2ecf20Sopenharmony_ci
35098c2ecf20Sopenharmony_ci	spin_lock_irqsave(&wl->irq_lock, flags);
35108c2ecf20Sopenharmony_ci	b43legacy_update_templates(wl);
35118c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&wl->irq_lock, flags);
35128c2ecf20Sopenharmony_ci
35138c2ecf20Sopenharmony_ci	return 0;
35148c2ecf20Sopenharmony_ci}
35158c2ecf20Sopenharmony_ci
35168c2ecf20Sopenharmony_cistatic int b43legacy_op_get_survey(struct ieee80211_hw *hw, int idx,
35178c2ecf20Sopenharmony_ci				   struct survey_info *survey)
35188c2ecf20Sopenharmony_ci{
35198c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = hw_to_b43legacy_wl(hw);
35208c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev = wl->current_dev;
35218c2ecf20Sopenharmony_ci	struct ieee80211_conf *conf = &hw->conf;
35228c2ecf20Sopenharmony_ci
35238c2ecf20Sopenharmony_ci	if (idx != 0)
35248c2ecf20Sopenharmony_ci		return -ENOENT;
35258c2ecf20Sopenharmony_ci
35268c2ecf20Sopenharmony_ci	survey->channel = conf->chandef.chan;
35278c2ecf20Sopenharmony_ci	survey->filled = SURVEY_INFO_NOISE_DBM;
35288c2ecf20Sopenharmony_ci	survey->noise = dev->stats.link_noise;
35298c2ecf20Sopenharmony_ci
35308c2ecf20Sopenharmony_ci	return 0;
35318c2ecf20Sopenharmony_ci}
35328c2ecf20Sopenharmony_ci
35338c2ecf20Sopenharmony_cistatic const struct ieee80211_ops b43legacy_hw_ops = {
35348c2ecf20Sopenharmony_ci	.tx			= b43legacy_op_tx,
35358c2ecf20Sopenharmony_ci	.conf_tx		= b43legacy_op_conf_tx,
35368c2ecf20Sopenharmony_ci	.add_interface		= b43legacy_op_add_interface,
35378c2ecf20Sopenharmony_ci	.remove_interface	= b43legacy_op_remove_interface,
35388c2ecf20Sopenharmony_ci	.config			= b43legacy_op_dev_config,
35398c2ecf20Sopenharmony_ci	.bss_info_changed	= b43legacy_op_bss_info_changed,
35408c2ecf20Sopenharmony_ci	.configure_filter	= b43legacy_op_configure_filter,
35418c2ecf20Sopenharmony_ci	.get_stats		= b43legacy_op_get_stats,
35428c2ecf20Sopenharmony_ci	.start			= b43legacy_op_start,
35438c2ecf20Sopenharmony_ci	.stop			= b43legacy_op_stop,
35448c2ecf20Sopenharmony_ci	.set_tim		= b43legacy_op_beacon_set_tim,
35458c2ecf20Sopenharmony_ci	.get_survey		= b43legacy_op_get_survey,
35468c2ecf20Sopenharmony_ci	.rfkill_poll		= b43legacy_rfkill_poll,
35478c2ecf20Sopenharmony_ci};
35488c2ecf20Sopenharmony_ci
35498c2ecf20Sopenharmony_ci/* Hard-reset the chip. Do not call this directly.
35508c2ecf20Sopenharmony_ci * Use b43legacy_controller_restart()
35518c2ecf20Sopenharmony_ci */
35528c2ecf20Sopenharmony_cistatic void b43legacy_chip_reset(struct work_struct *work)
35538c2ecf20Sopenharmony_ci{
35548c2ecf20Sopenharmony_ci	struct b43legacy_wldev *dev =
35558c2ecf20Sopenharmony_ci		container_of(work, struct b43legacy_wldev, restart_work);
35568c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
35578c2ecf20Sopenharmony_ci	int err = 0;
35588c2ecf20Sopenharmony_ci	int prev_status;
35598c2ecf20Sopenharmony_ci
35608c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
35618c2ecf20Sopenharmony_ci
35628c2ecf20Sopenharmony_ci	prev_status = b43legacy_status(dev);
35638c2ecf20Sopenharmony_ci	/* Bring the device down... */
35648c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_STARTED)
35658c2ecf20Sopenharmony_ci		b43legacy_wireless_core_stop(dev);
35668c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_INITIALIZED)
35678c2ecf20Sopenharmony_ci		b43legacy_wireless_core_exit(dev);
35688c2ecf20Sopenharmony_ci
35698c2ecf20Sopenharmony_ci	/* ...and up again. */
35708c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_INITIALIZED) {
35718c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_init(dev);
35728c2ecf20Sopenharmony_ci		if (err)
35738c2ecf20Sopenharmony_ci			goto out;
35748c2ecf20Sopenharmony_ci	}
35758c2ecf20Sopenharmony_ci	if (prev_status >= B43legacy_STAT_STARTED) {
35768c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_start(dev);
35778c2ecf20Sopenharmony_ci		if (err) {
35788c2ecf20Sopenharmony_ci			b43legacy_wireless_core_exit(dev);
35798c2ecf20Sopenharmony_ci			goto out;
35808c2ecf20Sopenharmony_ci		}
35818c2ecf20Sopenharmony_ci	}
35828c2ecf20Sopenharmony_ciout:
35838c2ecf20Sopenharmony_ci	if (err)
35848c2ecf20Sopenharmony_ci		wl->current_dev = NULL; /* Failed to init the dev. */
35858c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
35868c2ecf20Sopenharmony_ci	if (err)
35878c2ecf20Sopenharmony_ci		b43legacyerr(wl, "Controller restart FAILED\n");
35888c2ecf20Sopenharmony_ci	else
35898c2ecf20Sopenharmony_ci		b43legacyinfo(wl, "Controller restarted\n");
35908c2ecf20Sopenharmony_ci}
35918c2ecf20Sopenharmony_ci
35928c2ecf20Sopenharmony_cistatic int b43legacy_setup_modes(struct b43legacy_wldev *dev,
35938c2ecf20Sopenharmony_ci				 int have_bphy,
35948c2ecf20Sopenharmony_ci				 int have_gphy)
35958c2ecf20Sopenharmony_ci{
35968c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = dev->wl->hw;
35978c2ecf20Sopenharmony_ci	struct b43legacy_phy *phy = &dev->phy;
35988c2ecf20Sopenharmony_ci
35998c2ecf20Sopenharmony_ci	phy->possible_phymodes = 0;
36008c2ecf20Sopenharmony_ci	if (have_bphy) {
36018c2ecf20Sopenharmony_ci		hw->wiphy->bands[NL80211_BAND_2GHZ] =
36028c2ecf20Sopenharmony_ci			&b43legacy_band_2GHz_BPHY;
36038c2ecf20Sopenharmony_ci		phy->possible_phymodes |= B43legacy_PHYMODE_B;
36048c2ecf20Sopenharmony_ci	}
36058c2ecf20Sopenharmony_ci
36068c2ecf20Sopenharmony_ci	if (have_gphy) {
36078c2ecf20Sopenharmony_ci		hw->wiphy->bands[NL80211_BAND_2GHZ] =
36088c2ecf20Sopenharmony_ci			&b43legacy_band_2GHz_GPHY;
36098c2ecf20Sopenharmony_ci		phy->possible_phymodes |= B43legacy_PHYMODE_G;
36108c2ecf20Sopenharmony_ci	}
36118c2ecf20Sopenharmony_ci
36128c2ecf20Sopenharmony_ci	return 0;
36138c2ecf20Sopenharmony_ci}
36148c2ecf20Sopenharmony_ci
36158c2ecf20Sopenharmony_cistatic void b43legacy_wireless_core_detach(struct b43legacy_wldev *dev)
36168c2ecf20Sopenharmony_ci{
36178c2ecf20Sopenharmony_ci	/* We release firmware that late to not be required to re-request
36188c2ecf20Sopenharmony_ci	 * is all the time when we reinit the core. */
36198c2ecf20Sopenharmony_ci	b43legacy_release_firmware(dev);
36208c2ecf20Sopenharmony_ci}
36218c2ecf20Sopenharmony_ci
36228c2ecf20Sopenharmony_cistatic int b43legacy_wireless_core_attach(struct b43legacy_wldev *dev)
36238c2ecf20Sopenharmony_ci{
36248c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = dev->wl;
36258c2ecf20Sopenharmony_ci	struct ssb_bus *bus = dev->dev->bus;
36268c2ecf20Sopenharmony_ci	struct pci_dev *pdev = (bus->bustype == SSB_BUSTYPE_PCI) ? bus->host_pci : NULL;
36278c2ecf20Sopenharmony_ci	int err;
36288c2ecf20Sopenharmony_ci	int have_bphy = 0;
36298c2ecf20Sopenharmony_ci	int have_gphy = 0;
36308c2ecf20Sopenharmony_ci	u32 tmp;
36318c2ecf20Sopenharmony_ci
36328c2ecf20Sopenharmony_ci	/* Do NOT do any device initialization here.
36338c2ecf20Sopenharmony_ci	 * Do it in wireless_core_init() instead.
36348c2ecf20Sopenharmony_ci	 * This function is for gathering basic information about the HW, only.
36358c2ecf20Sopenharmony_ci	 * Also some structs may be set up here. But most likely you want to
36368c2ecf20Sopenharmony_ci	 * have that in core_init(), too.
36378c2ecf20Sopenharmony_ci	 */
36388c2ecf20Sopenharmony_ci
36398c2ecf20Sopenharmony_ci	err = ssb_bus_powerup(bus, 0);
36408c2ecf20Sopenharmony_ci	if (err) {
36418c2ecf20Sopenharmony_ci		b43legacyerr(wl, "Bus powerup failed\n");
36428c2ecf20Sopenharmony_ci		goto out;
36438c2ecf20Sopenharmony_ci	}
36448c2ecf20Sopenharmony_ci	/* Get the PHY type. */
36458c2ecf20Sopenharmony_ci	if (dev->dev->id.revision >= 5) {
36468c2ecf20Sopenharmony_ci		u32 tmshigh;
36478c2ecf20Sopenharmony_ci
36488c2ecf20Sopenharmony_ci		tmshigh = ssb_read32(dev->dev, SSB_TMSHIGH);
36498c2ecf20Sopenharmony_ci		have_gphy = !!(tmshigh & B43legacy_TMSHIGH_GPHY);
36508c2ecf20Sopenharmony_ci		if (!have_gphy)
36518c2ecf20Sopenharmony_ci			have_bphy = 1;
36528c2ecf20Sopenharmony_ci	} else if (dev->dev->id.revision == 4)
36538c2ecf20Sopenharmony_ci		have_gphy = 1;
36548c2ecf20Sopenharmony_ci	else
36558c2ecf20Sopenharmony_ci		have_bphy = 1;
36568c2ecf20Sopenharmony_ci
36578c2ecf20Sopenharmony_ci	dev->phy.gmode = (have_gphy || have_bphy);
36588c2ecf20Sopenharmony_ci	dev->phy.radio_on = true;
36598c2ecf20Sopenharmony_ci	tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
36608c2ecf20Sopenharmony_ci	b43legacy_wireless_core_reset(dev, tmp);
36618c2ecf20Sopenharmony_ci
36628c2ecf20Sopenharmony_ci	err = b43legacy_phy_versioning(dev);
36638c2ecf20Sopenharmony_ci	if (err)
36648c2ecf20Sopenharmony_ci		goto err_powerdown;
36658c2ecf20Sopenharmony_ci	/* Check if this device supports multiband. */
36668c2ecf20Sopenharmony_ci	if (!pdev ||
36678c2ecf20Sopenharmony_ci	    (pdev->device != 0x4312 &&
36688c2ecf20Sopenharmony_ci	     pdev->device != 0x4319 &&
36698c2ecf20Sopenharmony_ci	     pdev->device != 0x4324)) {
36708c2ecf20Sopenharmony_ci		/* No multiband support. */
36718c2ecf20Sopenharmony_ci		have_bphy = 0;
36728c2ecf20Sopenharmony_ci		have_gphy = 0;
36738c2ecf20Sopenharmony_ci		switch (dev->phy.type) {
36748c2ecf20Sopenharmony_ci		case B43legacy_PHYTYPE_B:
36758c2ecf20Sopenharmony_ci			have_bphy = 1;
36768c2ecf20Sopenharmony_ci			break;
36778c2ecf20Sopenharmony_ci		case B43legacy_PHYTYPE_G:
36788c2ecf20Sopenharmony_ci			have_gphy = 1;
36798c2ecf20Sopenharmony_ci			break;
36808c2ecf20Sopenharmony_ci		default:
36818c2ecf20Sopenharmony_ci			B43legacy_BUG_ON(1);
36828c2ecf20Sopenharmony_ci		}
36838c2ecf20Sopenharmony_ci	}
36848c2ecf20Sopenharmony_ci	dev->phy.gmode = (have_gphy || have_bphy);
36858c2ecf20Sopenharmony_ci	tmp = dev->phy.gmode ? B43legacy_TMSLOW_GMODE : 0;
36868c2ecf20Sopenharmony_ci	b43legacy_wireless_core_reset(dev, tmp);
36878c2ecf20Sopenharmony_ci
36888c2ecf20Sopenharmony_ci	err = b43legacy_validate_chipaccess(dev);
36898c2ecf20Sopenharmony_ci	if (err)
36908c2ecf20Sopenharmony_ci		goto err_powerdown;
36918c2ecf20Sopenharmony_ci	err = b43legacy_setup_modes(dev, have_bphy, have_gphy);
36928c2ecf20Sopenharmony_ci	if (err)
36938c2ecf20Sopenharmony_ci		goto err_powerdown;
36948c2ecf20Sopenharmony_ci
36958c2ecf20Sopenharmony_ci	/* Now set some default "current_dev" */
36968c2ecf20Sopenharmony_ci	if (!wl->current_dev)
36978c2ecf20Sopenharmony_ci		wl->current_dev = dev;
36988c2ecf20Sopenharmony_ci	INIT_WORK(&dev->restart_work, b43legacy_chip_reset);
36998c2ecf20Sopenharmony_ci
37008c2ecf20Sopenharmony_ci	b43legacy_radio_turn_off(dev, 1);
37018c2ecf20Sopenharmony_ci	b43legacy_switch_analog(dev, 0);
37028c2ecf20Sopenharmony_ci	ssb_device_disable(dev->dev, 0);
37038c2ecf20Sopenharmony_ci	ssb_bus_may_powerdown(bus);
37048c2ecf20Sopenharmony_ci
37058c2ecf20Sopenharmony_ciout:
37068c2ecf20Sopenharmony_ci	return err;
37078c2ecf20Sopenharmony_ci
37088c2ecf20Sopenharmony_cierr_powerdown:
37098c2ecf20Sopenharmony_ci	ssb_bus_may_powerdown(bus);
37108c2ecf20Sopenharmony_ci	return err;
37118c2ecf20Sopenharmony_ci}
37128c2ecf20Sopenharmony_ci
37138c2ecf20Sopenharmony_cistatic void b43legacy_one_core_detach(struct ssb_device *dev)
37148c2ecf20Sopenharmony_ci{
37158c2ecf20Sopenharmony_ci	struct b43legacy_wldev *wldev;
37168c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl;
37178c2ecf20Sopenharmony_ci
37188c2ecf20Sopenharmony_ci	/* Do not cancel ieee80211-workqueue based work here.
37198c2ecf20Sopenharmony_ci	 * See comment in b43legacy_remove(). */
37208c2ecf20Sopenharmony_ci
37218c2ecf20Sopenharmony_ci	wldev = ssb_get_drvdata(dev);
37228c2ecf20Sopenharmony_ci	wl = wldev->wl;
37238c2ecf20Sopenharmony_ci	b43legacy_debugfs_remove_device(wldev);
37248c2ecf20Sopenharmony_ci	b43legacy_wireless_core_detach(wldev);
37258c2ecf20Sopenharmony_ci	list_del(&wldev->list);
37268c2ecf20Sopenharmony_ci	wl->nr_devs--;
37278c2ecf20Sopenharmony_ci	ssb_set_drvdata(dev, NULL);
37288c2ecf20Sopenharmony_ci	kfree(wldev);
37298c2ecf20Sopenharmony_ci}
37308c2ecf20Sopenharmony_ci
37318c2ecf20Sopenharmony_cistatic int b43legacy_one_core_attach(struct ssb_device *dev,
37328c2ecf20Sopenharmony_ci				     struct b43legacy_wl *wl)
37338c2ecf20Sopenharmony_ci{
37348c2ecf20Sopenharmony_ci	struct b43legacy_wldev *wldev;
37358c2ecf20Sopenharmony_ci	int err = -ENOMEM;
37368c2ecf20Sopenharmony_ci
37378c2ecf20Sopenharmony_ci	wldev = kzalloc(sizeof(*wldev), GFP_KERNEL);
37388c2ecf20Sopenharmony_ci	if (!wldev)
37398c2ecf20Sopenharmony_ci		goto out;
37408c2ecf20Sopenharmony_ci
37418c2ecf20Sopenharmony_ci	wldev->dev = dev;
37428c2ecf20Sopenharmony_ci	wldev->wl = wl;
37438c2ecf20Sopenharmony_ci	b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
37448c2ecf20Sopenharmony_ci	wldev->bad_frames_preempt = modparam_bad_frames_preempt;
37458c2ecf20Sopenharmony_ci	tasklet_setup(&wldev->isr_tasklet, b43legacy_interrupt_tasklet);
37468c2ecf20Sopenharmony_ci	if (modparam_pio)
37478c2ecf20Sopenharmony_ci		wldev->__using_pio = true;
37488c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&wldev->list);
37498c2ecf20Sopenharmony_ci
37508c2ecf20Sopenharmony_ci	err = b43legacy_wireless_core_attach(wldev);
37518c2ecf20Sopenharmony_ci	if (err)
37528c2ecf20Sopenharmony_ci		goto err_kfree_wldev;
37538c2ecf20Sopenharmony_ci
37548c2ecf20Sopenharmony_ci	list_add(&wldev->list, &wl->devlist);
37558c2ecf20Sopenharmony_ci	wl->nr_devs++;
37568c2ecf20Sopenharmony_ci	ssb_set_drvdata(dev, wldev);
37578c2ecf20Sopenharmony_ci	b43legacy_debugfs_add_device(wldev);
37588c2ecf20Sopenharmony_ciout:
37598c2ecf20Sopenharmony_ci	return err;
37608c2ecf20Sopenharmony_ci
37618c2ecf20Sopenharmony_cierr_kfree_wldev:
37628c2ecf20Sopenharmony_ci	kfree(wldev);
37638c2ecf20Sopenharmony_ci	return err;
37648c2ecf20Sopenharmony_ci}
37658c2ecf20Sopenharmony_ci
37668c2ecf20Sopenharmony_cistatic void b43legacy_sprom_fixup(struct ssb_bus *bus)
37678c2ecf20Sopenharmony_ci{
37688c2ecf20Sopenharmony_ci	/* boardflags workarounds */
37698c2ecf20Sopenharmony_ci	if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
37708c2ecf20Sopenharmony_ci	    bus->boardinfo.type == 0x4E &&
37718c2ecf20Sopenharmony_ci	    bus->sprom.board_rev > 0x40)
37728c2ecf20Sopenharmony_ci		bus->sprom.boardflags_lo |= B43legacy_BFL_PACTRL;
37738c2ecf20Sopenharmony_ci}
37748c2ecf20Sopenharmony_ci
37758c2ecf20Sopenharmony_cistatic void b43legacy_wireless_exit(struct ssb_device *dev,
37768c2ecf20Sopenharmony_ci				  struct b43legacy_wl *wl)
37778c2ecf20Sopenharmony_ci{
37788c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = wl->hw;
37798c2ecf20Sopenharmony_ci
37808c2ecf20Sopenharmony_ci	ssb_set_devtypedata(dev, NULL);
37818c2ecf20Sopenharmony_ci	ieee80211_free_hw(hw);
37828c2ecf20Sopenharmony_ci}
37838c2ecf20Sopenharmony_ci
37848c2ecf20Sopenharmony_cistatic int b43legacy_wireless_init(struct ssb_device *dev)
37858c2ecf20Sopenharmony_ci{
37868c2ecf20Sopenharmony_ci	struct ssb_sprom *sprom = &dev->bus->sprom;
37878c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw;
37888c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl;
37898c2ecf20Sopenharmony_ci	int err = -ENOMEM;
37908c2ecf20Sopenharmony_ci	int queue_num;
37918c2ecf20Sopenharmony_ci
37928c2ecf20Sopenharmony_ci	b43legacy_sprom_fixup(dev->bus);
37938c2ecf20Sopenharmony_ci
37948c2ecf20Sopenharmony_ci	hw = ieee80211_alloc_hw(sizeof(*wl), &b43legacy_hw_ops);
37958c2ecf20Sopenharmony_ci	if (!hw) {
37968c2ecf20Sopenharmony_ci		b43legacyerr(NULL, "Could not allocate ieee80211 device\n");
37978c2ecf20Sopenharmony_ci		goto out;
37988c2ecf20Sopenharmony_ci	}
37998c2ecf20Sopenharmony_ci
38008c2ecf20Sopenharmony_ci	/* fill hw info */
38018c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
38028c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, SIGNAL_DBM);
38038c2ecf20Sopenharmony_ci	ieee80211_hw_set(hw, MFP_CAPABLE); /* Allow WPA3 in software */
38048c2ecf20Sopenharmony_ci
38058c2ecf20Sopenharmony_ci	hw->wiphy->interface_modes =
38068c2ecf20Sopenharmony_ci		BIT(NL80211_IFTYPE_AP) |
38078c2ecf20Sopenharmony_ci		BIT(NL80211_IFTYPE_STATION) |
38088c2ecf20Sopenharmony_ci#ifdef CONFIG_WIRELESS_WDS
38098c2ecf20Sopenharmony_ci		BIT(NL80211_IFTYPE_WDS) |
38108c2ecf20Sopenharmony_ci#endif
38118c2ecf20Sopenharmony_ci		BIT(NL80211_IFTYPE_ADHOC);
38128c2ecf20Sopenharmony_ci	hw->queues = 1; /* FIXME: hardware has more queues */
38138c2ecf20Sopenharmony_ci	hw->max_rates = 2;
38148c2ecf20Sopenharmony_ci	SET_IEEE80211_DEV(hw, dev->dev);
38158c2ecf20Sopenharmony_ci	if (is_valid_ether_addr(sprom->et1mac))
38168c2ecf20Sopenharmony_ci		SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
38178c2ecf20Sopenharmony_ci	else
38188c2ecf20Sopenharmony_ci		SET_IEEE80211_PERM_ADDR(hw, sprom->il0mac);
38198c2ecf20Sopenharmony_ci
38208c2ecf20Sopenharmony_ci	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
38218c2ecf20Sopenharmony_ci
38228c2ecf20Sopenharmony_ci	/* Get and initialize struct b43legacy_wl */
38238c2ecf20Sopenharmony_ci	wl = hw_to_b43legacy_wl(hw);
38248c2ecf20Sopenharmony_ci	memset(wl, 0, sizeof(*wl));
38258c2ecf20Sopenharmony_ci	wl->hw = hw;
38268c2ecf20Sopenharmony_ci	spin_lock_init(&wl->irq_lock);
38278c2ecf20Sopenharmony_ci	spin_lock_init(&wl->leds_lock);
38288c2ecf20Sopenharmony_ci	mutex_init(&wl->mutex);
38298c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&wl->devlist);
38308c2ecf20Sopenharmony_ci	INIT_WORK(&wl->beacon_update_trigger, b43legacy_beacon_update_trigger_work);
38318c2ecf20Sopenharmony_ci	INIT_WORK(&wl->tx_work, b43legacy_tx_work);
38328c2ecf20Sopenharmony_ci
38338c2ecf20Sopenharmony_ci	/* Initialize queues and flags. */
38348c2ecf20Sopenharmony_ci	for (queue_num = 0; queue_num < B43legacy_QOS_QUEUE_NUM; queue_num++) {
38358c2ecf20Sopenharmony_ci		skb_queue_head_init(&wl->tx_queue[queue_num]);
38368c2ecf20Sopenharmony_ci		wl->tx_queue_stopped[queue_num] = 0;
38378c2ecf20Sopenharmony_ci	}
38388c2ecf20Sopenharmony_ci
38398c2ecf20Sopenharmony_ci	ssb_set_devtypedata(dev, wl);
38408c2ecf20Sopenharmony_ci	b43legacyinfo(wl, "Broadcom %04X WLAN found (core revision %u)\n",
38418c2ecf20Sopenharmony_ci		      dev->bus->chip_id, dev->id.revision);
38428c2ecf20Sopenharmony_ci	err = 0;
38438c2ecf20Sopenharmony_ciout:
38448c2ecf20Sopenharmony_ci	return err;
38458c2ecf20Sopenharmony_ci}
38468c2ecf20Sopenharmony_ci
38478c2ecf20Sopenharmony_cistatic int b43legacy_probe(struct ssb_device *dev,
38488c2ecf20Sopenharmony_ci			 const struct ssb_device_id *id)
38498c2ecf20Sopenharmony_ci{
38508c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl;
38518c2ecf20Sopenharmony_ci	int err;
38528c2ecf20Sopenharmony_ci	int first = 0;
38538c2ecf20Sopenharmony_ci
38548c2ecf20Sopenharmony_ci	wl = ssb_get_devtypedata(dev);
38558c2ecf20Sopenharmony_ci	if (!wl) {
38568c2ecf20Sopenharmony_ci		/* Probing the first core - setup common struct b43legacy_wl */
38578c2ecf20Sopenharmony_ci		first = 1;
38588c2ecf20Sopenharmony_ci		err = b43legacy_wireless_init(dev);
38598c2ecf20Sopenharmony_ci		if (err)
38608c2ecf20Sopenharmony_ci			goto out;
38618c2ecf20Sopenharmony_ci		wl = ssb_get_devtypedata(dev);
38628c2ecf20Sopenharmony_ci		B43legacy_WARN_ON(!wl);
38638c2ecf20Sopenharmony_ci	}
38648c2ecf20Sopenharmony_ci	err = b43legacy_one_core_attach(dev, wl);
38658c2ecf20Sopenharmony_ci	if (err)
38668c2ecf20Sopenharmony_ci		goto err_wireless_exit;
38678c2ecf20Sopenharmony_ci
38688c2ecf20Sopenharmony_ci	/* setup and start work to load firmware */
38698c2ecf20Sopenharmony_ci	INIT_WORK(&wl->firmware_load, b43legacy_request_firmware);
38708c2ecf20Sopenharmony_ci	schedule_work(&wl->firmware_load);
38718c2ecf20Sopenharmony_ci
38728c2ecf20Sopenharmony_ciout:
38738c2ecf20Sopenharmony_ci	return err;
38748c2ecf20Sopenharmony_ci
38758c2ecf20Sopenharmony_cierr_wireless_exit:
38768c2ecf20Sopenharmony_ci	if (first)
38778c2ecf20Sopenharmony_ci		b43legacy_wireless_exit(dev, wl);
38788c2ecf20Sopenharmony_ci	return err;
38798c2ecf20Sopenharmony_ci}
38808c2ecf20Sopenharmony_ci
38818c2ecf20Sopenharmony_cistatic void b43legacy_remove(struct ssb_device *dev)
38828c2ecf20Sopenharmony_ci{
38838c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = ssb_get_devtypedata(dev);
38848c2ecf20Sopenharmony_ci	struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
38858c2ecf20Sopenharmony_ci
38868c2ecf20Sopenharmony_ci	/* We must cancel any work here before unregistering from ieee80211,
38878c2ecf20Sopenharmony_ci	 * as the ieee80211 unreg will destroy the workqueue. */
38888c2ecf20Sopenharmony_ci	cancel_work_sync(&wldev->restart_work);
38898c2ecf20Sopenharmony_ci	cancel_work_sync(&wl->firmware_load);
38908c2ecf20Sopenharmony_ci	complete(&wldev->fw_load_complete);
38918c2ecf20Sopenharmony_ci
38928c2ecf20Sopenharmony_ci	B43legacy_WARN_ON(!wl);
38938c2ecf20Sopenharmony_ci	if (!wldev->fw.ucode)
38948c2ecf20Sopenharmony_ci		return;			/* NULL if fw never loaded */
38958c2ecf20Sopenharmony_ci	if (wl->current_dev == wldev)
38968c2ecf20Sopenharmony_ci		ieee80211_unregister_hw(wl->hw);
38978c2ecf20Sopenharmony_ci
38988c2ecf20Sopenharmony_ci	b43legacy_one_core_detach(dev);
38998c2ecf20Sopenharmony_ci
39008c2ecf20Sopenharmony_ci	if (list_empty(&wl->devlist))
39018c2ecf20Sopenharmony_ci		/* Last core on the chip unregistered.
39028c2ecf20Sopenharmony_ci		 * We can destroy common struct b43legacy_wl.
39038c2ecf20Sopenharmony_ci		 */
39048c2ecf20Sopenharmony_ci		b43legacy_wireless_exit(dev, wl);
39058c2ecf20Sopenharmony_ci}
39068c2ecf20Sopenharmony_ci
39078c2ecf20Sopenharmony_ci/* Perform a hardware reset. This can be called from any context. */
39088c2ecf20Sopenharmony_civoid b43legacy_controller_restart(struct b43legacy_wldev *dev,
39098c2ecf20Sopenharmony_ci				  const char *reason)
39108c2ecf20Sopenharmony_ci{
39118c2ecf20Sopenharmony_ci	/* Must avoid requeueing, if we are in shutdown. */
39128c2ecf20Sopenharmony_ci	if (b43legacy_status(dev) < B43legacy_STAT_INITIALIZED)
39138c2ecf20Sopenharmony_ci		return;
39148c2ecf20Sopenharmony_ci	b43legacyinfo(dev->wl, "Controller RESET (%s) ...\n", reason);
39158c2ecf20Sopenharmony_ci	ieee80211_queue_work(dev->wl->hw, &dev->restart_work);
39168c2ecf20Sopenharmony_ci}
39178c2ecf20Sopenharmony_ci
39188c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
39198c2ecf20Sopenharmony_ci
39208c2ecf20Sopenharmony_cistatic int b43legacy_suspend(struct ssb_device *dev, pm_message_t state)
39218c2ecf20Sopenharmony_ci{
39228c2ecf20Sopenharmony_ci	struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
39238c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = wldev->wl;
39248c2ecf20Sopenharmony_ci
39258c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Suspending...\n");
39268c2ecf20Sopenharmony_ci
39278c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
39288c2ecf20Sopenharmony_ci	wldev->suspend_init_status = b43legacy_status(wldev);
39298c2ecf20Sopenharmony_ci	if (wldev->suspend_init_status >= B43legacy_STAT_STARTED)
39308c2ecf20Sopenharmony_ci		b43legacy_wireless_core_stop(wldev);
39318c2ecf20Sopenharmony_ci	if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED)
39328c2ecf20Sopenharmony_ci		b43legacy_wireless_core_exit(wldev);
39338c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
39348c2ecf20Sopenharmony_ci
39358c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Device suspended.\n");
39368c2ecf20Sopenharmony_ci
39378c2ecf20Sopenharmony_ci	return 0;
39388c2ecf20Sopenharmony_ci}
39398c2ecf20Sopenharmony_ci
39408c2ecf20Sopenharmony_cistatic int b43legacy_resume(struct ssb_device *dev)
39418c2ecf20Sopenharmony_ci{
39428c2ecf20Sopenharmony_ci	struct b43legacy_wldev *wldev = ssb_get_drvdata(dev);
39438c2ecf20Sopenharmony_ci	struct b43legacy_wl *wl = wldev->wl;
39448c2ecf20Sopenharmony_ci	int err = 0;
39458c2ecf20Sopenharmony_ci
39468c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Resuming...\n");
39478c2ecf20Sopenharmony_ci
39488c2ecf20Sopenharmony_ci	mutex_lock(&wl->mutex);
39498c2ecf20Sopenharmony_ci	if (wldev->suspend_init_status >= B43legacy_STAT_INITIALIZED) {
39508c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_init(wldev);
39518c2ecf20Sopenharmony_ci		if (err) {
39528c2ecf20Sopenharmony_ci			b43legacyerr(wl, "Resume failed at core init\n");
39538c2ecf20Sopenharmony_ci			goto out;
39548c2ecf20Sopenharmony_ci		}
39558c2ecf20Sopenharmony_ci	}
39568c2ecf20Sopenharmony_ci	if (wldev->suspend_init_status >= B43legacy_STAT_STARTED) {
39578c2ecf20Sopenharmony_ci		err = b43legacy_wireless_core_start(wldev);
39588c2ecf20Sopenharmony_ci		if (err) {
39598c2ecf20Sopenharmony_ci			b43legacy_wireless_core_exit(wldev);
39608c2ecf20Sopenharmony_ci			b43legacyerr(wl, "Resume failed at core start\n");
39618c2ecf20Sopenharmony_ci			goto out;
39628c2ecf20Sopenharmony_ci		}
39638c2ecf20Sopenharmony_ci	}
39648c2ecf20Sopenharmony_ci
39658c2ecf20Sopenharmony_ci	b43legacydbg(wl, "Device resumed.\n");
39668c2ecf20Sopenharmony_ciout:
39678c2ecf20Sopenharmony_ci	mutex_unlock(&wl->mutex);
39688c2ecf20Sopenharmony_ci	return err;
39698c2ecf20Sopenharmony_ci}
39708c2ecf20Sopenharmony_ci
39718c2ecf20Sopenharmony_ci#else	/* CONFIG_PM */
39728c2ecf20Sopenharmony_ci# define b43legacy_suspend	NULL
39738c2ecf20Sopenharmony_ci# define b43legacy_resume		NULL
39748c2ecf20Sopenharmony_ci#endif	/* CONFIG_PM */
39758c2ecf20Sopenharmony_ci
39768c2ecf20Sopenharmony_cistatic struct ssb_driver b43legacy_ssb_driver = {
39778c2ecf20Sopenharmony_ci	.name		= KBUILD_MODNAME,
39788c2ecf20Sopenharmony_ci	.id_table	= b43legacy_ssb_tbl,
39798c2ecf20Sopenharmony_ci	.probe		= b43legacy_probe,
39808c2ecf20Sopenharmony_ci	.remove		= b43legacy_remove,
39818c2ecf20Sopenharmony_ci	.suspend	= b43legacy_suspend,
39828c2ecf20Sopenharmony_ci	.resume		= b43legacy_resume,
39838c2ecf20Sopenharmony_ci};
39848c2ecf20Sopenharmony_ci
39858c2ecf20Sopenharmony_cistatic void b43legacy_print_driverinfo(void)
39868c2ecf20Sopenharmony_ci{
39878c2ecf20Sopenharmony_ci	const char *feat_pci = "", *feat_leds = "",
39888c2ecf20Sopenharmony_ci		   *feat_pio = "", *feat_dma = "";
39898c2ecf20Sopenharmony_ci
39908c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_PCI_AUTOSELECT
39918c2ecf20Sopenharmony_ci	feat_pci = "P";
39928c2ecf20Sopenharmony_ci#endif
39938c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_LEDS
39948c2ecf20Sopenharmony_ci	feat_leds = "L";
39958c2ecf20Sopenharmony_ci#endif
39968c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_PIO
39978c2ecf20Sopenharmony_ci	feat_pio = "I";
39988c2ecf20Sopenharmony_ci#endif
39998c2ecf20Sopenharmony_ci#ifdef CONFIG_B43LEGACY_DMA
40008c2ecf20Sopenharmony_ci	feat_dma = "D";
40018c2ecf20Sopenharmony_ci#endif
40028c2ecf20Sopenharmony_ci	printk(KERN_INFO "Broadcom 43xx-legacy driver loaded "
40038c2ecf20Sopenharmony_ci	       "[ Features: %s%s%s%s ]\n",
40048c2ecf20Sopenharmony_ci	       feat_pci, feat_leds, feat_pio, feat_dma);
40058c2ecf20Sopenharmony_ci}
40068c2ecf20Sopenharmony_ci
40078c2ecf20Sopenharmony_cistatic int __init b43legacy_init(void)
40088c2ecf20Sopenharmony_ci{
40098c2ecf20Sopenharmony_ci	int err;
40108c2ecf20Sopenharmony_ci
40118c2ecf20Sopenharmony_ci	b43legacy_debugfs_init();
40128c2ecf20Sopenharmony_ci
40138c2ecf20Sopenharmony_ci	err = ssb_driver_register(&b43legacy_ssb_driver);
40148c2ecf20Sopenharmony_ci	if (err)
40158c2ecf20Sopenharmony_ci		goto err_dfs_exit;
40168c2ecf20Sopenharmony_ci
40178c2ecf20Sopenharmony_ci	b43legacy_print_driverinfo();
40188c2ecf20Sopenharmony_ci
40198c2ecf20Sopenharmony_ci	return err;
40208c2ecf20Sopenharmony_ci
40218c2ecf20Sopenharmony_cierr_dfs_exit:
40228c2ecf20Sopenharmony_ci	b43legacy_debugfs_exit();
40238c2ecf20Sopenharmony_ci	return err;
40248c2ecf20Sopenharmony_ci}
40258c2ecf20Sopenharmony_ci
40268c2ecf20Sopenharmony_cistatic void __exit b43legacy_exit(void)
40278c2ecf20Sopenharmony_ci{
40288c2ecf20Sopenharmony_ci	ssb_driver_unregister(&b43legacy_ssb_driver);
40298c2ecf20Sopenharmony_ci	b43legacy_debugfs_exit();
40308c2ecf20Sopenharmony_ci}
40318c2ecf20Sopenharmony_ci
40328c2ecf20Sopenharmony_cimodule_init(b43legacy_init)
40338c2ecf20Sopenharmony_cimodule_exit(b43legacy_exit)
4034