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$	*/
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$");
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 <machine/bus.h>
46191746Sthompsa#include <machine/resource.h>
47191746Sthompsa#include <sys/rman.h>
48191746Sthompsa
49191746Sthompsa#include <net/bpf.h>
50191746Sthompsa#include <net/if.h>
51191746Sthompsa#include <net/if_arp.h>
52191746Sthompsa#include <net/ethernet.h>
53191746Sthompsa#include <net/if_dl.h>
54191746Sthompsa#include <net/if_media.h>
55191746Sthompsa#include <net/if_types.h>
56191746Sthompsa
57191746Sthompsa#ifdef INET
58191746Sthompsa#include <netinet/in.h>
59191746Sthompsa#include <netinet/in_systm.h>
60191746Sthompsa#include <netinet/in_var.h>
61191746Sthompsa#include <netinet/if_ether.h>
62191746Sthompsa#include <netinet/ip.h>
63191746Sthompsa#endif
64191746Sthompsa
65191746Sthompsa#include <net80211/ieee80211_var.h>
66191746Sthompsa#include <net80211/ieee80211_regdomain.h>
67191746Sthompsa#include <net80211/ieee80211_radiotap.h>
68206358Srpaulo#include <net80211/ieee80211_ratectl.h>
69191746Sthompsa
70188942Sthompsa#include <dev/usb/usb.h>
71194677Sthompsa#include <dev/usb/usbdi.h>
72194677Sthompsa#include <dev/usb/usbdi_util.h>
73191746Sthompsa#include "usbdevs.h"
74184610Salfred
75188942Sthompsa#include <dev/usb/wlan/if_zydreg.h>
76188942Sthompsa#include <dev/usb/wlan/if_zydfw.h>
77184610Salfred
78207077Sthompsa#ifdef USB_DEBUG
79184610Salfredstatic int zyd_debug = 0;
80184610Salfred
81227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
82192502SthompsaSYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RW, &zyd_debug, 0,
83184610Salfred    "zyd debug level");
84188417Sthompsa
85188417Sthompsaenum {
86188417Sthompsa	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
87188417Sthompsa	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
88188417Sthompsa	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
89188417Sthompsa	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
90188417Sthompsa	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
91188417Sthompsa	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
92188417Sthompsa	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
93188417Sthompsa	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
94188417Sthompsa	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
95188417Sthompsa	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
96188417Sthompsa	ZYD_DEBUG_ANY		= 0xffffffff
97188417Sthompsa};
98188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
99188419Sthompsa	if (zyd_debug & (m))					\
100188417Sthompsa		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
101188417Sthompsa} while (0)
102188417Sthompsa#else
103188417Sthompsa#define	DPRINTF(sc, m, fmt, ...) do {				\
104188417Sthompsa	(void) sc;						\
105188417Sthompsa} while (0)
106184610Salfred#endif
107184610Salfred
108188419Sthompsa#define	zyd_do_request(sc,req,data) \
109194228Sthompsa    usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
110188419Sthompsa
111188417Sthompsastatic device_probe_t zyd_match;
112184610Salfredstatic device_attach_t zyd_attach;
113184610Salfredstatic device_detach_t zyd_detach;
114184610Salfred
115193045Sthompsastatic usb_callback_t zyd_intr_read_callback;
116193045Sthompsastatic usb_callback_t zyd_intr_write_callback;
117193045Sthompsastatic usb_callback_t zyd_bulk_read_callback;
118193045Sthompsastatic usb_callback_t zyd_bulk_write_callback;
119184610Salfred
120185948Sthompsastatic struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
121228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
122228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
123228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
124185948Sthompsastatic void	zyd_vap_delete(struct ieee80211vap *);
125188417Sthompsastatic void	zyd_tx_free(struct zyd_tx_data *, int);
126188419Sthompsastatic void	zyd_setup_tx_list(struct zyd_softc *);
127188419Sthompsastatic void	zyd_unsetup_tx_list(struct zyd_softc *);
128188417Sthompsastatic int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
129188417Sthompsastatic int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
130188601Sthompsa		    void *, int, int);
131188417Sthompsastatic int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
132188417Sthompsastatic int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
133188417Sthompsastatic int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
134188417Sthompsastatic int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
135188417Sthompsastatic int	zyd_rfwrite(struct zyd_softc *, uint32_t);
136188417Sthompsastatic int	zyd_lock_phy(struct zyd_softc *);
137188417Sthompsastatic int	zyd_unlock_phy(struct zyd_softc *);
138188417Sthompsastatic int	zyd_rf_attach(struct zyd_softc *, uint8_t);
139188417Sthompsastatic const char *zyd_rf_name(uint8_t);
140188417Sthompsastatic int	zyd_hw_init(struct zyd_softc *);
141188417Sthompsastatic int	zyd_read_pod(struct zyd_softc *);
142188417Sthompsastatic int	zyd_read_eeprom(struct zyd_softc *);
143188417Sthompsastatic int	zyd_get_macaddr(struct zyd_softc *);
144188417Sthompsastatic int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
145188417Sthompsastatic int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
146188417Sthompsastatic int	zyd_switch_radio(struct zyd_softc *, int);
147188417Sthompsastatic int	zyd_set_led(struct zyd_softc *, int, int);
148188417Sthompsastatic void	zyd_set_multi(struct zyd_softc *);
149188417Sthompsastatic void	zyd_update_mcast(struct ifnet *);
150188417Sthompsastatic int	zyd_set_rxfilter(struct zyd_softc *);
151188417Sthompsastatic void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
152188417Sthompsastatic int	zyd_set_beacon_interval(struct zyd_softc *, int);
153192984Sthompsastatic void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
154193803Sweongyostatic int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
155185948Sthompsa		    struct ieee80211_node *);
156188417Sthompsastatic void	zyd_start(struct ifnet *);
157188417Sthompsastatic int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
158185948Sthompsa		    const struct ieee80211_bpf_params *);
159188417Sthompsastatic int	zyd_ioctl(struct ifnet *, u_long, caddr_t);
160191746Sthompsastatic void	zyd_init_locked(struct zyd_softc *);
161188417Sthompsastatic void	zyd_init(void *);
162191746Sthompsastatic void	zyd_stop(struct zyd_softc *);
163188417Sthompsastatic int	zyd_loadfirmware(struct zyd_softc *);
164188417Sthompsastatic void	zyd_scan_start(struct ieee80211com *);
165188417Sthompsastatic void	zyd_scan_end(struct ieee80211com *);
166188417Sthompsastatic void	zyd_set_channel(struct ieee80211com *);
167188417Sthompsastatic int	zyd_rfmd_init(struct zyd_rf *);
168188417Sthompsastatic int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
169188417Sthompsastatic int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
170188417Sthompsastatic int	zyd_al2230_init(struct zyd_rf *);
171188417Sthompsastatic int	zyd_al2230_switch_radio(struct zyd_rf *, int);
172188417Sthompsastatic int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
173188417Sthompsastatic int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
174188417Sthompsastatic int	zyd_al2230_init_b(struct zyd_rf *);
175188417Sthompsastatic int	zyd_al7230B_init(struct zyd_rf *);
176188417Sthompsastatic int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
177188417Sthompsastatic int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
178188417Sthompsastatic int	zyd_al2210_init(struct zyd_rf *);
179188417Sthompsastatic int	zyd_al2210_switch_radio(struct zyd_rf *, int);
180188417Sthompsastatic int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
181188417Sthompsastatic int	zyd_gct_init(struct zyd_rf *);
182188417Sthompsastatic int	zyd_gct_switch_radio(struct zyd_rf *, int);
183188417Sthompsastatic int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
184193420Sweongyostatic int	zyd_gct_mode(struct zyd_rf *);
185193420Sweongyostatic int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
186193420Sweongyostatic int	zyd_gct_write(struct zyd_rf *, uint16_t);
187193420Sweongyostatic int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
188188417Sthompsastatic int	zyd_maxim2_init(struct zyd_rf *);
189188417Sthompsastatic int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
190188417Sthompsastatic int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
191184610Salfred
192184610Salfredstatic const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
193184610Salfredstatic const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
194184610Salfred
195184610Salfred/* various supported device vendors/products */
196188417Sthompsa#define ZYD_ZD1211	0
197188417Sthompsa#define ZYD_ZD1211B	1
198184610Salfred
199193419Sweongyo#define	ZYD_ZD1211_DEV(v,p)	\
200193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
201193419Sweongyo#define	ZYD_ZD1211B_DEV(v,p)	\
202193419Sweongyo	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
203223486Shselaskystatic const STRUCT_USB_HOST_ID zyd_devs[] = {
204193419Sweongyo	/* ZYD_ZD1211 */
205193419Sweongyo	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
206193419Sweongyo	ZYD_ZD1211_DEV(ABOCOM, WL54),
207193419Sweongyo	ZYD_ZD1211_DEV(ASUS, WL159G),
208193419Sweongyo	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
209193419Sweongyo	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
210193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
211193419Sweongyo	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
212193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
213193419Sweongyo	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
214193419Sweongyo	ZYD_ZD1211_DEV(SAGEM, XG760A),
215193419Sweongyo	ZYD_ZD1211_DEV(SENAO, NUB8301),
216193419Sweongyo	ZYD_ZD1211_DEV(SITECOMEU, WL113),
217193419Sweongyo	ZYD_ZD1211_DEV(SWEEX, ZD1211),
218193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
219193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
220193419Sweongyo	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
221193419Sweongyo	ZYD_ZD1211_DEV(TWINMOS, G240),
222193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
223193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
224193419Sweongyo	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
225193419Sweongyo	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
226193419Sweongyo	ZYD_ZD1211_DEV(ZCOM, ZD1211),
227193419Sweongyo	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
228193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, AG225H),
229193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
230193419Sweongyo	ZYD_ZD1211_DEV(ZYXEL, G200V2),
231193419Sweongyo	/* ZYD_ZD1211B */
232223566Sgavin	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
233193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
234193419Sweongyo	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
235193419Sweongyo	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
236193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
237193419Sweongyo	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
238193419Sweongyo	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
239193419Sweongyo	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
240193419Sweongyo	ZYD_ZD1211B_DEV(MELCO, KG54L),
241193419Sweongyo	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
242193419Sweongyo	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
243193419Sweongyo	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
244193419Sweongyo	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
245193419Sweongyo	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
246193419Sweongyo	ZYD_ZD1211B_DEV(USR, USR5423),
247193419Sweongyo	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
248193419Sweongyo	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
249193419Sweongyo	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
250193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, M202),
251193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G202),
252193419Sweongyo	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
253184610Salfred};
254184610Salfred
255192984Sthompsastatic const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
256188417Sthompsa	[ZYD_BULK_WR] = {
257184610Salfred		.type = UE_BULK,
258184610Salfred		.endpoint = UE_ADDR_ANY,
259184610Salfred		.direction = UE_DIR_OUT,
260190734Sthompsa		.bufsize = ZYD_MAX_TXBUFSZ,
261190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
262190734Sthompsa		.callback = zyd_bulk_write_callback,
263184610Salfred		.ep_index = 0,
264190734Sthompsa		.timeout = 10000,	/* 10 seconds */
265184610Salfred	},
266188417Sthompsa	[ZYD_BULK_RD] = {
267184610Salfred		.type = UE_BULK,
268184610Salfred		.endpoint = UE_ADDR_ANY,
269184610Salfred		.direction = UE_DIR_IN,
270190734Sthompsa		.bufsize = ZYX_MAX_RXBUFSZ,
271190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
272190734Sthompsa		.callback = zyd_bulk_read_callback,
273184610Salfred		.ep_index = 0,
274184610Salfred	},
275188417Sthompsa	[ZYD_INTR_WR] = {
276184610Salfred		.type = UE_BULK_INTR,
277184610Salfred		.endpoint = UE_ADDR_ANY,
278184610Salfred		.direction = UE_DIR_OUT,
279190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
280190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
281190734Sthompsa		.callback = zyd_intr_write_callback,
282190734Sthompsa		.timeout = 1000,	/* 1 second */
283184610Salfred		.ep_index = 1,
284184610Salfred	},
285188417Sthompsa	[ZYD_INTR_RD] = {
286188417Sthompsa		.type = UE_INTERRUPT,
287184610Salfred		.endpoint = UE_ADDR_ANY,
288184610Salfred		.direction = UE_DIR_IN,
289190734Sthompsa		.bufsize = sizeof(struct zyd_cmd),
290190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
291190734Sthompsa		.callback = zyd_intr_read_callback,
292184610Salfred	},
293184610Salfred};
294188417Sthompsa#define zyd_read16_m(sc, val, data)	do {				\
295188417Sthompsa	error = zyd_read16(sc, val, data);				\
296188417Sthompsa	if (error != 0)							\
297188417Sthompsa		goto fail;						\
298188417Sthompsa} while (0)
299188417Sthompsa#define zyd_write16_m(sc, val, data)	do {				\
300188417Sthompsa	error = zyd_write16(sc, val, data);				\
301188417Sthompsa	if (error != 0)							\
302188417Sthompsa		goto fail;						\
303188417Sthompsa} while (0)
304188417Sthompsa#define zyd_read32_m(sc, val, data)	do {				\
305188417Sthompsa	error = zyd_read32(sc, val, data);				\
306188417Sthompsa	if (error != 0)							\
307188417Sthompsa		goto fail;						\
308188417Sthompsa} while (0)
309188417Sthompsa#define zyd_write32_m(sc, val, data)	do {				\
310188417Sthompsa	error = zyd_write32(sc, val, data);				\
311188417Sthompsa	if (error != 0)							\
312188417Sthompsa		goto fail;						\
313188417Sthompsa} while (0)
314184610Salfred
315188417Sthompsastatic int
316188417Sthompsazyd_match(device_t dev)
317184610Salfred{
318192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
319184610Salfred
320192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
321188417Sthompsa		return (ENXIO);
322188419Sthompsa	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
323188417Sthompsa		return (ENXIO);
324188417Sthompsa	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
325188417Sthompsa		return (ENXIO);
326184610Salfred
327194228Sthompsa	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
328184610Salfred}
329184610Salfred
330188417Sthompsastatic int
331188417Sthompsazyd_attach(device_t dev)
332184610Salfred{
333192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
334188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
335191746Sthompsa	struct ifnet *ifp;
336191746Sthompsa	struct ieee80211com *ic;
337191746Sthompsa	uint8_t iface_index, bands;
338188419Sthompsa	int error;
339184610Salfred
340188417Sthompsa	if (uaa->info.bcdDevice < 0x4330) {
341188417Sthompsa		device_printf(dev, "device version mismatch: 0x%X "
342188417Sthompsa		    "(only >= 43.30 supported)\n",
343188417Sthompsa		    uaa->info.bcdDevice);
344188417Sthompsa		return (EINVAL);
345184610Salfred	}
346184610Salfred
347194228Sthompsa	device_set_usb_desc(dev);
348188417Sthompsa	sc->sc_dev = dev;
349188417Sthompsa	sc->sc_udev = uaa->device;
350188417Sthompsa	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
351188419Sthompsa
352188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
353188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
354188417Sthompsa	STAILQ_INIT(&sc->sc_rqh);
355184610Salfred
356188417Sthompsa	iface_index = ZYD_IFACE_INDEX;
357194228Sthompsa	error = usbd_transfer_setup(uaa->device,
358188417Sthompsa	    &iface_index, sc->sc_xfer, zyd_config,
359188417Sthompsa	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
360188417Sthompsa	if (error) {
361188417Sthompsa		device_printf(dev, "could not allocate USB transfers, "
362194228Sthompsa		    "err=%s\n", usbd_errstr(error));
363188419Sthompsa		goto detach;
364184610Salfred	}
365184610Salfred
366188419Sthompsa	ZYD_LOCK(sc);
367188417Sthompsa	if ((error = zyd_get_macaddr(sc)) != 0) {
368188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
369191746Sthompsa		ZYD_UNLOCK(sc);
370191746Sthompsa		goto detach;
371188417Sthompsa	}
372188419Sthompsa	ZYD_UNLOCK(sc);
373188419Sthompsa
374188417Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
375188417Sthompsa	if (ifp == NULL) {
376188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
377191746Sthompsa		goto detach;
378184610Salfred	}
379188417Sthompsa	ifp->if_softc = sc;
380188417Sthompsa	if_initname(ifp, "zyd", device_get_unit(sc->sc_dev));
381188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
382188417Sthompsa	ifp->if_init = zyd_init;
383188417Sthompsa	ifp->if_ioctl = zyd_ioctl;
384188417Sthompsa	ifp->if_start = zyd_start;
385207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
386188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
387184610Salfred
388188417Sthompsa	ic = ifp->if_l2com;
389188417Sthompsa	ic->ic_ifp = ifp;
390188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
391188417Sthompsa	ic->ic_opmode = IEEE80211_M_STA;
392184610Salfred
393188417Sthompsa	/* set device capabilities */
394188417Sthompsa	ic->ic_caps =
395188417Sthompsa		  IEEE80211_C_STA		/* station mode */
396188417Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode */
397188417Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
398188417Sthompsa	        | IEEE80211_C_SHSLOT		/* short slot time supported */
399188417Sthompsa		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
400188417Sthompsa	        | IEEE80211_C_WPA		/* 802.11i */
401188417Sthompsa		;
402184610Salfred
403188417Sthompsa	bands = 0;
404188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
405188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
406188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
407184610Salfred
408190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
409188417Sthompsa	ic->ic_raw_xmit = zyd_raw_xmit;
410188417Sthompsa	ic->ic_scan_start = zyd_scan_start;
411188417Sthompsa	ic->ic_scan_end = zyd_scan_end;
412188417Sthompsa	ic->ic_set_channel = zyd_set_channel;
413184610Salfred
414188417Sthompsa	ic->ic_vap_create = zyd_vap_create;
415188417Sthompsa	ic->ic_vap_delete = zyd_vap_delete;
416188417Sthompsa	ic->ic_update_mcast = zyd_update_mcast;
417188601Sthompsa	ic->ic_update_promisc = zyd_update_mcast;
418184610Salfred
419192468Ssam	ieee80211_radiotap_attach(ic,
420192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
421192468Ssam		ZYD_TX_RADIOTAP_PRESENT,
422192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
423192468Ssam		ZYD_RX_RADIOTAP_PRESENT);
424184610Salfred
425188417Sthompsa	if (bootverbose)
426188417Sthompsa		ieee80211_announce(ic);
427184610Salfred
428191746Sthompsa	return (0);
429191746Sthompsa
430191746Sthompsadetach:
431191746Sthompsa	zyd_detach(dev);
432191746Sthompsa	return (ENXIO);			/* failure */
433184610Salfred}
434184610Salfred
435188417Sthompsastatic int
436188417Sthompsazyd_detach(device_t dev)
437184610Salfred{
438188417Sthompsa	struct zyd_softc *sc = device_get_softc(dev);
439188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
440188601Sthompsa	struct ieee80211com *ic;
441246614Shselasky	unsigned int x;
442184610Salfred
443246614Shselasky	/*
444246614Shselasky	 * Prevent further allocations from RX/TX data
445246614Shselasky	 * lists and ioctls:
446246614Shselasky	 */
447246614Shselasky	ZYD_LOCK(sc);
448246614Shselasky	sc->sc_flags |= ZYD_FLAG_DETACHED;
449246614Shselasky	STAILQ_INIT(&sc->tx_q);
450246614Shselasky	STAILQ_INIT(&sc->tx_free);
451246614Shselasky	ZYD_UNLOCK(sc);
452184610Salfred
453246614Shselasky	/* drain USB transfers */
454246614Shselasky	for (x = 0; x != ZYD_N_TRANSFER; x++)
455246614Shselasky		usbd_transfer_drain(sc->sc_xfer[x]);
456246614Shselasky
457188419Sthompsa	/* free TX list, if any */
458246614Shselasky	ZYD_LOCK(sc);
459188419Sthompsa	zyd_unsetup_tx_list(sc);
460246614Shselasky	ZYD_UNLOCK(sc);
461188419Sthompsa
462246614Shselasky	/* free USB transfers and some data buffers */
463246614Shselasky	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
464246614Shselasky
465188417Sthompsa	if (ifp) {
466188601Sthompsa		ic = ifp->if_l2com;
467188417Sthompsa		ieee80211_ifdetach(ic);
468188417Sthompsa		if_free(ifp);
469184610Salfred	}
470188417Sthompsa	mtx_destroy(&sc->sc_mtx);
471184610Salfred
472188417Sthompsa	return (0);
473184610Salfred}
474184610Salfred
475188417Sthompsastatic struct ieee80211vap *
476228621Sbschmidtzyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
477228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
478228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
479228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
480184610Salfred{
481188417Sthompsa	struct zyd_vap *zvp;
482188417Sthompsa	struct ieee80211vap *vap;
483184610Salfred
484188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
485188417Sthompsa		return (NULL);
486188417Sthompsa	zvp = (struct zyd_vap *) malloc(sizeof(struct zyd_vap),
487188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
488188417Sthompsa	if (zvp == NULL)
489188417Sthompsa		return (NULL);
490188417Sthompsa	vap = &zvp->vap;
491259453Shselasky
492188417Sthompsa	/* enable s/w bmiss handling for sta mode */
493259453Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
494259453Shselasky	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
495259453Shselasky		/* out of memory */
496259453Shselasky		free(zvp, M_80211_VAP);
497259453Shselasky		return (NULL);
498259453Shselasky	}
499184610Salfred
500188417Sthompsa	/* override state transition machine */
501188417Sthompsa	zvp->newstate = vap->iv_newstate;
502188417Sthompsa	vap->iv_newstate = zyd_newstate;
503184610Salfred
504206358Srpaulo	ieee80211_ratectl_init(vap);
505206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
506184610Salfred
507188417Sthompsa	/* complete setup */
508188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change,
509188417Sthompsa	    ieee80211_media_status);
510188417Sthompsa	ic->ic_opmode = opmode;
511188417Sthompsa	return (vap);
512184610Salfred}
513184610Salfred
514184610Salfredstatic void
515188417Sthompsazyd_vap_delete(struct ieee80211vap *vap)
516184610Salfred{
517188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
518184610Salfred
519206358Srpaulo	ieee80211_ratectl_deinit(vap);
520188417Sthompsa	ieee80211_vap_detach(vap);
521188417Sthompsa	free(zvp, M_80211_VAP);
522184610Salfred}
523184610Salfred
524184610Salfredstatic void
525188417Sthompsazyd_tx_free(struct zyd_tx_data *data, int txerr)
526184610Salfred{
527188417Sthompsa	struct zyd_softc *sc = data->sc;
528184610Salfred
529188417Sthompsa	if (data->m != NULL) {
530188417Sthompsa		if (data->m->m_flags & M_TXCB)
531188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
532188417Sthompsa			    txerr ? ETIMEDOUT : 0);
533188417Sthompsa		m_freem(data->m);
534188417Sthompsa		data->m = NULL;
535188417Sthompsa
536188417Sthompsa		ieee80211_free_node(data->ni);
537188417Sthompsa		data->ni = NULL;
538184610Salfred	}
539188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
540188417Sthompsa	sc->tx_nfree++;
541184610Salfred}
542184610Salfred
543188419Sthompsastatic void
544188419Sthompsazyd_setup_tx_list(struct zyd_softc *sc)
545184610Salfred{
546188417Sthompsa	struct zyd_tx_data *data;
547188417Sthompsa	int i;
548184610Salfred
549188417Sthompsa	sc->tx_nfree = 0;
550188417Sthompsa	STAILQ_INIT(&sc->tx_q);
551188417Sthompsa	STAILQ_INIT(&sc->tx_free);
552184610Salfred
553188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
554188417Sthompsa		data = &sc->tx_data[i];
555184610Salfred
556188417Sthompsa		data->sc = sc;
557188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
558188417Sthompsa		sc->tx_nfree++;
559188417Sthompsa	}
560184610Salfred}
561184610Salfred
562184610Salfredstatic void
563188419Sthompsazyd_unsetup_tx_list(struct zyd_softc *sc)
564184610Salfred{
565188417Sthompsa	struct zyd_tx_data *data;
566188417Sthompsa	int i;
567184610Salfred
568188419Sthompsa	/* make sure any subsequent use of the queues will fail */
569188419Sthompsa	sc->tx_nfree = 0;
570188419Sthompsa	STAILQ_INIT(&sc->tx_q);
571188419Sthompsa	STAILQ_INIT(&sc->tx_free);
572184610Salfred
573188419Sthompsa	/* free up all node references and mbufs */
574188417Sthompsa	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
575188417Sthompsa		data = &sc->tx_data[i];
576184610Salfred
577188417Sthompsa		if (data->m != NULL) {
578188417Sthompsa			m_freem(data->m);
579188417Sthompsa			data->m = NULL;
580188417Sthompsa		}
581188417Sthompsa		if (data->ni != NULL) {
582188417Sthompsa			ieee80211_free_node(data->ni);
583188417Sthompsa			data->ni = NULL;
584188417Sthompsa		}
585188417Sthompsa	}
586184610Salfred}
587184610Salfred
588191746Sthompsastatic int
589191746Sthompsazyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
590186730Salfred{
591188417Sthompsa	struct zyd_vap *zvp = ZYD_VAP(vap);
592191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
593191746Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
594188417Sthompsa	int error;
595186730Salfred
596191746Sthompsa	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
597191746Sthompsa	    ieee80211_state_name[vap->iv_state],
598191746Sthompsa	    ieee80211_state_name[nstate]);
599191746Sthompsa
600191746Sthompsa	IEEE80211_UNLOCK(ic);
601191746Sthompsa	ZYD_LOCK(sc);
602191746Sthompsa	switch (nstate) {
603188417Sthompsa	case IEEE80211_S_AUTH:
604188417Sthompsa		zyd_set_chan(sc, ic->ic_curchan);
605188417Sthompsa		break;
606188417Sthompsa	case IEEE80211_S_RUN:
607188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_MONITOR)
608188417Sthompsa			break;
609184610Salfred
610188417Sthompsa		/* turn link LED on */
611188417Sthompsa		error = zyd_set_led(sc, ZYD_LED1, 1);
612188417Sthompsa		if (error != 0)
613191746Sthompsa			break;
614191746Sthompsa
615188417Sthompsa		/* make data LED blink upon Tx */
616188417Sthompsa		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
617191746Sthompsa
618212127Sthompsa		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
619188417Sthompsa		zyd_set_bssid(sc, sc->sc_bssid);
620188417Sthompsa		break;
621188417Sthompsa	default:
622188417Sthompsa		break;
623184610Salfred	}
624188417Sthompsafail:
625188417Sthompsa	ZYD_UNLOCK(sc);
626188417Sthompsa	IEEE80211_LOCK(ic);
627191746Sthompsa	return (zvp->newstate(vap, nstate, arg));
628184610Salfred}
629184610Salfred
630188417Sthompsa/*
631188417Sthompsa * Callback handler for interrupt transfer
632188417Sthompsa */
633184610Salfredstatic void
634194677Sthompsazyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
635184610Salfred{
636194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
637184610Salfred	struct ifnet *ifp = sc->sc_ifp;
638184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
639188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
640184610Salfred	struct ieee80211_node *ni;
641188417Sthompsa	struct zyd_cmd *cmd = &sc->sc_ibuf;
642194677Sthompsa	struct usb_page_cache *pc;
643188417Sthompsa	int datalen;
644194677Sthompsa	int actlen;
645184610Salfred
646194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
647194677Sthompsa
648184610Salfred	switch (USB_GET_STATE(xfer)) {
649184610Salfred	case USB_ST_TRANSFERRED:
650194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
651194677Sthompsa		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
652184610Salfred
653188417Sthompsa		switch (le16toh(cmd->code)) {
654188417Sthompsa		case ZYD_NOTIF_RETRYSTATUS:
655188417Sthompsa		{
656188417Sthompsa			struct zyd_notif_retry *retry =
657188417Sthompsa			    (struct zyd_notif_retry *)cmd->data;
658184610Salfred
659188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
660188417Sthompsa			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
661188417Sthompsa			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
662188417Sthompsa			    le16toh(retry->count)&0xff, le16toh(retry->count));
663184610Salfred
664188417Sthompsa			/*
665188417Sthompsa			 * Find the node to which the packet was sent and
666188417Sthompsa			 * update its retry statistics.  In BSS mode, this node
667188417Sthompsa			 * is the AP we're associated to so no lookup is
668188417Sthompsa			 * actually needed.
669188417Sthompsa			 */
670188417Sthompsa			ni = ieee80211_find_txnode(vap, retry->macaddr);
671188417Sthompsa			if (ni != NULL) {
672206358Srpaulo				int retrycnt =
673206358Srpaulo				    (int)(le16toh(retry->count) & 0xff);
674206358Srpaulo
675206358Srpaulo				ieee80211_ratectl_tx_complete(vap, ni,
676206358Srpaulo				    IEEE80211_RATECTL_TX_FAILURE,
677206358Srpaulo				    &retrycnt, NULL);
678188417Sthompsa				ieee80211_free_node(ni);
679188417Sthompsa			}
680188417Sthompsa			if (le16toh(retry->count) & 0x100)
681188417Sthompsa				ifp->if_oerrors++;	/* too many retries */
682188417Sthompsa			break;
683188417Sthompsa		}
684188417Sthompsa		case ZYD_NOTIF_IORD:
685188417Sthompsa		{
686188417Sthompsa			struct zyd_rq *rqp;
687184610Salfred
688188417Sthompsa			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
689188417Sthompsa				break;	/* HMAC interrupt */
690184610Salfred
691194677Sthompsa			datalen = actlen - sizeof(cmd->code);
692188417Sthompsa			datalen -= 2;	/* XXX: padding? */
693184610Salfred
694188417Sthompsa			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
695233774Shselasky				int i;
696233774Shselasky				int count;
697184610Salfred
698188417Sthompsa				if (rqp->olen != datalen)
699188417Sthompsa					continue;
700233774Shselasky				count = rqp->olen / sizeof(struct zyd_pair);
701233774Shselasky				for (i = 0; i < count; i++) {
702188417Sthompsa					if (*(((const uint16_t *)rqp->idata) + i) !=
703188417Sthompsa					    (((struct zyd_pair *)cmd->data) + i)->reg)
704188417Sthompsa						break;
705184610Salfred				}
706233774Shselasky				if (i != count)
707188417Sthompsa					continue;
708188417Sthompsa				/* copy answer into caller-supplied buffer */
709227461Shselasky				memcpy(rqp->odata, cmd->data, rqp->olen);
710188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_CMD,
711188417Sthompsa				    "command %p complete, data = %*D \n",
712228303Shselasky				    rqp, rqp->olen, (char *)rqp->odata, ":");
713188417Sthompsa				wakeup(rqp);	/* wakeup caller */
714188417Sthompsa				break;
715184610Salfred			}
716188417Sthompsa			if (rqp == NULL) {
717188417Sthompsa				device_printf(sc->sc_dev,
718188417Sthompsa				    "unexpected IORD notification %*D\n",
719188417Sthompsa				    datalen, cmd->data, ":");
720188417Sthompsa			}
721188417Sthompsa			break;
722184610Salfred		}
723188417Sthompsa		default:
724188417Sthompsa			device_printf(sc->sc_dev, "unknown notification %x\n",
725188417Sthompsa			    le16toh(cmd->code));
726188417Sthompsa		}
727184610Salfred
728188417Sthompsa		/* FALLTHROUGH */
729184610Salfred	case USB_ST_SETUP:
730184610Salfredtr_setup:
731194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
732194228Sthompsa		usbd_transfer_submit(xfer);
733188417Sthompsa		break;
734184610Salfred
735188417Sthompsa	default:			/* Error */
736188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
737194677Sthompsa		    usbd_errstr(error));
738188417Sthompsa
739194677Sthompsa		if (error != USB_ERR_CANCELLED) {
740188417Sthompsa			/* try to clear stall first */
741194677Sthompsa			usbd_xfer_set_stall(xfer);
742188417Sthompsa			goto tr_setup;
743184610Salfred		}
744188417Sthompsa		break;
745188417Sthompsa	}
746188417Sthompsa}
747184610Salfred
748188417Sthompsastatic void
749194677Sthompsazyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
750188417Sthompsa{
751194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
752194677Sthompsa	struct zyd_rq *rqp, *cmd;
753194677Sthompsa	struct usb_page_cache *pc;
754184610Salfred
755188417Sthompsa	switch (USB_GET_STATE(xfer)) {
756188417Sthompsa	case USB_ST_TRANSFERRED:
757194677Sthompsa		cmd = usbd_xfer_get_priv(xfer);
758194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
759189452Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
760189452Sthompsa			/* Ensure the cached rq pointer is still valid */
761194677Sthompsa			if (rqp == cmd &&
762189452Sthompsa			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
763189452Sthompsa				wakeup(rqp);	/* wakeup caller */
764189452Sthompsa		}
765184610Salfred
766188417Sthompsa		/* FALLTHROUGH */
767188417Sthompsa	case USB_ST_SETUP:
768188417Sthompsatr_setup:
769188417Sthompsa		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
770188417Sthompsa			if (rqp->flags & ZYD_CMD_FLAG_SENT)
771188417Sthompsa				continue;
772184610Salfred
773194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
774194677Sthompsa			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
775184610Salfred
776194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
777194677Sthompsa			usbd_xfer_set_priv(xfer, rqp);
778188417Sthompsa			rqp->flags |= ZYD_CMD_FLAG_SENT;
779194228Sthompsa			usbd_transfer_submit(xfer);
780188417Sthompsa			break;
781184610Salfred		}
782184610Salfred		break;
783184610Salfred
784184610Salfred	default:			/* Error */
785188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
786194677Sthompsa		    usbd_errstr(error));
787184610Salfred
788194677Sthompsa		if (error != USB_ERR_CANCELLED) {
789184610Salfred			/* try to clear stall first */
790194677Sthompsa			usbd_xfer_set_stall(xfer);
791188417Sthompsa			goto tr_setup;
792184610Salfred		}
793184610Salfred		break;
794184610Salfred	}
795184610Salfred}
796184610Salfred
797188417Sthompsastatic int
798188417Sthompsazyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
799188601Sthompsa    void *odata, int olen, int flags)
800184610Salfred{
801188417Sthompsa	struct zyd_cmd cmd;
802188417Sthompsa	struct zyd_rq rq;
803188417Sthompsa	int error;
804184610Salfred
805233774Shselasky	if (ilen > (int)sizeof(cmd.data))
806188417Sthompsa		return (EINVAL);
807188419Sthompsa
808188417Sthompsa	cmd.code = htole16(code);
809227461Shselasky	memcpy(cmd.data, idata, ilen);
810188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
811188417Sthompsa	    &rq, ilen, idata, ":");
812184610Salfred
813188417Sthompsa	rq.cmd = &cmd;
814188417Sthompsa	rq.idata = idata;
815188417Sthompsa	rq.odata = odata;
816188417Sthompsa	rq.ilen = sizeof(uint16_t) + ilen;
817188417Sthompsa	rq.olen = olen;
818188417Sthompsa	rq.flags = flags;
819188417Sthompsa	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
820194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
821194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
822184610Salfred
823188417Sthompsa	/* wait at most one second for command reply */
824188417Sthompsa	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
825188417Sthompsa	if (error)
826188417Sthompsa		device_printf(sc->sc_dev, "command timeout\n");
827188417Sthompsa	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
828188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
829188417Sthompsa	    &rq, error);
830184610Salfred
831188417Sthompsa	return (error);
832184610Salfred}
833184610Salfred
834184610Salfredstatic int
835188417Sthompsazyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
836184610Salfred{
837188417Sthompsa	struct zyd_pair tmp;
838188417Sthompsa	int error;
839184610Salfred
840188417Sthompsa	reg = htole16(reg);
841188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
842188417Sthompsa	    ZYD_CMD_FLAG_READ);
843188417Sthompsa	if (error == 0)
844188417Sthompsa		*val = le16toh(tmp.val);
845188417Sthompsa	return (error);
846184610Salfred}
847184610Salfred
848184610Salfredstatic int
849188417Sthompsazyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
850184610Salfred{
851188417Sthompsa	struct zyd_pair tmp[2];
852188417Sthompsa	uint16_t regs[2];
853184610Salfred	int error;
854184610Salfred
855188417Sthompsa	regs[0] = htole16(ZYD_REG32_HI(reg));
856188417Sthompsa	regs[1] = htole16(ZYD_REG32_LO(reg));
857188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
858188417Sthompsa	    ZYD_CMD_FLAG_READ);
859188417Sthompsa	if (error == 0)
860188417Sthompsa		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
861188417Sthompsa	return (error);
862188417Sthompsa}
863184610Salfred
864188417Sthompsastatic int
865188417Sthompsazyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
866188417Sthompsa{
867188417Sthompsa	struct zyd_pair pair;
868184610Salfred
869188417Sthompsa	pair.reg = htole16(reg);
870188417Sthompsa	pair.val = htole16(val);
871184610Salfred
872188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
873188417Sthompsa}
874184610Salfred
875188417Sthompsastatic int
876188417Sthompsazyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
877188417Sthompsa{
878188417Sthompsa	struct zyd_pair pair[2];
879184610Salfred
880188417Sthompsa	pair[0].reg = htole16(ZYD_REG32_HI(reg));
881188417Sthompsa	pair[0].val = htole16(val >> 16);
882188417Sthompsa	pair[1].reg = htole16(ZYD_REG32_LO(reg));
883188417Sthompsa	pair[1].val = htole16(val & 0xffff);
884184610Salfred
885188417Sthompsa	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
886184610Salfred}
887184610Salfred
888188417Sthompsastatic int
889188417Sthompsazyd_rfwrite(struct zyd_softc *sc, uint32_t val)
890184610Salfred{
891188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
892188417Sthompsa	struct zyd_rfwrite_cmd req;
893188417Sthompsa	uint16_t cr203;
894188417Sthompsa	int error, i;
895184610Salfred
896188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &cr203);
897188417Sthompsa	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
898188417Sthompsa
899188417Sthompsa	req.code  = htole16(2);
900188417Sthompsa	req.width = htole16(rf->width);
901188417Sthompsa	for (i = 0; i < rf->width; i++) {
902188417Sthompsa		req.bit[i] = htole16(cr203);
903188417Sthompsa		if (val & (1 << (rf->width - 1 - i)))
904188417Sthompsa			req.bit[i] |= htole16(ZYD_RF_DATA);
905188417Sthompsa	}
906188417Sthompsa	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
907188417Sthompsafail:
908188417Sthompsa	return (error);
909184610Salfred}
910184610Salfred
911188417Sthompsastatic int
912188417Sthompsazyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
913184610Salfred{
914188417Sthompsa	int error;
915184610Salfred
916188417Sthompsa	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
917188417Sthompsa	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
918188417Sthompsa	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
919188417Sthompsafail:
920188417Sthompsa	return (error);
921184610Salfred}
922184610Salfred
923188417Sthompsastatic int
924188417Sthompsazyd_lock_phy(struct zyd_softc *sc)
925184610Salfred{
926188417Sthompsa	int error;
927188417Sthompsa	uint32_t tmp;
928186730Salfred
929188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
930188417Sthompsa	tmp &= ~ZYD_UNLOCK_PHY_REGS;
931188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
932188417Sthompsafail:
933188417Sthompsa	return (error);
934184610Salfred}
935184610Salfred
936188417Sthompsastatic int
937188417Sthompsazyd_unlock_phy(struct zyd_softc *sc)
938184610Salfred{
939188417Sthompsa	int error;
940188417Sthompsa	uint32_t tmp;
941184610Salfred
942188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
943188417Sthompsa	tmp |= ZYD_UNLOCK_PHY_REGS;
944188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
945188417Sthompsafail:
946188417Sthompsa	return (error);
947184610Salfred}
948184610Salfred
949184610Salfred/*
950188417Sthompsa * RFMD RF methods.
951184610Salfred */
952188417Sthompsastatic int
953188417Sthompsazyd_rfmd_init(struct zyd_rf *rf)
954184610Salfred{
955233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
956188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
957184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
958184610Salfred	static const uint32_t rfini[] = ZYD_RFMD_RF;
959188417Sthompsa	int i, error;
960184610Salfred
961184610Salfred	/* init RF-dependent PHY registers */
962188417Sthompsa	for (i = 0; i < N(phyini); i++) {
963188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
964184610Salfred	}
965184610Salfred
966184610Salfred	/* init RFMD radio */
967188417Sthompsa	for (i = 0; i < N(rfini); i++) {
968188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
969188417Sthompsa			return (error);
970184610Salfred	}
971188417Sthompsafail:
972188417Sthompsa	return (error);
973188417Sthompsa#undef N
974184610Salfred}
975184610Salfred
976188417Sthompsastatic int
977188417Sthompsazyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
978184610Salfred{
979188417Sthompsa	int error;
980188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
981188417Sthompsa
982188417Sthompsa	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
983188417Sthompsa	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
984188417Sthompsafail:
985188417Sthompsa	return (error);
986184610Salfred}
987184610Salfred
988188417Sthompsastatic int
989188417Sthompsazyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
990184610Salfred{
991188417Sthompsa	int error;
992188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
993184610Salfred	static const struct {
994188417Sthompsa		uint32_t	r1, r2;
995188417Sthompsa	} rfprog[] = ZYD_RFMD_CHANTABLE;
996184610Salfred
997188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
998188417Sthompsa	if (error != 0)
999188417Sthompsa		goto fail;
1000188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1001188417Sthompsa	if (error != 0)
1002188417Sthompsa		goto fail;
1003184610Salfred
1004188417Sthompsafail:
1005188417Sthompsa	return (error);
1006184610Salfred}
1007184610Salfred
1008184610Salfred/*
1009188417Sthompsa * AL2230 RF methods.
1010184610Salfred */
1011188417Sthompsastatic int
1012188417Sthompsazyd_al2230_init(struct zyd_rf *rf)
1013184610Salfred{
1014233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1015188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1016184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1017186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1018186730Salfred	static const struct zyd_phy_pair phypll[] = {
1019188417Sthompsa		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1020188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1021186730Salfred	};
1022186730Salfred	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1023186730Salfred	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1024186730Salfred	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1025188417Sthompsa	int i, error;
1026184610Salfred
1027184610Salfred	/* init RF-dependent PHY registers */
1028188417Sthompsa	for (i = 0; i < N(phyini); i++)
1029188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1030186730Salfred
1031188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1032188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1033188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1034184610Salfred	}
1035188417Sthompsa
1036186730Salfred	/* init AL2230 radio */
1037188417Sthompsa	for (i = 0; i < N(rfini1); i++) {
1038188417Sthompsa		error = zyd_rfwrite(sc, rfini1[i]);
1039188417Sthompsa		if (error != 0)
1040188417Sthompsa			goto fail;
1041188417Sthompsa	}
1042184610Salfred
1043188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1044188417Sthompsa		error = zyd_rfwrite(sc, 0x000824);
1045186730Salfred	else
1046188417Sthompsa		error = zyd_rfwrite(sc, 0x0005a4);
1047188417Sthompsa	if (error != 0)
1048188417Sthompsa		goto fail;
1049186730Salfred
1050188417Sthompsa	for (i = 0; i < N(rfini2); i++) {
1051188417Sthompsa		error = zyd_rfwrite(sc, rfini2[i]);
1052188417Sthompsa		if (error != 0)
1053188417Sthompsa			goto fail;
1054188417Sthompsa	}
1055186730Salfred
1056188417Sthompsa	for (i = 0; i < N(phypll); i++)
1057188417Sthompsa		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1058186730Salfred
1059188417Sthompsa	for (i = 0; i < N(rfini3); i++) {
1060188417Sthompsa		error = zyd_rfwrite(sc, rfini3[i]);
1061188417Sthompsa		if (error != 0)
1062188417Sthompsa			goto fail;
1063188417Sthompsa	}
1064188417Sthompsafail:
1065188417Sthompsa	return (error);
1066188417Sthompsa#undef N
1067184610Salfred}
1068184610Salfred
1069188417Sthompsastatic int
1070188417Sthompsazyd_al2230_fini(struct zyd_rf *rf)
1071186730Salfred{
1072233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1073188417Sthompsa	int error, i;
1074188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1075186730Salfred	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1076186730Salfred
1077188417Sthompsa	for (i = 0; i < N(phy); i++)
1078188417Sthompsa		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1079186730Salfred
1080186730Salfred	if (sc->sc_newphy != 0)
1081188417Sthompsa		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1082188417Sthompsa
1083188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x6);
1084188417Sthompsafail:
1085188417Sthompsa	return (error);
1086188417Sthompsa#undef N
1087186730Salfred}
1088186730Salfred
1089188417Sthompsastatic int
1090188417Sthompsazyd_al2230_init_b(struct zyd_rf *rf)
1091184610Salfred{
1092233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1093188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1094186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1095186730Salfred	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1096186730Salfred	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1097186730Salfred	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1098188417Sthompsa	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1099186730Salfred	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1100186730Salfred	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1101186730Salfred	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1102186730Salfred	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1103188417Sthompsa	int i, error;
1104184610Salfred
1105188417Sthompsa	for (i = 0; i < N(phy1); i++)
1106188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1107186730Salfred
1108184610Salfred	/* init RF-dependent PHY registers */
1109188417Sthompsa	for (i = 0; i < N(phyini); i++)
1110188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1111184610Salfred
1112188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1113188417Sthompsa		for (i = 0; i < N(phy2230s); i++)
1114188417Sthompsa			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1115188417Sthompsa	}
1116186730Salfred
1117188417Sthompsa	for (i = 0; i < 3; i++) {
1118188417Sthompsa		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1119188417Sthompsa		if (error != 0)
1120188417Sthompsa			return (error);
1121188417Sthompsa	}
1122186730Salfred
1123188417Sthompsa	for (i = 0; i < N(rfini_part1); i++) {
1124188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1125188417Sthompsa		if (error != 0)
1126188417Sthompsa			return (error);
1127188417Sthompsa	}
1128186730Salfred
1129188417Sthompsa	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1130188417Sthompsa		error = zyd_rfwrite(sc, 0x241000);
1131186730Salfred	else
1132188417Sthompsa		error = zyd_rfwrite(sc, 0x25a000);
1133188417Sthompsa	if (error != 0)
1134188417Sthompsa		goto fail;
1135186730Salfred
1136188417Sthompsa	for (i = 0; i < N(rfini_part2); i++) {
1137188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1138188417Sthompsa		if (error != 0)
1139188417Sthompsa			return (error);
1140188417Sthompsa	}
1141186730Salfred
1142188417Sthompsa	for (i = 0; i < N(phy2); i++)
1143188417Sthompsa		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1144186730Salfred
1145188417Sthompsa	for (i = 0; i < N(rfini_part3); i++) {
1146188417Sthompsa		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1147188417Sthompsa		if (error != 0)
1148188417Sthompsa			return (error);
1149188417Sthompsa	}
1150186730Salfred
1151188417Sthompsa	for (i = 0; i < N(phy3); i++)
1152188417Sthompsa		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1153186730Salfred
1154188417Sthompsa	error = zyd_al2230_fini(rf);
1155188417Sthompsafail:
1156188417Sthompsa	return (error);
1157188417Sthompsa#undef N
1158184610Salfred}
1159184610Salfred
1160188417Sthompsastatic int
1161188417Sthompsazyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1162184610Salfred{
1163188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1164188417Sthompsa	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1165188417Sthompsa
1166188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1167188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1168188417Sthompsafail:
1169188417Sthompsa	return (error);
1170188417Sthompsa}
1171188417Sthompsa
1172188417Sthompsastatic int
1173188417Sthompsazyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1174188417Sthompsa{
1175233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1176188417Sthompsa	int error, i;
1177188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1178186730Salfred	static const struct zyd_phy_pair phy1[] = {
1179188417Sthompsa		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1180186730Salfred	};
1181184610Salfred	static const struct {
1182188417Sthompsa		uint32_t	r1, r2, r3;
1183188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE;
1184184610Salfred
1185188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1186188417Sthompsa	if (error != 0)
1187188417Sthompsa		goto fail;
1188188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1189188417Sthompsa	if (error != 0)
1190188417Sthompsa		goto fail;
1191188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1192188417Sthompsa	if (error != 0)
1193188417Sthompsa		goto fail;
1194184610Salfred
1195188417Sthompsa	for (i = 0; i < N(phy1); i++)
1196188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1197188417Sthompsafail:
1198188417Sthompsa	return (error);
1199188417Sthompsa#undef N
1200184610Salfred}
1201184610Salfred
1202188417Sthompsastatic int
1203188417Sthompsazyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1204186730Salfred{
1205233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1206188417Sthompsa	int error, i;
1207188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1208186730Salfred	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1209186730Salfred	static const struct {
1210188417Sthompsa		uint32_t	r1, r2, r3;
1211188417Sthompsa	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1212186730Salfred
1213188417Sthompsa	for (i = 0; i < N(phy1); i++)
1214188417Sthompsa		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1215186730Salfred
1216188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1217188417Sthompsa	if (error != 0)
1218188417Sthompsa		goto fail;
1219188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1220188417Sthompsa	if (error != 0)
1221188417Sthompsa		goto fail;
1222188417Sthompsa	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1223188417Sthompsa	if (error != 0)
1224188417Sthompsa		goto fail;
1225188417Sthompsa	error = zyd_al2230_fini(rf);
1226188417Sthompsafail:
1227188417Sthompsa	return (error);
1228188417Sthompsa#undef N
1229186730Salfred}
1230186730Salfred
1231186730Salfred#define	ZYD_AL2230_PHY_BANDEDGE6					\
1232186730Salfred{									\
1233188417Sthompsa	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1234186730Salfred	{ ZYD_CR47,  0x1e }						\
1235186730Salfred}
1236186730Salfred
1237188417Sthompsastatic int
1238188417Sthompsazyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1239186730Salfred{
1240233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1241188417Sthompsa	int error = 0, i;
1242188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1243188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1244188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1245186730Salfred	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1246188601Sthompsa	int chan = ieee80211_chan2ieee(ic, c);
1247186730Salfred
1248188417Sthompsa	if (chan == 1 || chan == 11)
1249186730Salfred		r[0].val = 0x12;
1250188417Sthompsa
1251188417Sthompsa	for (i = 0; i < N(r); i++)
1252188417Sthompsa		zyd_write16_m(sc, r[i].reg, r[i].val);
1253188417Sthompsafail:
1254188417Sthompsa	return (error);
1255188417Sthompsa#undef N
1256186730Salfred}
1257186730Salfred
1258184610Salfred/*
1259184610Salfred * AL7230B RF methods.
1260184610Salfred */
1261188417Sthompsastatic int
1262188417Sthompsazyd_al7230B_init(struct zyd_rf *rf)
1263184610Salfred{
1264233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1265188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1266184610Salfred	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1267184610Salfred	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1268184610Salfred	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1269184610Salfred	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1270184610Salfred	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1271188417Sthompsa	int i, error;
1272184610Salfred
1273184610Salfred	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1274184610Salfred
1275184610Salfred	/* init RF-dependent PHY registers, part one */
1276188417Sthompsa	for (i = 0; i < N(phyini_1); i++)
1277188417Sthompsa		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1278188417Sthompsa
1279184610Salfred	/* init AL7230B radio, part one */
1280188417Sthompsa	for (i = 0; i < N(rfini_1); i++) {
1281188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1282188417Sthompsa			return (error);
1283184610Salfred	}
1284184610Salfred	/* init RF-dependent PHY registers, part two */
1285188417Sthompsa	for (i = 0; i < N(phyini_2); i++)
1286188417Sthompsa		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1287188417Sthompsa
1288184610Salfred	/* init AL7230B radio, part two */
1289188417Sthompsa	for (i = 0; i < N(rfini_2); i++) {
1290188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1291188417Sthompsa			return (error);
1292184610Salfred	}
1293184610Salfred	/* init RF-dependent PHY registers, part three */
1294188417Sthompsa	for (i = 0; i < N(phyini_3); i++)
1295188417Sthompsa		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1296188417Sthompsafail:
1297188417Sthompsa	return (error);
1298188417Sthompsa#undef N
1299184610Salfred}
1300184610Salfred
1301188417Sthompsastatic int
1302188417Sthompsazyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1303184610Salfred{
1304188417Sthompsa	int error;
1305188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1306188417Sthompsa
1307188417Sthompsa	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1308188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1309188417Sthompsafail:
1310188417Sthompsa	return (error);
1311188417Sthompsa}
1312188417Sthompsa
1313188417Sthompsastatic int
1314188417Sthompsazyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1315188417Sthompsa{
1316233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1317188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1318184610Salfred	static const struct {
1319188417Sthompsa		uint32_t	r1, r2;
1320188417Sthompsa	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1321184610Salfred	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1322188417Sthompsa	int i, error;
1323184610Salfred
1324188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x57);
1325188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1326184610Salfred
1327188417Sthompsa	for (i = 0; i < N(rfsc); i++) {
1328188417Sthompsa		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1329188417Sthompsa			return (error);
1330184610Salfred	}
1331184610Salfred
1332188417Sthompsa	zyd_write16_m(sc, ZYD_CR128, 0x14);
1333188417Sthompsa	zyd_write16_m(sc, ZYD_CR129, 0x12);
1334188417Sthompsa	zyd_write16_m(sc, ZYD_CR130, 0x10);
1335188417Sthompsa	zyd_write16_m(sc, ZYD_CR38,  0x38);
1336188417Sthompsa	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1337184610Salfred
1338188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1339188417Sthompsa	if (error != 0)
1340188417Sthompsa		goto fail;
1341188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1342188417Sthompsa	if (error != 0)
1343188417Sthompsa		goto fail;
1344188417Sthompsa	error = zyd_rfwrite(sc, 0x3c9000);
1345188417Sthompsa	if (error != 0)
1346188417Sthompsa		goto fail;
1347184610Salfred
1348188417Sthompsa	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1349188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, 0x06);
1350188417Sthompsa	zyd_write16_m(sc, ZYD_CR240, 0x08);
1351188417Sthompsafail:
1352188417Sthompsa	return (error);
1353188417Sthompsa#undef N
1354184610Salfred}
1355184610Salfred
1356184610Salfred/*
1357184610Salfred * AL2210 RF methods.
1358184610Salfred */
1359188417Sthompsastatic int
1360188417Sthompsazyd_al2210_init(struct zyd_rf *rf)
1361184610Salfred{
1362233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1363188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1364184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1365184610Salfred	static const uint32_t rfini[] = ZYD_AL2210_RF;
1366184610Salfred	uint32_t tmp;
1367188417Sthompsa	int i, error;
1368184610Salfred
1369188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1370184610Salfred
1371184610Salfred	/* init RF-dependent PHY registers */
1372188417Sthompsa	for (i = 0; i < N(phyini); i++)
1373188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1374188417Sthompsa
1375184610Salfred	/* init AL2210 radio */
1376188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1377188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1378188417Sthompsa			return (error);
1379184610Salfred	}
1380188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1381188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1382188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1383188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1384188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1385188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1386188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1387188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1388188417Sthompsafail:
1389188417Sthompsa	return (error);
1390188417Sthompsa#undef N
1391184610Salfred}
1392184610Salfred
1393188417Sthompsastatic int
1394188417Sthompsazyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1395184610Salfred{
1396188417Sthompsa	/* vendor driver does nothing for this RF chip */
1397188417Sthompsa
1398188417Sthompsa	return (0);
1399188417Sthompsa}
1400188417Sthompsa
1401188417Sthompsastatic int
1402188417Sthompsazyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1403188417Sthompsa{
1404188417Sthompsa	int error;
1405188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1406184610Salfred	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1407184610Salfred	uint32_t tmp;
1408184610Salfred
1409188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 2);
1410188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1411188417Sthompsa	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1412188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1413188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1414188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1415188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1416188417Sthompsa	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1417184610Salfred
1418184610Salfred	/* actually set the channel */
1419188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1420188417Sthompsa	if (error != 0)
1421188417Sthompsa		goto fail;
1422184610Salfred
1423188417Sthompsa	zyd_write32_m(sc, ZYD_CR18, 3);
1424188417Sthompsafail:
1425188417Sthompsa	return (error);
1426184610Salfred}
1427184610Salfred
1428184610Salfred/*
1429184610Salfred * GCT RF methods.
1430184610Salfred */
1431188417Sthompsastatic int
1432188417Sthompsazyd_gct_init(struct zyd_rf *rf)
1433184610Salfred{
1434193420Sweongyo#define	ZYD_GCT_INTR_REG	0x85c1
1435233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1436188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1437184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1438184610Salfred	static const uint32_t rfini[] = ZYD_GCT_RF;
1439193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1440193420Sweongyo	int i, idx = -1, error;
1441193420Sweongyo	uint16_t data;
1442184610Salfred
1443184610Salfred	/* init RF-dependent PHY registers */
1444188417Sthompsa	for (i = 0; i < N(phyini); i++)
1445188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1446188417Sthompsa
1447184610Salfred	/* init cgt radio */
1448188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1449188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1450188417Sthompsa			return (error);
1451184610Salfred	}
1452193420Sweongyo
1453193420Sweongyo	error = zyd_gct_mode(rf);
1454193420Sweongyo	if (error != 0)
1455193420Sweongyo		return (error);
1456193420Sweongyo
1457233774Shselasky	for (i = 0; i < (int)(N(vco) - 1); i++) {
1458193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 0);
1459193420Sweongyo		if (error != 0)
1460193420Sweongyo			goto fail;
1461193420Sweongyo		error = zyd_gct_write(rf, vco[i][0]);
1462193420Sweongyo		if (error != 0)
1463193420Sweongyo			goto fail;
1464193420Sweongyo		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1465193420Sweongyo		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1466193420Sweongyo		if ((data & 0xf) == 0) {
1467193420Sweongyo			idx = i;
1468193420Sweongyo			break;
1469193420Sweongyo		}
1470193420Sweongyo	}
1471193420Sweongyo	if (idx == -1) {
1472193420Sweongyo		error = zyd_gct_set_channel_synth(rf, 1, 1);
1473193420Sweongyo		if (error != 0)
1474193420Sweongyo			goto fail;
1475193420Sweongyo		error = zyd_gct_write(rf, 0x6662);
1476193420Sweongyo		if (error != 0)
1477193420Sweongyo			goto fail;
1478193420Sweongyo	}
1479193420Sweongyo
1480193420Sweongyo	rf->idx = idx;
1481193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1482188417Sthompsafail:
1483188417Sthompsa	return (error);
1484188417Sthompsa#undef N
1485193420Sweongyo#undef ZYD_GCT_INTR_REG
1486184610Salfred}
1487184610Salfred
1488188417Sthompsastatic int
1489193420Sweongyozyd_gct_mode(struct zyd_rf *rf)
1490184610Salfred{
1491233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1492193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1493193420Sweongyo	static const uint32_t mode[] = {
1494193420Sweongyo		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1495193420Sweongyo	};
1496193420Sweongyo	int i, error;
1497188417Sthompsa
1498193420Sweongyo	for (i = 0; i < N(mode); i++) {
1499193420Sweongyo		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1500193420Sweongyo			break;
1501193420Sweongyo	}
1502193420Sweongyo	return (error);
1503193420Sweongyo#undef N
1504188417Sthompsa}
1505188417Sthompsa
1506188417Sthompsastatic int
1507193420Sweongyozyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1508188417Sthompsa{
1509193420Sweongyo	int error, idx = chan - 1;
1510188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1511193420Sweongyo	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1512193420Sweongyo	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1513193420Sweongyo	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1514184610Salfred
1515193420Sweongyo	error = zyd_rfwrite(sc,
1516193420Sweongyo	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1517188417Sthompsa	if (error != 0)
1518193420Sweongyo		return (error);
1519193420Sweongyo	return zyd_rfwrite(sc, div_synth[idx]);
1520184610Salfred}
1521184610Salfred
1522188417Sthompsastatic int
1523193420Sweongyozyd_gct_write(struct zyd_rf *rf, uint16_t value)
1524184610Salfred{
1525188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1526184610Salfred
1527193420Sweongyo	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1528184610Salfred}
1529184610Salfred
1530188417Sthompsastatic int
1531193420Sweongyozyd_gct_switch_radio(struct zyd_rf *rf, int on)
1532184610Salfred{
1533193420Sweongyo	int error;
1534193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1535188417Sthompsa
1536193420Sweongyo	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1537193420Sweongyo	if (error != 0)
1538193420Sweongyo		return (error);
1539193420Sweongyo
1540193420Sweongyo	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1541193420Sweongyo	zyd_write16_m(sc, ZYD_CR251,
1542193420Sweongyo	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1543193420Sweongyofail:
1544193420Sweongyo	return (error);
1545188417Sthompsa}
1546188417Sthompsa
1547188417Sthompsastatic int
1548193420Sweongyozyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1549188417Sthompsa{
1550233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1551193420Sweongyo	int error, i;
1552188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1553193420Sweongyo	static const struct zyd_phy_pair cmd[] = {
1554193420Sweongyo		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1555193420Sweongyo		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1556193420Sweongyo	};
1557193420Sweongyo	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1558184610Salfred
1559193420Sweongyo	error = zyd_gct_set_channel_synth(rf, chan, 0);
1560188417Sthompsa	if (error != 0)
1561188417Sthompsa		goto fail;
1562193420Sweongyo	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1563193420Sweongyo	    vco[rf->idx][((chan - 1) / 2)]);
1564188417Sthompsa	if (error != 0)
1565188417Sthompsa		goto fail;
1566193420Sweongyo	error = zyd_gct_mode(rf);
1567193420Sweongyo	if (error != 0)
1568193420Sweongyo		return (error);
1569193420Sweongyo	for (i = 0; i < N(cmd); i++)
1570193420Sweongyo		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1571193420Sweongyo	error = zyd_gct_txgain(rf, chan);
1572193420Sweongyo	if (error != 0)
1573193420Sweongyo		return (error);
1574193420Sweongyo	zyd_write16_m(sc, ZYD_CR203, 0x6);
1575188417Sthompsafail:
1576188417Sthompsa	return (error);
1577188417Sthompsa#undef N
1578184610Salfred}
1579184610Salfred
1580193420Sweongyostatic int
1581193420Sweongyozyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1582193420Sweongyo{
1583193420Sweongyo#define N(a)	(sizeof(a) / sizeof((a)[0]))
1584193420Sweongyo	struct zyd_softc *sc = rf->rf_sc;
1585193420Sweongyo	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1586193420Sweongyo	uint8_t idx = sc->sc_pwrint[chan - 1];
1587193420Sweongyo
1588193420Sweongyo	if (idx >= N(txgain)) {
1589193420Sweongyo		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1590193420Sweongyo		    chan, idx);
1591193420Sweongyo		return 0;
1592193420Sweongyo	}
1593193420Sweongyo
1594193420Sweongyo	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1595193420Sweongyo#undef N
1596193420Sweongyo}
1597193420Sweongyo
1598184610Salfred/*
1599184610Salfred * Maxim2 RF methods.
1600184610Salfred */
1601188417Sthompsastatic int
1602188417Sthompsazyd_maxim2_init(struct zyd_rf *rf)
1603184610Salfred{
1604233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1605188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1606184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1607184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1608184610Salfred	uint16_t tmp;
1609188417Sthompsa	int i, error;
1610184610Salfred
1611184610Salfred	/* init RF-dependent PHY registers */
1612188417Sthompsa	for (i = 0; i < N(phyini); i++)
1613188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1614184610Salfred
1615188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1616188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1617188417Sthompsa
1618184610Salfred	/* init maxim2 radio */
1619188417Sthompsa	for (i = 0; i < N(rfini); i++) {
1620188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1621188417Sthompsa			return (error);
1622184610Salfred	}
1623188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1624188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1625188417Sthompsafail:
1626188417Sthompsa	return (error);
1627188417Sthompsa#undef N
1628184610Salfred}
1629184610Salfred
1630188417Sthompsastatic int
1631188417Sthompsazyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1632184610Salfred{
1633188417Sthompsa
1634188417Sthompsa	/* vendor driver does nothing for this RF chip */
1635188417Sthompsa	return (0);
1636188417Sthompsa}
1637188417Sthompsa
1638188417Sthompsastatic int
1639188417Sthompsazyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1640188417Sthompsa{
1641233774Shselasky#define N(a)	((int)(sizeof(a) / sizeof((a)[0])))
1642188417Sthompsa	struct zyd_softc *sc = rf->rf_sc;
1643184610Salfred	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1644184610Salfred	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1645184610Salfred	static const struct {
1646188417Sthompsa		uint32_t	r1, r2;
1647188417Sthompsa	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1648184610Salfred	uint16_t tmp;
1649188417Sthompsa	int i, error;
1650184610Salfred
1651184610Salfred	/*
1652184610Salfred	 * Do the same as we do when initializing it, except for the channel
1653184610Salfred	 * values coming from the two channel tables.
1654184610Salfred	 */
1655184610Salfred
1656184610Salfred	/* init RF-dependent PHY registers */
1657188417Sthompsa	for (i = 0; i < N(phyini); i++)
1658188417Sthompsa		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1659184610Salfred
1660188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1661188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1662188417Sthompsa
1663184610Salfred	/* first two values taken from the chantables */
1664188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1665188417Sthompsa	if (error != 0)
1666188417Sthompsa		goto fail;
1667188417Sthompsa	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1668188417Sthompsa	if (error != 0)
1669188417Sthompsa		goto fail;
1670184610Salfred
1671184610Salfred	/* init maxim2 radio - skipping the two first values */
1672188417Sthompsa	for (i = 2; i < N(rfini); i++) {
1673188417Sthompsa		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1674188417Sthompsa			return (error);
1675184610Salfred	}
1676188417Sthompsa	zyd_read16_m(sc, ZYD_CR203, &tmp);
1677188417Sthompsa	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1678188417Sthompsafail:
1679188417Sthompsa	return (error);
1680188417Sthompsa#undef N
1681184610Salfred}
1682184610Salfred
1683188417Sthompsastatic int
1684188417Sthompsazyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1685184610Salfred{
1686188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1687184610Salfred
1688188417Sthompsa	rf->rf_sc = sc;
1689193420Sweongyo	rf->update_pwr = 1;
1690188417Sthompsa
1691188417Sthompsa	switch (type) {
1692184610Salfred	case ZYD_RF_RFMD:
1693188417Sthompsa		rf->init         = zyd_rfmd_init;
1694188417Sthompsa		rf->switch_radio = zyd_rfmd_switch_radio;
1695188417Sthompsa		rf->set_channel  = zyd_rfmd_set_channel;
1696188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1697184610Salfred		break;
1698184610Salfred	case ZYD_RF_AL2230:
1699186730Salfred	case ZYD_RF_AL2230S:
1700188417Sthompsa		if (sc->sc_macrev == ZYD_ZD1211B) {
1701188417Sthompsa			rf->init = zyd_al2230_init_b;
1702188417Sthompsa			rf->set_channel = zyd_al2230_set_channel_b;
1703186730Salfred		} else {
1704188417Sthompsa			rf->init = zyd_al2230_init;
1705188417Sthompsa			rf->set_channel = zyd_al2230_set_channel;
1706186730Salfred		}
1707188417Sthompsa		rf->switch_radio = zyd_al2230_switch_radio;
1708188417Sthompsa		rf->bandedge6	 = zyd_al2230_bandedge6;
1709188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1710184610Salfred		break;
1711184610Salfred	case ZYD_RF_AL7230B:
1712188417Sthompsa		rf->init         = zyd_al7230B_init;
1713188417Sthompsa		rf->switch_radio = zyd_al7230B_switch_radio;
1714188417Sthompsa		rf->set_channel  = zyd_al7230B_set_channel;
1715188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1716184610Salfred		break;
1717184610Salfred	case ZYD_RF_AL2210:
1718188417Sthompsa		rf->init         = zyd_al2210_init;
1719188417Sthompsa		rf->switch_radio = zyd_al2210_switch_radio;
1720188417Sthompsa		rf->set_channel  = zyd_al2210_set_channel;
1721188417Sthompsa		rf->width        = 24;	/* 24-bit RF values */
1722184610Salfred		break;
1723193420Sweongyo	case ZYD_RF_MAXIM_NEW:
1724184610Salfred	case ZYD_RF_GCT:
1725188417Sthompsa		rf->init         = zyd_gct_init;
1726188417Sthompsa		rf->switch_radio = zyd_gct_switch_radio;
1727188417Sthompsa		rf->set_channel  = zyd_gct_set_channel;
1728193420Sweongyo		rf->width        = 24;	/* 24-bit RF values */
1729193420Sweongyo		rf->update_pwr   = 0;
1730184610Salfred		break;
1731184610Salfred	case ZYD_RF_MAXIM_NEW2:
1732188417Sthompsa		rf->init         = zyd_maxim2_init;
1733188417Sthompsa		rf->switch_radio = zyd_maxim2_switch_radio;
1734188417Sthompsa		rf->set_channel  = zyd_maxim2_set_channel;
1735188417Sthompsa		rf->width        = 18;	/* 18-bit RF values */
1736184610Salfred		break;
1737184610Salfred	default:
1738188417Sthompsa		device_printf(sc->sc_dev,
1739188417Sthompsa		    "sorry, radio \"%s\" is not supported yet\n",
1740188417Sthompsa		    zyd_rf_name(type));
1741188417Sthompsa		return (EINVAL);
1742184610Salfred	}
1743188417Sthompsa	return (0);
1744188417Sthompsa}
1745184610Salfred
1746188417Sthompsastatic const char *
1747188417Sthompsazyd_rf_name(uint8_t type)
1748188417Sthompsa{
1749188417Sthompsa	static const char * const zyd_rfs[] = {
1750188417Sthompsa		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1751188417Sthompsa		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1752188417Sthompsa		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1753188417Sthompsa		"PHILIPS"
1754188417Sthompsa	};
1755184610Salfred
1756188417Sthompsa	return zyd_rfs[(type > 15) ? 0 : type];
1757184610Salfred}
1758184610Salfred
1759188417Sthompsastatic int
1760188417Sthompsazyd_hw_init(struct zyd_softc *sc)
1761184610Salfred{
1762188417Sthompsa	int error;
1763184610Salfred	const struct zyd_phy_pair *phyp;
1764188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1765188417Sthompsa	uint16_t val;
1766184610Salfred
1767184610Salfred	/* specify that the plug and play is finished */
1768188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1769188417Sthompsa	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1770188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1771188417Sthompsa	    sc->sc_fwbase);
1772184610Salfred
1773184610Salfred	/* retrieve firmware revision number */
1774188417Sthompsa	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1775188417Sthompsa	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1776188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1777186730Salfred	/* set mandatory rates - XXX assumes 802.11b/g */
1778188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1779186730Salfred
1780184610Salfred	/* disable interrupts */
1781188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1782184610Salfred
1783188417Sthompsa	if ((error = zyd_read_pod(sc)) != 0) {
1784188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1785188417Sthompsa		goto fail;
1786184610Salfred	}
1787188417Sthompsa
1788188417Sthompsa	/* PHY init (resetting) */
1789188417Sthompsa	error = zyd_lock_phy(sc);
1790188417Sthompsa	if (error != 0)
1791188417Sthompsa		goto fail;
1792188417Sthompsa	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1793188417Sthompsa	for (; phyp->reg != 0; phyp++)
1794188417Sthompsa		zyd_write16_m(sc, phyp->reg, phyp->val);
1795188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1796188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1797188417Sthompsa		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1798184610Salfred	}
1799188417Sthompsa	error = zyd_unlock_phy(sc);
1800188417Sthompsa	if (error != 0)
1801188417Sthompsa		goto fail;
1802184610Salfred
1803184610Salfred	/* HMAC init */
1804188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1805188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1806188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1807188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1808188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1809188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1810188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1811188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1812188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1813188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1814188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1815188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1816188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1817188417Sthompsa	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1818188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1819188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1820188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1821188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1822188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1823188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1824184610Salfred
1825188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
1826188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1827188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1828186730Salfred	} else {
1829188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1830188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1831188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1832188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1833188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1834188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1835188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1836188417Sthompsa		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1837188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1838186730Salfred	}
1839186730Salfred
1840184610Salfred	/* init beacon interval to 100ms */
1841188417Sthompsa	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1842188417Sthompsa		goto fail;
1843184610Salfred
1844188417Sthompsa	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1845188417Sthompsa		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1846188417Sthompsa		    sc->sc_rfrev);
1847188417Sthompsa		goto fail;
1848188417Sthompsa	}
1849188417Sthompsa
1850188417Sthompsa	/* RF chip init */
1851188417Sthompsa	error = zyd_lock_phy(sc);
1852188417Sthompsa	if (error != 0)
1853188417Sthompsa		goto fail;
1854188417Sthompsa	error = (*rf->init)(rf);
1855188417Sthompsa	if (error != 0) {
1856188417Sthompsa		device_printf(sc->sc_dev,
1857188417Sthompsa		    "radio initialization failed, error %d\n", error);
1858188417Sthompsa		goto fail;
1859188417Sthompsa	}
1860188417Sthompsa	error = zyd_unlock_phy(sc);
1861188417Sthompsa	if (error != 0)
1862188417Sthompsa		goto fail;
1863188417Sthompsa
1864188417Sthompsa	if ((error = zyd_read_eeprom(sc)) != 0) {
1865188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM\n");
1866188417Sthompsa		goto fail;
1867188417Sthompsa	}
1868188417Sthompsa
1869188417Sthompsafail:	return (error);
1870184610Salfred}
1871184610Salfred
1872188417Sthompsastatic int
1873188417Sthompsazyd_read_pod(struct zyd_softc *sc)
1874184610Salfred{
1875188417Sthompsa	int error;
1876184610Salfred	uint32_t tmp;
1877184610Salfred
1878188417Sthompsa	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1879188417Sthompsa	sc->sc_rfrev     = tmp & 0x0f;
1880188417Sthompsa	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1881188417Sthompsa	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1882188417Sthompsa	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1883184610Salfred	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1884188417Sthompsa	sc->sc_parev     = (tmp >> 16) & 0x0f;
1885186730Salfred	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1886188417Sthompsa	sc->sc_newphy    = (tmp >> 31) & 0x01;
1887188417Sthompsa	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1888188417Sthompsafail:
1889188417Sthompsa	return (error);
1890188417Sthompsa}
1891184610Salfred
1892188417Sthompsastatic int
1893188417Sthompsazyd_read_eeprom(struct zyd_softc *sc)
1894188417Sthompsa{
1895188417Sthompsa	uint16_t val;
1896188417Sthompsa	int error, i;
1897184610Salfred
1898184610Salfred	/* read Tx power calibration tables */
1899184610Salfred	for (i = 0; i < 7; i++) {
1900188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1901188417Sthompsa		sc->sc_pwrcal[i * 2] = val >> 8;
1902188417Sthompsa		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1903188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1904188417Sthompsa		sc->sc_pwrint[i * 2] = val >> 8;
1905188417Sthompsa		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1906188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1907188417Sthompsa		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1908188417Sthompsa		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1909188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1910188417Sthompsa		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1911188417Sthompsa		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1912188417Sthompsa		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1913188417Sthompsa		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1914188417Sthompsa		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1915184610Salfred	}
1916188417Sthompsafail:
1917188417Sthompsa	return (error);
1918184610Salfred}
1919184610Salfred
1920188417Sthompsastatic int
1921188417Sthompsazyd_get_macaddr(struct zyd_softc *sc)
1922186730Salfred{
1923192984Sthompsa	struct usb_device_request req;
1924193045Sthompsa	usb_error_t error;
1925186730Salfred
1926186730Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1927186730Salfred	req.bRequest = ZYD_READFWDATAREQ;
1928186730Salfred	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1929186730Salfred	USETW(req.wIndex, 0);
1930186730Salfred	USETW(req.wLength, IEEE80211_ADDR_LEN);
1931186730Salfred
1932188419Sthompsa	error = zyd_do_request(sc, &req, sc->sc_bssid);
1933188417Sthompsa	if (error != 0) {
1934188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1935194228Sthompsa		    usbd_errstr(error));
1936188417Sthompsa	}
1937188417Sthompsa
1938188417Sthompsa	return (error);
1939186730Salfred}
1940186730Salfred
1941188417Sthompsastatic int
1942188417Sthompsazyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1943184610Salfred{
1944188417Sthompsa	int error;
1945184610Salfred	uint32_t tmp;
1946184610Salfred
1947188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1948188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1949188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1950188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1951188417Sthompsafail:
1952188417Sthompsa	return (error);
1953188417Sthompsa}
1954184610Salfred
1955188417Sthompsastatic int
1956188417Sthompsazyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1957188417Sthompsa{
1958188417Sthompsa	int error;
1959188417Sthompsa	uint32_t tmp;
1960188417Sthompsa
1961188417Sthompsa	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1962188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1963188417Sthompsa	tmp = addr[5] << 8 | addr[4];
1964188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1965188417Sthompsafail:
1966188417Sthompsa	return (error);
1967184610Salfred}
1968184610Salfred
1969188417Sthompsastatic int
1970188417Sthompsazyd_switch_radio(struct zyd_softc *sc, int on)
1971184610Salfred{
1972188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
1973188417Sthompsa	int error;
1974188417Sthompsa
1975188417Sthompsa	error = zyd_lock_phy(sc);
1976188417Sthompsa	if (error != 0)
1977188417Sthompsa		goto fail;
1978188417Sthompsa	error = (*rf->switch_radio)(rf, on);
1979188417Sthompsa	if (error != 0)
1980188417Sthompsa		goto fail;
1981188417Sthompsa	error = zyd_unlock_phy(sc);
1982188417Sthompsafail:
1983188417Sthompsa	return (error);
1984184610Salfred}
1985184610Salfred
1986188417Sthompsastatic int
1987188417Sthompsazyd_set_led(struct zyd_softc *sc, int which, int on)
1988184610Salfred{
1989188417Sthompsa	int error;
1990184610Salfred	uint32_t tmp;
1991184610Salfred
1992188417Sthompsa	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1993188417Sthompsa	tmp &= ~which;
1994188417Sthompsa	if (on)
1995188417Sthompsa		tmp |= which;
1996188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1997188417Sthompsafail:
1998188417Sthompsa	return (error);
1999188417Sthompsa}
2000184610Salfred
2001188417Sthompsastatic void
2002188417Sthompsazyd_set_multi(struct zyd_softc *sc)
2003184610Salfred{
2004188417Sthompsa	int error;
2005188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2006188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2007188417Sthompsa	struct ifmultiaddr *ifma;
2008188417Sthompsa	uint32_t low, high;
2009188417Sthompsa	uint8_t v;
2010184610Salfred
2011188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2012188417Sthompsa		return;
2013184610Salfred
2014188417Sthompsa	low = 0x00000000;
2015188417Sthompsa	high = 0x80000000;
2016184610Salfred
2017188417Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
2018188417Sthompsa	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC))) {
2019188417Sthompsa		low = 0xffffffff;
2020188417Sthompsa		high = 0xffffffff;
2021184610Salfred	} else {
2022195049Srwatson		if_maddr_rlock(ifp);
2023188417Sthompsa		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2024188417Sthompsa			if (ifma->ifma_addr->sa_family != AF_LINK)
2025188417Sthompsa				continue;
2026188417Sthompsa			v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2027188417Sthompsa			    ifma->ifma_addr))[5] >> 2;
2028188417Sthompsa			if (v < 32)
2029188417Sthompsa				low |= 1 << v;
2030188417Sthompsa			else
2031188417Sthompsa				high |= 1 << (v - 32);
2032188417Sthompsa		}
2033195049Srwatson		if_maddr_runlock(ifp);
2034184610Salfred	}
2035184610Salfred
2036188417Sthompsa	/* reprogram multicast global hash table */
2037188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2038188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2039188417Sthompsafail:
2040188417Sthompsa	if (error != 0)
2041188417Sthompsa		device_printf(sc->sc_dev,
2042188417Sthompsa		    "could not set multicast hash table\n");
2043188417Sthompsa}
2044184610Salfred
2045188417Sthompsastatic void
2046188417Sthompsazyd_update_mcast(struct ifnet *ifp)
2047188417Sthompsa{
2048188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2049184610Salfred
2050188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2051184610Salfred		return;
2052184610Salfred
2053188417Sthompsa	ZYD_LOCK(sc);
2054191746Sthompsa	zyd_set_multi(sc);
2055188417Sthompsa	ZYD_UNLOCK(sc);
2056188417Sthompsa}
2057184610Salfred
2058188417Sthompsastatic int
2059188417Sthompsazyd_set_rxfilter(struct zyd_softc *sc)
2060188417Sthompsa{
2061188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2062188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2063188417Sthompsa	uint32_t rxfilter;
2064184610Salfred
2065188417Sthompsa	switch (ic->ic_opmode) {
2066188417Sthompsa	case IEEE80211_M_STA:
2067188417Sthompsa		rxfilter = ZYD_FILTER_BSS;
2068188417Sthompsa		break;
2069188417Sthompsa	case IEEE80211_M_IBSS:
2070188417Sthompsa	case IEEE80211_M_HOSTAP:
2071188417Sthompsa		rxfilter = ZYD_FILTER_HOSTAP;
2072188417Sthompsa		break;
2073188417Sthompsa	case IEEE80211_M_MONITOR:
2074188417Sthompsa		rxfilter = ZYD_FILTER_MONITOR;
2075188417Sthompsa		break;
2076188417Sthompsa	default:
2077188417Sthompsa		/* should not get there */
2078188417Sthompsa		return (EINVAL);
2079184610Salfred	}
2080188417Sthompsa	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2081188417Sthompsa}
2082184610Salfred
2083188417Sthompsastatic void
2084188417Sthompsazyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2085188417Sthompsa{
2086188417Sthompsa	int error;
2087188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2088188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2089188417Sthompsa	struct zyd_rf *rf = &sc->sc_rf;
2090188417Sthompsa	uint32_t tmp;
2091188601Sthompsa	int chan;
2092184610Salfred
2093188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
2094188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2095188417Sthompsa		/* XXX should NEVER happen */
2096188417Sthompsa		device_printf(sc->sc_dev,
2097188417Sthompsa		    "%s: invalid channel %x\n", __func__, chan);
2098188417Sthompsa		return;
2099188417Sthompsa	}
2100184610Salfred
2101188417Sthompsa	error = zyd_lock_phy(sc);
2102188417Sthompsa	if (error != 0)
2103188417Sthompsa		goto fail;
2104184610Salfred
2105188417Sthompsa	error = (*rf->set_channel)(rf, chan);
2106188417Sthompsa	if (error != 0)
2107188417Sthompsa		goto fail;
2108184610Salfred
2109193420Sweongyo	if (rf->update_pwr) {
2110193420Sweongyo		/* update Tx power */
2111193420Sweongyo		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2112184610Salfred
2113193420Sweongyo		if (sc->sc_macrev == ZYD_ZD1211B) {
2114193420Sweongyo			zyd_write16_m(sc, ZYD_CR67,
2115193420Sweongyo			    sc->sc_ofdm36_cal[chan - 1]);
2116193420Sweongyo			zyd_write16_m(sc, ZYD_CR66,
2117193420Sweongyo			    sc->sc_ofdm48_cal[chan - 1]);
2118193420Sweongyo			zyd_write16_m(sc, ZYD_CR65,
2119193420Sweongyo			    sc->sc_ofdm54_cal[chan - 1]);
2120193420Sweongyo			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2121193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x28);
2122193420Sweongyo			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2123193420Sweongyo		}
2124188417Sthompsa	}
2125188417Sthompsa	if (sc->sc_cckgain) {
2126188417Sthompsa		/* set CCK baseband gain from EEPROM */
2127188417Sthompsa		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2128188417Sthompsa			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2129188417Sthompsa	}
2130188417Sthompsa	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2131188417Sthompsa		error = (*rf->bandedge6)(rf, c);
2132188417Sthompsa		if (error != 0)
2133188417Sthompsa			goto fail;
2134188417Sthompsa	}
2135188417Sthompsa	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2136184610Salfred
2137188417Sthompsa	error = zyd_unlock_phy(sc);
2138188417Sthompsa	if (error != 0)
2139188417Sthompsa		goto fail;
2140184610Salfred
2141188417Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2142188417Sthompsa	    htole16(c->ic_freq);
2143188417Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2144188417Sthompsa	    htole16(c->ic_flags);
2145188417Sthompsafail:
2146184610Salfred	return;
2147184610Salfred}
2148184610Salfred
2149184610Salfredstatic int
2150188417Sthompsazyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2151184610Salfred{
2152188417Sthompsa	int error;
2153188417Sthompsa	uint32_t val;
2154184610Salfred
2155188417Sthompsa	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2156188417Sthompsa	sc->sc_atim_wnd = val;
2157188417Sthompsa	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2158188417Sthompsa	sc->sc_pre_tbtt = val;
2159188417Sthompsa	sc->sc_bcn_int = bintval;
2160184610Salfred
2161188417Sthompsa	if (sc->sc_bcn_int <= 5)
2162188417Sthompsa		sc->sc_bcn_int = 5;
2163188417Sthompsa	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2164188417Sthompsa		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2165188417Sthompsa	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2166188417Sthompsa		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2167184610Salfred
2168188417Sthompsa	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2169188417Sthompsa	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2170188417Sthompsa	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2171188417Sthompsafail:
2172188417Sthompsa	return (error);
2173188417Sthompsa}
2174184610Salfred
2175188417Sthompsastatic void
2176192984Sthompsazyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2177188417Sthompsa{
2178194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2179188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2180192468Ssam	struct ieee80211com *ic = ifp->if_l2com;
2181188417Sthompsa	struct zyd_plcphdr plcp;
2182188417Sthompsa	struct zyd_rx_stat stat;
2183194677Sthompsa	struct usb_page_cache *pc;
2184188417Sthompsa	struct mbuf *m;
2185188417Sthompsa	int rlen, rssi;
2186184610Salfred
2187188417Sthompsa	if (len < ZYD_MIN_FRAGSZ) {
2188188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2189188417Sthompsa		    device_get_nameunit(sc->sc_dev), len);
2190188417Sthompsa		ifp->if_ierrors++;
2191188417Sthompsa		return;
2192188417Sthompsa	}
2193194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
2194194677Sthompsa	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2195194677Sthompsa	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2196184610Salfred
2197188417Sthompsa	if (stat.flags & ZYD_RX_ERROR) {
2198188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV,
2199188417Sthompsa		    "%s: RX status indicated error (%x)\n",
2200188417Sthompsa		    device_get_nameunit(sc->sc_dev), stat.flags);
2201188417Sthompsa		ifp->if_ierrors++;
2202188417Sthompsa		return;
2203188417Sthompsa	}
2204184610Salfred
2205188417Sthompsa	/* compute actual frame length */
2206188417Sthompsa	rlen = len - sizeof(struct zyd_plcphdr) -
2207188417Sthompsa	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2208184610Salfred
2209188417Sthompsa	/* allocate a mbuf to store the frame */
2210233774Shselasky	if (rlen > (int)MCLBYTES) {
2211188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2212188417Sthompsa		    device_get_nameunit(sc->sc_dev), rlen);
2213188417Sthompsa		ifp->if_ierrors++;
2214188417Sthompsa		return;
2215233774Shselasky	} else if (rlen > (int)MHLEN)
2216243857Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2217188417Sthompsa	else
2218243857Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
2219188417Sthompsa	if (m == NULL) {
2220188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2221188417Sthompsa		    device_get_nameunit(sc->sc_dev));
2222188417Sthompsa		ifp->if_ierrors++;
2223188417Sthompsa		return;
2224184610Salfred	}
2225188417Sthompsa	m->m_pkthdr.rcvif = ifp;
2226188417Sthompsa	m->m_pkthdr.len = m->m_len = rlen;
2227194677Sthompsa	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2228184610Salfred
2229192468Ssam	if (ieee80211_radiotap_active(ic)) {
2230188417Sthompsa		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2231184610Salfred
2232188417Sthompsa		tap->wr_flags = 0;
2233188417Sthompsa		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2234188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2235188417Sthompsa		/* XXX toss, no way to express errors */
2236188417Sthompsa		if (stat.flags & ZYD_RX_DECRYPTERR)
2237188417Sthompsa			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2238188417Sthompsa		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2239188417Sthompsa		    (stat.flags & ZYD_RX_OFDM) ?
2240188417Sthompsa			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2241188417Sthompsa		tap->wr_antsignal = stat.rssi + -95;
2242188417Sthompsa		tap->wr_antnoise = -95;	/* XXX */
2243188417Sthompsa	}
2244188417Sthompsa	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2245184610Salfred
2246188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2247188417Sthompsa	sc->sc_rx_data[sc->sc_rx_count].m = m;
2248188417Sthompsa	sc->sc_rx_count++;
2249184610Salfred}
2250184610Salfred
2251184610Salfredstatic void
2252194677Sthompsazyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2253184610Salfred{
2254194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2255184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2256184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2257188417Sthompsa	struct ieee80211_node *ni;
2258188417Sthompsa	struct zyd_rx_desc desc;
2259188417Sthompsa	struct mbuf *m;
2260194677Sthompsa	struct usb_page_cache *pc;
2261188417Sthompsa	uint32_t offset;
2262188417Sthompsa	uint8_t rssi;
2263188417Sthompsa	int8_t nf;
2264188417Sthompsa	int i;
2265194677Sthompsa	int actlen;
2266184610Salfred
2267194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2268194677Sthompsa
2269188417Sthompsa	sc->sc_rx_count = 0;
2270188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2271188417Sthompsa	case USB_ST_TRANSFERRED:
2272194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2273194677Sthompsa		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2274184610Salfred
2275188417Sthompsa		offset = 0;
2276188417Sthompsa		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2277188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2278188417Sthompsa			    "%s: received multi-frame transfer\n", __func__);
2279184610Salfred
2280188417Sthompsa			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2281188417Sthompsa				uint16_t len16 = UGETW(desc.len[i]);
2282184610Salfred
2283194677Sthompsa				if (len16 == 0 || len16 > actlen)
2284188417Sthompsa					break;
2285184610Salfred
2286188417Sthompsa				zyd_rx_data(xfer, offset, len16);
2287184610Salfred
2288188417Sthompsa				/* next frame is aligned on a 32-bit boundary */
2289188417Sthompsa				len16 = (len16 + 3) & ~3;
2290188417Sthompsa				offset += len16;
2291194677Sthompsa				if (len16 > actlen)
2292188417Sthompsa					break;
2293194677Sthompsa				actlen -= len16;
2294188417Sthompsa			}
2295188417Sthompsa		} else {
2296188417Sthompsa			DPRINTF(sc, ZYD_DEBUG_RECV,
2297188417Sthompsa			    "%s: received single-frame transfer\n", __func__);
2298184610Salfred
2299194677Sthompsa			zyd_rx_data(xfer, 0, actlen);
2300188417Sthompsa		}
2301188417Sthompsa		/* FALLTHROUGH */
2302188417Sthompsa	case USB_ST_SETUP:
2303188417Sthompsatr_setup:
2304194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2305194228Sthompsa		usbd_transfer_submit(xfer);
2306184610Salfred
2307188417Sthompsa		/*
2308188417Sthompsa		 * At the end of a USB callback it is always safe to unlock
2309188417Sthompsa		 * the private mutex of a device! That is why we do the
2310188417Sthompsa		 * "ieee80211_input" here, and not some lines up!
2311188417Sthompsa		 */
2312188417Sthompsa		ZYD_UNLOCK(sc);
2313188417Sthompsa		for (i = 0; i < sc->sc_rx_count; i++) {
2314188417Sthompsa			rssi = sc->sc_rx_data[i].rssi;
2315188417Sthompsa			m = sc->sc_rx_data[i].m;
2316188417Sthompsa			sc->sc_rx_data[i].m = NULL;
2317184610Salfred
2318188417Sthompsa			nf = -95;	/* XXX */
2319184610Salfred
2320188417Sthompsa			ni = ieee80211_find_rxnode(ic,
2321188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
2322188417Sthompsa			if (ni != NULL) {
2323192468Ssam				(void)ieee80211_input(ni, m, rssi, nf);
2324188417Sthompsa				ieee80211_free_node(ni);
2325188417Sthompsa			} else
2326192468Ssam				(void)ieee80211_input_all(ic, m, rssi, nf);
2327188417Sthompsa		}
2328198099Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2329198099Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
2330198099Sweongyo			zyd_start(ifp);
2331188417Sthompsa		ZYD_LOCK(sc);
2332188417Sthompsa		break;
2333184610Salfred
2334188417Sthompsa	default:			/* Error */
2335194677Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2336184610Salfred
2337194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2338188417Sthompsa			/* try to clear stall first */
2339194677Sthompsa			usbd_xfer_set_stall(xfer);
2340188417Sthompsa			goto tr_setup;
2341184610Salfred		}
2342188417Sthompsa		break;
2343184610Salfred	}
2344184610Salfred}
2345184610Salfred
2346184610Salfredstatic uint8_t
2347193803Sweongyozyd_plcp_signal(struct zyd_softc *sc, int rate)
2348184610Salfred{
2349184610Salfred	switch (rate) {
2350188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2351184610Salfred	case 12:
2352184610Salfred		return (0xb);
2353184610Salfred	case 18:
2354184610Salfred		return (0xf);
2355184610Salfred	case 24:
2356184610Salfred		return (0xa);
2357184610Salfred	case 36:
2358184610Salfred		return (0xe);
2359184610Salfred	case 48:
2360184610Salfred		return (0x9);
2361184610Salfred	case 72:
2362184610Salfred		return (0xd);
2363184610Salfred	case 96:
2364184610Salfred		return (0x8);
2365184610Salfred	case 108:
2366184610Salfred		return (0xc);
2367188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
2368188417Sthompsa	case 2:
2369188417Sthompsa		return (0x0);
2370188417Sthompsa	case 4:
2371188417Sthompsa		return (0x1);
2372188417Sthompsa	case 11:
2373188417Sthompsa		return (0x2);
2374188417Sthompsa	case 22:
2375188417Sthompsa		return (0x3);
2376184610Salfred	}
2377184610Salfred
2378193803Sweongyo	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2379193803Sweongyo	return (0x0);
2380184610Salfred}
2381184610Salfred
2382184610Salfredstatic void
2383194677Sthompsazyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2384184610Salfred{
2385194677Sthompsa	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2386184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2387192468Ssam	struct ieee80211vap *vap;
2388188417Sthompsa	struct zyd_tx_data *data;
2389188417Sthompsa	struct mbuf *m;
2390194677Sthompsa	struct usb_page_cache *pc;
2391194677Sthompsa	int actlen;
2392184610Salfred
2393194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2394194677Sthompsa
2395188417Sthompsa	switch (USB_GET_STATE(xfer)) {
2396188417Sthompsa	case USB_ST_TRANSFERRED:
2397188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2398194677Sthompsa		    actlen);
2399184610Salfred
2400188417Sthompsa		/* free resources */
2401194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2402188417Sthompsa		zyd_tx_free(data, 0);
2403194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2404184610Salfred
2405188417Sthompsa		ifp->if_opackets++;
2406188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2407184610Salfred
2408188417Sthompsa		/* FALLTHROUGH */
2409188417Sthompsa	case USB_ST_SETUP:
2410188417Sthompsatr_setup:
2411188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
2412188417Sthompsa		if (data) {
2413188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2414188417Sthompsa			m = data->m;
2415184610Salfred
2416233774Shselasky			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2417188417Sthompsa				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2418188417Sthompsa				    m->m_pkthdr.len);
2419188417Sthompsa				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2420188417Sthompsa			}
2421194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
2422194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2423194677Sthompsa			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2424188417Sthompsa			    m->m_pkthdr.len);
2425184610Salfred
2426192468Ssam			vap = data->ni->ni_vap;
2427192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
2428188417Sthompsa				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2429184610Salfred
2430188417Sthompsa				tap->wt_flags = 0;
2431188417Sthompsa				tap->wt_rate = data->rate;
2432184610Salfred
2433192468Ssam				ieee80211_radiotap_tx(vap, m);
2434188417Sthompsa			}
2435184610Salfred
2436194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2437194677Sthompsa			usbd_xfer_set_priv(xfer, data);
2438194228Sthompsa			usbd_transfer_submit(xfer);
2439188417Sthompsa		}
2440198099Sweongyo		ZYD_UNLOCK(sc);
2441198099Sweongyo		zyd_start(ifp);
2442198099Sweongyo		ZYD_LOCK(sc);
2443188417Sthompsa		break;
2444184610Salfred
2445188417Sthompsa	default:			/* Error */
2446188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2447194677Sthompsa		    usbd_errstr(error));
2448184610Salfred
2449188419Sthompsa		ifp->if_oerrors++;
2450194677Sthompsa		data = usbd_xfer_get_priv(xfer);
2451194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
2452188419Sthompsa		if (data != NULL)
2453194677Sthompsa			zyd_tx_free(data, error);
2454188419Sthompsa
2455203141Sthompsa		if (error != USB_ERR_CANCELLED) {
2456203141Sthompsa			if (error == USB_ERR_TIMEOUT)
2457203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
2458203141Sthompsa
2459203141Sthompsa			/*
2460203141Sthompsa			 * Try to clear stall first, also if other
2461203141Sthompsa			 * errors occur, hence clearing stall
2462203141Sthompsa			 * introduces a 50 ms delay:
2463203141Sthompsa			 */
2464194677Sthompsa			usbd_xfer_set_stall(xfer);
2465188417Sthompsa			goto tr_setup;
2466188417Sthompsa		}
2467184610Salfred		break;
2468184610Salfred	}
2469184610Salfred}
2470184610Salfred
2471188417Sthompsastatic int
2472193803Sweongyozyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2473184610Salfred{
2474188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2475188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2476188417Sthompsa	struct zyd_tx_desc *desc;
2477188417Sthompsa	struct zyd_tx_data *data;
2478188417Sthompsa	struct ieee80211_frame *wh;
2479188417Sthompsa	const struct ieee80211_txparam *tp;
2480188417Sthompsa	struct ieee80211_key *k;
2481188417Sthompsa	int rate, totlen;
2482269266Shselasky	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2483193803Sweongyo	uint8_t phy;
2484188417Sthompsa	uint16_t pktlen;
2485193803Sweongyo	uint32_t bits;
2486184610Salfred
2487188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
2488188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
2489188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2490188417Sthompsa	sc->tx_nfree--;
2491184610Salfred
2492193803Sweongyo	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT ||
2493193803Sweongyo	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_CTL) {
2494193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2495193803Sweongyo		rate = tp->mgmtrate;
2496188417Sthompsa	} else {
2497193803Sweongyo		tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2498193803Sweongyo		/* for data frames */
2499193803Sweongyo		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2500193803Sweongyo			rate = tp->mcastrate;
2501193803Sweongyo		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2502193803Sweongyo			rate = tp->ucastrate;
2503193803Sweongyo		else {
2504206358Srpaulo			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2505193803Sweongyo			rate = ni->ni_txrate;
2506193803Sweongyo		}
2507184610Salfred	}
2508184610Salfred
2509262007Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2510188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
2511188417Sthompsa		if (k == NULL) {
2512188417Sthompsa			m_freem(m0);
2513188417Sthompsa			return (ENOBUFS);
2514188417Sthompsa		}
2515188417Sthompsa		/* packet header may have moved, reset our local pointer */
2516188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
2517184610Salfred	}
2518184610Salfred
2519188417Sthompsa	data->ni = ni;
2520188417Sthompsa	data->m = m0;
2521193803Sweongyo	data->rate = rate;
2522184610Salfred
2523193803Sweongyo	/* fill Tx descriptor */
2524193803Sweongyo	desc = &data->desc;
2525193803Sweongyo	phy = zyd_plcp_signal(sc, rate);
2526193803Sweongyo	desc->phy = phy;
2527193803Sweongyo	if (ZYD_RATE_IS_OFDM(rate)) {
2528193803Sweongyo		desc->phy |= ZYD_TX_PHY_OFDM;
2529193803Sweongyo		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2530193803Sweongyo			desc->phy |= ZYD_TX_PHY_5GHZ;
2531193803Sweongyo	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2532193803Sweongyo		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2533193803Sweongyo
2534188417Sthompsa	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2535188417Sthompsa	desc->len = htole16(totlen);
2536184610Salfred
2537193803Sweongyo	desc->flags = ZYD_TX_FLAG_BACKOFF;
2538188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2539188417Sthompsa		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2540188417Sthompsa		if (totlen > vap->iv_rtsthreshold) {
2541188417Sthompsa			desc->flags |= ZYD_TX_FLAG_RTS;
2542188417Sthompsa		} else if (ZYD_RATE_IS_OFDM(rate) &&
2543188417Sthompsa		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2544188417Sthompsa			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2545188417Sthompsa				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2546188417Sthompsa			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2547188417Sthompsa				desc->flags |= ZYD_TX_FLAG_RTS;
2548188417Sthompsa		}
2549193803Sweongyo	} else
2550193803Sweongyo		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2551188417Sthompsa	if ((wh->i_fc[0] &
2552188417Sthompsa	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2553188417Sthompsa	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2554188417Sthompsa		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2555188417Sthompsa
2556184610Salfred	/* actual transmit length (XXX why +10?) */
2557193803Sweongyo	pktlen = ZYD_TX_DESC_SIZE + 10;
2558188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211)
2559184610Salfred		pktlen += totlen;
2560188417Sthompsa	desc->pktlen = htole16(pktlen);
2561184610Salfred
2562193803Sweongyo	bits = (rate == 11) ? (totlen * 16) + 10 :
2563193803Sweongyo	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2564196809Sweongyo	desc->plcp_length = htole16(bits / ratediv[phy]);
2565188417Sthompsa	desc->plcp_service = 0;
2566193803Sweongyo	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2567193803Sweongyo		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2568193803Sweongyo	desc->nextlen = 0;
2569193803Sweongyo
2570193803Sweongyo	if (ieee80211_radiotap_active_vap(vap)) {
2571193803Sweongyo		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2572193803Sweongyo
2573193803Sweongyo		tap->wt_flags = 0;
2574193803Sweongyo		tap->wt_rate = rate;
2575193803Sweongyo
2576193803Sweongyo		ieee80211_radiotap_tx(vap, m0);
2577184610Salfred	}
2578184610Salfred
2579188417Sthompsa	DPRINTF(sc, ZYD_DEBUG_XMIT,
2580188417Sthompsa	    "%s: sending data frame len=%zu rate=%u\n",
2581188417Sthompsa	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2582188417Sthompsa		rate);
2583184610Salfred
2584188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2585194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2586184610Salfred
2587188417Sthompsa	return (0);
2588184610Salfred}
2589184610Salfred
2590184610Salfredstatic void
2591188417Sthompsazyd_start(struct ifnet *ifp)
2592184610Salfred{
2593188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2594188417Sthompsa	struct ieee80211_node *ni;
2595184610Salfred	struct mbuf *m;
2596184610Salfred
2597188417Sthompsa	ZYD_LOCK(sc);
2598188417Sthompsa	for (;;) {
2599188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2600188417Sthompsa		if (m == NULL)
2601184610Salfred			break;
2602188417Sthompsa		if (sc->tx_nfree == 0) {
2603188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2604188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2605184610Salfred			break;
2606184610Salfred		}
2607188417Sthompsa		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2608193803Sweongyo		if (zyd_tx_start(sc, m, ni) != 0) {
2609188417Sthompsa			ieee80211_free_node(ni);
2610188417Sthompsa			ifp->if_oerrors++;
2611188417Sthompsa			break;
2612184610Salfred		}
2613184610Salfred	}
2614188417Sthompsa	ZYD_UNLOCK(sc);
2615184610Salfred}
2616184610Salfred
2617188417Sthompsastatic int
2618188417Sthompsazyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2619188417Sthompsa	const struct ieee80211_bpf_params *params)
2620184610Salfred{
2621188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
2622188417Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2623188417Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2624184610Salfred
2625188417Sthompsa	ZYD_LOCK(sc);
2626188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2627188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2628188417Sthompsa		ZYD_UNLOCK(sc);
2629188417Sthompsa		m_freem(m);
2630188417Sthompsa		ieee80211_free_node(ni);
2631188417Sthompsa		return (ENETDOWN);
2632188417Sthompsa	}
2633188417Sthompsa	if (sc->tx_nfree == 0) {
2634188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2635188417Sthompsa		ZYD_UNLOCK(sc);
2636188417Sthompsa		m_freem(m);
2637188417Sthompsa		ieee80211_free_node(ni);
2638188417Sthompsa		return (ENOBUFS);		/* XXX */
2639188417Sthompsa	}
2640188417Sthompsa
2641188417Sthompsa	/*
2642188417Sthompsa	 * Legacy path; interpret frame contents to decide
2643188417Sthompsa	 * precisely how to send the frame.
2644188417Sthompsa	 * XXX raw path
2645188417Sthompsa	 */
2646193803Sweongyo	if (zyd_tx_start(sc, m, ni) != 0) {
2647188417Sthompsa		ZYD_UNLOCK(sc);
2648188417Sthompsa		ifp->if_oerrors++;
2649188417Sthompsa		ieee80211_free_node(ni);
2650188417Sthompsa		return (EIO);
2651188417Sthompsa	}
2652188417Sthompsa	ZYD_UNLOCK(sc);
2653188417Sthompsa	return (0);
2654184610Salfred}
2655184610Salfred
2656184610Salfredstatic int
2657188417Sthompsazyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2658184610Salfred{
2659184610Salfred	struct zyd_softc *sc = ifp->if_softc;
2660184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
2661188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
2662246614Shselasky	int error;
2663246614Shselasky	int startall = 0;
2664184610Salfred
2665246614Shselasky	ZYD_LOCK(sc);
2666246614Shselasky	error = (sc->sc_flags & ZYD_FLAG_DETACHED) ? ENXIO : 0;
2667246614Shselasky	ZYD_UNLOCK(sc);
2668246614Shselasky	if (error)
2669246614Shselasky		return (error);
2670246614Shselasky
2671184610Salfred	switch (cmd) {
2672184610Salfred	case SIOCSIFFLAGS:
2673188417Sthompsa		ZYD_LOCK(sc);
2674184610Salfred		if (ifp->if_flags & IFF_UP) {
2675191746Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2676191746Sthompsa				zyd_init_locked(sc);
2677188417Sthompsa				startall = 1;
2678191746Sthompsa			} else
2679191746Sthompsa				zyd_set_multi(sc);
2680184610Salfred		} else {
2681191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2682191746Sthompsa				zyd_stop(sc);
2683184610Salfred		}
2684188417Sthompsa		ZYD_UNLOCK(sc);
2685188417Sthompsa		if (startall)
2686188417Sthompsa			ieee80211_start_all(ic);
2687184610Salfred		break;
2688184610Salfred	case SIOCGIFMEDIA:
2689188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2690184610Salfred		break;
2691188417Sthompsa	case SIOCGIFADDR:
2692184610Salfred		error = ether_ioctl(ifp, cmd, data);
2693184610Salfred		break;
2694188417Sthompsa	default:
2695188417Sthompsa		error = EINVAL;
2696188417Sthompsa		break;
2697184610Salfred	}
2698184610Salfred	return (error);
2699184610Salfred}
2700184610Salfred
2701184610Salfredstatic void
2702191746Sthompsazyd_init_locked(struct zyd_softc *sc)
2703184610Salfred{
2704188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2705188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2706192984Sthompsa	struct usb_config_descriptor *cd;
2707188419Sthompsa	int error;
2708188417Sthompsa	uint32_t val;
2709184610Salfred
2710188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2711184610Salfred
2712188417Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2713188417Sthompsa		error = zyd_loadfirmware(sc);
2714188417Sthompsa		if (error != 0) {
2715188417Sthompsa			device_printf(sc->sc_dev,
2716188417Sthompsa			    "could not load firmware (error=%d)\n", error);
2717188417Sthompsa			goto fail;
2718188417Sthompsa		}
2719188417Sthompsa
2720188417Sthompsa		/* reset device */
2721194228Sthompsa		cd = usbd_get_config_descriptor(sc->sc_udev);
2722194228Sthompsa		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2723188417Sthompsa		    cd->bConfigurationValue);
2724188417Sthompsa		if (error)
2725188417Sthompsa			device_printf(sc->sc_dev, "reset failed, continuing\n");
2726188417Sthompsa
2727188417Sthompsa		error = zyd_hw_init(sc);
2728188417Sthompsa		if (error) {
2729188417Sthompsa			device_printf(sc->sc_dev,
2730188417Sthompsa			    "hardware initialization failed\n");
2731188417Sthompsa			goto fail;
2732188417Sthompsa		}
2733188417Sthompsa
2734188417Sthompsa		device_printf(sc->sc_dev,
2735188417Sthompsa		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2736188417Sthompsa		    "BE%x NP%x Gain%x F%x\n",
2737188417Sthompsa		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2738188417Sthompsa		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2739188417Sthompsa		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2740188417Sthompsa		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2741188417Sthompsa		    sc->sc_cckgain, sc->sc_fix_cr157);
2742188417Sthompsa
2743188417Sthompsa		/* read regulatory domain (currently unused) */
2744188417Sthompsa		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2745188417Sthompsa		sc->sc_regdomain = val >> 16;
2746188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2747188417Sthompsa		    sc->sc_regdomain);
2748188417Sthompsa
2749188417Sthompsa		/* we'll do software WEP decryption for now */
2750188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2751188417Sthompsa		    __func__);
2752188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2753188417Sthompsa
2754188417Sthompsa		sc->sc_flags |= ZYD_FLAG_INITONCE;
2755184610Salfred	}
2756184610Salfred
2757188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2758191746Sthompsa		zyd_stop(sc);
2759188417Sthompsa
2760190526Ssam	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2761190526Ssam	    IF_LLADDR(ifp), ":");
2762190526Ssam	error = zyd_set_macaddr(sc, IF_LLADDR(ifp));
2763188417Sthompsa	if (error != 0)
2764184610Salfred		return;
2765184610Salfred
2766188417Sthompsa	/* set basic rates */
2767188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B)
2768188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2769188417Sthompsa	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2770188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2771188417Sthompsa	else	/* assumes 802.11b/g */
2772188417Sthompsa		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2773184610Salfred
2774188417Sthompsa	/* promiscuous mode */
2775188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2776188417Sthompsa	/* multicast setup */
2777188417Sthompsa	zyd_set_multi(sc);
2778188417Sthompsa	/* set RX filter  */
2779188417Sthompsa	error = zyd_set_rxfilter(sc);
2780188417Sthompsa	if (error != 0)
2781188417Sthompsa		goto fail;
2782184610Salfred
2783188417Sthompsa	/* switch radio transmitter ON */
2784188417Sthompsa	error = zyd_switch_radio(sc, 1);
2785188417Sthompsa	if (error != 0)
2786188417Sthompsa		goto fail;
2787188417Sthompsa	/* set default BSS channel */
2788188417Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2789184610Salfred
2790188417Sthompsa	/*
2791188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2792188417Sthompsa	 */
2793188419Sthompsa	zyd_setup_tx_list(sc);
2794184610Salfred
2795188417Sthompsa	/* enable interrupts */
2796188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2797184610Salfred
2798188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2799188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2800194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2801194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2802194228Sthompsa	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2803184610Salfred
2804188417Sthompsa	return;
2805184610Salfred
2806191746Sthompsafail:	zyd_stop(sc);
2807188417Sthompsa	return;
2808184610Salfred}
2809184610Salfred
2810184610Salfredstatic void
2811188417Sthompsazyd_init(void *priv)
2812184610Salfred{
2813188417Sthompsa	struct zyd_softc *sc = priv;
2814188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2815188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2816184610Salfred
2817188417Sthompsa	ZYD_LOCK(sc);
2818191746Sthompsa	zyd_init_locked(sc);
2819188417Sthompsa	ZYD_UNLOCK(sc);
2820188417Sthompsa
2821188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2822188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2823184610Salfred}
2824184610Salfred
2825184610Salfredstatic void
2826191746Sthompsazyd_stop(struct zyd_softc *sc)
2827184610Salfred{
2828188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2829188417Sthompsa	int error;
2830184610Salfred
2831188417Sthompsa	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2832184610Salfred
2833188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2834184610Salfred
2835188417Sthompsa	/*
2836188419Sthompsa	 * Drain all the transfers, if not already drained:
2837188417Sthompsa	 */
2838188419Sthompsa	ZYD_UNLOCK(sc);
2839194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2840194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2841188419Sthompsa	ZYD_LOCK(sc);
2842188417Sthompsa
2843188419Sthompsa	zyd_unsetup_tx_list(sc);
2844188417Sthompsa
2845188419Sthompsa	/* Stop now if the device was never set up */
2846188419Sthompsa	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2847184610Salfred		return;
2848184610Salfred
2849188417Sthompsa	/* switch radio transmitter OFF */
2850188417Sthompsa	error = zyd_switch_radio(sc, 0);
2851188417Sthompsa	if (error != 0)
2852188417Sthompsa		goto fail;
2853188417Sthompsa	/* disable Rx */
2854188417Sthompsa	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2855188417Sthompsa	/* disable interrupts */
2856188417Sthompsa	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2857184610Salfred
2858188417Sthompsafail:
2859188417Sthompsa	return;
2860184610Salfred}
2861184610Salfred
2862188417Sthompsastatic int
2863188417Sthompsazyd_loadfirmware(struct zyd_softc *sc)
2864184610Salfred{
2865192984Sthompsa	struct usb_device_request req;
2866188417Sthompsa	size_t size;
2867188417Sthompsa	u_char *fw;
2868188417Sthompsa	uint8_t stat;
2869188417Sthompsa	uint16_t addr;
2870184610Salfred
2871188417Sthompsa	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2872188417Sthompsa		return (0);
2873184610Salfred
2874188417Sthompsa	if (sc->sc_macrev == ZYD_ZD1211) {
2875188417Sthompsa		fw = (u_char *)zd1211_firmware;
2876188417Sthompsa		size = sizeof(zd1211_firmware);
2877188417Sthompsa	} else {
2878188417Sthompsa		fw = (u_char *)zd1211b_firmware;
2879188417Sthompsa		size = sizeof(zd1211b_firmware);
2880184610Salfred	}
2881184610Salfred
2882188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2883188417Sthompsa	req.bRequest = ZYD_DOWNLOADREQ;
2884188417Sthompsa	USETW(req.wIndex, 0);
2885184610Salfred
2886188417Sthompsa	addr = ZYD_FIRMWARE_START_ADDR;
2887188417Sthompsa	while (size > 0) {
2888188417Sthompsa		/*
2889188417Sthompsa		 * When the transfer size is 4096 bytes, it is not
2890188417Sthompsa		 * likely to be able to transfer it.
2891188417Sthompsa		 * The cause is port or machine or chip?
2892188417Sthompsa		 */
2893188417Sthompsa		const int mlen = min(size, 64);
2894184610Salfred
2895188417Sthompsa		DPRINTF(sc, ZYD_DEBUG_FW,
2896188417Sthompsa		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2897184610Salfred
2898188417Sthompsa		USETW(req.wValue, addr);
2899188417Sthompsa		USETW(req.wLength, mlen);
2900188419Sthompsa		if (zyd_do_request(sc, &req, fw) != 0)
2901188417Sthompsa			return (EIO);
2902184610Salfred
2903188417Sthompsa		addr += mlen / 2;
2904188417Sthompsa		fw   += mlen;
2905188417Sthompsa		size -= mlen;
2906184610Salfred	}
2907184610Salfred
2908188417Sthompsa	/* check whether the upload succeeded */
2909188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2910188417Sthompsa	req.bRequest = ZYD_DOWNLOADSTS;
2911188417Sthompsa	USETW(req.wValue, 0);
2912188417Sthompsa	USETW(req.wIndex, 0);
2913188417Sthompsa	USETW(req.wLength, sizeof(stat));
2914188419Sthompsa	if (zyd_do_request(sc, &req, &stat) != 0)
2915188417Sthompsa		return (EIO);
2916184610Salfred
2917188417Sthompsa	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2918184610Salfred
2919188417Sthompsa	return (stat & 0x80) ? (EIO) : (0);
2920184610Salfred}
2921184610Salfred
2922184610Salfredstatic void
2923188417Sthompsazyd_scan_start(struct ieee80211com *ic)
2924184610Salfred{
2925191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2926191746Sthompsa	struct zyd_softc *sc = ifp->if_softc;
2927184610Salfred
2928188417Sthompsa	ZYD_LOCK(sc);
2929191746Sthompsa	/* want broadcast address while scanning */
2930191746Sthompsa	zyd_set_bssid(sc, ifp->if_broadcastaddr);
2931188417Sthompsa	ZYD_UNLOCK(sc);
2932184610Salfred}
2933184610Salfred
2934184610Salfredstatic void
2935188417Sthompsazyd_scan_end(struct ieee80211com *ic)
2936184610Salfred{
2937188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2938184610Salfred
2939188417Sthompsa	ZYD_LOCK(sc);
2940191746Sthompsa	/* restore previous bssid */
2941191746Sthompsa	zyd_set_bssid(sc, sc->sc_bssid);
2942188417Sthompsa	ZYD_UNLOCK(sc);
2943184610Salfred}
2944184610Salfred
2945184610Salfredstatic void
2946188417Sthompsazyd_set_channel(struct ieee80211com *ic)
2947184610Salfred{
2948188417Sthompsa	struct zyd_softc *sc = ic->ic_ifp->if_softc;
2949184610Salfred
2950188417Sthompsa	ZYD_LOCK(sc);
2951191746Sthompsa	zyd_set_chan(sc, ic->ic_curchan);
2952188417Sthompsa	ZYD_UNLOCK(sc);
2953184610Salfred}
2954184610Salfred
2955188417Sthompsastatic device_method_t zyd_methods[] = {
2956188417Sthompsa        /* Device interface */
2957188417Sthompsa        DEVMETHOD(device_probe, zyd_match),
2958188417Sthompsa        DEVMETHOD(device_attach, zyd_attach),
2959188417Sthompsa        DEVMETHOD(device_detach, zyd_detach),
2960246614Shselasky	DEVMETHOD_END
2961188417Sthompsa};
2962184610Salfred
2963188417Sthompsastatic driver_t zyd_driver = {
2964233774Shselasky	.name = "zyd",
2965233774Shselasky	.methods = zyd_methods,
2966233774Shselasky	.size = sizeof(struct zyd_softc)
2967188417Sthompsa};
2968184610Salfred
2969188417Sthompsastatic devclass_t zyd_devclass;
2970184610Salfred
2971189275SthompsaDRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2972188942SthompsaMODULE_DEPEND(zyd, usb, 1, 1, 1);
2973188417SthompsaMODULE_DEPEND(zyd, wlan, 1, 1, 1);
2974212122SthompsaMODULE_VERSION(zyd, 1);
2975