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