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