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