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