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