1184610Salfred/*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
2184610Salfred/*	$NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $	*/
3184610Salfred/*	$FreeBSD: stable/10/sys/dev/usb/wlan/if_zyd.c 343760 2019-02-05 03:01:10Z avos $	*/
4184610Salfred
5184610Salfred/*-
6184610Salfred * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
7184610Salfred * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
8184610Salfred *
9184610Salfred * Permission to use, copy, modify, and distribute this software for any
10184610Salfred * purpose with or without fee is hereby granted, provided that the above
11184610Salfred * copyright notice and this permission notice appear in all copies.
12184610Salfred *
13184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20184610Salfred */
21184610Salfred
22184610Salfred#include <sys/cdefs.h>
23184610Salfred__FBSDID("$FreeBSD: stable/10/sys/dev/usb/wlan/if_zyd.c 343760 2019-02-05 03:01:10Z avos $");
24184610Salfred
25184610Salfred/*
26188417Sthompsa * ZyDAS ZD1211/ZD1211B USB WLAN driver.
27184610Salfred */
28184610Salfred
29191746Sthompsa#include <sys/param.h>
30191746Sthompsa#include <sys/sockio.h>
31191746Sthompsa#include <sys/sysctl.h>
32191746Sthompsa#include <sys/lock.h>
33191746Sthompsa#include <sys/mutex.h>
34194677Sthompsa#include <sys/condvar.h>
35191746Sthompsa#include <sys/mbuf.h>
36191746Sthompsa#include <sys/kernel.h>
37191746Sthompsa#include <sys/socket.h>
38191746Sthompsa#include <sys/systm.h>
39191746Sthompsa#include <sys/malloc.h>
40191746Sthompsa#include <sys/module.h>
41191746Sthompsa#include <sys/bus.h>
42191746Sthompsa#include <sys/endian.h>
43191746Sthompsa#include <sys/kdb.h>
44191746Sthompsa
45191746Sthompsa#include <net/bpf.h>
46191746Sthompsa#include <net/if.h>
47191746Sthompsa#include <net/if_arp.h>
48191746Sthompsa#include <net/ethernet.h>
49191746Sthompsa#include <net/if_dl.h>
50191746Sthompsa#include <net/if_media.h>
51191746Sthompsa#include <net/if_types.h>
52191746Sthompsa
53191746Sthompsa#ifdef INET
54191746Sthompsa#include <netinet/in.h>
55191746Sthompsa#include <netinet/in_systm.h>
56191746Sthompsa#include <netinet/in_var.h>
57191746Sthompsa#include <netinet/if_ether.h>
58191746Sthompsa#include <netinet/ip.h>
59191746Sthompsa#endif
60191746Sthompsa
61191746Sthompsa#include <net80211/ieee80211_var.h>
62191746Sthompsa#include <net80211/ieee80211_regdomain.h>
63191746Sthompsa#include <net80211/ieee80211_radiotap.h>
64206358Srpaulo#include <net80211/ieee80211_ratectl.h>
65191746Sthompsa
66188942Sthompsa#include <dev/usb/usb.h>
67194677Sthompsa#include <dev/usb/usbdi.h>
68194677Sthompsa#include <dev/usb/usbdi_util.h>
69191746Sthompsa#include "usbdevs.h"
70184610Salfred
71188942Sthompsa#include <dev/usb/wlan/if_zydreg.h>
72188942Sthompsa#include <dev/usb/wlan/if_zydfw.h>
73184610Salfred
74207077Sthompsa#ifdef USB_DEBUG
75184610Salfredstatic int zyd_debug = 0;
76184610Salfred
77227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
78192502SthompsaSYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
79184610Salfred    "zyd debug level");
80188417Sthompsa
81188417Sthompsaenum {
82188417Sthompsa	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
83188417Sthompsa	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
84188417Sthompsa	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
85188417Sthompsa	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
86188417Sthompsa	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
87188417Sthompsa	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
88188417Sthompsa	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
89188417Sthompsa	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
90188417Sthompsa	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
91188417Sthompsa	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
92188417Sthompsa	ZYD_DEBUG_ANY		= 0xffffffff
93188417Sthompsa};
94188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
95188419Sthompsa	if (zyd_debug & (m))					\
96188417Sthompsa		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
97188417Sthompsa} while (0)
98188417Sthompsa#else
99188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
100188417Sthompsa	(void) sc;						\
101188417Sthompsa} while (0)
102184610Salfred#endif
103184610Salfred
104188419Sthompsa#define	zyd_do_request(sc,req,data) \
105194228Sthompsa    usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
106188419Sthompsa
107188417Sthompsastatic device_probe_t zyd_match;
108184610Salfredstatic device_attach_t zyd_attach;
109184610Salfredstatic device_detach_t zyd_detach;
110184610Salfred
111193045Sthompsastatic usb_callback_t zyd_intr_read_callback;
112193045Sthompsastatic usb_callback_t zyd_intr_write_callback;
113193045Sthompsastatic usb_callback_t zyd_bulk_read_callback;
114193045Sthompsastatic usb_callback_t zyd_bulk_write_callback;
115184610Salfred
116185948Sthompsastatic struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
117228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
118228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
119228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
120185948Sthompsastatic void	zyd_vap_delete(struct ieee80211vap *);
121188417Sthompsastatic void	zyd_tx_free(struct zyd_tx_data *, int);
122188419Sthompsastatic void	zyd_setup_tx_list(struct zyd_softc *);
123188419Sthompsastatic void	zyd_unsetup_tx_list(struct zyd_softc *);
124188417Sthompsastatic int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
125188417Sthompsastatic int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
126188601Sthompsa		    void *, int, int);
127188417Sthompsastatic int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
128188417Sthompsastatic int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
129188417Sthompsastatic int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
130188417Sthompsastatic int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
131188417Sthompsastatic int	zyd_rfwrite(struct zyd_softc *, uint32_t);
132188417Sthompsastatic int	zyd_lock_phy(struct zyd_softc *);
133188417Sthompsastatic int	zyd_unlock_phy(struct zyd_softc *);
134188417Sthompsastatic int	zyd_rf_attach(struct zyd_softc *, uint8_t);
135188417Sthompsastatic const char *zyd_rf_name(uint8_t);
136188417Sthompsastatic int	zyd_hw_init(struct zyd_softc *);
137188417Sthompsastatic int	zyd_read_pod(struct zyd_softc *);
138188417Sthompsastatic int	zyd_read_eeprom(struct zyd_softc *);
139188417Sthompsastatic int	zyd_get_macaddr(struct zyd_softc *);
140188417Sthompsastatic int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
141188417Sthompsastatic int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
142188417Sthompsastatic int	zyd_switch_radio(struct zyd_softc *, int);
143188417Sthompsastatic int	zyd_set_led(struct zyd_softc *, int, int);
144188417Sthompsastatic void	zyd_set_multi(struct zyd_softc *);
145188417Sthompsastatic void	zyd_update_mcast(struct ifnet *);
146188417Sthompsastatic int	zyd_set_rxfilter(struct zyd_softc *);
147188417Sthompsastatic void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
148188417Sthompsastatic int	zyd_set_beacon_interval(struct zyd_softc *, int);
149192984Sthompsastatic void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
150193803Sweongyostatic int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
151185948Sthompsa		    struct ieee80211_node *);
152188417Sthompsastatic void	zyd_start(struct ifnet *);
153188417Sthompsastatic int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
154185948Sthompsa		    const struct ieee80211_bpf_params *);
155188417Sthompsastatic int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
156191746Sthompsastatic void	zyd_init_locked(struct zyd_softc *);
157188417Sthompsastatic void	zyd_init(void *);
158191746Sthompsastatic void	zyd_stop(struct zyd_softc *);
159188417Sthompsastatic int	zyd_loadfirmware(struct zyd_softc *);
160188417Sthompsastatic void	zyd_scan_start(struct ieee80211com *);
161188417Sthompsastatic void	zyd_scan_end(struct ieee80211com *);
162188417Sthompsastatic void	zyd_set_channel(struct ieee80211com *);
163188417Sthompsastatic int	zyd_rfmd_init(struct zyd_rf *);
164188417Sthompsastatic int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
165188417Sthompsastatic int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
166188417Sthompsastatic int	zyd_al2230_init(struct zyd_rf *);
167188417Sthompsastatic int	zyd_al2230_switch_radio(struct zyd_rf *, int);
168188417Sthompsastatic int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
169188417Sthompsastatic int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
170188417Sthompsastatic int	zyd_al2230_init_b(struct zyd_rf *);
171188417Sthompsastatic int	zyd_al7230B_init(struct zyd_rf *);
172188417Sthompsastatic int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
173188417Sthompsastatic int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
174188417Sthompsastatic int	zyd_al2210_init(struct zyd_rf *);
175188417Sthompsastatic int	zyd_al2210_switch_radio(struct zyd_rf *, int);
176188417Sthompsastatic int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
177188417Sthompsastatic int	zyd_gct_init(struct zyd_rf *);
178188417Sthompsastatic int	zyd_gct_switch_radio(struct zyd_rf *, int);
179188417Sthompsastatic int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
180193420Sweongyostatic int	zyd_gct_mode(struct zyd_rf *);
181193420Sweongyostatic int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
182193420Sweongyostatic int	zyd_gct_write(struct zyd_rf *, uint16_t);
183193420Sweongyostatic int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
184188417Sthompsastatic int	zyd_maxim2_init(struct zyd_rf *);
185188417Sthompsastatic int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
186188417Sthompsastatic int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
187184610Salfred
188184610Salfredstatic const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
189184610Salfredstatic const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
190184610Salfred
191184610Salfred/* various supported device vendors/products */
192188417Sthompsa#define ZYD_ZD1211	0
193188417Sthompsa#define ZYD_ZD1211B	1
194184610Salfred
195193419Sweongyo#define	ZYD_ZD1211_DEV(v,p)	\
196193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
197193419Sweongyo#define	ZYD_ZD1211B_DEV(v,p)	\
198193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
199223486Shselaskystatic const STRUCT_USB_HOST_ID zyd_devs[] = {
200193419Sweongyo	/* ZYD_ZD1211 */
201193419Sweongyo	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
202193419Sweongyo	ZYD_ZD1211_DEV(ABOCOM, WL54),
203193419Sweongyo	ZYD_ZD1211_DEV(ASUS, WL159G),
204193419Sweongyo	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
205193419Sweongyo	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
206193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
207193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
208193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
209193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
210193419Sweongyo	ZYD_ZD1211_DEV(SAGEM, XG760A),
211193419Sweongyo	ZYD_ZD1211_DEV(SENAO, NUB8301),
212193419Sweongyo	ZYD_ZD1211_DEV(SITECOMEU, WL113),
213193419Sweongyo	ZYD_ZD1211_DEV(SWEEX, ZD1211),
214193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
215193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
216193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
217193419Sweongyo	ZYD_ZD1211_DEV(TWINMOS, G240),
218193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
219193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
220193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
221193419Sweongyo	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
222193419Sweongyo	ZYD_ZD1211_DEV(ZCOM, ZD1211),
223193419Sweongyo	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
224193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, AG225H),
225193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
226193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, G200V2),
227193419Sweongyo	/* ZYD_ZD1211B */
228223566Sgavin	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
229193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
230193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
231193419Sweongyo	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
232193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
233193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
234193419Sweongyo	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
235193419Sweongyo	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
236193419Sweongyo	ZYD_ZD1211B_DEV(MELCO, KG54L),
237193419Sweongyo	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
238193419Sweongyo	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
239193419Sweongyo	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
240193419Sweongyo	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
241193419Sweongyo	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
242193419Sweongyo	ZYD_ZD1211B_DEV(USR, USR5423),
243193419Sweongyo	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
244193419Sweongyo	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
245193419Sweongyo	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
246193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, M202),
247193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G202),
248193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
249184610Salfred};
250184610Salfred
251192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
252188417Sthompsa	[ZYD_BULK_WR] = {
253184610Salfred		.type = UE_BULK,
254184610Salfred		.endpoint = UE_ADDR_ANY,
255184610Salfred		.direction = UE_DIR_OUT,
256190734Sthompsa		.bufsize = ZYD_MAX_TXBUFSZ,
257190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
258190734Sthompsa		.callback = zyd_bulk_write_callback,
259184610Salfred		.ep_index = 0,
260190734Sthompsa		.timeout = 10000,	/* 10 seconds */
261184610Salfred	},
262188417Sthompsa	[ZYD_BULK_RD] = {
263184610Salfred		.type = UE_BULK,
264184610Salfred		.endpoint = UE_ADDR_ANY,
265184610Salfred		.direction = UE_DIR_IN,
266190734Sthompsa		.bufsize = ZYX_MAX_RXBUFSZ,
267190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
268190734Sthompsa		.callback = zyd_bulk_read_callback,
269184610Salfred		.ep_index = 0,
270184610Salfred	},
271188417Sthompsa	[ZYD_INTR_WR] = {
272184610Salfred		.type = UE_BULK_INTR,
273184610Salfred		.endpoint = UE_ADDR_ANY,
274184610Salfred		.direction = UE_DIR_OUT,
275190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
276190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
277190734Sthompsa		.callback = zyd_intr_write_callback,
278190734Sthompsa		.timeout = 1000,	/* 1 second */
279184610Salfred		.ep_index = 1,
280184610Salfred	},
281188417Sthompsa	[ZYD_INTR_RD] = {
282188417Sthompsa		.type = UE_INTERRUPT,
283184610Salfred		.endpoint = UE_ADDR_ANY,
284184610Salfred		.direction = UE_DIR_IN,
285190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
286190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
287190734Sthompsa		.callback = zyd_intr_read_callback,
288184610Salfred	},
289184610Salfred};
290188417Sthompsa#define zyd_read16_m(sc, val, data)	do {				\
291188417Sthompsa	error = zyd_read16(sc, val, data);				\
292188417Sthompsa	if (error != 0)							\
293188417Sthompsa		goto fail;						\
294188417Sthompsa} while (0)
295188417Sthompsa#define zyd_write16_m(sc, val, data)	do {				\
296188417Sthompsa	error = zyd_write16(sc, val, data);				\
297188417Sthompsa	if (error != 0)							\
298188417Sthompsa		goto fail;						\
299188417Sthompsa} while (0)
300188417Sthompsa#define zyd_read32_m(sc, val, data)	do {				\
301188417Sthompsa	error = zyd_read32(sc, val, data);				\
302188417Sthompsa	if (error != 0)							\
303188417Sthompsa		goto fail;						\
304188417Sthompsa} while (0)
305188417Sthompsa#define zyd_write32_m(sc, val, data)	do {				\
306188417Sthompsa	error = zyd_write32(sc, val, data);				\
307188417Sthompsa	if (error != 0)							\
308188417Sthompsa		goto fail;						\
309188417Sthompsa} while (0)
310184610Salfred
311188417Sthompsastatic int
312188417Sthompsazyd_match(device_t dev)
313184610Salfred{
314192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
315184610Salfred
316192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
317188417Sthompsa		return (ENXIO);
318188419Sthompsa	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
319188417Sthompsa		return (ENXIO);
320188417Sthompsa	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
321188417Sthompsa		return (ENXIO);
322184610Salfred
323194228Sthompsa	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
324184610Salfred}
325184610Salfred
326188417Sthompsastatic int
327188417Sthompsazyd_attach(device_t dev)
328184610Salfred{
329192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
330188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
331191746Sthompsa	struct ifnet *ifp;
332191746Sthompsa	struct ieee80211com *ic;
333191746Sthompsa	uint8_t iface_index, bands;
334188419Sthompsa	int error;
335184610Salfred
336188417Sthompsa	if (uaa->info.bcdDevice < 0x4330) {
337188417Sthompsa		device_printf(dev, "device version mismatch: 0x%X "
338188417Sthompsa		    "(only >= 43.30 supported)\n",
339188417Sthompsa		    uaa->info.bcdDevice);
340188417Sthompsa		return (EINVAL);
341184610Salfred	}
342184610Salfred
343194228Sthompsa	device_set_usb_desc(dev);
344188417Sthompsa	sc->sc_dev = dev;
345188417Sthompsa	sc->sc_udev = uaa->device;
346188417Sthompsa	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
347188419Sthompsa
348188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
349188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
350188417Sthompsa	STAILQ_INIT(&sc->sc_rqh);
351184610Salfred
352188417Sthompsa	iface_index = ZYD_IFACE_INDEX;
353194228Sthompsa	error = usbd_transfer_setup(uaa->device,
354188417Sthompsa	    &iface_index, sc->sc_xfer, zyd_config,
355188417Sthompsa	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
356188417Sthompsa	if (error) {
357188417Sthompsa		device_printf(dev, "could not allocate USB transfers, "
358194228Sthompsa		    "err=%s\n", usbd_errstr(error));
359188419Sthompsa		goto detach;
360184610Salfred	}
361184610Salfred
362188419Sthompsa	ZYD_LOCK(sc);
363188417Sthompsa	if ((error = zyd_get_macaddr(sc)) != 0) {
364188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
365191746Sthompsa		ZYD_UNLOCK(sc);
366191746Sthompsa		goto detach;
367188417Sthompsa	}
368188419Sthompsa	ZYD_UNLOCK(sc);
369188419Sthompsa
370188417Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
371188417Sthompsa	if (ifp == NULL) {
372188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
373191746Sthompsa		goto detach;
374184610Salfred	}
375188417Sthompsa	ifp->if_softc = sc;
376188417Sthompsa	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
377188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
378188417Sthompsa	ifp->if_init = zyd_init;
379188417Sthompsa	ifp->if_ioctl = zyd_ioctl;
380188417Sthompsa	ifp->if_start = zyd_start;
381207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
382188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
383184610Salfred
384188417Sthompsa	ic = ifp->if_l2com;
385188417Sthompsa	ic->ic_ifp = ifp;
386188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
387188417Sthompsa	ic->ic_opmode = IEEE80211_M_STA;
388184610Salfred
389188417Sthompsa	/* set device capabilities */
390188417Sthompsa	ic->ic_caps =
391188417Sthompsa		  IEEE80211_C_STA		/* station mode */
392188417Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode */
393188417Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
394188417Sthompsa	        | IEEE80211_C_SHSLOT		/* short slot time supported */
395188417Sthompsa		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
396188417Sthompsa	        | IEEE80211_C_WPA		/* 802.11i */
397188417Sthompsa		;
398184610Salfred
399188417Sthompsa	bands = 0;
400188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
401188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
402188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
403184610Salfred
404190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
405188417Sthompsa	ic->ic_raw_xmit = zyd_raw_xmit;
406188417Sthompsa	ic->ic_scan_start = zyd_scan_start;
407188417Sthompsa	ic->ic_scan_end = zyd_scan_end;
408188417Sthompsa	ic->ic_set_channel = zyd_set_channel;
409184610Salfred
410188417Sthompsa	ic->ic_vap_create = zyd_vap_create;
411188417Sthompsa	ic->ic_vap_delete = zyd_vap_delete;
412188417Sthompsa	ic->ic_update_mcast = zyd_update_mcast;
413188601Sthompsa	ic->ic_update_promisc = zyd_update_mcast;
414184610Salfred
415192468Ssam	ieee80211_radiotap_attach(ic,
416192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
417192468Ssam		ZYD_TX_RADIOTAP_PRESENT,
418192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
419192468Ssam		ZYD_RX_RADIOTAP_PRESENT);
420184610Salfred
421188417Sthompsa	if (bootverbose)
422188417Sthompsa		ieee80211_announce(ic);
423184610Salfred
424191746Sthompsa	return (0);
425191746Sthompsa
426191746Sthompsadetach:
427191746Sthompsa	zyd_detach(dev);
428191746Sthompsa	return (ENXIO);			/* failure */
429184610Salfred}
430184610Salfred
431188417Sthompsastatic int
432188417Sthompsazyd_detach(device_t dev)
433184610Salfred{
434188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
435188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
436188601Sthompsa	struct ieee80211com *ic;
437246614Shselasky	unsigned int x;
438184610Salfred
439246614Shselasky	/*
440246614Shselasky	 * Prevent further allocations from RX/TX data
441246614Shselasky	 * lists and ioctls:
442246614Shselasky	 */
443246614Shselasky	ZYD_LOCK(sc);
444246614Shselasky	sc->sc_flags |= ZYD_FLAG_DETACHED;
445246614Shselasky	STAILQ_INIT(&sc->tx_q);
446246614Shselasky	STAILQ_INIT(&sc->tx_free);
447246614Shselasky	ZYD_UNLOCK(sc);
448184610Salfred
449246614Shselasky	/* drain USB transfers */
450246614Shselasky	for (x = 0; x != ZYD_N_TRANSFER; x++)
451246614Shselasky		usbd_transfer_drain(sc->sc_xfer[x]);
452246614Shselasky
453188419Sthompsa	/* free TX list, if any */
454246614Shselasky	ZYD_LOCK(sc);
455188419Sthompsa	zyd_unsetup_tx_list(sc);
456246614Shselasky	ZYD_UNLOCK(sc);
457188419Sthompsa
458246614Shselasky	/* free USB transfers and some data buffers */
459246614Shselasky	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
460246614Shselasky
461188417Sthompsa	if (ifp) {
462188601Sthompsa		ic = ifp->if_l2com;
463188417Sthompsa		ieee80211_ifdetach(ic);
464188417Sthompsa		if_free(ifp);
465184610Salfred	}
466188417Sthompsa	mtx_destroy(&sc->sc_mtx);
467184610Salfred
468188417Sthompsa	return (0);
469184610Salfred}
470184610Salfred
471188417Sthompsastatic struct ieee80211vap *
472228621Sbschmidtzyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
473228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
474228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
475228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
476184610Salfred{
477188417Sthompsa	struct zyd_vap *zvp;
478188417Sthompsa	struct ieee80211vap *vap;
479184610Salfred
480188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
481188417Sthompsa		return (NULL);
482188417Sthompsa	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
483188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
484188417Sthompsa	if (zvp == NULL)
485188417Sthompsa		return (NULL);
486188417Sthompsa	vap = &zvp->vap;
487259453Shselasky
488188417Sthompsa	/* enable s/w bmiss handling for sta mode */
489259453Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
490259453Shselasky	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
491259453Shselasky		/* out of memory */
492259453Shselasky		free(zvp, M_80211_VAP);
493259453Shselasky		return (NULL);
494259453Shselasky	}
495184610Salfred
496188417Sthompsa	/* override state transition machine */
497188417Sthompsa	zvp->newstate = vap->iv_newstate;
498188417Sthompsa	vap->iv_newstate = zyd_newstate;
499184610Salfred
500206358Srpaulo	ieee80211_ratectl_init(vap);
501206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
502184610Salfred
503188417Sthompsa	/* complete setup */
504188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change,
505188417Sthompsa	    ieee80211_media_status);
506188417Sthompsa	ic->ic_opmode = opmode;
507188417Sthompsa	return (vap);
508184610Salfred}
509184610Salfred
510184610Salfredstatic void
511188417Sthompsazyd_vap_delete(struct ieee80211vap *vap)
512184610Salfred{
513188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
514184610Salfred
515206358Srpaulo	ieee80211_ratectl_deinit(vap);
516188417Sthompsa	ieee80211_vap_detach(vap);
517188417Sthompsa	free(zvp, M_80211_VAP);
518184610Salfred}
519184610Salfred
520184610Salfredstatic void
521188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr)
522184610Salfred{
523188417Sthompsa	struct zyd_softc *sc = data->sc;
524184610Salfred
525188417Sthompsa	if (data->m != NULL) {
526188417Sthompsa		if (data->m->m_flags & M_TXCB)
527188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
528188417Sthompsa			    txerr ? ETIMEDOUT : 0);
529188417Sthompsa		m_freem(data->m);
530188417Sthompsa		data->m = NULL;
531188417Sthompsa
532188417Sthompsa		ieee80211_free_node(data->ni);
533188417Sthompsa		data->ni = NULL;
534184610Salfred	}
535188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
536188417Sthompsa	sc->tx_nfree++;
537184610Salfred}
538184610Salfred
539188419Sthompsastatic void
540188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc)
541184610Salfred{
542188417Sthompsa	struct zyd_tx_data *data;
543188417Sthompsa	int i;
544184610Salfred
545188417Sthompsa	sc->tx_nfree = 0;
546188417Sthompsa	STAILQ_INIT(&sc->tx_q);
547188417Sthompsa	STAILQ_INIT(&sc->tx_free);
548184610Salfred
549188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
550188417Sthompsa		data = &sc->tx_data[i];
551184610Salfred
552188417Sthompsa		data->sc = sc;
553188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
554188417Sthompsa		sc->tx_nfree++;
555188417Sthompsa	}
556184610Salfred}
557184610Salfred
558184610Salfredstatic void
559188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc)
560184610Salfred{
561188417Sthompsa	struct zyd_tx_data *data;
562188417Sthompsa	int i;
563184610Salfred
564188419Sthompsa	/* make sure any subsequent use of the queues will fail */
565188419Sthompsa	sc->tx_nfree = 0;
566188419Sthompsa	STAILQ_INIT(&sc->tx_q);
567188419Sthompsa	STAILQ_INIT(&sc->tx_free);
568184610Salfred
569188419Sthompsa	/* free up all node references and mbufs */
570188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
571188417Sthompsa		data = &sc->tx_data[i];
572184610Salfred
573188417Sthompsa		if (data->m != NULL) {
574188417Sthompsa			m_freem(data->m);
575188417Sthompsa			data->m = NULL;
576188417Sthompsa		}
577188417Sthompsa		if (data->ni != NULL) {
578188417Sthompsa			ieee80211_free_node(data->ni);
579188417Sthompsa			data->ni = NULL;
580188417Sthompsa		}
581188417Sthompsa	}
582184610Salfred}
583184610Salfred
584191746Sthompsastatic int
585191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
586186730Salfred{
587188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
588191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
589191746Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
590188417Sthompsa	int error;
591186730Salfred
592191746Sthompsa	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
593191746Sthompsa	    ieee80211_state_name[vap->iv_state],
594191746Sthompsa	    ieee80211_state_name[nstate]);
595191746Sthompsa
596191746Sthompsa	IEEE80211_UNLOCK(ic);
597191746Sthompsa	ZYD_LOCK(sc);
598191746Sthompsa	switch (nstate) {
599188417Sthompsa	case IEEE80211_S_AUTH:
600188417Sthompsa		zyd_set_chan(sc, ic->ic_curchan);
601188417Sthompsa		break;
602188417Sthompsa	case IEEE80211_S_RUN:
603188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_MONITOR)
604188417Sthompsa			break;
605184610Salfred
606188417Sthompsa		/* turn link LED on */
607188417Sthompsa		error = zyd_set_led(sc, ZYD_LED1, 1);
608188417Sthompsa		if (error != 0)
609191746Sthompsa			break;
610191746Sthompsa
611188417Sthompsa		/* make data LED blink upon Tx */
612188417Sthompsa		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
613191746Sthompsa
614212127Sthompsa		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
615188417Sthompsa		zyd_set_bssid(sc, sc->sc_bssid);
616188417Sthompsa		break;
617188417Sthompsa	default:
618188417Sthompsa		break;
619184610Salfred	}
620188417Sthompsafail:
621188417Sthompsa	ZYD_UNLOCK(sc);
622188417Sthompsa	IEEE80211_LOCK(ic);
623191746Sthompsa	return (zvp->newstate(vap, nstate, arg));
624184610Salfred}
625184610Salfred
626188417Sthompsa/*
627188417Sthompsa * Callback handler for interrupt transfer
628188417Sthompsa */
629184610Salfredstatic void
630194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
631184610Salfred{
632194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
633184610Salfred	struct ifnet *ifp = sc->sc_ifp;
634184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
635188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
636184610Salfred	struct ieee80211_node *ni;
637188417Sthompsa	struct zyd_cmd *cmd = &sc->sc_ibuf;
638194677Sthompsa	struct usb_page_cache *pc;
639188417Sthompsa	int datalen;
640194677Sthompsa	int actlen;
641184610Salfred
642194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
643194677Sthompsa
644184610Salfred	switch (USB_GET_STATE(xfer)) {
645184610Salfred	case USB_ST_TRANSFERRED:
646194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
647194677Sthompsa		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
648184610Salfred
649188417Sthompsa		switch (le16toh(cmd->code)) {
650188417Sthompsa		case ZYD_NOTIF_RETRYSTATUS:
651188417Sthompsa		{
652188417Sthompsa			struct zyd_notif_retry *retry =
653188417Sthompsa			    (struct zyd_notif_retry *)cmd->data;
654184610Salfred
655188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
656188417Sthompsa			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
657188417Sthompsa			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
658188417Sthompsa			    le16toh(retry->count)&0xff, le16toh(retry->count));
659184610Salfred
660188417Sthompsa			/*
661188417Sthompsa			 * Find the node to which the packet was sent and
662188417Sthompsa			 * update its retry statistics.  In BSS mode, this node
663188417Sthompsa			 * is the AP we're associated to so no lookup is
664188417Sthompsa			 * actually needed.
665188417Sthompsa			 */
666188417Sthompsa			ni = ieee80211_find_txnode(vap, retry->macaddr);
667188417Sthompsa			if (ni != NULL) {
668206358Srpaulo				int retrycnt =
669206358Srpaulo				    (int)(le16toh(retry->count) & 0xff);
670206358Srpaulo
671206358Srpaulo				ieee80211_ratectl_tx_complete(vap, ni,
672206358Srpaulo				    IEEE80211_RATECTL_TX_FAILURE,
673206358Srpaulo				    &retrycnt, NULL);
674188417Sthompsa				ieee80211_free_node(ni);
675188417Sthompsa			}
676188417Sthompsa			if (le16toh(retry->count) & 0x100)
677188417Sthompsa				ifp->if_oerrors++;	/* too many retries */
678188417Sthompsa			break;
679188417Sthompsa		}
680188417Sthompsa		case ZYD_NOTIF_IORD:
681188417Sthompsa		{
682188417Sthompsa			struct zyd_rq *rqp;
683184610Salfred
684188417Sthompsa			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
685188417Sthompsa				break;	/* HMAC interrupt */
686184610Salfred
687194677Sthompsa			datalen = actlen - sizeof(cmd->code);
688188417Sthompsa			datalen -= 2;	/* XXX: padding? */
689184610Salfred
690188417Sthompsa			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
691233774Shselasky				int i;
692233774Shselasky				int count;
693184610Salfred
694188417Sthompsa				if (rqp->olen != datalen)
695188417Sthompsa					continue;
696233774Shselasky				count = rqp->olen / sizeof(struct zyd_pair);
697233774Shselasky				for (i = 0; i < count; i++) {
698188417Sthompsa					if (*(((const uint16_t *)rqp->idata) + i) !=
699188417Sthompsa					    (((struct zyd_pair *)cmd->data) + i)->reg)
700188417Sthompsa						break;
701184610Salfred				}
702233774Shselasky				if (i != count)
703188417Sthompsa					continue;
704188417Sthompsa				/* copy answer into caller-supplied buffer */
705227461Shselasky				memcpy(rqp->odata, cmd->data, rqp->olen);
706188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_CMD,
707188417Sthompsa				    "command %p complete, data = %*D \n",
708228303Shselasky				    rqp, rqp->olen, (char *)rqp->odata, ":");
709188417Sthompsa				wakeup(rqp);	/* wakeup caller */
710188417Sthompsa				break;
711184610Salfred			}
712188417Sthompsa			if (rqp == NULL) {
713188417Sthompsa				device_printf(sc->sc_dev,
714188417Sthompsa				    "unexpected IORD notification %*D\n",
715188417Sthompsa				    datalen, cmd->data, ":");
716188417Sthompsa			}
717188417Sthompsa			break;
718184610Salfred		}
719188417Sthompsa		default:
720188417Sthompsa			device_printf(sc->sc_dev, "unknown notification %x\n",
721188417Sthompsa			    le16toh(cmd->code));
722188417Sthompsa		}
723184610Salfred
724188417Sthompsa		/* FALLTHROUGH */
725184610Salfred	case USB_ST_SETUP:
726184610Salfredtr_setup:
727194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
728194228Sthompsa		usbd_transfer_submit(xfer);
729188417Sthompsa		break;
730184610Salfred
731188417Sthompsa	default:			/* Error */
732188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
733194677Sthompsa		    usbd_errstr(error));
734188417Sthompsa
735194677Sthompsa		if (error != USB_ERR_CANCELLED) {
736188417Sthompsa			/* try to clear stall first */
737194677Sthompsa			usbd_xfer_set_stall(xfer);
738188417Sthompsa			goto tr_setup;
739184610Salfred		}
740188417Sthompsa		break;
741188417Sthompsa	}
742188417Sthompsa}
743184610Salfred
744188417Sthompsastatic void
745194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
746188417Sthompsa{
747194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
748194677Sthompsa	struct zyd_rq *rqp, *cmd;
749194677Sthompsa	struct usb_page_cache *pc;
750184610Salfred
751188417Sthompsa	switch (USB_GET_STATE(xfer)) {
752188417Sthompsa	case USB_ST_TRANSFERRED:
753194677Sthompsa		cmd = usbd_xfer_get_priv(xfer);
754194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
755189452Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
756189452Sthompsa			/* Ensure the cached rq pointer is still valid */
757194677Sthompsa			if (rqp == cmd &&
758189452Sthompsa			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
759189452Sthompsa				wakeup(rqp);	/* wakeup caller */
760189452Sthompsa		}
761184610Salfred
762188417Sthompsa		/* FALLTHROUGH */
763188417Sthompsa	case USB_ST_SETUP:
764188417Sthompsatr_setup:
765188417Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
766188417Sthompsa			if (rqp->flags & ZYD_CMD_FLAG_SENT)
767188417Sthompsa				continue;
768184610Salfred
769194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
770194677Sthompsa			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
771184610Salfred
772194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
773194677Sthompsa			usbd_xfer_set_priv(xfer, rqp);
774188417Sthompsa			rqp->flags |= ZYD_CMD_FLAG_SENT;
775194228Sthompsa			usbd_transfer_submit(xfer);
776188417Sthompsa			break;
777184610Salfred		}
778184610Salfred		break;
779184610Salfred
780184610Salfred	default:			/* Error */
781188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
782194677Sthompsa		    usbd_errstr(error));
783184610Salfred
784194677Sthompsa		if (error != USB_ERR_CANCELLED) {
785184610Salfred			/* try to clear stall first */
786194677Sthompsa			usbd_xfer_set_stall(xfer);
787188417Sthompsa			goto tr_setup;
788184610Salfred		}
789184610Salfred		break;
790184610Salfred	}
791184610Salfred}
792184610Salfred
793188417Sthompsastatic int
794188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
795188601Sthompsa    void *odata, int olen, int flags)
796184610Salfred{
797188417Sthompsa	struct zyd_cmd cmd;
798188417Sthompsa	struct zyd_rq rq;
799188417Sthompsa	int error;
800184610Salfred
801233774Shselasky	if (ilen > (int)sizeof(cmd.data))
802188417Sthompsa		return (EINVAL);
803188419Sthompsa
804188417Sthompsa	cmd.code = htole16(code);
805227461Shselasky	memcpy(cmd.data, idata, ilen);
806188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
807188417Sthompsa	    &rq, ilen, idata, ":");
808184610Salfred
809188417Sthompsa	rq.cmd = &cmd;
810188417Sthompsa	rq.idata = idata;
811188417Sthompsa	rq.odata = odata;
812188417Sthompsa	rq.ilen = sizeof(uint16_t) + ilen;
813188417Sthompsa	rq.olen = olen;
814188417Sthompsa	rq.flags = flags;
815188417Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
816194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
817194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
818184610Salfred
819188417Sthompsa	/* wait at most one second for command reply */
820188417Sthompsa	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
821188417Sthompsa	if (error)
822188417Sthompsa		device_printf(sc->sc_dev, "command timeout\n");
823188417Sthompsa	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
824188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
825188417Sthompsa	    &rq, error);
826184610Salfred
827188417Sthompsa	return (error);
828184610Salfred}
829184610Salfred
830184610Salfredstatic int
831188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
832184610Salfred{
833188417Sthompsa	struct zyd_pair tmp;
834188417Sthompsa	int error;
835184610Salfred
836188417Sthompsa	reg = htole16(reg);
837188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
838188417Sthompsa	    ZYD_CMD_FLAG_READ);
839188417Sthompsa	if (error == 0)
840188417Sthompsa		*val = le16toh(tmp.val);
841188417Sthompsa	return (error);
842184610Salfred}
843184610Salfred
844184610Salfredstatic int
845188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
846184610Salfred{
847188417Sthompsa	struct zyd_pair tmp[2];
848188417Sthompsa	uint16_t regs[2];
849184610Salfred	int error;
850184610Salfred
851188417Sthompsa	regs[0] = htole16(ZYD_REG32_HI(reg));
852188417Sthompsa	regs[1] = htole16(ZYD_REG32_LO(reg));
853188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
854188417Sthompsa	    ZYD_CMD_FLAG_READ);
855188417Sthompsa	if (error == 0)
856188417Sthompsa		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
857188417Sthompsa	return (error);
858188417Sthompsa}
859184610Salfred
860188417Sthompsastatic int
861188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
862188417Sthompsa{
863188417Sthompsa	struct zyd_pair pair;
864184610Salfred
865188417Sthompsa	pair.reg = htole16(reg);
866188417Sthompsa	pair.val = htole16(val);
867184610Salfred
868188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
869188417Sthompsa}
870184610Salfred
871188417Sthompsastatic int
872188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
873188417Sthompsa{
874188417Sthompsa	struct zyd_pair pair[2];
875184610Salfred
876188417Sthompsa	pair[0].reg = htole16(ZYD_REG32_HI(reg));
877188417Sthompsa	pair[0].val = htole16(val >> 16);
878188417Sthompsa	pair[1].reg = htole16(ZYD_REG32_LO(reg));
879188417Sthompsa	pair[1].val = htole16(val & 0xffff);
880184610Salfred
881188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
882184610Salfred}
883184610Salfred
884188417Sthompsastatic int
885188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val)
886184610Salfred{
887188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
888188417Sthompsa	struct zyd_rfwrite_cmd req;
889188417Sthompsa	uint16_t cr203;
890188417Sthompsa	int error, i;
891184610Salfred
892188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &cr203);
893188417Sthompsa	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
894188417Sthompsa
895188417Sthompsa	req.code  = htole16(2);
896188417Sthompsa	req.width = htole16(rf->width);
897188417Sthompsa	for (i = 0; i < rf->width; i++) {
898188417Sthompsa		req.bit[i] = htole16(cr203);
899188417Sthompsa		if (val & (1 << (rf->width - 1 - i)))
900188417Sthompsa			req.bit[i] |= htole16(ZYD_RF_DATA);
901188417Sthompsa	}
902188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
903188417Sthompsafail:
904188417Sthompsa	return (error);
905184610Salfred}
906184610Salfred
907188417Sthompsastatic int
908188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
909184610Salfred{
910188417Sthompsa	int error;
911184610Salfred
912188417Sthompsa	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
913188417Sthompsa	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
914188417Sthompsa	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
915188417Sthompsafail:
916188417Sthompsa	return (error);
917184610Salfred}
918184610Salfred
919188417Sthompsastatic int
920188417Sthompsazyd_lock_phy(struct zyd_softc *sc)
921184610Salfred{
922188417Sthompsa	int error;
923188417Sthompsa	uint32_t tmp;
924186730Salfred
925188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
926188417Sthompsa	tmp &= ~ZYD_UNLOCK_PHY_REGS;
927188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
928188417Sthompsafail:
929188417Sthompsa	return (error);
930184610Salfred}
931184610Salfred
932188417Sthompsastatic int
933188417Sthompsazyd_unlock_phy(struct zyd_softc *sc)
934184610Salfred{
935188417Sthompsa	int error;
936188417Sthompsa	uint32_t tmp;
937184610Salfred
938188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
939188417Sthompsa	tmp |= ZYD_UNLOCK_PHY_REGS;
940188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
941188417Sthompsafail:
942188417Sthompsa	return (error);
943184610Salfred}
944184610Salfred
945184610Salfred/*
946188417Sthompsa * RFMD RF methods.
947184610Salfred */
948188417Sthompsastatic int
949188417Sthompsazyd_rfmd_init(struct zyd_rf *rf)
950184610Salfred{
951233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
952188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
953184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
954184610Salfred	static const uint32_t rfini[] = ZYD_RFMD_RF;
955188417Sthompsa	int i, error;
956184610Salfred
957184610Salfred	/* init RF-dependent PHY registers */
958188417Sthompsa	for (i = 0; i < N(phyini); i++) {
959188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
960184610Salfred	}
961184610Salfred
962184610Salfred	/* init RFMD radio */
963188417Sthompsa	for (i = 0; i < N(rfini); i++) {
964188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
965188417Sthompsa			return (error);
966184610Salfred	}
967188417Sthompsafail:
968188417Sthompsa	return (error);
969188417Sthompsa#undef N
970184610Salfred}
971184610Salfred
972188417Sthompsastatic int
973188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
974184610Salfred{
975188417Sthompsa	int error;
976188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
977188417Sthompsa
978188417Sthompsa	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
979188417Sthompsa	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
980188417Sthompsafail:
981188417Sthompsa	return (error);
982184610Salfred}
983184610Salfred
984188417Sthompsastatic int
985188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
986184610Salfred{
987188417Sthompsa	int error;
988188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
989184610Salfred	static const struct {
990188417Sthompsa		uint32_t	r1, r2;
991188417Sthompsa	} rfprog[] = ZYD_RFMD_CHANTABLE;
992184610Salfred
993188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
994188417Sthompsa	if (error != 0)
995188417Sthompsa		goto fail;
996188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
997188417Sthompsa	if (error != 0)
998188417Sthompsa		goto fail;
999184610Salfred
1000188417Sthompsafail:
1001188417Sthompsa	return (error);
1002184610Salfred}
1003184610Salfred
1004184610Salfred/*
1005188417Sthompsa * AL2230 RF methods.
1006184610Salfred */
1007188417Sthompsastatic int
1008188417Sthompsazyd_al2230_init(struct zyd_rf *rf)
1009184610Salfred{
1010233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1011188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1012184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1013186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1014186730Salfred	static const struct zyd_phy_pair phypll[] = {
1015188417Sthompsa		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1016188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1017186730Salfred	};
1018186730Salfred	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1019186730Salfred	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1020186730Salfred	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1021188417Sthompsa	int i, error;
1022184610Salfred
1023184610Salfred	/* init RF-dependent PHY registers */
1024188417Sthompsa	for (i = 0; i < N(phyini); i++)
1025188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1026186730Salfred
1027188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1028188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1029188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1030184610Salfred	}
1031188417Sthompsa
1032186730Salfred	/* init AL2230 radio */
1033188417Sthompsa	for (i = 0; i < N(rfini1); i++) {
1034188417Sthompsa		error = zyd_rfwrite(sc, rfini1[i]);
1035188417Sthompsa		if (error != 0)
1036188417Sthompsa			goto fail;
1037188417Sthompsa	}
1038184610Salfred
1039188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1040188417Sthompsa		error = zyd_rfwrite(sc, 0x000824);
1041186730Salfred	else
1042188417Sthompsa		error = zyd_rfwrite(sc, 0x0005a4);
1043188417Sthompsa	if (error != 0)
1044188417Sthompsa		goto fail;
1045186730Salfred
1046188417Sthompsa	for (i = 0; i < N(rfini2); i++) {
1047188417Sthompsa		error = zyd_rfwrite(sc, rfini2[i]);
1048188417Sthompsa		if (error != 0)
1049188417Sthompsa			goto fail;
1050188417Sthompsa	}
1051186730Salfred
1052188417Sthompsa	for (i = 0; i < N(phypll); i++)
1053188417Sthompsa		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1054186730Salfred
1055188417Sthompsa	for (i = 0; i < N(rfini3); i++) {
1056188417Sthompsa		error = zyd_rfwrite(sc, rfini3[i]);
1057188417Sthompsa		if (error != 0)
1058188417Sthompsa			goto fail;
1059188417Sthompsa	}
1060188417Sthompsafail:
1061188417Sthompsa	return (error);
1062188417Sthompsa#undef N
1063184610Salfred}
1064184610Salfred
1065188417Sthompsastatic int
1066188417Sthompsazyd_al2230_fini(struct zyd_rf *rf)
1067186730Salfred{
1068233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1069188417Sthompsa	int error, i;
1070188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1071186730Salfred	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1072186730Salfred
1073188417Sthompsa	for (i = 0; i < N(phy); i++)
1074188417Sthompsa		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1075186730Salfred
1076186730Salfred	if (sc->sc_newphy != 0)
1077188417Sthompsa		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1078188417Sthompsa
1079188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x6);
1080188417Sthompsafail:
1081188417Sthompsa	return (error);
1082188417Sthompsa#undef N
1083186730Salfred}
1084186730Salfred
1085188417Sthompsastatic int
1086188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf)
1087184610Salfred{
1088233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1089188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1090186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1091186730Salfred	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1092186730Salfred	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1093186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1094188417Sthompsa	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1095186730Salfred	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1096186730Salfred	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1097186730Salfred	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1098186730Salfred	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1099188417Sthompsa	int i, error;
1100184610Salfred
1101188417Sthompsa	for (i = 0; i < N(phy1); i++)
1102188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1103186730Salfred
1104184610Salfred	/* init RF-dependent PHY registers */
1105188417Sthompsa	for (i = 0; i < N(phyini); i++)
1106188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1107184610Salfred
1108188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1109188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1110188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1111188417Sthompsa	}
1112186730Salfred
1113188417Sthompsa	for (i = 0; i < 3; i++) {
1114188417Sthompsa		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1115188417Sthompsa		if (error != 0)
1116188417Sthompsa			return (error);
1117188417Sthompsa	}
1118186730Salfred
1119188417Sthompsa	for (i = 0; i < N(rfini_part1); i++) {
1120188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1121188417Sthompsa		if (error != 0)
1122188417Sthompsa			return (error);
1123188417Sthompsa	}
1124186730Salfred
1125188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1126188417Sthompsa		error = zyd_rfwrite(sc, 0x241000);
1127186730Salfred	else
1128188417Sthompsa		error = zyd_rfwrite(sc, 0x25a000);
1129188417Sthompsa	if (error != 0)
1130188417Sthompsa		goto fail;
1131186730Salfred
1132188417Sthompsa	for (i = 0; i < N(rfini_part2); i++) {
1133188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1134188417Sthompsa		if (error != 0)
1135188417Sthompsa			return (error);
1136188417Sthompsa	}
1137186730Salfred
1138188417Sthompsa	for (i = 0; i < N(phy2); i++)
1139188417Sthompsa		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1140186730Salfred
1141188417Sthompsa	for (i = 0; i < N(rfini_part3); i++) {
1142188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1143188417Sthompsa		if (error != 0)
1144188417Sthompsa			return (error);
1145188417Sthompsa	}
1146186730Salfred
1147188417Sthompsa	for (i = 0; i < N(phy3); i++)
1148188417Sthompsa		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1149186730Salfred
1150188417Sthompsa	error = zyd_al2230_fini(rf);
1151188417Sthompsafail:
1152188417Sthompsa	return (error);
1153188417Sthompsa#undef N
1154184610Salfred}
1155184610Salfred
1156188417Sthompsastatic int
1157188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1158184610Salfred{
1159188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1160188417Sthompsa	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1161188417Sthompsa
1162188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1163188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1164188417Sthompsafail:
1165188417Sthompsa	return (error);
1166188417Sthompsa}
1167188417Sthompsa
1168188417Sthompsastatic int
1169188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1170188417Sthompsa{
1171233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1172188417Sthompsa	int error, i;
1173188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1174186730Salfred	static const struct zyd_phy_pair phy1[] = {
1175188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1176186730Salfred	};
1177184610Salfred	static const struct {
1178188417Sthompsa		uint32_t	r1, r2, r3;
1179188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE;
1180184610Salfred
1181188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1182188417Sthompsa	if (error != 0)
1183188417Sthompsa		goto fail;
1184188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1185188417Sthompsa	if (error != 0)
1186188417Sthompsa		goto fail;
1187188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1188188417Sthompsa	if (error != 0)
1189188417Sthompsa		goto fail;
1190184610Salfred
1191188417Sthompsa	for (i = 0; i < N(phy1); i++)
1192188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1193188417Sthompsafail:
1194188417Sthompsa	return (error);
1195188417Sthompsa#undef N
1196184610Salfred}
1197184610Salfred
1198188417Sthompsastatic int
1199188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1200186730Salfred{
1201233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1202188417Sthompsa	int error, i;
1203188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1204186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1205186730Salfred	static const struct {
1206188417Sthompsa		uint32_t	r1, r2, r3;
1207188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1208186730Salfred
1209188417Sthompsa	for (i = 0; i < N(phy1); i++)
1210188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1211186730Salfred
1212188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1213188417Sthompsa	if (error != 0)
1214188417Sthompsa		goto fail;
1215188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1216188417Sthompsa	if (error != 0)
1217188417Sthompsa		goto fail;
1218188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1219188417Sthompsa	if (error != 0)
1220188417Sthompsa		goto fail;
1221188417Sthompsa	error = zyd_al2230_fini(rf);
1222188417Sthompsafail:
1223188417Sthompsa	return (error);
1224188417Sthompsa#undef N
1225186730Salfred}
1226186730Salfred
1227186730Salfred#define	ZYD_AL2230_PHY_BANDEDGE6					\
1228186730Salfred{									\
1229188417Sthompsa	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1230186730Salfred	{ ZYD_CR47,  0x1e }						\
1231186730Salfred}
1232186730Salfred
1233188417Sthompsastatic int
1234188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1235186730Salfred{
1236233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1237188417Sthompsa	int error = 0, i;
1238188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1239188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1240188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1241186730Salfred	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1242188601Sthompsa	int chan = ieee80211_chan2ieee(ic, c);
1243186730Salfred
1244188417Sthompsa	if (chan == 1 || chan == 11)
1245186730Salfred		r[0].val = 0x12;
1246188417Sthompsa
1247188417Sthompsa	for (i = 0; i < N(r); i++)
1248188417Sthompsa		zyd_write16_m(sc, r[i].reg, r[i].val);
1249188417Sthompsafail:
1250188417Sthompsa	return (error);
1251188417Sthompsa#undef N
1252186730Salfred}
1253186730Salfred
1254184610Salfred/*
1255184610Salfred * AL7230B RF methods.
1256184610Salfred */
1257188417Sthompsastatic int
1258188417Sthompsazyd_al7230B_init(struct zyd_rf *rf)
1259184610Salfred{
1260233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1261188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1262184610Salfred	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1263184610Salfred	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1264184610Salfred	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1265184610Salfred	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1266184610Salfred	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1267188417Sthompsa	int i, error;
1268184610Salfred
1269184610Salfred	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1270184610Salfred
1271184610Salfred	/* init RF-dependent PHY registers, part one */
1272188417Sthompsa	for (i = 0; i < N(phyini_1); i++)
1273188417Sthompsa		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1274188417Sthompsa
1275184610Salfred	/* init AL7230B radio, part one */
1276188417Sthompsa	for (i = 0; i < N(rfini_1); i++) {
1277188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1278188417Sthompsa			return (error);
1279184610Salfred	}
1280184610Salfred	/* init RF-dependent PHY registers, part two */
1281188417Sthompsa	for (i = 0; i < N(phyini_2); i++)
1282188417Sthompsa		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1283188417Sthompsa
1284184610Salfred	/* init AL7230B radio, part two */
1285188417Sthompsa	for (i = 0; i < N(rfini_2); i++) {
1286188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1287188417Sthompsa			return (error);
1288184610Salfred	}
1289184610Salfred	/* init RF-dependent PHY registers, part three */
1290188417Sthompsa	for (i = 0; i < N(phyini_3); i++)
1291188417Sthompsa		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1292188417Sthompsafail:
1293188417Sthompsa	return (error);
1294188417Sthompsa#undef N
1295184610Salfred}
1296184610Salfred
1297188417Sthompsastatic int
1298188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1299184610Salfred{
1300188417Sthompsa	int error;
1301188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1302188417Sthompsa
1303188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1304188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1305188417Sthompsafail:
1306188417Sthompsa	return (error);
1307188417Sthompsa}
1308188417Sthompsa
1309188417Sthompsastatic int
1310188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1311188417Sthompsa{
1312233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1313188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1314184610Salfred	static const struct {
1315188417Sthompsa		uint32_t	r1, r2;
1316188417Sthompsa	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1317184610Salfred	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1318188417Sthompsa	int i, error;
1319184610Salfred
1320188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x57);
1321188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1322184610Salfred
1323188417Sthompsa	for (i = 0; i < N(rfsc); i++) {
1324188417Sthompsa		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1325188417Sthompsa			return (error);
1326184610Salfred	}
1327184610Salfred
1328188417Sthompsa	zyd_write16_m(sc, ZYD_CR128, 0x14);
1329188417Sthompsa	zyd_write16_m(sc, ZYD_CR129, 0x12);
1330188417Sthompsa	zyd_write16_m(sc, ZYD_CR130, 0x10);
1331188417Sthompsa	zyd_write16_m(sc, ZYD_CR38,  0x38);
1332188417Sthompsa	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1333184610Salfred
1334188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1335188417Sthompsa	if (error != 0)
1336188417Sthompsa		goto fail;
1337188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1338188417Sthompsa	if (error != 0)
1339188417Sthompsa		goto fail;
1340188417Sthompsa	error = zyd_rfwrite(sc, 0x3c9000);
1341188417Sthompsa	if (error != 0)
1342188417Sthompsa		goto fail;
1343184610Salfred
1344188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1345188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x06);
1346188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x08);
1347188417Sthompsafail:
1348188417Sthompsa	return (error);
1349188417Sthompsa#undef N
1350184610Salfred}
1351184610Salfred
1352184610Salfred/*
1353184610Salfred * AL2210 RF methods.
1354184610Salfred */
1355188417Sthompsastatic int
1356188417Sthompsazyd_al2210_init(struct zyd_rf *rf)
1357184610Salfred{
1358233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1359188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1360184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1361184610Salfred	static const uint32_t rfini[] = ZYD_AL2210_RF;
1362184610Salfred	uint32_t tmp;
1363188417Sthompsa	int i, error;
1364184610Salfred
1365188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1366184610Salfred
1367184610Salfred	/* init RF-dependent PHY registers */
1368188417Sthompsa	for (i = 0; i < N(phyini); i++)
1369188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1370188417Sthompsa
1371184610Salfred	/* init AL2210 radio */
1372188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1373188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1374188417Sthompsa			return (error);
1375184610Salfred	}
1376188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1377188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1378188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1379188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1380188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1381188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1382188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1383188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1384188417Sthompsafail:
1385188417Sthompsa	return (error);
1386188417Sthompsa#undef N
1387184610Salfred}
1388184610Salfred
1389188417Sthompsastatic int
1390188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1391184610Salfred{
1392188417Sthompsa	/* vendor driver does nothing for this RF chip */
1393188417Sthompsa
1394188417Sthompsa	return (0);
1395188417Sthompsa}
1396188417Sthompsa
1397188417Sthompsastatic int
1398188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1399188417Sthompsa{
1400188417Sthompsa	int error;
1401188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1402184610Salfred	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1403184610Salfred	uint32_t tmp;
1404184610Salfred
1405188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1406188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1407188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1408188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1409188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1410188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1411188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1412188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1413184610Salfred
1414184610Salfred	/* actually set the channel */
1415188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1416188417Sthompsa	if (error != 0)
1417188417Sthompsa		goto fail;
1418184610Salfred
1419188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1420188417Sthompsafail:
1421188417Sthompsa	return (error);
1422184610Salfred}
1423184610Salfred
1424184610Salfred/*
1425184610Salfred * GCT RF methods.
1426184610Salfred */
1427188417Sthompsastatic int
1428188417Sthompsazyd_gct_init(struct zyd_rf *rf)
1429184610Salfred{
1430193420Sweongyo#define	ZYD_GCT_INTR_REG	0x85c1
1431233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1432188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1433184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1434184610Salfred	static const uint32_t rfini[] = ZYD_GCT_RF;
1435193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1436193420Sweongyo	int i, idx = -1, error;
1437193420Sweongyo	uint16_t data;
1438184610Salfred
1439184610Salfred	/* init RF-dependent PHY registers */
1440188417Sthompsa	for (i = 0; i < N(phyini); i++)
1441188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1442188417Sthompsa
1443184610Salfred	/* init cgt radio */
1444188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1445188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1446188417Sthompsa			return (error);
1447184610Salfred	}
1448193420Sweongyo
1449193420Sweongyo	error = zyd_gct_mode(rf);
1450193420Sweongyo	if (error != 0)
1451193420Sweongyo		return (error);
1452193420Sweongyo
1453233774Shselasky	for (i = 0; i < (int)(N(vco) - 1); i++) {
1454193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 0);
1455193420Sweongyo		if (error != 0)
1456193420Sweongyo			goto fail;
1457193420Sweongyo		error = zyd_gct_write(rf, vco[i][0]);
1458193420Sweongyo		if (error != 0)
1459193420Sweongyo			goto fail;
1460193420Sweongyo		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1461193420Sweongyo		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1462193420Sweongyo		if ((data & 0xf) == 0) {
1463193420Sweongyo			idx = i;
1464193420Sweongyo			break;
1465193420Sweongyo		}
1466193420Sweongyo	}
1467193420Sweongyo	if (idx == -1) {
1468193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 1);
1469193420Sweongyo		if (error != 0)
1470193420Sweongyo			goto fail;
1471193420Sweongyo		error = zyd_gct_write(rf, 0x6662);
1472193420Sweongyo		if (error != 0)
1473193420Sweongyo			goto fail;
1474193420Sweongyo	}
1475193420Sweongyo
1476193420Sweongyo	rf->idx = idx;
1477193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1478188417Sthompsafail:
1479188417Sthompsa	return (error);
1480188417Sthompsa#undef N
1481193420Sweongyo#undef ZYD_GCT_INTR_REG
1482184610Salfred}
1483184610Salfred
1484188417Sthompsastatic int
1485193420Sweongyozyd_gct_mode(struct zyd_rf *rf)
1486184610Salfred{
1487233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1488193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1489193420Sweongyo	static const uint32_t mode[] = {
1490193420Sweongyo		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1491193420Sweongyo	};
1492193420Sweongyo	int i, error;
1493188417Sthompsa
1494193420Sweongyo	for (i = 0; i < N(mode); i++) {
1495193420Sweongyo		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1496193420Sweongyo			break;
1497193420Sweongyo	}
1498193420Sweongyo	return (error);
1499193420Sweongyo#undef N
1500188417Sthompsa}
1501188417Sthompsa
1502188417Sthompsastatic int
1503193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1504188417Sthompsa{
1505193420Sweongyo	int error, idx = chan - 1;
1506188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1507193420Sweongyo	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1508193420Sweongyo	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1509193420Sweongyo	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1510184610Salfred
1511193420Sweongyo	error = zyd_rfwrite(sc,
1512193420Sweongyo	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1513188417Sthompsa	if (error != 0)
1514193420Sweongyo		return (error);
1515193420Sweongyo	return zyd_rfwrite(sc, div_synth[idx]);
1516184610Salfred}
1517184610Salfred
1518188417Sthompsastatic int
1519193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value)
1520184610Salfred{
1521188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1522184610Salfred
1523193420Sweongyo	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1524184610Salfred}
1525184610Salfred
1526188417Sthompsastatic int
1527193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on)
1528184610Salfred{
1529193420Sweongyo	int error;
1530193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1531188417Sthompsa
1532193420Sweongyo	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1533193420Sweongyo	if (error != 0)
1534193420Sweongyo		return (error);
1535193420Sweongyo
1536193420Sweongyo	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1537193420Sweongyo	zyd_write16_m(sc, ZYD_CR251,
1538193420Sweongyo	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1539193420Sweongyofail:
1540193420Sweongyo	return (error);
1541188417Sthompsa}
1542188417Sthompsa
1543188417Sthompsastatic int
1544193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1545188417Sthompsa{
1546233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1547193420Sweongyo	int error, i;
1548188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1549193420Sweongyo	static const struct zyd_phy_pair cmd[] = {
1550193420Sweongyo		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1551193420Sweongyo		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1552193420Sweongyo	};
1553193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1554184610Salfred
1555193420Sweongyo	error = zyd_gct_set_channel_synth(rf, chan, 0);
1556188417Sthompsa	if (error != 0)
1557188417Sthompsa		goto fail;
1558193420Sweongyo	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1559193420Sweongyo	    vco[rf->idx][((chan - 1) / 2)]);
1560188417Sthompsa	if (error != 0)
1561188417Sthompsa		goto fail;
1562193420Sweongyo	error = zyd_gct_mode(rf);
1563193420Sweongyo	if (error != 0)
1564193420Sweongyo		return (error);
1565193420Sweongyo	for (i = 0; i < N(cmd); i++)
1566193420Sweongyo		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1567193420Sweongyo	error = zyd_gct_txgain(rf, chan);
1568193420Sweongyo	if (error != 0)
1569193420Sweongyo		return (error);
1570193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1571188417Sthompsafail:
1572188417Sthompsa	return (error);
1573188417Sthompsa#undef N
1574184610Salfred}
1575184610Salfred
1576193420Sweongyostatic int
1577193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1578193420Sweongyo{
1579193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1580193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1581193420Sweongyo	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1582193420Sweongyo	uint8_t idx = sc->sc_pwrint[chan - 1];
1583193420Sweongyo
1584193420Sweongyo	if (idx >= N(txgain)) {
1585193420Sweongyo		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1586193420Sweongyo		    chan, idx);
1587193420Sweongyo		return 0;
1588193420Sweongyo	}
1589193420Sweongyo
1590193420Sweongyo	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1591193420Sweongyo#undef N
1592193420Sweongyo}
1593193420Sweongyo
1594184610Salfred/*
1595184610Salfred * Maxim2 RF methods.
1596184610Salfred */
1597188417Sthompsastatic int
1598188417Sthompsazyd_maxim2_init(struct zyd_rf *rf)
1599184610Salfred{
1600233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1601188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1602184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1603184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1604184610Salfred	uint16_t tmp;
1605188417Sthompsa	int i, error;
1606184610Salfred
1607184610Salfred	/* init RF-dependent PHY registers */
1608188417Sthompsa	for (i = 0; i < N(phyini); i++)
1609188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1610184610Salfred
1611188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1612188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1613188417Sthompsa
1614184610Salfred	/* init maxim2 radio */
1615188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1616188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1617188417Sthompsa			return (error);
1618184610Salfred	}
1619188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1620188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1621188417Sthompsafail:
1622188417Sthompsa	return (error);
1623188417Sthompsa#undef N
1624184610Salfred}
1625184610Salfred
1626188417Sthompsastatic int
1627188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1628184610Salfred{
1629188417Sthompsa
1630188417Sthompsa	/* vendor driver does nothing for this RF chip */
1631188417Sthompsa	return (0);
1632188417Sthompsa}
1633188417Sthompsa
1634188417Sthompsastatic int
1635188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1636188417Sthompsa{
1637233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1638188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1639184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1640184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1641184610Salfred	static const struct {
1642188417Sthompsa		uint32_t	r1, r2;
1643188417Sthompsa	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1644184610Salfred	uint16_t tmp;
1645188417Sthompsa	int i, error;
1646184610Salfred
1647184610Salfred	/*
1648184610Salfred	 * Do the same as we do when initializing it, except for the channel
1649184610Salfred	 * values coming from the two channel tables.
1650184610Salfred	 */
1651184610Salfred
1652184610Salfred	/* init RF-dependent PHY registers */
1653188417Sthompsa	for (i = 0; i < N(phyini); i++)
1654188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1655184610Salfred
1656188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1657188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1658188417Sthompsa
1659184610Salfred	/* first two values taken from the chantables */
1660188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1661188417Sthompsa	if (error != 0)
1662188417Sthompsa		goto fail;
1663188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1664188417Sthompsa	if (error != 0)
1665188417Sthompsa		goto fail;
1666184610Salfred
1667184610Salfred	/* init maxim2 radio - skipping the two first values */
1668188417Sthompsa	for (i = 2; i < N(rfini); i++) {
1669188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1670188417Sthompsa			return (error);
1671184610Salfred	}
1672188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1673188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1674188417Sthompsafail:
1675188417Sthompsa	return (error);
1676188417Sthompsa#undef N
1677184610Salfred}
1678184610Salfred
1679188417Sthompsastatic int
1680188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1681184610Salfred{
1682188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1683184610Salfred
1684188417Sthompsa	rf->rf_sc = sc;
1685193420Sweongyo	rf->update_pwr = 1;
1686188417Sthompsa
1687188417Sthompsa	switch (type) {
1688184610Salfred	case ZYD_RF_RFMD:
1689188417Sthompsa		rf->init         = zyd_rfmd_init;
1690188417Sthompsa		rf->switch_radio = zyd_rfmd_switch_radio;
1691188417Sthompsa		rf->set_channel  = zyd_rfmd_set_channel;
1692188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1693184610Salfred		break;
1694184610Salfred	case ZYD_RF_AL2230:
1695186730Salfred	case ZYD_RF_AL2230S:
1696188417Sthompsa		if (sc->sc_macrev == ZYD_ZD1211B) {
1697188417Sthompsa			rf->init = zyd_al2230_init_b;
1698188417Sthompsa			rf->set_channel = zyd_al2230_set_channel_b;
1699186730Salfred		} else {
1700188417Sthompsa			rf->init = zyd_al2230_init;
1701188417Sthompsa			rf->set_channel = zyd_al2230_set_channel;
1702186730Salfred		}
1703188417Sthompsa		rf->switch_radio = zyd_al2230_switch_radio;
1704188417Sthompsa		rf->bandedge6	 = zyd_al2230_bandedge6;
1705188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1706184610Salfred		break;
1707184610Salfred	case ZYD_RF_AL7230B:
1708188417Sthompsa		rf->init         = zyd_al7230B_init;
1709188417Sthompsa		rf->switch_radio = zyd_al7230B_switch_radio;
1710188417Sthompsa		rf->set_channel  = zyd_al7230B_set_channel;
1711188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1712184610Salfred		break;
1713184610Salfred	case ZYD_RF_AL2210:
1714188417Sthompsa		rf->init         = zyd_al2210_init;
1715188417Sthompsa		rf->switch_radio = zyd_al2210_switch_radio;
1716188417Sthompsa		rf->set_channel  = zyd_al2210_set_channel;
1717188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1718184610Salfred		break;
1719193420Sweongyo	case ZYD_RF_MAXIM_NEW:
1720184610Salfred	case ZYD_RF_GCT:
1721188417Sthompsa		rf->init         = zyd_gct_init;
1722188417Sthompsa		rf->switch_radio = zyd_gct_switch_radio;
1723188417Sthompsa		rf->set_channel  = zyd_gct_set_channel;
1724193420Sweongyo		rf->width        = 24;	/* 24-bit RF values */
1725193420Sweongyo		rf->update_pwr   = 0;
1726184610Salfred		break;
1727184610Salfred	case ZYD_RF_MAXIM_NEW2:
1728188417Sthompsa		rf->init         = zyd_maxim2_init;
1729188417Sthompsa		rf->switch_radio = zyd_maxim2_switch_radio;
1730188417Sthompsa		rf->set_channel  = zyd_maxim2_set_channel;
1731188417Sthompsa		rf->width        = 18;	/* 18-bit RF values */
1732184610Salfred		break;
1733184610Salfred	default:
1734188417Sthompsa		device_printf(sc->sc_dev,
1735188417Sthompsa		    "sorry, radio \"%s\" is not supported yet\n",
1736188417Sthompsa		    zyd_rf_name(type));
1737188417Sthompsa		return (EINVAL);
1738184610Salfred	}
1739188417Sthompsa	return (0);
1740188417Sthompsa}
1741184610Salfred
1742188417Sthompsastatic const char *
1743188417Sthompsazyd_rf_name(uint8_t type)
1744188417Sthompsa{
1745188417Sthompsa	static const char * const zyd_rfs[] = {
1746188417Sthompsa		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1747188417Sthompsa		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1748188417Sthompsa		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1749188417Sthompsa		"PHILIPS"
1750188417Sthompsa	};
1751184610Salfred
1752188417Sthompsa	return zyd_rfs[(type > 15) ? 0 : type];
1753184610Salfred}
1754184610Salfred
1755188417Sthompsastatic int
1756188417Sthompsazyd_hw_init(struct zyd_softc *sc)
1757184610Salfred{
1758188417Sthompsa	int error;
1759184610Salfred	const struct zyd_phy_pair *phyp;
1760188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1761188417Sthompsa	uint16_t val;
1762184610Salfred
1763184610Salfred	/* specify that the plug and play is finished */
1764188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1765188417Sthompsa	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1766188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1767188417Sthompsa	    sc->sc_fwbase);
1768184610Salfred
1769184610Salfred	/* retrieve firmware revision number */
1770188417Sthompsa	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1771188417Sthompsa	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1772188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1773186730Salfred	/* set mandatory rates - XXX assumes 802.11b/g */
1774188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1775186730Salfred
1776184610Salfred	/* disable interrupts */
1777188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1778184610Salfred
1779188417Sthompsa	if ((error = zyd_read_pod(sc)) != 0) {
1780188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1781188417Sthompsa		goto fail;
1782184610Salfred	}
1783188417Sthompsa
1784188417Sthompsa	/* PHY init (resetting) */
1785188417Sthompsa	error = zyd_lock_phy(sc);
1786188417Sthompsa	if (error != 0)
1787188417Sthompsa		goto fail;
1788188417Sthompsa	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1789188417Sthompsa	for (; phyp->reg != 0; phyp++)
1790188417Sthompsa		zyd_write16_m(sc, phyp->reg, phyp->val);
1791188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1792188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1793188417Sthompsa		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1794184610Salfred	}
1795188417Sthompsa	error = zyd_unlock_phy(sc);
1796188417Sthompsa	if (error != 0)
1797188417Sthompsa		goto fail;
1798184610Salfred
1799184610Salfred	/* HMAC init */
1800188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1801188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1802188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1803188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1804188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1805188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1806188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1807188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1808188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1809188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1810188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1811188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1812188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1813188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1814188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1815188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1816188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1817188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1818188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1819188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1820184610Salfred
1821188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
1822188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1823188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1824186730Salfred	} else {
1825188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1826188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1827188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1828188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1829188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1830188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1831188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1832188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1833188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1834186730Salfred	}
1835186730Salfred
1836184610Salfred	/* init beacon interval to 100ms */
1837188417Sthompsa	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1838188417Sthompsa		goto fail;
1839184610Salfred
1840188417Sthompsa	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1841188417Sthompsa		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1842188417Sthompsa		    sc->sc_rfrev);
1843188417Sthompsa		goto fail;
1844188417Sthompsa	}
1845188417Sthompsa
1846188417Sthompsa	/* RF chip init */
1847188417Sthompsa	error = zyd_lock_phy(sc);
1848188417Sthompsa	if (error != 0)
1849188417Sthompsa		goto fail;
1850188417Sthompsa	error = (*rf->init)(rf);
1851188417Sthompsa	if (error != 0) {
1852188417Sthompsa		device_printf(sc->sc_dev,
1853188417Sthompsa		    "radio initialization failed, error %d\n", error);
1854188417Sthompsa		goto fail;
1855188417Sthompsa	}
1856188417Sthompsa	error = zyd_unlock_phy(sc);
1857188417Sthompsa	if (error != 0)
1858188417Sthompsa		goto fail;
1859188417Sthompsa
1860188417Sthompsa	if ((error = zyd_read_eeprom(sc)) != 0) {
1861188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1862188417Sthompsa		goto fail;
1863188417Sthompsa	}
1864188417Sthompsa
1865188417Sthompsafail:	return (error);
1866184610Salfred}
1867184610Salfred
1868188417Sthompsastatic int
1869188417Sthompsazyd_read_pod(struct zyd_softc *sc)
1870184610Salfred{
1871188417Sthompsa	int error;
1872184610Salfred	uint32_t tmp;
1873184610Salfred
1874188417Sthompsa	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1875188417Sthompsa	sc->sc_rfrev     = tmp & 0x0f;
1876188417Sthompsa	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1877188417Sthompsa	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1878188417Sthompsa	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1879184610Salfred	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1880188417Sthompsa	sc->sc_parev     = (tmp >> 16) & 0x0f;
1881186730Salfred	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1882188417Sthompsa	sc->sc_newphy    = (tmp >> 31) & 0x01;
1883188417Sthompsa	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1884188417Sthompsafail:
1885188417Sthompsa	return (error);
1886188417Sthompsa}
1887184610Salfred
1888188417Sthompsastatic int
1889188417Sthompsazyd_read_eeprom(struct zyd_softc *sc)
1890188417Sthompsa{
1891188417Sthompsa	uint16_t val;
1892188417Sthompsa	int error, i;
1893184610Salfred
1894184610Salfred	/* read Tx power calibration tables */
1895184610Salfred	for (i = 0; i < 7; i++) {
1896188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1897188417Sthompsa		sc->sc_pwrcal[i * 2] = val >> 8;
1898188417Sthompsa		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1899188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1900188417Sthompsa		sc->sc_pwrint[i * 2] = val >> 8;
1901188417Sthompsa		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1902188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1903188417Sthompsa		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1904188417Sthompsa		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1905188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1906188417Sthompsa		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1907188417Sthompsa		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1908188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1909188417Sthompsa		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1910188417Sthompsa		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1911184610Salfred	}
1912188417Sthompsafail:
1913188417Sthompsa	return (error);
1914184610Salfred}
1915184610Salfred
1916188417Sthompsastatic int
1917188417Sthompsazyd_get_macaddr(struct zyd_softc *sc)
1918186730Salfred{
1919192984Sthompsa	struct usb_device_request req;
1920193045Sthompsa	usb_error_t error;
1921186730Salfred
1922186730Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1923186730Salfred	req.bRequest = ZYD_READFWDATAREQ;
1924186730Salfred	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1925186730Salfred	USETW(req.wIndex, 0);
1926186730Salfred	USETW(req.wLength, IEEE80211_ADDR_LEN);
1927186730Salfred
1928188419Sthompsa	error = zyd_do_request(sc, &req, sc->sc_bssid);
1929188417Sthompsa	if (error != 0) {
1930188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1931194228Sthompsa		    usbd_errstr(error));
1932188417Sthompsa	}
1933188417Sthompsa
1934188417Sthompsa	return (error);
1935186730Salfred}
1936186730Salfred
1937188417Sthompsastatic int
1938188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1939184610Salfred{
1940188417Sthompsa	int error;
1941184610Salfred	uint32_t tmp;
1942184610Salfred
1943188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1944188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1945188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1946188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1947188417Sthompsafail:
1948188417Sthompsa	return (error);
1949188417Sthompsa}
1950184610Salfred
1951188417Sthompsastatic int
1952188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1953188417Sthompsa{
1954188417Sthompsa	int error;
1955188417Sthompsa	uint32_t tmp;
1956188417Sthompsa
1957188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1958188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1959188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1960188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1961188417Sthompsafail:
1962188417Sthompsa	return (error);
1963184610Salfred}
1964184610Salfred
1965188417Sthompsastatic int
1966188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on)
1967184610Salfred{
1968188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1969188417Sthompsa	int error;
1970188417Sthompsa
1971188417Sthompsa	error = zyd_lock_phy(sc);
1972188417Sthompsa	if (error != 0)
1973188417Sthompsa		goto fail;
1974188417Sthompsa	error = (*rf->switch_radio)(rf, on);
1975188417Sthompsa	if (error != 0)
1976188417Sthompsa		goto fail;
1977188417Sthompsa	error = zyd_unlock_phy(sc);
1978188417Sthompsafail:
1979188417Sthompsa	return (error);
1980184610Salfred}
1981184610Salfred
1982188417Sthompsastatic int
1983188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on)
1984184610Salfred{
1985188417Sthompsa	int error;
1986184610Salfred	uint32_t tmp;
1987184610Salfred
1988188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1989188417Sthompsa	tmp &= ~which;
1990188417Sthompsa	if (on)
1991188417Sthompsa		tmp |= which;
1992188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1993188417Sthompsafail:
1994188417Sthompsa	return (error);
1995188417Sthompsa}
1996184610Salfred
1997188417Sthompsastatic void
1998188417Sthompsazyd_set_multi(struct zyd_softc *sc)
1999184610Salfred{
2000188417Sthompsa	int error;
2001188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2002188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2003188417Sthompsa	struct ifmultiaddr *ifma;
2004188417Sthompsa	uint32_t low, high;
2005188417Sthompsa	uint8_t v;
2006184610Salfred
2007188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2008188417Sthompsa		return;
2009184610Salfred
2010188417Sthompsa	low = 0x00000000;
2011188417Sthompsa	high = 0x80000000;
2012184610Salfred
2013188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
2014188417Sthompsa	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
2015188417Sthompsa		low = 0xffffffff;
2016188417Sthompsa		high = 0xffffffff;
2017184610Salfred	} else {
2018195049Srwatson		if_maddr_rlock(ifp);
2019188417Sthompsa		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2020188417Sthompsa			if (ifma->ifma_addr->sa_family != AF_LINK)
2021188417Sthompsa				continue;
2022188417Sthompsa			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2023188417Sthompsa			    ifma->ifma_addr))[5] >> 2;
2024188417Sthompsa			if (v < 32)
2025188417Sthompsa				low |= 1 << v;
2026188417Sthompsa			else
2027188417Sthompsa				high |= 1 << (v - 32);
2028188417Sthompsa		}
2029195049Srwatson		if_maddr_runlock(ifp);
2030184610Salfred	}
2031184610Salfred
2032188417Sthompsa	/* reprogram multicast global hash table */
2033188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2034188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2035188417Sthompsafail:
2036188417Sthompsa	if (error != 0)
2037188417Sthompsa		device_printf(sc->sc_dev,
2038188417Sthompsa		    "could not set multicast hash table\n");
2039188417Sthompsa}
2040184610Salfred
2041188417Sthompsastatic void
2042188417Sthompsazyd_update_mcast(struct ifnet *ifp)
2043188417Sthompsa{
2044188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2045184610Salfred
2046188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2047184610Salfred		return;
2048184610Salfred
2049188417Sthompsa	ZYD_LOCK(sc);
2050191746Sthompsa	zyd_set_multi(sc);
2051188417Sthompsa	ZYD_UNLOCK(sc);
2052188417Sthompsa}
2053184610Salfred
2054188417Sthompsastatic int
2055188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc)
2056188417Sthompsa{
2057188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2058188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2059188417Sthompsa	uint32_t rxfilter;
2060184610Salfred
2061188417Sthompsa	switch (ic->ic_opmode) {
2062188417Sthompsa	case IEEE80211_M_STA:
2063188417Sthompsa		rxfilter = ZYD_FILTER_BSS;
2064188417Sthompsa		break;
2065188417Sthompsa	case IEEE80211_M_IBSS:
2066188417Sthompsa	case IEEE80211_M_HOSTAP:
2067188417Sthompsa		rxfilter = ZYD_FILTER_HOSTAP;
2068188417Sthompsa		break;
2069188417Sthompsa	case IEEE80211_M_MONITOR:
2070188417Sthompsa		rxfilter = ZYD_FILTER_MONITOR;
2071188417Sthompsa		break;
2072188417Sthompsa	default:
2073188417Sthompsa		/* should not get there */
2074188417Sthompsa		return (EINVAL);
2075184610Salfred	}
2076188417Sthompsa	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2077188417Sthompsa}
2078184610Salfred
2079188417Sthompsastatic void
2080188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2081188417Sthompsa{
2082188417Sthompsa	int error;
2083188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2084188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2085188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
2086188417Sthompsa	uint32_t tmp;
2087188601Sthompsa	int chan;
2088184610Salfred
2089188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
2090188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2091188417Sthompsa		/* XXX should NEVER happen */
2092188417Sthompsa		device_printf(sc->sc_dev,
2093188417Sthompsa		    "%s: invalid channel %x\n", __func__, chan);
2094188417Sthompsa		return;
2095188417Sthompsa	}
2096184610Salfred
2097188417Sthompsa	error = zyd_lock_phy(sc);
2098188417Sthompsa	if (error != 0)
2099188417Sthompsa		goto fail;
2100184610Salfred
2101188417Sthompsa	error = (*rf->set_channel)(rf, chan);
2102188417Sthompsa	if (error != 0)
2103188417Sthompsa		goto fail;
2104184610Salfred
2105193420Sweongyo	if (rf->update_pwr) {
2106193420Sweongyo		/* update Tx power */
2107193420Sweongyo		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2108184610Salfred
2109193420Sweongyo		if (sc->sc_macrev == ZYD_ZD1211B) {
2110193420Sweongyo			zyd_write16_m(sc, ZYD_CR67,
2111193420Sweongyo			    sc->sc_ofdm36_cal[chan - 1]);
2112193420Sweongyo			zyd_write16_m(sc, ZYD_CR66,
2113193420Sweongyo			    sc->sc_ofdm48_cal[chan - 1]);
2114193420Sweongyo			zyd_write16_m(sc, ZYD_CR65,
2115193420Sweongyo			    sc->sc_ofdm54_cal[chan - 1]);
2116193420Sweongyo			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2117193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x28);
2118193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2119193420Sweongyo		}
2120188417Sthompsa	}
2121188417Sthompsa	if (sc->sc_cckgain) {
2122188417Sthompsa		/* set CCK baseband gain from EEPROM */
2123188417Sthompsa		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2124188417Sthompsa			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2125188417Sthompsa	}
2126188417Sthompsa	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2127188417Sthompsa		error = (*rf->bandedge6)(rf, c);
2128188417Sthompsa		if (error != 0)
2129188417Sthompsa			goto fail;
2130188417Sthompsa	}
2131188417Sthompsa	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2132184610Salfred
2133188417Sthompsa	error = zyd_unlock_phy(sc);
2134188417Sthompsa	if (error != 0)
2135188417Sthompsa		goto fail;
2136184610Salfred
2137188417Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2138188417Sthompsa	    htole16(c->ic_freq);
2139188417Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2140188417Sthompsa	    htole16(c->ic_flags);
2141188417Sthompsafail:
2142184610Salfred	return;
2143184610Salfred}
2144184610Salfred
2145184610Salfredstatic int
2146188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2147184610Salfred{
2148188417Sthompsa	int error;
2149188417Sthompsa	uint32_t val;
2150184610Salfred
2151188417Sthompsa	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2152188417Sthompsa	sc->sc_atim_wnd = val;
2153188417Sthompsa	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2154188417Sthompsa	sc->sc_pre_tbtt = val;
2155188417Sthompsa	sc->sc_bcn_int = bintval;
2156184610Salfred
2157188417Sthompsa	if (sc->sc_bcn_int <= 5)
2158188417Sthompsa		sc->sc_bcn_int = 5;
2159188417Sthompsa	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2160188417Sthompsa		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2161188417Sthompsa	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2162188417Sthompsa		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2163184610Salfred
2164188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2165188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2166188417Sthompsa	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2167188417Sthompsafail:
2168188417Sthompsa	return (error);
2169188417Sthompsa}
2170184610Salfred
2171188417Sthompsastatic void
2172192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2173188417Sthompsa{
2174194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2175188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2176192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
2177188417Sthompsa	struct zyd_plcphdr plcp;
2178188417Sthompsa	struct zyd_rx_stat stat;
2179194677Sthompsa	struct usb_page_cache *pc;
2180188417Sthompsa	struct mbuf *m;
2181188417Sthompsa	int rlen, rssi;
2182184610Salfred
2183188417Sthompsa	if (len < ZYD_MIN_FRAGSZ) {
2184188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2185188417Sthompsa		    device_get_nameunit(sc->sc_dev), len);
2186188417Sthompsa		ifp->if_ierrors++;
2187188417Sthompsa		return;
2188188417Sthompsa	}
2189194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
2190194677Sthompsa	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2191194677Sthompsa	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2192184610Salfred
2193188417Sthompsa	if (stat.flags & ZYD_RX_ERROR) {
2194188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV,
2195188417Sthompsa		    "%s: RX status indicated error (%x)\n",
2196188417Sthompsa		    device_get_nameunit(sc->sc_dev), stat.flags);
2197188417Sthompsa		ifp->if_ierrors++;
2198188417Sthompsa		return;
2199188417Sthompsa	}
2200184610Salfred
2201188417Sthompsa	/* compute actual frame length */
2202188417Sthompsa	rlen = len - sizeof(struct zyd_plcphdr) -
2203188417Sthompsa	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2204184610Salfred
2205188417Sthompsa	/* allocate a mbuf to store the frame */
2206233774Shselasky	if (rlen > (int)MCLBYTES) {
2207188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2208188417Sthompsa		    device_get_nameunit(sc->sc_dev), rlen);
2209188417Sthompsa		ifp->if_ierrors++;
2210188417Sthompsa		return;
2211233774Shselasky	} else if (rlen > (int)MHLEN)
2212243857Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2213188417Sthompsa	else
2214243857Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
2215188417Sthompsa	if (m == NULL) {
2216188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2217188417Sthompsa		    device_get_nameunit(sc->sc_dev));
2218188417Sthompsa		ifp->if_ierrors++;
2219188417Sthompsa		return;
2220184610Salfred	}
2221188417Sthompsa	m->m_pkthdr.rcvif = ifp;
2222188417Sthompsa	m->m_pkthdr.len = m->m_len = rlen;
2223194677Sthompsa	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2224184610Salfred
2225192468Ssam	if (ieee80211_radiotap_active(ic)) {
2226188417Sthompsa		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2227184610Salfred
2228188417Sthompsa		tap->wr_flags = 0;
2229188417Sthompsa		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2230188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2231188417Sthompsa		/* XXX toss, no way to express errors */
2232188417Sthompsa		if (stat.flags & ZYD_RX_DECRYPTERR)
2233188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2234188417Sthompsa		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2235188417Sthompsa		    (stat.flags & ZYD_RX_OFDM) ?
2236188417Sthompsa			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2237188417Sthompsa		tap->wr_antsignal = stat.rssi + -95;
2238188417Sthompsa		tap->wr_antnoise = -95;	/* XXX */
2239188417Sthompsa	}
2240188417Sthompsa	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2241184610Salfred
2242188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2243188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].m = m;
2244188417Sthompsa	sc->sc_rx_count++;
2245184610Salfred}
2246184610Salfred
2247184610Salfredstatic void
2248194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2249184610Salfred{
2250194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2251184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2252184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2253188417Sthompsa	struct ieee80211_node *ni;
2254188417Sthompsa	struct zyd_rx_desc desc;
2255188417Sthompsa	struct mbuf *m;
2256194677Sthompsa	struct usb_page_cache *pc;
2257188417Sthompsa	uint32_t offset;
2258188417Sthompsa	uint8_t rssi;
2259188417Sthompsa	int8_t nf;
2260188417Sthompsa	int i;
2261194677Sthompsa	int actlen;
2262184610Salfred
2263194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2264194677Sthompsa
2265188417Sthompsa	sc->sc_rx_count = 0;
2266188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2267188417Sthompsa	case USB_ST_TRANSFERRED:
2268194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2269194677Sthompsa		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2270184610Salfred
2271188417Sthompsa		offset = 0;
2272188417Sthompsa		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2273188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2274188417Sthompsa			    "%s: received multi-frame transfer\n", __func__);
2275184610Salfred
2276188417Sthompsa			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2277188417Sthompsa				uint16_t len16 = UGETW(desc.len[i]);
2278184610Salfred
2279194677Sthompsa				if (len16 == 0 || len16 > actlen)
2280188417Sthompsa					break;
2281184610Salfred
2282188417Sthompsa				zyd_rx_data(xfer, offset, len16);
2283184610Salfred
2284188417Sthompsa				/* next frame is aligned on a 32-bit boundary */
2285188417Sthompsa				len16 = (len16 + 3) & ~3;
2286188417Sthompsa				offset += len16;
2287194677Sthompsa				if (len16 > actlen)
2288188417Sthompsa					break;
2289194677Sthompsa				actlen -= len16;
2290188417Sthompsa			}
2291188417Sthompsa		} else {
2292188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2293188417Sthompsa			    "%s: received single-frame transfer\n", __func__);
2294184610Salfred
2295194677Sthompsa			zyd_rx_data(xfer, 0, actlen);
2296188417Sthompsa		}
2297188417Sthompsa		/* FALLTHROUGH */
2298188417Sthompsa	case USB_ST_SETUP:
2299188417Sthompsatr_setup:
2300194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2301194228Sthompsa		usbd_transfer_submit(xfer);
2302184610Salfred
2303188417Sthompsa		/*
2304188417Sthompsa		 * At the end of a USB callback it is always safe to unlock
2305188417Sthompsa		 * the private mutex of a device! That is why we do the
2306188417Sthompsa		 * "ieee80211_input" here, and not some lines up!
2307188417Sthompsa		 */
2308188417Sthompsa		ZYD_UNLOCK(sc);
2309188417Sthompsa		for (i = 0; i < sc->sc_rx_count; i++) {
2310188417Sthompsa			rssi = sc->sc_rx_data[i].rssi;
2311188417Sthompsa			m = sc->sc_rx_data[i].m;
2312188417Sthompsa			sc->sc_rx_data[i].m = NULL;
2313184610Salfred
2314188417Sthompsa			nf = -95;	/* XXX */
2315184610Salfred
2316188417Sthompsa			ni = ieee80211_find_rxnode(ic,
2317188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
2318188417Sthompsa			if (ni != NULL) {
2319192468Ssam				(void)ieee80211_input(ni, m, rssi, nf);
2320188417Sthompsa				ieee80211_free_node(ni);
2321188417Sthompsa			} else
2322192468Ssam				(void)ieee80211_input_all(ic, m, rssi, nf);
2323188417Sthompsa		}
2324198099Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2325198099Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
2326198099Sweongyo			zyd_start(ifp);
2327188417Sthompsa		ZYD_LOCK(sc);
2328188417Sthompsa		break;
2329184610Salfred
2330188417Sthompsa	default:			/* Error */
2331194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2332184610Salfred
2333194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2334188417Sthompsa			/* try to clear stall first */
2335194677Sthompsa			usbd_xfer_set_stall(xfer);
2336188417Sthompsa			goto tr_setup;
2337184610Salfred		}
2338188417Sthompsa		break;
2339184610Salfred	}
2340184610Salfred}
2341184610Salfred
2342184610Salfredstatic uint8_t
2343193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate)
2344184610Salfred{
2345184610Salfred	switch (rate) {
2346188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2347184610Salfred	case 12:
2348184610Salfred		return (0xb);
2349184610Salfred	case 18:
2350184610Salfred		return (0xf);
2351184610Salfred	case 24:
2352184610Salfred		return (0xa);
2353184610Salfred	case 36:
2354184610Salfred		return (0xe);
2355184610Salfred	case 48:
2356184610Salfred		return (0x9);
2357184610Salfred	case 72:
2358184610Salfred		return (0xd);
2359184610Salfred	case 96:
2360184610Salfred		return (0x8);
2361184610Salfred	case 108:
2362184610Salfred		return (0xc);
2363188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
2364188417Sthompsa	case 2:
2365188417Sthompsa		return (0x0);
2366188417Sthompsa	case 4:
2367188417Sthompsa		return (0x1);
2368188417Sthompsa	case 11:
2369188417Sthompsa		return (0x2);
2370188417Sthompsa	case 22:
2371188417Sthompsa		return (0x3);
2372184610Salfred	}
2373184610Salfred
2374193803Sweongyo	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2375193803Sweongyo	return (0x0);
2376184610Salfred}
2377184610Salfred
2378184610Salfredstatic void
2379194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2380184610Salfred{
2381194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2382184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2383192468Ssam	struct ieee80211vap *vap;
2384188417Sthompsa	struct zyd_tx_data *data;
2385188417Sthompsa	struct mbuf *m;
2386194677Sthompsa	struct usb_page_cache *pc;
2387194677Sthompsa	int actlen;
2388184610Salfred
2389194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2390194677Sthompsa
2391188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2392188417Sthompsa	case USB_ST_TRANSFERRED:
2393188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2394194677Sthompsa		    actlen);
2395184610Salfred
2396188417Sthompsa		/* free resources */
2397194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2398188417Sthompsa		zyd_tx_free(data, 0);
2399194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2400184610Salfred
2401188417Sthompsa		ifp->if_opackets++;
2402188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2403184610Salfred
2404188417Sthompsa		/* FALLTHROUGH */
2405188417Sthompsa	case USB_ST_SETUP:
2406188417Sthompsatr_setup:
2407188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
2408188417Sthompsa		if (data) {
2409188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2410188417Sthompsa			m = data->m;
2411184610Salfred
2412233774Shselasky			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2413188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2414188417Sthompsa				    m->m_pkthdr.len);
2415188417Sthompsa				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2416188417Sthompsa			}
2417194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
2418194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2419194677Sthompsa			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2420188417Sthompsa			    m->m_pkthdr.len);
2421184610Salfred
2422192468Ssam			vap = data->ni->ni_vap;
2423192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
2424188417Sthompsa				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2425184610Salfred
2426188417Sthompsa				tap->wt_flags = 0;
2427188417Sthompsa				tap->wt_rate = data->rate;
2428184610Salfred
2429192468Ssam				ieee80211_radiotap_tx(vap, m);
2430188417Sthompsa			}
2431184610Salfred
2432194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2433194677Sthompsa			usbd_xfer_set_priv(xfer, data);
2434194228Sthompsa			usbd_transfer_submit(xfer);
2435188417Sthompsa		}
2436198099Sweongyo		ZYD_UNLOCK(sc);
2437198099Sweongyo		zyd_start(ifp);
2438198099Sweongyo		ZYD_LOCK(sc);
2439188417Sthompsa		break;
2440184610Salfred
2441188417Sthompsa	default:			/* Error */
2442188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2443194677Sthompsa		    usbd_errstr(error));
2444184610Salfred
2445188419Sthompsa		ifp->if_oerrors++;
2446194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2447194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2448188419Sthompsa		if (data != NULL)
2449194677Sthompsa			zyd_tx_free(data, error);
2450188419Sthompsa
2451203141Sthompsa		if (error != USB_ERR_CANCELLED) {
2452203141Sthompsa			if (error == USB_ERR_TIMEOUT)
2453203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2454203141Sthompsa
2455203141Sthompsa			/*
2456203141Sthompsa			 * Try to clear stall first, also if other
2457203141Sthompsa			 * errors occur, hence clearing stall
2458203141Sthompsa			 * introduces a 50 ms delay:
2459203141Sthompsa			 */
2460194677Sthompsa			usbd_xfer_set_stall(xfer);
2461188417Sthompsa			goto tr_setup;
2462188417Sthompsa		}
2463184610Salfred		break;
2464184610Salfred	}
2465184610Salfred}
2466184610Salfred
2467188417Sthompsastatic int
2468193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2469184610Salfred{
2470188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2471188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2472188417Sthompsa	struct zyd_tx_desc *desc;
2473188417Sthompsa	struct zyd_tx_data *data;
2474188417Sthompsa	struct ieee80211_frame *wh;
2475188417Sthompsa	const struct ieee80211_txparam *tp;
2476188417Sthompsa	struct ieee80211_key *k;
2477188417Sthompsa	int rate, totlen;
2478269266Shselasky	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2479193803Sweongyo	uint8_t phy;
2480188417Sthompsa	uint16_t pktlen;
2481193803Sweongyo	uint32_t bits;
2482184610Salfred
2483188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
2484188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
2485188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2486188417Sthompsa	sc->tx_nfree--;
2487184610Salfred
2488193803Sweongyo	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2489193803Sweongyo	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2490193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2491193803Sweongyo		rate = tp->mgmtrate;
2492188417Sthompsa	} else {
2493193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2494193803Sweongyo		/* for data frames */
2495193803Sweongyo		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2496193803Sweongyo			rate = tp->mcastrate;
2497193803Sweongyo		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2498193803Sweongyo			rate = tp->ucastrate;
2499193803Sweongyo		else {
2500206358Srpaulo			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2501193803Sweongyo			rate = ni->ni_txrate;
2502193803Sweongyo		}
2503184610Salfred	}
2504184610Salfred
2505262007Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2506188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
2507188417Sthompsa		if (k == NULL) {
2508188417Sthompsa			m_freem(m0);
2509188417Sthompsa			return (ENOBUFS);
2510188417Sthompsa		}
2511188417Sthompsa		/* packet header may have moved, reset our local pointer */
2512188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
2513184610Salfred	}
2514184610Salfred
2515188417Sthompsa	data->ni = ni;
2516188417Sthompsa	data->m = m0;
2517193803Sweongyo	data->rate = rate;
2518184610Salfred
2519193803Sweongyo	/* fill Tx descriptor */
2520193803Sweongyo	desc = &data->desc;
2521193803Sweongyo	phy = zyd_plcp_signal(sc, rate);
2522193803Sweongyo	desc->phy = phy;
2523193803Sweongyo	if (ZYD_RATE_IS_OFDM(rate)) {
2524193803Sweongyo		desc->phy |= ZYD_TX_PHY_OFDM;
2525193803Sweongyo		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2526193803Sweongyo			desc->phy |= ZYD_TX_PHY_5GHZ;
2527193803Sweongyo	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2528193803Sweongyo		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2529193803Sweongyo
2530188417Sthompsa	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2531188417Sthompsa	desc->len = htole16(totlen);
2532184610Salfred
2533193803Sweongyo	desc->flags = ZYD_TX_FLAG_BACKOFF;
2534188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2535188417Sthompsa		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2536188417Sthompsa		if (totlen > vap->iv_rtsthreshold) {
2537188417Sthompsa			desc->flags |= ZYD_TX_FLAG_RTS;
2538188417Sthompsa		} else if (ZYD_RATE_IS_OFDM(rate) &&
2539188417Sthompsa		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2540188417Sthompsa			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2541188417Sthompsa				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2542188417Sthompsa			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2543188417Sthompsa				desc->flags |= ZYD_TX_FLAG_RTS;
2544188417Sthompsa		}
2545193803Sweongyo	} else
2546193803Sweongyo		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2547188417Sthompsa	if ((wh->i_fc[0] &
2548188417Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2549188417Sthompsa	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2550188417Sthompsa		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2551188417Sthompsa
2552184610Salfred	/* actual transmit length (XXX why +10?) */
2553193803Sweongyo	pktlen = ZYD_TX_DESC_SIZE + 10;
2554188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211)
2555184610Salfred		pktlen += totlen;
2556188417Sthompsa	desc->pktlen = htole16(pktlen);
2557184610Salfred
2558193803Sweongyo	bits = (rate == 11) ? (totlen * 16) + 10 :
2559193803Sweongyo	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2560196809Sweongyo	desc->plcp_length = htole16(bits / ratediv[phy]);
2561188417Sthompsa	desc->plcp_service = 0;
2562193803Sweongyo	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2563193803Sweongyo		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2564193803Sweongyo	desc->nextlen = 0;
2565193803Sweongyo
2566193803Sweongyo	if (ieee80211_radiotap_active_vap(vap)) {
2567193803Sweongyo		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2568193803Sweongyo
2569193803Sweongyo		tap->wt_flags = 0;
2570193803Sweongyo		tap->wt_rate = rate;
2571193803Sweongyo
2572193803Sweongyo		ieee80211_radiotap_tx(vap, m0);
2573184610Salfred	}
2574184610Salfred
2575188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_XMIT,
2576188417Sthompsa	    "%s: sending data frame len=%zu rate=%u\n",
2577188417Sthompsa	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2578188417Sthompsa		rate);
2579184610Salfred
2580188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2581194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2582184610Salfred
2583188417Sthompsa	return (0);
2584184610Salfred}
2585184610Salfred
2586184610Salfredstatic void
2587188417Sthompsazyd_start(struct ifnet *ifp)
2588184610Salfred{
2589188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2590188417Sthompsa	struct ieee80211_node *ni;
2591184610Salfred	struct mbuf *m;
2592184610Salfred
2593188417Sthompsa	ZYD_LOCK(sc);
2594188417Sthompsa	for (;;) {
2595188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2596188417Sthompsa		if (m == NULL)
2597184610Salfred			break;
2598188417Sthompsa		if (sc->tx_nfree == 0) {
2599188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2600188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2601184610Salfred			break;
2602184610Salfred		}
2603188417Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2604193803Sweongyo		if (zyd_tx_start(sc, m, ni) != 0) {
2605188417Sthompsa			ieee80211_free_node(ni);
2606188417Sthompsa			ifp->if_oerrors++;
2607188417Sthompsa			break;
2608184610Salfred		}
2609184610Salfred	}
2610188417Sthompsa	ZYD_UNLOCK(sc);
2611184610Salfred}
2612184610Salfred
2613188417Sthompsastatic int
2614188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2615188417Sthompsa	const struct ieee80211_bpf_params *params)
2616184610Salfred{
2617188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2618188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2619188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2620184610Salfred
2621188417Sthompsa	ZYD_LOCK(sc);
2622188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2623188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2624188417Sthompsa		ZYD_UNLOCK(sc);
2625188417Sthompsa		m_freem(m);
2626188417Sthompsa		ieee80211_free_node(ni);
2627188417Sthompsa		return (ENETDOWN);
2628188417Sthompsa	}
2629188417Sthompsa	if (sc->tx_nfree == 0) {
2630188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2631188417Sthompsa		ZYD_UNLOCK(sc);
2632188417Sthompsa		m_freem(m);
2633188417Sthompsa		ieee80211_free_node(ni);
2634188417Sthompsa		return (ENOBUFS);		/* XXX */
2635188417Sthompsa	}
2636188417Sthompsa
2637188417Sthompsa	/*
2638188417Sthompsa	 * Legacy path; interpret frame contents to decide
2639188417Sthompsa	 * precisely how to send the frame.
2640188417Sthompsa	 * XXX raw path
2641188417Sthompsa	 */
2642193803Sweongyo	if (zyd_tx_start(sc, m, ni) != 0) {
2643188417Sthompsa		ZYD_UNLOCK(sc);
2644188417Sthompsa		ifp->if_oerrors++;
2645188417Sthompsa		ieee80211_free_node(ni);
2646188417Sthompsa		return (EIO);
2647188417Sthompsa	}
2648188417Sthompsa	ZYD_UNLOCK(sc);
2649188417Sthompsa	return (0);
2650184610Salfred}
2651184610Salfred
2652184610Salfredstatic int
2653188417Sthompsazyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2654184610Salfred{
2655184610Salfred	struct zyd_softc *sc = ifp->if_softc;
2656184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2657188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
2658246614Shselasky	int error;
2659246614Shselasky	int startall = 0;
2660184610Salfred
2661246614Shselasky	ZYD_LOCK(sc);
2662246614Shselasky	error = (sc->sc_flags & ZYD_FLAG_DETACHED) ? ENXIO : 0;
2663246614Shselasky	ZYD_UNLOCK(sc);
2664246614Shselasky	if (error)
2665246614Shselasky		return (error);
2666246614Shselasky
2667184610Salfred	switch (cmd) {
2668184610Salfred	case SIOCSIFFLAGS:
2669188417Sthompsa		ZYD_LOCK(sc);
2670184610Salfred		if (ifp->if_flags & IFF_UP) {
2671191746Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2672191746Sthompsa				zyd_init_locked(sc);
2673188417Sthompsa				startall = 1;
2674191746Sthompsa			} else
2675191746Sthompsa				zyd_set_multi(sc);
2676184610Salfred		} else {
2677191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2678191746Sthompsa				zyd_stop(sc);
2679184610Salfred		}
2680188417Sthompsa		ZYD_UNLOCK(sc);
2681188417Sthompsa		if (startall)
2682188417Sthompsa			ieee80211_start_all(ic);
2683184610Salfred		break;
2684184610Salfred	case SIOCGIFMEDIA:
2685188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2686184610Salfred		break;
2687188417Sthompsa	case SIOCGIFADDR:
2688184610Salfred		error = ether_ioctl(ifp, cmd, data);
2689184610Salfred		break;
2690188417Sthompsa	default:
2691188417Sthompsa		error = EINVAL;
2692188417Sthompsa		break;
2693184610Salfred	}
2694184610Salfred	return (error);
2695184610Salfred}
2696184610Salfred
2697184610Salfredstatic void
2698191746Sthompsazyd_init_locked(struct zyd_softc *sc)
2699184610Salfred{
2700188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2701188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2702192984Sthompsa	struct usb_config_descriptor *cd;
2703188419Sthompsa	int error;
2704188417Sthompsa	uint32_t val;
2705184610Salfred
2706188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2707184610Salfred
2708188417Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2709188417Sthompsa		error = zyd_loadfirmware(sc);
2710188417Sthompsa		if (error != 0) {
2711188417Sthompsa			device_printf(sc->sc_dev,
2712188417Sthompsa			    "could not load firmware (error=%d)\n", error);
2713188417Sthompsa			goto fail;
2714188417Sthompsa		}
2715188417Sthompsa
2716188417Sthompsa		/* reset device */
2717194228Sthompsa		cd = usbd_get_config_descriptor(sc->sc_udev);
2718194228Sthompsa		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2719188417Sthompsa		    cd->bConfigurationValue);
2720188417Sthompsa		if (error)
2721188417Sthompsa			device_printf(sc->sc_dev, "reset failed, continuing\n");
2722188417Sthompsa
2723188417Sthompsa		error = zyd_hw_init(sc);
2724188417Sthompsa		if (error) {
2725188417Sthompsa			device_printf(sc->sc_dev,
2726188417Sthompsa			    "hardware initialization failed\n");
2727188417Sthompsa			goto fail;
2728188417Sthompsa		}
2729188417Sthompsa
2730188417Sthompsa		device_printf(sc->sc_dev,
2731188417Sthompsa		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2732188417Sthompsa		    "BE%x NP%x Gain%x F%x\n",
2733188417Sthompsa		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2734188417Sthompsa		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2735188417Sthompsa		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2736188417Sthompsa		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2737188417Sthompsa		    sc->sc_cckgain, sc->sc_fix_cr157);
2738188417Sthompsa
2739188417Sthompsa		/* read regulatory domain (currently unused) */
2740188417Sthompsa		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2741188417Sthompsa		sc->sc_regdomain = val >> 16;
2742188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2743188417Sthompsa		    sc->sc_regdomain);
2744188417Sthompsa
2745188417Sthompsa		/* we'll do software WEP decryption for now */
2746188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2747188417Sthompsa		    __func__);
2748188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2749188417Sthompsa
2750188417Sthompsa		sc->sc_flags |= ZYD_FLAG_INITONCE;
2751184610Salfred	}
2752184610Salfred
2753188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2754191746Sthompsa		zyd_stop(sc);
2755188417Sthompsa
2756190526Ssam	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2757190526Ssam	    IF_LLADDR(ifp), ":");
2758190526Ssam	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2759188417Sthompsa	if (error != 0)
2760184610Salfred		return;
2761184610Salfred
2762188417Sthompsa	/* set basic rates */
2763188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
2764188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2765188417Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2766188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2767188417Sthompsa	else	/* assumes 802.11b/g */
2768188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2769184610Salfred
2770188417Sthompsa	/* promiscuous mode */
2771188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2772188417Sthompsa	/* multicast setup */
2773188417Sthompsa	zyd_set_multi(sc);
2774188417Sthompsa	/* set RX filter  */
2775188417Sthompsa	error = zyd_set_rxfilter(sc);
2776188417Sthompsa	if (error != 0)
2777188417Sthompsa		goto fail;
2778184610Salfred
2779188417Sthompsa	/* switch radio transmitter ON */
2780188417Sthompsa	error = zyd_switch_radio(sc, 1);
2781188417Sthompsa	if (error != 0)
2782188417Sthompsa		goto fail;
2783188417Sthompsa	/* set default BSS channel */
2784188417Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2785184610Salfred
2786188417Sthompsa	/*
2787188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2788188417Sthompsa	 */
2789188419Sthompsa	zyd_setup_tx_list(sc);
2790184610Salfred
2791188417Sthompsa	/* enable interrupts */
2792188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2793184610Salfred
2794188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2795188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2796194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2797194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2798194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2799184610Salfred
2800188417Sthompsa	return;
2801184610Salfred
2802191746Sthompsafail:	zyd_stop(sc);
2803188417Sthompsa	return;
2804184610Salfred}
2805184610Salfred
2806184610Salfredstatic void
2807188417Sthompsazyd_init(void *priv)
2808184610Salfred{
2809188417Sthompsa	struct zyd_softc *sc = priv;
2810188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2811188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2812184610Salfred
2813188417Sthompsa	ZYD_LOCK(sc);
2814191746Sthompsa	zyd_init_locked(sc);
2815188417Sthompsa	ZYD_UNLOCK(sc);
2816188417Sthompsa
2817188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2818188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2819184610Salfred}
2820184610Salfred
2821184610Salfredstatic void
2822191746Sthompsazyd_stop(struct zyd_softc *sc)
2823184610Salfred{
2824188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2825188417Sthompsa	int error;
2826184610Salfred
2827188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2828184610Salfred
2829188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2830184610Salfred
2831188417Sthompsa	/*
2832188419Sthompsa	 * Drain all the transfers, if not already drained:
2833188417Sthompsa	 */
2834188419Sthompsa	ZYD_UNLOCK(sc);
2835194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2836194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2837188419Sthompsa	ZYD_LOCK(sc);
2838188417Sthompsa
2839188419Sthompsa	zyd_unsetup_tx_list(sc);
2840188417Sthompsa
2841188419Sthompsa	/* Stop now if the device was never set up */
2842188419Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2843184610Salfred		return;
2844184610Salfred
2845188417Sthompsa	/* switch radio transmitter OFF */
2846188417Sthompsa	error = zyd_switch_radio(sc, 0);
2847188417Sthompsa	if (error != 0)
2848188417Sthompsa		goto fail;
2849188417Sthompsa	/* disable Rx */
2850188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2851188417Sthompsa	/* disable interrupts */
2852188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2853184610Salfred
2854188417Sthompsafail:
2855188417Sthompsa	return;
2856184610Salfred}
2857184610Salfred
2858188417Sthompsastatic int
2859188417Sthompsazyd_loadfirmware(struct zyd_softc *sc)
2860184610Salfred{
2861192984Sthompsa	struct usb_device_request req;
2862188417Sthompsa	size_t size;
2863188417Sthompsa	u_char *fw;
2864188417Sthompsa	uint8_t stat;
2865188417Sthompsa	uint16_t addr;
2866184610Salfred
2867188417Sthompsa	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2868188417Sthompsa		return (0);
2869184610Salfred
2870188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
2871188417Sthompsa		fw = (u_char *)zd1211_firmware;
2872188417Sthompsa		size = sizeof(zd1211_firmware);
2873188417Sthompsa	} else {
2874188417Sthompsa		fw = (u_char *)zd1211b_firmware;
2875188417Sthompsa		size = sizeof(zd1211b_firmware);
2876184610Salfred	}
2877184610Salfred
2878188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2879188417Sthompsa	req.bRequest = ZYD_DOWNLOADREQ;
2880188417Sthompsa	USETW(req.wIndex, 0);
2881184610Salfred
2882188417Sthompsa	addr = ZYD_FIRMWARE_START_ADDR;
2883188417Sthompsa	while (size > 0) {
2884188417Sthompsa		/*
2885188417Sthompsa		 * When the transfer size is 4096 bytes, it is not
2886188417Sthompsa		 * likely to be able to transfer it.
2887188417Sthompsa		 * The cause is port or machine or chip?
2888188417Sthompsa		 */
2889188417Sthompsa		const int mlen = min(size, 64);
2890184610Salfred
2891188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_FW,
2892188417Sthompsa		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2893184610Salfred
2894188417Sthompsa		USETW(req.wValue, addr);
2895188417Sthompsa		USETW(req.wLength, mlen);
2896188419Sthompsa		if (zyd_do_request(sc, &req, fw) != 0)
2897188417Sthompsa			return (EIO);
2898184610Salfred
2899188417Sthompsa		addr += mlen / 2;
2900188417Sthompsa		fw   += mlen;
2901188417Sthompsa		size -= mlen;
2902184610Salfred	}
2903184610Salfred
2904188417Sthompsa	/* check whether the upload succeeded */
2905188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2906188417Sthompsa	req.bRequest = ZYD_DOWNLOADSTS;
2907188417Sthompsa	USETW(req.wValue, 0);
2908188417Sthompsa	USETW(req.wIndex, 0);
2909188417Sthompsa	USETW(req.wLength, sizeof(stat));
2910188419Sthompsa	if (zyd_do_request(sc, &req, &stat) != 0)
2911188417Sthompsa		return (EIO);
2912184610Salfred
2913188417Sthompsa	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2914184610Salfred
2915188417Sthompsa	return (stat & 0x80) ? (EIO) : (0);
2916184610Salfred}
2917184610Salfred
2918184610Salfredstatic void
2919188417Sthompsazyd_scan_start(struct ieee80211com *ic)
2920184610Salfred{
2921191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2922191746Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2923184610Salfred
2924188417Sthompsa	ZYD_LOCK(sc);
2925191746Sthompsa	/* want broadcast address while scanning */
2926191746Sthompsa	zyd_set_bssid(sc, ifp->if_broadcastaddr);
2927188417Sthompsa	ZYD_UNLOCK(sc);
2928184610Salfred}
2929184610Salfred
2930184610Salfredstatic void
2931188417Sthompsazyd_scan_end(struct ieee80211com *ic)
2932184610Salfred{
2933188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2934184610Salfred
2935188417Sthompsa	ZYD_LOCK(sc);
2936191746Sthompsa	/* restore previous bssid */
2937191746Sthompsa	zyd_set_bssid(sc, sc->sc_bssid);
2938188417Sthompsa	ZYD_UNLOCK(sc);
2939184610Salfred}
2940184610Salfred
2941184610Salfredstatic void
2942188417Sthompsazyd_set_channel(struct ieee80211com *ic)
2943184610Salfred{
2944188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2945184610Salfred
2946188417Sthompsa	ZYD_LOCK(sc);
2947191746Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2948188417Sthompsa	ZYD_UNLOCK(sc);
2949184610Salfred}
2950184610Salfred
2951188417Sthompsastatic device_method_t zyd_methods[] = {
2952188417Sthompsa        /* Device interface */
2953188417Sthompsa        DEVMETHOD(device_probe, zyd_match),
2954188417Sthompsa        DEVMETHOD(device_attach, zyd_attach),
2955188417Sthompsa        DEVMETHOD(device_detach, zyd_detach),
2956246614Shselasky	DEVMETHOD_END
2957188417Sthompsa};
2958184610Salfred
2959188417Sthompsastatic driver_t zyd_driver = {
2960233774Shselasky	.name = "zyd",
2961233774Shselasky	.methods = zyd_methods,
2962233774Shselasky	.size = sizeof(struct zyd_softc)
2963188417Sthompsa};
2964184610Salfred
2965188417Sthompsastatic devclass_t zyd_devclass;
2966184610Salfred
2967189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2968188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1);
2969188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1);
2970212122SthompsaMODULE_VERSION(zyd, 1);
2971