xref: /freebsd/sys/dev/usb/wlan/if_zyd.c (revision 0b37c1590418417c894529d371800dfac71ef887)
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 u_int
1974 zyd_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
1975 {
1976 	uint32_t *hash = arg;
1977 	uint8_t v;
1978 
1979 	v = ((uint8_t *)LLADDR(sdl))[5] >> 2;
1980 	if (v < 32)
1981 		hash[0] |= 1 << v;
1982 	else
1983 		hash[1] |= 1 << (v - 32);
1984 
1985 	return (1);
1986 }
1987 
1988 static void
1989 zyd_set_multi(struct zyd_softc *sc)
1990 {
1991 	struct ieee80211com *ic = &sc->sc_ic;
1992 	uint32_t hash[2];
1993 	int error;
1994 
1995 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0)
1996 		return;
1997 
1998 	hash[0] = 0x00000000;
1999 	hash[1] = 0x80000000;
2000 
2001 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
2002 	    ic->ic_promisc > 0) {
2003 		hash[0] = 0xffffffff;
2004 		hash[1] = 0xffffffff;
2005 	} else {
2006 		struct ieee80211vap *vap;
2007 
2008 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
2009 			if_foreach_llmaddr(vap->iv_ifp, zyd_hash_maddr, &hash);
2010 	}
2011 
2012 	/* reprogram multicast global hash table */
2013 	zyd_write32_m(sc, ZYD_MAC_GHTBL, hash[0]);
2014 	zyd_write32_m(sc, ZYD_MAC_GHTBH, hash[1]);
2015 fail:
2016 	if (error != 0)
2017 		device_printf(sc->sc_dev,
2018 		    "could not set multicast hash table\n");
2019 }
2020 
2021 static void
2022 zyd_update_mcast(struct ieee80211com *ic)
2023 {
2024 	struct zyd_softc *sc = ic->ic_softc;
2025 
2026 	ZYD_LOCK(sc);
2027 	zyd_set_multi(sc);
2028 	ZYD_UNLOCK(sc);
2029 }
2030 
2031 static int
2032 zyd_set_rxfilter(struct zyd_softc *sc)
2033 {
2034 	struct ieee80211com *ic = &sc->sc_ic;
2035 	uint32_t rxfilter;
2036 
2037 	switch (ic->ic_opmode) {
2038 	case IEEE80211_M_STA:
2039 		rxfilter = ZYD_FILTER_BSS;
2040 		break;
2041 	case IEEE80211_M_IBSS:
2042 	case IEEE80211_M_HOSTAP:
2043 		rxfilter = ZYD_FILTER_HOSTAP;
2044 		break;
2045 	case IEEE80211_M_MONITOR:
2046 		rxfilter = ZYD_FILTER_MONITOR;
2047 		break;
2048 	default:
2049 		/* should not get there */
2050 		return (EINVAL);
2051 	}
2052 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2053 }
2054 
2055 static void
2056 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2057 {
2058 	int error;
2059 	struct ieee80211com *ic = &sc->sc_ic;
2060 	struct zyd_rf *rf = &sc->sc_rf;
2061 	uint32_t tmp;
2062 	int chan;
2063 
2064 	chan = ieee80211_chan2ieee(ic, c);
2065 	if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2066 		/* XXX should NEVER happen */
2067 		device_printf(sc->sc_dev,
2068 		    "%s: invalid channel %x\n", __func__, chan);
2069 		return;
2070 	}
2071 
2072 	error = zyd_lock_phy(sc);
2073 	if (error != 0)
2074 		goto fail;
2075 
2076 	error = (*rf->set_channel)(rf, chan);
2077 	if (error != 0)
2078 		goto fail;
2079 
2080 	if (rf->update_pwr) {
2081 		/* update Tx power */
2082 		zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2083 
2084 		if (sc->sc_macrev == ZYD_ZD1211B) {
2085 			zyd_write16_m(sc, ZYD_CR67,
2086 			    sc->sc_ofdm36_cal[chan - 1]);
2087 			zyd_write16_m(sc, ZYD_CR66,
2088 			    sc->sc_ofdm48_cal[chan - 1]);
2089 			zyd_write16_m(sc, ZYD_CR65,
2090 			    sc->sc_ofdm54_cal[chan - 1]);
2091 			zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2092 			zyd_write16_m(sc, ZYD_CR69, 0x28);
2093 			zyd_write16_m(sc, ZYD_CR69, 0x2a);
2094 		}
2095 	}
2096 	if (sc->sc_cckgain) {
2097 		/* set CCK baseband gain from EEPROM */
2098 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2099 			zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2100 	}
2101 	if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2102 		error = (*rf->bandedge6)(rf, c);
2103 		if (error != 0)
2104 			goto fail;
2105 	}
2106 	zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2107 
2108 	error = zyd_unlock_phy(sc);
2109 	if (error != 0)
2110 		goto fail;
2111 
2112 	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2113 	    htole16(c->ic_freq);
2114 	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2115 	    htole16(c->ic_flags);
2116 fail:
2117 	return;
2118 }
2119 
2120 static int
2121 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2122 {
2123 	int error;
2124 	uint32_t val;
2125 
2126 	zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2127 	sc->sc_atim_wnd = val;
2128 	zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2129 	sc->sc_pre_tbtt = val;
2130 	sc->sc_bcn_int = bintval;
2131 
2132 	if (sc->sc_bcn_int <= 5)
2133 		sc->sc_bcn_int = 5;
2134 	if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2135 		sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2136 	if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2137 		sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2138 
2139 	zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2140 	zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2141 	zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2142 fail:
2143 	return (error);
2144 }
2145 
2146 static void
2147 zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2148 {
2149 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2150 	struct ieee80211com *ic = &sc->sc_ic;
2151 	struct zyd_plcphdr plcp;
2152 	struct zyd_rx_stat stat;
2153 	struct usb_page_cache *pc;
2154 	struct mbuf *m;
2155 	int rlen, rssi;
2156 
2157 	if (len < ZYD_MIN_FRAGSZ) {
2158 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2159 		    device_get_nameunit(sc->sc_dev), len);
2160 		counter_u64_add(ic->ic_ierrors, 1);
2161 		return;
2162 	}
2163 	pc = usbd_xfer_get_frame(xfer, 0);
2164 	usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2165 	usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2166 
2167 	if (stat.flags & ZYD_RX_ERROR) {
2168 		DPRINTF(sc, ZYD_DEBUG_RECV,
2169 		    "%s: RX status indicated error (%x)\n",
2170 		    device_get_nameunit(sc->sc_dev), stat.flags);
2171 		counter_u64_add(ic->ic_ierrors, 1);
2172 		return;
2173 	}
2174 
2175 	/* compute actual frame length */
2176 	rlen = len - sizeof(struct zyd_plcphdr) -
2177 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2178 
2179 	/* allocate a mbuf to store the frame */
2180 	if (rlen > (int)MCLBYTES) {
2181 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2182 		    device_get_nameunit(sc->sc_dev), rlen);
2183 		counter_u64_add(ic->ic_ierrors, 1);
2184 		return;
2185 	} else if (rlen > (int)MHLEN)
2186 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2187 	else
2188 		m = m_gethdr(M_NOWAIT, MT_DATA);
2189 	if (m == NULL) {
2190 		DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2191 		    device_get_nameunit(sc->sc_dev));
2192 		counter_u64_add(ic->ic_ierrors, 1);
2193 		return;
2194 	}
2195 	m->m_pkthdr.len = m->m_len = rlen;
2196 	usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2197 
2198 	if (ieee80211_radiotap_active(ic)) {
2199 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2200 
2201 		tap->wr_flags = 0;
2202 		if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2203 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2204 		/* XXX toss, no way to express errors */
2205 		if (stat.flags & ZYD_RX_DECRYPTERR)
2206 			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2207 		tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2208 		    (stat.flags & ZYD_RX_OFDM) ?
2209 			IEEE80211_T_OFDM : IEEE80211_T_CCK);
2210 		tap->wr_antsignal = stat.rssi + -95;
2211 		tap->wr_antnoise = -95;	/* XXX */
2212 	}
2213 	rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2214 
2215 	sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2216 	sc->sc_rx_data[sc->sc_rx_count].m = m;
2217 	sc->sc_rx_count++;
2218 }
2219 
2220 static void
2221 zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2222 {
2223 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2224 	struct ieee80211com *ic = &sc->sc_ic;
2225 	struct ieee80211_node *ni;
2226 	struct zyd_rx_desc desc;
2227 	struct mbuf *m;
2228 	struct usb_page_cache *pc;
2229 	uint32_t offset;
2230 	uint8_t rssi;
2231 	int8_t nf;
2232 	int i;
2233 	int actlen;
2234 
2235 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2236 
2237 	sc->sc_rx_count = 0;
2238 	switch (USB_GET_STATE(xfer)) {
2239 	case USB_ST_TRANSFERRED:
2240 		pc = usbd_xfer_get_frame(xfer, 0);
2241 		usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2242 
2243 		offset = 0;
2244 		if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2245 			DPRINTF(sc, ZYD_DEBUG_RECV,
2246 			    "%s: received multi-frame transfer\n", __func__);
2247 
2248 			for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2249 				uint16_t len16 = UGETW(desc.len[i]);
2250 
2251 				if (len16 == 0 || len16 > actlen)
2252 					break;
2253 
2254 				zyd_rx_data(xfer, offset, len16);
2255 
2256 				/* next frame is aligned on a 32-bit boundary */
2257 				len16 = (len16 + 3) & ~3;
2258 				offset += len16;
2259 				if (len16 > actlen)
2260 					break;
2261 				actlen -= len16;
2262 			}
2263 		} else {
2264 			DPRINTF(sc, ZYD_DEBUG_RECV,
2265 			    "%s: received single-frame transfer\n", __func__);
2266 
2267 			zyd_rx_data(xfer, 0, actlen);
2268 		}
2269 		/* FALLTHROUGH */
2270 	case USB_ST_SETUP:
2271 tr_setup:
2272 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2273 		usbd_transfer_submit(xfer);
2274 
2275 		/*
2276 		 * At the end of a USB callback it is always safe to unlock
2277 		 * the private mutex of a device! That is why we do the
2278 		 * "ieee80211_input" here, and not some lines up!
2279 		 */
2280 		ZYD_UNLOCK(sc);
2281 		for (i = 0; i < sc->sc_rx_count; i++) {
2282 			rssi = sc->sc_rx_data[i].rssi;
2283 			m = sc->sc_rx_data[i].m;
2284 			sc->sc_rx_data[i].m = NULL;
2285 
2286 			nf = -95;	/* XXX */
2287 
2288 			ni = ieee80211_find_rxnode(ic,
2289 			    mtod(m, struct ieee80211_frame_min *));
2290 			if (ni != NULL) {
2291 				(void)ieee80211_input(ni, m, rssi, nf);
2292 				ieee80211_free_node(ni);
2293 			} else
2294 				(void)ieee80211_input_all(ic, m, rssi, nf);
2295 		}
2296 		ZYD_LOCK(sc);
2297 		zyd_start(sc);
2298 		break;
2299 
2300 	default:			/* Error */
2301 		DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2302 
2303 		if (error != USB_ERR_CANCELLED) {
2304 			/* try to clear stall first */
2305 			usbd_xfer_set_stall(xfer);
2306 			goto tr_setup;
2307 		}
2308 		break;
2309 	}
2310 }
2311 
2312 static uint8_t
2313 zyd_plcp_signal(struct zyd_softc *sc, int rate)
2314 {
2315 	switch (rate) {
2316 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2317 	case 12:
2318 		return (0xb);
2319 	case 18:
2320 		return (0xf);
2321 	case 24:
2322 		return (0xa);
2323 	case 36:
2324 		return (0xe);
2325 	case 48:
2326 		return (0x9);
2327 	case 72:
2328 		return (0xd);
2329 	case 96:
2330 		return (0x8);
2331 	case 108:
2332 		return (0xc);
2333 	/* CCK rates (NB: not IEEE std, device-specific) */
2334 	case 2:
2335 		return (0x0);
2336 	case 4:
2337 		return (0x1);
2338 	case 11:
2339 		return (0x2);
2340 	case 22:
2341 		return (0x3);
2342 	}
2343 
2344 	device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2345 	return (0x0);
2346 }
2347 
2348 static void
2349 zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2350 {
2351 	struct zyd_softc *sc = usbd_xfer_softc(xfer);
2352 	struct ieee80211vap *vap;
2353 	struct zyd_tx_data *data;
2354 	struct mbuf *m;
2355 	struct usb_page_cache *pc;
2356 	int actlen;
2357 
2358 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2359 
2360 	switch (USB_GET_STATE(xfer)) {
2361 	case USB_ST_TRANSFERRED:
2362 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2363 		    actlen);
2364 
2365 		/* free resources */
2366 		data = usbd_xfer_get_priv(xfer);
2367 		zyd_tx_free(data, 0);
2368 		usbd_xfer_set_priv(xfer, NULL);
2369 
2370 		/* FALLTHROUGH */
2371 	case USB_ST_SETUP:
2372 tr_setup:
2373 		data = STAILQ_FIRST(&sc->tx_q);
2374 		if (data) {
2375 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2376 			m = data->m;
2377 
2378 			if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2379 				DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2380 				    m->m_pkthdr.len);
2381 				m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2382 			}
2383 			pc = usbd_xfer_get_frame(xfer, 0);
2384 			usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2385 			usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2386 			    m->m_pkthdr.len);
2387 
2388 			vap = data->ni->ni_vap;
2389 			if (ieee80211_radiotap_active_vap(vap)) {
2390 				struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2391 
2392 				tap->wt_flags = 0;
2393 				tap->wt_rate = data->rate;
2394 
2395 				ieee80211_radiotap_tx(vap, m);
2396 			}
2397 
2398 			usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2399 			usbd_xfer_set_priv(xfer, data);
2400 			usbd_transfer_submit(xfer);
2401 		}
2402 		zyd_start(sc);
2403 		break;
2404 
2405 	default:			/* Error */
2406 		DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2407 		    usbd_errstr(error));
2408 
2409 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
2410 		data = usbd_xfer_get_priv(xfer);
2411 		usbd_xfer_set_priv(xfer, NULL);
2412 		if (data != NULL)
2413 			zyd_tx_free(data, error);
2414 
2415 		if (error != USB_ERR_CANCELLED) {
2416 			if (error == USB_ERR_TIMEOUT)
2417 				device_printf(sc->sc_dev, "device timeout\n");
2418 
2419 			/*
2420 			 * Try to clear stall first, also if other
2421 			 * errors occur, hence clearing stall
2422 			 * introduces a 50 ms delay:
2423 			 */
2424 			usbd_xfer_set_stall(xfer);
2425 			goto tr_setup;
2426 		}
2427 		break;
2428 	}
2429 }
2430 
2431 static int
2432 zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2433 {
2434 	struct ieee80211vap *vap = ni->ni_vap;
2435 	struct ieee80211com *ic = ni->ni_ic;
2436 	struct zyd_tx_desc *desc;
2437 	struct zyd_tx_data *data;
2438 	struct ieee80211_frame *wh;
2439 	const struct ieee80211_txparam *tp = ni->ni_txparms;
2440 	struct ieee80211_key *k;
2441 	int rate, totlen, type, ismcast;
2442 	static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2443 	uint8_t phy;
2444 	uint16_t pktlen;
2445 	uint32_t bits;
2446 
2447 	wh = mtod(m0, struct ieee80211_frame *);
2448 	data = STAILQ_FIRST(&sc->tx_free);
2449 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2450 	sc->tx_nfree--;
2451 
2452 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2453 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2454 
2455 	if (type == IEEE80211_FC0_TYPE_MGT ||
2456 	    type == IEEE80211_FC0_TYPE_CTL ||
2457 	    (m0->m_flags & M_EAPOL) != 0) {
2458 		rate = tp->mgmtrate;
2459 	} else {
2460 		/* for data frames */
2461 		if (ismcast)
2462 			rate = tp->mcastrate;
2463 		else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2464 			rate = tp->ucastrate;
2465 		else {
2466 			(void) ieee80211_ratectl_rate(ni, NULL, 0);
2467 			rate = ni->ni_txrate;
2468 		}
2469 	}
2470 
2471 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2472 		k = ieee80211_crypto_encap(ni, m0);
2473 		if (k == NULL) {
2474 			return (ENOBUFS);
2475 		}
2476 		/* packet header may have moved, reset our local pointer */
2477 		wh = mtod(m0, struct ieee80211_frame *);
2478 	}
2479 
2480 	data->ni = ni;
2481 	data->m = m0;
2482 	data->rate = rate;
2483 
2484 	/* fill Tx descriptor */
2485 	desc = &data->desc;
2486 	phy = zyd_plcp_signal(sc, rate);
2487 	desc->phy = phy;
2488 	if (ZYD_RATE_IS_OFDM(rate)) {
2489 		desc->phy |= ZYD_TX_PHY_OFDM;
2490 		if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2491 			desc->phy |= ZYD_TX_PHY_5GHZ;
2492 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2493 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2494 
2495 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2496 	desc->len = htole16(totlen);
2497 
2498 	desc->flags = ZYD_TX_FLAG_BACKOFF;
2499 	if (!ismcast) {
2500 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2501 		if (totlen > vap->iv_rtsthreshold) {
2502 			desc->flags |= ZYD_TX_FLAG_RTS;
2503 		} else if (ZYD_RATE_IS_OFDM(rate) &&
2504 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2505 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2506 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2507 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2508 				desc->flags |= ZYD_TX_FLAG_RTS;
2509 		}
2510 	} else
2511 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2512 	if ((wh->i_fc[0] &
2513 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2514 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2515 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2516 
2517 	/* actual transmit length (XXX why +10?) */
2518 	pktlen = ZYD_TX_DESC_SIZE + 10;
2519 	if (sc->sc_macrev == ZYD_ZD1211)
2520 		pktlen += totlen;
2521 	desc->pktlen = htole16(pktlen);
2522 
2523 	bits = (rate == 11) ? (totlen * 16) + 10 :
2524 	    ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2525 	desc->plcp_length = htole16(bits / ratediv[phy]);
2526 	desc->plcp_service = 0;
2527 	if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2528 		desc->plcp_service |= ZYD_PLCP_LENGEXT;
2529 	desc->nextlen = 0;
2530 
2531 	if (ieee80211_radiotap_active_vap(vap)) {
2532 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2533 
2534 		tap->wt_flags = 0;
2535 		tap->wt_rate = rate;
2536 
2537 		ieee80211_radiotap_tx(vap, m0);
2538 	}
2539 
2540 	DPRINTF(sc, ZYD_DEBUG_XMIT,
2541 	    "%s: sending data frame len=%zu rate=%u\n",
2542 	    device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2543 		rate);
2544 
2545 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2546 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2547 
2548 	return (0);
2549 }
2550 
2551 static int
2552 zyd_transmit(struct ieee80211com *ic, struct mbuf *m)
2553 {
2554 	struct zyd_softc *sc = ic->ic_softc;
2555 	int error;
2556 
2557 	ZYD_LOCK(sc);
2558 	if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2559 		ZYD_UNLOCK(sc);
2560 		return (ENXIO);
2561 	}
2562 	error = mbufq_enqueue(&sc->sc_snd, m);
2563 	if (error) {
2564 		ZYD_UNLOCK(sc);
2565 		return (error);
2566 	}
2567 	zyd_start(sc);
2568 	ZYD_UNLOCK(sc);
2569 
2570 	return (0);
2571 }
2572 
2573 static void
2574 zyd_start(struct zyd_softc *sc)
2575 {
2576 	struct ieee80211_node *ni;
2577 	struct mbuf *m;
2578 
2579 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2580 
2581 	while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2582 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2583 		if (zyd_tx_start(sc, m, ni) != 0) {
2584 			m_freem(m);
2585 			if_inc_counter(ni->ni_vap->iv_ifp,
2586 			    IFCOUNTER_OERRORS, 1);
2587 			ieee80211_free_node(ni);
2588 			break;
2589 		}
2590 	}
2591 }
2592 
2593 static int
2594 zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2595 	const struct ieee80211_bpf_params *params)
2596 {
2597 	struct ieee80211com *ic = ni->ni_ic;
2598 	struct zyd_softc *sc = ic->ic_softc;
2599 
2600 	ZYD_LOCK(sc);
2601 	/* prevent management frames from being sent if we're not ready */
2602 	if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) {
2603 		ZYD_UNLOCK(sc);
2604 		m_freem(m);
2605 		return (ENETDOWN);
2606 	}
2607 	if (sc->tx_nfree == 0) {
2608 		ZYD_UNLOCK(sc);
2609 		m_freem(m);
2610 		return (ENOBUFS);		/* XXX */
2611 	}
2612 
2613 	/*
2614 	 * Legacy path; interpret frame contents to decide
2615 	 * precisely how to send the frame.
2616 	 * XXX raw path
2617 	 */
2618 	if (zyd_tx_start(sc, m, ni) != 0) {
2619 		ZYD_UNLOCK(sc);
2620 		m_freem(m);
2621 		return (EIO);
2622 	}
2623 	ZYD_UNLOCK(sc);
2624 	return (0);
2625 }
2626 
2627 static void
2628 zyd_parent(struct ieee80211com *ic)
2629 {
2630 	struct zyd_softc *sc = ic->ic_softc;
2631 	int startall = 0;
2632 
2633 	ZYD_LOCK(sc);
2634 	if (sc->sc_flags & ZYD_FLAG_DETACHED) {
2635 		ZYD_UNLOCK(sc);
2636 		return;
2637 	}
2638 	if (ic->ic_nrunning > 0) {
2639 		if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2640 			zyd_init_locked(sc);
2641 			startall = 1;
2642 		} else
2643 			zyd_set_multi(sc);
2644 	} else if (sc->sc_flags & ZYD_FLAG_RUNNING)
2645 		zyd_stop(sc);
2646 	ZYD_UNLOCK(sc);
2647 	if (startall)
2648 		ieee80211_start_all(ic);
2649 }
2650 
2651 static void
2652 zyd_init_locked(struct zyd_softc *sc)
2653 {
2654 	struct ieee80211com *ic = &sc->sc_ic;
2655 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2656 	struct usb_config_descriptor *cd;
2657 	int error;
2658 	uint32_t val;
2659 
2660 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2661 
2662 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2663 		error = zyd_loadfirmware(sc);
2664 		if (error != 0) {
2665 			device_printf(sc->sc_dev,
2666 			    "could not load firmware (error=%d)\n", error);
2667 			goto fail;
2668 		}
2669 
2670 		/* reset device */
2671 		cd = usbd_get_config_descriptor(sc->sc_udev);
2672 		error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2673 		    cd->bConfigurationValue);
2674 		if (error)
2675 			device_printf(sc->sc_dev, "reset failed, continuing\n");
2676 
2677 		error = zyd_hw_init(sc);
2678 		if (error) {
2679 			device_printf(sc->sc_dev,
2680 			    "hardware initialization failed\n");
2681 			goto fail;
2682 		}
2683 
2684 		device_printf(sc->sc_dev,
2685 		    "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2686 		    "BE%x NP%x Gain%x F%x\n",
2687 		    (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2688 		    sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2689 		    zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2690 		    sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2691 		    sc->sc_cckgain, sc->sc_fix_cr157);
2692 
2693 		/* read regulatory domain (currently unused) */
2694 		zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2695 		sc->sc_regdomain = val >> 16;
2696 		DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2697 		    sc->sc_regdomain);
2698 
2699 		/* we'll do software WEP decryption for now */
2700 		DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2701 		    __func__);
2702 		zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2703 
2704 		sc->sc_flags |= ZYD_FLAG_INITONCE;
2705 	}
2706 
2707 	if (sc->sc_flags & ZYD_FLAG_RUNNING)
2708 		zyd_stop(sc);
2709 
2710 	DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2711 	    vap ? vap->iv_myaddr : ic->ic_macaddr, ":");
2712 	error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2713 	if (error != 0)
2714 		return;
2715 
2716 	/* set basic rates */
2717 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2718 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2719 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2720 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2721 	else	/* assumes 802.11b/g */
2722 		zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2723 
2724 	/* promiscuous mode */
2725 	zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2726 	/* multicast setup */
2727 	zyd_set_multi(sc);
2728 	/* set RX filter  */
2729 	error = zyd_set_rxfilter(sc);
2730 	if (error != 0)
2731 		goto fail;
2732 
2733 	/* switch radio transmitter ON */
2734 	error = zyd_switch_radio(sc, 1);
2735 	if (error != 0)
2736 		goto fail;
2737 	/* set default BSS channel */
2738 	zyd_set_chan(sc, ic->ic_curchan);
2739 
2740 	/*
2741 	 * Allocate Tx and Rx xfer queues.
2742 	 */
2743 	zyd_setup_tx_list(sc);
2744 
2745 	/* enable interrupts */
2746 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2747 
2748 	sc->sc_flags |= ZYD_FLAG_RUNNING;
2749 	usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2750 	usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2751 	usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2752 
2753 	return;
2754 
2755 fail:	zyd_stop(sc);
2756 	return;
2757 }
2758 
2759 static void
2760 zyd_stop(struct zyd_softc *sc)
2761 {
2762 	int error;
2763 
2764 	ZYD_LOCK_ASSERT(sc, MA_OWNED);
2765 
2766 	sc->sc_flags &= ~ZYD_FLAG_RUNNING;
2767 	zyd_drain_mbufq(sc);
2768 
2769 	/*
2770 	 * Drain all the transfers, if not already drained:
2771 	 */
2772 	ZYD_UNLOCK(sc);
2773 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2774 	usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2775 	ZYD_LOCK(sc);
2776 
2777 	zyd_unsetup_tx_list(sc);
2778 
2779 	/* Stop now if the device was never set up */
2780 	if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2781 		return;
2782 
2783 	/* switch radio transmitter OFF */
2784 	error = zyd_switch_radio(sc, 0);
2785 	if (error != 0)
2786 		goto fail;
2787 	/* disable Rx */
2788 	zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2789 	/* disable interrupts */
2790 	zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2791 
2792 fail:
2793 	return;
2794 }
2795 
2796 static int
2797 zyd_loadfirmware(struct zyd_softc *sc)
2798 {
2799 	struct usb_device_request req;
2800 	size_t size;
2801 	u_char *fw;
2802 	uint8_t stat;
2803 	uint16_t addr;
2804 
2805 	if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2806 		return (0);
2807 
2808 	if (sc->sc_macrev == ZYD_ZD1211) {
2809 		fw = (u_char *)zd1211_firmware;
2810 		size = sizeof(zd1211_firmware);
2811 	} else {
2812 		fw = (u_char *)zd1211b_firmware;
2813 		size = sizeof(zd1211b_firmware);
2814 	}
2815 
2816 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2817 	req.bRequest = ZYD_DOWNLOADREQ;
2818 	USETW(req.wIndex, 0);
2819 
2820 	addr = ZYD_FIRMWARE_START_ADDR;
2821 	while (size > 0) {
2822 		/*
2823 		 * When the transfer size is 4096 bytes, it is not
2824 		 * likely to be able to transfer it.
2825 		 * The cause is port or machine or chip?
2826 		 */
2827 		const int mlen = min(size, 64);
2828 
2829 		DPRINTF(sc, ZYD_DEBUG_FW,
2830 		    "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2831 
2832 		USETW(req.wValue, addr);
2833 		USETW(req.wLength, mlen);
2834 		if (zyd_do_request(sc, &req, fw) != 0)
2835 			return (EIO);
2836 
2837 		addr += mlen / 2;
2838 		fw   += mlen;
2839 		size -= mlen;
2840 	}
2841 
2842 	/* check whether the upload succeeded */
2843 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2844 	req.bRequest = ZYD_DOWNLOADSTS;
2845 	USETW(req.wValue, 0);
2846 	USETW(req.wIndex, 0);
2847 	USETW(req.wLength, sizeof(stat));
2848 	if (zyd_do_request(sc, &req, &stat) != 0)
2849 		return (EIO);
2850 
2851 	sc->sc_flags |= ZYD_FLAG_FWLOADED;
2852 
2853 	return (stat & 0x80) ? (EIO) : (0);
2854 }
2855 
2856 static void
2857 zyd_scan_start(struct ieee80211com *ic)
2858 {
2859 	struct zyd_softc *sc = ic->ic_softc;
2860 
2861 	ZYD_LOCK(sc);
2862 	/* want broadcast address while scanning */
2863 	zyd_set_bssid(sc, ieee80211broadcastaddr);
2864 	ZYD_UNLOCK(sc);
2865 }
2866 
2867 static void
2868 zyd_scan_end(struct ieee80211com *ic)
2869 {
2870 	struct zyd_softc *sc = ic->ic_softc;
2871 
2872 	ZYD_LOCK(sc);
2873 	/* restore previous bssid */
2874 	zyd_set_bssid(sc, sc->sc_bssid);
2875 	ZYD_UNLOCK(sc);
2876 }
2877 
2878 static void
2879 zyd_getradiocaps(struct ieee80211com *ic,
2880     int maxchans, int *nchans, struct ieee80211_channel chans[])
2881 {
2882 	uint8_t bands[IEEE80211_MODE_BYTES];
2883 
2884 	memset(bands, 0, sizeof(bands));
2885 	setbit(bands, IEEE80211_MODE_11B);
2886 	setbit(bands, IEEE80211_MODE_11G);
2887 	ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
2888 }
2889 
2890 static void
2891 zyd_set_channel(struct ieee80211com *ic)
2892 {
2893 	struct zyd_softc *sc = ic->ic_softc;
2894 
2895 	ZYD_LOCK(sc);
2896 	zyd_set_chan(sc, ic->ic_curchan);
2897 	ZYD_UNLOCK(sc);
2898 }
2899 
2900 static device_method_t zyd_methods[] = {
2901         /* Device interface */
2902         DEVMETHOD(device_probe, zyd_match),
2903         DEVMETHOD(device_attach, zyd_attach),
2904         DEVMETHOD(device_detach, zyd_detach),
2905 	DEVMETHOD_END
2906 };
2907 
2908 static driver_t zyd_driver = {
2909 	.name = "zyd",
2910 	.methods = zyd_methods,
2911 	.size = sizeof(struct zyd_softc)
2912 };
2913 
2914 static devclass_t zyd_devclass;
2915 
2916 DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2917 MODULE_DEPEND(zyd, usb, 1, 1, 1);
2918 MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2919 MODULE_VERSION(zyd, 1);
2920 USB_PNP_HOST_INFO(zyd_devs);
2921