xref: /openbsd-src/sys/dev/usb/if_uath.c (revision e326c169eb08d20d122e7717c7356b69c46b34a5)
1 /*	$OpenBSD: if_uath.c,v 1.6 2006/09/18 16:20:20 damien Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006
5  *	Damien Bergamini <damien.bergamini@free.fr>
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  * Driver for Atheros AR5005UG/AR5005UX chipsets.
22  * http://www.atheros.com/pt/bulletins/AR5005UGBulletin.pdf
23  * http://www.atheros.com/pt/bulletins/AR5005UXBulletin.pdf
24  *
25  * IMPORTANT NOTICE:
26  * This driver was written without any documentation or support from Atheros
27  * Communications. It is based on a black-box analysis of the Windows binary
28  * driver. It handles both pre and post-firmware devices.
29  */
30 
31 #include "bpfilter.h"
32 
33 #include <sys/param.h>
34 #include <sys/sockio.h>
35 #include <sys/sysctl.h>
36 #include <sys/mbuf.h>
37 #include <sys/kernel.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/timeout.h>
42 #include <sys/conf.h>
43 #include <sys/device.h>
44 
45 #include <machine/bus.h>
46 #include <machine/endian.h>
47 #include <machine/intr.h>
48 
49 #if NBPFILTER > 0
50 #include <net/bpf.h>
51 #endif
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/if_types.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/if_ether.h>
62 #include <netinet/ip.h>
63 
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_amrr.h>
66 #include <net80211/ieee80211_radiotap.h>
67 
68 #include <dev/rndvar.h>
69 #include <crypto/arc4.h>
70 
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include <dev/usb/usbdevs.h>
75 
76 #include <dev/usb/if_uathreg.h>
77 #include <dev/usb/if_uathvar.h>
78 
79 #ifdef USB_DEBUG
80 #define UATH_DEBUG
81 #endif
82 
83 #ifdef UATH_DEBUG
84 #define DPRINTF(x)	do { if (uath_debug) logprintf x; } while (0)
85 #define DPRINTFN(n, x)	do { if (uath_debug >= (n)) logprintf x; } while (0)
86 int uath_debug = 1;
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n, x)
90 #endif
91 
92 /*
93  * Various supported device vendors/products
94  * UB51: AR5005UG 802.11b/g, UB52: AR5005UX 802.11a/b/g
95  */
96 #define UATH_DEV(v, p, f)						\
97 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p },			\
98 	    (f) },							\
99 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p##_NF }, 		\
100 	    (f) | UATH_FLAG_PRE_FIRMWARE }
101 static const struct uath_type {
102 	struct usb_devno	dev;
103 	unsigned int		flags;
104 #define UATH_FLAG_PRE_FIRMWARE	(1 << 0)
105 #define UATH_FLAG_DUAL_BAND_RF	(1 << 1)
106 } uath_devs[] = {
107 	/* Atheros Communications */
108 	UATH_DEV(ATHEROS, AR5523, 0),
109 	UATH_DEV(ATHEROS2, AR5523_1, 0),
110 	UATH_DEV(ATHEROS2, AR5523_2, 0),
111 	UATH_DEV(ATHEROS2, AR5523_3, UATH_FLAG_DUAL_BAND_RF),
112 
113 	/* Conceptronic */
114 	UATH_DEV(CONCEPTRONIC, AR5523_1, 0),
115 	UATH_DEV(CONCEPTRONIC, AR5523_2, UATH_FLAG_DUAL_BAND_RF),
116 
117 	/* D-Link */
118 	UATH_DEV(DLINK, DWLAG122, UATH_FLAG_DUAL_BAND_RF),
119 	UATH_DEV(DLINK, DWLAG132, UATH_FLAG_DUAL_BAND_RF),
120 	UATH_DEV(DLINK, DWLG132, 0),
121 
122 	/* Gigaset */
123 	UATH_DEV(GIGASET, SMCWUSBTG, 0),
124 	UATH_DEV(GIGASET, AR5523, 0),
125 
126 	/* Global Sun Technology */
127 	UATH_DEV(GLOBALSUN, AR5523_1, 0),
128 	UATH_DEV(GLOBALSUN, AR5523_2, UATH_FLAG_DUAL_BAND_RF),
129 
130 	/* Netgear */
131 	UATH_DEV(NETGEAR, WG111U, UATH_FLAG_DUAL_BAND_RF),
132 	UATH_DEV(NETGEAR3, WG111T, 0),
133 	UATH_DEV(NETGEAR3, WPN111, 0),
134 
135 	/* U-MEDIA Communications */
136 	UATH_DEV(UMEDIA, AR5523_1, 0),
137 	UATH_DEV(UMEDIA, AR5523_2, UATH_FLAG_DUAL_BAND_RF),
138 	UATH_DEV(UMEDIA, TEW444UBEU, 0),
139 
140 	/* Wistron NeWeb */
141 	UATH_DEV(WISTRONNEWEB, AR5523_1, 0),
142 	UATH_DEV(WISTRONNEWEB, AR5523_2, UATH_FLAG_DUAL_BAND_RF),
143 
144 	/* Z-Com */
145 	UATH_DEV(ZCOM, AR5523, 0)
146 };
147 #define uath_lookup(v, p)	\
148 	((struct uath_type *)usb_lookup(uath_devs, v, p))
149 
150 Static void	uath_attachhook(void *);
151 Static int	uath_open_pipes(struct uath_softc *);
152 Static void	uath_close_pipes(struct uath_softc *);
153 Static int	uath_alloc_tx_data_list(struct uath_softc *);
154 Static void	uath_free_tx_data_list(struct uath_softc *);
155 Static int	uath_alloc_rx_data_list(struct uath_softc *);
156 Static void	uath_free_rx_data_list(struct uath_softc *);
157 Static int	uath_alloc_tx_cmd_list(struct uath_softc *);
158 Static void	uath_free_tx_cmd_list(struct uath_softc *);
159 Static int	uath_alloc_rx_cmd_list(struct uath_softc *);
160 Static void	uath_free_rx_cmd_list(struct uath_softc *);
161 Static int	uath_media_change(struct ifnet *);
162 Static void	uath_stat(void *);
163 Static void	uath_next_scan(void *);
164 Static void	uath_task(void *);
165 Static int	uath_newstate(struct ieee80211com *, enum ieee80211_state,
166 		    int);
167 #ifdef UATH_DEBUG
168 Static void	uath_dump_cmd(const uint8_t *, int, char);
169 #endif
170 Static int	uath_cmd(struct uath_softc *, uint32_t, const void *, int,
171 		    void *, int);
172 Static int	uath_cmd_write(struct uath_softc *, uint32_t, const void *,
173 		    int, int);
174 Static int	uath_cmd_read(struct uath_softc *, uint32_t, const void *,
175 		    int, void *, int);
176 Static int	uath_write_reg(struct uath_softc *, uint32_t, uint32_t);
177 Static int	uath_write_multi(struct uath_softc *, uint32_t, const void *,
178 		    int);
179 Static int	uath_read_reg(struct uath_softc *, uint32_t, uint32_t *);
180 Static int	uath_read_eeprom(struct uath_softc *, uint32_t, void *);
181 Static void	uath_cmd_rxeof(usbd_xfer_handle, usbd_private_handle,
182 		    usbd_status);
183 Static void	uath_data_rxeof(usbd_xfer_handle, usbd_private_handle,
184 		    usbd_status);
185 Static void	uath_data_txeof(usbd_xfer_handle, usbd_private_handle,
186 		    usbd_status);
187 Static int	uath_tx_null(struct uath_softc *);
188 Static int	uath_tx_data(struct uath_softc *, struct mbuf *,
189 		    struct ieee80211_node *);
190 Static void	uath_start(struct ifnet *);
191 Static void	uath_watchdog(struct ifnet *);
192 Static int	uath_ioctl(struct ifnet *, u_long, caddr_t);
193 Static int	uath_query_eeprom(struct uath_softc *);
194 Static int	uath_reset(struct uath_softc *);
195 Static int	uath_reset_tx_queues(struct uath_softc *);
196 Static int	uath_wme_init(struct uath_softc *);
197 Static int	uath_set_chan(struct uath_softc *, struct ieee80211_channel *);
198 Static int	uath_set_key(struct uath_softc *,
199 		    const struct ieee80211_wepkey *, int);
200 Static int	uath_set_keys(struct uath_softc *);
201 Static int	uath_set_rates(struct uath_softc *,
202 		    const struct ieee80211_rateset *);
203 Static int	uath_set_rxfilter(struct uath_softc *, uint32_t, uint32_t);
204 Static int	uath_set_led(struct uath_softc *, int, int);
205 Static int	uath_switch_channel(struct uath_softc *,
206 		    struct ieee80211_channel *);
207 Static int	uath_init(struct ifnet *);
208 Static void	uath_stop(struct ifnet *, int);
209 Static int	uath_loadfirmware(struct uath_softc *, const u_char *, int);
210 Static int	uath_activate(device_ptr_t, enum devact);
211 
212 /*
213  * Supported rates for 802.11b/g modes (in 500Kbps unit).
214  */
215 static const struct ieee80211_rateset uath_rateset_11b =
216 	{ 4, { 2, 4, 11, 22 } };
217 
218 static const struct ieee80211_rateset uath_rateset_11g =
219 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
220 
221 USB_DECLARE_DRIVER(uath);
222 
223 USB_MATCH(uath)
224 {
225 	USB_MATCH_START(uath, uaa);
226 
227 	if (uaa->iface != NULL)
228 		return UMATCH_NONE;
229 
230 	return (uath_lookup(uaa->vendor, uaa->product) != NULL) ?
231 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
232 }
233 
234 Static void
235 uath_attachhook(void *xsc)
236 {
237 	struct uath_softc *sc = xsc;
238 	u_char *fw;
239 	size_t size;
240 	int error;
241 
242 	if ((error = loadfirmware("uath-ar5523", &fw, &size)) != 0) {
243 		printf("%s: could not read firmware (error=%d)\n",
244 		    USBDEVNAME(sc->sc_dev), error);
245 		return;
246 	}
247 
248 	if ((error = uath_loadfirmware(sc, fw, size)) != 0) {
249 		printf("%s: could not load firmware (error=%s)\n",
250 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
251 	}
252 
253 	free(fw, M_DEVBUF);
254 }
255 
256 USB_ATTACH(uath)
257 {
258 	USB_ATTACH_START(uath, sc, uaa);
259 	struct ieee80211com *ic = &sc->sc_ic;
260 	struct ifnet *ifp = &ic->ic_if;
261 	usbd_status error;
262 	char *devinfop;
263 	int i;
264 
265 	sc->sc_udev = uaa->device;
266 
267 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
268 	USB_ATTACH_SETUP;
269 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
270 	usbd_devinfo_free(devinfop);
271 
272 	sc->sc_flags = uath_lookup(uaa->vendor, uaa->product)->flags;
273 
274 	if (usbd_set_config_no(sc->sc_udev, UATH_CONFIG_NO, 0) != 0) {
275 		printf("%s: could not set configuration no\n",
276 		    USBDEVNAME(sc->sc_dev));
277 		USB_ATTACH_ERROR_RETURN;
278 	}
279 
280 	/* get the first interface handle */
281 	error = usbd_device2interface_handle(sc->sc_udev, UATH_IFACE_INDEX,
282 	    &sc->sc_iface);
283 	if (error != 0) {
284 		printf("%s: could not get interface handle\n",
285 		    USBDEVNAME(sc->sc_dev));
286 		USB_ATTACH_ERROR_RETURN;
287 	}
288 
289 	/*
290 	 * We must open the pipes early because they're used to upload the
291 	 * firmware (pre-firmware devices) or to send firmware commands.
292 	 */
293 	if (uath_open_pipes(sc) != 0) {
294 		printf("%s: could not open pipes\n", USBDEVNAME(sc->sc_dev));
295 		USB_ATTACH_ERROR_RETURN;
296 	}
297 
298 	if (sc->sc_flags & UATH_FLAG_PRE_FIRMWARE) {
299 		if (rootvp == NULL)
300 			mountroothook_establish(uath_attachhook, sc);
301 		else
302 			uath_attachhook(sc);
303 		USB_ATTACH_SUCCESS_RETURN;
304 	}
305 
306 	/*
307 	 * Only post-firmware devices here.
308 	 */
309 	usb_init_task(&sc->sc_task, uath_task, sc);
310 	timeout_set(&sc->scan_to, uath_next_scan, sc);
311 	timeout_set(&sc->stat_to, uath_stat, sc);
312 
313 	/*
314 	 * Allocate xfers for firmware commands.
315 	 */
316 	if (uath_alloc_tx_cmd_list(sc) != 0) {
317 		printf("%s: could not allocate Tx command list\n",
318 		    USBDEVNAME(sc->sc_dev));
319 		goto fail1;
320 	}
321 	if (uath_alloc_rx_cmd_list(sc) != 0) {
322 		printf("%s: could not allocate Rx command list\n",
323 		    USBDEVNAME(sc->sc_dev));
324 		goto fail2;
325 	}
326 
327 	/*
328 	 * Queue Rx command xfers.
329 	 */
330 	for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++) {
331 		struct uath_rx_cmd *cmd = &sc->rx_cmd[i];
332 
333 		usbd_setup_xfer(cmd->xfer, sc->cmd_rx_pipe, cmd, cmd->buf,
334 		    UATH_MAX_RXCMDSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
335 		    USBD_NO_TIMEOUT, uath_cmd_rxeof);
336 		error = usbd_transfer(cmd->xfer);
337 		if (error != USBD_IN_PROGRESS && error != 0) {
338 			printf("%s: could not queue Rx command xfer\n",
339 			    USBDEVNAME(sc->sc_dev));
340 			goto fail3;
341 		}
342 	}
343 
344 	/*
345 	 * We're now ready to send/receive firmware commands.
346 	 */
347 	if (uath_reset(sc) != 0) {
348 		printf("%s: could not initialize adapter\n",
349 		    USBDEVNAME(sc->sc_dev));
350 		goto fail3;
351 	}
352 	if (uath_query_eeprom(sc) != 0) {
353 		printf("%s: could not read EEPROM\n", USBDEVNAME(sc->sc_dev));
354 		goto fail3;
355 	}
356 
357 	printf("%s: MAC/BBP AR5523, RF AR%c112, address %s\n",
358 	    USBDEVNAME(sc->sc_dev),
359 	    (sc->sc_flags & UATH_FLAG_DUAL_BAND_RF) ? '5': '2',
360 	    ether_sprintf(ic->ic_myaddr));
361 
362 	/*
363 	 * Allocate xfers for Tx/Rx data pipes.
364 	 */
365 	if (uath_alloc_tx_data_list(sc) != 0) {
366 		printf("%s: could not allocate Tx data list\n",
367 		    USBDEVNAME(sc->sc_dev));
368 		goto fail3;
369 	}
370 	if (uath_alloc_rx_data_list(sc) != 0) {
371 		printf("%s: could not allocate Rx data list\n",
372 		    USBDEVNAME(sc->sc_dev));
373 		goto fail4;
374 	}
375 
376 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
377 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
378 	ic->ic_state = IEEE80211_S_INIT;
379 
380 	/* set device capabilities */
381 	ic->ic_caps =
382 	    IEEE80211_C_TXPMGT |	/* tx power management */
383 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
384 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
385 	    IEEE80211_C_WEP;		/* h/w WEP */
386 
387 	/* set supported .11b and .11g rates */
388 	ic->ic_sup_rates[IEEE80211_MODE_11B] = uath_rateset_11b;
389 	ic->ic_sup_rates[IEEE80211_MODE_11G] = uath_rateset_11g;
390 
391 	/* set supported .11b and .11g channels (1 through 14) */
392 	for (i = 1; i <= 14; i++) {
393 		ic->ic_channels[i].ic_freq =
394 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
395 		ic->ic_channels[i].ic_flags =
396 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
397 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
398 	}
399 
400 	ifp->if_softc = sc;
401 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
402 	ifp->if_init = uath_init;
403 	ifp->if_ioctl = uath_ioctl;
404 	ifp->if_start = uath_start;
405 	ifp->if_watchdog = uath_watchdog;
406 	IFQ_SET_READY(&ifp->if_snd);
407 	memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
408 
409 	if_attach(ifp);
410 	ieee80211_ifattach(ifp);
411 
412 	/* override state transition machine */
413 	sc->sc_newstate = ic->ic_newstate;
414 	ic->ic_newstate = uath_newstate;
415 	ieee80211_media_init(ifp, uath_media_change, ieee80211_media_status);
416 
417 #if NBPFILTER > 0
418 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
419 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
420 
421 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
422 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
423 	sc->sc_rxtap.wr_ihdr.it_present = htole32(UATH_RX_RADIOTAP_PRESENT);
424 
425 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
426 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
427 	sc->sc_txtap.wt_ihdr.it_present = htole32(UATH_TX_RADIOTAP_PRESENT);
428 #endif
429 
430 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
431 	    USBDEV(sc->sc_dev));
432 
433 	USB_ATTACH_SUCCESS_RETURN;
434 
435 fail4:	uath_free_tx_data_list(sc);
436 fail3:	uath_free_rx_cmd_list(sc);
437 fail2:	uath_free_tx_cmd_list(sc);
438 fail1:	uath_close_pipes(sc);
439 
440 	USB_ATTACH_ERROR_RETURN;
441 }
442 
443 USB_DETACH(uath)
444 {
445 	USB_DETACH_START(uath, sc);
446 	struct ifnet *ifp = &sc->sc_ic.ic_if;
447 	int s;
448 
449 	s = splusb();
450 
451 	if (sc->sc_flags & UATH_FLAG_PRE_FIRMWARE) {
452 		uath_close_pipes(sc);
453 		splx(s);
454 		return 0;
455 	}
456 
457 	/* post-firmware device */
458 
459 	usb_rem_task(sc->sc_udev, &sc->sc_task);
460 	timeout_del(&sc->scan_to);
461 	timeout_del(&sc->stat_to);
462 
463 	/* abort and free xfers */
464 	uath_free_tx_data_list(sc);
465 	uath_free_rx_data_list(sc);
466 	uath_free_tx_cmd_list(sc);
467 	uath_free_rx_cmd_list(sc);
468 
469 	/* close Tx/Rx pipes */
470 	uath_close_pipes(sc);
471 
472 	ieee80211_ifdetach(ifp);	/* free all nodes */
473 	if_detach(ifp);
474 
475 	splx(s);
476 
477 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
478 	    USBDEV(sc->sc_dev));
479 
480 	return 0;
481 }
482 
483 Static int
484 uath_open_pipes(struct uath_softc *sc)
485 {
486 	int error;
487 
488 	/*
489 	 * XXX pipes numbers are hardcoded because we don't have any way
490 	 * to distinguish the data pipes from the firmware command pipes
491 	 * (both are bulk pipes) using the endpoints descriptors.
492 	 */
493 	error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
494 	    &sc->cmd_tx_pipe);
495 	if (error != 0) {
496 		printf("%s: could not open Tx command pipe: %s\n",
497 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
498 		goto fail;
499 	}
500 
501 	error = usbd_open_pipe(sc->sc_iface, 0x02, USBD_EXCLUSIVE_USE,
502 	    &sc->data_tx_pipe);
503 	if (error != 0) {
504 		printf("%s: could not open Tx data pipe: %s\n",
505 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
506 		goto fail;
507 	}
508 
509 	error = usbd_open_pipe(sc->sc_iface, 0x81, USBD_EXCLUSIVE_USE,
510 	    &sc->cmd_rx_pipe);
511 	if (error != 0) {
512 		printf("%s: could not open Rx command pipe: %s\n",
513 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
514 		goto fail;
515 	}
516 
517 	error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
518 	    &sc->data_rx_pipe);
519 	if (error != 0) {
520 		printf("%s: could not open Rx data pipe: %s\n",
521 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
522 		goto fail;
523 	}
524 
525 	return 0;
526 
527 fail:	uath_close_pipes(sc);
528 	return error;
529 }
530 
531 Static void
532 uath_close_pipes(struct uath_softc *sc)
533 {
534 	/* assumes no transfers are pending on the pipes */
535 
536 	if (sc->data_tx_pipe != NULL)
537 		usbd_close_pipe(sc->data_tx_pipe);
538 
539 	if (sc->data_rx_pipe != NULL)
540 		usbd_close_pipe(sc->data_rx_pipe);
541 
542 	if (sc->cmd_tx_pipe != NULL)
543 		usbd_close_pipe(sc->cmd_tx_pipe);
544 
545 	if (sc->cmd_rx_pipe != NULL)
546 		usbd_close_pipe(sc->cmd_rx_pipe);
547 }
548 
549 Static int
550 uath_alloc_tx_data_list(struct uath_softc *sc)
551 {
552 	int i, error;
553 
554 	for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) {
555 		struct uath_tx_data *data = &sc->tx_data[i];
556 
557 		data->sc = sc;	/* backpointer for callbacks */
558 
559 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
560 		if (data->xfer == NULL) {
561 			printf("%s: could not allocate xfer\n",
562 			    USBDEVNAME(sc->sc_dev));
563 			error = ENOMEM;
564 			goto fail;
565 		}
566 		data->buf = usbd_alloc_buffer(data->xfer, UATH_MAX_TXBUFSZ);
567 		if (data->buf == NULL) {
568 			printf("%s: could not allocate xfer buffer\n",
569 			    USBDEVNAME(sc->sc_dev));
570 			error = ENOMEM;
571 			goto fail;
572 		}
573 	}
574 	return 0;
575 
576 fail:	uath_free_tx_data_list(sc);
577 	return error;
578 }
579 
580 Static void
581 uath_free_tx_data_list(struct uath_softc *sc)
582 {
583 	int i;
584 
585 	/* make sure no transfers are pending */
586 	usbd_abort_pipe(sc->data_tx_pipe);
587 
588 	for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++)
589 		if (sc->tx_data[i].xfer != NULL)
590 			usbd_free_xfer(sc->tx_data[i].xfer);
591 }
592 
593 Static int
594 uath_alloc_rx_data_list(struct uath_softc *sc)
595 {
596 	int i, error;
597 
598 	for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) {
599 		struct uath_rx_data *data = &sc->rx_data[i];
600 
601 		data->sc = sc;	/* backpointer for callbacks */
602 
603 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
604 		if (data->xfer == NULL) {
605 			printf("%s: could not allocate xfer\n",
606 			    USBDEVNAME(sc->sc_dev));
607 			error = ENOMEM;
608 			goto fail;
609 		}
610 		if (usbd_alloc_buffer(data->xfer, sc->rxbufsz) == NULL) {
611 			printf("%s: could not allocate xfer buffer\n",
612 			    USBDEVNAME(sc->sc_dev));
613 			error = ENOMEM;
614 			goto fail;
615 		}
616 
617 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
618 		if (data->m == NULL) {
619 			printf("%s: could not allocate rx mbuf\n",
620 			    USBDEVNAME(sc->sc_dev));
621 			error = ENOMEM;
622 			goto fail;
623 		}
624 		MCLGET(data->m, M_DONTWAIT);
625 		if (!(data->m->m_flags & M_EXT)) {
626 			printf("%s: could not allocate rx mbuf cluster\n",
627 			    USBDEVNAME(sc->sc_dev));
628 			error = ENOMEM;
629 			goto fail;
630 		}
631 
632 		data->buf = mtod(data->m, uint8_t *);
633 	}
634 	return 0;
635 
636 fail:	uath_free_rx_data_list(sc);
637 	return error;
638 }
639 
640 Static void
641 uath_free_rx_data_list(struct uath_softc *sc)
642 {
643 	int i;
644 
645 	/* make sure no transfers are pending */
646 	usbd_abort_pipe(sc->data_rx_pipe);
647 
648 	for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) {
649 		struct uath_rx_data *data = &sc->rx_data[i];
650 
651 		if (data->xfer != NULL)
652 			usbd_free_xfer(data->xfer);
653 
654 		if (data->m != NULL)
655 			m_freem(data->m);
656 	}
657 }
658 
659 Static int
660 uath_alloc_tx_cmd_list(struct uath_softc *sc)
661 {
662 	int i, error;
663 
664 	for (i = 0; i < UATH_TX_CMD_LIST_COUNT; i++) {
665 		struct uath_tx_cmd *cmd = &sc->tx_cmd[i];
666 
667 		cmd->sc = sc;	/* backpointer for callbacks */
668 
669 		cmd->xfer = usbd_alloc_xfer(sc->sc_udev);
670 		if (cmd->xfer == NULL) {
671 			printf("%s: could not allocate xfer\n",
672 			    USBDEVNAME(sc->sc_dev));
673 			error = ENOMEM;
674 			goto fail;
675 		}
676 		cmd->buf = usbd_alloc_buffer(cmd->xfer, UATH_MAX_TXCMDSZ);
677 		if (cmd->buf == NULL) {
678 			printf("%s: could not allocate xfer buffer\n",
679 			    USBDEVNAME(sc->sc_dev));
680 			error = ENOMEM;
681 			goto fail;
682 		}
683 	}
684 	return 0;
685 
686 fail:	uath_free_tx_cmd_list(sc);
687 	return error;
688 }
689 
690 Static void
691 uath_free_tx_cmd_list(struct uath_softc *sc)
692 {
693 	int i;
694 
695 	/* make sure no transfers are pending */
696 	usbd_abort_pipe(sc->cmd_tx_pipe);
697 
698 	for (i = 0; i < UATH_TX_CMD_LIST_COUNT; i++)
699 		if (sc->tx_cmd[i].xfer != NULL)
700 			usbd_free_xfer(sc->tx_cmd[i].xfer);
701 }
702 
703 Static int
704 uath_alloc_rx_cmd_list(struct uath_softc *sc)
705 {
706 	int i, error;
707 
708 	for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++) {
709 		struct uath_rx_cmd *cmd = &sc->rx_cmd[i];
710 
711 		cmd->sc = sc;	/* backpointer for callbacks */
712 
713 		cmd->xfer = usbd_alloc_xfer(sc->sc_udev);
714 		if (cmd->xfer == NULL) {
715 			printf("%s: could not allocate xfer\n",
716 			    USBDEVNAME(sc->sc_dev));
717 			error = ENOMEM;
718 			goto fail;
719 		}
720 		cmd->buf = usbd_alloc_buffer(cmd->xfer, UATH_MAX_RXCMDSZ);
721 		if (cmd->buf == NULL) {
722 			printf("%s: could not allocate xfer buffer\n",
723 			    USBDEVNAME(sc->sc_dev));
724 			error = ENOMEM;
725 			goto fail;
726 		}
727 	}
728 	return 0;
729 
730 fail:	uath_free_rx_cmd_list(sc);
731 	return error;
732 }
733 
734 Static void
735 uath_free_rx_cmd_list(struct uath_softc *sc)
736 {
737 	int i;
738 
739 	/* make sure no transfers are pending */
740 	usbd_abort_pipe(sc->cmd_rx_pipe);
741 
742 	for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++)
743 		if (sc->rx_cmd[i].xfer != NULL)
744 			usbd_free_xfer(sc->rx_cmd[i].xfer);
745 }
746 
747 Static int
748 uath_media_change(struct ifnet *ifp)
749 {
750 	int error;
751 
752 	error = ieee80211_media_change(ifp);
753 	if (error != ENETRESET)
754 		return error;
755 
756 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
757 		uath_init(ifp);
758 
759 	return 0;
760 }
761 
762 /*
763  * This function is called periodically (every second) when associated to
764  * query device statistics.
765  */
766 Static void
767 uath_stat(void *arg)
768 {
769 	struct uath_softc *sc = arg;
770 	int error;
771 
772 	/*
773 	 * Send request for statistics asynchronously. The timer will be
774 	 * restarted when we'll get the stats notification.
775 	 */
776 	error = uath_cmd_write(sc, UATH_CMD_STATS, NULL, 0,
777 	    UATH_CMD_FLAG_ASYNC);
778 	if (error != 0) {
779 		printf("%s: could not query statistics (error=%d)\n",
780 		    USBDEVNAME(sc->sc_dev), error);
781 	}
782 }
783 
784 /*
785  * This function is called periodically (every 250ms) during scanning to
786  * switch from one channel to another.
787  */
788 Static void
789 uath_next_scan(void *arg)
790 {
791 	struct uath_softc *sc = arg;
792 	struct ieee80211com *ic = &sc->sc_ic;
793 	struct ifnet *ifp = &ic->ic_if;
794 
795 	if (ic->ic_state == IEEE80211_S_SCAN)
796 		ieee80211_next_scan(ifp);
797 }
798 
799 Static void
800 uath_task(void *arg)
801 {
802 	struct uath_softc *sc = arg;
803 	struct ieee80211com *ic = &sc->sc_ic;
804 	enum ieee80211_state ostate;
805 
806 	ostate = ic->ic_state;
807 
808 	switch (sc->sc_state) {
809 	case IEEE80211_S_INIT:
810 		if (ostate == IEEE80211_S_RUN) {
811 			/* turn link and activity LEDs off */
812 			(void)uath_set_led(sc, UATH_LED_LINK, 0);
813 			(void)uath_set_led(sc, UATH_LED_ACTIVITY, 0);
814 		}
815 		break;
816 
817 	case IEEE80211_S_SCAN:
818 		if (uath_switch_channel(sc, ic->ic_bss->ni_chan) != 0) {
819 			printf("%s: could not switch channel\n",
820 			    USBDEVNAME(sc->sc_dev));
821 			break;
822 		}
823 		timeout_add(&sc->scan_to, hz / 4);
824 		break;
825 
826 	case IEEE80211_S_AUTH:
827 	{
828 		struct ieee80211_node *ni = ic->ic_bss;
829 		struct uath_cmd_bssid bssid;
830 		struct uath_cmd_0b cmd0b;
831 		struct uath_cmd_0c cmd0c;
832 
833 		if (uath_switch_channel(sc, ni->ni_chan) != 0) {
834 			printf("%s: could not switch channel\n",
835 			    USBDEVNAME(sc->sc_dev));
836 			break;
837 		}
838 
839 		(void)uath_cmd_write(sc, UATH_CMD_24, NULL, 0, 0);
840 
841 		bzero(&bssid, sizeof bssid);
842 		bssid.len = htobe32(IEEE80211_ADDR_LEN);
843 		IEEE80211_ADDR_COPY(bssid.bssid, ni->ni_bssid);
844 		(void)uath_cmd_write(sc, UATH_CMD_SET_BSSID, &bssid,
845 		    sizeof bssid, 0);
846 
847 		bzero(&cmd0b, sizeof cmd0b);
848 		cmd0b.code = htobe32(2);
849 		cmd0b.size = htobe32(sizeof (cmd0b.data));
850 		(void)uath_cmd_write(sc, UATH_CMD_0B, &cmd0b, sizeof cmd0b, 0);
851 
852 		bzero(&cmd0c, sizeof cmd0c);
853 		cmd0c.magic1 = htobe32(2);
854 		cmd0c.magic2 = htobe32(7);
855 		cmd0c.magic3 = htobe32(1);
856 		(void)uath_cmd_write(sc, UATH_CMD_0C, &cmd0c, sizeof cmd0c, 0);
857 
858 		if (uath_set_rates(sc, &ni->ni_rates) != 0) {
859 			printf("%s: could not set negotiated rate set\n",
860 			    USBDEVNAME(sc->sc_dev));
861 			break;
862 		}
863 		break;
864 	}
865 
866 	case IEEE80211_S_ASSOC:
867 		break;
868 
869 	case IEEE80211_S_RUN:
870 	{
871 		struct ieee80211_node *ni = ic->ic_bss;
872 		struct uath_cmd_bssid bssid;
873 		struct uath_cmd_xled xled;
874 		uint32_t val;
875 
876 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
877 			/* make both LEDs blink while monitoring */
878 			bzero(&xled, sizeof xled);
879 			xled.which = htobe32(0);
880 			xled.rate = htobe32(1);
881 			xled.mode = htobe32(2);
882 			(void)uath_cmd_write(sc, UATH_CMD_SET_XLED, &xled,
883 			    sizeof xled, 0);
884 			break;
885 		}
886 
887 		/*
888 		 * Tx rate is controlled by firmware, report the maximum
889 		 * negotiated rate in ifconfig output.
890 		 */
891 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
892 
893 		val = htobe32(1);
894 		(void)uath_cmd_write(sc, UATH_CMD_2E, &val, sizeof val, 0);
895 
896 		bzero(&bssid, sizeof bssid);
897 		bssid.flags1 = htobe32(0xc004);
898 		bssid.flags2 = htobe32(0x003b);
899 		bssid.len = htobe32(IEEE80211_ADDR_LEN);
900 		IEEE80211_ADDR_COPY(bssid.bssid, ni->ni_bssid);
901 		(void)uath_cmd_write(sc, UATH_CMD_SET_BSSID, &bssid,
902 		    sizeof bssid, 0);
903 
904 		/* turn link LED on */
905 		(void)uath_set_led(sc, UATH_LED_LINK, 1);
906 
907 		/* make activity LED blink */
908 		bzero(&xled, sizeof xled);
909 		xled.which = htobe32(1);
910 		xled.rate = htobe32(1);
911 		xled.mode = htobe32(2);
912 		(void)uath_cmd_write(sc, UATH_CMD_SET_XLED, &xled, sizeof xled,
913 		    0);
914 
915 		/* set state to associated */
916 		val = htobe32(1);
917 		(void)uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val,
918 		    0);
919 
920 		/* start statistics timer */
921 		timeout_add(&sc->stat_to, hz);
922 		break;
923 	}
924 	}
925 	sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
926 }
927 
928 Static int
929 uath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
930 {
931 	struct uath_softc *sc = ic->ic_softc;
932 
933 	usb_rem_task(sc->sc_udev, &sc->sc_task);
934 	timeout_del(&sc->scan_to);
935 	timeout_del(&sc->stat_to);
936 
937 	/* do it in a process context */
938 	sc->sc_state = nstate;
939 	sc->sc_arg = arg;
940 	usb_add_task(sc->sc_udev, &sc->sc_task);
941 
942 	return 0;
943 }
944 
945 #ifdef UATH_DEBUG
946 Static void
947 uath_dump_cmd(const uint8_t *buf, int len, char prefix)
948 {
949 	int i;
950 
951 	for (i = 0; i < len; i++) {
952 		if ((i % 16) == 0)
953 			printf("\n%c ", prefix);
954 		else if ((i % 4) == 0)
955 			printf(" ");
956 		printf("%02x", buf[i]);
957 	}
958 	printf("\n");
959 }
960 #endif
961 
962 /*
963  * Low-level function to send read or write commands to the firmware.
964  */
965 Static int
966 uath_cmd(struct uath_softc *sc, uint32_t code, const void *idata, int ilen,
967     void *odata, int flags)
968 {
969 	struct uath_cmd_hdr *hdr;
970 	struct uath_tx_cmd *cmd;
971 	uint16_t xferflags;
972 	int s, xferlen, error;
973 
974 	/* grab a xfer */
975 	cmd = &sc->tx_cmd[sc->cmd_idx];
976 
977 	/* always bulk-out a multiple of 4 bytes */
978 	xferlen = (sizeof (struct uath_cmd_hdr) + ilen + 3) & ~3;
979 
980 	hdr = (struct uath_cmd_hdr *)cmd->buf;
981 	bzero(hdr, sizeof (struct uath_cmd_hdr));
982 	hdr->len   = htobe32(xferlen);
983 	hdr->code  = htobe32(code);
984 	hdr->priv  = sc->cmd_idx;	/* don't care about endianness */
985 	hdr->magic = htobe32((flags & UATH_CMD_FLAG_MAGIC) ? 1 << 24 : 0);
986 	bcopy(idata, (uint8_t *)(hdr + 1), ilen);
987 
988 #ifdef UATH_DEBUG
989 	if (uath_debug >= 5) {
990 		printf("sending command code=0x%02x flags=0x%x index=%u",
991 		    code, flags, sc->cmd_idx);
992 		uath_dump_cmd(cmd->buf, xferlen, '+');
993 	}
994 #endif
995 	xferflags = USBD_FORCE_SHORT_XFER | USBD_NO_COPY;
996 	if (!(flags & UATH_CMD_FLAG_READ)) {
997 		if (!(flags & UATH_CMD_FLAG_ASYNC))
998 			xferflags |= USBD_SYNCHRONOUS;
999 	} else
1000 		s = splusb();
1001 
1002 	cmd->odata = odata;
1003 
1004 	usbd_setup_xfer(cmd->xfer, sc->cmd_tx_pipe, cmd, cmd->buf, xferlen,
1005 	    xferflags, UATH_CMD_TIMEOUT, NULL);
1006 	error = usbd_transfer(cmd->xfer);
1007 	if (error != USBD_IN_PROGRESS && error != 0) {
1008 		if (flags & UATH_CMD_FLAG_READ)
1009 			splx(s);
1010 		printf("%s: could not send command (error=%s)\n",
1011 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1012 		return error;
1013 	}
1014 	sc->cmd_idx = (sc->cmd_idx + 1) % UATH_TX_CMD_LIST_COUNT;
1015 
1016 	if (!(flags & UATH_CMD_FLAG_READ))
1017 		return 0;	/* write: don't wait for reply */
1018 
1019 	/* wait at most two seconds for command reply */
1020 	error = tsleep(cmd, PCATCH, "uathcmd", 2 * hz);
1021 	splx(s);
1022 	if (error != 0) {
1023 		printf("%s: timeout waiting for command reply\n",
1024 		    USBDEVNAME(sc->sc_dev));
1025 	}
1026 	return error;
1027 }
1028 
1029 Static int
1030 uath_cmd_write(struct uath_softc *sc, uint32_t code, const void *data, int len,
1031     int flags)
1032 {
1033 	flags &= ~UATH_CMD_FLAG_READ;
1034 	return uath_cmd(sc, code, data, len, NULL, flags);
1035 }
1036 
1037 Static int
1038 uath_cmd_read(struct uath_softc *sc, uint32_t code, const void *idata,
1039     int ilen, void *odata, int flags)
1040 {
1041 	flags |= UATH_CMD_FLAG_READ;
1042 	return uath_cmd(sc, code, idata, ilen, odata, flags);
1043 }
1044 
1045 Static int
1046 uath_write_reg(struct uath_softc *sc, uint32_t reg, uint32_t val)
1047 {
1048 	struct uath_write_mac write;
1049 	int error;
1050 
1051 	write.reg = htobe32(reg);
1052 	write.len = htobe32(0);	/* 0 = single write */
1053 	*(uint32_t *)write.data = htobe32(val);
1054 
1055 	error = uath_cmd_write(sc, UATH_CMD_WRITE_MAC, &write,
1056 	    3 * sizeof (uint32_t), 0);
1057 	if (error != 0) {
1058 		printf("%s: could not write register 0x%02x\n",
1059 		    USBDEVNAME(sc->sc_dev), reg);
1060 	}
1061 	return error;
1062 }
1063 
1064 Static int
1065 uath_write_multi(struct uath_softc *sc, uint32_t reg, const void *data,
1066     int len)
1067 {
1068 	struct uath_write_mac write;
1069 	int error;
1070 
1071 	write.reg = htobe32(reg);
1072 	write.len = htobe32(len);
1073 	bcopy(data, write.data, len);
1074 
1075 	/* properly handle the case where len is zero (reset) */
1076 	error = uath_cmd_write(sc, UATH_CMD_WRITE_MAC, &write,
1077 	    (len == 0) ? sizeof (uint32_t) : 2 * sizeof (uint32_t) + len, 0);
1078 	if (error != 0) {
1079 		printf("%s: could not write %d bytes to register 0x%02x\n",
1080 		    USBDEVNAME(sc->sc_dev), len, reg);
1081 	}
1082 	return error;
1083 }
1084 
1085 Static int
1086 uath_read_reg(struct uath_softc *sc, uint32_t reg, uint32_t *val)
1087 {
1088 	struct uath_read_mac read;
1089 	int error;
1090 
1091 	reg = htobe32(reg);
1092 	error = uath_cmd_read(sc, UATH_CMD_READ_MAC, &reg, sizeof reg, &read,
1093 	    0);
1094 	if (error != 0) {
1095 		printf("%s: could not read register 0x%02x\n",
1096 		    USBDEVNAME(sc->sc_dev), betoh32(reg));
1097 		return error;
1098 	}
1099 	*val = betoh32(*(uint32_t *)read.data);
1100 	return error;
1101 }
1102 
1103 Static int
1104 uath_read_eeprom(struct uath_softc *sc, uint32_t reg, void *odata)
1105 {
1106 	struct uath_read_mac read;
1107 	int len, error;
1108 
1109 	reg = htobe32(reg);
1110 	error = uath_cmd_read(sc, UATH_CMD_READ_EEPROM, &reg, sizeof reg,
1111 	    &read, 0);
1112 	if (error != 0) {
1113 		printf("%s: could not read EEPROM offset 0x%02x\n",
1114 		    USBDEVNAME(sc->sc_dev), betoh32(reg));
1115 		return error;
1116 	}
1117 	len = betoh32(read.len);
1118 	bcopy(read.data, odata, (len == 0) ? sizeof (uint32_t) : len);
1119 	return error;
1120 }
1121 
1122 Static void
1123 uath_cmd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1124     usbd_status status)
1125 {
1126 	struct uath_rx_cmd *cmd = priv;
1127 	struct uath_softc *sc = cmd->sc;
1128 	struct uath_cmd_hdr *hdr;
1129 
1130 	if (status != USBD_NORMAL_COMPLETION) {
1131 		if (status == USBD_STALLED)
1132 			usbd_clear_endpoint_stall_async(sc->cmd_rx_pipe);
1133 		return;
1134 	}
1135 
1136 	hdr = (struct uath_cmd_hdr *)cmd->buf;
1137 
1138 #ifdef UATH_DEBUG
1139 	if (uath_debug >= 5) {
1140 		printf("received command code=0x%x index=%u len=%u",
1141 		    betoh32(hdr->code), hdr->priv, betoh32(hdr->len));
1142 		uath_dump_cmd(cmd->buf, betoh32(hdr->len), '-');
1143 	}
1144 #endif
1145 
1146 	switch (betoh32(hdr->code) & 0xff) {
1147 	/* reply to a read command */
1148 	default:
1149 	{
1150 		struct uath_tx_cmd *txcmd = &sc->tx_cmd[hdr->priv];
1151 
1152 		if (txcmd->odata != NULL) {
1153 			/* copy answer into caller's supplied buffer */
1154 			bcopy((uint8_t *)(hdr + 1), txcmd->odata,
1155 			    betoh32(hdr->len) - sizeof (struct uath_cmd_hdr));
1156 		}
1157 		wakeup(txcmd);	/* wake up caller */
1158 		break;
1159 	}
1160 	/* spontaneous firmware notifications */
1161 	case UATH_NOTIF_READY:
1162 		DPRINTF(("received device ready notification\n"));
1163 		wakeup(UATH_COND_INIT(sc));
1164 		break;
1165 
1166 	case UATH_NOTIF_TX:
1167 		/* this notification is sent when UATH_TX_NOTIFY is set */
1168 		DPRINTF(("received Tx notification\n"));
1169 		break;
1170 
1171 	case UATH_NOTIF_STATS:
1172 		DPRINTFN(2, ("received device statistics\n"));
1173 		timeout_add(&sc->stat_to, hz);
1174 		break;
1175 	}
1176 
1177 	/* setup a new transfer */
1178 	usbd_setup_xfer(xfer, sc->cmd_rx_pipe, cmd, cmd->buf, UATH_MAX_RXCMDSZ,
1179 	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1180 	    uath_cmd_rxeof);
1181 	(void)usbd_transfer(xfer);
1182 }
1183 
1184 Static void
1185 uath_data_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1186     usbd_status status)
1187 {
1188 	struct uath_rx_data *data = priv;
1189 	struct uath_softc *sc = data->sc;
1190 	struct ieee80211com *ic = &sc->sc_ic;
1191 	struct ifnet *ifp = &ic->ic_if;
1192 	struct ieee80211_frame *wh;
1193 	struct ieee80211_node *ni;
1194 	struct uath_rx_desc *desc;
1195 	struct mbuf *mnew, *m;
1196 	uint32_t hdr;
1197 	int s, len;
1198 
1199 	if (status != USBD_NORMAL_COMPLETION) {
1200 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1201 			return;
1202 
1203 		if (status == USBD_STALLED)
1204 			usbd_clear_endpoint_stall_async(sc->data_rx_pipe);
1205 
1206 		ifp->if_ierrors++;
1207 		return;
1208 	}
1209 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1210 
1211 	if (len < UATH_MIN_RXBUFSZ || len > sc->rxbufsz) {
1212 		DPRINTF(("wrong xfer size: !(%d <= %d <= %d)\n",
1213 		    UATH_MIN_RXBUFSZ, len, sc->rxbufsz));
1214 		ifp->if_ierrors++;
1215 		goto skip;
1216 	}
1217 
1218 	hdr = betoh32(*(uint32_t *)data->buf);
1219 
1220 	/* Rx descriptor is located at the end, 32-bit aligned */
1221 	desc = (struct uath_rx_desc *)
1222 	    (data->buf + len - sizeof (struct uath_rx_desc));
1223 
1224 	/* there's probably a "bad CRC" flag somewhere in the descriptor.. */
1225 
1226 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1227 	if (mnew == NULL) {
1228 		printf("%s: could not allocate rx mbuf\n",
1229 		    USBDEVNAME(sc->sc_dev));
1230 		ifp->if_ierrors++;
1231 		goto skip;
1232 	}
1233 	MCLGET(mnew, M_DONTWAIT);
1234 	if (!(mnew->m_flags & M_EXT)) {
1235 		printf("%s: could not allocate rx mbuf cluster\n",
1236 		    USBDEVNAME(sc->sc_dev));
1237 		m_freem(mnew);
1238 		ifp->if_ierrors++;
1239 		goto skip;
1240 	}
1241 
1242 	m = data->m;
1243 	data->m = mnew;
1244 
1245 	/* finalize mbuf */
1246 	m->m_pkthdr.rcvif = ifp;
1247 	m->m_data = data->buf + sizeof (uint32_t);
1248 	m->m_pkthdr.len = m->m_len =
1249 	    betoh32(desc->len) - sizeof (struct uath_rx_desc);
1250 
1251 	data->buf = mtod(data->m, uint8_t *);
1252 
1253 	wh = mtod(m, struct ieee80211_frame *);
1254 	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1255 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
1256 		/*
1257 		 * Hardware decrypts the frame itself but leaves the WEP bit
1258 		 * set in the 802.11 header and doesn't remove the IV and CRC
1259 		 * fields.
1260 		 */
1261 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1262 		ovbcopy(wh, (caddr_t)wh + IEEE80211_WEP_IVLEN +
1263 		    IEEE80211_WEP_KIDLEN, sizeof (struct ieee80211_frame));
1264 		m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
1265 		m_adj(m, -IEEE80211_WEP_CRCLEN);
1266 		wh = mtod(m, struct ieee80211_frame *);
1267 	}
1268 
1269 #if NBPFILTER > 0
1270 	/* there are a lot more fields in the Rx descriptor */
1271 	if (sc->sc_drvbpf != NULL) {
1272 		struct mbuf mb;
1273 		struct uath_rx_radiotap_header *tap = &sc->sc_rxtap;
1274 
1275 		tap->wr_flags = 0;
1276 		tap->wr_chan_freq = htole16(betoh32(desc->freq));
1277 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1278 		tap->wr_dbm_antsignal = (int8_t)betoh32(desc->rssi);
1279 
1280 		M_DUP_PKTHDR(&mb, m);
1281 		mb.m_data = (caddr_t)tap;
1282 		mb.m_len = sc->sc_rxtap_len;
1283 		mb.m_next = m;
1284 		mb.m_pkthdr.len += mb.m_len;
1285 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
1286 	}
1287 #endif
1288 
1289 	s = splnet();
1290 	ni = ieee80211_find_rxnode(ic, wh);
1291 	ieee80211_input(ifp, m, ni, (int)betoh32(desc->rssi), 0);
1292 
1293 	/* node is no longer needed */
1294 	ieee80211_release_node(ic, ni);
1295 	splx(s);
1296 
1297 skip:	/* setup a new transfer */
1298 	usbd_setup_xfer(xfer, sc->data_rx_pipe, data, data->buf, sc->rxbufsz,
1299 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, uath_data_rxeof);
1300 	(void)usbd_transfer(xfer);
1301 }
1302 
1303 Static int
1304 uath_tx_null(struct uath_softc *sc)
1305 {
1306 	struct uath_tx_data *data;
1307 	struct uath_tx_desc *desc;
1308 
1309 	data = &sc->tx_data[sc->data_idx];
1310 
1311 	data->ni = NULL;
1312 
1313 	*(uint32_t *)data->buf = UATH_MAKECTL(1, sizeof (struct uath_tx_desc));
1314 	desc = (struct uath_tx_desc *)(data->buf + sizeof (uint32_t));
1315 
1316 	bzero(desc, sizeof (struct uath_tx_desc));
1317 	desc->len  = htobe32(sizeof (struct uath_tx_desc));
1318 	desc->type = htobe32(UATH_TX_NULL);
1319 
1320 	usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf,
1321 	    sizeof (uint32_t) + sizeof (struct uath_tx_desc), USBD_NO_COPY |
1322 	    USBD_FORCE_SHORT_XFER, UATH_DATA_TIMEOUT, NULL);
1323 	if (usbd_sync_transfer(data->xfer) != 0)
1324 		return EIO;
1325 
1326 	sc->data_idx = (sc->data_idx + 1) % UATH_TX_DATA_LIST_COUNT;
1327 
1328 	return uath_cmd_write(sc, UATH_CMD_0F, NULL, 0, UATH_CMD_FLAG_ASYNC);
1329 }
1330 
1331 Static void
1332 uath_data_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1333     usbd_status status)
1334 {
1335 	struct uath_tx_data *data = priv;
1336 	struct uath_softc *sc = data->sc;
1337 	struct ieee80211com *ic = &sc->sc_ic;
1338 	struct ifnet *ifp = &ic->ic_if;
1339 	int s;
1340 
1341 	if (status != USBD_NORMAL_COMPLETION) {
1342 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1343 			return;
1344 
1345 		printf("%s: could not transmit buffer: %s\n",
1346 		    USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1347 
1348 		if (status == USBD_STALLED)
1349 			usbd_clear_endpoint_stall_async(sc->data_tx_pipe);
1350 
1351 		ifp->if_oerrors++;
1352 		return;
1353 	}
1354 
1355 	s = splnet();
1356 
1357 	ieee80211_release_node(ic, data->ni);
1358 	data->ni = NULL;
1359 
1360 	sc->tx_queued--;
1361 	ifp->if_opackets++;
1362 
1363 	sc->sc_tx_timer = 0;
1364 	ifp->if_flags &= ~IFF_OACTIVE;
1365 	uath_start(ifp);
1366 
1367 	splx(s);
1368 }
1369 
1370 Static int
1371 uath_tx_data(struct uath_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1372 {
1373 	struct ieee80211com *ic = &sc->sc_ic;
1374 	struct uath_tx_data *data;
1375 	struct uath_tx_desc *desc;
1376 	const struct ieee80211_frame *wh;
1377 	int paylen, totlen, xferlen, error;
1378 
1379 	data = &sc->tx_data[sc->data_idx];
1380 	desc = (struct uath_tx_desc *)(data->buf + sizeof (uint32_t));
1381 
1382 	data->ni = ni;
1383 
1384 #if NBPFILTER > 0
1385 	if (sc->sc_drvbpf != NULL) {
1386 		struct mbuf mb;
1387 		struct uath_tx_radiotap_header *tap = &sc->sc_txtap;
1388 
1389 		tap->wt_flags = 0;
1390 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1391 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1392 
1393 		M_DUP_PKTHDR(&mb, m0);
1394 		mb.m_data = (caddr_t)tap;
1395 		mb.m_len = sc->sc_txtap_len;
1396 		mb.m_next = m0;
1397 		mb.m_pkthdr.len += mb.m_len;
1398 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1399 	}
1400 #endif
1401 
1402 	paylen = m0->m_pkthdr.len;
1403 	xferlen = sizeof (uint32_t) + sizeof (struct uath_tx_desc) + paylen;
1404 
1405 	wh = mtod(m0, struct ieee80211_frame *);
1406 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1407 		uint8_t *frm = (uint8_t *)(desc + 1);
1408 		uint32_t iv;
1409 
1410 		/* h/w WEP: it's up to the host to fill the IV field */
1411 		bcopy(wh, frm, sizeof (struct ieee80211_frame));
1412 		frm += sizeof (struct ieee80211_frame);
1413 
1414 		/* insert IV: code copied from net80211 */
1415 		iv = (ic->ic_iv != 0) ? ic->ic_iv : arc4random();
1416 		if (iv >= 0x03ff00 && (iv & 0xf8ff00) == 0x00ff00)
1417 			iv += 0x000100;
1418 		ic->ic_iv = iv + 1;
1419 
1420 		*frm++ = iv & 0xff;
1421 		*frm++ = (iv >>  8) & 0xff;
1422 		*frm++ = (iv >> 16) & 0xff;
1423 		*frm++ = ic->ic_wep_txkey << 6;
1424 
1425 		m_copydata(m0, sizeof (struct ieee80211_frame),
1426 		    m0->m_pkthdr.len - sizeof (struct ieee80211_frame), frm);
1427 
1428 		paylen  += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
1429 		xferlen += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
1430 		totlen = xferlen + IEEE80211_WEP_CRCLEN;
1431 	} else {
1432 		m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(desc + 1));
1433 		totlen = xferlen;
1434 	}
1435 
1436 	/* fill Tx descriptor */
1437 	*(uint32_t *)data->buf = UATH_MAKECTL(1, xferlen - sizeof (uint32_t));
1438 
1439 	desc->len    = htobe32(totlen);
1440 	desc->priv   = sc->data_idx;	/* don't care about endianness */
1441 	desc->paylen = htobe32(paylen);
1442 	desc->type   = htobe32(UATH_TX_DATA);
1443 	desc->flags  = htobe32(0);
1444 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1445 		desc->dest  = htobe32(UATH_ID_BROADCAST);
1446 		desc->magic = htobe32(3);
1447 	} else {
1448 		desc->dest  = htobe32(UATH_ID_BSS);
1449 		desc->magic = htobe32(1);
1450 	}
1451 
1452 	m_freem(m0);	/* mbuf is no longer needed */
1453 
1454 #ifdef UATH_DEBUG
1455 	if (uath_debug >= 6) {
1456 		printf("sending frame index=%u len=%d xferlen=%d",
1457 		    sc->data_idx, paylen, xferlen);
1458 		uath_dump_cmd(data->buf, xferlen, '+');
1459 	}
1460 #endif
1461 	usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf, xferlen,
1462 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, UATH_DATA_TIMEOUT,
1463 	    uath_data_txeof);
1464 	error = usbd_transfer(data->xfer);
1465 	if (error != USBD_IN_PROGRESS && error != 0) {
1466 		ic->ic_if.if_oerrors++;
1467 		return error;
1468 	}
1469 	sc->data_idx = (sc->data_idx + 1) % UATH_TX_DATA_LIST_COUNT;
1470 	sc->tx_queued++;
1471 
1472 	return 0;
1473 }
1474 
1475 Static void
1476 uath_start(struct ifnet *ifp)
1477 {
1478 	struct uath_softc *sc = ifp->if_softc;
1479 	struct ieee80211com *ic = &sc->sc_ic;
1480 	struct ieee80211_node *ni;
1481 	struct mbuf *m0;
1482 
1483 	/*
1484 	 * net80211 may still try to send management frames even if the
1485 	 * IFF_RUNNING flag is not set...
1486 	 */
1487 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1488 		return;
1489 
1490 	for (;;) {
1491 		IF_POLL(&ic->ic_mgtq, m0);
1492 		if (m0 != NULL) {
1493 			if (sc->tx_queued >= UATH_TX_DATA_LIST_COUNT) {
1494 				ifp->if_flags |= IFF_OACTIVE;
1495 				break;
1496 			}
1497 			IF_DEQUEUE(&ic->ic_mgtq, m0);
1498 
1499 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1500 			m0->m_pkthdr.rcvif = NULL;
1501 #if NBPFILTER > 0
1502 			if (ic->ic_rawbpf != NULL)
1503 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1504 #endif
1505 			if (uath_tx_data(sc, m0, ni) != 0)
1506 				break;
1507 		} else {
1508 			if (ic->ic_state != IEEE80211_S_RUN)
1509 				break;
1510 			IFQ_POLL(&ifp->if_snd, m0);
1511 			if (m0 == NULL)
1512 				break;
1513 			if (sc->tx_queued >= UATH_TX_DATA_LIST_COUNT) {
1514 				ifp->if_flags |= IFF_OACTIVE;
1515 				break;
1516 			}
1517 			IFQ_DEQUEUE(&ifp->if_snd, m0);
1518 #if NBPFILTER > 0
1519 			if (ifp->if_bpf != NULL)
1520 				bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1521 #endif
1522 			m0 = ieee80211_encap(ifp, m0, &ni);
1523 			if (m0 == NULL)
1524 				continue;
1525 #if NBPFILTER > 0
1526 			if (ic->ic_rawbpf != NULL)
1527 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1528 #endif
1529 			if (uath_tx_data(sc, m0, ni) != 0) {
1530 				if (ni != NULL)
1531 					ieee80211_release_node(ic, ni);
1532 				ifp->if_oerrors++;
1533 				break;
1534 			}
1535 		}
1536 
1537 		sc->sc_tx_timer = 5;
1538 		ifp->if_timer = 1;
1539 	}
1540 }
1541 
1542 Static void
1543 uath_watchdog(struct ifnet *ifp)
1544 {
1545 	struct uath_softc *sc = ifp->if_softc;
1546 
1547 	ifp->if_timer = 0;
1548 
1549 	if (sc->sc_tx_timer > 0) {
1550 		if (--sc->sc_tx_timer == 0) {
1551 			printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1552 			/*uath_init(ifp); XXX needs a process context! */
1553 			ifp->if_oerrors++;
1554 			return;
1555 		}
1556 		ifp->if_timer = 1;
1557 	}
1558 
1559 	ieee80211_watchdog(ifp);
1560 }
1561 
1562 Static int
1563 uath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1564 {
1565 	struct uath_softc *sc = ifp->if_softc;
1566 	struct ieee80211com *ic = &sc->sc_ic;
1567 	struct ifaddr *ifa;
1568 	struct ifreq *ifr;
1569 	int s, error = 0;
1570 
1571 	s = splnet();
1572 
1573 	switch (cmd) {
1574 	case SIOCSIFADDR:
1575 		ifa = (struct ifaddr *)data;
1576 		ifp->if_flags |= IFF_UP;
1577 #ifdef INET
1578 		if (ifa->ifa_addr->sa_family == AF_INET)
1579 			arp_ifinit(&ic->ic_ac, ifa);
1580 #endif
1581 		/* FALLTHROUGH */
1582 	case SIOCSIFFLAGS:
1583 		if (ifp->if_flags & IFF_UP) {
1584 			if (!(ifp->if_flags & IFF_RUNNING))
1585 				uath_init(ifp);
1586 		} else {
1587 			if (ifp->if_flags & IFF_RUNNING)
1588 				uath_stop(ifp, 1);
1589 		}
1590 		break;
1591 
1592 	case SIOCADDMULTI:
1593 	case SIOCDELMULTI:
1594 		ifr = (struct ifreq *)data;
1595 		error = (cmd == SIOCADDMULTI) ?
1596 		    ether_addmulti(ifr, &ic->ic_ac) :
1597 		    ether_delmulti(ifr, &ic->ic_ac);
1598 		if (error == ENETRESET)
1599 			error = 0;
1600 		break;
1601 
1602 	default:
1603 		error = ieee80211_ioctl(ifp, cmd, data);
1604 	}
1605 
1606 	if (error == ENETRESET) {
1607 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1608 		    (IFF_UP | IFF_RUNNING))
1609 			uath_init(ifp);
1610 		error = 0;
1611 	}
1612 
1613 	splx(s);
1614 
1615 	return error;
1616 }
1617 
1618 Static int
1619 uath_query_eeprom(struct uath_softc *sc)
1620 {
1621 	uint32_t tmp;
1622 	int error;
1623 
1624 	/* retrieve MAC address */
1625 	error = uath_read_eeprom(sc, UATH_EEPROM_MACADDR, sc->sc_ic.ic_myaddr);
1626 	if (error != 0) {
1627 		printf("%s: could not read MAC address\n",
1628 		    USBDEVNAME(sc->sc_dev));
1629 		return error;
1630 	}
1631 
1632 	/* retrieve the maximum frame size that the hardware can receive */
1633 	error = uath_read_eeprom(sc, UATH_EEPROM_RXBUFSZ, &tmp);
1634 	if (error != 0) {
1635 		printf("%s: could not read maximum Rx buffer size\n",
1636 		    USBDEVNAME(sc->sc_dev));
1637 		return error;
1638 	}
1639 	sc->rxbufsz = betoh32(tmp) & 0xfff;
1640 	DPRINTF(("maximum Rx buffer size %d\n", sc->rxbufsz));
1641 	return 0;
1642 }
1643 
1644 Static int
1645 uath_reset(struct uath_softc *sc)
1646 {
1647 	struct uath_cmd_setup setup;
1648 	uint32_t reg, val;
1649 	int s, error;
1650 
1651 	/* init device with some voodoo incantations.. */
1652 	setup.magic1 = htobe32(1);
1653 	setup.magic2 = htobe32(5);
1654 	setup.magic3 = htobe32(200);
1655 	setup.magic4 = htobe32(27);
1656 	s = splusb();
1657 	error = uath_cmd_write(sc, UATH_CMD_SETUP, &setup, sizeof setup,
1658 	    UATH_CMD_FLAG_ASYNC);
1659 	/* ..and wait until firmware notifies us that it is ready */
1660 	if (error == 0)
1661 		error = tsleep(UATH_COND_INIT(sc), PCATCH, "uathinit", 5 * hz);
1662 	splx(s);
1663 
1664 	/* read PHY registers */
1665 	for (reg = 0x09; reg <= 0x24; reg++) {
1666 		if (reg == 0x0b || reg == 0x0c)
1667 			continue;
1668 		if ((error = uath_read_reg(sc, reg, &val)) != 0)
1669 			return error;
1670 		DPRINTFN(2, ("reg 0x%02x=0x%08x\n", reg, val));
1671 	}
1672 	return error;
1673 }
1674 
1675 Static int
1676 uath_reset_tx_queues(struct uath_softc *sc)
1677 {
1678 	int ac, error;
1679 
1680 	for (ac = 0; ac < 4; ac++) {
1681 		const uint32_t qid = htobe32(UATH_AC_TO_QID(ac));
1682 
1683 		DPRINTF(("resetting Tx queue %d\n", UATH_AC_TO_QID(ac)));
1684 		error = uath_cmd_write(sc, UATH_CMD_RESET_QUEUE, &qid,
1685 		    sizeof qid, 0);
1686 		if (error != 0)
1687 			break;
1688 	}
1689 	return error;
1690 }
1691 
1692 Static int
1693 uath_wme_init(struct uath_softc *sc)
1694 {
1695 	struct uath_qinfo qinfo;
1696 	int ac, error;
1697 	static const struct uath_wme_settings uath_wme_11g[4] = {
1698 		{ 7, 4, 10,  0, 0 },	/* Background */
1699 		{ 3, 4, 10,  0, 0 },	/* Best-Effort */
1700 		{ 3, 3,  4, 26, 0 },	/* Video */
1701 		{ 2, 2,  3, 47, 0 }	/* Voice */
1702 	};
1703 
1704 	bzero(&qinfo, sizeof qinfo);
1705 	qinfo.size   = htobe32(32);
1706 	qinfo.magic1 = htobe32(1);	/* XXX ack policy? */
1707 	qinfo.magic2 = htobe32(1);
1708 	for (ac = 0; ac < 4; ac++) {
1709 		qinfo.qid      = htobe32(UATH_AC_TO_QID(ac));
1710 		qinfo.ac       = htobe32(ac);
1711 		qinfo.aifsn    = htobe32(uath_wme_11g[ac].aifsn);
1712 		qinfo.logcwmin = htobe32(uath_wme_11g[ac].logcwmin);
1713 		qinfo.logcwmax = htobe32(uath_wme_11g[ac].logcwmax);
1714 		qinfo.txop     = htobe32(UATH_TXOP_TO_US(
1715 				     uath_wme_11g[ac].txop));
1716 		qinfo.acm      = htobe32(uath_wme_11g[ac].acm);
1717 
1718 		DPRINTF(("setting up Tx queue %d\n", UATH_AC_TO_QID(ac)));
1719 		error = uath_cmd_write(sc, UATH_CMD_SET_QUEUE, &qinfo,
1720 		    sizeof qinfo, 0);
1721 		if (error != 0)
1722 			break;
1723 	}
1724 	return error;
1725 }
1726 
1727 Static int
1728 uath_set_chan(struct uath_softc *sc, struct ieee80211_channel *c)
1729 {
1730 #ifdef UATH_DEBUG
1731 	struct ieee80211com *ic = &sc->sc_ic;
1732 #endif
1733 	struct uath_set_chan chan;
1734 
1735 	bzero(&chan, sizeof chan);
1736 	chan.flags  = htobe32(0x1400);
1737 	chan.freq   = htobe32(c->ic_freq);
1738 	chan.magic1 = htobe32(20);
1739 	chan.magic2 = htobe32(50);
1740 	chan.magic3 = htobe32(1);
1741 
1742 	DPRINTF(("switching to channel %d\n", ieee80211_chan2ieee(ic, c)));
1743 	return uath_cmd_write(sc, UATH_CMD_SET_CHAN, &chan, sizeof chan, 0);
1744 }
1745 
1746 Static int
1747 uath_set_key(struct uath_softc *sc, const struct ieee80211_wepkey *wk,
1748     int index)
1749 {
1750 	struct uath_cmd_crypto crypto;
1751 	int i;
1752 
1753 	bzero(&crypto, sizeof crypto);
1754 	crypto.keyidx = htobe32(index);
1755 	crypto.magic1 = htobe32(1);
1756 	crypto.size   = htobe32(368);
1757 	crypto.mask   = htobe32(0xffff);
1758 	crypto.flags  = htobe32(0x80000068);
1759 	if (index != UATH_DEFAULT_KEY)
1760 		crypto.flags |= htobe32(index << 16);
1761 	memset(crypto.magic2, 0xff, sizeof crypto.magic2);
1762 
1763 	/*
1764 	 * Each byte of the key must be XOR'ed with 10101010 before being
1765 	 * transmitted to the firmware.
1766 	 */
1767 	for (i = 0; i < wk->wk_len; i++)
1768 		crypto.key[i] = wk->wk_key[i] ^ 0xaa;
1769 
1770 	DPRINTF(("setting crypto key index=%d len=%d\n", index, wk->wk_len));
1771 	return uath_cmd_write(sc, UATH_CMD_CRYPTO, &crypto, sizeof crypto, 0);
1772 }
1773 
1774 Static int
1775 uath_set_keys(struct uath_softc *sc)
1776 {
1777 	const struct ieee80211com *ic = &sc->sc_ic;
1778 	int i, error;
1779 
1780 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1781 		const struct ieee80211_wepkey *wk = &ic->ic_nw_keys[i];
1782 
1783 		if (wk->wk_len > 0 &&
1784 		    (error = uath_set_key(sc, wk, i)) != 0)
1785 			return error;
1786 	}
1787 	return uath_set_key(sc, &ic->ic_nw_keys[ic->ic_wep_txkey],
1788 	    UATH_DEFAULT_KEY);
1789 }
1790 
1791 Static int
1792 uath_set_rates(struct uath_softc *sc, const struct ieee80211_rateset *rs)
1793 {
1794 	struct uath_cmd_rates rates;
1795 
1796 	bzero(&rates, sizeof rates);
1797 	rates.magic1 = htobe32(0x02);
1798 	rates.magic2 = htobe32(0x21);
1799 	rates.nrates = rs->rs_nrates;
1800 	bcopy(rs->rs_rates, rates.rates, rs->rs_nrates);
1801 
1802 	DPRINTF(("setting supported rates nrates=%d\n", rs->rs_nrates));
1803 	return uath_cmd_write(sc, UATH_CMD_SET_RATES, &rates,
1804 	    3 * 4 + 1 + rs->rs_nrates, 0);
1805 }
1806 
1807 Static int
1808 uath_set_rxfilter(struct uath_softc *sc, uint32_t filter, uint32_t flags)
1809 {
1810 	struct uath_cmd_filter rxfilter;
1811 
1812 	rxfilter.filter = htobe32(filter);
1813 	rxfilter.flags  = htobe32(flags);
1814 
1815 	DPRINTF(("setting Rx filter=0x%x flags=0x%x\n", filter, flags));
1816 	return uath_cmd_write(sc, UATH_CMD_SET_FILTER, &rxfilter,
1817 	    sizeof rxfilter, 0);
1818 }
1819 
1820 Static int
1821 uath_set_led(struct uath_softc *sc, int which, int on)
1822 {
1823 	struct uath_cmd_led led;
1824 
1825 	led.which = htobe32(which);
1826 	led.state = htobe32(on ? UATH_LED_ON : UATH_LED_OFF);
1827 
1828 	DPRINTFN(2, ("switching %s led %s\n",
1829 	    (which == UATH_LED_LINK) ? "link" : "activity",
1830 	    on ? "on" : "off"));
1831 	return uath_cmd_write(sc, UATH_CMD_SET_LED, &led, sizeof led, 0);
1832 }
1833 
1834 Static int
1835 uath_switch_channel(struct uath_softc *sc, struct ieee80211_channel *c)
1836 {
1837 	uint32_t val;
1838 	int error;
1839 
1840 	/* set radio frequency */
1841 	if ((error = uath_set_chan(sc, c)) != 0) {
1842 		printf("%s: could not set channel\n", USBDEVNAME(sc->sc_dev));
1843 		return error;
1844 	}
1845 
1846 	/* reset Tx rings */
1847 	if ((error = uath_reset_tx_queues(sc)) != 0) {
1848 		printf("%s: could not reset Tx queues\n",
1849 		    USBDEVNAME(sc->sc_dev));
1850 		return error;
1851 	}
1852 
1853 	/* set Tx rings WME properties */
1854 	if ((error = uath_wme_init(sc)) != 0) {
1855 		printf("%s: could not init Tx queues\n",
1856 		    USBDEVNAME(sc->sc_dev));
1857 		return error;
1858 	}
1859 
1860 	val = htobe32(0);
1861 	error = uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 0);
1862 	if (error != 0) {
1863 		printf("%s: could not set state\n", USBDEVNAME(sc->sc_dev));
1864 		return error;
1865 	}
1866 
1867 	return uath_tx_null(sc);
1868 }
1869 
1870 Static int
1871 uath_init(struct ifnet *ifp)
1872 {
1873 	struct uath_softc *sc = ifp->if_softc;
1874 	struct ieee80211com *ic = &sc->sc_ic;
1875 	struct uath_cmd_31 cmd31;
1876 	uint32_t val;
1877 	int i, error;
1878 
1879 	/* reset data and command rings */
1880 	sc->tx_queued = sc->data_idx = sc->cmd_idx = 0;
1881 
1882 	val = htobe32(0);
1883 	(void)uath_cmd_write(sc, UATH_CMD_02, &val, sizeof val, 0);
1884 
1885 	/* set MAC address */
1886 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1887 	(void)uath_write_multi(sc, 0x13, ic->ic_myaddr, IEEE80211_ADDR_LEN);
1888 
1889 	(void)uath_write_reg(sc, 0x02, 0x00000001);
1890 	(void)uath_write_reg(sc, 0x0e, 0x0000003f);
1891 	(void)uath_write_reg(sc, 0x10, 0x00000001);
1892 	(void)uath_write_reg(sc, 0x06, 0x0000001e);
1893 
1894 	/*
1895 	 * Queue Rx data xfers.
1896 	 */
1897 	for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) {
1898 		struct uath_rx_data *data = &sc->rx_data[i];
1899 
1900 		usbd_setup_xfer(data->xfer, sc->data_rx_pipe, data, data->buf,
1901 		    sc->rxbufsz, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
1902 		    uath_data_rxeof);
1903 		error = usbd_transfer(data->xfer);
1904 		if (error != USBD_IN_PROGRESS && error != 0) {
1905 			printf("%s: could not queue Rx transfer\n",
1906 			    USBDEVNAME(sc->sc_dev));
1907 			goto fail;
1908 		}
1909 	}
1910 
1911 	error = uath_cmd_read(sc, UATH_CMD_07, 0, NULL, &val,
1912 	    UATH_CMD_FLAG_MAGIC);
1913 	if (error != 0) {
1914 		printf("%s: could not send read command 07h\n",
1915 		    USBDEVNAME(sc->sc_dev));
1916 		goto fail;
1917 	}
1918 	DPRINTF(("command 07h return code: %x\n", betoh32(val)));
1919 
1920 	/* set default channel */
1921 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1922 	if ((error = uath_set_chan(sc, ic->ic_bss->ni_chan)) != 0) {
1923 		printf("%s: could not set channel\n", USBDEVNAME(sc->sc_dev));
1924 		goto fail;
1925 	}
1926 
1927 	if ((error = uath_wme_init(sc)) != 0) {
1928 		printf("%s: could not setup WME parameters\n",
1929 		    USBDEVNAME(sc->sc_dev));
1930 		goto fail;
1931 	}
1932 
1933 	/* init MAC registers */
1934 	(void)uath_write_reg(sc, 0x19, 0x00000000);
1935 	(void)uath_write_reg(sc, 0x1a, 0x0000003c);
1936 	(void)uath_write_reg(sc, 0x1b, 0x0000003c);
1937 	(void)uath_write_reg(sc, 0x1c, 0x00000000);
1938 	(void)uath_write_reg(sc, 0x1e, 0x00000000);
1939 	(void)uath_write_reg(sc, 0x1f, 0x00000003);
1940 	(void)uath_write_reg(sc, 0x0c, 0x00000000);
1941 	(void)uath_write_reg(sc, 0x0f, 0x00000002);
1942 	(void)uath_write_reg(sc, 0x0a, 0x00000007);	/* XXX retry? */
1943 	(void)uath_write_reg(sc, 0x09, ic->ic_rtsthreshold);
1944 
1945 	val = htobe32(4);
1946 	(void)uath_cmd_write(sc, UATH_CMD_27, &val, sizeof val, 0);
1947 	(void)uath_cmd_write(sc, UATH_CMD_27, &val, sizeof val, 0);
1948 	(void)uath_cmd_write(sc, UATH_CMD_1B, NULL, 0, 0);
1949 
1950 	if ((error = uath_set_keys(sc)) != 0) {
1951 		printf("%s: could not set crypto keys\n",
1952 		    USBDEVNAME(sc->sc_dev));
1953 		goto fail;
1954 	}
1955 
1956 	/* enable Rx */
1957 	(void)uath_set_rxfilter(sc, 0x0000, 4);
1958 	(void)uath_set_rxfilter(sc, 0x0817, 1);
1959 
1960 	cmd31.magic1 = htobe32(0xffffffff);
1961 	cmd31.magic2 = htobe32(0xffffffff);
1962 	(void)uath_cmd_write(sc, UATH_CMD_31, &cmd31, sizeof cmd31, 0);
1963 
1964 	ifp->if_flags &= ~IFF_OACTIVE;
1965 	ifp->if_flags |= IFF_RUNNING;
1966 
1967 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
1968 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1969 	else
1970 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1971 
1972 	return 0;
1973 
1974 fail:	uath_stop(ifp, 1);
1975 	return error;
1976 }
1977 
1978 Static void
1979 uath_stop(struct ifnet *ifp, int disable)
1980 {
1981 	struct uath_softc *sc = ifp->if_softc;
1982 	struct ieee80211com *ic = &sc->sc_ic;
1983 	uint32_t val;
1984 	int s;
1985 
1986 	s = splusb();
1987 
1988 	sc->sc_tx_timer = 0;
1989 	ifp->if_timer = 0;
1990 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1991 
1992 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
1993 
1994 	val = htobe32(0);
1995 	(void)uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 0);
1996 	(void)uath_cmd_write(sc, UATH_CMD_RESET, NULL, 0, 0);
1997 
1998 	val = htobe32(0);
1999 	(void)uath_cmd_write(sc, UATH_CMD_15, &val, sizeof val, 0);
2000 
2001 #if 0
2002 	(void)uath_cmd_read(sc, UATH_CMD_SHUTDOWN, NULL, 0, NULL,
2003 	    UATH_CMD_FLAG_MAGIC);
2004 #endif
2005 
2006 	/* abort any pending transfers */
2007 	usbd_abort_pipe(sc->data_tx_pipe);
2008 	usbd_abort_pipe(sc->data_rx_pipe);
2009 	usbd_abort_pipe(sc->cmd_tx_pipe);
2010 	usbd_abort_pipe(sc->cmd_rx_pipe);
2011 
2012 	splx(s);
2013 }
2014 
2015 /*
2016  * Load the MIPS R4000 microcode into the device.  Once the image is loaded,
2017  * the device will detach itself from the bus and reattach later with a new
2018  * product Id (a la ezusb).  XXX this could also be implemented in userland
2019  * through /dev/ugen.
2020  */
2021 Static int
2022 uath_loadfirmware(struct uath_softc *sc, const u_char *fw, int len)
2023 {
2024 	usbd_xfer_handle ctlxfer, txxfer, rxxfer;
2025 	struct uath_fwblock *txblock, *rxblock;
2026 	uint8_t *txdata;
2027 	int error = 0;
2028 
2029 	if ((ctlxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
2030 		printf("%s: could not allocate Tx control xfer\n",
2031 		    USBDEVNAME(sc->sc_dev));
2032 		error = USBD_NOMEM;
2033 		goto fail1;
2034 	}
2035 	txblock = usbd_alloc_buffer(ctlxfer, sizeof (struct uath_fwblock));
2036 	if (txblock == NULL) {
2037 		printf("%s: could not allocate Tx control block\n",
2038 		    USBDEVNAME(sc->sc_dev));
2039 		error = USBD_NOMEM;
2040 		goto fail2;
2041 	}
2042 
2043 	if ((txxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
2044 		printf("%s: could not allocate Tx xfer\n",
2045 		    USBDEVNAME(sc->sc_dev));
2046 		error = USBD_NOMEM;
2047 		goto fail2;
2048 	}
2049 	txdata = usbd_alloc_buffer(txxfer, UATH_MAX_FWBLOCK_SIZE);
2050 	if (txdata == NULL) {
2051 		printf("%s: could not allocate Tx buffer\n",
2052 		    USBDEVNAME(sc->sc_dev));
2053 		error = USBD_NOMEM;
2054 		goto fail3;
2055 	}
2056 
2057 	if ((rxxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
2058 		printf("%s: could not allocate Rx control xfer\n",
2059 		    USBDEVNAME(sc->sc_dev));
2060 		error = USBD_NOMEM;
2061 		goto fail3;
2062 	}
2063 	rxblock = usbd_alloc_buffer(rxxfer, sizeof (struct uath_fwblock));
2064 	if (rxblock == NULL) {
2065 		printf("%s: could not allocate Rx control block\n",
2066 		    USBDEVNAME(sc->sc_dev));
2067 		error = USBD_NOMEM;
2068 		goto fail4;
2069 	}
2070 
2071 	bzero(txblock, sizeof (struct uath_fwblock));
2072 	txblock->flags = htobe32(UATH_WRITE_BLOCK);
2073 	txblock->total = htobe32(len);
2074 
2075 	while (len > 0) {
2076 		int mlen = min(len, UATH_MAX_FWBLOCK_SIZE);
2077 
2078 		txblock->remain = htobe32(len - mlen);
2079 		txblock->len = htobe32(mlen);
2080 
2081 		DPRINTF(("sending firmware block: %d bytes remaining\n",
2082 		    len - mlen));
2083 
2084 		/* send firmware block meta-data */
2085 		usbd_setup_xfer(ctlxfer, sc->cmd_tx_pipe, sc, txblock,
2086 		    sizeof (struct uath_fwblock), USBD_NO_COPY,
2087 		    UATH_CMD_TIMEOUT, NULL);
2088 		if ((error = usbd_sync_transfer(ctlxfer)) != 0) {
2089 			printf("%s: could not send firmware block info\n",
2090 			    USBDEVNAME(sc->sc_dev));
2091 			break;
2092 		}
2093 
2094 		/* send firmware block data */
2095 		bcopy(fw, txdata, mlen);
2096 		usbd_setup_xfer(txxfer, sc->data_tx_pipe, sc, txdata, mlen,
2097 		    USBD_NO_COPY, UATH_DATA_TIMEOUT, NULL);
2098 		if ((error = usbd_sync_transfer(txxfer)) != 0) {
2099 			printf("%s: could not send firmware block data\n",
2100 			    USBDEVNAME(sc->sc_dev));
2101 			break;
2102 		}
2103 
2104 		/* wait for ack from firmware */
2105 		usbd_setup_xfer(rxxfer, sc->cmd_rx_pipe, sc, rxblock,
2106 		    sizeof (struct uath_fwblock), USBD_SHORT_XFER_OK |
2107 		    USBD_NO_COPY, UATH_CMD_TIMEOUT, NULL);
2108 		if ((error = usbd_sync_transfer(rxxfer)) != 0) {
2109 			printf("%s: could not read firmware answer\n",
2110 			    USBDEVNAME(sc->sc_dev));
2111 			break;
2112 		}
2113 
2114 		DPRINTFN(2, ("rxblock flags=0x%x total=%d\n",
2115 		    betoh32(rxblock->flags), betoh32(rxblock->rxtotal)));
2116 		fw += mlen;
2117 		len -= mlen;
2118 	}
2119 
2120 fail4:	usbd_free_xfer(rxxfer);
2121 fail3:	usbd_free_xfer(txxfer);
2122 fail2:	usbd_free_xfer(ctlxfer);
2123 fail1:	return error;
2124 }
2125 
2126 Static int
2127 uath_activate(device_ptr_t self, enum devact act)
2128 {
2129 	switch (act) {
2130 	case DVACT_ACTIVATE:
2131 		break;
2132 
2133 	case DVACT_DEACTIVATE:
2134 		/*if_deactivate(&sc->sc_ic.ic_if);*/
2135 		break;
2136 	}
2137 	return 0;
2138 }
2139