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