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