xref: /freebsd/sys/dev/usb/wlan/if_zyd.c (revision 148a8da8)
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, 0, "USB zyd");
81 SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RWTUN, &zyd_debug, 0,
82     "zyd debug level");
83 
84 enum {
85 	ZYD_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
86 	ZYD_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
87 	ZYD_DEBUG_RESET		= 0x00000004,	/* reset processing */
88 	ZYD_DEBUG_INIT		= 0x00000008,	/* device init */
89 	ZYD_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
90 	ZYD_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
91 	ZYD_DEBUG_STATE		= 0x00000040,	/* 802.11 state transitions */
92 	ZYD_DEBUG_STAT		= 0x00000080,	/* statistic */
93 	ZYD_DEBUG_FW		= 0x00000100,	/* firmware */
94 	ZYD_DEBUG_CMD		= 0x00000200,	/* fw commands */
95 	ZYD_DEBUG_ANY		= 0xffffffff
96 };
97 #define	DPRINTF(sc, m, fmt, ...) do {				\
98 	if (zyd_debug & (m))					\
99 		printf("%s: " fmt, __func__, ## __VA_ARGS__);	\
100 } while (0)
101 #else
102 #define	DPRINTF(sc, m, fmt, ...) do {				\
103 	(void) sc;						\
104 } while (0)
105 #endif
106 
107 #define	zyd_do_request(sc,req,data) \
108     usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
109 
110 static device_probe_t zyd_match;
111 static device_attach_t zyd_attach;
112 static device_detach_t zyd_detach;
113 
114 static usb_callback_t zyd_intr_read_callback;
115 static usb_callback_t zyd_intr_write_callback;
116 static usb_callback_t zyd_bulk_read_callback;
117 static usb_callback_t zyd_bulk_write_callback;
118 
119 static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
120 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
121 		    const uint8_t [IEEE80211_ADDR_LEN],
122 		    const uint8_t [IEEE80211_ADDR_LEN]);
123 static void	zyd_vap_delete(struct ieee80211vap *);
124 static void	zyd_tx_free(struct zyd_tx_data *, int);
125 static void	zyd_setup_tx_list(struct zyd_softc *);
126 static void	zyd_unsetup_tx_list(struct zyd_softc *);
127 static int	zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
128 static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
129 		    void *, int, int);
130 static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
131 static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
132 static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
133 static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
134 static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
135 static int	zyd_lock_phy(struct zyd_softc *);
136 static int	zyd_unlock_phy(struct zyd_softc *);
137 static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
138 static const char *zyd_rf_name(uint8_t);
139 static int	zyd_hw_init(struct zyd_softc *);
140 static int	zyd_read_pod(struct zyd_softc *);
141 static int	zyd_read_eeprom(struct zyd_softc *);
142 static int	zyd_get_macaddr(struct zyd_softc *);
143 static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
144 static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
145 static int	zyd_switch_radio(struct zyd_softc *, int);
146 static int	zyd_set_led(struct zyd_softc *, int, int);
147 static void	zyd_set_multi(struct zyd_softc *);
148 static void	zyd_update_mcast(struct ieee80211com *);
149 static int	zyd_set_rxfilter(struct zyd_softc *);
150 static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
151 static int	zyd_set_beacon_interval(struct zyd_softc *, int);
152 static void	zyd_rx_data(struct usb_xfer *, int, uint16_t);
153 static int	zyd_tx_start(struct zyd_softc *, struct mbuf *,
154 		    struct ieee80211_node *);
155 static int	zyd_transmit(struct ieee80211com *, struct mbuf *);
156 static void	zyd_start(struct zyd_softc *);
157 static int	zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
158 		    const struct ieee80211_bpf_params *);
159 static void	zyd_parent(struct ieee80211com *);
160 static void	zyd_init_locked(struct zyd_softc *);
161 static void	zyd_stop(struct zyd_softc *);
162 static int	zyd_loadfirmware(struct zyd_softc *);
163 static void	zyd_scan_start(struct ieee80211com *);
164 static void	zyd_scan_end(struct ieee80211com *);
165 static void	zyd_getradiocaps(struct ieee80211com *, int, int *,
166 		    struct ieee80211_channel[]);
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;
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 	zyd_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
391 	    ic->ic_channels);
392 
393 	ieee80211_ifattach(ic);
394 	ic->ic_raw_xmit = zyd_raw_xmit;
395 	ic->ic_scan_start = zyd_scan_start;
396 	ic->ic_scan_end = zyd_scan_end;
397 	ic->ic_getradiocaps = zyd_getradiocaps;
398 	ic->ic_set_channel = zyd_set_channel;
399 	ic->ic_vap_create = zyd_vap_create;
400 	ic->ic_vap_delete = zyd_vap_delete;
401 	ic->ic_update_mcast = zyd_update_mcast;
402 	ic->ic_update_promisc = zyd_update_mcast;
403 	ic->ic_parent = zyd_parent;
404 	ic->ic_transmit = zyd_transmit;
405 
406 	ieee80211_radiotap_attach(ic,
407 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
408 		ZYD_TX_RADIOTAP_PRESENT,
409 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
410 		ZYD_RX_RADIOTAP_PRESENT);
411 
412 	if (bootverbose)
413 		ieee80211_announce(ic);
414 
415 	return (0);
416 
417 detach:
418 	zyd_detach(dev);
419 	return (ENXIO);			/* failure */
420 }
421 
422 static void
423 zyd_drain_mbufq(struct zyd_softc *sc)
424 {
425 	struct mbuf *m;
426 	struct ieee80211_node *ni;
427 
428 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
429 	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
430 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
431 		m->m_pkthdr.rcvif = NULL;
432 		ieee80211_free_node(ni);
433 		m_freem(m);
434 	}
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 int 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 
680 				ieee80211_ratectl_tx_complete(ni, txs);
681 				ieee80211_free_node(ni);
682 			}
683 			if (count & 0x100)
684 				/* too many retries */
685 				if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS,
686 				    1);
687 			break;
688 		}
689 		case ZYD_NOTIF_IORD:
690 		{
691 			struct zyd_rq *rqp;
692 
693 			if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
694 				break;	/* HMAC interrupt */
695 
696 			datalen = actlen - sizeof(cmd->code);
697 			datalen -= 2;	/* XXX: padding? */
698 
699 			STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
700 				int i;
701 				int count;
702 
703 				if (rqp->olen != datalen)
704 					continue;
705 				count = rqp->olen / sizeof(struct zyd_pair);
706 				for (i = 0; i < count; i++) {
707 					if (*(((const uint16_t *)rqp->idata) + i) !=
708 					    (((struct zyd_pair *)cmd->data) + i)->reg)
709 						break;
710 				}
711 				if (i != count)
712 					continue;
713 				/* copy answer into caller-supplied buffer */
714 				memcpy(rqp->odata, cmd->data, rqp->olen);
715 				DPRINTF(sc, ZYD_DEBUG_CMD,
716 				    "command %p complete, data = %*D \n",
717 				    rqp, rqp->olen, (char *)rqp->odata, ":");
718 				wakeup(rqp);	/* wakeup caller */
719 				break;
720 			}
721 			if (rqp == NULL) {
722 				device_printf(sc->sc_dev,
723 				    "unexpected IORD notification %*D\n",
724 				    datalen, cmd->data, ":");
725 			}
726 			break;
727 		}
728 		default:
729 			device_printf(sc->sc_dev, "unknown notification %x\n",
730 			    le16toh(cmd->code));
731 		}
732 
733 		/* FALLTHROUGH */
734 	case USB_ST_SETUP:
735 tr_setup:
736 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
737 		usbd_transfer_submit(xfer);
738 		break;
739 
740 	default:			/* Error */
741 		DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
742 		    usbd_errstr(error));
743 
744 		if (error != USB_ERR_CANCELLED) {
745 			/* try to clear stall first */
746 			usbd_xfer_set_stall(xfer);
747 			goto tr_setup;
748 		}
749 		break;
750 	}
751 }
752 
753 static void
754 zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
755 {
756 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
757 	struct zyd_rq *rqp, *cmd;
758 	struct usb_page_cache *pc;
759 
760 	switch (USB_GET_STATE(xfer)) {
761 	case USB_ST_TRANSFERRED:
762 		cmd = usbd_xfer_get_priv(xfer);
763 		DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
764 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
765 			/* Ensure the cached rq pointer is still valid */
766 			if (rqp == cmd &&
767 			    (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
768 				wakeup(rqp);	/* wakeup caller */
769 		}
770 
771 		/* FALLTHROUGH */
772 	case USB_ST_SETUP:
773 tr_setup:
774 		STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
775 			if (rqp->flags & ZYD_CMD_FLAG_SENT)
776 				continue;
777 
778 			pc = usbd_xfer_get_frame(xfer, 0);
779 			usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
780 
781 			usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
782 			usbd_xfer_set_priv(xfer, rqp);
783 			rqp->flags |= ZYD_CMD_FLAG_SENT;
784 			usbd_transfer_submit(xfer);
785 			break;
786 		}
787 		break;
788 
789 	default:			/* Error */
790 		DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
791 		    usbd_errstr(error));
792 
793 		if (error != USB_ERR_CANCELLED) {
794 			/* try to clear stall first */
795 			usbd_xfer_set_stall(xfer);
796 			goto tr_setup;
797 		}
798 		break;
799 	}
800 }
801 
802 static int
803 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
804     void *odata, int olen, int flags)
805 {
806 	struct zyd_cmd cmd;
807 	struct zyd_rq rq;
808 	int error;
809 
810 	if (ilen > (int)sizeof(cmd.data))
811 		return (EINVAL);
812 
813 	cmd.code = htole16(code);
814 	memcpy(cmd.data, idata, ilen);
815 	DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
816 	    &rq, ilen, idata, ":");
817 
818 	rq.cmd = &cmd;
819 	rq.idata = idata;
820 	rq.odata = odata;
821 	rq.ilen = sizeof(uint16_t) + ilen;
822 	rq.olen = olen;
823 	rq.flags = flags;
824 	STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
825 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
826 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
827 
828 	/* wait at most one second for command reply */
829 	error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
830 	if (error)
831 		device_printf(sc->sc_dev, "command timeout\n");
832 	STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
833 	DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
834 	    &rq, error);
835 
836 	return (error);
837 }
838 
839 static int
840 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
841 {
842 	struct zyd_pair tmp;
843 	int error;
844 
845 	reg = htole16(reg);
846 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
847 	    ZYD_CMD_FLAG_READ);
848 	if (error == 0)
849 		*val = le16toh(tmp.val);
850 	return (error);
851 }
852 
853 static int
854 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
855 {
856 	struct zyd_pair tmp[2];
857 	uint16_t regs[2];
858 	int error;
859 
860 	regs[0] = htole16(ZYD_REG32_HI(reg));
861 	regs[1] = htole16(ZYD_REG32_LO(reg));
862 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
863 	    ZYD_CMD_FLAG_READ);
864 	if (error == 0)
865 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
866 	return (error);
867 }
868 
869 static int
870 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
871 {
872 	struct zyd_pair pair;
873 
874 	pair.reg = htole16(reg);
875 	pair.val = htole16(val);
876 
877 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
878 }
879 
880 static int
881 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
882 {
883 	struct zyd_pair pair[2];
884 
885 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
886 	pair[0].val = htole16(val >> 16);
887 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
888 	pair[1].val = htole16(val & 0xffff);
889 
890 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
891 }
892 
893 static int
894 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
895 {
896 	struct zyd_rf *rf = &sc->sc_rf;
897 	struct zyd_rfwrite_cmd req;
898 	uint16_t cr203;
899 	int error, i;
900 
901 	zyd_read16_m(sc, ZYD_CR203, &cr203);
902 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
903 
904 	req.code  = htole16(2);
905 	req.width = htole16(rf->width);
906 	for (i = 0; i < rf->width; i++) {
907 		req.bit[i] = htole16(cr203);
908 		if (val & (1 << (rf->width - 1 - i)))
909 			req.bit[i] |= htole16(ZYD_RF_DATA);
910 	}
911 	error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
912 fail:
913 	return (error);
914 }
915 
916 static int
917 zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
918 {
919 	int error;
920 
921 	zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
922 	zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
923 	zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
924 fail:
925 	return (error);
926 }
927 
928 static int
929 zyd_lock_phy(struct zyd_softc *sc)
930 {
931 	int error;
932 	uint32_t tmp;
933 
934 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
935 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
936 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
937 fail:
938 	return (error);
939 }
940 
941 static int
942 zyd_unlock_phy(struct zyd_softc *sc)
943 {
944 	int error;
945 	uint32_t tmp;
946 
947 	zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
948 	tmp |= ZYD_UNLOCK_PHY_REGS;
949 	zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
950 fail:
951 	return (error);
952 }
953 
954 /*
955  * RFMD RF methods.
956  */
957 static int
958 zyd_rfmd_init(struct zyd_rf *rf)
959 {
960 	struct zyd_softc *sc = rf->rf_sc;
961 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
962 	static const uint32_t rfini[] = ZYD_RFMD_RF;
963 	int i, error;
964 
965 	/* init RF-dependent PHY registers */
966 	for (i = 0; i < nitems(phyini); i++) {
967 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
968 	}
969 
970 	/* init RFMD radio */
971 	for (i = 0; i < nitems(rfini); i++) {
972 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
973 			return (error);
974 	}
975 fail:
976 	return (error);
977 }
978 
979 static int
980 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
981 {
982 	int error;
983 	struct zyd_softc *sc = rf->rf_sc;
984 
985 	zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
986 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
987 fail:
988 	return (error);
989 }
990 
991 static int
992 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
993 {
994 	int error;
995 	struct zyd_softc *sc = rf->rf_sc;
996 	static const struct {
997 		uint32_t	r1, r2;
998 	} rfprog[] = ZYD_RFMD_CHANTABLE;
999 
1000 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1001 	if (error != 0)
1002 		goto fail;
1003 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1004 	if (error != 0)
1005 		goto fail;
1006 
1007 fail:
1008 	return (error);
1009 }
1010 
1011 /*
1012  * AL2230 RF methods.
1013  */
1014 static int
1015 zyd_al2230_init(struct zyd_rf *rf)
1016 {
1017 	struct zyd_softc *sc = rf->rf_sc;
1018 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1019 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1020 	static const struct zyd_phy_pair phypll[] = {
1021 		{ ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1022 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1023 	};
1024 	static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1025 	static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1026 	static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1027 	int i, error;
1028 
1029 	/* init RF-dependent PHY registers */
1030 	for (i = 0; i < nitems(phyini); i++)
1031 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1032 
1033 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1034 		for (i = 0; i < nitems(phy2230s); i++)
1035 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1036 	}
1037 
1038 	/* init AL2230 radio */
1039 	for (i = 0; i < nitems(rfini1); i++) {
1040 		error = zyd_rfwrite(sc, rfini1[i]);
1041 		if (error != 0)
1042 			goto fail;
1043 	}
1044 
1045 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1046 		error = zyd_rfwrite(sc, 0x000824);
1047 	else
1048 		error = zyd_rfwrite(sc, 0x0005a4);
1049 	if (error != 0)
1050 		goto fail;
1051 
1052 	for (i = 0; i < nitems(rfini2); i++) {
1053 		error = zyd_rfwrite(sc, rfini2[i]);
1054 		if (error != 0)
1055 			goto fail;
1056 	}
1057 
1058 	for (i = 0; i < nitems(phypll); i++)
1059 		zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1060 
1061 	for (i = 0; i < nitems(rfini3); i++) {
1062 		error = zyd_rfwrite(sc, rfini3[i]);
1063 		if (error != 0)
1064 			goto fail;
1065 	}
1066 fail:
1067 	return (error);
1068 }
1069 
1070 static int
1071 zyd_al2230_fini(struct zyd_rf *rf)
1072 {
1073 	int error, i;
1074 	struct zyd_softc *sc = rf->rf_sc;
1075 	static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1076 
1077 	for (i = 0; i < nitems(phy); i++)
1078 		zyd_write16_m(sc, phy[i].reg, phy[i].val);
1079 
1080 	if (sc->sc_newphy != 0)
1081 		zyd_write16_m(sc, ZYD_CR9, 0xe1);
1082 
1083 	zyd_write16_m(sc, ZYD_CR203, 0x6);
1084 fail:
1085 	return (error);
1086 }
1087 
1088 static int
1089 zyd_al2230_init_b(struct zyd_rf *rf)
1090 {
1091 	struct zyd_softc *sc = rf->rf_sc;
1092 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1093 	static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1094 	static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1095 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1096 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1097 	static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1098 	static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1099 	static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1100 	static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1101 	int i, error;
1102 
1103 	for (i = 0; i < nitems(phy1); i++)
1104 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1105 
1106 	/* init RF-dependent PHY registers */
1107 	for (i = 0; i < nitems(phyini); i++)
1108 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1109 
1110 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1111 		for (i = 0; i < nitems(phy2230s); i++)
1112 			zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1113 	}
1114 
1115 	for (i = 0; i < 3; i++) {
1116 		error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1117 		if (error != 0)
1118 			return (error);
1119 	}
1120 
1121 	for (i = 0; i < nitems(rfini_part1); i++) {
1122 		error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1123 		if (error != 0)
1124 			return (error);
1125 	}
1126 
1127 	if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1128 		error = zyd_rfwrite(sc, 0x241000);
1129 	else
1130 		error = zyd_rfwrite(sc, 0x25a000);
1131 	if (error != 0)
1132 		goto fail;
1133 
1134 	for (i = 0; i < nitems(rfini_part2); i++) {
1135 		error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1136 		if (error != 0)
1137 			return (error);
1138 	}
1139 
1140 	for (i = 0; i < nitems(phy2); i++)
1141 		zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1142 
1143 	for (i = 0; i < nitems(rfini_part3); i++) {
1144 		error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1145 		if (error != 0)
1146 			return (error);
1147 	}
1148 
1149 	for (i = 0; i < nitems(phy3); i++)
1150 		zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1151 
1152 	error = zyd_al2230_fini(rf);
1153 fail:
1154 	return (error);
1155 }
1156 
1157 static int
1158 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1159 {
1160 	struct zyd_softc *sc = rf->rf_sc;
1161 	int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1162 
1163 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1164 	zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1165 fail:
1166 	return (error);
1167 }
1168 
1169 static int
1170 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1171 {
1172 	int error, i;
1173 	struct zyd_softc *sc = rf->rf_sc;
1174 	static const struct zyd_phy_pair phy1[] = {
1175 		{ ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1176 	};
1177 	static const struct {
1178 		uint32_t	r1, r2, r3;
1179 	} rfprog[] = ZYD_AL2230_CHANTABLE;
1180 
1181 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1182 	if (error != 0)
1183 		goto fail;
1184 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1185 	if (error != 0)
1186 		goto fail;
1187 	error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1188 	if (error != 0)
1189 		goto fail;
1190 
1191 	for (i = 0; i < nitems(phy1); i++)
1192 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1193 fail:
1194 	return (error);
1195 }
1196 
1197 static int
1198 zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1199 {
1200 	int error, i;
1201 	struct zyd_softc *sc = rf->rf_sc;
1202 	static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1203 	static const struct {
1204 		uint32_t	r1, r2, r3;
1205 	} rfprog[] = ZYD_AL2230_CHANTABLE_B;
1206 
1207 	for (i = 0; i < nitems(phy1); i++)
1208 		zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1209 
1210 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1211 	if (error != 0)
1212 		goto fail;
1213 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1214 	if (error != 0)
1215 		goto fail;
1216 	error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1217 	if (error != 0)
1218 		goto fail;
1219 	error = zyd_al2230_fini(rf);
1220 fail:
1221 	return (error);
1222 }
1223 
1224 #define	ZYD_AL2230_PHY_BANDEDGE6					\
1225 {									\
1226 	{ ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },	\
1227 	{ ZYD_CR47,  0x1e }						\
1228 }
1229 
1230 static int
1231 zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1232 {
1233 	int error = 0, i;
1234 	struct zyd_softc *sc = rf->rf_sc;
1235 	struct ieee80211com *ic = &sc->sc_ic;
1236 	struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1237 	int chan = ieee80211_chan2ieee(ic, c);
1238 
1239 	if (chan == 1 || chan == 11)
1240 		r[0].val = 0x12;
1241 
1242 	for (i = 0; i < nitems(r); i++)
1243 		zyd_write16_m(sc, r[i].reg, r[i].val);
1244 fail:
1245 	return (error);
1246 }
1247 
1248 /*
1249  * AL7230B RF methods.
1250  */
1251 static int
1252 zyd_al7230B_init(struct zyd_rf *rf)
1253 {
1254 	struct zyd_softc *sc = rf->rf_sc;
1255 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1256 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1257 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1258 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1259 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1260 	int i, error;
1261 
1262 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1263 
1264 	/* init RF-dependent PHY registers, part one */
1265 	for (i = 0; i < nitems(phyini_1); i++)
1266 		zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1267 
1268 	/* init AL7230B radio, part one */
1269 	for (i = 0; i < nitems(rfini_1); i++) {
1270 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1271 			return (error);
1272 	}
1273 	/* init RF-dependent PHY registers, part two */
1274 	for (i = 0; i < nitems(phyini_2); i++)
1275 		zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1276 
1277 	/* init AL7230B radio, part two */
1278 	for (i = 0; i < nitems(rfini_2); i++) {
1279 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1280 			return (error);
1281 	}
1282 	/* init RF-dependent PHY registers, part three */
1283 	for (i = 0; i < nitems(phyini_3); i++)
1284 		zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1285 fail:
1286 	return (error);
1287 }
1288 
1289 static int
1290 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1291 {
1292 	int error;
1293 	struct zyd_softc *sc = rf->rf_sc;
1294 
1295 	zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1296 	zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1297 fail:
1298 	return (error);
1299 }
1300 
1301 static int
1302 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1303 {
1304 	struct zyd_softc *sc = rf->rf_sc;
1305 	static const struct {
1306 		uint32_t	r1, r2;
1307 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1308 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1309 	int i, error;
1310 
1311 	zyd_write16_m(sc, ZYD_CR240, 0x57);
1312 	zyd_write16_m(sc, ZYD_CR251, 0x2f);
1313 
1314 	for (i = 0; i < nitems(rfsc); i++) {
1315 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1316 			return (error);
1317 	}
1318 
1319 	zyd_write16_m(sc, ZYD_CR128, 0x14);
1320 	zyd_write16_m(sc, ZYD_CR129, 0x12);
1321 	zyd_write16_m(sc, ZYD_CR130, 0x10);
1322 	zyd_write16_m(sc, ZYD_CR38,  0x38);
1323 	zyd_write16_m(sc, ZYD_CR136, 0xdf);
1324 
1325 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1326 	if (error != 0)
1327 		goto fail;
1328 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1329 	if (error != 0)
1330 		goto fail;
1331 	error = zyd_rfwrite(sc, 0x3c9000);
1332 	if (error != 0)
1333 		goto fail;
1334 
1335 	zyd_write16_m(sc, ZYD_CR251, 0x3f);
1336 	zyd_write16_m(sc, ZYD_CR203, 0x06);
1337 	zyd_write16_m(sc, ZYD_CR240, 0x08);
1338 fail:
1339 	return (error);
1340 }
1341 
1342 /*
1343  * AL2210 RF methods.
1344  */
1345 static int
1346 zyd_al2210_init(struct zyd_rf *rf)
1347 {
1348 	struct zyd_softc *sc = rf->rf_sc;
1349 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1350 	static const uint32_t rfini[] = ZYD_AL2210_RF;
1351 	uint32_t tmp;
1352 	int i, error;
1353 
1354 	zyd_write32_m(sc, ZYD_CR18, 2);
1355 
1356 	/* init RF-dependent PHY registers */
1357 	for (i = 0; i < nitems(phyini); i++)
1358 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1359 
1360 	/* init AL2210 radio */
1361 	for (i = 0; i < nitems(rfini); i++) {
1362 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1363 			return (error);
1364 	}
1365 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1366 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1367 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1368 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1369 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1370 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1371 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1372 	zyd_write32_m(sc, ZYD_CR18, 3);
1373 fail:
1374 	return (error);
1375 }
1376 
1377 static int
1378 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1379 {
1380 	/* vendor driver does nothing for this RF chip */
1381 
1382 	return (0);
1383 }
1384 
1385 static int
1386 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1387 {
1388 	int error;
1389 	struct zyd_softc *sc = rf->rf_sc;
1390 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1391 	uint32_t tmp;
1392 
1393 	zyd_write32_m(sc, ZYD_CR18, 2);
1394 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1395 	zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1396 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1397 	zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1398 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1399 	zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1400 	zyd_write16_m(sc, ZYD_CR47, 0x1e);
1401 
1402 	/* actually set the channel */
1403 	error = zyd_rfwrite(sc, rfprog[chan - 1]);
1404 	if (error != 0)
1405 		goto fail;
1406 
1407 	zyd_write32_m(sc, ZYD_CR18, 3);
1408 fail:
1409 	return (error);
1410 }
1411 
1412 /*
1413  * GCT RF methods.
1414  */
1415 static int
1416 zyd_gct_init(struct zyd_rf *rf)
1417 {
1418 #define	ZYD_GCT_INTR_REG	0x85c1
1419 	struct zyd_softc *sc = rf->rf_sc;
1420 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1421 	static const uint32_t rfini[] = ZYD_GCT_RF;
1422 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1423 	int i, idx = -1, error;
1424 	uint16_t data;
1425 
1426 	/* init RF-dependent PHY registers */
1427 	for (i = 0; i < nitems(phyini); i++)
1428 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1429 
1430 	/* init cgt radio */
1431 	for (i = 0; i < nitems(rfini); i++) {
1432 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1433 			return (error);
1434 	}
1435 
1436 	error = zyd_gct_mode(rf);
1437 	if (error != 0)
1438 		return (error);
1439 
1440 	for (i = 0; i < (int)(nitems(vco) - 1); i++) {
1441 		error = zyd_gct_set_channel_synth(rf, 1, 0);
1442 		if (error != 0)
1443 			goto fail;
1444 		error = zyd_gct_write(rf, vco[i][0]);
1445 		if (error != 0)
1446 			goto fail;
1447 		zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1448 		zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1449 		if ((data & 0xf) == 0) {
1450 			idx = i;
1451 			break;
1452 		}
1453 	}
1454 	if (idx == -1) {
1455 		error = zyd_gct_set_channel_synth(rf, 1, 1);
1456 		if (error != 0)
1457 			goto fail;
1458 		error = zyd_gct_write(rf, 0x6662);
1459 		if (error != 0)
1460 			goto fail;
1461 	}
1462 
1463 	rf->idx = idx;
1464 	zyd_write16_m(sc, ZYD_CR203, 0x6);
1465 fail:
1466 	return (error);
1467 #undef ZYD_GCT_INTR_REG
1468 }
1469 
1470 static int
1471 zyd_gct_mode(struct zyd_rf *rf)
1472 {
1473 	struct zyd_softc *sc = rf->rf_sc;
1474 	static const uint32_t mode[] = {
1475 		0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1476 	};
1477 	int i, error;
1478 
1479 	for (i = 0; i < nitems(mode); i++) {
1480 		if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1481 			break;
1482 	}
1483 	return (error);
1484 }
1485 
1486 static int
1487 zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1488 {
1489 	int error, idx = chan - 1;
1490 	struct zyd_softc *sc = rf->rf_sc;
1491 	static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1492 	static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1493 	static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1494 
1495 	error = zyd_rfwrite(sc,
1496 	    (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1497 	if (error != 0)
1498 		return (error);
1499 	return zyd_rfwrite(sc, div_synth[idx]);
1500 }
1501 
1502 static int
1503 zyd_gct_write(struct zyd_rf *rf, uint16_t value)
1504 {
1505 	struct zyd_softc *sc = rf->rf_sc;
1506 
1507 	return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1508 }
1509 
1510 static int
1511 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1512 {
1513 	int error;
1514 	struct zyd_softc *sc = rf->rf_sc;
1515 
1516 	error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1517 	if (error != 0)
1518 		return (error);
1519 
1520 	zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1521 	zyd_write16_m(sc, ZYD_CR251,
1522 	    on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1523 fail:
1524 	return (error);
1525 }
1526 
1527 static int
1528 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1529 {
1530 	int error, i;
1531 	struct zyd_softc *sc = rf->rf_sc;
1532 	static const struct zyd_phy_pair cmd[] = {
1533 		{ ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1534 		{ ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1535 	};
1536 	static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1537 
1538 	error = zyd_gct_set_channel_synth(rf, chan, 0);
1539 	if (error != 0)
1540 		goto fail;
1541 	error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1542 	    vco[rf->idx][((chan - 1) / 2)]);
1543 	if (error != 0)
1544 		goto fail;
1545 	error = zyd_gct_mode(rf);
1546 	if (error != 0)
1547 		return (error);
1548 	for (i = 0; i < nitems(cmd); i++)
1549 		zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1550 	error = zyd_gct_txgain(rf, chan);
1551 	if (error != 0)
1552 		return (error);
1553 	zyd_write16_m(sc, ZYD_CR203, 0x6);
1554 fail:
1555 	return (error);
1556 }
1557 
1558 static int
1559 zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1560 {
1561 	struct zyd_softc *sc = rf->rf_sc;
1562 	static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1563 	uint8_t idx = sc->sc_pwrint[chan - 1];
1564 
1565 	if (idx >= nitems(txgain)) {
1566 		device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1567 		    chan, idx);
1568 		return 0;
1569 	}
1570 
1571 	return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1572 }
1573 
1574 /*
1575  * Maxim2 RF methods.
1576  */
1577 static int
1578 zyd_maxim2_init(struct zyd_rf *rf)
1579 {
1580 	struct zyd_softc *sc = rf->rf_sc;
1581 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1582 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1583 	uint16_t tmp;
1584 	int i, error;
1585 
1586 	/* init RF-dependent PHY registers */
1587 	for (i = 0; i < nitems(phyini); i++)
1588 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1589 
1590 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1591 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1592 
1593 	/* init maxim2 radio */
1594 	for (i = 0; i < nitems(rfini); i++) {
1595 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1596 			return (error);
1597 	}
1598 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1599 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1600 fail:
1601 	return (error);
1602 }
1603 
1604 static int
1605 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1606 {
1607 
1608 	/* vendor driver does nothing for this RF chip */
1609 	return (0);
1610 }
1611 
1612 static int
1613 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1614 {
1615 	struct zyd_softc *sc = rf->rf_sc;
1616 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1617 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1618 	static const struct {
1619 		uint32_t	r1, r2;
1620 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1621 	uint16_t tmp;
1622 	int i, error;
1623 
1624 	/*
1625 	 * Do the same as we do when initializing it, except for the channel
1626 	 * values coming from the two channel tables.
1627 	 */
1628 
1629 	/* init RF-dependent PHY registers */
1630 	for (i = 0; i < nitems(phyini); i++)
1631 		zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1632 
1633 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1634 	zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1635 
1636 	/* first two values taken from the chantables */
1637 	error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1638 	if (error != 0)
1639 		goto fail;
1640 	error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1641 	if (error != 0)
1642 		goto fail;
1643 
1644 	/* init maxim2 radio - skipping the two first values */
1645 	for (i = 2; i < nitems(rfini); i++) {
1646 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1647 			return (error);
1648 	}
1649 	zyd_read16_m(sc, ZYD_CR203, &tmp);
1650 	zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1651 fail:
1652 	return (error);
1653 }
1654 
1655 static int
1656 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1657 {
1658 	struct zyd_rf *rf = &sc->sc_rf;
1659 
1660 	rf->rf_sc = sc;
1661 	rf->update_pwr = 1;
1662 
1663 	switch (type) {
1664 	case ZYD_RF_RFMD:
1665 		rf->init         = zyd_rfmd_init;
1666 		rf->switch_radio = zyd_rfmd_switch_radio;
1667 		rf->set_channel  = zyd_rfmd_set_channel;
1668 		rf->width        = 24;	/* 24-bit RF values */
1669 		break;
1670 	case ZYD_RF_AL2230:
1671 	case ZYD_RF_AL2230S:
1672 		if (sc->sc_macrev == ZYD_ZD1211B) {
1673 			rf->init = zyd_al2230_init_b;
1674 			rf->set_channel = zyd_al2230_set_channel_b;
1675 		} else {
1676 			rf->init = zyd_al2230_init;
1677 			rf->set_channel = zyd_al2230_set_channel;
1678 		}
1679 		rf->switch_radio = zyd_al2230_switch_radio;
1680 		rf->bandedge6	 = zyd_al2230_bandedge6;
1681 		rf->width        = 24;	/* 24-bit RF values */
1682 		break;
1683 	case ZYD_RF_AL7230B:
1684 		rf->init         = zyd_al7230B_init;
1685 		rf->switch_radio = zyd_al7230B_switch_radio;
1686 		rf->set_channel  = zyd_al7230B_set_channel;
1687 		rf->width        = 24;	/* 24-bit RF values */
1688 		break;
1689 	case ZYD_RF_AL2210:
1690 		rf->init         = zyd_al2210_init;
1691 		rf->switch_radio = zyd_al2210_switch_radio;
1692 		rf->set_channel  = zyd_al2210_set_channel;
1693 		rf->width        = 24;	/* 24-bit RF values */
1694 		break;
1695 	case ZYD_RF_MAXIM_NEW:
1696 	case ZYD_RF_GCT:
1697 		rf->init         = zyd_gct_init;
1698 		rf->switch_radio = zyd_gct_switch_radio;
1699 		rf->set_channel  = zyd_gct_set_channel;
1700 		rf->width        = 24;	/* 24-bit RF values */
1701 		rf->update_pwr   = 0;
1702 		break;
1703 	case ZYD_RF_MAXIM_NEW2:
1704 		rf->init         = zyd_maxim2_init;
1705 		rf->switch_radio = zyd_maxim2_switch_radio;
1706 		rf->set_channel  = zyd_maxim2_set_channel;
1707 		rf->width        = 18;	/* 18-bit RF values */
1708 		break;
1709 	default:
1710 		device_printf(sc->sc_dev,
1711 		    "sorry, radio \"%s\" is not supported yet\n",
1712 		    zyd_rf_name(type));
1713 		return (EINVAL);
1714 	}
1715 	return (0);
1716 }
1717 
1718 static const char *
1719 zyd_rf_name(uint8_t type)
1720 {
1721 	static const char * const zyd_rfs[] = {
1722 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1723 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1724 		"AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1725 		"PHILIPS"
1726 	};
1727 
1728 	return zyd_rfs[(type > 15) ? 0 : type];
1729 }
1730 
1731 static int
1732 zyd_hw_init(struct zyd_softc *sc)
1733 {
1734 	int error;
1735 	const struct zyd_phy_pair *phyp;
1736 	struct zyd_rf *rf = &sc->sc_rf;
1737 	uint16_t val;
1738 
1739 	/* specify that the plug and play is finished */
1740 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1741 	zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1742 	DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1743 	    sc->sc_fwbase);
1744 
1745 	/* retrieve firmware revision number */
1746 	zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1747 	zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1748 	zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1749 	/* set mandatory rates - XXX assumes 802.11b/g */
1750 	zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1751 
1752 	/* disable interrupts */
1753 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1754 
1755 	if ((error = zyd_read_pod(sc)) != 0) {
1756 		device_printf(sc->sc_dev, "could not read EEPROM\n");
1757 		goto fail;
1758 	}
1759 
1760 	/* PHY init (resetting) */
1761 	error = zyd_lock_phy(sc);
1762 	if (error != 0)
1763 		goto fail;
1764 	phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1765 	for (; phyp->reg != 0; phyp++)
1766 		zyd_write16_m(sc, phyp->reg, phyp->val);
1767 	if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1768 		zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1769 		zyd_write32_m(sc, ZYD_CR157, val >> 8);
1770 	}
1771 	error = zyd_unlock_phy(sc);
1772 	if (error != 0)
1773 		goto fail;
1774 
1775 	/* HMAC init */
1776 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1777 	zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1778 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1779 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1780 	zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1781 	zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1782 	zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1783 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1784 	zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1785 	zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1786 	zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1787 	zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1788 	zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1789 	zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1790 	zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1791 	zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1792 	zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1793 	zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1794 	zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1795 	zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1796 
1797 	if (sc->sc_macrev == ZYD_ZD1211) {
1798 		zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1799 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1800 	} else {
1801 		zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1802 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1803 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1804 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1805 		zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1806 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1807 		zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1808 		zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1809 		zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1810 	}
1811 
1812 	/* init beacon interval to 100ms */
1813 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1814 		goto fail;
1815 
1816 	if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1817 		device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1818 		    sc->sc_rfrev);
1819 		goto fail;
1820 	}
1821 
1822 	/* RF chip init */
1823 	error = zyd_lock_phy(sc);
1824 	if (error != 0)
1825 		goto fail;
1826 	error = (*rf->init)(rf);
1827 	if (error != 0) {
1828 		device_printf(sc->sc_dev,
1829 		    "radio initialization failed, error %d\n", error);
1830 		goto fail;
1831 	}
1832 	error = zyd_unlock_phy(sc);
1833 	if (error != 0)
1834 		goto fail;
1835 
1836 	if ((error = zyd_read_eeprom(sc)) != 0) {
1837 		device_printf(sc->sc_dev, "could not read EEPROM\n");
1838 		goto fail;
1839 	}
1840 
1841 fail:	return (error);
1842 }
1843 
1844 static int
1845 zyd_read_pod(struct zyd_softc *sc)
1846 {
1847 	int error;
1848 	uint32_t tmp;
1849 
1850 	zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1851 	sc->sc_rfrev     = tmp & 0x0f;
1852 	sc->sc_ledtype   = (tmp >>  4) & 0x01;
1853 	sc->sc_al2230s   = (tmp >>  7) & 0x01;
1854 	sc->sc_cckgain   = (tmp >>  8) & 0x01;
1855 	sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1856 	sc->sc_parev     = (tmp >> 16) & 0x0f;
1857 	sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1858 	sc->sc_newphy    = (tmp >> 31) & 0x01;
1859 	sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1860 fail:
1861 	return (error);
1862 }
1863 
1864 static int
1865 zyd_read_eeprom(struct zyd_softc *sc)
1866 {
1867 	uint16_t val;
1868 	int error, i;
1869 
1870 	/* read Tx power calibration tables */
1871 	for (i = 0; i < 7; i++) {
1872 		zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1873 		sc->sc_pwrcal[i * 2] = val >> 8;
1874 		sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1875 		zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1876 		sc->sc_pwrint[i * 2] = val >> 8;
1877 		sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1878 		zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1879 		sc->sc_ofdm36_cal[i * 2] = val >> 8;
1880 		sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1881 		zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1882 		sc->sc_ofdm48_cal[i * 2] = val >> 8;
1883 		sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1884 		zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1885 		sc->sc_ofdm54_cal[i * 2] = val >> 8;
1886 		sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1887 	}
1888 fail:
1889 	return (error);
1890 }
1891 
1892 static int
1893 zyd_get_macaddr(struct zyd_softc *sc)
1894 {
1895 	struct usb_device_request req;
1896 	usb_error_t error;
1897 
1898 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1899 	req.bRequest = ZYD_READFWDATAREQ;
1900 	USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1901 	USETW(req.wIndex, 0);
1902 	USETW(req.wLength, IEEE80211_ADDR_LEN);
1903 
1904 	error = zyd_do_request(sc, &req, sc->sc_ic.ic_macaddr);
1905 	if (error != 0) {
1906 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1907 		    usbd_errstr(error));
1908 	}
1909 
1910 	return (error);
1911 }
1912 
1913 static int
1914 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1915 {
1916 	int error;
1917 	uint32_t tmp;
1918 
1919 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1920 	zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1921 	tmp = addr[5] << 8 | addr[4];
1922 	zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1923 fail:
1924 	return (error);
1925 }
1926 
1927 static int
1928 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1929 {
1930 	int error;
1931 	uint32_t tmp;
1932 
1933 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1934 	zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1935 	tmp = addr[5] << 8 | addr[4];
1936 	zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1937 fail:
1938 	return (error);
1939 }
1940 
1941 static int
1942 zyd_switch_radio(struct zyd_softc *sc, int on)
1943 {
1944 	struct zyd_rf *rf = &sc->sc_rf;
1945 	int error;
1946 
1947 	error = zyd_lock_phy(sc);
1948 	if (error != 0)
1949 		goto fail;
1950 	error = (*rf->switch_radio)(rf, on);
1951 	if (error != 0)
1952 		goto fail;
1953 	error = zyd_unlock_phy(sc);
1954 fail:
1955 	return (error);
1956 }
1957 
1958 static int
1959 zyd_set_led(struct zyd_softc *sc, int which, int on)
1960 {
1961 	int error;
1962 	uint32_t tmp;
1963 
1964 	zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1965 	tmp &= ~which;
1966 	if (on)
1967 		tmp |= which;
1968 	zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1969 fail:
1970 	return (error);
1971 }
1972 
1973 static void
1974 zyd_set_multi(struct zyd_softc *sc)
1975 {
1976 	struct ieee80211com *ic = &sc->sc_ic;
1977 	uint32_t low, high;
1978 	int error;
1979 
1980 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0)
1981 		return;
1982 
1983 	low = 0x00000000;
1984 	high = 0x80000000;
1985 
1986 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
1987 	    ic->ic_promisc > 0) {
1988 		low = 0xffffffff;
1989 		high = 0xffffffff;
1990 	} else {
1991 		struct ieee80211vap *vap;
1992 		struct ifnet *ifp;
1993 		struct ifmultiaddr *ifma;
1994 		uint8_t v;
1995 
1996 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1997 			ifp = vap->iv_ifp;
1998 			if_maddr_rlock(ifp);
1999 			CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2000 				if (ifma->ifma_addr->sa_family != AF_LINK)
2001 					continue;
2002 				v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2003 				    ifma->ifma_addr))[5] >> 2;
2004 				if (v < 32)
2005 					low |= 1 << v;
2006 				else
2007 					high |= 1 << (v - 32);
2008 			}
2009 			if_maddr_runlock(ifp);
2010 		}
2011 	}
2012 
2013 	/* reprogram multicast global hash table */
2014 	zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2015 	zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2016 fail:
2017 	if (error != 0)
2018 		device_printf(sc->sc_dev,
2019 		    "could not set multicast hash table\n");
2020 }
2021 
2022 static void
2023 zyd_update_mcast(struct ieee80211com *ic)
2024 {
2025 	struct zyd_softc *sc = ic->ic_softc;
2026 
2027 	ZYD_LOCK(sc);
2028 	zyd_set_multi(sc);
2029 	ZYD_UNLOCK(sc);
2030 }
2031 
2032 static int
2033 zyd_set_rxfilter(struct zyd_softc *sc)
2034 {
2035 	struct ieee80211com *ic = &sc->sc_ic;
2036 	uint32_t rxfilter;
2037 
2038 	switch (ic->ic_opmode) {
2039 	case IEEE80211_M_STA:
2040 		rxfilter = ZYD_FILTER_BSS;
2041 		break;
2042 	case IEEE80211_M_IBSS:
2043 	case IEEE80211_M_HOSTAP:
2044 		rxfilter = ZYD_FILTER_HOSTAP;
2045 		break;
2046 	case IEEE80211_M_MONITOR:
2047 		rxfilter = ZYD_FILTER_MONITOR;
2048 		break;
2049 	default:
2050 		/* should not get there */
2051 		return (EINVAL);
2052 	}
2053 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2054 }
2055 
2056 static void
2057 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2058 {
2059 	int error;
2060 	struct ieee80211com *ic = &sc->sc_ic;
2061 	struct zyd_rf *rf = &sc->sc_rf;
2062 	uint32_t tmp;
2063 	int chan;
2064 
2065 	chan = ieee80211_chan2ieee(ic, c);
2066 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2067 		/* XXX should NEVER happen */
2068 		device_printf(sc->sc_dev,
2069 		    "%s: invalid channel %x\n", __func__, chan);
2070 		return;
2071 	}
2072 
2073 	error = zyd_lock_phy(sc);
2074 	if (error != 0)
2075 		goto fail;
2076 
2077 	error = (*rf->set_channel)(rf, chan);
2078 	if (error != 0)
2079 		goto fail;
2080 
2081 	if (rf->update_pwr) {
2082 		/* update Tx power */
2083 		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2084 
2085 		if (sc->sc_macrev == ZYD_ZD1211B) {
2086 			zyd_write16_m(sc, ZYD_CR67,
2087 			    sc->sc_ofdm36_cal[chan - 1]);
2088 			zyd_write16_m(sc, ZYD_CR66,
2089 			    sc->sc_ofdm48_cal[chan - 1]);
2090 			zyd_write16_m(sc, ZYD_CR65,
2091 			    sc->sc_ofdm54_cal[chan - 1]);
2092 			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2093 			zyd_write16_m(sc, ZYD_CR69, 0x28);
2094 			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2095 		}
2096 	}
2097 	if (sc->sc_cckgain) {
2098 		/* set CCK baseband gain from EEPROM */
2099 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2100 			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2101 	}
2102 	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2103 		error = (*rf->bandedge6)(rf, c);
2104 		if (error != 0)
2105 			goto fail;
2106 	}
2107 	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2108 
2109 	error = zyd_unlock_phy(sc);
2110 	if (error != 0)
2111 		goto fail;
2112 
2113 	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2114 	    htole16(c->ic_freq);
2115 	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2116 	    htole16(c->ic_flags);
2117 fail:
2118 	return;
2119 }
2120 
2121 static int
2122 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2123 {
2124 	int error;
2125 	uint32_t val;
2126 
2127 	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2128 	sc->sc_atim_wnd = val;
2129 	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2130 	sc->sc_pre_tbtt = val;
2131 	sc->sc_bcn_int = bintval;
2132 
2133 	if (sc->sc_bcn_int <= 5)
2134 		sc->sc_bcn_int = 5;
2135 	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2136 		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2137 	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2138 		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2139 
2140 	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2141 	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2142 	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2143 fail:
2144 	return (error);
2145 }
2146 
2147 static void
2148 zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2149 {
2150 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2151 	struct ieee80211com *ic = &sc->sc_ic;
2152 	struct zyd_plcphdr plcp;
2153 	struct zyd_rx_stat stat;
2154 	struct usb_page_cache *pc;
2155 	struct mbuf *m;
2156 	int rlen, rssi;
2157 
2158 	if (len < ZYD_MIN_FRAGSZ) {
2159 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2160 		    device_get_nameunit(sc->sc_dev), len);
2161 		counter_u64_add(ic->ic_ierrors, 1);
2162 		return;
2163 	}
2164 	pc = usbd_xfer_get_frame(xfer, 0);
2165 	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2166 	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2167 
2168 	if (stat.flags & ZYD_RX_ERROR) {
2169 		DPRINTF(sc, ZYD_DEBUG_RECV,
2170 		    "%s: RX status indicated error (%x)\n",
2171 		    device_get_nameunit(sc->sc_dev), stat.flags);
2172 		counter_u64_add(ic->ic_ierrors, 1);
2173 		return;
2174 	}
2175 
2176 	/* compute actual frame length */
2177 	rlen = len - sizeof(struct zyd_plcphdr) -
2178 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2179 
2180 	/* allocate a mbuf to store the frame */
2181 	if (rlen > (int)MCLBYTES) {
2182 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2183 		    device_get_nameunit(sc->sc_dev), rlen);
2184 		counter_u64_add(ic->ic_ierrors, 1);
2185 		return;
2186 	} else if (rlen > (int)MHLEN)
2187 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2188 	else
2189 		m = m_gethdr(M_NOWAIT, MT_DATA);
2190 	if (m == NULL) {
2191 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2192 		    device_get_nameunit(sc->sc_dev));
2193 		counter_u64_add(ic->ic_ierrors, 1);
2194 		return;
2195 	}
2196 	m->m_pkthdr.len = m->m_len = rlen;
2197 	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2198 
2199 	if (ieee80211_radiotap_active(ic)) {
2200 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2201 
2202 		tap->wr_flags = 0;
2203 		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2204 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2205 		/* XXX toss, no way to express errors */
2206 		if (stat.flags & ZYD_RX_DECRYPTERR)
2207 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2208 		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2209 		    (stat.flags & ZYD_RX_OFDM) ?
2210 			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2211 		tap->wr_antsignal = stat.rssi + -95;
2212 		tap->wr_antnoise = -95;	/* XXX */
2213 	}
2214 	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2215 
2216 	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2217 	sc->sc_rx_data[sc->sc_rx_count].m = m;
2218 	sc->sc_rx_count++;
2219 }
2220 
2221 static void
2222 zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2223 {
2224 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2225 	struct ieee80211com *ic = &sc->sc_ic;
2226 	struct ieee80211_node *ni;
2227 	struct zyd_rx_desc desc;
2228 	struct mbuf *m;
2229 	struct usb_page_cache *pc;
2230 	uint32_t offset;
2231 	uint8_t rssi;
2232 	int8_t nf;
2233 	int i;
2234 	int actlen;
2235 
2236 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2237 
2238 	sc->sc_rx_count = 0;
2239 	switch (USB_GET_STATE(xfer)) {
2240 	case USB_ST_TRANSFERRED:
2241 		pc = usbd_xfer_get_frame(xfer, 0);
2242 		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2243 
2244 		offset = 0;
2245 		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2246 			DPRINTF(sc, ZYD_DEBUG_RECV,
2247 			    "%s: received multi-frame transfer\n", __func__);
2248 
2249 			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2250 				uint16_t len16 = UGETW(desc.len[i]);
2251 
2252 				if (len16 == 0 || len16 > actlen)
2253 					break;
2254 
2255 				zyd_rx_data(xfer, offset, len16);
2256 
2257 				/* next frame is aligned on a 32-bit boundary */
2258 				len16 = (len16 + 3) & ~3;
2259 				offset += len16;
2260 				if (len16 > actlen)
2261 					break;
2262 				actlen -= len16;
2263 			}
2264 		} else {
2265 			DPRINTF(sc, ZYD_DEBUG_RECV,
2266 			    "%s: received single-frame transfer\n", __func__);
2267 
2268 			zyd_rx_data(xfer, 0, actlen);
2269 		}
2270 		/* FALLTHROUGH */
2271 	case USB_ST_SETUP:
2272 tr_setup:
2273 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2274 		usbd_transfer_submit(xfer);
2275 
2276 		/*
2277 		 * At the end of a USB callback it is always safe to unlock
2278 		 * the private mutex of a device! That is why we do the
2279 		 * "ieee80211_input" here, and not some lines up!
2280 		 */
2281 		ZYD_UNLOCK(sc);
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 		ZYD_LOCK(sc);
2298 		zyd_start(sc);
2299 		break;
2300 
2301 	default:			/* Error */
2302 		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2303 
2304 		if (error != USB_ERR_CANCELLED) {
2305 			/* try to clear stall first */
2306 			usbd_xfer_set_stall(xfer);
2307 			goto tr_setup;
2308 		}
2309 		break;
2310 	}
2311 }
2312 
2313 static uint8_t
2314 zyd_plcp_signal(struct zyd_softc *sc, int rate)
2315 {
2316 	switch (rate) {
2317 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2318 	case 12:
2319 		return (0xb);
2320 	case 18:
2321 		return (0xf);
2322 	case 24:
2323 		return (0xa);
2324 	case 36:
2325 		return (0xe);
2326 	case 48:
2327 		return (0x9);
2328 	case 72:
2329 		return (0xd);
2330 	case 96:
2331 		return (0x8);
2332 	case 108:
2333 		return (0xc);
2334 	/* CCK rates (NB: not IEEE std, device-specific) */
2335 	case 2:
2336 		return (0x0);
2337 	case 4:
2338 		return (0x1);
2339 	case 11:
2340 		return (0x2);
2341 	case 22:
2342 		return (0x3);
2343 	}
2344 
2345 	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2346 	return (0x0);
2347 }
2348 
2349 static void
2350 zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2351 {
2352 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2353 	struct ieee80211vap *vap;
2354 	struct zyd_tx_data *data;
2355 	struct mbuf *m;
2356 	struct usb_page_cache *pc;
2357 	int actlen;
2358 
2359 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2360 
2361 	switch (USB_GET_STATE(xfer)) {
2362 	case USB_ST_TRANSFERRED:
2363 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2364 		    actlen);
2365 
2366 		/* free resources */
2367 		data = usbd_xfer_get_priv(xfer);
2368 		zyd_tx_free(data, 0);
2369 		usbd_xfer_set_priv(xfer, NULL);
2370 
2371 		/* FALLTHROUGH */
2372 	case USB_ST_SETUP:
2373 tr_setup:
2374 		data = STAILQ_FIRST(&sc->tx_q);
2375 		if (data) {
2376 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2377 			m = data->m;
2378 
2379 			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2380 				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2381 				    m->m_pkthdr.len);
2382 				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2383 			}
2384 			pc = usbd_xfer_get_frame(xfer, 0);
2385 			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2386 			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2387 			    m->m_pkthdr.len);
2388 
2389 			vap = data->ni->ni_vap;
2390 			if (ieee80211_radiotap_active_vap(vap)) {
2391 				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2392 
2393 				tap->wt_flags = 0;
2394 				tap->wt_rate = data->rate;
2395 
2396 				ieee80211_radiotap_tx(vap, m);
2397 			}
2398 
2399 			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2400 			usbd_xfer_set_priv(xfer, data);
2401 			usbd_transfer_submit(xfer);
2402 		}
2403 		zyd_start(sc);
2404 		break;
2405 
2406 	default:			/* Error */
2407 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2408 		    usbd_errstr(error));
2409 
2410 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
2411 		data = usbd_xfer_get_priv(xfer);
2412 		usbd_xfer_set_priv(xfer, NULL);
2413 		if (data != NULL)
2414 			zyd_tx_free(data, error);
2415 
2416 		if (error != USB_ERR_CANCELLED) {
2417 			if (error == USB_ERR_TIMEOUT)
2418 				device_printf(sc->sc_dev, "device timeout\n");
2419 
2420 			/*
2421 			 * Try to clear stall first, also if other
2422 			 * errors occur, hence clearing stall
2423 			 * introduces a 50 ms delay:
2424 			 */
2425 			usbd_xfer_set_stall(xfer);
2426 			goto tr_setup;
2427 		}
2428 		break;
2429 	}
2430 }
2431 
2432 static int
2433 zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2434 {
2435 	struct ieee80211vap *vap = ni->ni_vap;
2436 	struct ieee80211com *ic = ni->ni_ic;
2437 	struct zyd_tx_desc *desc;
2438 	struct zyd_tx_data *data;
2439 	struct ieee80211_frame *wh;
2440 	const struct ieee80211_txparam *tp = ni->ni_txparms;
2441 	struct ieee80211_key *k;
2442 	int rate, totlen, type, ismcast;
2443 	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2444 	uint8_t phy;
2445 	uint16_t pktlen;
2446 	uint32_t bits;
2447 
2448 	wh = mtod(m0, struct ieee80211_frame *);
2449 	data = STAILQ_FIRST(&sc->tx_free);
2450 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2451 	sc->tx_nfree--;
2452 
2453 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2454 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2455 
2456 	if (type == IEEE80211_FC0_TYPE_MGT ||
2457 	    type == IEEE80211_FC0_TYPE_CTL ||
2458 	    (m0->m_flags & M_EAPOL) != 0) {
2459 		rate = tp->mgmtrate;
2460 	} else {
2461 		/* for data frames */
2462 		if (ismcast)
2463 			rate = tp->mcastrate;
2464 		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2465 			rate = tp->ucastrate;
2466 		else {
2467 			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2468 			rate = ni->ni_txrate;
2469 		}
2470 	}
2471 
2472 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2473 		k = ieee80211_crypto_encap(ni, m0);
2474 		if (k == NULL) {
2475 			return (ENOBUFS);
2476 		}
2477 		/* packet header may have moved, reset our local pointer */
2478 		wh = mtod(m0, struct ieee80211_frame *);
2479 	}
2480 
2481 	data->ni = ni;
2482 	data->m = m0;
2483 	data->rate = rate;
2484 
2485 	/* fill Tx descriptor */
2486 	desc = &data->desc;
2487 	phy = zyd_plcp_signal(sc, rate);
2488 	desc->phy = phy;
2489 	if (ZYD_RATE_IS_OFDM(rate)) {
2490 		desc->phy |= ZYD_TX_PHY_OFDM;
2491 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2492 			desc->phy |= ZYD_TX_PHY_5GHZ;
2493 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2494 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2495 
2496 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2497 	desc->len = htole16(totlen);
2498 
2499 	desc->flags = ZYD_TX_FLAG_BACKOFF;
2500 	if (!ismcast) {
2501 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2502 		if (totlen > vap->iv_rtsthreshold) {
2503 			desc->flags |= ZYD_TX_FLAG_RTS;
2504 		} else if (ZYD_RATE_IS_OFDM(rate) &&
2505 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2506 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2507 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2508 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2509 				desc->flags |= ZYD_TX_FLAG_RTS;
2510 		}
2511 	} else
2512 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2513 	if ((wh->i_fc[0] &
2514 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2515 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2516 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2517 
2518 	/* actual transmit length (XXX why +10?) */
2519 	pktlen = ZYD_TX_DESC_SIZE + 10;
2520 	if (sc->sc_macrev == ZYD_ZD1211)
2521 		pktlen += totlen;
2522 	desc->pktlen = htole16(pktlen);
2523 
2524 	bits = (rate == 11) ? (totlen * 16) + 10 :
2525 	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2526 	desc->plcp_length = htole16(bits / ratediv[phy]);
2527 	desc->plcp_service = 0;
2528 	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2529 		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2530 	desc->nextlen = 0;
2531 
2532 	if (ieee80211_radiotap_active_vap(vap)) {
2533 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2534 
2535 		tap->wt_flags = 0;
2536 		tap->wt_rate = rate;
2537 
2538 		ieee80211_radiotap_tx(vap, m0);
2539 	}
2540 
2541 	DPRINTF(sc, ZYD_DEBUG_XMIT,
2542 	    "%s: sending data frame len=%zu rate=%u\n",
2543 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2544 		rate);
2545 
2546 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2547 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2548 
2549 	return (0);
2550 }
2551 
2552 static int
2553 zyd_transmit(struct ieee80211com *ic, struct mbuf *m)
2554 {
2555 	struct zyd_softc *sc = ic->ic_softc;
2556 	int error;
2557 
2558 	ZYD_LOCK(sc);
2559 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2560 		ZYD_UNLOCK(sc);
2561 		return (ENXIO);
2562 	}
2563 	error = mbufq_enqueue(&sc->sc_snd, m);
2564 	if (error) {
2565 		ZYD_UNLOCK(sc);
2566 		return (error);
2567 	}
2568 	zyd_start(sc);
2569 	ZYD_UNLOCK(sc);
2570 
2571 	return (0);
2572 }
2573 
2574 static void
2575 zyd_start(struct zyd_softc *sc)
2576 {
2577 	struct ieee80211_node *ni;
2578 	struct mbuf *m;
2579 
2580 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2581 
2582 	while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2583 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2584 		if (zyd_tx_start(sc, m, ni) != 0) {
2585 			m_freem(m);
2586 			if_inc_counter(ni->ni_vap->iv_ifp,
2587 			    IFCOUNTER_OERRORS, 1);
2588 			ieee80211_free_node(ni);
2589 			break;
2590 		}
2591 	}
2592 }
2593 
2594 static int
2595 zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2596 	const struct ieee80211_bpf_params *params)
2597 {
2598 	struct ieee80211com *ic = ni->ni_ic;
2599 	struct zyd_softc *sc = ic->ic_softc;
2600 
2601 	ZYD_LOCK(sc);
2602 	/* prevent management frames from being sent if we're not ready */
2603 	if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) {
2604 		ZYD_UNLOCK(sc);
2605 		m_freem(m);
2606 		return (ENETDOWN);
2607 	}
2608 	if (sc->tx_nfree == 0) {
2609 		ZYD_UNLOCK(sc);
2610 		m_freem(m);
2611 		return (ENOBUFS);		/* XXX */
2612 	}
2613 
2614 	/*
2615 	 * Legacy path; interpret frame contents to decide
2616 	 * precisely how to send the frame.
2617 	 * XXX raw path
2618 	 */
2619 	if (zyd_tx_start(sc, m, ni) != 0) {
2620 		ZYD_UNLOCK(sc);
2621 		m_freem(m);
2622 		return (EIO);
2623 	}
2624 	ZYD_UNLOCK(sc);
2625 	return (0);
2626 }
2627 
2628 static void
2629 zyd_parent(struct ieee80211com *ic)
2630 {
2631 	struct zyd_softc *sc = ic->ic_softc;
2632 	int startall = 0;
2633 
2634 	ZYD_LOCK(sc);
2635 	if (sc->sc_flags & ZYD_FLAG_DETACHED) {
2636 		ZYD_UNLOCK(sc);
2637 		return;
2638 	}
2639 	if (ic->ic_nrunning > 0) {
2640 		if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2641 			zyd_init_locked(sc);
2642 			startall = 1;
2643 		} else
2644 			zyd_set_multi(sc);
2645 	} else if (sc->sc_flags & ZYD_FLAG_RUNNING)
2646 		zyd_stop(sc);
2647 	ZYD_UNLOCK(sc);
2648 	if (startall)
2649 		ieee80211_start_all(ic);
2650 }
2651 
2652 static void
2653 zyd_init_locked(struct zyd_softc *sc)
2654 {
2655 	struct ieee80211com *ic = &sc->sc_ic;
2656 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2657 	struct usb_config_descriptor *cd;
2658 	int error;
2659 	uint32_t val;
2660 
2661 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2662 
2663 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2664 		error = zyd_loadfirmware(sc);
2665 		if (error != 0) {
2666 			device_printf(sc->sc_dev,
2667 			    "could not load firmware (error=%d)\n", error);
2668 			goto fail;
2669 		}
2670 
2671 		/* reset device */
2672 		cd = usbd_get_config_descriptor(sc->sc_udev);
2673 		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2674 		    cd->bConfigurationValue);
2675 		if (error)
2676 			device_printf(sc->sc_dev, "reset failed, continuing\n");
2677 
2678 		error = zyd_hw_init(sc);
2679 		if (error) {
2680 			device_printf(sc->sc_dev,
2681 			    "hardware initialization failed\n");
2682 			goto fail;
2683 		}
2684 
2685 		device_printf(sc->sc_dev,
2686 		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2687 		    "BE%x NP%x Gain%x F%x\n",
2688 		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2689 		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2690 		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2691 		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2692 		    sc->sc_cckgain, sc->sc_fix_cr157);
2693 
2694 		/* read regulatory domain (currently unused) */
2695 		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2696 		sc->sc_regdomain = val >> 16;
2697 		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2698 		    sc->sc_regdomain);
2699 
2700 		/* we'll do software WEP decryption for now */
2701 		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2702 		    __func__);
2703 		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2704 
2705 		sc->sc_flags |= ZYD_FLAG_INITONCE;
2706 	}
2707 
2708 	if (sc->sc_flags & ZYD_FLAG_RUNNING)
2709 		zyd_stop(sc);
2710 
2711 	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2712 	    vap ? vap->iv_myaddr : ic->ic_macaddr, ":");
2713 	error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2714 	if (error != 0)
2715 		return;
2716 
2717 	/* set basic rates */
2718 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2719 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2720 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2721 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2722 	else	/* assumes 802.11b/g */
2723 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2724 
2725 	/* promiscuous mode */
2726 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2727 	/* multicast setup */
2728 	zyd_set_multi(sc);
2729 	/* set RX filter  */
2730 	error = zyd_set_rxfilter(sc);
2731 	if (error != 0)
2732 		goto fail;
2733 
2734 	/* switch radio transmitter ON */
2735 	error = zyd_switch_radio(sc, 1);
2736 	if (error != 0)
2737 		goto fail;
2738 	/* set default BSS channel */
2739 	zyd_set_chan(sc, ic->ic_curchan);
2740 
2741 	/*
2742 	 * Allocate Tx and Rx xfer queues.
2743 	 */
2744 	zyd_setup_tx_list(sc);
2745 
2746 	/* enable interrupts */
2747 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2748 
2749 	sc->sc_flags |= ZYD_FLAG_RUNNING;
2750 	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2751 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2752 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2753 
2754 	return;
2755 
2756 fail:	zyd_stop(sc);
2757 	return;
2758 }
2759 
2760 static void
2761 zyd_stop(struct zyd_softc *sc)
2762 {
2763 	int error;
2764 
2765 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2766 
2767 	sc->sc_flags &= ~ZYD_FLAG_RUNNING;
2768 	zyd_drain_mbufq(sc);
2769 
2770 	/*
2771 	 * Drain all the transfers, if not already drained:
2772 	 */
2773 	ZYD_UNLOCK(sc);
2774 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2775 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2776 	ZYD_LOCK(sc);
2777 
2778 	zyd_unsetup_tx_list(sc);
2779 
2780 	/* Stop now if the device was never set up */
2781 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2782 		return;
2783 
2784 	/* switch radio transmitter OFF */
2785 	error = zyd_switch_radio(sc, 0);
2786 	if (error != 0)
2787 		goto fail;
2788 	/* disable Rx */
2789 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2790 	/* disable interrupts */
2791 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2792 
2793 fail:
2794 	return;
2795 }
2796 
2797 static int
2798 zyd_loadfirmware(struct zyd_softc *sc)
2799 {
2800 	struct usb_device_request req;
2801 	size_t size;
2802 	u_char *fw;
2803 	uint8_t stat;
2804 	uint16_t addr;
2805 
2806 	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2807 		return (0);
2808 
2809 	if (sc->sc_macrev == ZYD_ZD1211) {
2810 		fw = (u_char *)zd1211_firmware;
2811 		size = sizeof(zd1211_firmware);
2812 	} else {
2813 		fw = (u_char *)zd1211b_firmware;
2814 		size = sizeof(zd1211b_firmware);
2815 	}
2816 
2817 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2818 	req.bRequest = ZYD_DOWNLOADREQ;
2819 	USETW(req.wIndex, 0);
2820 
2821 	addr = ZYD_FIRMWARE_START_ADDR;
2822 	while (size > 0) {
2823 		/*
2824 		 * When the transfer size is 4096 bytes, it is not
2825 		 * likely to be able to transfer it.
2826 		 * The cause is port or machine or chip?
2827 		 */
2828 		const int mlen = min(size, 64);
2829 
2830 		DPRINTF(sc, ZYD_DEBUG_FW,
2831 		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2832 
2833 		USETW(req.wValue, addr);
2834 		USETW(req.wLength, mlen);
2835 		if (zyd_do_request(sc, &req, fw) != 0)
2836 			return (EIO);
2837 
2838 		addr += mlen / 2;
2839 		fw   += mlen;
2840 		size -= mlen;
2841 	}
2842 
2843 	/* check whether the upload succeeded */
2844 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2845 	req.bRequest = ZYD_DOWNLOADSTS;
2846 	USETW(req.wValue, 0);
2847 	USETW(req.wIndex, 0);
2848 	USETW(req.wLength, sizeof(stat));
2849 	if (zyd_do_request(sc, &req, &stat) != 0)
2850 		return (EIO);
2851 
2852 	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2853 
2854 	return (stat & 0x80) ? (EIO) : (0);
2855 }
2856 
2857 static void
2858 zyd_scan_start(struct ieee80211com *ic)
2859 {
2860 	struct zyd_softc *sc = ic->ic_softc;
2861 
2862 	ZYD_LOCK(sc);
2863 	/* want broadcast address while scanning */
2864 	zyd_set_bssid(sc, ieee80211broadcastaddr);
2865 	ZYD_UNLOCK(sc);
2866 }
2867 
2868 static void
2869 zyd_scan_end(struct ieee80211com *ic)
2870 {
2871 	struct zyd_softc *sc = ic->ic_softc;
2872 
2873 	ZYD_LOCK(sc);
2874 	/* restore previous bssid */
2875 	zyd_set_bssid(sc, sc->sc_bssid);
2876 	ZYD_UNLOCK(sc);
2877 }
2878 
2879 static void
2880 zyd_getradiocaps(struct ieee80211com *ic,
2881     int maxchans, int *nchans, struct ieee80211_channel chans[])
2882 {
2883 	uint8_t bands[IEEE80211_MODE_BYTES];
2884 
2885 	memset(bands, 0, sizeof(bands));
2886 	setbit(bands, IEEE80211_MODE_11B);
2887 	setbit(bands, IEEE80211_MODE_11G);
2888 	ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
2889 }
2890 
2891 static void
2892 zyd_set_channel(struct ieee80211com *ic)
2893 {
2894 	struct zyd_softc *sc = ic->ic_softc;
2895 
2896 	ZYD_LOCK(sc);
2897 	zyd_set_chan(sc, ic->ic_curchan);
2898 	ZYD_UNLOCK(sc);
2899 }
2900 
2901 static device_method_t zyd_methods[] = {
2902         /* Device interface */
2903         DEVMETHOD(device_probe, zyd_match),
2904         DEVMETHOD(device_attach, zyd_attach),
2905         DEVMETHOD(device_detach, zyd_detach),
2906 	DEVMETHOD_END
2907 };
2908 
2909 static driver_t zyd_driver = {
2910 	.name = "zyd",
2911 	.methods = zyd_methods,
2912 	.size = sizeof(struct zyd_softc)
2913 };
2914 
2915 static devclass_t zyd_devclass;
2916 
2917 DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2918 MODULE_DEPEND(zyd, usb, 1, 1, 1);
2919 MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2920 MODULE_VERSION(zyd, 1);
2921 USB_PNP_HOST_INFO(zyd_devs);
2922