xref: /openbsd-src/sys/dev/usb/if_uath.c (revision 6a54674fc1702685a23219f4ad615c28c32bc7f1)
1 /*	$OpenBSD: if_uath.c,v 1.13 2006/12/03 16:09:21 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/usbdivar.h>	/* needs_reattach() */
74 #include <dev/usb/usbdi_util.h>
75 #include <dev/usb/usbdevs.h>
76 
77 #include <dev/usb/if_uathreg.h>
78 #include <dev/usb/if_uathvar.h>
79 
80 #ifdef USB_DEBUG
81 #define UATH_DEBUG
82 #endif
83 
84 #ifdef UATH_DEBUG
85 #define DPRINTF(x)	do { if (uath_debug) logprintf x; } while (0)
86 #define DPRINTFN(n, x)	do { if (uath_debug >= (n)) logprintf x; } while (0)
87 int uath_debug = 1;
88 #else
89 #define DPRINTF(x)
90 #define DPRINTFN(n, x)
91 #endif
92 
93 /*-
94  * Various supported device vendors/products.
95  * UB51: AR5005UG 802.11b/g, UB52: AR5005UX 802.11a/b/g
96  */
97 #define UATH_DEV(v, p, f)						\
98 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, (f) },		\
99 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p##_NF },		\
100 	    (f) | UATH_FLAG_PRE_FIRMWARE }
101 #define UATH_DEV_UG(v, p)	UATH_DEV(v, p, 0)
102 #define UATH_DEV_UX(v, p)	UATH_DEV(v, p, UATH_FLAG_ABG)
103 static const struct uath_type {
104 	struct usb_devno	dev;
105 	unsigned int		flags;
106 #define UATH_FLAG_PRE_FIRMWARE	(1 << 0)
107 #define UATH_FLAG_ABG		(1 << 1)
108 } uath_devs[] = {
109 	UATH_DEV_UG(ATHEROS,		AR5523),
110 	UATH_DEV_UG(ATHEROS2,		AR5523_1),
111 	UATH_DEV_UG(ATHEROS2,		AR5523_2),
112 	UATH_DEV_UX(ATHEROS2,		AR5523_3),
113 	UATH_DEV_UG(CONCEPTRONIC,	AR5523_1),
114 	UATH_DEV_UX(CONCEPTRONIC,	AR5523_2),
115 	UATH_DEV_UX(DLINK,		DWLAG122),
116 	UATH_DEV_UX(DLINK,		DWLAG132),
117 	UATH_DEV_UG(DLINK,		DWLG132),
118 	UATH_DEV_UG(GIGASET,		AR5523),
119 	UATH_DEV_UG(GIGASET,		SMCWUSBTG),
120 	UATH_DEV_UG(GLOBALSUN,		AR5523_1),
121 	UATH_DEV_UX(GLOBALSUN,		AR5523_2),
122 	UATH_DEV_UX(NETGEAR,		WG111U),
123 	UATH_DEV_UG(NETGEAR3,		WG111T),
124 	UATH_DEV_UG(NETGEAR3,		WPN111),
125 	UATH_DEV_UG(UMEDIA,		AR5523_1),
126 	UATH_DEV_UX(UMEDIA,		AR5523_2),
127 	UATH_DEV_UG(UMEDIA,		TEW444UBEU),
128 	UATH_DEV_UG(WISTRONNEWEB,	AR5523_1),
129 	UATH_DEV_UX(WISTRONNEWEB,	AR5523_2),
130 	UATH_DEV_UG(ZCOM,		AR5523)
131 };
132 #define uath_lookup(v, p)	\
133 	((const struct uath_type *)usb_lookup(uath_devs, v, p))
134 
135 Static void	uath_attachhook(void *);
136 Static int	uath_open_pipes(struct uath_softc *);
137 Static void	uath_close_pipes(struct uath_softc *);
138 Static int	uath_alloc_tx_data_list(struct uath_softc *);
139 Static void	uath_free_tx_data_list(struct uath_softc *);
140 Static int	uath_alloc_rx_data_list(struct uath_softc *);
141 Static void	uath_free_rx_data_list(struct uath_softc *);
142 Static void	uath_free_rx_data(caddr_t, u_int, void *);
143 Static int	uath_alloc_tx_cmd_list(struct uath_softc *);
144 Static void	uath_free_tx_cmd_list(struct uath_softc *);
145 Static int	uath_alloc_rx_cmd_list(struct uath_softc *);
146 Static void	uath_free_rx_cmd_list(struct uath_softc *);
147 Static int	uath_media_change(struct ifnet *);
148 Static void	uath_stat(void *);
149 Static void	uath_next_scan(void *);
150 Static void	uath_task(void *);
151 Static int	uath_newstate(struct ieee80211com *, enum ieee80211_state,
152 		    int);
153 #ifdef UATH_DEBUG
154 Static void	uath_dump_cmd(const uint8_t *, int, char);
155 #endif
156 Static int	uath_cmd(struct uath_softc *, uint32_t, const void *, int,
157 		    void *, int);
158 Static int	uath_cmd_write(struct uath_softc *, uint32_t, const void *,
159 		    int, int);
160 Static int	uath_cmd_read(struct uath_softc *, uint32_t, const void *,
161 		    int, void *, int);
162 Static int	uath_write_reg(struct uath_softc *, uint32_t, uint32_t);
163 Static int	uath_write_multi(struct uath_softc *, uint32_t, const void *,
164 		    int);
165 Static int	uath_read_reg(struct uath_softc *, uint32_t, uint32_t *);
166 Static int	uath_read_eeprom(struct uath_softc *, uint32_t, void *);
167 Static void	uath_cmd_rxeof(usbd_xfer_handle, usbd_private_handle,
168 		    usbd_status);
169 Static void	uath_data_rxeof(usbd_xfer_handle, usbd_private_handle,
170 		    usbd_status);
171 Static void	uath_data_txeof(usbd_xfer_handle, usbd_private_handle,
172 		    usbd_status);
173 Static int	uath_tx_null(struct uath_softc *);
174 Static int	uath_tx_data(struct uath_softc *, struct mbuf *,
175 		    struct ieee80211_node *);
176 Static void	uath_start(struct ifnet *);
177 Static void	uath_watchdog(struct ifnet *);
178 Static int	uath_ioctl(struct ifnet *, u_long, caddr_t);
179 Static int	uath_query_eeprom(struct uath_softc *);
180 Static int	uath_reset(struct uath_softc *);
181 Static int	uath_reset_tx_queues(struct uath_softc *);
182 Static int	uath_wme_init(struct uath_softc *);
183 Static int	uath_set_chan(struct uath_softc *, struct ieee80211_channel *);
184 Static int	uath_set_key(struct uath_softc *,
185 		    const struct ieee80211_wepkey *, int);
186 Static int	uath_set_keys(struct uath_softc *);
187 Static int	uath_set_rates(struct uath_softc *,
188 		    const struct ieee80211_rateset *);
189 Static int	uath_set_rxfilter(struct uath_softc *, uint32_t, uint32_t);
190 Static int	uath_set_led(struct uath_softc *, int, int);
191 Static int	uath_switch_channel(struct uath_softc *,
192 		    struct ieee80211_channel *);
193 Static int	uath_init(struct ifnet *);
194 Static void	uath_stop(struct ifnet *, int);
195 Static int	uath_loadfirmware(struct uath_softc *, const u_char *, int);
196 Static int	uath_activate(device_ptr_t, enum devact);
197 
198 USB_DECLARE_DRIVER(uath);
199 
200 USB_MATCH(uath)
201 {
202 	USB_MATCH_START(uath, uaa);
203 
204 	if (uaa->iface != NULL)
205 		return UMATCH_NONE;
206 
207 	return (uath_lookup(uaa->vendor, uaa->product) != NULL) ?
208 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
209 }
210 
211 Static void
212 uath_attachhook(void *xsc)
213 {
214 	struct uath_softc *sc = xsc;
215 	u_char *fw;
216 	size_t size;
217 	int error;
218 
219 	if ((error = loadfirmware("uath-ar5523", &fw, &size)) != 0) {
220 		printf("%s: could not read firmware (error=%d)\n",
221 		    USBDEVNAME(sc->sc_dev), error);
222 		return;
223 	}
224 
225 	error = uath_loadfirmware(sc, fw, size);
226 	free(fw, M_DEVBUF);
227 
228 	if (error == 0) {
229 		usb_port_status_t status;
230 
231 		/*
232 		 * Hack alert: the device doesn't always gracefully detach
233 		 * from the bus after a firmware upload.  We need to force
234 		 * a port reset and a re-exploration on the parent hub.
235 		 */
236 		usbd_reset_port(sc->sc_uhub, sc->sc_port, &status);
237 		usb_needs_reattach(sc->sc_udev);
238 	} else {
239 		printf("%s: could not load firmware (error=%s)\n",
240 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
241 	}
242 }
243 
244 USB_ATTACH(uath)
245 {
246 	USB_ATTACH_START(uath, sc, uaa);
247 	struct ieee80211com *ic = &sc->sc_ic;
248 	struct ifnet *ifp = &ic->ic_if;
249 	usbd_status error;
250 	char *devinfop;
251 	int i;
252 
253 	sc->sc_udev = uaa->device;
254 	sc->sc_uhub = uaa->device->myhub;
255 	sc->sc_port = uaa->port;
256 
257 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
258 	USB_ATTACH_SETUP;
259 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
260 	usbd_devinfo_free(devinfop);
261 
262 	sc->sc_flags = uath_lookup(uaa->vendor, uaa->product)->flags;
263 
264 	if (usbd_set_config_no(sc->sc_udev, UATH_CONFIG_NO, 0) != 0) {
265 		printf("%s: could not set configuration no\n",
266 		    USBDEVNAME(sc->sc_dev));
267 		USB_ATTACH_ERROR_RETURN;
268 	}
269 
270 	/* get the first interface handle */
271 	error = usbd_device2interface_handle(sc->sc_udev, UATH_IFACE_INDEX,
272 	    &sc->sc_iface);
273 	if (error != 0) {
274 		printf("%s: could not get interface handle\n",
275 		    USBDEVNAME(sc->sc_dev));
276 		USB_ATTACH_ERROR_RETURN;
277 	}
278 
279 	/*
280 	 * We must open the pipes early because they're used to upload the
281 	 * firmware (pre-firmware devices) or to send firmware commands.
282 	 */
283 	if (uath_open_pipes(sc) != 0) {
284 		printf("%s: could not open pipes\n", USBDEVNAME(sc->sc_dev));
285 		USB_ATTACH_ERROR_RETURN;
286 	}
287 
288 	if (sc->sc_flags & UATH_FLAG_PRE_FIRMWARE) {
289 		if (rootvp == NULL)
290 			mountroothook_establish(uath_attachhook, sc);
291 		else
292 			uath_attachhook(sc);
293 		USB_ATTACH_SUCCESS_RETURN;
294 	}
295 
296 	/*
297 	 * Only post-firmware devices here.
298 	 */
299 	usb_init_task(&sc->sc_task, uath_task, sc);
300 	timeout_set(&sc->scan_to, uath_next_scan, sc);
301 	timeout_set(&sc->stat_to, uath_stat, sc);
302 
303 	/*
304 	 * Allocate xfers for firmware commands.
305 	 */
306 	if (uath_alloc_tx_cmd_list(sc) != 0) {
307 		printf("%s: could not allocate Tx command list\n",
308 		    USBDEVNAME(sc->sc_dev));
309 		goto fail1;
310 	}
311 	if (uath_alloc_rx_cmd_list(sc) != 0) {
312 		printf("%s: could not allocate Rx command list\n",
313 		    USBDEVNAME(sc->sc_dev));
314 		goto fail2;
315 	}
316 
317 	/*
318 	 * Queue Rx command xfers.
319 	 */
320 	for (i = 0; i < UATH_RX_CMD_LIST_COUNT; i++) {
321 		struct uath_rx_cmd *cmd = &sc->rx_cmd[i];
322 
323 		usbd_setup_xfer(cmd->xfer, sc->cmd_rx_pipe, cmd, cmd->buf,
324 		    UATH_MAX_RXCMDSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
325 		    USBD_NO_TIMEOUT, uath_cmd_rxeof);
326 		error = usbd_transfer(cmd->xfer);
327 		if (error != USBD_IN_PROGRESS && error != 0) {
328 			printf("%s: could not queue Rx command xfer\n",
329 			    USBDEVNAME(sc->sc_dev));
330 			goto fail3;
331 		}
332 	}
333 
334 	/*
335 	 * We're now ready to send/receive firmware commands.
336 	 */
337 	if (uath_reset(sc) != 0) {
338 		printf("%s: could not initialize adapter\n",
339 		    USBDEVNAME(sc->sc_dev));
340 		goto fail3;
341 	}
342 	if (uath_query_eeprom(sc) != 0) {
343 		printf("%s: could not read EEPROM\n", USBDEVNAME(sc->sc_dev));
344 		goto fail3;
345 	}
346 
347 	printf("%s: MAC/BBP AR5523, RF AR%c112, address %s\n",
348 	    USBDEVNAME(sc->sc_dev), (sc->sc_flags & UATH_FLAG_ABG) ? '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] = ieee80211_std_rateset_11b;
378 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_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 = splnet();
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 	ieee80211_ifdetach(ifp);	/* free all nodes */
453 	if_detach(ifp);
454 
455 	sc->sc_dying = 1;
456 	DPRINTF(("reclaiming %d references\n", sc->sc_refcnt));
457 	while (sc->sc_refcnt > 0)
458 		(void)tsleep(UATH_COND_NOREF(sc), 0, "uathdet", 0);
459 	DPRINTF(("all references reclaimed\n"));
460 
461 	/* abort and free xfers */
462 	uath_free_tx_data_list(sc);
463 	uath_free_rx_data_list(sc);
464 	uath_free_tx_cmd_list(sc);
465 	uath_free_rx_cmd_list(sc);
466 
467 	/* close Tx/Rx pipes */
468 	uath_close_pipes(sc);
469 
470 	splx(s);
471 
472 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
473 	    USBDEV(sc->sc_dev));
474 
475 	return 0;
476 }
477 
478 Static int
479 uath_open_pipes(struct uath_softc *sc)
480 {
481 	int error;
482 
483 	/*
484 	 * XXX pipes numbers are hardcoded because we don't have any way
485 	 * to distinguish the data pipes from the firmware command pipes
486 	 * (both are bulk pipes) using the endpoints descriptors.
487 	 */
488 	error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
489 	    &sc->cmd_tx_pipe);
490 	if (error != 0) {
491 		printf("%s: could not open Tx command pipe: %s\n",
492 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
493 		goto fail;
494 	}
495 
496 	error = usbd_open_pipe(sc->sc_iface, 0x02, USBD_EXCLUSIVE_USE,
497 	    &sc->data_tx_pipe);
498 	if (error != 0) {
499 		printf("%s: could not open Tx data pipe: %s\n",
500 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
501 		goto fail;
502 	}
503 
504 	error = usbd_open_pipe(sc->sc_iface, 0x81, USBD_EXCLUSIVE_USE,
505 	    &sc->cmd_rx_pipe);
506 	if (error != 0) {
507 		printf("%s: could not open Rx command pipe: %s\n",
508 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
509 		goto fail;
510 	}
511 
512 	error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
513 	    &sc->data_rx_pipe);
514 	if (error != 0) {
515 		printf("%s: could not open Rx data pipe: %s\n",
516 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
517 		goto fail;
518 	}
519 
520 	return 0;
521 
522 fail:	uath_close_pipes(sc);
523 	return error;
524 }
525 
526 Static void
527 uath_close_pipes(struct uath_softc *sc)
528 {
529 	/* assumes no transfers are pending on the pipes */
530 
531 	if (sc->data_tx_pipe != NULL)
532 		usbd_close_pipe(sc->data_tx_pipe);
533 
534 	if (sc->data_rx_pipe != NULL)
535 		usbd_close_pipe(sc->data_rx_pipe);
536 
537 	if (sc->cmd_tx_pipe != NULL)
538 		usbd_close_pipe(sc->cmd_tx_pipe);
539 
540 	if (sc->cmd_rx_pipe != NULL)
541 		usbd_close_pipe(sc->cmd_rx_pipe);
542 }
543 
544 Static int
545 uath_alloc_tx_data_list(struct uath_softc *sc)
546 {
547 	int i, error;
548 
549 	for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) {
550 		struct uath_tx_data *data = &sc->tx_data[i];
551 
552 		data->sc = sc;	/* backpointer for callbacks */
553 
554 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
555 		if (data->xfer == NULL) {
556 			printf("%s: could not allocate xfer\n",
557 			    USBDEVNAME(sc->sc_dev));
558 			error = ENOMEM;
559 			goto fail;
560 		}
561 		data->buf = usbd_alloc_buffer(data->xfer, UATH_MAX_TXBUFSZ);
562 		if (data->buf == NULL) {
563 			printf("%s: could not allocate xfer buffer\n",
564 			    USBDEVNAME(sc->sc_dev));
565 			error = ENOMEM;
566 			goto fail;
567 		}
568 	}
569 	return 0;
570 
571 fail:	uath_free_tx_data_list(sc);
572 	return error;
573 }
574 
575 Static void
576 uath_free_tx_data_list(struct uath_softc *sc)
577 {
578 	int i;
579 
580 	/* make sure no transfers are pending */
581 	usbd_abort_pipe(sc->data_tx_pipe);
582 
583 	for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++)
584 		if (sc->tx_data[i].xfer != NULL)
585 			usbd_free_xfer(sc->tx_data[i].xfer);
586 }
587 
588 Static int
589 uath_alloc_rx_data_list(struct uath_softc *sc)
590 {
591 	int i, error;
592 
593 	SLIST_INIT(&sc->rx_freelist);
594 	for (i = 0; i < UATH_RX_DATA_POOL_COUNT; i++) {
595 		struct uath_rx_data *data = &sc->rx_data[i];
596 
597 		data->sc = sc;	/* backpointer for callbacks */
598 
599 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
600 		if (data->xfer == NULL) {
601 			printf("%s: could not allocate xfer\n",
602 			    USBDEVNAME(sc->sc_dev));
603 			error = ENOMEM;
604 			goto fail;
605 		}
606 		data->buf = usbd_alloc_buffer(data->xfer, sc->rxbufsz);
607 		if (data->buf == NULL) {
608 			printf("%s: could not allocate xfer buffer\n",
609 			    USBDEVNAME(sc->sc_dev));
610 			error = ENOMEM;
611 			goto fail;
612 		}
613 		SLIST_INSERT_HEAD(&sc->rx_freelist, data, next);
614 	}
615 	return 0;
616 
617 fail:	uath_free_rx_data_list(sc);
618 	return error;
619 }
620 
621 Static void
622 uath_free_rx_data_list(struct uath_softc *sc)
623 {
624 	int i;
625 
626 	/* make sure no transfers are pending */
627 	usbd_abort_pipe(sc->data_rx_pipe);
628 
629 	for (i = 0; i < UATH_RX_DATA_POOL_COUNT; i++)
630 		if (sc->rx_data[i].xfer != NULL)
631 			usbd_free_xfer(sc->rx_data[i].xfer);
632 }
633 
634 Static void
635 uath_free_rx_data(caddr_t buf, u_int size, void *arg)
636 {
637 	struct uath_rx_data *data = arg;
638 	struct uath_softc *sc = data->sc;
639 
640 	/* put the buffer back in the free list */
641 	SLIST_INSERT_HEAD(&sc->rx_freelist, data, next);
642 
643 	/* release reference to softc */
644 	if (--sc->sc_refcnt == 0 && sc->sc_dying)
645 		wakeup(UATH_COND_NOREF(sc));
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 	sc->sc_state = nstate;
927 	sc->sc_arg = arg;
928 	if (curproc != NULL) {
929 		uath_task(sc);
930 	} else {
931 		/* do it in a process context */
932 		usb_add_task(sc->sc_udev, &sc->sc_task);
933 	}
934 	return 0;
935 }
936 
937 #ifdef UATH_DEBUG
938 Static void
939 uath_dump_cmd(const uint8_t *buf, int len, char prefix)
940 {
941 	int i;
942 
943 	for (i = 0; i < len; i++) {
944 		if ((i % 16) == 0)
945 			printf("\n%c ", prefix);
946 		else if ((i % 4) == 0)
947 			printf(" ");
948 		printf("%02x", buf[i]);
949 	}
950 	printf("\n");
951 }
952 #endif
953 
954 /*
955  * Low-level function to send read or write commands to the firmware.
956  */
957 Static int
958 uath_cmd(struct uath_softc *sc, uint32_t code, const void *idata, int ilen,
959     void *odata, int flags)
960 {
961 	struct uath_cmd_hdr *hdr;
962 	struct uath_tx_cmd *cmd;
963 	uint16_t xferflags;
964 	int s, xferlen, error;
965 
966 	/* grab a xfer */
967 	cmd = &sc->tx_cmd[sc->cmd_idx];
968 
969 	/* always bulk-out a multiple of 4 bytes */
970 	xferlen = (sizeof (struct uath_cmd_hdr) + ilen + 3) & ~3;
971 
972 	hdr = (struct uath_cmd_hdr *)cmd->buf;
973 	bzero(hdr, sizeof (struct uath_cmd_hdr));
974 	hdr->len   = htobe32(xferlen);
975 	hdr->code  = htobe32(code);
976 	hdr->priv  = sc->cmd_idx;	/* don't care about endianness */
977 	hdr->magic = htobe32((flags & UATH_CMD_FLAG_MAGIC) ? 1 << 24 : 0);
978 	bcopy(idata, (uint8_t *)(hdr + 1), ilen);
979 
980 #ifdef UATH_DEBUG
981 	if (uath_debug >= 5) {
982 		printf("sending command code=0x%02x flags=0x%x index=%u",
983 		    code, flags, sc->cmd_idx);
984 		uath_dump_cmd(cmd->buf, xferlen, '+');
985 	}
986 #endif
987 	xferflags = USBD_FORCE_SHORT_XFER | USBD_NO_COPY;
988 	if (!(flags & UATH_CMD_FLAG_READ)) {
989 		if (!(flags & UATH_CMD_FLAG_ASYNC))
990 			xferflags |= USBD_SYNCHRONOUS;
991 	} else
992 		s = splusb();
993 
994 	cmd->odata = odata;
995 
996 	usbd_setup_xfer(cmd->xfer, sc->cmd_tx_pipe, cmd, cmd->buf, xferlen,
997 	    xferflags, UATH_CMD_TIMEOUT, NULL);
998 	error = usbd_transfer(cmd->xfer);
999 	if (error != USBD_IN_PROGRESS && error != 0) {
1000 		if (flags & UATH_CMD_FLAG_READ)
1001 			splx(s);
1002 		printf("%s: could not send command 0x%x (error=%s)\n",
1003 		    USBDEVNAME(sc->sc_dev), code, usbd_errstr(error));
1004 		return error;
1005 	}
1006 	sc->cmd_idx = (sc->cmd_idx + 1) % UATH_TX_CMD_LIST_COUNT;
1007 
1008 	if (!(flags & UATH_CMD_FLAG_READ))
1009 		return 0;	/* write: don't wait for reply */
1010 
1011 	/* wait at most two seconds for command reply */
1012 	error = tsleep(cmd, PCATCH, "uathcmd", 2 * hz);
1013 	cmd->odata = NULL;	/* in case answer is received too late */
1014 	splx(s);
1015 	if (error != 0) {
1016 		printf("%s: timeout waiting for command reply\n",
1017 		    USBDEVNAME(sc->sc_dev));
1018 	}
1019 	return error;
1020 }
1021 
1022 Static int
1023 uath_cmd_write(struct uath_softc *sc, uint32_t code, const void *data, int len,
1024     int flags)
1025 {
1026 	flags &= ~UATH_CMD_FLAG_READ;
1027 	return uath_cmd(sc, code, data, len, NULL, flags);
1028 }
1029 
1030 Static int
1031 uath_cmd_read(struct uath_softc *sc, uint32_t code, const void *idata,
1032     int ilen, void *odata, int flags)
1033 {
1034 	flags |= UATH_CMD_FLAG_READ;
1035 	return uath_cmd(sc, code, idata, ilen, odata, flags);
1036 }
1037 
1038 Static int
1039 uath_write_reg(struct uath_softc *sc, uint32_t reg, uint32_t val)
1040 {
1041 	struct uath_write_mac write;
1042 	int error;
1043 
1044 	write.reg = htobe32(reg);
1045 	write.len = htobe32(0);	/* 0 = single write */
1046 	*(uint32_t *)write.data = htobe32(val);
1047 
1048 	error = uath_cmd_write(sc, UATH_CMD_WRITE_MAC, &write,
1049 	    3 * sizeof (uint32_t), 0);
1050 	if (error != 0) {
1051 		printf("%s: could not write register 0x%02x\n",
1052 		    USBDEVNAME(sc->sc_dev), reg);
1053 	}
1054 	return error;
1055 }
1056 
1057 Static int
1058 uath_write_multi(struct uath_softc *sc, uint32_t reg, const void *data,
1059     int len)
1060 {
1061 	struct uath_write_mac write;
1062 	int error;
1063 
1064 	write.reg = htobe32(reg);
1065 	write.len = htobe32(len);
1066 	bcopy(data, write.data, len);
1067 
1068 	/* properly handle the case where len is zero (reset) */
1069 	error = uath_cmd_write(sc, UATH_CMD_WRITE_MAC, &write,
1070 	    (len == 0) ? sizeof (uint32_t) : 2 * sizeof (uint32_t) + len, 0);
1071 	if (error != 0) {
1072 		printf("%s: could not write %d bytes to register 0x%02x\n",
1073 		    USBDEVNAME(sc->sc_dev), len, reg);
1074 	}
1075 	return error;
1076 }
1077 
1078 Static int
1079 uath_read_reg(struct uath_softc *sc, uint32_t reg, uint32_t *val)
1080 {
1081 	struct uath_read_mac read;
1082 	int error;
1083 
1084 	reg = htobe32(reg);
1085 	error = uath_cmd_read(sc, UATH_CMD_READ_MAC, &reg, sizeof reg, &read,
1086 	    0);
1087 	if (error != 0) {
1088 		printf("%s: could not read register 0x%02x\n",
1089 		    USBDEVNAME(sc->sc_dev), betoh32(reg));
1090 		return error;
1091 	}
1092 	*val = betoh32(*(uint32_t *)read.data);
1093 	return error;
1094 }
1095 
1096 Static int
1097 uath_read_eeprom(struct uath_softc *sc, uint32_t reg, void *odata)
1098 {
1099 	struct uath_read_mac read;
1100 	int len, error;
1101 
1102 	reg = htobe32(reg);
1103 	error = uath_cmd_read(sc, UATH_CMD_READ_EEPROM, &reg, sizeof reg,
1104 	    &read, 0);
1105 	if (error != 0) {
1106 		printf("%s: could not read EEPROM offset 0x%02x\n",
1107 		    USBDEVNAME(sc->sc_dev), betoh32(reg));
1108 		return error;
1109 	}
1110 	len = betoh32(read.len);
1111 	bcopy(read.data, odata, (len == 0) ? sizeof (uint32_t) : len);
1112 	return error;
1113 }
1114 
1115 Static void
1116 uath_cmd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1117     usbd_status status)
1118 {
1119 	struct uath_rx_cmd *cmd = priv;
1120 	struct uath_softc *sc = cmd->sc;
1121 	struct uath_cmd_hdr *hdr;
1122 
1123 	if (status != USBD_NORMAL_COMPLETION) {
1124 		if (status == USBD_STALLED)
1125 			usbd_clear_endpoint_stall_async(sc->cmd_rx_pipe);
1126 		return;
1127 	}
1128 
1129 	hdr = (struct uath_cmd_hdr *)cmd->buf;
1130 
1131 #ifdef UATH_DEBUG
1132 	if (uath_debug >= 5) {
1133 		printf("received command code=0x%x index=%u len=%u",
1134 		    betoh32(hdr->code), hdr->priv, betoh32(hdr->len));
1135 		uath_dump_cmd(cmd->buf, betoh32(hdr->len), '-');
1136 	}
1137 #endif
1138 
1139 	switch (betoh32(hdr->code) & 0xff) {
1140 	/* reply to a read command */
1141 	default:
1142 	{
1143 		struct uath_tx_cmd *txcmd = &sc->tx_cmd[hdr->priv];
1144 
1145 		if (txcmd->odata != NULL) {
1146 			/* copy answer into caller's supplied buffer */
1147 			bcopy((uint8_t *)(hdr + 1), txcmd->odata,
1148 			    betoh32(hdr->len) - sizeof (struct uath_cmd_hdr));
1149 		}
1150 		wakeup(txcmd);	/* wake up caller */
1151 		break;
1152 	}
1153 	/* spontaneous firmware notifications */
1154 	case UATH_NOTIF_READY:
1155 		DPRINTF(("received device ready notification\n"));
1156 		wakeup(UATH_COND_INIT(sc));
1157 		break;
1158 
1159 	case UATH_NOTIF_TX:
1160 		/* this notification is sent when UATH_TX_NOTIFY is set */
1161 		DPRINTF(("received Tx notification\n"));
1162 		break;
1163 
1164 	case UATH_NOTIF_STATS:
1165 		DPRINTFN(2, ("received device statistics\n"));
1166 		timeout_add(&sc->stat_to, hz);
1167 		break;
1168 	}
1169 
1170 	/* setup a new transfer */
1171 	usbd_setup_xfer(xfer, sc->cmd_rx_pipe, cmd, cmd->buf, UATH_MAX_RXCMDSZ,
1172 	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1173 	    uath_cmd_rxeof);
1174 	(void)usbd_transfer(xfer);
1175 }
1176 
1177 Static void
1178 uath_data_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1179     usbd_status status)
1180 {
1181 	struct uath_rx_data *data = priv;
1182 	struct uath_softc *sc = data->sc;
1183 	struct ieee80211com *ic = &sc->sc_ic;
1184 	struct ifnet *ifp = &ic->ic_if;
1185 	struct ieee80211_frame *wh;
1186 	struct ieee80211_node *ni;
1187 	struct uath_rx_data *ndata;
1188 	struct uath_rx_desc *desc;
1189 	struct mbuf *m;
1190 	uint32_t hdr;
1191 	int s, len;
1192 
1193 	if (status != USBD_NORMAL_COMPLETION) {
1194 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1195 			return;
1196 
1197 		if (status == USBD_STALLED)
1198 			usbd_clear_endpoint_stall_async(sc->data_rx_pipe);
1199 
1200 		ifp->if_ierrors++;
1201 		return;
1202 	}
1203 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1204 
1205 	if (len < UATH_MIN_RXBUFSZ) {
1206 		DPRINTF(("wrong xfer size (len=%d)\n", len));
1207 		ifp->if_ierrors++;
1208 		goto skip;
1209 	}
1210 
1211 	hdr = betoh32(*(uint32_t *)data->buf);
1212 
1213 	/* Rx descriptor is located at the end, 32-bit aligned */
1214 	desc = (struct uath_rx_desc *)
1215 	    (data->buf + len - sizeof (struct uath_rx_desc));
1216 
1217 	if (betoh32(desc->len) > sc->rxbufsz) {
1218 		DPRINTF(("bad descriptor (len=%d)\n", betoh32(desc->len)));
1219 		ifp->if_ierrors++;
1220 		goto skip;
1221 	}
1222 
1223 	/* there's probably a "bad CRC" flag somewhere in the descriptor.. */
1224 
1225 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1226 	if (m == NULL) {
1227 		ifp->if_ierrors++;
1228 		goto skip;
1229 	}
1230 
1231 	/* grab a new Rx buffer */
1232 	ndata = SLIST_FIRST(&sc->rx_freelist);
1233 	if (ndata == NULL) {
1234 		printf("%s: could not allocate Rx buffer\n",
1235 		    USBDEVNAME(sc->sc_dev));
1236 		m_freem(m);
1237 		ifp->if_ierrors++;
1238 		goto skip;
1239 	}
1240 	SLIST_REMOVE_HEAD(&sc->rx_freelist, next);
1241 
1242 	MEXTADD(m, data->buf, sc->rxbufsz, 0, uath_free_rx_data, data);
1243 
1244 	/* finalize mbuf */
1245 	m->m_pkthdr.rcvif = ifp;
1246 	m->m_data = data->buf + sizeof (uint32_t);
1247 	m->m_pkthdr.len = m->m_len =
1248 	    betoh32(desc->len) - sizeof (struct uath_rx_desc);
1249 
1250 	data = ndata;
1251 
1252 	wh = mtod(m, struct ieee80211_frame *);
1253 	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1254 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
1255 		/*
1256 		 * Hardware decrypts the frame itself but leaves the WEP bit
1257 		 * set in the 802.11 header and doesn't remove the IV and CRC
1258 		 * fields.
1259 		 */
1260 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1261 		ovbcopy(wh, (caddr_t)wh + IEEE80211_WEP_IVLEN +
1262 		    IEEE80211_WEP_KIDLEN, sizeof (struct ieee80211_frame));
1263 		m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
1264 		m_adj(m, -IEEE80211_WEP_CRCLEN);
1265 		wh = mtod(m, struct ieee80211_frame *);
1266 	}
1267 
1268 #if NBPFILTER > 0
1269 	/* there are a lot more fields in the Rx descriptor */
1270 	if (sc->sc_drvbpf != NULL) {
1271 		struct mbuf mb;
1272 		struct uath_rx_radiotap_header *tap = &sc->sc_rxtap;
1273 
1274 		tap->wr_flags = 0;
1275 		tap->wr_chan_freq = htole16(betoh32(desc->freq));
1276 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1277 		tap->wr_dbm_antsignal = (int8_t)betoh32(desc->rssi);
1278 
1279 		M_DUP_PKTHDR(&mb, m);
1280 		mb.m_data = (caddr_t)tap;
1281 		mb.m_len = sc->sc_rxtap_len;
1282 		mb.m_next = m;
1283 		mb.m_pkthdr.len += mb.m_len;
1284 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
1285 	}
1286 #endif
1287 
1288 	s = splnet();
1289 	sc->sc_refcnt++;
1290 	ni = ieee80211_find_rxnode(ic, wh);
1291 	ieee80211_input(ifp, m, ni, (int)betoh32(desc->rssi), 0);
1292 
1293 	/* node is no longer needed */
1294 	ieee80211_release_node(ic, ni);
1295 	splx(s);
1296 
1297 skip:	/* setup a new transfer */
1298 	usbd_setup_xfer(data->xfer, sc->data_rx_pipe, data, data->buf,
1299 	    sc->rxbufsz, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1300 	    uath_data_rxeof);
1301 	(void)usbd_transfer(data->xfer);
1302 }
1303 
1304 Static int
1305 uath_tx_null(struct uath_softc *sc)
1306 {
1307 	struct uath_tx_data *data;
1308 	struct uath_tx_desc *desc;
1309 
1310 	data = &sc->tx_data[sc->data_idx];
1311 
1312 	data->ni = NULL;
1313 
1314 	*(uint32_t *)data->buf = UATH_MAKECTL(1, sizeof (struct uath_tx_desc));
1315 	desc = (struct uath_tx_desc *)(data->buf + sizeof (uint32_t));
1316 
1317 	bzero(desc, sizeof (struct uath_tx_desc));
1318 	desc->len  = htobe32(sizeof (struct uath_tx_desc));
1319 	desc->type = htobe32(UATH_TX_NULL);
1320 
1321 	usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf,
1322 	    sizeof (uint32_t) + sizeof (struct uath_tx_desc), USBD_NO_COPY |
1323 	    USBD_FORCE_SHORT_XFER, UATH_DATA_TIMEOUT, NULL);
1324 	if (usbd_sync_transfer(data->xfer) != 0)
1325 		return EIO;
1326 
1327 	sc->data_idx = (sc->data_idx + 1) % UATH_TX_DATA_LIST_COUNT;
1328 
1329 	return uath_cmd_write(sc, UATH_CMD_0F, NULL, 0, UATH_CMD_FLAG_ASYNC);
1330 }
1331 
1332 Static void
1333 uath_data_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1334     usbd_status status)
1335 {
1336 	struct uath_tx_data *data = priv;
1337 	struct uath_softc *sc = data->sc;
1338 	struct ieee80211com *ic = &sc->sc_ic;
1339 	struct ifnet *ifp = &ic->ic_if;
1340 	int s;
1341 
1342 	if (status != USBD_NORMAL_COMPLETION) {
1343 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1344 			return;
1345 
1346 		printf("%s: could not transmit buffer: %s\n",
1347 		    USBDEVNAME(sc->sc_dev), usbd_errstr(status));
1348 
1349 		if (status == USBD_STALLED)
1350 			usbd_clear_endpoint_stall_async(sc->data_tx_pipe);
1351 
1352 		ifp->if_oerrors++;
1353 		return;
1354 	}
1355 
1356 	s = splnet();
1357 
1358 	ieee80211_release_node(ic, data->ni);
1359 	data->ni = NULL;
1360 
1361 	sc->tx_queued--;
1362 	ifp->if_opackets++;
1363 
1364 	sc->sc_tx_timer = 0;
1365 	ifp->if_flags &= ~IFF_OACTIVE;
1366 	uath_start(ifp);
1367 
1368 	splx(s);
1369 }
1370 
1371 Static int
1372 uath_tx_data(struct uath_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1373 {
1374 	struct ieee80211com *ic = &sc->sc_ic;
1375 	struct uath_tx_data *data;
1376 	struct uath_tx_desc *desc;
1377 	const struct ieee80211_frame *wh;
1378 	int paylen, totlen, xferlen, error;
1379 
1380 	data = &sc->tx_data[sc->data_idx];
1381 	desc = (struct uath_tx_desc *)(data->buf + sizeof (uint32_t));
1382 
1383 	data->ni = ni;
1384 
1385 #if NBPFILTER > 0
1386 	if (sc->sc_drvbpf != NULL) {
1387 		struct mbuf mb;
1388 		struct uath_tx_radiotap_header *tap = &sc->sc_txtap;
1389 
1390 		tap->wt_flags = 0;
1391 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1392 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1393 
1394 		M_DUP_PKTHDR(&mb, m0);
1395 		mb.m_data = (caddr_t)tap;
1396 		mb.m_len = sc->sc_txtap_len;
1397 		mb.m_next = m0;
1398 		mb.m_pkthdr.len += mb.m_len;
1399 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1400 	}
1401 #endif
1402 
1403 	paylen = m0->m_pkthdr.len;
1404 	xferlen = sizeof (uint32_t) + sizeof (struct uath_tx_desc) + paylen;
1405 
1406 	wh = mtod(m0, struct ieee80211_frame *);
1407 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1408 		uint8_t *frm = (uint8_t *)(desc + 1);
1409 		uint32_t iv;
1410 
1411 		/* h/w WEP: it's up to the host to fill the IV field */
1412 		bcopy(wh, frm, sizeof (struct ieee80211_frame));
1413 		frm += sizeof (struct ieee80211_frame);
1414 
1415 		/* insert IV: code copied from net80211 */
1416 		iv = (ic->ic_iv != 0) ? ic->ic_iv : arc4random();
1417 		if (iv >= 0x03ff00 && (iv & 0xf8ff00) == 0x00ff00)
1418 			iv += 0x000100;
1419 		ic->ic_iv = iv + 1;
1420 
1421 		*frm++ = iv & 0xff;
1422 		*frm++ = (iv >>  8) & 0xff;
1423 		*frm++ = (iv >> 16) & 0xff;
1424 		*frm++ = ic->ic_wep_txkey << 6;
1425 
1426 		m_copydata(m0, sizeof (struct ieee80211_frame),
1427 		    m0->m_pkthdr.len - sizeof (struct ieee80211_frame), frm);
1428 
1429 		paylen  += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
1430 		xferlen += IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN;
1431 		totlen = xferlen + IEEE80211_WEP_CRCLEN;
1432 	} else {
1433 		m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(desc + 1));
1434 		totlen = xferlen;
1435 	}
1436 
1437 	/* fill Tx descriptor */
1438 	*(uint32_t *)data->buf = UATH_MAKECTL(1, xferlen - sizeof (uint32_t));
1439 
1440 	desc->len    = htobe32(totlen);
1441 	desc->priv   = sc->data_idx;	/* don't care about endianness */
1442 	desc->paylen = htobe32(paylen);
1443 	desc->type   = htobe32(UATH_TX_DATA);
1444 	desc->flags  = htobe32(0);
1445 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1446 		desc->dest  = htobe32(UATH_ID_BROADCAST);
1447 		desc->magic = htobe32(3);
1448 	} else {
1449 		desc->dest  = htobe32(UATH_ID_BSS);
1450 		desc->magic = htobe32(1);
1451 	}
1452 
1453 	m_freem(m0);	/* mbuf is no longer needed */
1454 
1455 #ifdef UATH_DEBUG
1456 	if (uath_debug >= 6) {
1457 		printf("sending frame index=%u len=%d xferlen=%d",
1458 		    sc->data_idx, paylen, xferlen);
1459 		uath_dump_cmd(data->buf, xferlen, '+');
1460 	}
1461 #endif
1462 	usbd_setup_xfer(data->xfer, sc->data_tx_pipe, data, data->buf, xferlen,
1463 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, UATH_DATA_TIMEOUT,
1464 	    uath_data_txeof);
1465 	error = usbd_transfer(data->xfer);
1466 	if (error != USBD_IN_PROGRESS && error != 0) {
1467 		ic->ic_if.if_oerrors++;
1468 		return error;
1469 	}
1470 	sc->data_idx = (sc->data_idx + 1) % UATH_TX_DATA_LIST_COUNT;
1471 	sc->tx_queued++;
1472 
1473 	return 0;
1474 }
1475 
1476 Static void
1477 uath_start(struct ifnet *ifp)
1478 {
1479 	struct uath_softc *sc = ifp->if_softc;
1480 	struct ieee80211com *ic = &sc->sc_ic;
1481 	struct ieee80211_node *ni;
1482 	struct mbuf *m0;
1483 
1484 	/*
1485 	 * net80211 may still try to send management frames even if the
1486 	 * IFF_RUNNING flag is not set...
1487 	 */
1488 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1489 		return;
1490 
1491 	for (;;) {
1492 		IF_POLL(&ic->ic_mgtq, m0);
1493 		if (m0 != NULL) {
1494 			if (sc->tx_queued >= UATH_TX_DATA_LIST_COUNT) {
1495 				ifp->if_flags |= IFF_OACTIVE;
1496 				break;
1497 			}
1498 			IF_DEQUEUE(&ic->ic_mgtq, m0);
1499 
1500 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1501 			m0->m_pkthdr.rcvif = NULL;
1502 #if NBPFILTER > 0
1503 			if (ic->ic_rawbpf != NULL)
1504 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1505 #endif
1506 			if (uath_tx_data(sc, m0, ni) != 0)
1507 				break;
1508 		} else {
1509 			if (ic->ic_state != IEEE80211_S_RUN)
1510 				break;
1511 			IFQ_POLL(&ifp->if_snd, m0);
1512 			if (m0 == NULL)
1513 				break;
1514 			if (sc->tx_queued >= UATH_TX_DATA_LIST_COUNT) {
1515 				ifp->if_flags |= IFF_OACTIVE;
1516 				break;
1517 			}
1518 			IFQ_DEQUEUE(&ifp->if_snd, m0);
1519 #if NBPFILTER > 0
1520 			if (ifp->if_bpf != NULL)
1521 				bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1522 #endif
1523 			m0 = ieee80211_encap(ifp, m0, &ni);
1524 			if (m0 == NULL)
1525 				continue;
1526 #if NBPFILTER > 0
1527 			if (ic->ic_rawbpf != NULL)
1528 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1529 #endif
1530 			if (uath_tx_data(sc, m0, ni) != 0) {
1531 				if (ni != NULL)
1532 					ieee80211_release_node(ic, ni);
1533 				ifp->if_oerrors++;
1534 				break;
1535 			}
1536 		}
1537 
1538 		sc->sc_tx_timer = 5;
1539 		ifp->if_timer = 1;
1540 	}
1541 }
1542 
1543 Static void
1544 uath_watchdog(struct ifnet *ifp)
1545 {
1546 	struct uath_softc *sc = ifp->if_softc;
1547 
1548 	ifp->if_timer = 0;
1549 
1550 	if (sc->sc_tx_timer > 0) {
1551 		if (--sc->sc_tx_timer == 0) {
1552 			printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1553 			/*uath_init(ifp); XXX needs a process context! */
1554 			ifp->if_oerrors++;
1555 			return;
1556 		}
1557 		ifp->if_timer = 1;
1558 	}
1559 
1560 	ieee80211_watchdog(ifp);
1561 }
1562 
1563 Static int
1564 uath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1565 {
1566 	struct uath_softc *sc = ifp->if_softc;
1567 	struct ieee80211com *ic = &sc->sc_ic;
1568 	struct ifaddr *ifa;
1569 	struct ifreq *ifr;
1570 	int s, error = 0;
1571 
1572 	s = splnet();
1573 
1574 	switch (cmd) {
1575 	case SIOCSIFADDR:
1576 		ifa = (struct ifaddr *)data;
1577 		ifp->if_flags |= IFF_UP;
1578 #ifdef INET
1579 		if (ifa->ifa_addr->sa_family == AF_INET)
1580 			arp_ifinit(&ic->ic_ac, ifa);
1581 #endif
1582 		/* FALLTHROUGH */
1583 	case SIOCSIFFLAGS:
1584 		if (ifp->if_flags & IFF_UP) {
1585 			if (!(ifp->if_flags & IFF_RUNNING))
1586 				uath_init(ifp);
1587 		} else {
1588 			if (ifp->if_flags & IFF_RUNNING)
1589 				uath_stop(ifp, 1);
1590 		}
1591 		break;
1592 
1593 	case SIOCADDMULTI:
1594 	case SIOCDELMULTI:
1595 		ifr = (struct ifreq *)data;
1596 		error = (cmd == SIOCADDMULTI) ?
1597 		    ether_addmulti(ifr, &ic->ic_ac) :
1598 		    ether_delmulti(ifr, &ic->ic_ac);
1599 		if (error == ENETRESET)
1600 			error = 0;
1601 		break;
1602 
1603 	default:
1604 		error = ieee80211_ioctl(ifp, cmd, data);
1605 	}
1606 
1607 	if (error == ENETRESET) {
1608 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1609 		    (IFF_UP | IFF_RUNNING))
1610 			uath_init(ifp);
1611 		error = 0;
1612 	}
1613 
1614 	splx(s);
1615 
1616 	return error;
1617 }
1618 
1619 Static int
1620 uath_query_eeprom(struct uath_softc *sc)
1621 {
1622 	uint32_t tmp;
1623 	int error;
1624 
1625 	/* retrieve MAC address */
1626 	error = uath_read_eeprom(sc, UATH_EEPROM_MACADDR, sc->sc_ic.ic_myaddr);
1627 	if (error != 0) {
1628 		printf("%s: could not read MAC address\n",
1629 		    USBDEVNAME(sc->sc_dev));
1630 		return error;
1631 	}
1632 
1633 	/* retrieve the maximum frame size that the hardware can receive */
1634 	error = uath_read_eeprom(sc, UATH_EEPROM_RXBUFSZ, &tmp);
1635 	if (error != 0) {
1636 		printf("%s: could not read maximum Rx buffer size\n",
1637 		    USBDEVNAME(sc->sc_dev));
1638 		return error;
1639 	}
1640 	sc->rxbufsz = betoh32(tmp) & 0xfff;
1641 	DPRINTF(("maximum Rx buffer size %d\n", sc->rxbufsz));
1642 	return 0;
1643 }
1644 
1645 Static int
1646 uath_reset(struct uath_softc *sc)
1647 {
1648 	struct uath_cmd_setup setup;
1649 	uint32_t reg, val;
1650 	int s, error;
1651 
1652 	/* init device with some voodoo incantations.. */
1653 	setup.magic1 = htobe32(1);
1654 	setup.magic2 = htobe32(5);
1655 	setup.magic3 = htobe32(200);
1656 	setup.magic4 = htobe32(27);
1657 	s = splusb();
1658 	error = uath_cmd_write(sc, UATH_CMD_SETUP, &setup, sizeof setup,
1659 	    UATH_CMD_FLAG_ASYNC);
1660 	/* ..and wait until firmware notifies us that it is ready */
1661 	if (error == 0)
1662 		error = tsleep(UATH_COND_INIT(sc), PCATCH, "uathinit", 5 * hz);
1663 	splx(s);
1664 	if (error != 0)
1665 		return error;
1666 
1667 	/* read PHY registers */
1668 	for (reg = 0x09; reg <= 0x24; reg++) {
1669 		if (reg == 0x0b || reg == 0x0c)
1670 			continue;
1671 		DELAY(100);
1672 		if ((error = uath_read_reg(sc, reg, &val)) != 0)
1673 			return error;
1674 		DPRINTFN(2, ("reg 0x%02x=0x%08x\n", reg, val));
1675 	}
1676 	return error;
1677 }
1678 
1679 Static int
1680 uath_reset_tx_queues(struct uath_softc *sc)
1681 {
1682 	int ac, error;
1683 
1684 	for (ac = 0; ac < 4; ac++) {
1685 		const uint32_t qid = htobe32(UATH_AC_TO_QID(ac));
1686 
1687 		DPRINTF(("resetting Tx queue %d\n", UATH_AC_TO_QID(ac)));
1688 		error = uath_cmd_write(sc, UATH_CMD_RESET_QUEUE, &qid,
1689 		    sizeof qid, 0);
1690 		if (error != 0)
1691 			break;
1692 	}
1693 	return error;
1694 }
1695 
1696 Static int
1697 uath_wme_init(struct uath_softc *sc)
1698 {
1699 	struct uath_qinfo qinfo;
1700 	int ac, error;
1701 	static const struct uath_wme_settings uath_wme_11g[4] = {
1702 		{ 7, 4, 10,  0, 0 },	/* Background */
1703 		{ 3, 4, 10,  0, 0 },	/* Best-Effort */
1704 		{ 3, 3,  4, 26, 0 },	/* Video */
1705 		{ 2, 2,  3, 47, 0 }	/* Voice */
1706 	};
1707 
1708 	bzero(&qinfo, sizeof qinfo);
1709 	qinfo.size   = htobe32(32);
1710 	qinfo.magic1 = htobe32(1);	/* XXX ack policy? */
1711 	qinfo.magic2 = htobe32(1);
1712 	for (ac = 0; ac < 4; ac++) {
1713 		qinfo.qid      = htobe32(UATH_AC_TO_QID(ac));
1714 		qinfo.ac       = htobe32(ac);
1715 		qinfo.aifsn    = htobe32(uath_wme_11g[ac].aifsn);
1716 		qinfo.logcwmin = htobe32(uath_wme_11g[ac].logcwmin);
1717 		qinfo.logcwmax = htobe32(uath_wme_11g[ac].logcwmax);
1718 		qinfo.txop     = htobe32(UATH_TXOP_TO_US(
1719 				     uath_wme_11g[ac].txop));
1720 		qinfo.acm      = htobe32(uath_wme_11g[ac].acm);
1721 
1722 		DPRINTF(("setting up Tx queue %d\n", UATH_AC_TO_QID(ac)));
1723 		error = uath_cmd_write(sc, UATH_CMD_SET_QUEUE, &qinfo,
1724 		    sizeof qinfo, 0);
1725 		if (error != 0)
1726 			break;
1727 	}
1728 	return error;
1729 }
1730 
1731 Static int
1732 uath_set_chan(struct uath_softc *sc, struct ieee80211_channel *c)
1733 {
1734 	struct uath_set_chan chan;
1735 
1736 	bzero(&chan, sizeof chan);
1737 	chan.flags  = htobe32(0x1400);
1738 	chan.freq   = htobe32(c->ic_freq);
1739 	chan.magic1 = htobe32(20);
1740 	chan.magic2 = htobe32(50);
1741 	chan.magic3 = htobe32(1);
1742 
1743 	DPRINTF(("switching to channel %d\n",
1744 	    ieee80211_chan2ieee(&sc->sc_ic, c)));
1745 	return uath_cmd_write(sc, UATH_CMD_SET_CHAN, &chan, sizeof chan, 0);
1746 }
1747 
1748 Static int
1749 uath_set_key(struct uath_softc *sc, const struct ieee80211_wepkey *wk,
1750     int index)
1751 {
1752 	struct uath_cmd_crypto crypto;
1753 	int i;
1754 
1755 	bzero(&crypto, sizeof crypto);
1756 	crypto.keyidx = htobe32(index);
1757 	crypto.magic1 = htobe32(1);
1758 	crypto.size   = htobe32(368);
1759 	crypto.mask   = htobe32(0xffff);
1760 	crypto.flags  = htobe32(0x80000068);
1761 	if (index != UATH_DEFAULT_KEY)
1762 		crypto.flags |= htobe32(index << 16);
1763 	memset(crypto.magic2, 0xff, sizeof crypto.magic2);
1764 
1765 	/*
1766 	 * Each byte of the key must be XOR'ed with 10101010 before being
1767 	 * transmitted to the firmware.
1768 	 */
1769 	for (i = 0; i < wk->wk_len; i++)
1770 		crypto.key[i] = wk->wk_key[i] ^ 0xaa;
1771 
1772 	DPRINTF(("setting crypto key index=%d len=%d\n", index, wk->wk_len));
1773 	return uath_cmd_write(sc, UATH_CMD_CRYPTO, &crypto, sizeof crypto, 0);
1774 }
1775 
1776 Static int
1777 uath_set_keys(struct uath_softc *sc)
1778 {
1779 	const struct ieee80211com *ic = &sc->sc_ic;
1780 	int i, error;
1781 
1782 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1783 		const struct ieee80211_wepkey *wk = &ic->ic_nw_keys[i];
1784 
1785 		if (wk->wk_len > 0 &&
1786 		    (error = uath_set_key(sc, wk, i)) != 0)
1787 			return error;
1788 	}
1789 	return uath_set_key(sc, &ic->ic_nw_keys[ic->ic_wep_txkey],
1790 	    UATH_DEFAULT_KEY);
1791 }
1792 
1793 Static int
1794 uath_set_rates(struct uath_softc *sc, const struct ieee80211_rateset *rs)
1795 {
1796 	struct uath_cmd_rates rates;
1797 
1798 	bzero(&rates, sizeof rates);
1799 	rates.magic1 = htobe32(0x02);
1800 	rates.size   = htobe32(1 + sizeof rates.rates);
1801 	rates.nrates = rs->rs_nrates;
1802 	bcopy(rs->rs_rates, rates.rates, rs->rs_nrates);
1803 
1804 	DPRINTF(("setting supported rates nrates=%d\n", rs->rs_nrates));
1805 	return uath_cmd_write(sc, UATH_CMD_SET_RATES, &rates, sizeof rates, 0);
1806 }
1807 
1808 Static int
1809 uath_set_rxfilter(struct uath_softc *sc, uint32_t filter, uint32_t flags)
1810 {
1811 	struct uath_cmd_filter rxfilter;
1812 
1813 	rxfilter.filter = htobe32(filter);
1814 	rxfilter.flags  = htobe32(flags);
1815 
1816 	DPRINTF(("setting Rx filter=0x%x flags=0x%x\n", filter, flags));
1817 	return uath_cmd_write(sc, UATH_CMD_SET_FILTER, &rxfilter,
1818 	    sizeof rxfilter, 0);
1819 }
1820 
1821 Static int
1822 uath_set_led(struct uath_softc *sc, int which, int on)
1823 {
1824 	struct uath_cmd_led led;
1825 
1826 	led.which = htobe32(which);
1827 	led.state = htobe32(on ? UATH_LED_ON : UATH_LED_OFF);
1828 
1829 	DPRINTFN(2, ("switching %s led %s\n",
1830 	    (which == UATH_LED_LINK) ? "link" : "activity",
1831 	    on ? "on" : "off"));
1832 	return uath_cmd_write(sc, UATH_CMD_SET_LED, &led, sizeof led, 0);
1833 }
1834 
1835 Static int
1836 uath_switch_channel(struct uath_softc *sc, struct ieee80211_channel *c)
1837 {
1838 	uint32_t val;
1839 	int error;
1840 
1841 	/* set radio frequency */
1842 	if ((error = uath_set_chan(sc, c)) != 0) {
1843 		printf("%s: could not set channel\n", USBDEVNAME(sc->sc_dev));
1844 		return error;
1845 	}
1846 
1847 	/* reset Tx rings */
1848 	if ((error = uath_reset_tx_queues(sc)) != 0) {
1849 		printf("%s: could not reset Tx queues\n",
1850 		    USBDEVNAME(sc->sc_dev));
1851 		return error;
1852 	}
1853 
1854 	/* set Tx rings WME properties */
1855 	if ((error = uath_wme_init(sc)) != 0) {
1856 		printf("%s: could not init Tx queues\n",
1857 		    USBDEVNAME(sc->sc_dev));
1858 		return error;
1859 	}
1860 
1861 	val = htobe32(0);
1862 	error = uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 0);
1863 	if (error != 0) {
1864 		printf("%s: could not set state\n", USBDEVNAME(sc->sc_dev));
1865 		return error;
1866 	}
1867 
1868 	return uath_tx_null(sc);
1869 }
1870 
1871 Static int
1872 uath_init(struct ifnet *ifp)
1873 {
1874 	struct uath_softc *sc = ifp->if_softc;
1875 	struct ieee80211com *ic = &sc->sc_ic;
1876 	struct uath_cmd_31 cmd31;
1877 	uint32_t val;
1878 	int i, error;
1879 
1880 	/* reset data and command rings */
1881 	sc->tx_queued = sc->data_idx = sc->cmd_idx = 0;
1882 
1883 	val = htobe32(0);
1884 	(void)uath_cmd_write(sc, UATH_CMD_02, &val, sizeof val, 0);
1885 
1886 	/* set MAC address */
1887 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1888 	(void)uath_write_multi(sc, 0x13, ic->ic_myaddr, IEEE80211_ADDR_LEN);
1889 
1890 	(void)uath_write_reg(sc, 0x02, 0x00000001);
1891 	(void)uath_write_reg(sc, 0x0e, 0x0000003f);
1892 	(void)uath_write_reg(sc, 0x10, 0x00000001);
1893 	(void)uath_write_reg(sc, 0x06, 0x0000001e);
1894 
1895 	/*
1896 	 * Queue Rx data xfers.
1897 	 */
1898 	for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) {
1899 		struct uath_rx_data *data = SLIST_FIRST(&sc->rx_freelist);
1900 
1901 		usbd_setup_xfer(data->xfer, sc->data_rx_pipe, data, data->buf,
1902 		    sc->rxbufsz, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1903 		    USBD_NO_TIMEOUT, uath_data_rxeof);
1904 		error = usbd_transfer(data->xfer);
1905 		if (error != USBD_IN_PROGRESS && error != 0) {
1906 			printf("%s: could not queue Rx transfer\n",
1907 			    USBDEVNAME(sc->sc_dev));
1908 			goto fail;
1909 		}
1910 		SLIST_REMOVE_HEAD(&sc->rx_freelist, next);
1911 	}
1912 
1913 	error = uath_cmd_read(sc, UATH_CMD_07, 0, NULL, &val,
1914 	    UATH_CMD_FLAG_MAGIC);
1915 	if (error != 0) {
1916 		printf("%s: could not send read command 07h\n",
1917 		    USBDEVNAME(sc->sc_dev));
1918 		goto fail;
1919 	}
1920 	DPRINTF(("command 07h return code: %x\n", betoh32(val)));
1921 
1922 	/* set default channel */
1923 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1924 	if ((error = uath_set_chan(sc, ic->ic_bss->ni_chan)) != 0) {
1925 		printf("%s: could not set channel\n", USBDEVNAME(sc->sc_dev));
1926 		goto fail;
1927 	}
1928 
1929 	if ((error = uath_wme_init(sc)) != 0) {
1930 		printf("%s: could not setup WME parameters\n",
1931 		    USBDEVNAME(sc->sc_dev));
1932 		goto fail;
1933 	}
1934 
1935 	/* init MAC registers */
1936 	(void)uath_write_reg(sc, 0x19, 0x00000000);
1937 	(void)uath_write_reg(sc, 0x1a, 0x0000003c);
1938 	(void)uath_write_reg(sc, 0x1b, 0x0000003c);
1939 	(void)uath_write_reg(sc, 0x1c, 0x00000000);
1940 	(void)uath_write_reg(sc, 0x1e, 0x00000000);
1941 	(void)uath_write_reg(sc, 0x1f, 0x00000003);
1942 	(void)uath_write_reg(sc, 0x0c, 0x00000000);
1943 	(void)uath_write_reg(sc, 0x0f, 0x00000002);
1944 	(void)uath_write_reg(sc, 0x0a, 0x00000007);	/* XXX retry? */
1945 	(void)uath_write_reg(sc, 0x09, ic->ic_rtsthreshold);
1946 
1947 	val = htobe32(4);
1948 	(void)uath_cmd_write(sc, UATH_CMD_27, &val, sizeof val, 0);
1949 	(void)uath_cmd_write(sc, UATH_CMD_27, &val, sizeof val, 0);
1950 	(void)uath_cmd_write(sc, UATH_CMD_1B, NULL, 0, 0);
1951 
1952 	if ((error = uath_set_keys(sc)) != 0) {
1953 		printf("%s: could not set crypto keys\n",
1954 		    USBDEVNAME(sc->sc_dev));
1955 		goto fail;
1956 	}
1957 
1958 	/* enable Rx */
1959 	(void)uath_set_rxfilter(sc, 0x0000, 4);
1960 	(void)uath_set_rxfilter(sc, 0x0817, 1);
1961 
1962 	cmd31.magic1 = htobe32(0xffffffff);
1963 	cmd31.magic2 = htobe32(0xffffffff);
1964 	(void)uath_cmd_write(sc, UATH_CMD_31, &cmd31, sizeof cmd31, 0);
1965 
1966 	ifp->if_flags &= ~IFF_OACTIVE;
1967 	ifp->if_flags |= IFF_RUNNING;
1968 
1969 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
1970 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1971 	else
1972 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1973 
1974 	return 0;
1975 
1976 fail:	uath_stop(ifp, 1);
1977 	return error;
1978 }
1979 
1980 Static void
1981 uath_stop(struct ifnet *ifp, int disable)
1982 {
1983 	struct uath_softc *sc = ifp->if_softc;
1984 	struct ieee80211com *ic = &sc->sc_ic;
1985 	uint32_t val;
1986 	int s;
1987 
1988 	s = splusb();
1989 
1990 	sc->sc_tx_timer = 0;
1991 	ifp->if_timer = 0;
1992 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1993 
1994 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
1995 
1996 	val = htobe32(0);
1997 	(void)uath_cmd_write(sc, UATH_CMD_SET_STATE, &val, sizeof val, 0);
1998 	(void)uath_cmd_write(sc, UATH_CMD_RESET, NULL, 0, 0);
1999 
2000 	val = htobe32(0);
2001 	(void)uath_cmd_write(sc, UATH_CMD_15, &val, sizeof val, 0);
2002 
2003 #if 0
2004 	(void)uath_cmd_read(sc, UATH_CMD_SHUTDOWN, NULL, 0, NULL,
2005 	    UATH_CMD_FLAG_MAGIC);
2006 #endif
2007 
2008 	/* abort any pending transfers */
2009 	usbd_abort_pipe(sc->data_tx_pipe);
2010 	usbd_abort_pipe(sc->data_rx_pipe);
2011 	usbd_abort_pipe(sc->cmd_tx_pipe);
2012 
2013 	splx(s);
2014 }
2015 
2016 /*
2017  * Load the MIPS R4000 microcode into the device.  Once the image is loaded,
2018  * the device will detach itself from the bus and reattach later with a new
2019  * product Id (a la ezusb).  XXX this could also be implemented in userland
2020  * through /dev/ugen.
2021  */
2022 Static int
2023 uath_loadfirmware(struct uath_softc *sc, const u_char *fw, int len)
2024 {
2025 	usbd_xfer_handle ctlxfer, txxfer, rxxfer;
2026 	struct uath_fwblock *txblock, *rxblock;
2027 	uint8_t *txdata;
2028 	int error = 0;
2029 
2030 	if ((ctlxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
2031 		printf("%s: could not allocate Tx control xfer\n",
2032 		    USBDEVNAME(sc->sc_dev));
2033 		error = USBD_NOMEM;
2034 		goto fail1;
2035 	}
2036 	txblock = usbd_alloc_buffer(ctlxfer, sizeof (struct uath_fwblock));
2037 	if (txblock == NULL) {
2038 		printf("%s: could not allocate Tx control block\n",
2039 		    USBDEVNAME(sc->sc_dev));
2040 		error = USBD_NOMEM;
2041 		goto fail2;
2042 	}
2043 
2044 	if ((txxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
2045 		printf("%s: could not allocate Tx xfer\n",
2046 		    USBDEVNAME(sc->sc_dev));
2047 		error = USBD_NOMEM;
2048 		goto fail2;
2049 	}
2050 	txdata = usbd_alloc_buffer(txxfer, UATH_MAX_FWBLOCK_SIZE);
2051 	if (txdata == NULL) {
2052 		printf("%s: could not allocate Tx buffer\n",
2053 		    USBDEVNAME(sc->sc_dev));
2054 		error = USBD_NOMEM;
2055 		goto fail3;
2056 	}
2057 
2058 	if ((rxxfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) {
2059 		printf("%s: could not allocate Rx control xfer\n",
2060 		    USBDEVNAME(sc->sc_dev));
2061 		error = USBD_NOMEM;
2062 		goto fail3;
2063 	}
2064 	rxblock = usbd_alloc_buffer(rxxfer, sizeof (struct uath_fwblock));
2065 	if (rxblock == NULL) {
2066 		printf("%s: could not allocate Rx control block\n",
2067 		    USBDEVNAME(sc->sc_dev));
2068 		error = USBD_NOMEM;
2069 		goto fail4;
2070 	}
2071 
2072 	bzero(txblock, sizeof (struct uath_fwblock));
2073 	txblock->flags = htobe32(UATH_WRITE_BLOCK);
2074 	txblock->total = htobe32(len);
2075 
2076 	while (len > 0) {
2077 		int mlen = min(len, UATH_MAX_FWBLOCK_SIZE);
2078 
2079 		txblock->remain = htobe32(len - mlen);
2080 		txblock->len = htobe32(mlen);
2081 
2082 		DPRINTF(("sending firmware block: %d bytes remaining\n",
2083 		    len - mlen));
2084 
2085 		/* send firmware block meta-data */
2086 		usbd_setup_xfer(ctlxfer, sc->cmd_tx_pipe, sc, txblock,
2087 		    sizeof (struct uath_fwblock), USBD_NO_COPY,
2088 		    UATH_CMD_TIMEOUT, NULL);
2089 		if ((error = usbd_sync_transfer(ctlxfer)) != 0) {
2090 			printf("%s: could not send firmware block info\n",
2091 			    USBDEVNAME(sc->sc_dev));
2092 			break;
2093 		}
2094 
2095 		/* send firmware block data */
2096 		bcopy(fw, txdata, mlen);
2097 		usbd_setup_xfer(txxfer, sc->data_tx_pipe, sc, txdata, mlen,
2098 		    USBD_NO_COPY, UATH_DATA_TIMEOUT, NULL);
2099 		if ((error = usbd_sync_transfer(txxfer)) != 0) {
2100 			printf("%s: could not send firmware block data\n",
2101 			    USBDEVNAME(sc->sc_dev));
2102 			break;
2103 		}
2104 
2105 		/* wait for ack from firmware */
2106 		usbd_setup_xfer(rxxfer, sc->cmd_rx_pipe, sc, rxblock,
2107 		    sizeof (struct uath_fwblock), USBD_SHORT_XFER_OK |
2108 		    USBD_NO_COPY, UATH_CMD_TIMEOUT, NULL);
2109 		if ((error = usbd_sync_transfer(rxxfer)) != 0) {
2110 			printf("%s: could not read firmware answer\n",
2111 			    USBDEVNAME(sc->sc_dev));
2112 			break;
2113 		}
2114 
2115 		DPRINTFN(2, ("rxblock flags=0x%x total=%d\n",
2116 		    betoh32(rxblock->flags), betoh32(rxblock->rxtotal)));
2117 		fw += mlen;
2118 		len -= mlen;
2119 	}
2120 
2121 fail4:	usbd_free_xfer(rxxfer);
2122 fail3:	usbd_free_xfer(txxfer);
2123 fail2:	usbd_free_xfer(ctlxfer);
2124 fail1:	return error;
2125 }
2126 
2127 Static int
2128 uath_activate(device_ptr_t self, enum devact act)
2129 {
2130 	switch (act) {
2131 	case DVACT_ACTIVATE:
2132 		break;
2133 
2134 	case DVACT_DEACTIVATE:
2135 		/*if_deactivate(&sc->sc_ic.ic_if);*/
2136 		break;
2137 	}
2138 	return 0;
2139 }
2140