xref: /freebsd/sys/dev/usb/wlan/if_zyd.c (revision 9768746b)
1 /*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
2 /*	$NetBSD: if_zyd.c,v 1.7 2007/06/21 04:04:29 kiyohara Exp $	*/
3 /*	$FreeBSD$	*/
4 
5 /*-
6  * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
7  * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24 
25 /*
26  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
27  */
28 
29 #include "opt_wlan.h"
30 
31 #include <sys/param.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/condvar.h>
37 #include <sys/mbuf.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 #include <sys/endian.h>
45 #include <sys/kdb.h>
46 
47 #include <net/bpf.h>
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
62 #endif
63 
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_regdomain.h>
66 #include <net80211/ieee80211_radiotap.h>
67 #include <net80211/ieee80211_ratectl.h>
68 
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72 #include "usbdevs.h"
73 
74 #include <dev/usb/wlan/if_zydreg.h>
75 #include <dev/usb/wlan/if_zydfw.h>
76 
77 #ifdef USB_DEBUG
78 static int zyd_debug = 0;
79 
80 static SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
81     "USB zyd");
82 SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RWTUN, &zyd_debug, 0,
83     "zyd debug level");
84 
85 enum {
86 	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
87 	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
88 	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
89 	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
90 	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
91 	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
92 	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
93 	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
94 	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
95 	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
96 	ZYD_DEBUG_ANY		= 0xffffffff
97 };
98 #define	DPRINTF(sc, m, fmt, ...) do {				\
99 	if (zyd_debug & (m))					\
100 		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
101 } while (0)
102 #else
103 #define	DPRINTF(sc, m, fmt, ...) do {				\
104 	(void) sc;						\
105 } while (0)
106 #endif
107 
108 #define	zyd_do_request(sc,req,data) \
109     usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
110 
111 static device_probe_t zyd_match;
112 static device_attach_t zyd_attach;
113 static device_detach_t zyd_detach;
114 
115 static usb_callback_t zyd_intr_read_callback;
116 static usb_callback_t zyd_intr_write_callback;
117 static usb_callback_t zyd_bulk_read_callback;
118 static usb_callback_t zyd_bulk_write_callback;
119 
120 static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
121 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
122 		    const uint8_t [IEEE80211_ADDR_LEN],
123 		    const uint8_t [IEEE80211_ADDR_LEN]);
124 static void	zyd_vap_delete(struct ieee80211vap *);
125 static void	zyd_tx_free(struct zyd_tx_data *, int);
126 static void	zyd_setup_tx_list(struct zyd_softc *);
127 static void	zyd_unsetup_tx_list(struct zyd_softc *);
128 static int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
129 static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
130 		    void *, int, int);
131 static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
132 static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
133 static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
134 static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
135 static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
136 static int	zyd_lock_phy(struct zyd_softc *);
137 static int	zyd_unlock_phy(struct zyd_softc *);
138 static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
139 static const char *zyd_rf_name(uint8_t);
140 static int	zyd_hw_init(struct zyd_softc *);
141 static int	zyd_read_pod(struct zyd_softc *);
142 static int	zyd_read_eeprom(struct zyd_softc *);
143 static int	zyd_get_macaddr(struct zyd_softc *);
144 static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
145 static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
146 static int	zyd_switch_radio(struct zyd_softc *, int);
147 static int	zyd_set_led(struct zyd_softc *, int, int);
148 static void	zyd_set_multi(struct zyd_softc *);
149 static void	zyd_update_mcast(struct ieee80211com *);
150 static int	zyd_set_rxfilter(struct zyd_softc *);
151 static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
152 static int	zyd_set_beacon_interval(struct zyd_softc *, int);
153 static void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
154 static int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
155 		    struct ieee80211_node *);
156 static int	zyd_transmit(struct ieee80211com *, struct mbuf *);
157 static void	zyd_start(struct zyd_softc *);
158 static int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
159 		    const struct ieee80211_bpf_params *);
160 static void	zyd_parent(struct ieee80211com *);
161 static void	zyd_init_locked(struct zyd_softc *);
162 static void	zyd_stop(struct zyd_softc *);
163 static int	zyd_loadfirmware(struct zyd_softc *);
164 static void	zyd_scan_start(struct ieee80211com *);
165 static void	zyd_scan_end(struct ieee80211com *);
166 static void	zyd_getradiocaps(struct ieee80211com *, int, int *,
167 		    struct ieee80211_channel[]);
168 static void	zyd_set_channel(struct ieee80211com *);
169 static int	zyd_rfmd_init(struct zyd_rf *);
170 static int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
171 static int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
172 static int	zyd_al2230_init(struct zyd_rf *);
173 static int	zyd_al2230_switch_radio(struct zyd_rf *, int);
174 static int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
175 static int	zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
176 static int	zyd_al2230_init_b(struct zyd_rf *);
177 static int	zyd_al7230B_init(struct zyd_rf *);
178 static int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
179 static int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
180 static int	zyd_al2210_init(struct zyd_rf *);
181 static int	zyd_al2210_switch_radio(struct zyd_rf *, int);
182 static int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
183 static int	zyd_gct_init(struct zyd_rf *);
184 static int	zyd_gct_switch_radio(struct zyd_rf *, int);
185 static int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
186 static int	zyd_gct_mode(struct zyd_rf *);
187 static int	zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
188 static int	zyd_gct_write(struct zyd_rf *, uint16_t);
189 static int	zyd_gct_txgain(struct zyd_rf *, uint8_t);
190 static int	zyd_maxim2_init(struct zyd_rf *);
191 static int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
192 static int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
193 
194 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
195 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
196 
197 /* various supported device vendors/products */
198 #define ZYD_ZD1211	0
199 #define ZYD_ZD1211B	1
200 
201 #define	ZYD_ZD1211_DEV(v,p)	\
202 	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
203 #define	ZYD_ZD1211B_DEV(v,p)	\
204 	{ USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
205 static const STRUCT_USB_HOST_ID zyd_devs[] = {
206 	/* ZYD_ZD1211 */
207 	ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
208 	ZYD_ZD1211_DEV(ABOCOM, WL54),
209 	ZYD_ZD1211_DEV(ASUS, WL159G),
210 	ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
211 	ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
212 	ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
213 	ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
214 	ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
215 	ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
216 	ZYD_ZD1211_DEV(SAGEM, XG760A),
217 	ZYD_ZD1211_DEV(SENAO, NUB8301),
218 	ZYD_ZD1211_DEV(SITECOMEU, WL113),
219 	ZYD_ZD1211_DEV(SWEEX, ZD1211),
220 	ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
221 	ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
222 	ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
223 	ZYD_ZD1211_DEV(TWINMOS, G240),
224 	ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
225 	ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
226 	ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
227 	ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
228 	ZYD_ZD1211_DEV(ZCOM, ZD1211),
229 	ZYD_ZD1211_DEV(ZYDAS, ZD1211),
230 	ZYD_ZD1211_DEV(ZYXEL, AG225H),
231 	ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
232 	ZYD_ZD1211_DEV(ZYXEL, G200V2),
233 	/* ZYD_ZD1211B */
234 	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
235 	ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
236 	ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
237 	ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
238 	ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
239 	ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
240 	ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
241 	ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
242 	ZYD_ZD1211B_DEV(MELCO, KG54L),
243 	ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
244 	ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
245 	ZYD_ZD1211B_DEV(SAGEM, XG76NA),
246 	ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
247 	ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
248 	ZYD_ZD1211B_DEV(USR, USR5423),
249 	ZYD_ZD1211B_DEV(VTECH, ZD1211B),
250 	ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
251 	ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
252 	ZYD_ZD1211B_DEV(ZYXEL, M202),
253 	ZYD_ZD1211B_DEV(ZYXEL, G202),
254 	ZYD_ZD1211B_DEV(ZYXEL, G220V2)
255 };
256 
257 static const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
258 	[ZYD_BULK_WR] = {
259 		.type = UE_BULK,
260 		.endpoint = UE_ADDR_ANY,
261 		.direction = UE_DIR_OUT,
262 		.bufsize = ZYD_MAX_TXBUFSZ,
263 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
264 		.callback = zyd_bulk_write_callback,
265 		.ep_index = 0,
266 		.timeout = 10000,	/* 10 seconds */
267 	},
268 	[ZYD_BULK_RD] = {
269 		.type = UE_BULK,
270 		.endpoint = UE_ADDR_ANY,
271 		.direction = UE_DIR_IN,
272 		.bufsize = ZYX_MAX_RXBUFSZ,
273 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
274 		.callback = zyd_bulk_read_callback,
275 		.ep_index = 0,
276 	},
277 	[ZYD_INTR_WR] = {
278 		.type = UE_BULK_INTR,
279 		.endpoint = UE_ADDR_ANY,
280 		.direction = UE_DIR_OUT,
281 		.bufsize = sizeof(struct zyd_cmd),
282 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
283 		.callback = zyd_intr_write_callback,
284 		.timeout = 1000,	/* 1 second */
285 		.ep_index = 1,
286 	},
287 	[ZYD_INTR_RD] = {
288 		.type = UE_INTERRUPT,
289 		.endpoint = UE_ADDR_ANY,
290 		.direction = UE_DIR_IN,
291 		.bufsize = sizeof(struct zyd_cmd),
292 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
293 		.callback = zyd_intr_read_callback,
294 	},
295 };
296 #define zyd_read16_m(sc, val, data)	do {				\
297 	error = zyd_read16(sc, val, data);				\
298 	if (error != 0)							\
299 		goto fail;						\
300 } while (0)
301 #define zyd_write16_m(sc, val, data)	do {				\
302 	error = zyd_write16(sc, val, data);				\
303 	if (error != 0)							\
304 		goto fail;						\
305 } while (0)
306 #define zyd_read32_m(sc, val, data)	do {				\
307 	error = zyd_read32(sc, val, data);				\
308 	if (error != 0)							\
309 		goto fail;						\
310 } while (0)
311 #define zyd_write32_m(sc, val, data)	do {				\
312 	error = zyd_write32(sc, val, data);				\
313 	if (error != 0)							\
314 		goto fail;						\
315 } while (0)
316 
317 static int
318 zyd_match(device_t dev)
319 {
320 	struct usb_attach_arg *uaa = device_get_ivars(dev);
321 
322 	if (uaa->usb_mode != USB_MODE_HOST)
323 		return (ENXIO);
324 	if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
325 		return (ENXIO);
326 	if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
327 		return (ENXIO);
328 
329 	return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
330 }
331 
332 static int
333 zyd_attach(device_t dev)
334 {
335 	struct usb_attach_arg *uaa = device_get_ivars(dev);
336 	struct zyd_softc *sc = device_get_softc(dev);
337 	struct ieee80211com *ic = &sc->sc_ic;
338 	uint8_t iface_index;
339 	int error;
340 
341 	if (uaa->info.bcdDevice < 0x4330) {
342 		device_printf(dev, "device version mismatch: 0x%X "
343 		    "(only >= 43.30 supported)\n",
344 		    uaa->info.bcdDevice);
345 		return (EINVAL);
346 	}
347 
348 	device_set_usb_desc(dev);
349 	sc->sc_dev = dev;
350 	sc->sc_udev = uaa->device;
351 	sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
352 
353 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
354 	    MTX_NETWORK_LOCK, MTX_DEF);
355 	STAILQ_INIT(&sc->sc_rqh);
356 	mbufq_init(&sc->sc_snd, ifqmaxlen);
357 
358 	iface_index = ZYD_IFACE_INDEX;
359 	error = usbd_transfer_setup(uaa->device,
360 	    &iface_index, sc->sc_xfer, zyd_config,
361 	    ZYD_N_TRANSFER, sc, &sc->sc_mtx);
362 	if (error) {
363 		device_printf(dev, "could not allocate USB transfers, "
364 		    "err=%s\n", usbd_errstr(error));
365 		goto detach;
366 	}
367 
368 	ZYD_LOCK(sc);
369 	if ((error = zyd_get_macaddr(sc)) != 0) {
370 		device_printf(sc->sc_dev, "could not read EEPROM\n");
371 		ZYD_UNLOCK(sc);
372 		goto detach;
373 	}
374 	ZYD_UNLOCK(sc);
375 
376 	ic->ic_softc = sc;
377 	ic->ic_name = device_get_nameunit(dev);
378 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
379 	ic->ic_opmode = IEEE80211_M_STA;
380 
381 	/* set device capabilities */
382 	ic->ic_caps =
383 		  IEEE80211_C_STA		/* station mode */
384 		| IEEE80211_C_MONITOR		/* monitor mode */
385 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
386 	        | IEEE80211_C_SHSLOT		/* short slot time supported */
387 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
388 	        | IEEE80211_C_WPA		/* 802.11i */
389 		;
390 
391 	zyd_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
392 	    ic->ic_channels);
393 
394 	ieee80211_ifattach(ic);
395 	ic->ic_raw_xmit = zyd_raw_xmit;
396 	ic->ic_scan_start = zyd_scan_start;
397 	ic->ic_scan_end = zyd_scan_end;
398 	ic->ic_getradiocaps = zyd_getradiocaps;
399 	ic->ic_set_channel = zyd_set_channel;
400 	ic->ic_vap_create = zyd_vap_create;
401 	ic->ic_vap_delete = zyd_vap_delete;
402 	ic->ic_update_mcast = zyd_update_mcast;
403 	ic->ic_update_promisc = zyd_update_mcast;
404 	ic->ic_parent = zyd_parent;
405 	ic->ic_transmit = zyd_transmit;
406 
407 	ieee80211_radiotap_attach(ic,
408 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
409 		ZYD_TX_RADIOTAP_PRESENT,
410 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
411 		ZYD_RX_RADIOTAP_PRESENT);
412 
413 	if (bootverbose)
414 		ieee80211_announce(ic);
415 
416 	return (0);
417 
418 detach:
419 	zyd_detach(dev);
420 	return (ENXIO);			/* failure */
421 }
422 
423 static void
424 zyd_drain_mbufq(struct zyd_softc *sc)
425 {
426 	struct mbuf *m;
427 	struct ieee80211_node *ni;
428 
429 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
430 	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
431 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
432 		m->m_pkthdr.rcvif = NULL;
433 		ieee80211_free_node(ni);
434 		m_freem(m);
435 	}
436 }
437 
438 static int
439 zyd_detach(device_t dev)
440 {
441 	struct zyd_softc *sc = device_get_softc(dev);
442 	struct ieee80211com *ic = &sc->sc_ic;
443 	unsigned x;
444 
445 	/*
446 	 * Prevent further allocations from RX/TX data
447 	 * lists and ioctls:
448 	 */
449 	ZYD_LOCK(sc);
450 	sc->sc_flags |= ZYD_FLAG_DETACHED;
451 	zyd_drain_mbufq(sc);
452 	STAILQ_INIT(&sc->tx_q);
453 	STAILQ_INIT(&sc->tx_free);
454 	ZYD_UNLOCK(sc);
455 
456 	/* drain USB transfers */
457 	for (x = 0; x != ZYD_N_TRANSFER; x++)
458 		usbd_transfer_drain(sc->sc_xfer[x]);
459 
460 	/* free TX list, if any */
461 	ZYD_LOCK(sc);
462 	zyd_unsetup_tx_list(sc);
463 	ZYD_UNLOCK(sc);
464 
465 	/* free USB transfers and some data buffers */
466 	usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
467 
468 	if (ic->ic_softc == sc)
469 		ieee80211_ifdetach(ic);
470 	mtx_destroy(&sc->sc_mtx);
471 
472 	return (0);
473 }
474 
475 static struct ieee80211vap *
476 zyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
477     enum ieee80211_opmode opmode, int flags,
478     const uint8_t bssid[IEEE80211_ADDR_LEN],
479     const uint8_t mac[IEEE80211_ADDR_LEN])
480 {
481 	struct zyd_vap *zvp;
482 	struct ieee80211vap *vap;
483 
484 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
485 		return (NULL);
486 	zvp = malloc(sizeof(struct zyd_vap), M_80211_VAP, M_WAITOK | M_ZERO);
487 	vap = &zvp->vap;
488 
489 	/* enable s/w bmiss handling for sta mode */
490 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
491 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
492 		/* out of memory */
493 		free(zvp, M_80211_VAP);
494 		return (NULL);
495 	}
496 
497 	/* override state transition machine */
498 	zvp->newstate = vap->iv_newstate;
499 	vap->iv_newstate = zyd_newstate;
500 
501 	ieee80211_ratectl_init(vap);
502 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
503 
504 	/* complete setup */
505 	ieee80211_vap_attach(vap, ieee80211_media_change,
506 	    ieee80211_media_status, mac);
507 	ic->ic_opmode = opmode;
508 	return (vap);
509 }
510 
511 static void
512 zyd_vap_delete(struct ieee80211vap *vap)
513 {
514 	struct zyd_vap *zvp = ZYD_VAP(vap);
515 
516 	ieee80211_ratectl_deinit(vap);
517 	ieee80211_vap_detach(vap);
518 	free(zvp, M_80211_VAP);
519 }
520 
521 static void
522 zyd_tx_free(struct zyd_tx_data *data, int txerr)
523 {
524 	struct zyd_softc *sc = data->sc;
525 
526 	if (data->m != NULL) {
527 		ieee80211_tx_complete(data->ni, data->m, txerr);
528 		data->m = NULL;
529 		data->ni = NULL;
530 	}
531 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
532 	sc->tx_nfree++;
533 }
534 
535 static void
536 zyd_setup_tx_list(struct zyd_softc *sc)
537 {
538 	struct zyd_tx_data *data;
539 	int i;
540 
541 	sc->tx_nfree = 0;
542 	STAILQ_INIT(&sc->tx_q);
543 	STAILQ_INIT(&sc->tx_free);
544 
545 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
546 		data = &sc->tx_data[i];
547 
548 		data->sc = sc;
549 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
550 		sc->tx_nfree++;
551 	}
552 }
553 
554 static void
555 zyd_unsetup_tx_list(struct zyd_softc *sc)
556 {
557 	struct zyd_tx_data *data;
558 	int i;
559 
560 	/* make sure any subsequent use of the queues will fail */
561 	sc->tx_nfree = 0;
562 	STAILQ_INIT(&sc->tx_q);
563 	STAILQ_INIT(&sc->tx_free);
564 
565 	/* free up all node references and mbufs */
566 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
567 		data = &sc->tx_data[i];
568 
569 		if (data->m != NULL) {
570 			m_freem(data->m);
571 			data->m = NULL;
572 		}
573 		if (data->ni != NULL) {
574 			ieee80211_free_node(data->ni);
575 			data->ni = NULL;
576 		}
577 	}
578 }
579 
580 static int
581 zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
582 {
583 	struct zyd_vap *zvp = ZYD_VAP(vap);
584 	struct ieee80211com *ic = vap->iv_ic;
585 	struct zyd_softc *sc = ic->ic_softc;
586 	int error;
587 
588 	DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
589 	    ieee80211_state_name[vap->iv_state],
590 	    ieee80211_state_name[nstate]);
591 
592 	IEEE80211_UNLOCK(ic);
593 	ZYD_LOCK(sc);
594 	switch (nstate) {
595 	case IEEE80211_S_AUTH:
596 		zyd_set_chan(sc, ic->ic_curchan);
597 		break;
598 	case IEEE80211_S_RUN:
599 		if (vap->iv_opmode == IEEE80211_M_MONITOR)
600 			break;
601 
602 		/* turn link LED on */
603 		error = zyd_set_led(sc, ZYD_LED1, 1);
604 		if (error != 0)
605 			break;
606 
607 		/* make data LED blink upon Tx */
608 		zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
609 
610 		IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
611 		zyd_set_bssid(sc, sc->sc_bssid);
612 		break;
613 	default:
614 		break;
615 	}
616 fail:
617 	ZYD_UNLOCK(sc);
618 	IEEE80211_LOCK(ic);
619 	return (zvp->newstate(vap, nstate, arg));
620 }
621 
622 /*
623  * Callback handler for interrupt transfer
624  */
625 static void
626 zyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
627 {
628 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
629 	struct ieee80211com *ic = &sc->sc_ic;
630 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
631 	struct ieee80211_node *ni;
632 	struct zyd_cmd *cmd = &sc->sc_ibuf;
633 	struct usb_page_cache *pc;
634 	int datalen;
635 	int actlen;
636 
637 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
638 
639 	switch (USB_GET_STATE(xfer)) {
640 	case USB_ST_TRANSFERRED:
641 		pc = usbd_xfer_get_frame(xfer, 0);
642 		usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
643 
644 		switch (le16toh(cmd->code)) {
645 		case ZYD_NOTIF_RETRYSTATUS:
646 		{
647 			struct zyd_notif_retry *retry =
648 			    (struct zyd_notif_retry *)cmd->data;
649 			uint16_t count = le16toh(retry->count);
650 
651 			DPRINTF(sc, ZYD_DEBUG_TX_PROC,
652 			    "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
653 			    le16toh(retry->rate), ether_sprintf(retry->macaddr),
654 			    count & 0xff, count);
655 
656 			/*
657 			 * Find the node to which the packet was sent and
658 			 * update its retry statistics.  In BSS mode, this node
659 			 * is the AP we're associated to so no lookup is
660 			 * actually needed.
661 			 */
662 			ni = ieee80211_find_txnode(vap, retry->macaddr);
663 			if (ni != NULL) {
664 				struct ieee80211_ratectl_tx_status *txs =
665 				    &sc->sc_txs;
666 				int retrycnt = count & 0xff;
667 
668 				txs->flags =
669 				    IEEE80211_RATECTL_STATUS_LONG_RETRY;
670 				txs->long_retries = retrycnt;
671 				if (count & 0x100) {
672 					txs->status =
673 					    IEEE80211_RATECTL_TX_FAIL_LONG;
674 				} else {
675 					txs->status =
676 					    IEEE80211_RATECTL_TX_SUCCESS;
677 				}
678 
679 				ieee80211_ratectl_tx_complete(ni, txs);
680 				ieee80211_free_node(ni);
681 			}
682 			if (count & 0x100)
683 				/* too many retries */
684 				if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS,
685 				    1);
686 			break;
687 		}
688 		case ZYD_NOTIF_IORD:
689 		{
690 			struct zyd_rq *rqp;
691 
692 			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
693 				break;	/* HMAC interrupt */
694 
695 			datalen = actlen - sizeof(cmd->code);
696 			datalen -= 2;	/* XXX: padding? */
697 
698 			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
699 				int i;
700 				int count;
701 
702 				if (rqp->olen != datalen)
703 					continue;
704 				count = rqp->olen / sizeof(struct zyd_pair);
705 				for (i = 0; i < count; i++) {
706 					if (*(((const uint16_t *)rqp->idata) + i) !=
707 					    (((struct zyd_pair *)cmd->data) + i)->reg)
708 						break;
709 				}
710 				if (i != count)
711 					continue;
712 				/* copy answer into caller-supplied buffer */
713 				memcpy(rqp->odata, cmd->data, rqp->olen);
714 				DPRINTF(sc, ZYD_DEBUG_CMD,
715 				    "command %p complete, data = %*D \n",
716 				    rqp, rqp->olen, (char *)rqp->odata, ":");
717 				wakeup(rqp);	/* wakeup caller */
718 				break;
719 			}
720 			if (rqp == NULL) {
721 				device_printf(sc->sc_dev,
722 				    "unexpected IORD notification %*D\n",
723 				    datalen, cmd->data, ":");
724 			}
725 			break;
726 		}
727 		default:
728 			device_printf(sc->sc_dev, "unknown notification %x\n",
729 			    le16toh(cmd->code));
730 		}
731 
732 		/* FALLTHROUGH */
733 	case USB_ST_SETUP:
734 tr_setup:
735 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
736 		usbd_transfer_submit(xfer);
737 		break;
738 
739 	default:			/* Error */
740 		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
741 		    usbd_errstr(error));
742 
743 		if (error != USB_ERR_CANCELLED) {
744 			/* try to clear stall first */
745 			usbd_xfer_set_stall(xfer);
746 			goto tr_setup;
747 		}
748 		break;
749 	}
750 }
751 
752 static void
753 zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
754 {
755 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
756 	struct zyd_rq *rqp, *cmd;
757 	struct usb_page_cache *pc;
758 
759 	switch (USB_GET_STATE(xfer)) {
760 	case USB_ST_TRANSFERRED:
761 		cmd = usbd_xfer_get_priv(xfer);
762 		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
763 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
764 			/* Ensure the cached rq pointer is still valid */
765 			if (rqp == cmd &&
766 			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
767 				wakeup(rqp);	/* wakeup caller */
768 		}
769 
770 		/* FALLTHROUGH */
771 	case USB_ST_SETUP:
772 tr_setup:
773 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
774 			if (rqp->flags & ZYD_CMD_FLAG_SENT)
775 				continue;
776 
777 			pc = usbd_xfer_get_frame(xfer, 0);
778 			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
779 
780 			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
781 			usbd_xfer_set_priv(xfer, rqp);
782 			rqp->flags |= ZYD_CMD_FLAG_SENT;
783 			usbd_transfer_submit(xfer);
784 			break;
785 		}
786 		break;
787 
788 	default:			/* Error */
789 		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
790 		    usbd_errstr(error));
791 
792 		if (error != USB_ERR_CANCELLED) {
793 			/* try to clear stall first */
794 			usbd_xfer_set_stall(xfer);
795 			goto tr_setup;
796 		}
797 		break;
798 	}
799 }
800 
801 static int
802 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
803     void *odata, int olen, int flags)
804 {
805 	struct zyd_cmd cmd;
806 	struct zyd_rq rq;
807 	int error;
808 
809 	if (ilen > (int)sizeof(cmd.data))
810 		return (EINVAL);
811 
812 	cmd.code = htole16(code);
813 	memcpy(cmd.data, idata, ilen);
814 	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
815 	    &rq, ilen, idata, ":");
816 
817 	rq.cmd = &cmd;
818 	rq.idata = idata;
819 	rq.odata = odata;
820 	rq.ilen = sizeof(uint16_t) + ilen;
821 	rq.olen = olen;
822 	rq.flags = flags;
823 	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
824 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
825 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
826 
827 	/* wait at most one second for command reply */
828 	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
829 	if (error)
830 		device_printf(sc->sc_dev, "command timeout\n");
831 	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
832 	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
833 	    &rq, error);
834 
835 	return (error);
836 }
837 
838 static int
839 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
840 {
841 	struct zyd_pair tmp;
842 	int error;
843 
844 	reg = htole16(reg);
845 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
846 	    ZYD_CMD_FLAG_READ);
847 	if (error == 0)
848 		*val = le16toh(tmp.val);
849 	return (error);
850 }
851 
852 static int
853 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
854 {
855 	struct zyd_pair tmp[2];
856 	uint16_t regs[2];
857 	int error;
858 
859 	regs[0] = htole16(ZYD_REG32_HI(reg));
860 	regs[1] = htole16(ZYD_REG32_LO(reg));
861 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
862 	    ZYD_CMD_FLAG_READ);
863 	if (error == 0)
864 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
865 	return (error);
866 }
867 
868 static int
869 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
870 {
871 	struct zyd_pair pair;
872 
873 	pair.reg = htole16(reg);
874 	pair.val = htole16(val);
875 
876 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
877 }
878 
879 static int
880 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
881 {
882 	struct zyd_pair pair[2];
883 
884 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
885 	pair[0].val = htole16(val >> 16);
886 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
887 	pair[1].val = htole16(val & 0xffff);
888 
889 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
890 }
891 
892 static int
893 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
894 {
895 	struct zyd_rf *rf = &sc->sc_rf;
896 	struct zyd_rfwrite_cmd req;
897 	uint16_t cr203;
898 	int error, i;
899 
900 	zyd_read16_m(sc, ZYD_CR203, &cr203);
901 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
902 
903 	req.code  = htole16(2);
904 	req.width = htole16(rf->width);
905 	for (i = 0; i < rf->width; i++) {
906 		req.bit[i] = htole16(cr203);
907 		if (val & (1 << (rf->width - 1 - i)))
908 			req.bit[i] |= htole16(ZYD_RF_DATA);
909 	}
910 	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
911 fail:
912 	return (error);
913 }
914 
915 static int
916 zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
917 {
918 	int error;
919 
920 	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
921 	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
922 	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
923 fail:
924 	return (error);
925 }
926 
927 static int
928 zyd_lock_phy(struct zyd_softc *sc)
929 {
930 	int error;
931 	uint32_t tmp;
932 
933 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
934 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
935 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
936 fail:
937 	return (error);
938 }
939 
940 static int
941 zyd_unlock_phy(struct zyd_softc *sc)
942 {
943 	int error;
944 	uint32_t tmp;
945 
946 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
947 	tmp |= ZYD_UNLOCK_PHY_REGS;
948 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
949 fail:
950 	return (error);
951 }
952 
953 /*
954  * RFMD RF methods.
955  */
956 static int
957 zyd_rfmd_init(struct zyd_rf *rf)
958 {
959 	struct zyd_softc *sc = rf->rf_sc;
960 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
961 	static const uint32_t rfini[] = ZYD_RFMD_RF;
962 	int i, error;
963 
964 	/* init RF-dependent PHY registers */
965 	for (i = 0; i < nitems(phyini); i++) {
966 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
967 	}
968 
969 	/* init RFMD radio */
970 	for (i = 0; i < nitems(rfini); i++) {
971 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
972 			return (error);
973 	}
974 fail:
975 	return (error);
976 }
977 
978 static int
979 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
980 {
981 	int error;
982 	struct zyd_softc *sc = rf->rf_sc;
983 
984 	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
985 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
986 fail:
987 	return (error);
988 }
989 
990 static int
991 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
992 {
993 	int error;
994 	struct zyd_softc *sc = rf->rf_sc;
995 	static const struct {
996 		uint32_t	r1, r2;
997 	} rfprog[] = ZYD_RFMD_CHANTABLE;
998 
999 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1000 	if (error != 0)
1001 		goto fail;
1002 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1003 	if (error != 0)
1004 		goto fail;
1005 
1006 fail:
1007 	return (error);
1008 }
1009 
1010 /*
1011  * AL2230 RF methods.
1012  */
1013 static int
1014 zyd_al2230_init(struct zyd_rf *rf)
1015 {
1016 	struct zyd_softc *sc = rf->rf_sc;
1017 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1018 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1019 	static const struct zyd_phy_pair phypll[] = {
1020 		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1021 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1022 	};
1023 	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1024 	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1025 	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1026 	int i, error;
1027 
1028 	/* init RF-dependent PHY registers */
1029 	for (i = 0; i < nitems(phyini); i++)
1030 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1031 
1032 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1033 		for (i = 0; i < nitems(phy2230s); i++)
1034 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1035 	}
1036 
1037 	/* init AL2230 radio */
1038 	for (i = 0; i < nitems(rfini1); i++) {
1039 		error = zyd_rfwrite(sc, rfini1[i]);
1040 		if (error != 0)
1041 			goto fail;
1042 	}
1043 
1044 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1045 		error = zyd_rfwrite(sc, 0x000824);
1046 	else
1047 		error = zyd_rfwrite(sc, 0x0005a4);
1048 	if (error != 0)
1049 		goto fail;
1050 
1051 	for (i = 0; i < nitems(rfini2); i++) {
1052 		error = zyd_rfwrite(sc, rfini2[i]);
1053 		if (error != 0)
1054 			goto fail;
1055 	}
1056 
1057 	for (i = 0; i < nitems(phypll); i++)
1058 		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1059 
1060 	for (i = 0; i < nitems(rfini3); i++) {
1061 		error = zyd_rfwrite(sc, rfini3[i]);
1062 		if (error != 0)
1063 			goto fail;
1064 	}
1065 fail:
1066 	return (error);
1067 }
1068 
1069 static int
1070 zyd_al2230_fini(struct zyd_rf *rf)
1071 {
1072 	int error, i;
1073 	struct zyd_softc *sc = rf->rf_sc;
1074 	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1075 
1076 	for (i = 0; i < nitems(phy); i++)
1077 		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1078 
1079 	if (sc->sc_newphy != 0)
1080 		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1081 
1082 	zyd_write16_m(sc, ZYD_CR203, 0x6);
1083 fail:
1084 	return (error);
1085 }
1086 
1087 static int
1088 zyd_al2230_init_b(struct zyd_rf *rf)
1089 {
1090 	struct zyd_softc *sc = rf->rf_sc;
1091 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1092 	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1093 	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1094 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1095 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1096 	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1097 	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1098 	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1099 	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1100 	int i, error;
1101 
1102 	for (i = 0; i < nitems(phy1); i++)
1103 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1104 
1105 	/* init RF-dependent PHY registers */
1106 	for (i = 0; i < nitems(phyini); i++)
1107 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1108 
1109 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1110 		for (i = 0; i < nitems(phy2230s); i++)
1111 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1112 	}
1113 
1114 	for (i = 0; i < 3; i++) {
1115 		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1116 		if (error != 0)
1117 			return (error);
1118 	}
1119 
1120 	for (i = 0; i < nitems(rfini_part1); i++) {
1121 		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1122 		if (error != 0)
1123 			return (error);
1124 	}
1125 
1126 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1127 		error = zyd_rfwrite(sc, 0x241000);
1128 	else
1129 		error = zyd_rfwrite(sc, 0x25a000);
1130 	if (error != 0)
1131 		goto fail;
1132 
1133 	for (i = 0; i < nitems(rfini_part2); i++) {
1134 		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1135 		if (error != 0)
1136 			return (error);
1137 	}
1138 
1139 	for (i = 0; i < nitems(phy2); i++)
1140 		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1141 
1142 	for (i = 0; i < nitems(rfini_part3); i++) {
1143 		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1144 		if (error != 0)
1145 			return (error);
1146 	}
1147 
1148 	for (i = 0; i < nitems(phy3); i++)
1149 		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1150 
1151 	error = zyd_al2230_fini(rf);
1152 fail:
1153 	return (error);
1154 }
1155 
1156 static int
1157 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1158 {
1159 	struct zyd_softc *sc = rf->rf_sc;
1160 	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1161 
1162 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1163 	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1164 fail:
1165 	return (error);
1166 }
1167 
1168 static int
1169 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1170 {
1171 	int error, i;
1172 	struct zyd_softc *sc = rf->rf_sc;
1173 	static const struct zyd_phy_pair phy1[] = {
1174 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1175 	};
1176 	static const struct {
1177 		uint32_t	r1, r2, r3;
1178 	} rfprog[] = ZYD_AL2230_CHANTABLE;
1179 
1180 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1181 	if (error != 0)
1182 		goto fail;
1183 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1184 	if (error != 0)
1185 		goto fail;
1186 	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1187 	if (error != 0)
1188 		goto fail;
1189 
1190 	for (i = 0; i < nitems(phy1); i++)
1191 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1192 fail:
1193 	return (error);
1194 }
1195 
1196 static int
1197 zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1198 {
1199 	int error, i;
1200 	struct zyd_softc *sc = rf->rf_sc;
1201 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1202 	static const struct {
1203 		uint32_t	r1, r2, r3;
1204 	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1205 
1206 	for (i = 0; i < nitems(phy1); i++)
1207 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1208 
1209 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1210 	if (error != 0)
1211 		goto fail;
1212 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1213 	if (error != 0)
1214 		goto fail;
1215 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1216 	if (error != 0)
1217 		goto fail;
1218 	error = zyd_al2230_fini(rf);
1219 fail:
1220 	return (error);
1221 }
1222 
1223 #define	ZYD_AL2230_PHY_BANDEDGE6					\
1224 {									\
1225 	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1226 	{ ZYD_CR47,  0x1e }						\
1227 }
1228 
1229 static int
1230 zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1231 {
1232 	int error = 0, i;
1233 	struct zyd_softc *sc = rf->rf_sc;
1234 	struct ieee80211com *ic = &sc->sc_ic;
1235 	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1236 	int chan = ieee80211_chan2ieee(ic, c);
1237 
1238 	if (chan == 1 || chan == 11)
1239 		r[0].val = 0x12;
1240 
1241 	for (i = 0; i < nitems(r); i++)
1242 		zyd_write16_m(sc, r[i].reg, r[i].val);
1243 fail:
1244 	return (error);
1245 }
1246 
1247 /*
1248  * AL7230B RF methods.
1249  */
1250 static int
1251 zyd_al7230B_init(struct zyd_rf *rf)
1252 {
1253 	struct zyd_softc *sc = rf->rf_sc;
1254 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1255 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1256 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1257 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1258 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1259 	int i, error;
1260 
1261 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1262 
1263 	/* init RF-dependent PHY registers, part one */
1264 	for (i = 0; i < nitems(phyini_1); i++)
1265 		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1266 
1267 	/* init AL7230B radio, part one */
1268 	for (i = 0; i < nitems(rfini_1); i++) {
1269 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1270 			return (error);
1271 	}
1272 	/* init RF-dependent PHY registers, part two */
1273 	for (i = 0; i < nitems(phyini_2); i++)
1274 		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1275 
1276 	/* init AL7230B radio, part two */
1277 	for (i = 0; i < nitems(rfini_2); i++) {
1278 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1279 			return (error);
1280 	}
1281 	/* init RF-dependent PHY registers, part three */
1282 	for (i = 0; i < nitems(phyini_3); i++)
1283 		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1284 fail:
1285 	return (error);
1286 }
1287 
1288 static int
1289 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1290 {
1291 	int error;
1292 	struct zyd_softc *sc = rf->rf_sc;
1293 
1294 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1295 	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1296 fail:
1297 	return (error);
1298 }
1299 
1300 static int
1301 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1302 {
1303 	struct zyd_softc *sc = rf->rf_sc;
1304 	static const struct {
1305 		uint32_t	r1, r2;
1306 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1307 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1308 	int i, error;
1309 
1310 	zyd_write16_m(sc, ZYD_CR240, 0x57);
1311 	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1312 
1313 	for (i = 0; i < nitems(rfsc); i++) {
1314 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1315 			return (error);
1316 	}
1317 
1318 	zyd_write16_m(sc, ZYD_CR128, 0x14);
1319 	zyd_write16_m(sc, ZYD_CR129, 0x12);
1320 	zyd_write16_m(sc, ZYD_CR130, 0x10);
1321 	zyd_write16_m(sc, ZYD_CR38,  0x38);
1322 	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1323 
1324 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1325 	if (error != 0)
1326 		goto fail;
1327 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1328 	if (error != 0)
1329 		goto fail;
1330 	error = zyd_rfwrite(sc, 0x3c9000);
1331 	if (error != 0)
1332 		goto fail;
1333 
1334 	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1335 	zyd_write16_m(sc, ZYD_CR203, 0x06);
1336 	zyd_write16_m(sc, ZYD_CR240, 0x08);
1337 fail:
1338 	return (error);
1339 }
1340 
1341 /*
1342  * AL2210 RF methods.
1343  */
1344 static int
1345 zyd_al2210_init(struct zyd_rf *rf)
1346 {
1347 	struct zyd_softc *sc = rf->rf_sc;
1348 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1349 	static const uint32_t rfini[] = ZYD_AL2210_RF;
1350 	uint32_t tmp;
1351 	int i, error;
1352 
1353 	zyd_write32_m(sc, ZYD_CR18, 2);
1354 
1355 	/* init RF-dependent PHY registers */
1356 	for (i = 0; i < nitems(phyini); i++)
1357 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1358 
1359 	/* init AL2210 radio */
1360 	for (i = 0; i < nitems(rfini); i++) {
1361 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1362 			return (error);
1363 	}
1364 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1365 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1366 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1367 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1368 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1369 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1370 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1371 	zyd_write32_m(sc, ZYD_CR18, 3);
1372 fail:
1373 	return (error);
1374 }
1375 
1376 static int
1377 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1378 {
1379 	/* vendor driver does nothing for this RF chip */
1380 
1381 	return (0);
1382 }
1383 
1384 static int
1385 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1386 {
1387 	int error;
1388 	struct zyd_softc *sc = rf->rf_sc;
1389 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1390 	uint32_t tmp;
1391 
1392 	zyd_write32_m(sc, ZYD_CR18, 2);
1393 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1394 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1395 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1396 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1397 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1398 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1399 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1400 
1401 	/* actually set the channel */
1402 	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1403 	if (error != 0)
1404 		goto fail;
1405 
1406 	zyd_write32_m(sc, ZYD_CR18, 3);
1407 fail:
1408 	return (error);
1409 }
1410 
1411 /*
1412  * GCT RF methods.
1413  */
1414 static int
1415 zyd_gct_init(struct zyd_rf *rf)
1416 {
1417 #define	ZYD_GCT_INTR_REG	0x85c1
1418 	struct zyd_softc *sc = rf->rf_sc;
1419 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1420 	static const uint32_t rfini[] = ZYD_GCT_RF;
1421 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1422 	int i, idx = -1, error;
1423 	uint16_t data;
1424 
1425 	/* init RF-dependent PHY registers */
1426 	for (i = 0; i < nitems(phyini); i++)
1427 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1428 
1429 	/* init cgt radio */
1430 	for (i = 0; i < nitems(rfini); i++) {
1431 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1432 			return (error);
1433 	}
1434 
1435 	error = zyd_gct_mode(rf);
1436 	if (error != 0)
1437 		return (error);
1438 
1439 	for (i = 0; i < (int)(nitems(vco) - 1); i++) {
1440 		error = zyd_gct_set_channel_synth(rf, 1, 0);
1441 		if (error != 0)
1442 			goto fail;
1443 		error = zyd_gct_write(rf, vco[i][0]);
1444 		if (error != 0)
1445 			goto fail;
1446 		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1447 		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1448 		if ((data & 0xf) == 0) {
1449 			idx = i;
1450 			break;
1451 		}
1452 	}
1453 	if (idx == -1) {
1454 		error = zyd_gct_set_channel_synth(rf, 1, 1);
1455 		if (error != 0)
1456 			goto fail;
1457 		error = zyd_gct_write(rf, 0x6662);
1458 		if (error != 0)
1459 			goto fail;
1460 	}
1461 
1462 	rf->idx = idx;
1463 	zyd_write16_m(sc, ZYD_CR203, 0x6);
1464 fail:
1465 	return (error);
1466 #undef ZYD_GCT_INTR_REG
1467 }
1468 
1469 static int
1470 zyd_gct_mode(struct zyd_rf *rf)
1471 {
1472 	struct zyd_softc *sc = rf->rf_sc;
1473 	static const uint32_t mode[] = {
1474 		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1475 	};
1476 	int i, error;
1477 
1478 	for (i = 0; i < nitems(mode); i++) {
1479 		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1480 			break;
1481 	}
1482 	return (error);
1483 }
1484 
1485 static int
1486 zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1487 {
1488 	int error, idx = chan - 1;
1489 	struct zyd_softc *sc = rf->rf_sc;
1490 	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1491 	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1492 	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1493 
1494 	error = zyd_rfwrite(sc,
1495 	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1496 	if (error != 0)
1497 		return (error);
1498 	return zyd_rfwrite(sc, div_synth[idx]);
1499 }
1500 
1501 static int
1502 zyd_gct_write(struct zyd_rf *rf, uint16_t value)
1503 {
1504 	struct zyd_softc *sc = rf->rf_sc;
1505 
1506 	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1507 }
1508 
1509 static int
1510 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1511 {
1512 	int error;
1513 	struct zyd_softc *sc = rf->rf_sc;
1514 
1515 	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1516 	if (error != 0)
1517 		return (error);
1518 
1519 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1520 	zyd_write16_m(sc, ZYD_CR251,
1521 	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1522 fail:
1523 	return (error);
1524 }
1525 
1526 static int
1527 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1528 {
1529 	int error, i;
1530 	struct zyd_softc *sc = rf->rf_sc;
1531 	static const struct zyd_phy_pair cmd[] = {
1532 		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1533 		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1534 	};
1535 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1536 
1537 	error = zyd_gct_set_channel_synth(rf, chan, 0);
1538 	if (error != 0)
1539 		goto fail;
1540 	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1541 	    vco[rf->idx][((chan - 1) / 2)]);
1542 	if (error != 0)
1543 		goto fail;
1544 	error = zyd_gct_mode(rf);
1545 	if (error != 0)
1546 		return (error);
1547 	for (i = 0; i < nitems(cmd); i++)
1548 		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1549 	error = zyd_gct_txgain(rf, chan);
1550 	if (error != 0)
1551 		return (error);
1552 	zyd_write16_m(sc, ZYD_CR203, 0x6);
1553 fail:
1554 	return (error);
1555 }
1556 
1557 static int
1558 zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1559 {
1560 	struct zyd_softc *sc = rf->rf_sc;
1561 	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1562 	uint8_t idx = sc->sc_pwrint[chan - 1];
1563 
1564 	if (idx >= nitems(txgain)) {
1565 		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1566 		    chan, idx);
1567 		return 0;
1568 	}
1569 
1570 	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1571 }
1572 
1573 /*
1574  * Maxim2 RF methods.
1575  */
1576 static int
1577 zyd_maxim2_init(struct zyd_rf *rf)
1578 {
1579 	struct zyd_softc *sc = rf->rf_sc;
1580 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1581 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1582 	uint16_t tmp;
1583 	int i, error;
1584 
1585 	/* init RF-dependent PHY registers */
1586 	for (i = 0; i < nitems(phyini); i++)
1587 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1588 
1589 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1590 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1591 
1592 	/* init maxim2 radio */
1593 	for (i = 0; i < nitems(rfini); i++) {
1594 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1595 			return (error);
1596 	}
1597 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1598 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1599 fail:
1600 	return (error);
1601 }
1602 
1603 static int
1604 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1605 {
1606 
1607 	/* vendor driver does nothing for this RF chip */
1608 	return (0);
1609 }
1610 
1611 static int
1612 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1613 {
1614 	struct zyd_softc *sc = rf->rf_sc;
1615 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1616 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1617 	static const struct {
1618 		uint32_t	r1, r2;
1619 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1620 	uint16_t tmp;
1621 	int i, error;
1622 
1623 	/*
1624 	 * Do the same as we do when initializing it, except for the channel
1625 	 * values coming from the two channel tables.
1626 	 */
1627 
1628 	/* init RF-dependent PHY registers */
1629 	for (i = 0; i < nitems(phyini); i++)
1630 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1631 
1632 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1633 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1634 
1635 	/* first two values taken from the chantables */
1636 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1637 	if (error != 0)
1638 		goto fail;
1639 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1640 	if (error != 0)
1641 		goto fail;
1642 
1643 	/* init maxim2 radio - skipping the two first values */
1644 	for (i = 2; i < nitems(rfini); i++) {
1645 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1646 			return (error);
1647 	}
1648 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1649 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1650 fail:
1651 	return (error);
1652 }
1653 
1654 static int
1655 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1656 {
1657 	struct zyd_rf *rf = &sc->sc_rf;
1658 
1659 	rf->rf_sc = sc;
1660 	rf->update_pwr = 1;
1661 
1662 	switch (type) {
1663 	case ZYD_RF_RFMD:
1664 		rf->init         = zyd_rfmd_init;
1665 		rf->switch_radio = zyd_rfmd_switch_radio;
1666 		rf->set_channel  = zyd_rfmd_set_channel;
1667 		rf->width        = 24;	/* 24-bit RF values */
1668 		break;
1669 	case ZYD_RF_AL2230:
1670 	case ZYD_RF_AL2230S:
1671 		if (sc->sc_macrev == ZYD_ZD1211B) {
1672 			rf->init = zyd_al2230_init_b;
1673 			rf->set_channel = zyd_al2230_set_channel_b;
1674 		} else {
1675 			rf->init = zyd_al2230_init;
1676 			rf->set_channel = zyd_al2230_set_channel;
1677 		}
1678 		rf->switch_radio = zyd_al2230_switch_radio;
1679 		rf->bandedge6	 = zyd_al2230_bandedge6;
1680 		rf->width        = 24;	/* 24-bit RF values */
1681 		break;
1682 	case ZYD_RF_AL7230B:
1683 		rf->init         = zyd_al7230B_init;
1684 		rf->switch_radio = zyd_al7230B_switch_radio;
1685 		rf->set_channel  = zyd_al7230B_set_channel;
1686 		rf->width        = 24;	/* 24-bit RF values */
1687 		break;
1688 	case ZYD_RF_AL2210:
1689 		rf->init         = zyd_al2210_init;
1690 		rf->switch_radio = zyd_al2210_switch_radio;
1691 		rf->set_channel  = zyd_al2210_set_channel;
1692 		rf->width        = 24;	/* 24-bit RF values */
1693 		break;
1694 	case ZYD_RF_MAXIM_NEW:
1695 	case ZYD_RF_GCT:
1696 		rf->init         = zyd_gct_init;
1697 		rf->switch_radio = zyd_gct_switch_radio;
1698 		rf->set_channel  = zyd_gct_set_channel;
1699 		rf->width        = 24;	/* 24-bit RF values */
1700 		rf->update_pwr   = 0;
1701 		break;
1702 	case ZYD_RF_MAXIM_NEW2:
1703 		rf->init         = zyd_maxim2_init;
1704 		rf->switch_radio = zyd_maxim2_switch_radio;
1705 		rf->set_channel  = zyd_maxim2_set_channel;
1706 		rf->width        = 18;	/* 18-bit RF values */
1707 		break;
1708 	default:
1709 		device_printf(sc->sc_dev,
1710 		    "sorry, radio \"%s\" is not supported yet\n",
1711 		    zyd_rf_name(type));
1712 		return (EINVAL);
1713 	}
1714 	return (0);
1715 }
1716 
1717 static const char *
1718 zyd_rf_name(uint8_t type)
1719 {
1720 	static const char * const zyd_rfs[] = {
1721 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1722 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1723 		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1724 		"PHILIPS"
1725 	};
1726 
1727 	return zyd_rfs[(type > 15) ? 0 : type];
1728 }
1729 
1730 static int
1731 zyd_hw_init(struct zyd_softc *sc)
1732 {
1733 	int error;
1734 	const struct zyd_phy_pair *phyp;
1735 	struct zyd_rf *rf = &sc->sc_rf;
1736 	uint16_t val;
1737 
1738 	/* specify that the plug and play is finished */
1739 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1740 	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1741 	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1742 	    sc->sc_fwbase);
1743 
1744 	/* retrieve firmware revision number */
1745 	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1746 	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1747 	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1748 	/* set mandatory rates - XXX assumes 802.11b/g */
1749 	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1750 
1751 	/* disable interrupts */
1752 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1753 
1754 	if ((error = zyd_read_pod(sc)) != 0) {
1755 		device_printf(sc->sc_dev, "could not read EEPROM\n");
1756 		goto fail;
1757 	}
1758 
1759 	/* PHY init (resetting) */
1760 	error = zyd_lock_phy(sc);
1761 	if (error != 0)
1762 		goto fail;
1763 	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1764 	for (; phyp->reg != 0; phyp++)
1765 		zyd_write16_m(sc, phyp->reg, phyp->val);
1766 	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1767 		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1768 		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1769 	}
1770 	error = zyd_unlock_phy(sc);
1771 	if (error != 0)
1772 		goto fail;
1773 
1774 	/* HMAC init */
1775 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1776 	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1777 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1778 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1779 	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1780 	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1781 	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1782 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1783 	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1784 	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1785 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1786 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1787 	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1788 	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1789 	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1790 	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1791 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1792 	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1793 	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1794 	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1795 
1796 	if (sc->sc_macrev == ZYD_ZD1211) {
1797 		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1798 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1799 	} else {
1800 		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1801 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1802 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1803 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1804 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1805 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1806 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1807 		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1808 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1809 	}
1810 
1811 	/* init beacon interval to 100ms */
1812 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1813 		goto fail;
1814 
1815 	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1816 		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1817 		    sc->sc_rfrev);
1818 		goto fail;
1819 	}
1820 
1821 	/* RF chip init */
1822 	error = zyd_lock_phy(sc);
1823 	if (error != 0)
1824 		goto fail;
1825 	error = (*rf->init)(rf);
1826 	if (error != 0) {
1827 		device_printf(sc->sc_dev,
1828 		    "radio initialization failed, error %d\n", error);
1829 		goto fail;
1830 	}
1831 	error = zyd_unlock_phy(sc);
1832 	if (error != 0)
1833 		goto fail;
1834 
1835 	if ((error = zyd_read_eeprom(sc)) != 0) {
1836 		device_printf(sc->sc_dev, "could not read EEPROM\n");
1837 		goto fail;
1838 	}
1839 
1840 fail:	return (error);
1841 }
1842 
1843 static int
1844 zyd_read_pod(struct zyd_softc *sc)
1845 {
1846 	int error;
1847 	uint32_t tmp;
1848 
1849 	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1850 	sc->sc_rfrev     = tmp & 0x0f;
1851 	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1852 	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1853 	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1854 	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1855 	sc->sc_parev     = (tmp >> 16) & 0x0f;
1856 	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1857 	sc->sc_newphy    = (tmp >> 31) & 0x01;
1858 	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1859 fail:
1860 	return (error);
1861 }
1862 
1863 static int
1864 zyd_read_eeprom(struct zyd_softc *sc)
1865 {
1866 	uint16_t val;
1867 	int error, i;
1868 
1869 	/* read Tx power calibration tables */
1870 	for (i = 0; i < 7; i++) {
1871 		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1872 		sc->sc_pwrcal[i * 2] = val >> 8;
1873 		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1874 		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1875 		sc->sc_pwrint[i * 2] = val >> 8;
1876 		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1877 		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1878 		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1879 		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1880 		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1881 		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1882 		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1883 		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1884 		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1885 		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1886 	}
1887 fail:
1888 	return (error);
1889 }
1890 
1891 static int
1892 zyd_get_macaddr(struct zyd_softc *sc)
1893 {
1894 	struct usb_device_request req;
1895 	usb_error_t error;
1896 
1897 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1898 	req.bRequest = ZYD_READFWDATAREQ;
1899 	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1900 	USETW(req.wIndex, 0);
1901 	USETW(req.wLength, IEEE80211_ADDR_LEN);
1902 
1903 	error = zyd_do_request(sc, &req, sc->sc_ic.ic_macaddr);
1904 	if (error != 0) {
1905 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1906 		    usbd_errstr(error));
1907 	}
1908 
1909 	return (error);
1910 }
1911 
1912 static int
1913 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1914 {
1915 	int error;
1916 	uint32_t tmp;
1917 
1918 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1919 	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1920 	tmp = addr[5] << 8 | addr[4];
1921 	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1922 fail:
1923 	return (error);
1924 }
1925 
1926 static int
1927 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1928 {
1929 	int error;
1930 	uint32_t tmp;
1931 
1932 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1933 	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1934 	tmp = addr[5] << 8 | addr[4];
1935 	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1936 fail:
1937 	return (error);
1938 }
1939 
1940 static int
1941 zyd_switch_radio(struct zyd_softc *sc, int on)
1942 {
1943 	struct zyd_rf *rf = &sc->sc_rf;
1944 	int error;
1945 
1946 	error = zyd_lock_phy(sc);
1947 	if (error != 0)
1948 		goto fail;
1949 	error = (*rf->switch_radio)(rf, on);
1950 	if (error != 0)
1951 		goto fail;
1952 	error = zyd_unlock_phy(sc);
1953 fail:
1954 	return (error);
1955 }
1956 
1957 static int
1958 zyd_set_led(struct zyd_softc *sc, int which, int on)
1959 {
1960 	int error;
1961 	uint32_t tmp;
1962 
1963 	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1964 	tmp &= ~which;
1965 	if (on)
1966 		tmp |= which;
1967 	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1968 fail:
1969 	return (error);
1970 }
1971 
1972 static u_int
1973 zyd_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
1974 {
1975 	uint32_t *hash = arg;
1976 	uint8_t v;
1977 
1978 	v = ((uint8_t *)LLADDR(sdl))[5] >> 2;
1979 	if (v < 32)
1980 		hash[0] |= 1 << v;
1981 	else
1982 		hash[1] |= 1 << (v - 32);
1983 
1984 	return (1);
1985 }
1986 
1987 static void
1988 zyd_set_multi(struct zyd_softc *sc)
1989 {
1990 	struct ieee80211com *ic = &sc->sc_ic;
1991 	uint32_t hash[2];
1992 	int error;
1993 
1994 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0)
1995 		return;
1996 
1997 	hash[0] = 0x00000000;
1998 	hash[1] = 0x80000000;
1999 
2000 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
2001 	    ic->ic_promisc > 0) {
2002 		hash[0] = 0xffffffff;
2003 		hash[1] = 0xffffffff;
2004 	} else {
2005 		struct ieee80211vap *vap;
2006 
2007 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2008 			if_foreach_llmaddr(vap->iv_ifp, zyd_hash_maddr, &hash);
2009 	}
2010 
2011 	/* reprogram multicast global hash table */
2012 	zyd_write32_m(sc, ZYD_MAC_GHTBL, hash[0]);
2013 	zyd_write32_m(sc, ZYD_MAC_GHTBH, hash[1]);
2014 fail:
2015 	if (error != 0)
2016 		device_printf(sc->sc_dev,
2017 		    "could not set multicast hash table\n");
2018 }
2019 
2020 static void
2021 zyd_update_mcast(struct ieee80211com *ic)
2022 {
2023 	struct zyd_softc *sc = ic->ic_softc;
2024 
2025 	ZYD_LOCK(sc);
2026 	zyd_set_multi(sc);
2027 	ZYD_UNLOCK(sc);
2028 }
2029 
2030 static int
2031 zyd_set_rxfilter(struct zyd_softc *sc)
2032 {
2033 	struct ieee80211com *ic = &sc->sc_ic;
2034 	uint32_t rxfilter;
2035 
2036 	switch (ic->ic_opmode) {
2037 	case IEEE80211_M_STA:
2038 		rxfilter = ZYD_FILTER_BSS;
2039 		break;
2040 	case IEEE80211_M_IBSS:
2041 	case IEEE80211_M_HOSTAP:
2042 		rxfilter = ZYD_FILTER_HOSTAP;
2043 		break;
2044 	case IEEE80211_M_MONITOR:
2045 		rxfilter = ZYD_FILTER_MONITOR;
2046 		break;
2047 	default:
2048 		/* should not get there */
2049 		return (EINVAL);
2050 	}
2051 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2052 }
2053 
2054 static void
2055 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2056 {
2057 	int error;
2058 	struct ieee80211com *ic = &sc->sc_ic;
2059 	struct zyd_rf *rf = &sc->sc_rf;
2060 	uint32_t tmp;
2061 	int chan;
2062 
2063 	chan = ieee80211_chan2ieee(ic, c);
2064 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2065 		/* XXX should NEVER happen */
2066 		device_printf(sc->sc_dev,
2067 		    "%s: invalid channel %x\n", __func__, chan);
2068 		return;
2069 	}
2070 
2071 	error = zyd_lock_phy(sc);
2072 	if (error != 0)
2073 		goto fail;
2074 
2075 	error = (*rf->set_channel)(rf, chan);
2076 	if (error != 0)
2077 		goto fail;
2078 
2079 	if (rf->update_pwr) {
2080 		/* update Tx power */
2081 		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2082 
2083 		if (sc->sc_macrev == ZYD_ZD1211B) {
2084 			zyd_write16_m(sc, ZYD_CR67,
2085 			    sc->sc_ofdm36_cal[chan - 1]);
2086 			zyd_write16_m(sc, ZYD_CR66,
2087 			    sc->sc_ofdm48_cal[chan - 1]);
2088 			zyd_write16_m(sc, ZYD_CR65,
2089 			    sc->sc_ofdm54_cal[chan - 1]);
2090 			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2091 			zyd_write16_m(sc, ZYD_CR69, 0x28);
2092 			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2093 		}
2094 	}
2095 	if (sc->sc_cckgain) {
2096 		/* set CCK baseband gain from EEPROM */
2097 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2098 			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2099 	}
2100 	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2101 		error = (*rf->bandedge6)(rf, c);
2102 		if (error != 0)
2103 			goto fail;
2104 	}
2105 	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2106 
2107 	error = zyd_unlock_phy(sc);
2108 	if (error != 0)
2109 		goto fail;
2110 
2111 	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2112 	    htole16(c->ic_freq);
2113 	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2114 	    htole16(c->ic_flags);
2115 fail:
2116 	return;
2117 }
2118 
2119 static int
2120 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2121 {
2122 	int error;
2123 	uint32_t val;
2124 
2125 	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2126 	sc->sc_atim_wnd = val;
2127 	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2128 	sc->sc_pre_tbtt = val;
2129 	sc->sc_bcn_int = bintval;
2130 
2131 	if (sc->sc_bcn_int <= 5)
2132 		sc->sc_bcn_int = 5;
2133 	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2134 		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2135 	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2136 		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2137 
2138 	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2139 	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2140 	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2141 fail:
2142 	return (error);
2143 }
2144 
2145 static void
2146 zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2147 {
2148 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2149 	struct ieee80211com *ic = &sc->sc_ic;
2150 	struct zyd_plcphdr plcp;
2151 	struct zyd_rx_stat stat;
2152 	struct usb_page_cache *pc;
2153 	struct mbuf *m;
2154 	int rlen, rssi;
2155 
2156 	if (len < ZYD_MIN_FRAGSZ) {
2157 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2158 		    device_get_nameunit(sc->sc_dev), len);
2159 		counter_u64_add(ic->ic_ierrors, 1);
2160 		return;
2161 	}
2162 	pc = usbd_xfer_get_frame(xfer, 0);
2163 	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2164 	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2165 
2166 	if (stat.flags & ZYD_RX_ERROR) {
2167 		DPRINTF(sc, ZYD_DEBUG_RECV,
2168 		    "%s: RX status indicated error (%x)\n",
2169 		    device_get_nameunit(sc->sc_dev), stat.flags);
2170 		counter_u64_add(ic->ic_ierrors, 1);
2171 		return;
2172 	}
2173 
2174 	/* compute actual frame length */
2175 	rlen = len - sizeof(struct zyd_plcphdr) -
2176 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2177 
2178 	/* allocate a mbuf to store the frame */
2179 	if (rlen > (int)MCLBYTES) {
2180 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2181 		    device_get_nameunit(sc->sc_dev), rlen);
2182 		counter_u64_add(ic->ic_ierrors, 1);
2183 		return;
2184 	} else if (rlen > (int)MHLEN)
2185 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2186 	else
2187 		m = m_gethdr(M_NOWAIT, MT_DATA);
2188 	if (m == NULL) {
2189 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2190 		    device_get_nameunit(sc->sc_dev));
2191 		counter_u64_add(ic->ic_ierrors, 1);
2192 		return;
2193 	}
2194 	m->m_pkthdr.len = m->m_len = rlen;
2195 	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2196 
2197 	if (ieee80211_radiotap_active(ic)) {
2198 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2199 
2200 		tap->wr_flags = 0;
2201 		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2202 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2203 		/* XXX toss, no way to express errors */
2204 		if (stat.flags & ZYD_RX_DECRYPTERR)
2205 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2206 		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2207 		    (stat.flags & ZYD_RX_OFDM) ?
2208 			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2209 		tap->wr_antsignal = stat.rssi + -95;
2210 		tap->wr_antnoise = -95;	/* XXX */
2211 	}
2212 	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2213 
2214 	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2215 	sc->sc_rx_data[sc->sc_rx_count].m = m;
2216 	sc->sc_rx_count++;
2217 }
2218 
2219 static void
2220 zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2221 {
2222 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2223 	struct ieee80211com *ic = &sc->sc_ic;
2224 	struct ieee80211_node *ni;
2225 	struct epoch_tracker et;
2226 	struct zyd_rx_desc desc;
2227 	struct mbuf *m;
2228 	struct usb_page_cache *pc;
2229 	uint32_t offset;
2230 	uint8_t rssi;
2231 	int8_t nf;
2232 	int i;
2233 	int actlen;
2234 
2235 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2236 
2237 	sc->sc_rx_count = 0;
2238 	switch (USB_GET_STATE(xfer)) {
2239 	case USB_ST_TRANSFERRED:
2240 		pc = usbd_xfer_get_frame(xfer, 0);
2241 		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2242 
2243 		offset = 0;
2244 		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2245 			DPRINTF(sc, ZYD_DEBUG_RECV,
2246 			    "%s: received multi-frame transfer\n", __func__);
2247 
2248 			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2249 				uint16_t len16 = UGETW(desc.len[i]);
2250 
2251 				if (len16 == 0 || len16 > actlen)
2252 					break;
2253 
2254 				zyd_rx_data(xfer, offset, len16);
2255 
2256 				/* next frame is aligned on a 32-bit boundary */
2257 				len16 = (len16 + 3) & ~3;
2258 				offset += len16;
2259 				if (len16 > actlen)
2260 					break;
2261 				actlen -= len16;
2262 			}
2263 		} else {
2264 			DPRINTF(sc, ZYD_DEBUG_RECV,
2265 			    "%s: received single-frame transfer\n", __func__);
2266 
2267 			zyd_rx_data(xfer, 0, actlen);
2268 		}
2269 		/* FALLTHROUGH */
2270 	case USB_ST_SETUP:
2271 tr_setup:
2272 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2273 		usbd_transfer_submit(xfer);
2274 
2275 		/*
2276 		 * At the end of a USB callback it is always safe to unlock
2277 		 * the private mutex of a device! That is why we do the
2278 		 * "ieee80211_input" here, and not some lines up!
2279 		 */
2280 		ZYD_UNLOCK(sc);
2281 		NET_EPOCH_ENTER(et);
2282 		for (i = 0; i < sc->sc_rx_count; i++) {
2283 			rssi = sc->sc_rx_data[i].rssi;
2284 			m = sc->sc_rx_data[i].m;
2285 			sc->sc_rx_data[i].m = NULL;
2286 
2287 			nf = -95;	/* XXX */
2288 
2289 			ni = ieee80211_find_rxnode(ic,
2290 			    mtod(m, struct ieee80211_frame_min *));
2291 			if (ni != NULL) {
2292 				(void)ieee80211_input(ni, m, rssi, nf);
2293 				ieee80211_free_node(ni);
2294 			} else
2295 				(void)ieee80211_input_all(ic, m, rssi, nf);
2296 		}
2297 		NET_EPOCH_EXIT(et);
2298 		ZYD_LOCK(sc);
2299 		zyd_start(sc);
2300 		break;
2301 
2302 	default:			/* Error */
2303 		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2304 
2305 		if (error != USB_ERR_CANCELLED) {
2306 			/* try to clear stall first */
2307 			usbd_xfer_set_stall(xfer);
2308 			goto tr_setup;
2309 		}
2310 		break;
2311 	}
2312 }
2313 
2314 static uint8_t
2315 zyd_plcp_signal(struct zyd_softc *sc, int rate)
2316 {
2317 	switch (rate) {
2318 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2319 	case 12:
2320 		return (0xb);
2321 	case 18:
2322 		return (0xf);
2323 	case 24:
2324 		return (0xa);
2325 	case 36:
2326 		return (0xe);
2327 	case 48:
2328 		return (0x9);
2329 	case 72:
2330 		return (0xd);
2331 	case 96:
2332 		return (0x8);
2333 	case 108:
2334 		return (0xc);
2335 	/* CCK rates (NB: not IEEE std, device-specific) */
2336 	case 2:
2337 		return (0x0);
2338 	case 4:
2339 		return (0x1);
2340 	case 11:
2341 		return (0x2);
2342 	case 22:
2343 		return (0x3);
2344 	}
2345 
2346 	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2347 	return (0x0);
2348 }
2349 
2350 static void
2351 zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2352 {
2353 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2354 	struct ieee80211vap *vap;
2355 	struct zyd_tx_data *data;
2356 	struct mbuf *m;
2357 	struct usb_page_cache *pc;
2358 	int actlen;
2359 
2360 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2361 
2362 	switch (USB_GET_STATE(xfer)) {
2363 	case USB_ST_TRANSFERRED:
2364 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2365 		    actlen);
2366 
2367 		/* free resources */
2368 		data = usbd_xfer_get_priv(xfer);
2369 		zyd_tx_free(data, 0);
2370 		usbd_xfer_set_priv(xfer, NULL);
2371 
2372 		/* FALLTHROUGH */
2373 	case USB_ST_SETUP:
2374 tr_setup:
2375 		data = STAILQ_FIRST(&sc->tx_q);
2376 		if (data) {
2377 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2378 			m = data->m;
2379 
2380 			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2381 				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2382 				    m->m_pkthdr.len);
2383 				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2384 			}
2385 			pc = usbd_xfer_get_frame(xfer, 0);
2386 			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2387 			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2388 			    m->m_pkthdr.len);
2389 
2390 			vap = data->ni->ni_vap;
2391 			if (ieee80211_radiotap_active_vap(vap)) {
2392 				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2393 
2394 				tap->wt_flags = 0;
2395 				tap->wt_rate = data->rate;
2396 
2397 				ieee80211_radiotap_tx(vap, m);
2398 			}
2399 
2400 			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2401 			usbd_xfer_set_priv(xfer, data);
2402 			usbd_transfer_submit(xfer);
2403 		}
2404 		zyd_start(sc);
2405 		break;
2406 
2407 	default:			/* Error */
2408 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2409 		    usbd_errstr(error));
2410 
2411 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
2412 		data = usbd_xfer_get_priv(xfer);
2413 		usbd_xfer_set_priv(xfer, NULL);
2414 		if (data != NULL)
2415 			zyd_tx_free(data, error);
2416 
2417 		if (error != USB_ERR_CANCELLED) {
2418 			if (error == USB_ERR_TIMEOUT)
2419 				device_printf(sc->sc_dev, "device timeout\n");
2420 
2421 			/*
2422 			 * Try to clear stall first, also if other
2423 			 * errors occur, hence clearing stall
2424 			 * introduces a 50 ms delay:
2425 			 */
2426 			usbd_xfer_set_stall(xfer);
2427 			goto tr_setup;
2428 		}
2429 		break;
2430 	}
2431 }
2432 
2433 static int
2434 zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2435 {
2436 	struct ieee80211vap *vap = ni->ni_vap;
2437 	struct ieee80211com *ic = ni->ni_ic;
2438 	struct zyd_tx_desc *desc;
2439 	struct zyd_tx_data *data;
2440 	struct ieee80211_frame *wh;
2441 	const struct ieee80211_txparam *tp = ni->ni_txparms;
2442 	struct ieee80211_key *k;
2443 	int rate, totlen, type, ismcast;
2444 	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2445 	uint8_t phy;
2446 	uint16_t pktlen;
2447 	uint32_t bits;
2448 
2449 	wh = mtod(m0, struct ieee80211_frame *);
2450 	data = STAILQ_FIRST(&sc->tx_free);
2451 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2452 	sc->tx_nfree--;
2453 
2454 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2455 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2456 
2457 	if (type == IEEE80211_FC0_TYPE_MGT ||
2458 	    type == IEEE80211_FC0_TYPE_CTL ||
2459 	    (m0->m_flags & M_EAPOL) != 0) {
2460 		rate = tp->mgmtrate;
2461 	} else {
2462 		/* for data frames */
2463 		if (ismcast)
2464 			rate = tp->mcastrate;
2465 		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2466 			rate = tp->ucastrate;
2467 		else {
2468 			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2469 			rate = ni->ni_txrate;
2470 		}
2471 	}
2472 
2473 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2474 		k = ieee80211_crypto_encap(ni, m0);
2475 		if (k == NULL) {
2476 			return (ENOBUFS);
2477 		}
2478 		/* packet header may have moved, reset our local pointer */
2479 		wh = mtod(m0, struct ieee80211_frame *);
2480 	}
2481 
2482 	data->ni = ni;
2483 	data->m = m0;
2484 	data->rate = rate;
2485 
2486 	/* fill Tx descriptor */
2487 	desc = &data->desc;
2488 	phy = zyd_plcp_signal(sc, rate);
2489 	desc->phy = phy;
2490 	if (ZYD_RATE_IS_OFDM(rate)) {
2491 		desc->phy |= ZYD_TX_PHY_OFDM;
2492 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2493 			desc->phy |= ZYD_TX_PHY_5GHZ;
2494 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2495 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2496 
2497 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2498 	desc->len = htole16(totlen);
2499 
2500 	desc->flags = ZYD_TX_FLAG_BACKOFF;
2501 	if (!ismcast) {
2502 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2503 		if (totlen > vap->iv_rtsthreshold) {
2504 			desc->flags |= ZYD_TX_FLAG_RTS;
2505 		} else if (ZYD_RATE_IS_OFDM(rate) &&
2506 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2507 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2508 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2509 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2510 				desc->flags |= ZYD_TX_FLAG_RTS;
2511 		}
2512 	} else
2513 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2514 	if ((wh->i_fc[0] &
2515 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2516 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2517 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2518 
2519 	/* actual transmit length (XXX why +10?) */
2520 	pktlen = ZYD_TX_DESC_SIZE + 10;
2521 	if (sc->sc_macrev == ZYD_ZD1211)
2522 		pktlen += totlen;
2523 	desc->pktlen = htole16(pktlen);
2524 
2525 	bits = (rate == 11) ? (totlen * 16) + 10 :
2526 	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2527 	desc->plcp_length = htole16(bits / ratediv[phy]);
2528 	desc->plcp_service = 0;
2529 	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2530 		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2531 	desc->nextlen = 0;
2532 
2533 	if (ieee80211_radiotap_active_vap(vap)) {
2534 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2535 
2536 		tap->wt_flags = 0;
2537 		tap->wt_rate = rate;
2538 
2539 		ieee80211_radiotap_tx(vap, m0);
2540 	}
2541 
2542 	DPRINTF(sc, ZYD_DEBUG_XMIT,
2543 	    "%s: sending data frame len=%zu rate=%u\n",
2544 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2545 		rate);
2546 
2547 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2548 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2549 
2550 	return (0);
2551 }
2552 
2553 static int
2554 zyd_transmit(struct ieee80211com *ic, struct mbuf *m)
2555 {
2556 	struct zyd_softc *sc = ic->ic_softc;
2557 	int error;
2558 
2559 	ZYD_LOCK(sc);
2560 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2561 		ZYD_UNLOCK(sc);
2562 		return (ENXIO);
2563 	}
2564 	error = mbufq_enqueue(&sc->sc_snd, m);
2565 	if (error) {
2566 		ZYD_UNLOCK(sc);
2567 		return (error);
2568 	}
2569 	zyd_start(sc);
2570 	ZYD_UNLOCK(sc);
2571 
2572 	return (0);
2573 }
2574 
2575 static void
2576 zyd_start(struct zyd_softc *sc)
2577 {
2578 	struct ieee80211_node *ni;
2579 	struct mbuf *m;
2580 
2581 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2582 
2583 	while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2584 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2585 		if (zyd_tx_start(sc, m, ni) != 0) {
2586 			m_freem(m);
2587 			if_inc_counter(ni->ni_vap->iv_ifp,
2588 			    IFCOUNTER_OERRORS, 1);
2589 			ieee80211_free_node(ni);
2590 			break;
2591 		}
2592 	}
2593 }
2594 
2595 static int
2596 zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2597 	const struct ieee80211_bpf_params *params)
2598 {
2599 	struct ieee80211com *ic = ni->ni_ic;
2600 	struct zyd_softc *sc = ic->ic_softc;
2601 
2602 	ZYD_LOCK(sc);
2603 	/* prevent management frames from being sent if we're not ready */
2604 	if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) {
2605 		ZYD_UNLOCK(sc);
2606 		m_freem(m);
2607 		return (ENETDOWN);
2608 	}
2609 	if (sc->tx_nfree == 0) {
2610 		ZYD_UNLOCK(sc);
2611 		m_freem(m);
2612 		return (ENOBUFS);		/* XXX */
2613 	}
2614 
2615 	/*
2616 	 * Legacy path; interpret frame contents to decide
2617 	 * precisely how to send the frame.
2618 	 * XXX raw path
2619 	 */
2620 	if (zyd_tx_start(sc, m, ni) != 0) {
2621 		ZYD_UNLOCK(sc);
2622 		m_freem(m);
2623 		return (EIO);
2624 	}
2625 	ZYD_UNLOCK(sc);
2626 	return (0);
2627 }
2628 
2629 static void
2630 zyd_parent(struct ieee80211com *ic)
2631 {
2632 	struct zyd_softc *sc = ic->ic_softc;
2633 	int startall = 0;
2634 
2635 	ZYD_LOCK(sc);
2636 	if (sc->sc_flags & ZYD_FLAG_DETACHED) {
2637 		ZYD_UNLOCK(sc);
2638 		return;
2639 	}
2640 	if (ic->ic_nrunning > 0) {
2641 		if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2642 			zyd_init_locked(sc);
2643 			startall = 1;
2644 		} else
2645 			zyd_set_multi(sc);
2646 	} else if (sc->sc_flags & ZYD_FLAG_RUNNING)
2647 		zyd_stop(sc);
2648 	ZYD_UNLOCK(sc);
2649 	if (startall)
2650 		ieee80211_start_all(ic);
2651 }
2652 
2653 static void
2654 zyd_init_locked(struct zyd_softc *sc)
2655 {
2656 	struct ieee80211com *ic = &sc->sc_ic;
2657 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2658 	struct usb_config_descriptor *cd;
2659 	int error;
2660 	uint32_t val;
2661 
2662 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2663 
2664 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2665 		error = zyd_loadfirmware(sc);
2666 		if (error != 0) {
2667 			device_printf(sc->sc_dev,
2668 			    "could not load firmware (error=%d)\n", error);
2669 			goto fail;
2670 		}
2671 
2672 		/* reset device */
2673 		cd = usbd_get_config_descriptor(sc->sc_udev);
2674 		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2675 		    cd->bConfigurationValue);
2676 		if (error)
2677 			device_printf(sc->sc_dev, "reset failed, continuing\n");
2678 
2679 		error = zyd_hw_init(sc);
2680 		if (error) {
2681 			device_printf(sc->sc_dev,
2682 			    "hardware initialization failed\n");
2683 			goto fail;
2684 		}
2685 
2686 		device_printf(sc->sc_dev,
2687 		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2688 		    "BE%x NP%x Gain%x F%x\n",
2689 		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2690 		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2691 		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2692 		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2693 		    sc->sc_cckgain, sc->sc_fix_cr157);
2694 
2695 		/* read regulatory domain (currently unused) */
2696 		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2697 		sc->sc_regdomain = val >> 16;
2698 		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2699 		    sc->sc_regdomain);
2700 
2701 		/* we'll do software WEP decryption for now */
2702 		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2703 		    __func__);
2704 		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2705 
2706 		sc->sc_flags |= ZYD_FLAG_INITONCE;
2707 	}
2708 
2709 	if (sc->sc_flags & ZYD_FLAG_RUNNING)
2710 		zyd_stop(sc);
2711 
2712 	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2713 	    vap ? vap->iv_myaddr : ic->ic_macaddr, ":");
2714 	error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2715 	if (error != 0)
2716 		return;
2717 
2718 	/* set basic rates */
2719 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2720 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2721 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2722 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2723 	else	/* assumes 802.11b/g */
2724 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2725 
2726 	/* promiscuous mode */
2727 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2728 	/* multicast setup */
2729 	zyd_set_multi(sc);
2730 	/* set RX filter  */
2731 	error = zyd_set_rxfilter(sc);
2732 	if (error != 0)
2733 		goto fail;
2734 
2735 	/* switch radio transmitter ON */
2736 	error = zyd_switch_radio(sc, 1);
2737 	if (error != 0)
2738 		goto fail;
2739 	/* set default BSS channel */
2740 	zyd_set_chan(sc, ic->ic_curchan);
2741 
2742 	/*
2743 	 * Allocate Tx and Rx xfer queues.
2744 	 */
2745 	zyd_setup_tx_list(sc);
2746 
2747 	/* enable interrupts */
2748 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2749 
2750 	sc->sc_flags |= ZYD_FLAG_RUNNING;
2751 	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2752 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2753 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2754 
2755 	return;
2756 
2757 fail:	zyd_stop(sc);
2758 	return;
2759 }
2760 
2761 static void
2762 zyd_stop(struct zyd_softc *sc)
2763 {
2764 	int error;
2765 
2766 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2767 
2768 	sc->sc_flags &= ~ZYD_FLAG_RUNNING;
2769 	zyd_drain_mbufq(sc);
2770 
2771 	/*
2772 	 * Drain all the transfers, if not already drained:
2773 	 */
2774 	ZYD_UNLOCK(sc);
2775 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2776 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2777 	ZYD_LOCK(sc);
2778 
2779 	zyd_unsetup_tx_list(sc);
2780 
2781 	/* Stop now if the device was never set up */
2782 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2783 		return;
2784 
2785 	/* switch radio transmitter OFF */
2786 	error = zyd_switch_radio(sc, 0);
2787 	if (error != 0)
2788 		goto fail;
2789 	/* disable Rx */
2790 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2791 	/* disable interrupts */
2792 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2793 
2794 fail:
2795 	return;
2796 }
2797 
2798 static int
2799 zyd_loadfirmware(struct zyd_softc *sc)
2800 {
2801 	struct usb_device_request req;
2802 	size_t size;
2803 	u_char *fw;
2804 	uint8_t stat;
2805 	uint16_t addr;
2806 
2807 	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2808 		return (0);
2809 
2810 	if (sc->sc_macrev == ZYD_ZD1211) {
2811 		fw = (u_char *)zd1211_firmware;
2812 		size = sizeof(zd1211_firmware);
2813 	} else {
2814 		fw = (u_char *)zd1211b_firmware;
2815 		size = sizeof(zd1211b_firmware);
2816 	}
2817 
2818 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2819 	req.bRequest = ZYD_DOWNLOADREQ;
2820 	USETW(req.wIndex, 0);
2821 
2822 	addr = ZYD_FIRMWARE_START_ADDR;
2823 	while (size > 0) {
2824 		/*
2825 		 * When the transfer size is 4096 bytes, it is not
2826 		 * likely to be able to transfer it.
2827 		 * The cause is port or machine or chip?
2828 		 */
2829 		const int mlen = min(size, 64);
2830 
2831 		DPRINTF(sc, ZYD_DEBUG_FW,
2832 		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2833 
2834 		USETW(req.wValue, addr);
2835 		USETW(req.wLength, mlen);
2836 		if (zyd_do_request(sc, &req, fw) != 0)
2837 			return (EIO);
2838 
2839 		addr += mlen / 2;
2840 		fw   += mlen;
2841 		size -= mlen;
2842 	}
2843 
2844 	/* check whether the upload succeeded */
2845 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2846 	req.bRequest = ZYD_DOWNLOADSTS;
2847 	USETW(req.wValue, 0);
2848 	USETW(req.wIndex, 0);
2849 	USETW(req.wLength, sizeof(stat));
2850 	if (zyd_do_request(sc, &req, &stat) != 0)
2851 		return (EIO);
2852 
2853 	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2854 
2855 	return (stat & 0x80) ? (EIO) : (0);
2856 }
2857 
2858 static void
2859 zyd_scan_start(struct ieee80211com *ic)
2860 {
2861 	struct zyd_softc *sc = ic->ic_softc;
2862 
2863 	ZYD_LOCK(sc);
2864 	/* want broadcast address while scanning */
2865 	zyd_set_bssid(sc, ieee80211broadcastaddr);
2866 	ZYD_UNLOCK(sc);
2867 }
2868 
2869 static void
2870 zyd_scan_end(struct ieee80211com *ic)
2871 {
2872 	struct zyd_softc *sc = ic->ic_softc;
2873 
2874 	ZYD_LOCK(sc);
2875 	/* restore previous bssid */
2876 	zyd_set_bssid(sc, sc->sc_bssid);
2877 	ZYD_UNLOCK(sc);
2878 }
2879 
2880 static void
2881 zyd_getradiocaps(struct ieee80211com *ic,
2882     int maxchans, int *nchans, struct ieee80211_channel chans[])
2883 {
2884 	uint8_t bands[IEEE80211_MODE_BYTES];
2885 
2886 	memset(bands, 0, sizeof(bands));
2887 	setbit(bands, IEEE80211_MODE_11B);
2888 	setbit(bands, IEEE80211_MODE_11G);
2889 	ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
2890 }
2891 
2892 static void
2893 zyd_set_channel(struct ieee80211com *ic)
2894 {
2895 	struct zyd_softc *sc = ic->ic_softc;
2896 
2897 	ZYD_LOCK(sc);
2898 	zyd_set_chan(sc, ic->ic_curchan);
2899 	ZYD_UNLOCK(sc);
2900 }
2901 
2902 static device_method_t zyd_methods[] = {
2903         /* Device interface */
2904         DEVMETHOD(device_probe, zyd_match),
2905         DEVMETHOD(device_attach, zyd_attach),
2906         DEVMETHOD(device_detach, zyd_detach),
2907 	DEVMETHOD_END
2908 };
2909 
2910 static driver_t zyd_driver = {
2911 	.name = "zyd",
2912 	.methods = zyd_methods,
2913 	.size = sizeof(struct zyd_softc)
2914 };
2915 
2916 DRIVER_MODULE(zyd, uhub, zyd_driver, NULL, NULL);
2917 MODULE_DEPEND(zyd, usb, 1, 1, 1);
2918 MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2919 MODULE_VERSION(zyd, 1);
2920 USB_PNP_HOST_INFO(zyd_devs);
2921