xref: /openbsd/sys/dev/usb/if_zyd.c (revision cca36db2)
1 /*	$OpenBSD: if_zyd.c,v 1.88 2011/07/03 15:47:17 matthew Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
22  */
23 
24 #include "bpfilter.h"
25 
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/proc.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/timeout.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 
38 #include <machine/bus.h>
39 #include <machine/endian.h>
40 
41 #if NBPFILTER > 0
42 #include <net/bpf.h>
43 #endif
44 #include <net/if.h>
45 #include <net/if_arp.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49 
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/ip.h>
56 #endif
57 
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_amrr.h>
60 #include <net80211/ieee80211_radiotap.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66 
67 #include <dev/usb/if_zydreg.h>
68 
69 #ifdef USB_DEBUG
70 #define ZYD_DEBUG
71 #endif
72 
73 #ifdef ZYD_DEBUG
74 #define DPRINTF(x)	do { if (zyddebug > 0) printf x; } while (0)
75 #define DPRINTFN(n, x)	do { if (zyddebug > (n)) printf x; } while (0)
76 int zyddebug = 0;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n, x)
80 #endif
81 
82 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
83 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
84 
85 /* various supported device vendors/products */
86 #define ZYD_ZD1211_DEV(v, p)	\
87 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 }
88 #define ZYD_ZD1211B_DEV(v, p)	\
89 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B }
90 static const struct zyd_type {
91 	struct usb_devno	dev;
92 	uint8_t			rev;
93 #define ZYD_ZD1211	0
94 #define ZYD_ZD1211B	1
95 } zyd_devs[] = {
96 	ZYD_ZD1211_DEV(3COM2,		3CRUSB10075),
97 	ZYD_ZD1211_DEV(ABOCOM,		WL54),
98 	ZYD_ZD1211_DEV(ASUS,		WL159G),
99 	ZYD_ZD1211_DEV(CYBERTAN,	TG54USB),
100 	ZYD_ZD1211_DEV(DRAYTEK,		VIGOR550),
101 	ZYD_ZD1211_DEV(PLANEX2,		GWUS54GD),
102 	ZYD_ZD1211_DEV(PLANEX2,		GWUS54GZL),
103 	ZYD_ZD1211_DEV(PLANEX3,		GWUS54GZ),
104 	ZYD_ZD1211_DEV(PLANEX3,		GWUS54MINI),
105 	ZYD_ZD1211_DEV(SAGEM,		XG760A),
106 	ZYD_ZD1211_DEV(SENAO,		NUB8301),
107 	ZYD_ZD1211_DEV(SITECOMEU,	WL113),
108 	ZYD_ZD1211_DEV(SWEEX,		ZD1211),
109 	ZYD_ZD1211_DEV(TEKRAM,		QUICKWLAN),
110 	ZYD_ZD1211_DEV(TEKRAM,		ZD1211_1),
111 	ZYD_ZD1211_DEV(TEKRAM,		ZD1211_2),
112 	ZYD_ZD1211_DEV(TWINMOS,		G240),
113 	ZYD_ZD1211_DEV(UMEDIA,		ALL0298V2),
114 	ZYD_ZD1211_DEV(UMEDIA,		TEW429UB_A),
115 	ZYD_ZD1211_DEV(UMEDIA,		TEW429UB),
116 	ZYD_ZD1211_DEV(UNKNOWN2,	NW3100),
117 	ZYD_ZD1211_DEV(WISTRONNEWEB,	UR055G),
118 	ZYD_ZD1211_DEV(ZCOM,		ZD1211),
119 	ZYD_ZD1211_DEV(ZYDAS,		ALL0298),
120 	ZYD_ZD1211_DEV(ZYDAS,		ZD1211),
121 	ZYD_ZD1211_DEV(ZYXEL,		AG225H),
122 	ZYD_ZD1211_DEV(ZYXEL,		ZYAIRG220),
123 	ZYD_ZD1211_DEV(ZYXEL,		G200V2),
124 	ZYD_ZD1211_DEV(ZYXEL,		G202),
125 
126 	ZYD_ZD1211B_DEV(ACCTON,		SMCWUSBG),
127 	ZYD_ZD1211B_DEV(ACCTON,		WN4501H_LF_IR),
128 	ZYD_ZD1211B_DEV(ACCTON,		WUS201),
129 	ZYD_ZD1211B_DEV(ACCTON,		ZD1211B),
130 	ZYD_ZD1211B_DEV(ASUS,		A9T_WIFI),
131 	ZYD_ZD1211B_DEV(BELKIN,		F5D7050C),
132 	ZYD_ZD1211B_DEV(BELKIN,		ZD1211B),
133 	ZYD_ZD1211B_DEV(BEWAN,		BWIFI_USB54AR),
134 	ZYD_ZD1211B_DEV(CISCOLINKSYS,	WUSBF54G),
135 	ZYD_ZD1211B_DEV(CYBERTAN,	ZD1211B),
136 	ZYD_ZD1211B_DEV(FIBERLINE,	WL430U),
137 	ZYD_ZD1211B_DEV(MELCO,		KG54L),
138 	ZYD_ZD1211B_DEV(PHILIPS,	SNU5600),
139 	ZYD_ZD1211B_DEV(PHILIPS,	SNU5630NS05),
140 	ZYD_ZD1211B_DEV(PLANEX2,	GW_US54GXS),
141 	ZYD_ZD1211B_DEV(SAGEM,		XG76NA),
142 	ZYD_ZD1211B_DEV(SITECOMEU,	WL603),
143 	ZYD_ZD1211B_DEV(SITECOMEU,	ZD1211B),
144 	ZYD_ZD1211B_DEV(UMEDIA,		TEW429UBC1),
145 	ZYD_ZD1211B_DEV(UNKNOWN1,	ZD1211B_1),
146 	ZYD_ZD1211B_DEV(UNKNOWN1,	ZD1211B_2),
147 	ZYD_ZD1211B_DEV(UNKNOWN2,	ZD1211B),
148 	ZYD_ZD1211B_DEV(UNKNOWN3,	ZD1211B),
149 	ZYD_ZD1211B_DEV(SONY,		IFU_WLM2),
150 	ZYD_ZD1211B_DEV(USR,		USR5423),
151 	ZYD_ZD1211B_DEV(VTECH,		ZD1211B),
152 	ZYD_ZD1211B_DEV(ZCOM,		ZD1211B),
153 	ZYD_ZD1211B_DEV(ZYDAS,		ZD1211B),
154 	ZYD_ZD1211B_DEV(ZYDAS,		ZD1211B_2),
155 	ZYD_ZD1211B_DEV(ZYXEL,		M202),
156 	ZYD_ZD1211B_DEV(ZYXEL,		G220V2),
157 };
158 #define zyd_lookup(v, p)	\
159 	((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
160 
161 int zyd_match(struct device *, void *, void *);
162 void zyd_attach(struct device *, struct device *, void *);
163 int zyd_detach(struct device *, int);
164 int zyd_activate(struct device *, int);
165 
166 struct cfdriver zyd_cd = {
167 	NULL, "zyd", DV_IFNET
168 };
169 
170 const struct cfattach zyd_ca = {
171 	sizeof(struct zyd_softc),
172 	zyd_match,
173 	zyd_attach,
174 	zyd_detach,
175 	zyd_activate,
176 };
177 
178 void		zyd_attachhook(void *);
179 int		zyd_complete_attach(struct zyd_softc *);
180 int		zyd_open_pipes(struct zyd_softc *);
181 void		zyd_close_pipes(struct zyd_softc *);
182 int		zyd_alloc_tx_list(struct zyd_softc *);
183 void		zyd_free_tx_list(struct zyd_softc *);
184 int		zyd_alloc_rx_list(struct zyd_softc *);
185 void		zyd_free_rx_list(struct zyd_softc *);
186 struct		ieee80211_node *zyd_node_alloc(struct ieee80211com *);
187 int		zyd_media_change(struct ifnet *);
188 void		zyd_next_scan(void *);
189 void		zyd_task(void *);
190 int		zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
191 int		zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
192 		    void *, int, u_int);
193 int		zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
194 int		zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
195 int		zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
196 int		zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
197 int		zyd_rfwrite(struct zyd_softc *, uint32_t);
198 void		zyd_lock_phy(struct zyd_softc *);
199 void		zyd_unlock_phy(struct zyd_softc *);
200 int		zyd_rfmd_init(struct zyd_rf *);
201 int		zyd_rfmd_switch_radio(struct zyd_rf *, int);
202 int		zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
203 int		zyd_al2230_init(struct zyd_rf *);
204 int		zyd_al2230_switch_radio(struct zyd_rf *, int);
205 int		zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
206 int		zyd_al2230_init_b(struct zyd_rf *);
207 int		zyd_al7230B_init(struct zyd_rf *);
208 int		zyd_al7230B_switch_radio(struct zyd_rf *, int);
209 int		zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
210 int		zyd_al2210_init(struct zyd_rf *);
211 int		zyd_al2210_switch_radio(struct zyd_rf *, int);
212 int		zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
213 int		zyd_gct_init(struct zyd_rf *);
214 int		zyd_gct_switch_radio(struct zyd_rf *, int);
215 int		zyd_gct_set_channel(struct zyd_rf *, uint8_t);
216 int		zyd_maxim_init(struct zyd_rf *);
217 int		zyd_maxim_switch_radio(struct zyd_rf *, int);
218 int		zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
219 int		zyd_maxim2_init(struct zyd_rf *);
220 int		zyd_maxim2_switch_radio(struct zyd_rf *, int);
221 int		zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
222 int		zyd_rf_attach(struct zyd_softc *, uint8_t);
223 const char	*zyd_rf_name(uint8_t);
224 int		zyd_hw_init(struct zyd_softc *);
225 int		zyd_read_eeprom(struct zyd_softc *);
226 void		zyd_set_multi(struct zyd_softc *);
227 void		zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
228 void		zyd_set_bssid(struct zyd_softc *, const uint8_t *);
229 int		zyd_switch_radio(struct zyd_softc *, int);
230 void		zyd_set_led(struct zyd_softc *, int, int);
231 int		zyd_set_rxfilter(struct zyd_softc *);
232 void		zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
233 int		zyd_set_beacon_interval(struct zyd_softc *, int);
234 uint8_t		zyd_plcp_signal(int);
235 void		zyd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
236 void		zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
237 void		zyd_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
238 void		zyd_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
239 int		zyd_tx(struct zyd_softc *, struct mbuf *,
240 		    struct ieee80211_node *);
241 void		zyd_start(struct ifnet *);
242 void		zyd_watchdog(struct ifnet *);
243 int		zyd_ioctl(struct ifnet *, u_long, caddr_t);
244 int		zyd_init(struct ifnet *);
245 void		zyd_stop(struct ifnet *, int);
246 int		zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
247 void		zyd_iter_func(void *, struct ieee80211_node *);
248 void		zyd_amrr_timeout(void *);
249 void		zyd_newassoc(struct ieee80211com *, struct ieee80211_node *,
250 		    int);
251 
252 int
253 zyd_match(struct device *parent, void *match, void *aux)
254 {
255 	struct usb_attach_arg *uaa = aux;
256 
257 	if (!uaa->iface)
258 		return UMATCH_NONE;
259 
260 	return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ?
261 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
262 }
263 
264 void
265 zyd_attachhook(void *xsc)
266 {
267 	struct zyd_softc *sc = xsc;
268 	const char *fwname;
269 	u_char *fw;
270 	size_t size;
271 	int error;
272 
273 	fwname = (sc->mac_rev == ZYD_ZD1211) ? "zd1211" : "zd1211b";
274 	if ((error = loadfirmware(fwname, &fw, &size)) != 0) {
275 		printf("%s: error %d, could not read firmware file %s\n",
276 		    sc->sc_dev.dv_xname, error, fwname);
277 		return;
278 	}
279 
280 	error = zyd_loadfirmware(sc, fw, size);
281 	free(fw, M_DEVBUF);
282 	if (error != 0) {
283 		printf("%s: could not load firmware (error=%d)\n",
284 		    sc->sc_dev.dv_xname, error);
285 		return;
286 	}
287 
288 	/* complete the attach process */
289 	if (zyd_complete_attach(sc) == 0)
290 		sc->attached = 1;
291 }
292 
293 void
294 zyd_attach(struct device *parent, struct device *self, void *aux)
295 {
296 	struct zyd_softc *sc = (struct zyd_softc *)self;
297 	struct usb_attach_arg *uaa = aux;
298 	usb_device_descriptor_t* ddesc;
299 
300 	sc->sc_udev = uaa->device;
301 
302 	sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev;
303 
304 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
305 	if (UGETW(ddesc->bcdDevice) < 0x4330) {
306 		printf("%s: device version mismatch: 0x%x "
307 		    "(only >= 43.30 supported)\n", sc->sc_dev.dv_xname,
308 		    UGETW(ddesc->bcdDevice));
309 		return;
310 	}
311 
312 	if (rootvp == NULL)
313 		mountroothook_establish(zyd_attachhook, sc);
314 	else
315 		zyd_attachhook(sc);
316 }
317 
318 int
319 zyd_complete_attach(struct zyd_softc *sc)
320 {
321 	struct ieee80211com *ic = &sc->sc_ic;
322 	struct ifnet *ifp = &ic->ic_if;
323 	usbd_status error;
324 	int i;
325 
326 	usb_init_task(&sc->sc_task, zyd_task, sc, USB_TASK_TYPE_GENERIC);
327 	timeout_set(&sc->scan_to, zyd_next_scan, sc);
328 
329 	sc->amrr.amrr_min_success_threshold =  1;
330 	sc->amrr.amrr_max_success_threshold = 10;
331 	timeout_set(&sc->amrr_to, zyd_amrr_timeout, sc);
332 
333 	error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
334 	if (error != 0) {
335 		printf("%s: setting config no failed\n",
336 		    sc->sc_dev.dv_xname);
337 		goto fail;
338 	}
339 
340 	error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
341 	    &sc->sc_iface);
342 	if (error != 0) {
343 		printf("%s: getting interface handle failed\n",
344 		    sc->sc_dev.dv_xname);
345 		goto fail;
346 	}
347 
348 	if ((error = zyd_open_pipes(sc)) != 0) {
349 		printf("%s: could not open pipes\n", sc->sc_dev.dv_xname);
350 		goto fail;
351 	}
352 
353 	if ((error = zyd_read_eeprom(sc)) != 0) {
354 		printf("%s: could not read EEPROM\n", sc->sc_dev.dv_xname);
355 		goto fail;
356 	}
357 
358 	if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
359 		printf("%s: could not attach RF\n", sc->sc_dev.dv_xname);
360 		goto fail;
361 	}
362 
363 	if ((error = zyd_hw_init(sc)) != 0) {
364 		printf("%s: hardware initialization failed\n",
365 		    sc->sc_dev.dv_xname);
366 		goto fail;
367 	}
368 
369 	printf("%s: HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
370 	    sc->sc_dev.dv_xname, (sc->mac_rev == ZYD_ZD1211) ? "": "B",
371 	    sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
372 	    sc->pa_rev, ether_sprintf(ic->ic_myaddr));
373 
374 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
375 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
376 	ic->ic_state = IEEE80211_S_INIT;
377 
378 	/* set device capabilities */
379 	ic->ic_caps =
380 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
381 	    IEEE80211_C_TXPMGT |	/* tx power management */
382 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
383 	    IEEE80211_C_WEP |		/* s/w WEP */
384 	    IEEE80211_C_RSN;		/* WPA/RSN */
385 
386 	/* set supported .11b and .11g rates */
387 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
388 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
389 
390 	/* set supported .11b and .11g channels (1 through 14) */
391 	for (i = 1; i <= 14; i++) {
392 		ic->ic_channels[i].ic_freq =
393 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
394 		ic->ic_channels[i].ic_flags =
395 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
396 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
397 	}
398 
399 	ifp->if_softc = sc;
400 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
401 	ifp->if_ioctl = zyd_ioctl;
402 	ifp->if_start = zyd_start;
403 	ifp->if_watchdog = zyd_watchdog;
404 	IFQ_SET_READY(&ifp->if_snd);
405 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
406 
407 	if_attach(ifp);
408 	ieee80211_ifattach(ifp);
409 	ic->ic_node_alloc = zyd_node_alloc;
410 	ic->ic_newassoc = zyd_newassoc;
411 
412 	/* override state transition machine */
413 	sc->sc_newstate = ic->ic_newstate;
414 	ic->ic_newstate = zyd_newstate;
415 	ieee80211_media_init(ifp, zyd_media_change, ieee80211_media_status);
416 
417 #if NBPFILTER > 0
418 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
419 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
420 
421 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
422 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
423 	sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
424 
425 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
426 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
427 	sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
428 #endif
429 
430 fail:	return error;
431 }
432 
433 int
434 zyd_detach(struct device *self, int flags)
435 {
436 	struct zyd_softc *sc = (struct zyd_softc *)self;
437 	struct ifnet *ifp = &sc->sc_ic.ic_if;
438 	int s;
439 
440 	s = splusb();
441 
442 	usb_rem_task(sc->sc_udev, &sc->sc_task);
443 	if (timeout_initialized(&sc->scan_to))
444 		timeout_del(&sc->scan_to);
445 	if (timeout_initialized(&sc->amrr_to))
446 		timeout_del(&sc->amrr_to);
447 
448 	zyd_close_pipes(sc);
449 
450 	if (!sc->attached) {
451 		splx(s);
452 		return 0;
453 	}
454 
455 	if (ifp->if_softc != NULL) {
456 		ieee80211_ifdetach(ifp);
457 		if_detach(ifp);
458 	}
459 
460 	zyd_free_rx_list(sc);
461 	zyd_free_tx_list(sc);
462 
463 	sc->attached = 0;
464 
465 	splx(s);
466 
467 	return 0;
468 }
469 
470 int
471 zyd_open_pipes(struct zyd_softc *sc)
472 {
473 	usb_endpoint_descriptor_t *edesc;
474 	int isize;
475 	usbd_status error;
476 
477 	/* interrupt in */
478 	edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
479 	if (edesc == NULL)
480 		return EINVAL;
481 
482 	isize = UGETW(edesc->wMaxPacketSize);
483 	if (isize == 0)	/* should not happen */
484 		return EINVAL;
485 
486 	sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
487 	if (sc->ibuf == NULL)
488 		return ENOMEM;
489 
490 	error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
491 	    &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr,
492 	    USBD_DEFAULT_INTERVAL);
493 	if (error != 0) {
494 		printf("%s: open rx intr pipe failed: %s\n",
495 		    sc->sc_dev.dv_xname, usbd_errstr(error));
496 		goto fail;
497 	}
498 
499 	/* interrupt out (not necessarily an interrupt pipe) */
500 	error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
501 	    &sc->zyd_ep[ZYD_ENDPT_IOUT]);
502 	if (error != 0) {
503 		printf("%s: open tx intr pipe failed: %s\n",
504 		    sc->sc_dev.dv_xname, usbd_errstr(error));
505 		goto fail;
506 	}
507 
508 	/* bulk in */
509 	error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
510 	    &sc->zyd_ep[ZYD_ENDPT_BIN]);
511 	if (error != 0) {
512 		printf("%s: open rx pipe failed: %s\n",
513 		    sc->sc_dev.dv_xname, usbd_errstr(error));
514 		goto fail;
515 	}
516 
517 	/* bulk out */
518 	error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
519 	    &sc->zyd_ep[ZYD_ENDPT_BOUT]);
520 	if (error != 0) {
521 		printf("%s: open tx pipe failed: %s\n",
522 		    sc->sc_dev.dv_xname, usbd_errstr(error));
523 		goto fail;
524 	}
525 
526 	return 0;
527 
528 fail:	zyd_close_pipes(sc);
529 	return error;
530 }
531 
532 void
533 zyd_close_pipes(struct zyd_softc *sc)
534 {
535 	int i;
536 
537 	for (i = 0; i < ZYD_ENDPT_CNT; i++) {
538 		if (sc->zyd_ep[i] != NULL) {
539 			usbd_abort_pipe(sc->zyd_ep[i]);
540 			usbd_close_pipe(sc->zyd_ep[i]);
541 			sc->zyd_ep[i] = NULL;
542 		}
543 	}
544 	if (sc->ibuf != NULL) {
545 		free(sc->ibuf, M_USBDEV);
546 		sc->ibuf = NULL;
547 	}
548 }
549 
550 int
551 zyd_alloc_tx_list(struct zyd_softc *sc)
552 {
553 	int i, error;
554 
555 	sc->tx_queued = 0;
556 
557 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
558 		struct zyd_tx_data *data = &sc->tx_data[i];
559 
560 		data->sc = sc;	/* backpointer for callbacks */
561 
562 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
563 		if (data->xfer == NULL) {
564 			printf("%s: could not allocate tx xfer\n",
565 			    sc->sc_dev.dv_xname);
566 			error = ENOMEM;
567 			goto fail;
568 		}
569 		data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ);
570 		if (data->buf == NULL) {
571 			printf("%s: could not allocate tx buffer\n",
572 			    sc->sc_dev.dv_xname);
573 			error = ENOMEM;
574 			goto fail;
575 		}
576 
577 		/* clear Tx descriptor */
578 		bzero(data->buf, sizeof (struct zyd_tx_desc));
579 	}
580 	return 0;
581 
582 fail:	zyd_free_tx_list(sc);
583 	return error;
584 }
585 
586 void
587 zyd_free_tx_list(struct zyd_softc *sc)
588 {
589 	struct ieee80211com *ic = &sc->sc_ic;
590 	int i;
591 
592 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
593 		struct zyd_tx_data *data = &sc->tx_data[i];
594 
595 		if (data->xfer != NULL) {
596 			usbd_free_xfer(data->xfer);
597 			data->xfer = NULL;
598 		}
599 		if (data->ni != NULL) {
600 			ieee80211_release_node(ic, data->ni);
601 			data->ni = NULL;
602 		}
603 	}
604 }
605 
606 int
607 zyd_alloc_rx_list(struct zyd_softc *sc)
608 {
609 	int i, error;
610 
611 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
612 		struct zyd_rx_data *data = &sc->rx_data[i];
613 
614 		data->sc = sc;	/* backpointer for callbacks */
615 
616 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
617 		if (data->xfer == NULL) {
618 			printf("%s: could not allocate rx xfer\n",
619 			    sc->sc_dev.dv_xname);
620 			error = ENOMEM;
621 			goto fail;
622 		}
623 		data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ);
624 		if (data->buf == NULL) {
625 			printf("%s: could not allocate rx buffer\n",
626 			    sc->sc_dev.dv_xname);
627 			error = ENOMEM;
628 			goto fail;
629 		}
630 	}
631 	return 0;
632 
633 fail:	zyd_free_rx_list(sc);
634 	return error;
635 }
636 
637 void
638 zyd_free_rx_list(struct zyd_softc *sc)
639 {
640 	int i;
641 
642 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
643 		struct zyd_rx_data *data = &sc->rx_data[i];
644 
645 		if (data->xfer != NULL) {
646 			usbd_free_xfer(data->xfer);
647 			data->xfer = NULL;
648 		}
649 	}
650 }
651 
652 struct ieee80211_node *
653 zyd_node_alloc(struct ieee80211com *ic)
654 {
655 	return malloc(sizeof (struct zyd_node), M_DEVBUF, M_NOWAIT | M_ZERO);
656 }
657 
658 int
659 zyd_media_change(struct ifnet *ifp)
660 {
661 	int error;
662 
663 	error = ieee80211_media_change(ifp);
664 	if (error != ENETRESET)
665 		return error;
666 
667 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
668 		zyd_init(ifp);
669 
670 	return 0;
671 }
672 
673 /*
674  * This function is called periodically (every 200ms) during scanning to
675  * switch from one channel to another.
676  */
677 void
678 zyd_next_scan(void *arg)
679 {
680 	struct zyd_softc *sc = arg;
681 	struct ieee80211com *ic = &sc->sc_ic;
682 	struct ifnet *ifp = &ic->ic_if;
683 
684 	if (ic->ic_state == IEEE80211_S_SCAN)
685 		ieee80211_next_scan(ifp);
686 }
687 
688 void
689 zyd_task(void *arg)
690 {
691 	struct zyd_softc *sc = arg;
692 	struct ieee80211com *ic = &sc->sc_ic;
693 	enum ieee80211_state ostate;
694 
695 	ostate = ic->ic_state;
696 
697 	switch (sc->sc_state) {
698 	case IEEE80211_S_INIT:
699 		if (ostate == IEEE80211_S_RUN) {
700 			/* turn link LED off */
701 			zyd_set_led(sc, ZYD_LED1, 0);
702 
703 			/* stop data LED from blinking */
704 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
705 		}
706 		break;
707 
708 	case IEEE80211_S_SCAN:
709 		zyd_set_chan(sc, ic->ic_bss->ni_chan);
710 		timeout_add_msec(&sc->scan_to, 200);
711 		break;
712 
713 	case IEEE80211_S_AUTH:
714 	case IEEE80211_S_ASSOC:
715 		zyd_set_chan(sc, ic->ic_bss->ni_chan);
716 		break;
717 
718 	case IEEE80211_S_RUN:
719 	{
720 		struct ieee80211_node *ni = ic->ic_bss;
721 
722 		zyd_set_chan(sc, ni->ni_chan);
723 
724 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
725 			/* turn link LED on */
726 			zyd_set_led(sc, ZYD_LED1, 1);
727 
728 			/* make data LED blink upon Tx */
729 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
730 
731 			zyd_set_bssid(sc, ni->ni_bssid);
732 		}
733 
734 		if (ic->ic_opmode == IEEE80211_M_STA) {
735 			/* fake a join to init the tx rate */
736 			zyd_newassoc(ic, ni, 1);
737 		}
738 
739 		/* start automatic rate control timer */
740 		if (ic->ic_fixed_rate == -1)
741 			timeout_add_sec(&sc->amrr_to, 1);
742 
743 		break;
744 	}
745 	}
746 
747 	sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
748 }
749 
750 int
751 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
752 {
753 	struct zyd_softc *sc = ic->ic_softc;
754 
755 	usb_rem_task(sc->sc_udev, &sc->sc_task);
756 	timeout_del(&sc->scan_to);
757 	timeout_del(&sc->amrr_to);
758 
759 	/* do it in a process context */
760 	sc->sc_state = nstate;
761 	sc->sc_arg = arg;
762 	usb_add_task(sc->sc_udev, &sc->sc_task);
763 
764 	return 0;
765 }
766 
767 int
768 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
769     void *odata, int olen, u_int flags)
770 {
771 	usbd_xfer_handle xfer;
772 	struct zyd_cmd cmd;
773 	uint16_t xferflags;
774 	usbd_status error;
775 	int s;
776 
777 	if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
778 		return ENOMEM;
779 
780 	cmd.code = htole16(code);
781 	bcopy(idata, cmd.data, ilen);
782 
783 	xferflags = USBD_FORCE_SHORT_XFER;
784 	if (!(flags & ZYD_CMD_FLAG_READ))
785 		xferflags |= USBD_SYNCHRONOUS;
786 	else
787 		s = splusb();
788 
789 	sc->odata = odata;
790 	sc->olen  = olen;
791 
792 	usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
793 	    sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
794 	error = usbd_transfer(xfer);
795 	if (error != USBD_IN_PROGRESS && error != 0) {
796 		if (flags & ZYD_CMD_FLAG_READ)
797 			splx(s);
798 		printf("%s: could not send command (error=%s)\n",
799 		    sc->sc_dev.dv_xname, usbd_errstr(error));
800 		(void)usbd_free_xfer(xfer);
801 		return EIO;
802 	}
803 	if (!(flags & ZYD_CMD_FLAG_READ)) {
804 		(void)usbd_free_xfer(xfer);
805 		return 0;	/* write: don't wait for reply */
806 	}
807 	/* wait at most one second for command reply */
808 	error = tsleep(sc, PCATCH, "zydcmd", hz);
809 	sc->odata = NULL;	/* in case answer is received too late */
810 	splx(s);
811 
812 	(void)usbd_free_xfer(xfer);
813 	return error;
814 }
815 
816 int
817 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
818 {
819 	struct zyd_pair tmp;
820 	int error;
821 
822 	reg = htole16(reg);
823 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof reg, &tmp, sizeof tmp,
824 	    ZYD_CMD_FLAG_READ);
825 	if (error == 0)
826 		*val = letoh16(tmp.val);
827 	return error;
828 }
829 
830 int
831 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
832 {
833 	struct zyd_pair tmp[2];
834 	uint16_t regs[2];
835 	int error;
836 
837 	regs[0] = htole16(ZYD_REG32_HI(reg));
838 	regs[1] = htole16(ZYD_REG32_LO(reg));
839 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp,
840 	    ZYD_CMD_FLAG_READ);
841 	if (error == 0)
842 		*val = letoh16(tmp[0].val) << 16 | letoh16(tmp[1].val);
843 	return error;
844 }
845 
846 int
847 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
848 {
849 	struct zyd_pair pair;
850 
851 	pair.reg = htole16(reg);
852 	pair.val = htole16(val);
853 
854 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0);
855 }
856 
857 int
858 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
859 {
860 	struct zyd_pair pair[2];
861 
862 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
863 	pair[0].val = htole16(val >> 16);
864 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
865 	pair[1].val = htole16(val & 0xffff);
866 
867 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0);
868 }
869 
870 int
871 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
872 {
873 	struct zyd_rf *rf = &sc->sc_rf;
874 	struct zyd_rfwrite req;
875 	uint16_t cr203;
876 	int i;
877 
878 	(void)zyd_read16(sc, ZYD_CR203, &cr203);
879 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
880 
881 	req.code  = htole16(2);
882 	req.width = htole16(rf->width);
883 	for (i = 0; i < rf->width; i++) {
884 		req.bit[i] = htole16(cr203);
885 		if (val & (1 << (rf->width - 1 - i)))
886 			req.bit[i] |= htole16(ZYD_RF_DATA);
887 	}
888 	return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
889 }
890 
891 void
892 zyd_lock_phy(struct zyd_softc *sc)
893 {
894 	uint32_t tmp;
895 
896 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
897 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
898 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
899 }
900 
901 void
902 zyd_unlock_phy(struct zyd_softc *sc)
903 {
904 	uint32_t tmp;
905 
906 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
907 	tmp |= ZYD_UNLOCK_PHY_REGS;
908 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
909 }
910 
911 /*
912  * RFMD RF methods.
913  */
914 int
915 zyd_rfmd_init(struct zyd_rf *rf)
916 {
917 	struct zyd_softc *sc = rf->rf_sc;
918 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
919 	static const uint32_t rfini[] = ZYD_RFMD_RF;
920 	int i, error;
921 
922 	/* init RF-dependent PHY registers */
923 	for (i = 0; i < nitems(phyini); i++) {
924 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
925 		if (error != 0)
926 			return error;
927 	}
928 
929 	/* init RFMD radio */
930 	for (i = 0; i < nitems(rfini); i++) {
931 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
932 			return error;
933 	}
934 	return 0;
935 }
936 
937 int
938 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
939 {
940 	struct zyd_softc *sc = rf->rf_sc;
941 
942 	(void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
943 	(void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
944 
945 	return 0;
946 }
947 
948 int
949 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
950 {
951 	struct zyd_softc *sc = rf->rf_sc;
952 	static const struct {
953 		uint32_t	r1, r2;
954 	} rfprog[] = ZYD_RFMD_CHANTABLE;
955 
956 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
957 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
958 
959 	return 0;
960 }
961 
962 /*
963  * AL2230 RF methods.
964  */
965 int
966 zyd_al2230_init(struct zyd_rf *rf)
967 {
968 	struct zyd_softc *sc = rf->rf_sc;
969 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
970 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
971 	static const uint32_t rfini[] = ZYD_AL2230_RF;
972 	int i, error;
973 
974 	/* init RF-dependent PHY registers */
975 	for (i = 0; i < nitems(phyini); i++) {
976 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
977 		if (error != 0)
978 			return error;
979 	}
980 	if (sc->rf_rev == ZYD_RF_AL2230S) {
981 		for (i = 0; i < nitems(phy2230s); i++) {
982 			error = zyd_write16(sc, phy2230s[i].reg,
983 			    phy2230s[i].val);
984 			if (error != 0)
985 				return error;
986 		}
987 	}
988 	/* init AL2230 radio */
989 	for (i = 0; i < nitems(rfini); i++) {
990 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
991 			return error;
992 	}
993 	return 0;
994 }
995 
996 int
997 zyd_al2230_init_b(struct zyd_rf *rf)
998 {
999 	struct zyd_softc *sc = rf->rf_sc;
1000 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1001 	static const uint32_t rfini[] = ZYD_AL2230_RF_B;
1002 	int i, error;
1003 
1004 	/* init RF-dependent PHY registers */
1005 	for (i = 0; i < nitems(phyini); i++) {
1006 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1007 		if (error != 0)
1008 			return error;
1009 	}
1010 
1011 	/* init AL2230 radio */
1012 	for (i = 0; i < nitems(rfini); i++) {
1013 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1014 			return error;
1015 	}
1016 	return 0;
1017 }
1018 
1019 int
1020 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1021 {
1022 	struct zyd_softc *sc = rf->rf_sc;
1023 	int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1024 
1025 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1026 	(void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1027 
1028 	return 0;
1029 }
1030 
1031 int
1032 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1033 {
1034 	struct zyd_softc *sc = rf->rf_sc;
1035 	static const struct {
1036 		uint32_t	r1, r2, r3;
1037 	} rfprog[] = ZYD_AL2230_CHANTABLE;
1038 
1039 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1040 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1041 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1042 
1043 	(void)zyd_write16(sc, ZYD_CR138, 0x28);
1044 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
1045 
1046 	return 0;
1047 }
1048 
1049 /*
1050  * AL7230B RF methods.
1051  */
1052 int
1053 zyd_al7230B_init(struct zyd_rf *rf)
1054 {
1055 	struct zyd_softc *sc = rf->rf_sc;
1056 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1057 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1058 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1059 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1060 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1061 	int i, error;
1062 
1063 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
1064 
1065 	/* init RF-dependent PHY registers, part one */
1066 	for (i = 0; i < nitems(phyini_1); i++) {
1067 		error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1068 		if (error != 0)
1069 			return error;
1070 	}
1071 	/* init AL7230B radio, part one */
1072 	for (i = 0; i < nitems(rfini_1); i++) {
1073 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1074 			return error;
1075 	}
1076 	/* init RF-dependent PHY registers, part two */
1077 	for (i = 0; i < nitems(phyini_2); i++) {
1078 		error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1079 		if (error != 0)
1080 			return error;
1081 	}
1082 	/* init AL7230B radio, part two */
1083 	for (i = 0; i < nitems(rfini_2); i++) {
1084 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1085 			return error;
1086 	}
1087 	/* init RF-dependent PHY registers, part three */
1088 	for (i = 0; i < nitems(phyini_3); i++) {
1089 		error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1090 		if (error != 0)
1091 			return error;
1092 	}
1093 
1094 	return 0;
1095 }
1096 
1097 int
1098 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1099 {
1100 	struct zyd_softc *sc = rf->rf_sc;
1101 
1102 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1103 	(void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1104 
1105 	return 0;
1106 }
1107 
1108 int
1109 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1110 {
1111 	struct zyd_softc *sc = rf->rf_sc;
1112 	static const struct {
1113 		uint32_t	r1, r2;
1114 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
1115 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1116 	int i, error;
1117 
1118 	(void)zyd_write16(sc, ZYD_CR240, 0x57);
1119 	(void)zyd_write16(sc, ZYD_CR251, 0x2f);
1120 
1121 	for (i = 0; i < nitems(rfsc); i++) {
1122 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1123 			return error;
1124 	}
1125 
1126 	(void)zyd_write16(sc, ZYD_CR128, 0x14);
1127 	(void)zyd_write16(sc, ZYD_CR129, 0x12);
1128 	(void)zyd_write16(sc, ZYD_CR130, 0x10);
1129 	(void)zyd_write16(sc, ZYD_CR38,  0x38);
1130 	(void)zyd_write16(sc, ZYD_CR136, 0xdf);
1131 
1132 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1133 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1134 	(void)zyd_rfwrite(sc, 0x3c9000);
1135 
1136 	(void)zyd_write16(sc, ZYD_CR251, 0x3f);
1137 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
1138 	(void)zyd_write16(sc, ZYD_CR240, 0x08);
1139 
1140 	return 0;
1141 }
1142 
1143 /*
1144  * AL2210 RF methods.
1145  */
1146 int
1147 zyd_al2210_init(struct zyd_rf *rf)
1148 {
1149 	struct zyd_softc *sc = rf->rf_sc;
1150 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1151 	static const uint32_t rfini[] = ZYD_AL2210_RF;
1152 	uint32_t tmp;
1153 	int i, error;
1154 
1155 	(void)zyd_write32(sc, ZYD_CR18, 2);
1156 
1157 	/* init RF-dependent PHY registers */
1158 	for (i = 0; i < nitems(phyini); i++) {
1159 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1160 		if (error != 0)
1161 			return error;
1162 	}
1163 	/* init AL2210 radio */
1164 	for (i = 0; i < nitems(rfini); i++) {
1165 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1166 			return error;
1167 	}
1168 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1169 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1170 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1171 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1172 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1173 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1174 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1175 	(void)zyd_write32(sc, ZYD_CR18, 3);
1176 
1177 	return 0;
1178 }
1179 
1180 int
1181 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1182 {
1183 	/* vendor driver does nothing for this RF chip */
1184 
1185 	return 0;
1186 }
1187 
1188 int
1189 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1190 {
1191 	struct zyd_softc *sc = rf->rf_sc;
1192 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1193 	uint32_t tmp;
1194 
1195 	(void)zyd_write32(sc, ZYD_CR18, 2);
1196 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1197 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1198 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1199 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1200 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1201 
1202 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1203 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
1204 
1205 	/* actually set the channel */
1206 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
1207 
1208 	(void)zyd_write32(sc, ZYD_CR18, 3);
1209 
1210 	return 0;
1211 }
1212 
1213 /*
1214  * GCT RF methods.
1215  */
1216 int
1217 zyd_gct_init(struct zyd_rf *rf)
1218 {
1219 	struct zyd_softc *sc = rf->rf_sc;
1220 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1221 	static const uint32_t rfini[] = ZYD_GCT_RF;
1222 	int i, error;
1223 
1224 	/* init RF-dependent PHY registers */
1225 	for (i = 0; i < nitems(phyini); i++) {
1226 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1227 		if (error != 0)
1228 			return error;
1229 	}
1230 	/* init cgt radio */
1231 	for (i = 0; i < nitems(rfini); i++) {
1232 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1233 			return error;
1234 	}
1235 	return 0;
1236 }
1237 
1238 int
1239 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1240 {
1241 	/* vendor driver does nothing for this RF chip */
1242 
1243 	return 0;
1244 }
1245 
1246 int
1247 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1248 {
1249 	struct zyd_softc *sc = rf->rf_sc;
1250 	static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1251 
1252 	(void)zyd_rfwrite(sc, 0x1c0000);
1253 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
1254 	(void)zyd_rfwrite(sc, 0x1c0008);
1255 
1256 	return 0;
1257 }
1258 
1259 /*
1260  * Maxim RF methods.
1261  */
1262 int
1263 zyd_maxim_init(struct zyd_rf *rf)
1264 {
1265 	struct zyd_softc *sc = rf->rf_sc;
1266 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1267 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
1268 	uint16_t tmp;
1269 	int i, error;
1270 
1271 	/* init RF-dependent PHY registers */
1272 	for (i = 0; i < nitems(phyini); i++) {
1273 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1274 		if (error != 0)
1275 			return error;
1276 	}
1277 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1278 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1279 
1280 	/* init maxim radio */
1281 	for (i = 0; i < nitems(rfini); i++) {
1282 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1283 			return error;
1284 	}
1285 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1286 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1287 
1288 	return 0;
1289 }
1290 
1291 int
1292 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1293 {
1294 	/* vendor driver does nothing for this RF chip */
1295 
1296 	return 0;
1297 }
1298 
1299 int
1300 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1301 {
1302 	struct zyd_softc *sc = rf->rf_sc;
1303 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1304 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
1305 	static const struct {
1306 		uint32_t	r1, r2;
1307 	} rfprog[] = ZYD_MAXIM_CHANTABLE;
1308 	uint16_t tmp;
1309 	int i, error;
1310 
1311 	/*
1312 	 * Do the same as we do when initializing it, except for the channel
1313 	 * values coming from the two channel tables.
1314 	 */
1315 
1316 	/* init RF-dependent PHY registers */
1317 	for (i = 0; i < nitems(phyini); i++) {
1318 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1319 		if (error != 0)
1320 			return error;
1321 	}
1322 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1323 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1324 
1325 	/* first two values taken from the chantables */
1326 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1327 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1328 
1329 	/* init maxim radio - skipping the two first values */
1330 	for (i = 2; i < nitems(rfini); i++) {
1331 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1332 			return error;
1333 	}
1334 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1335 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1336 
1337 	return 0;
1338 }
1339 
1340 /*
1341  * Maxim2 RF methods.
1342  */
1343 int
1344 zyd_maxim2_init(struct zyd_rf *rf)
1345 {
1346 	struct zyd_softc *sc = rf->rf_sc;
1347 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1348 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1349 	uint16_t tmp;
1350 	int i, error;
1351 
1352 	/* init RF-dependent PHY registers */
1353 	for (i = 0; i < nitems(phyini); i++) {
1354 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1355 		if (error != 0)
1356 			return error;
1357 	}
1358 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1359 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1360 
1361 	/* init maxim2 radio */
1362 	for (i = 0; i < nitems(rfini); i++) {
1363 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1364 			return error;
1365 	}
1366 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1367 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1368 
1369 	return 0;
1370 }
1371 
1372 int
1373 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1374 {
1375 	/* vendor driver does nothing for this RF chip */
1376 
1377 	return 0;
1378 }
1379 
1380 int
1381 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1382 {
1383 	struct zyd_softc *sc = rf->rf_sc;
1384 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1385 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1386 	static const struct {
1387 		uint32_t	r1, r2;
1388 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
1389 	uint16_t tmp;
1390 	int i, error;
1391 
1392 	/*
1393 	 * Do the same as we do when initializing it, except for the channel
1394 	 * values coming from the two channel tables.
1395 	 */
1396 
1397 	/* init RF-dependent PHY registers */
1398 	for (i = 0; i < nitems(phyini); i++) {
1399 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1400 		if (error != 0)
1401 			return error;
1402 	}
1403 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1404 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1405 
1406 	/* first two values taken from the chantables */
1407 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1408 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1409 
1410 	/* init maxim2 radio - skipping the two first values */
1411 	for (i = 2; i < nitems(rfini); i++) {
1412 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1413 			return error;
1414 	}
1415 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
1416 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1417 
1418 	return 0;
1419 }
1420 
1421 int
1422 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1423 {
1424 	struct zyd_rf *rf = &sc->sc_rf;
1425 
1426 	rf->rf_sc = sc;
1427 
1428 	switch (type) {
1429 	case ZYD_RF_RFMD:
1430 		rf->init         = zyd_rfmd_init;
1431 		rf->switch_radio = zyd_rfmd_switch_radio;
1432 		rf->set_channel  = zyd_rfmd_set_channel;
1433 		rf->width        = 24;	/* 24-bit RF values */
1434 		break;
1435 	case ZYD_RF_AL2230:
1436 	case ZYD_RF_AL2230S:
1437 		if (sc->mac_rev == ZYD_ZD1211B)
1438 			rf->init = zyd_al2230_init_b;
1439 		else
1440 			rf->init = zyd_al2230_init;
1441 		rf->switch_radio = zyd_al2230_switch_radio;
1442 		rf->set_channel  = zyd_al2230_set_channel;
1443 		rf->width        = 24;	/* 24-bit RF values */
1444 		break;
1445 	case ZYD_RF_AL7230B:
1446 		rf->init         = zyd_al7230B_init;
1447 		rf->switch_radio = zyd_al7230B_switch_radio;
1448 		rf->set_channel  = zyd_al7230B_set_channel;
1449 		rf->width        = 24;	/* 24-bit RF values */
1450 		break;
1451 	case ZYD_RF_AL2210:
1452 		rf->init         = zyd_al2210_init;
1453 		rf->switch_radio = zyd_al2210_switch_radio;
1454 		rf->set_channel  = zyd_al2210_set_channel;
1455 		rf->width        = 24;	/* 24-bit RF values */
1456 		break;
1457 	case ZYD_RF_GCT:
1458 		rf->init         = zyd_gct_init;
1459 		rf->switch_radio = zyd_gct_switch_radio;
1460 		rf->set_channel  = zyd_gct_set_channel;
1461 		rf->width        = 21;	/* 21-bit RF values */
1462 		break;
1463 	case ZYD_RF_MAXIM_NEW:
1464 		rf->init         = zyd_maxim_init;
1465 		rf->switch_radio = zyd_maxim_switch_radio;
1466 		rf->set_channel  = zyd_maxim_set_channel;
1467 		rf->width        = 18;	/* 18-bit RF values */
1468 		break;
1469 	case ZYD_RF_MAXIM_NEW2:
1470 		rf->init         = zyd_maxim2_init;
1471 		rf->switch_radio = zyd_maxim2_switch_radio;
1472 		rf->set_channel  = zyd_maxim2_set_channel;
1473 		rf->width        = 18;	/* 18-bit RF values */
1474 		break;
1475 	default:
1476 		printf("%s: sorry, radio \"%s\" is not supported yet\n",
1477 		    sc->sc_dev.dv_xname, zyd_rf_name(type));
1478 		return EINVAL;
1479 	}
1480 	return 0;
1481 }
1482 
1483 const char *
1484 zyd_rf_name(uint8_t type)
1485 {
1486 	static const char * const zyd_rfs[] = {
1487 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1488 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1489 		"AL2230S", "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1490 		"PHILIPS"
1491 	};
1492 	return zyd_rfs[(type > 15) ? 0 : type];
1493 }
1494 
1495 int
1496 zyd_hw_init(struct zyd_softc *sc)
1497 {
1498 	struct zyd_rf *rf = &sc->sc_rf;
1499 	const struct zyd_phy_pair *phyp;
1500 	uint32_t tmp;
1501 	int error;
1502 
1503 	/* specify that the plug and play is finished */
1504 	(void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1505 
1506 	(void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1507 	DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1508 
1509 	/* retrieve firmware revision number */
1510 	(void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1511 
1512 	(void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1513 	(void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1514 
1515 	/* disable interrupts */
1516 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1517 
1518 	/* PHY init */
1519 	zyd_lock_phy(sc);
1520 	phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1521 	for (; phyp->reg != 0; phyp++) {
1522 		if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1523 			goto fail;
1524 	}
1525 	if (sc->fix_cr157) {
1526 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
1527 			(void)zyd_write32(sc, ZYD_CR157, tmp >> 8);
1528 	}
1529 	zyd_unlock_phy(sc);
1530 
1531 	/* HMAC init */
1532 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1533 	zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1534 
1535 	if (sc->mac_rev == ZYD_ZD1211) {
1536 		zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1537 	} else {
1538 		zyd_write32(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1539 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1540 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1541 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1542 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1543 		zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1544 		zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003c);
1545 		zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1546 	}
1547 
1548 	zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1549 	zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1550 	zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1551 	zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1552 	zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1553 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1554 	zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1555 	zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1556 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1557 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1558 	zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1559 	zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1560 	zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1561 	zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1562 	zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1563 	zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1564 	zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1565 
1566 	/* RF chip init */
1567 	zyd_lock_phy(sc);
1568 	error = (*rf->init)(rf);
1569 	zyd_unlock_phy(sc);
1570 	if (error != 0) {
1571 		printf("%s: radio initialization failed\n",
1572 		    sc->sc_dev.dv_xname);
1573 		goto fail;
1574 	}
1575 
1576 	/* init beacon interval to 100ms */
1577 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1578 		goto fail;
1579 
1580 fail:	return error;
1581 }
1582 
1583 int
1584 zyd_read_eeprom(struct zyd_softc *sc)
1585 {
1586 	struct ieee80211com *ic = &sc->sc_ic;
1587 	uint32_t tmp;
1588 	uint16_t val;
1589 	int i;
1590 
1591 	/* read MAC address */
1592 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1593 	ic->ic_myaddr[0] = tmp & 0xff;
1594 	ic->ic_myaddr[1] = tmp >>  8;
1595 	ic->ic_myaddr[2] = tmp >> 16;
1596 	ic->ic_myaddr[3] = tmp >> 24;
1597 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1598 	ic->ic_myaddr[4] = tmp & 0xff;
1599 	ic->ic_myaddr[5] = tmp >>  8;
1600 
1601 	(void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1602 	sc->rf_rev    = tmp & 0x0f;
1603 	sc->fix_cr47  = (tmp >> 8 ) & 0x01;
1604 	sc->fix_cr157 = (tmp >> 13) & 0x01;
1605 	sc->pa_rev    = (tmp >> 16) & 0x0f;
1606 
1607 	/* read regulatory domain (currently unused) */
1608 	(void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1609 	sc->regdomain = tmp >> 16;
1610 	DPRINTF(("regulatory domain %x\n", sc->regdomain));
1611 
1612 	/* read Tx power calibration tables */
1613 	for (i = 0; i < 7; i++) {
1614 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1615 		sc->pwr_cal[i * 2] = val >> 8;
1616 		sc->pwr_cal[i * 2 + 1] = val & 0xff;
1617 
1618 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1619 		sc->pwr_int[i * 2] = val >> 8;
1620 		sc->pwr_int[i * 2 + 1] = val & 0xff;
1621 
1622 		(void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1623 		sc->ofdm36_cal[i * 2] = val >> 8;
1624 		sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1625 
1626 		(void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1627 		sc->ofdm48_cal[i * 2] = val >> 8;
1628 		sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1629 
1630 		(void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1631 		sc->ofdm54_cal[i * 2] = val >> 8;
1632 		sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1633 	}
1634 	return 0;
1635 }
1636 
1637 void
1638 zyd_set_multi(struct zyd_softc *sc)
1639 {
1640 	struct arpcom *ac = &sc->sc_ic.ic_ac;
1641 	struct ifnet *ifp = &ac->ac_if;
1642 	struct ether_multi *enm;
1643 	struct ether_multistep step;
1644 	uint32_t lo, hi;
1645 	uint8_t bit;
1646 
1647 	if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
1648 		lo = hi = 0xffffffff;
1649 		goto done;
1650 	}
1651 	lo = hi = 0;
1652 	ETHER_FIRST_MULTI(step, ac, enm);
1653 	while (enm != NULL) {
1654 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1655 			ifp->if_flags |= IFF_ALLMULTI;
1656 			lo = hi = 0xffffffff;
1657 			goto done;
1658 		}
1659 		bit = enm->enm_addrlo[5] >> 2;
1660 		if (bit < 32)
1661 			lo |= 1 << bit;
1662 		else
1663 			hi |= 1 << (bit - 32);
1664 		ETHER_NEXT_MULTI(step, enm);
1665 	}
1666 
1667 done:
1668 	hi |= 1 << 31;	/* make sure the broadcast bit is set */
1669 	zyd_write32(sc, ZYD_MAC_GHTBL, lo);
1670 	zyd_write32(sc, ZYD_MAC_GHTBH, hi);
1671 }
1672 
1673 void
1674 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1675 {
1676 	uint32_t tmp;
1677 
1678 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1679 	(void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1680 
1681 	tmp = addr[5] << 8 | addr[4];
1682 	(void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1683 }
1684 
1685 void
1686 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1687 {
1688 	uint32_t tmp;
1689 
1690 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1691 	(void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1692 
1693 	tmp = addr[5] << 8 | addr[4];
1694 	(void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1695 }
1696 
1697 int
1698 zyd_switch_radio(struct zyd_softc *sc, int on)
1699 {
1700 	struct zyd_rf *rf = &sc->sc_rf;
1701 	int error;
1702 
1703 	zyd_lock_phy(sc);
1704 	error = (*rf->switch_radio)(rf, on);
1705 	zyd_unlock_phy(sc);
1706 
1707 	return error;
1708 }
1709 
1710 void
1711 zyd_set_led(struct zyd_softc *sc, int which, int on)
1712 {
1713 	uint32_t tmp;
1714 
1715 	(void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1716 	tmp &= ~which;
1717 	if (on)
1718 		tmp |= which;
1719 	(void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1720 }
1721 
1722 int
1723 zyd_set_rxfilter(struct zyd_softc *sc)
1724 {
1725 	uint32_t rxfilter;
1726 
1727 	switch (sc->sc_ic.ic_opmode) {
1728 	case IEEE80211_M_STA:
1729 		rxfilter = ZYD_FILTER_BSS;
1730 		break;
1731 #ifndef IEEE80211_STA_ONLY
1732 	case IEEE80211_M_IBSS:
1733 	case IEEE80211_M_HOSTAP:
1734 		rxfilter = ZYD_FILTER_HOSTAP;
1735 		break;
1736 #endif
1737 	case IEEE80211_M_MONITOR:
1738 		rxfilter = ZYD_FILTER_MONITOR;
1739 		break;
1740 	default:
1741 		/* should not get there */
1742 		return EINVAL;
1743 	}
1744 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1745 }
1746 
1747 void
1748 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1749 {
1750 	struct ieee80211com *ic = &sc->sc_ic;
1751 	struct zyd_rf *rf = &sc->sc_rf;
1752 	uint32_t tmp;
1753 	u_int chan;
1754 
1755 	chan = ieee80211_chan2ieee(ic, c);
1756 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1757 		return;
1758 
1759 	zyd_lock_phy(sc);
1760 
1761 	(*rf->set_channel)(rf, chan);
1762 
1763 	/* update Tx power */
1764 	(void)zyd_write16(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1765 
1766 	if (sc->mac_rev == ZYD_ZD1211B) {
1767 		(void)zyd_write16(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1768 		(void)zyd_write16(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1769 		(void)zyd_write16(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1770 
1771 		(void)zyd_write16(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1772 
1773 		(void)zyd_write16(sc, ZYD_CR69, 0x28);
1774 		(void)zyd_write16(sc, ZYD_CR69, 0x2a);
1775 	}
1776 
1777 	if (sc->fix_cr47) {
1778 		/* set CCK baseband gain from EEPROM */
1779 		if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
1780 			(void)zyd_write16(sc, ZYD_CR47, tmp & 0xff);
1781 	}
1782 
1783 	(void)zyd_write32(sc, ZYD_CR_CONFIG_PHILIPS, 0);
1784 
1785 	zyd_unlock_phy(sc);
1786 }
1787 
1788 int
1789 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1790 {
1791 	/* XXX this is probably broken.. */
1792 	(void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1793 	(void)zyd_write32(sc, ZYD_CR_PRE_TBTT,        bintval - 1);
1794 	(void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL,    bintval);
1795 
1796 	return 0;
1797 }
1798 
1799 uint8_t
1800 zyd_plcp_signal(int rate)
1801 {
1802 	switch (rate) {
1803 	/* CCK rates (returned values are device-dependent) */
1804 	case 2:		return 0x0;
1805 	case 4:		return 0x1;
1806 	case 11:	return 0x2;
1807 	case 22:	return 0x3;
1808 
1809 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1810 	case 12:	return 0xb;
1811 	case 18:	return 0xf;
1812 	case 24:	return 0xa;
1813 	case 36:	return 0xe;
1814 	case 48:	return 0x9;
1815 	case 72:	return 0xd;
1816 	case 96:	return 0x8;
1817 	case 108:	return 0xc;
1818 
1819 	/* unsupported rates (should not get there) */
1820 	default:	return 0xff;
1821 	}
1822 }
1823 
1824 void
1825 zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1826 {
1827 	struct zyd_softc *sc = (struct zyd_softc *)priv;
1828 	const struct zyd_cmd *cmd;
1829 	uint32_t len;
1830 
1831 	if (status != USBD_NORMAL_COMPLETION) {
1832 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1833 			return;
1834 
1835 		if (status == USBD_STALLED) {
1836 			usbd_clear_endpoint_stall_async(
1837 			    sc->zyd_ep[ZYD_ENDPT_IIN]);
1838 		}
1839 		return;
1840 	}
1841 
1842 	cmd = (const struct zyd_cmd *)sc->ibuf;
1843 
1844 	if (letoh16(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
1845 		struct zyd_notif_retry *retry =
1846 		    (struct zyd_notif_retry *)cmd->data;
1847 		struct ieee80211com *ic = &sc->sc_ic;
1848 		struct ifnet *ifp = &ic->ic_if;
1849 		struct ieee80211_node *ni;
1850 
1851 		DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
1852 		    letoh16(retry->rate), ether_sprintf(retry->macaddr),
1853 		    letoh16(retry->count) & 0xff, letoh16(retry->count)));
1854 
1855 		/*
1856 		 * Find the node to which the packet was sent and update its
1857 		 * retry statistics.  In BSS mode, this node is the AP we're
1858 		 * associated to so no lookup is actually needed.
1859 		 */
1860 		if (ic->ic_opmode != IEEE80211_M_STA) {
1861 			ni = ieee80211_find_node(ic, retry->macaddr);
1862 			if (ni == NULL)
1863 				return;	/* just ignore */
1864 		} else
1865 			ni = ic->ic_bss;
1866 
1867 		((struct zyd_node *)ni)->amn.amn_retrycnt++;
1868 
1869 		if (letoh16(retry->count) & 0x100)
1870 			ifp->if_oerrors++;	/* too many retries */
1871 
1872 	} else if (letoh16(cmd->code) == ZYD_NOTIF_IORD) {
1873 		if (letoh16(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1874 			return;	/* HMAC interrupt */
1875 
1876 		if (sc->odata == NULL)
1877 			return;	/* unexpected IORD notification */
1878 
1879 		/* copy answer into caller-supplied buffer */
1880 		usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1881 		bcopy(cmd->data, sc->odata, sc->olen);
1882 
1883 		wakeup(sc);	/* wakeup caller */
1884 
1885 	} else {
1886 		printf("%s: unknown notification %x\n", sc->sc_dev.dv_xname,
1887 		    letoh16(cmd->code));
1888 	}
1889 }
1890 
1891 void
1892 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
1893 {
1894 	struct ieee80211com *ic = &sc->sc_ic;
1895 	struct ifnet *ifp = &ic->ic_if;
1896 	struct ieee80211_node *ni;
1897 	struct ieee80211_frame *wh;
1898 	struct ieee80211_rxinfo rxi;
1899 	const struct zyd_plcphdr *plcp;
1900 	const struct zyd_rx_stat *stat;
1901 	struct mbuf *m;
1902 	int s;
1903 
1904 	if (len < ZYD_MIN_FRAGSZ) {
1905 		DPRINTFN(2, ("frame too short (length=%d)\n", len));
1906 		ifp->if_ierrors++;
1907 		return;
1908 	}
1909 
1910 	plcp = (const struct zyd_plcphdr *)buf;
1911 	stat = (const struct zyd_rx_stat *)(buf + len - sizeof (*stat));
1912 
1913 	if (stat->flags & ZYD_RX_ERROR) {
1914 		DPRINTF(("%s: RX status indicated error (%x)\n",
1915 		    sc->sc_dev.dv_xname, stat->flags));
1916 		ifp->if_ierrors++;
1917 		return;
1918 	}
1919 
1920 	/* compute actual frame length */
1921 	len -= sizeof (*plcp) - sizeof (*stat) - IEEE80211_CRC_LEN;
1922 
1923 	if (len > MCLBYTES) {
1924 		DPRINTFN(2, ("frame too large (length=%d)\n", len));
1925 		ifp->if_ierrors++;
1926 		return;
1927 	}
1928 
1929 	/* allocate a mbuf to store the frame */
1930 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1931 	if (m == NULL) {
1932 		ifp->if_ierrors++;
1933 		return;
1934 	}
1935 	if (len > MHLEN) {
1936 		MCLGET(m, M_DONTWAIT);
1937 		if (!(m->m_flags & M_EXT)) {
1938 			ifp->if_ierrors++;
1939 			m_freem(m);
1940 			return;
1941 		}
1942 	}
1943 	bcopy(plcp + 1, mtod(m, caddr_t), len);
1944 	m->m_pkthdr.rcvif = ifp;
1945 	m->m_pkthdr.len = m->m_len = len;
1946 
1947 #if NBPFILTER > 0
1948 	if (sc->sc_drvbpf != NULL) {
1949 		struct mbuf mb;
1950 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
1951 		static const uint8_t rates[] = {
1952 			/* reverse function of zyd_plcp_signal() */
1953 			2, 4, 11, 22, 0, 0, 0, 0,
1954 			96, 48, 24, 12, 108, 72, 36, 18
1955 		};
1956 
1957 		tap->wr_flags = 0;
1958 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1959 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1960 		tap->wr_rssi = stat->rssi;
1961 		tap->wr_rate = rates[plcp->signal & 0xf];
1962 
1963 		mb.m_data = (caddr_t)tap;
1964 		mb.m_len = sc->sc_rxtap_len;
1965 		mb.m_next = m;
1966 		mb.m_nextpkt = NULL;
1967 		mb.m_type = 0;
1968 		mb.m_flags = 0;
1969 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
1970 	}
1971 #endif
1972 
1973 	s = splnet();
1974 	wh = mtod(m, struct ieee80211_frame *);
1975 	ni = ieee80211_find_rxnode(ic, wh);
1976 	rxi.rxi_flags = 0;
1977 	rxi.rxi_rssi = stat->rssi;
1978 	rxi.rxi_tstamp = 0;	/* unused */
1979 	ieee80211_input(ifp, m, ni, &rxi);
1980 
1981 	/* node is no longer needed */
1982 	ieee80211_release_node(ic, ni);
1983 
1984 	splx(s);
1985 }
1986 
1987 void
1988 zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1989 {
1990 	struct zyd_rx_data *data = priv;
1991 	struct zyd_softc *sc = data->sc;
1992 	struct ieee80211com *ic = &sc->sc_ic;
1993 	struct ifnet *ifp = &ic->ic_if;
1994 	const struct zyd_rx_desc *desc;
1995 	int len;
1996 
1997 	if (status != USBD_NORMAL_COMPLETION) {
1998 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1999 			return;
2000 
2001 		if (status == USBD_STALLED)
2002 			usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2003 
2004 		goto skip;
2005 	}
2006 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2007 
2008 	if (len < ZYD_MIN_RXBUFSZ) {
2009 		DPRINTFN(2, ("xfer too short (length=%d)\n", len));
2010 		ifp->if_ierrors++;
2011 		goto skip;
2012 	}
2013 
2014 	desc = (const struct zyd_rx_desc *)
2015 	    (data->buf + len - sizeof (struct zyd_rx_desc));
2016 
2017 	if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2018 		const uint8_t *p = data->buf, *end = p + len;
2019 		int i;
2020 
2021 		DPRINTFN(3, ("received multi-frame transfer\n"));
2022 
2023 		for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2024 			const uint16_t len = UGETW(desc->len[i]);
2025 
2026 			if (len == 0 || p + len > end)
2027 				break;
2028 
2029 			zyd_rx_data(sc, p, len);
2030 			/* next frame is aligned on a 32-bit boundary */
2031 			p += (len + 3) & ~3;
2032 		}
2033 	} else {
2034 		DPRINTFN(3, ("received single-frame transfer\n"));
2035 
2036 		zyd_rx_data(sc, data->buf, len);
2037 	}
2038 
2039 skip:	/* setup a new transfer */
2040 	usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data, NULL,
2041 	    ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2042 	    USBD_NO_TIMEOUT, zyd_rxeof);
2043 	(void)usbd_transfer(xfer);
2044 }
2045 
2046 void
2047 zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
2048 {
2049 	struct zyd_tx_data *data = priv;
2050 	struct zyd_softc *sc = data->sc;
2051 	struct ieee80211com *ic = &sc->sc_ic;
2052 	struct ifnet *ifp = &ic->ic_if;
2053 	int s;
2054 
2055 	if (status != USBD_NORMAL_COMPLETION) {
2056 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2057 			return;
2058 
2059 		printf("%s: could not transmit buffer: %s\n",
2060 		    sc->sc_dev.dv_xname, usbd_errstr(status));
2061 
2062 		if (status == USBD_STALLED) {
2063 			usbd_clear_endpoint_stall_async(
2064 			    sc->zyd_ep[ZYD_ENDPT_BOUT]);
2065 		}
2066 		ifp->if_oerrors++;
2067 		return;
2068 	}
2069 
2070 	s = splnet();
2071 
2072 	/* update rate control statistics */
2073 	((struct zyd_node *)data->ni)->amn.amn_txcnt++;
2074 
2075 	ieee80211_release_node(ic, data->ni);
2076 	data->ni = NULL;
2077 
2078 	sc->tx_queued--;
2079 	ifp->if_opackets++;
2080 
2081 	sc->tx_timer = 0;
2082 	ifp->if_flags &= ~IFF_OACTIVE;
2083 	zyd_start(ifp);
2084 
2085 	splx(s);
2086 }
2087 
2088 int
2089 zyd_tx(struct zyd_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2090 {
2091 	struct ieee80211com *ic = &sc->sc_ic;
2092 	struct ifnet *ifp = &ic->ic_if;
2093 	struct zyd_tx_desc *desc;
2094 	struct zyd_tx_data *data;
2095 	struct ieee80211_frame *wh;
2096 	struct ieee80211_key *k;
2097 	int xferlen, totlen, rate;
2098 	uint16_t pktlen;
2099 	usbd_status error;
2100 
2101 	wh = mtod(m, struct ieee80211_frame *);
2102 
2103 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2104 		k = ieee80211_get_txkey(ic, wh, ni);
2105 		if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
2106 			return ENOBUFS;
2107 		wh = mtod(m, struct ieee80211_frame *);
2108 	}
2109 
2110 	/* pickup a rate */
2111 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2112 	    ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2113 	     IEEE80211_FC0_TYPE_MGT)) {
2114 		/* mgmt/multicast frames are sent at the lowest avail. rate */
2115 		rate = ni->ni_rates.rs_rates[0];
2116 	} else if (ic->ic_fixed_rate != -1) {
2117 		rate = ic->ic_sup_rates[ic->ic_curmode].
2118 		    rs_rates[ic->ic_fixed_rate];
2119 	} else
2120 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2121 	rate &= IEEE80211_RATE_VAL;
2122 	if (rate == 0)	/* XXX should not happen */
2123 		rate = 2;
2124 
2125 	data = &sc->tx_data[0];
2126 	desc = (struct zyd_tx_desc *)data->buf;
2127 
2128 	data->ni = ni;
2129 
2130 	xferlen = sizeof (struct zyd_tx_desc) + m->m_pkthdr.len;
2131 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
2132 
2133 	/* fill Tx descriptor */
2134 	desc->len = htole16(totlen);
2135 
2136 	desc->flags = ZYD_TX_FLAG_BACKOFF;
2137 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2138 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
2139 		if (totlen > ic->ic_rtsthreshold) {
2140 			desc->flags |= ZYD_TX_FLAG_RTS;
2141 		} else if (ZYD_RATE_IS_OFDM(rate) &&
2142 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
2143 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2144 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2145 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2146 				desc->flags |= ZYD_TX_FLAG_RTS;
2147 		}
2148 	} else
2149 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
2150 
2151 	if ((wh->i_fc[0] &
2152 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2153 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2154 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2155 
2156 	desc->phy = zyd_plcp_signal(rate);
2157 	if (ZYD_RATE_IS_OFDM(rate)) {
2158 		desc->phy |= ZYD_TX_PHY_OFDM;
2159 		if (ic->ic_curmode == IEEE80211_MODE_11A)
2160 			desc->phy |= ZYD_TX_PHY_5GHZ;
2161 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2162 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2163 
2164 	/* actual transmit length (XXX why +10?) */
2165 	pktlen = sizeof (struct zyd_tx_desc) + 10;
2166 	if (sc->mac_rev == ZYD_ZD1211)
2167 		pktlen += totlen;
2168 	desc->pktlen = htole16(pktlen);
2169 
2170 	desc->plcp_length = htole16((16 * totlen + rate - 1) / rate);
2171 	desc->plcp_service = 0;
2172 	if (rate == 22) {
2173 		const int remainder = (16 * totlen) % 22;
2174 		if (remainder != 0 && remainder < 7)
2175 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
2176 	}
2177 
2178 #if NBPFILTER > 0
2179 	if (sc->sc_drvbpf != NULL) {
2180 		struct mbuf mb;
2181 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2182 
2183 		tap->wt_flags = 0;
2184 		tap->wt_rate = rate;
2185 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
2186 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
2187 
2188 		mb.m_data = (caddr_t)tap;
2189 		mb.m_len = sc->sc_txtap_len;
2190 		mb.m_next = m;
2191 		mb.m_nextpkt = NULL;
2192 		mb.m_type = 0;
2193 		mb.m_flags = 0;
2194 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
2195 	}
2196 #endif
2197 
2198 	m_copydata(m, 0, m->m_pkthdr.len,
2199 	    data->buf + sizeof (struct zyd_tx_desc));
2200 
2201 	DPRINTFN(10, ("%s: sending data frame len=%u rate=%u xferlen=%u\n",
2202 	    sc->sc_dev.dv_xname, m->m_pkthdr.len, rate, xferlen));
2203 
2204 	m_freem(m);	/* mbuf no longer needed */
2205 
2206 	usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
2207 	    data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
2208 	    ZYD_TX_TIMEOUT, zyd_txeof);
2209 	error = usbd_transfer(data->xfer);
2210 	if (error != USBD_IN_PROGRESS && error != 0) {
2211 		ifp->if_oerrors++;
2212 		return EIO;
2213 	}
2214 	sc->tx_queued++;
2215 
2216 	return 0;
2217 }
2218 
2219 void
2220 zyd_start(struct ifnet *ifp)
2221 {
2222 	struct zyd_softc *sc = ifp->if_softc;
2223 	struct ieee80211com *ic = &sc->sc_ic;
2224 	struct ieee80211_node *ni;
2225 	struct mbuf *m;
2226 
2227 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2228 		return;
2229 
2230 	for (;;) {
2231 		if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2232 			ifp->if_flags |= IFF_OACTIVE;
2233 			break;
2234 		}
2235 		/* send pending management frames first */
2236 		IF_DEQUEUE(&ic->ic_mgtq, m);
2237 		if (m != NULL) {
2238 			ni = (void *)m->m_pkthdr.rcvif;
2239 			goto sendit;
2240 		}
2241 		if (ic->ic_state != IEEE80211_S_RUN)
2242 			break;
2243 
2244 		/* encapsulate and send data frames */
2245 		IFQ_DEQUEUE(&ifp->if_snd, m);
2246 		if (m == NULL)
2247 			break;
2248 #if NBPFILTER > 0
2249 		if (ifp->if_bpf != NULL)
2250 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
2251 #endif
2252 		if ((m = ieee80211_encap(ifp, m, &ni)) == NULL)
2253 			continue;
2254 sendit:
2255 #if NBPFILTER > 0
2256 		if (ic->ic_rawbpf != NULL)
2257 			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
2258 #endif
2259 		if (zyd_tx(sc, m, ni) != 0) {
2260 			ieee80211_release_node(ic, ni);
2261 			ifp->if_oerrors++;
2262 			continue;
2263 		}
2264 
2265 		sc->tx_timer = 5;
2266 		ifp->if_timer = 1;
2267 	}
2268 }
2269 
2270 void
2271 zyd_watchdog(struct ifnet *ifp)
2272 {
2273 	struct zyd_softc *sc = ifp->if_softc;
2274 
2275 	ifp->if_timer = 0;
2276 
2277 	if (sc->tx_timer > 0) {
2278 		if (--sc->tx_timer == 0) {
2279 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
2280 			/* zyd_init(ifp); XXX needs a process context ? */
2281 			ifp->if_oerrors++;
2282 			return;
2283 		}
2284 		ifp->if_timer = 1;
2285 	}
2286 
2287 	ieee80211_watchdog(ifp);
2288 }
2289 
2290 int
2291 zyd_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2292 {
2293 	struct zyd_softc *sc = ifp->if_softc;
2294 	struct ieee80211com *ic = &sc->sc_ic;
2295 	struct ifaddr *ifa;
2296 	struct ifreq *ifr;
2297 	int s, error = 0;
2298 
2299 	s = splnet();
2300 
2301 	switch (cmd) {
2302 	case SIOCSIFADDR:
2303 		ifa = (struct ifaddr *)data;
2304 		ifp->if_flags |= IFF_UP;
2305 #ifdef INET
2306 		if (ifa->ifa_addr->sa_family == AF_INET)
2307 			arp_ifinit(&ic->ic_ac, ifa);
2308 #endif
2309 		/* FALLTHROUGH */
2310 	case SIOCSIFFLAGS:
2311 		if (ifp->if_flags & IFF_UP) {
2312 			/*
2313 			 * If only the PROMISC or ALLMULTI flag changes, then
2314 			 * don't do a full re-init of the chip, just update
2315 			 * the Rx filter.
2316 			 */
2317 			if ((ifp->if_flags & IFF_RUNNING) &&
2318 			    ((ifp->if_flags ^ sc->sc_if_flags) &
2319 			     (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
2320 				zyd_set_multi(sc);
2321 			} else {
2322 				if (!(ifp->if_flags & IFF_RUNNING))
2323 					zyd_init(ifp);
2324 			}
2325 		} else {
2326 			if (ifp->if_flags & IFF_RUNNING)
2327 				zyd_stop(ifp, 1);
2328 		}
2329 		sc->sc_if_flags = ifp->if_flags;
2330 		break;
2331 
2332 	case SIOCADDMULTI:
2333 	case SIOCDELMULTI:
2334 		ifr = (struct ifreq *)data;
2335 		error = (cmd == SIOCADDMULTI) ?
2336 		    ether_addmulti(ifr, &ic->ic_ac) :
2337 		    ether_delmulti(ifr, &ic->ic_ac);
2338 		if (error == ENETRESET) {
2339 			if (ifp->if_flags & IFF_RUNNING)
2340 				zyd_set_multi(sc);
2341 			error = 0;
2342 		}
2343 		break;
2344 
2345 	case SIOCS80211CHANNEL:
2346 		/*
2347 		 * This allows for fast channel switching in monitor mode
2348 		 * (used by kismet). In IBSS mode, we must explicitly reset
2349 		 * the interface to generate a new beacon frame.
2350 		 */
2351 		error = ieee80211_ioctl(ifp, cmd, data);
2352 		if (error == ENETRESET &&
2353 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
2354 			zyd_set_chan(sc, ic->ic_ibss_chan);
2355 			error = 0;
2356 		}
2357 		break;
2358 
2359 	default:
2360 		error = ieee80211_ioctl(ifp, cmd, data);
2361 	}
2362 
2363 	if (error == ENETRESET) {
2364 		if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
2365 		    (IFF_RUNNING | IFF_UP))
2366 			zyd_init(ifp);
2367 		error = 0;
2368 	}
2369 
2370 	splx(s);
2371 
2372 	return error;
2373 }
2374 
2375 int
2376 zyd_init(struct ifnet *ifp)
2377 {
2378 	struct zyd_softc *sc = ifp->if_softc;
2379 	struct ieee80211com *ic = &sc->sc_ic;
2380 	int i, error;
2381 
2382 	zyd_stop(ifp, 0);
2383 
2384 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2385 	DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2386 	zyd_set_macaddr(sc, ic->ic_myaddr);
2387 
2388 	/* we'll do software WEP decryption for now */
2389 	DPRINTF(("setting encryption type\n"));
2390 	error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2391 	if (error != 0)
2392 		return error;
2393 
2394 	/* promiscuous mode */
2395 	(void)zyd_write32(sc, ZYD_MAC_SNIFFER,
2396 	    (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
2397 
2398 	(void)zyd_set_rxfilter(sc);
2399 
2400 	/* switch radio transmitter ON */
2401 	(void)zyd_switch_radio(sc, 1);
2402 
2403 	/* set basic rates */
2404 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2405 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
2406 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2407 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
2408 	else	/* assumes 802.11b/g */
2409 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
2410 
2411 	/* set mandatory rates */
2412 	if (ic->ic_curmode == IEEE80211_MODE_11B)
2413 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
2414 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
2415 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
2416 	else	/* assumes 802.11b/g */
2417 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
2418 
2419 	/* set default BSS channel */
2420 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2421 	zyd_set_chan(sc, ic->ic_bss->ni_chan);
2422 
2423 	/* enable interrupts */
2424 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2425 
2426 	/*
2427 	 * Allocate Tx and Rx xfer queues.
2428 	 */
2429 	if ((error = zyd_alloc_tx_list(sc)) != 0) {
2430 		printf("%s: could not allocate Tx list\n",
2431 		    sc->sc_dev.dv_xname);
2432 		goto fail;
2433 	}
2434 	if ((error = zyd_alloc_rx_list(sc)) != 0) {
2435 		printf("%s: could not allocate Rx list\n",
2436 		    sc->sc_dev.dv_xname);
2437 		goto fail;
2438 	}
2439 
2440 	/*
2441 	 * Start up the receive pipe.
2442 	 */
2443 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
2444 		struct zyd_rx_data *data = &sc->rx_data[i];
2445 
2446 		usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data,
2447 		    NULL, ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
2448 		    USBD_NO_TIMEOUT, zyd_rxeof);
2449 		error = usbd_transfer(data->xfer);
2450 		if (error != USBD_IN_PROGRESS && error != 0) {
2451 			printf("%s: could not queue Rx transfer\n",
2452 			    sc->sc_dev.dv_xname);
2453 			goto fail;
2454 		}
2455 	}
2456 
2457 	ifp->if_flags &= ~IFF_OACTIVE;
2458 	ifp->if_flags |= IFF_RUNNING;
2459 
2460 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2461 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2462 	else
2463 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2464 
2465 	return 0;
2466 
2467 fail:	zyd_stop(ifp, 1);
2468 	return error;
2469 }
2470 
2471 void
2472 zyd_stop(struct ifnet *ifp, int disable)
2473 {
2474 	struct zyd_softc *sc = ifp->if_softc;
2475 	struct ieee80211com *ic = &sc->sc_ic;
2476 
2477 	sc->tx_timer = 0;
2478 	ifp->if_timer = 0;
2479 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2480 
2481 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
2482 
2483 	/* switch radio transmitter OFF */
2484 	(void)zyd_switch_radio(sc, 0);
2485 
2486 	/* disable Rx */
2487 	(void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
2488 
2489 	/* disable interrupts */
2490 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
2491 
2492 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
2493 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
2494 
2495 	zyd_free_rx_list(sc);
2496 	zyd_free_tx_list(sc);
2497 }
2498 
2499 int
2500 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
2501 {
2502 	usb_device_request_t req;
2503 	uint16_t addr;
2504 	uint8_t stat;
2505 
2506 	DPRINTF(("firmware size=%d\n", size));
2507 
2508 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2509 	req.bRequest = ZYD_DOWNLOADREQ;
2510 	USETW(req.wIndex, 0);
2511 
2512 	addr = ZYD_FIRMWARE_START_ADDR;
2513 	while (size > 0) {
2514 		const int mlen = min(size, 4096);
2515 
2516 		DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
2517 		    addr));
2518 
2519 		USETW(req.wValue, addr);
2520 		USETW(req.wLength, mlen);
2521 		if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
2522 			return EIO;
2523 
2524 		addr += mlen / 2;
2525 		fw   += mlen;
2526 		size -= mlen;
2527 	}
2528 
2529 	/* check whether the upload succeeded */
2530 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2531 	req.bRequest = ZYD_DOWNLOADSTS;
2532 	USETW(req.wValue, 0);
2533 	USETW(req.wIndex, 0);
2534 	USETW(req.wLength, sizeof stat);
2535 	if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
2536 		return EIO;
2537 
2538 	return (stat & 0x80) ? EIO : 0;
2539 }
2540 
2541 void
2542 zyd_iter_func(void *arg, struct ieee80211_node *ni)
2543 {
2544 	struct zyd_softc *sc = arg;
2545 	struct zyd_node *zn = (struct zyd_node *)ni;
2546 
2547 	ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
2548 }
2549 
2550 void
2551 zyd_amrr_timeout(void *arg)
2552 {
2553 	struct zyd_softc *sc = arg;
2554 	struct ieee80211com *ic = &sc->sc_ic;
2555 	int s;
2556 
2557 	s = splnet();
2558 	if (ic->ic_opmode == IEEE80211_M_STA)
2559 		zyd_iter_func(sc, ic->ic_bss);
2560 	else
2561 		ieee80211_iterate_nodes(ic, zyd_iter_func, sc);
2562 	splx(s);
2563 
2564 	timeout_add_sec(&sc->amrr_to, 1);
2565 }
2566 
2567 void
2568 zyd_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
2569 {
2570 	struct zyd_softc *sc = ic->ic_softc;
2571 	int i;
2572 
2573 	ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
2574 
2575 	/* set rate to some reasonable initial value */
2576 	for (i = ni->ni_rates.rs_nrates - 1;
2577 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2578 	     i--);
2579 	ni->ni_txrate = i;
2580 }
2581 
2582 int
2583 zyd_activate(struct device *self, int act)
2584 {
2585 	struct zyd_softc *sc = (struct zyd_softc *)self;
2586 
2587 	switch (act) {
2588 	case DVACT_DEACTIVATE:
2589 		usbd_deactivate(sc->sc_udev);
2590 		break;
2591 	}
2592 	return 0;
2593 }
2594