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