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