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