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