xref: /netbsd-src/sys/dev/usb/if_athn_usb.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*	$NetBSD: if_athn_usb.c,v 1.12 2016/06/10 13:27:15 ozaki-r Exp $	*/
2 /*	$OpenBSD: if_athn_usb.c,v 1.12 2013/01/14 09:50:31 jsing Exp $	*/
3 
4 /*-
5  * Copyright (c) 2011 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  * USB front-end for Atheros AR9271 and AR7010 chipsets.
22  */
23 
24 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: if_athn_usb.c,v 1.12 2016/06/10 13:27:15 ozaki-r Exp $");
26 
27 #ifdef	_KERNEL_OPT
28 #include "opt_inet.h"
29 #endif
30 
31 #include <sys/param.h>
32 #include <sys/callout.h>
33 #include <sys/conf.h>
34 #include <sys/device.h>
35 #include <sys/kernel.h>
36 #include <sys/mbuf.h>
37 #include <sys/module.h>
38 #include <sys/proc.h>
39 #include <sys/socket.h>
40 #include <sys/sockio.h>
41 #include <sys/systm.h>
42 #include <sys/kmem.h>
43 
44 #include <sys/bus.h>
45 #include <sys/endian.h>
46 #include <sys/intr.h>
47 
48 #include <net/bpf.h>
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 
56 #include <netinet/if_inarp.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_amrr.h>
64 #include <net80211/ieee80211_radiotap.h>
65 
66 #include <dev/firmload.h>
67 
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdevs.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdi_util.h>
72 
73 #include <dev/ic/athnreg.h>
74 #include <dev/ic/athnvar.h>
75 #include <dev/ic/arn9285.h>
76 #include <dev/usb/if_athn_usb.h>
77 
78 #define ATHN_USB_SOFTC(sc)	((struct athn_usb_softc *)(sc))
79 #define ATHN_USB_NODE(ni)	((struct athn_usb_node *)(ni))
80 
81 #define IS_UP_AND_RUNNING(ifp) \
82 	(((ifp)->if_flags & IFF_UP) && ((ifp)->if_flags & IFF_RUNNING))
83 
84 #define athn_usb_wmi_cmd(sc, cmd_id) \
85 	athn_usb_wmi_xcmd(sc, cmd_id, NULL, 0, NULL)
86 
87 Static int	athn_usb_activate(device_t, enum devact);
88 Static int	athn_usb_detach(device_t, int);
89 Static int	athn_usb_match(device_t, cfdata_t, void *);
90 Static void	athn_usb_attach(device_t, device_t, void *);
91 
92 CFATTACH_DECL_NEW(athn_usb, sizeof(struct athn_usb_softc), athn_usb_match,
93     athn_usb_attach, athn_usb_detach, athn_usb_activate);
94 
95 Static int	athn_usb_alloc_rx_list(struct athn_usb_softc *);
96 Static int	athn_usb_alloc_tx_cmd(struct athn_usb_softc *);
97 Static int	athn_usb_alloc_tx_list(struct athn_usb_softc *);
98 Static void	athn_usb_attachhook(device_t);
99 Static void	athn_usb_bcneof(struct usbd_xfer *, void *,
100 		    usbd_status);
101 Static void	athn_usb_abort_pipes(struct athn_usb_softc *);
102 Static void	athn_usb_close_pipes(struct athn_usb_softc *);
103 Static int	athn_usb_create_hw_node(struct athn_usb_softc *,
104 		    struct ar_htc_target_sta *);
105 Static int	athn_usb_create_node(struct athn_usb_softc *,
106 		    struct ieee80211_node *);
107 Static void	athn_usb_do_async(struct athn_usb_softc *,
108 		    void (*)(struct athn_usb_softc *, void *), void *, int);
109 Static void	athn_usb_free_rx_list(struct athn_usb_softc *);
110 Static void	athn_usb_free_tx_cmd(struct athn_usb_softc *);
111 Static void	athn_usb_free_tx_list(struct athn_usb_softc *);
112 Static int	athn_usb_htc_connect_svc(struct athn_usb_softc *, uint16_t,
113 		    uint8_t, uint8_t, uint8_t *);
114 Static int	athn_usb_htc_msg(struct athn_usb_softc *, uint16_t, void *,
115 		    int);
116 Static int	athn_usb_htc_setup(struct athn_usb_softc *);
117 Static int	athn_usb_init(struct ifnet *);
118 Static void	athn_usb_intr(struct usbd_xfer *, void *,
119 		    usbd_status);
120 Static int	athn_usb_ioctl(struct ifnet *, u_long, void *);
121 Static int	athn_usb_load_firmware(struct athn_usb_softc *);
122 Static const struct athn_usb_type *
123 		athn_usb_lookup(int, int);
124 Static int	athn_usb_media_change(struct ifnet *);
125 Static void	athn_usb_newassoc(struct ieee80211_node *, int);
126 Static void	athn_usb_newassoc_cb(struct athn_usb_softc *, void *);
127 Static int	athn_usb_newstate(struct ieee80211com *, enum ieee80211_state,
128 		    int);
129 Static void	athn_usb_newstate_cb(struct athn_usb_softc *, void *);
130 Static void	athn_usb_node_cleanup(struct ieee80211_node *);
131 Static void	athn_usb_node_cleanup_cb(struct athn_usb_softc *, void *);
132 Static int	athn_usb_open_pipes(struct athn_usb_softc *);
133 Static uint32_t	athn_usb_read(struct athn_softc *, uint32_t);
134 Static int	athn_usb_remove_hw_node(struct athn_usb_softc *, uint8_t *);
135 Static void	athn_usb_rx_enable(struct athn_softc *);
136 Static void	athn_usb_rx_frame(struct athn_usb_softc *, struct mbuf *);
137 Static void	athn_usb_rx_radiotap(struct athn_softc *, struct mbuf *,
138 		    struct ar_rx_status *);
139 Static void	athn_usb_rx_wmi_ctrl(struct athn_usb_softc *, uint8_t *, size_t);
140 Static void	athn_usb_rxeof(struct usbd_xfer *, void *,
141 		    usbd_status);
142 Static void	athn_usb_start(struct ifnet *);
143 Static void	athn_usb_stop(struct ifnet *);
144 Static void	athn_usb_swba(struct athn_usb_softc *);
145 Static int	athn_usb_switch_chan(struct athn_softc *,
146 		    struct ieee80211_channel *, struct ieee80211_channel *);
147 Static void	athn_usb_task(void *);
148 Static int	athn_usb_tx(struct athn_softc *, struct mbuf *,
149 		    struct ieee80211_node *, struct athn_usb_tx_data *);
150 Static void	athn_usb_txeof(struct usbd_xfer *, void *,
151 		    usbd_status);
152 Static void	athn_usb_updateslot(struct ifnet *);
153 Static void	athn_usb_updateslot_cb(struct athn_usb_softc *, void *);
154 Static void	athn_usb_wait_async(struct athn_usb_softc *);
155 Static void	athn_usb_wait_cmd(struct athn_usb_softc *);
156 Static void	athn_usb_wait_msg(struct athn_usb_softc *);
157 Static void	athn_usb_wait_wmi(struct athn_usb_softc *);
158 Static void	athn_usb_watchdog(struct ifnet *);
159 Static int	athn_usb_wmi_xcmd(struct athn_usb_softc *, uint16_t, void *,
160 		    int, void *);
161 Static void	athn_usb_wmieof(struct usbd_xfer *, void *,
162 		    usbd_status);
163 Static void	athn_usb_write(struct athn_softc *, uint32_t, uint32_t);
164 Static void	athn_usb_write_barrier(struct athn_softc *);
165 
166 /************************************************************************
167  * unused/notyet declarations
168  */
169 #ifdef unused
170 Static int	athn_usb_read_rom(struct athn_softc *);
171 #endif /* unused */
172 
173 #ifdef notyet_edca
174 Static void	athn_usb_updateedca(struct ieee80211com *);
175 Static void	athn_usb_updateedca_cb(struct athn_usb_softc *, void *);
176 #endif /* notyet_edca */
177 
178 #ifdef notyet
179 Static int	athn_usb_ampdu_tx_start(struct ieee80211com *,
180 		    struct ieee80211_node *, uint8_t);
181 Static void	athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *, void *);
182 Static void	athn_usb_ampdu_tx_stop(struct ieee80211com *,
183 		    struct ieee80211_node *, uint8_t);
184 Static void	athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *, void *);
185 Static void	athn_usb_delete_key(struct ieee80211com *,
186 		    struct ieee80211_node *, struct ieee80211_key *);
187 Static void	athn_usb_delete_key_cb(struct athn_usb_softc *, void *);
188 Static int	athn_usb_set_key(struct ieee80211com *,
189 		    struct ieee80211_node *, struct ieee80211_key *);
190 Static void	athn_usb_set_key_cb(struct athn_usb_softc *, void *);
191 #endif /* notyet */
192 /************************************************************************/
193 
194 struct athn_usb_type {
195 	struct usb_devno	devno;
196 	u_int			flags;
197 };
198 
199 Static const struct athn_usb_type *
200 athn_usb_lookup(int vendor, int product)
201 {
202 	static const struct athn_usb_type athn_usb_devs[] = {
203 #define _D(v,p,f) \
204 		{{ USB_VENDOR_##v, USB_PRODUCT_##p }, ATHN_USB_FLAG_##f }
205 
206 		_D( ACCTON,	ACCTON_AR9280,		AR7010 ),
207 		_D( ACTIONTEC,	ACTIONTEC_AR9287,	AR7010 ),
208 		_D( ATHEROS2,	ATHEROS2_AR9271_1,	NONE ),
209 		_D( ATHEROS2,	ATHEROS2_AR9271_2,	NONE ),
210 		_D( ATHEROS2,	ATHEROS2_AR9271_3,	NONE ),
211 		_D( ATHEROS2,	ATHEROS2_AR9280,	AR7010 ),
212 		_D( ATHEROS2,	ATHEROS2_AR9287,	AR7010 ),
213 		_D( AZUREWAVE,	AZUREWAVE_AR9271_1,	NONE ),
214 		_D( AZUREWAVE,	AZUREWAVE_AR9271_2,	NONE ),
215 		_D( AZUREWAVE,	AZUREWAVE_AR9271_3,	NONE ),
216 		_D( AZUREWAVE,	AZUREWAVE_AR9271_4,	NONE ),
217 		_D( AZUREWAVE,	AZUREWAVE_AR9271_5,	NONE ),
218 		_D( AZUREWAVE,	AZUREWAVE_AR9271_6,	NONE ),
219 		_D( DLINK2,	DLINK2_AR9271,	  	NONE ),
220 		_D( LITEON,	LITEON_AR9271,	  	NONE ),
221 		_D( NETGEAR,	NETGEAR_WNA1100,	NONE ),
222 		_D( NETGEAR,	NETGEAR_WNDA3200,	AR7010 ),
223 		_D( VIA,	VIA_AR9271,		NONE )
224 #undef _D
225 	};
226 
227 	return (const void *)usb_lookup(athn_usb_devs, vendor, product);
228 }
229 
230 Static int
231 athn_usb_match(device_t parent, cfdata_t match, void *aux)
232 {
233 	struct usb_attach_arg *uaa = aux;
234 
235 	return athn_usb_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
236 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
237 }
238 
239 Static void
240 athn_usb_attach(device_t parent, device_t self, void *aux)
241 {
242 	struct athn_usb_softc *usc;
243 	struct athn_softc *sc;
244 	struct usb_attach_arg *uaa;
245 	int error;
246 
247 	usc = device_private(self);
248 	sc = &usc->usc_sc;
249 	uaa = aux;
250 	sc->sc_dev = self;
251 	usc->usc_udev = uaa->uaa_device;
252 
253 	aprint_naive("\n");
254 	aprint_normal("\n");
255 
256 	DPRINTFN(DBG_FN, sc, "\n");
257 
258 	usc->usc_athn_attached = 0;
259 	usc->usc_flags = athn_usb_lookup(uaa->uaa_vendor, uaa->uaa_product)->flags;
260 	sc->sc_flags |= ATHN_FLAG_USB;
261 #ifdef notyet
262 	/* Check if it is a combo WiFi+Bluetooth (WB193) device. */
263 	if (strncmp(product, "wb193", 5) == 0)
264 		sc->sc_flags |= ATHN_FLAG_BTCOEX3WIRE;
265 #endif
266 
267 	sc->sc_ops.read = athn_usb_read;
268 	sc->sc_ops.write = athn_usb_write;
269 	sc->sc_ops.write_barrier = athn_usb_write_barrier;
270 
271 	cv_init(&usc->usc_task_cv, "athntsk");
272 	mutex_init(&usc->usc_task_mtx, MUTEX_DEFAULT, IPL_NET);
273 	mutex_init(&usc->usc_tx_mtx, MUTEX_DEFAULT, IPL_NONE);
274 
275 	usb_init_task(&usc->usc_task, athn_usb_task, usc, 0);
276 
277 	if (usbd_set_config_no(usc->usc_udev, 1, 0) != 0) {
278 		aprint_error_dev(sc->sc_dev,
279 		    "could not set configuration no\n");
280 		goto fail;
281 	}
282 
283 	/* Get the first interface handle. */
284 	error = usbd_device2interface_handle(usc->usc_udev, 0, &usc->usc_iface);
285 	if (error != 0) {
286 		aprint_error_dev(sc->sc_dev,
287 		    "could not get interface handle\n");
288 		goto fail;
289 	}
290 
291 	if (athn_usb_open_pipes(usc) != 0)
292 		goto fail;
293 
294 	/* Allocate xfer for firmware commands. */
295 	if (athn_usb_alloc_tx_cmd(usc) != 0)
296 		goto fail;
297 
298 	/* Allocate Tx/Rx buffers. */
299 	error = athn_usb_alloc_rx_list(usc);
300 	if (error != 0)
301 		goto fail;
302 	error = athn_usb_alloc_tx_list(usc);
303 	if (error != 0)
304 		goto fail;
305 
306 	config_mountroot(self, athn_usb_attachhook);
307 
308 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, usc->usc_udev, sc->sc_dev);
309 	return;
310 
311  fail:
312 	/* Free Tx/Rx buffers. */
313 	athn_usb_abort_pipes(usc);
314 	athn_usb_free_tx_list(usc);
315 	athn_usb_free_rx_list(usc);
316 	athn_usb_free_tx_cmd(usc);
317 	athn_usb_close_pipes(usc);
318 	usb_rem_task(usc->usc_udev, &usc->usc_task);
319 	mutex_destroy(&usc->usc_tx_mtx);
320 	mutex_destroy(&usc->usc_task_mtx);
321 }
322 
323 Static void
324 athn_usb_node_cleanup_cb(struct athn_usb_softc *usc, void *arg)
325 {
326 	uint8_t sta_index = *(uint8_t *)arg;
327 
328 	DPRINTFN(DBG_FN, usc, "\n");
329 	DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
330 	athn_usb_remove_hw_node(usc, &sta_index);
331 }
332 
333 Static void
334 athn_usb_node_cleanup(struct ieee80211_node *ni)
335 {
336 	struct athn_usb_softc *usc;
337 	struct ieee80211com *ic;
338 	uint8_t sta_index;
339 
340 	usc = ATHN_USB_SOFTC(ni->ni_ic->ic_ifp->if_softc);
341 	ic = &ATHN_SOFTC(usc)->sc_ic;
342 
343 	DPRINTFN(DBG_FN, usc, "\n");
344 
345 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
346 		sta_index = ATHN_NODE(ni)->sta_index;
347 		if (sta_index != 0)
348 			athn_usb_do_async(usc, athn_usb_node_cleanup_cb,
349 			    &sta_index, sizeof(sta_index));
350 	}
351 	usc->usc_node_cleanup(ni);
352 }
353 
354 Static void
355 athn_usb_attachhook(device_t arg)
356 {
357 	struct athn_usb_softc *usc = device_private(arg);
358 	struct athn_softc *sc = &usc->usc_sc;
359 	struct athn_ops *ops = &sc->sc_ops;
360 	struct ieee80211com *ic = &sc->sc_ic;
361 	struct ifnet *ifp = &sc->sc_if;
362 	size_t i;
363 	int s, error;
364 
365 	if (usc->usc_dying)
366 		return;
367 
368 	DPRINTFN(DBG_FN, usc, "\n");
369 
370 	/* Load firmware. */
371 	error = athn_usb_load_firmware(usc);
372 	if (error != 0) {
373 		aprint_error_dev(sc->sc_dev,
374 		    "could not load firmware (%d)\n", error);
375 		return;
376 	}
377 
378 	/* Setup the host transport communication interface. */
379 	error = athn_usb_htc_setup(usc);
380 	if (error != 0)
381 		return;
382 
383 	/* We're now ready to attach the bus agnostic driver. */
384 	s = splnet();
385 	ic->ic_ifp = ifp;
386 	ic->ic_updateslot = athn_usb_updateslot;
387 	sc->sc_max_aid = AR_USB_MAX_STA;  /* Firmware is limited to 8 STA */
388 	sc->sc_media_change = athn_usb_media_change;
389 	error = athn_attach(sc);
390 	if (error != 0) {
391 		splx(s);
392 		return;
393 	}
394 	usc->usc_athn_attached = 1;
395 
396 	/* Override some operations for USB. */
397 	ifp->if_init = athn_usb_init;
398 	ifp->if_ioctl = athn_usb_ioctl;
399 	ifp->if_start = athn_usb_start;
400 	ifp->if_watchdog = athn_usb_watchdog;
401 
402 	/* hooks for HostAP association and disassociation */
403 	ic->ic_newassoc = athn_usb_newassoc;
404 	usc->usc_node_cleanup = ic->ic_node_cleanup;
405 	ic->ic_node_cleanup = athn_usb_node_cleanup;
406 
407 #ifdef notyet_edca
408 	ic->ic_updateedca = athn_usb_updateedca;
409 #endif
410 #ifdef notyet
411 	ic->ic_set_key = athn_usb_set_key;
412 	ic->ic_delete_key = athn_usb_delete_key;
413 	ic->ic_ampdu_tx_start = athn_usb_ampdu_tx_start;
414 	ic->ic_ampdu_tx_stop = athn_usb_ampdu_tx_stop;
415 #endif
416 	ic->ic_newstate = athn_usb_newstate;
417 
418 	ops->rx_enable = athn_usb_rx_enable;
419 	splx(s);
420 
421 	/* Reset HW key cache entries. */
422 	for (i = 0; i < sc->sc_kc_entries; i++)
423 		athn_reset_key(sc, i);
424 
425 	ops->enable_antenna_diversity(sc);
426 
427 #ifdef ATHN_BT_COEXISTENCE
428 	/* Configure bluetooth coexistence for combo chips. */
429 	if (sc->sc_flags & ATHN_FLAG_BTCOEX)
430 		athn_btcoex_init(sc);
431 #endif
432 	/* Configure LED. */
433 	athn_led_init(sc);
434 
435 	ieee80211_announce(ic);
436 }
437 
438 Static int
439 athn_usb_detach(device_t self, int flags)
440 {
441 	struct athn_usb_softc *usc = device_private(self);
442 	struct athn_softc *sc = &usc->usc_sc;
443 	int s;
444 
445 	DPRINTFN(DBG_FN, usc, "\n");
446 
447 	s = splusb();
448 	usc->usc_dying = 1;
449 
450 	athn_usb_wait_wmi(usc);
451 	athn_usb_wait_cmd(usc);
452 	athn_usb_wait_msg(usc);
453 	athn_usb_wait_async(usc);
454 
455 	usb_rem_task(usc->usc_udev, &usc->usc_task);
456 
457 	if (usc->usc_athn_attached) {
458 		usc->usc_athn_attached = 0;
459 		athn_detach(sc);
460 	}
461 	/* Abort Tx/Rx pipes. */
462 	athn_usb_abort_pipes(usc);
463 	splx(s);
464 
465 	/* Free Tx/Rx buffers. */
466 	athn_usb_free_rx_list(usc);
467 	athn_usb_free_tx_list(usc);
468 	athn_usb_free_tx_cmd(usc);
469 
470 	/* Close Tx/Rx pipes. */
471 	athn_usb_close_pipes(usc);
472 
473 	mutex_destroy(&usc->usc_tx_mtx);
474 	mutex_destroy(&usc->usc_task_mtx);
475 	cv_destroy(&usc->usc_task_cv);
476 
477 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, usc->usc_udev, sc->sc_dev);
478 	return 0;
479 }
480 
481 Static int
482 athn_usb_activate(device_t self, enum devact act)
483 {
484 	struct athn_usb_softc *usc = device_private(self);
485 	struct athn_softc *sc = &usc->usc_sc;
486 
487 	DPRINTFN(DBG_FN, usc, "\n");
488 
489 	switch (act) {
490 	case DVACT_DEACTIVATE:
491 		if_deactivate(sc->sc_ic.ic_ifp);
492 		usc->usc_dying = 1;
493 		return 0;
494 	default:
495 		return EOPNOTSUPP;
496 	}
497 }
498 
499 Static int
500 athn_usb_open_pipes(struct athn_usb_softc *usc)
501 {
502 	usb_endpoint_descriptor_t *ed;
503 	int error;
504 
505 	DPRINTFN(DBG_FN, usc, "\n");
506 
507 	error = usbd_open_pipe(usc->usc_iface, AR_PIPE_TX_DATA, 0,
508 	    &usc->usc_tx_data_pipe);
509 	if (error != 0) {
510 		aprint_error_dev(usc->usc_dev,
511 		    "could not open Tx bulk pipe\n");
512 		goto fail;
513 	}
514 
515 	error = usbd_open_pipe(usc->usc_iface, AR_PIPE_RX_DATA, 0,
516 	    &usc->usc_rx_data_pipe);
517 	if (error != 0) {
518 		aprint_error_dev(usc->usc_dev,
519 		    "could not open Rx bulk pipe\n");
520 		goto fail;
521 	}
522 
523 	ed = usbd_get_endpoint_descriptor(usc->usc_iface, AR_PIPE_RX_INTR);
524 	if (ed == NULL) {
525 		aprint_error_dev(usc->usc_dev,
526 		    "could not retrieve Rx intr pipe descriptor\n");
527 		goto fail;
528 	}
529 	usc->usc_ibufsize = UGETW(ed->wMaxPacketSize);
530 	if (usc->usc_ibufsize == 0) {
531 		aprint_error_dev(usc->usc_dev,
532 		    "invalid Rx intr pipe descriptor\n");
533 		goto fail;
534 	}
535 	usc->usc_ibuf = kmem_alloc(usc->usc_ibufsize, KM_SLEEP);
536 	if (usc->usc_ibuf == NULL) {
537 		aprint_error_dev(usc->usc_dev,
538 		    "could not allocate Rx intr buffer\n");
539 		goto fail;
540 	}
541 
542 	error = usbd_open_pipe_intr(usc->usc_iface, AR_PIPE_RX_INTR,
543 	    USBD_SHORT_XFER_OK, &usc->usc_rx_intr_pipe, usc, usc->usc_ibuf,
544 	    usc->usc_ibufsize, athn_usb_intr, USBD_DEFAULT_INTERVAL);
545 	if (error != 0) {
546 		aprint_error_dev(usc->usc_dev,
547 		    "could not open Rx intr pipe\n");
548 		goto fail;
549 	}
550 	error = usbd_open_pipe(usc->usc_iface, AR_PIPE_TX_INTR, 0,
551 	    &usc->usc_tx_intr_pipe);
552 	if (error != 0) {
553 		aprint_error_dev(usc->usc_dev,
554 		    "could not open Tx intr pipe\n");
555 		goto fail;
556 	}
557 	return 0;
558  fail:
559 	athn_usb_abort_pipes(usc);
560 	athn_usb_close_pipes(usc);
561 	return error;
562 }
563 
564 static inline void
565 athn_usb_kill_pipe(struct usbd_pipe **pipeptr)
566 {
567 	struct usbd_pipe *pipe;
568 
569 	CTASSERT(sizeof(pipe) == sizeof(void *));
570 	pipe = atomic_swap_ptr(pipeptr, NULL);
571 	if (pipe != NULL) {
572 		usbd_close_pipe(pipe);
573 	}
574 }
575 
576 Static void
577 athn_usb_abort_pipes(struct athn_usb_softc *usc)
578 {
579 	DPRINTFN(DBG_FN, usc, "\n");
580 
581 	if (usc->usc_tx_data_pipe != NULL)
582 		usbd_abort_pipe(usc->usc_tx_data_pipe);
583 	if (usc->usc_rx_data_pipe != NULL)
584 		usbd_abort_pipe(usc->usc_rx_data_pipe);
585 	if (usc->usc_tx_intr_pipe != NULL)
586 		usbd_abort_pipe(usc->usc_tx_intr_pipe);
587 	if (usc->usc_rx_intr_pipe != NULL)
588 		usbd_abort_pipe(usc->usc_rx_intr_pipe);
589 }
590 
591 Static void
592 athn_usb_close_pipes(struct athn_usb_softc *usc)
593 {
594 	uint8_t *ibuf;
595 
596 	DPRINTFN(DBG_FN, usc, "\n");
597 
598 	athn_usb_kill_pipe(&usc->usc_tx_data_pipe);
599 	athn_usb_kill_pipe(&usc->usc_rx_data_pipe);
600 	athn_usb_kill_pipe(&usc->usc_tx_intr_pipe);
601 	athn_usb_kill_pipe(&usc->usc_rx_intr_pipe);
602 	ibuf = atomic_swap_ptr(&usc->usc_ibuf, NULL);
603 	if (ibuf != NULL)
604 		kmem_free(ibuf, usc->usc_ibufsize);
605 }
606 
607 Static int
608 athn_usb_alloc_rx_list(struct athn_usb_softc *usc)
609 {
610 	struct athn_usb_rx_data *data;
611 	size_t i;
612 	int error = 0;
613 
614 	DPRINTFN(DBG_FN, usc, "\n");
615 
616 	for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
617 		data = &usc->usc_rx_data[i];
618 
619 		data->sc = usc;	/* Backpointer for callbacks. */
620 
621 		error = usbd_create_xfer(usc->usc_rx_data_pipe,
622 		    ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK, 0, &data->xfer);
623 		if (error) {
624 			aprint_error_dev(usc->usc_dev,
625 			    "could not allocate xfer\n");
626 			break;
627 		}
628 		data->buf = usbd_get_buffer(data->xfer);
629 	}
630 	if (error != 0)
631 		athn_usb_free_rx_list(usc);
632 	return error;
633 }
634 
635 Static void
636 athn_usb_free_rx_list(struct athn_usb_softc *usc)
637 {
638 	struct usbd_xfer *xfer;
639 	size_t i;
640 
641 	DPRINTFN(DBG_FN, usc, "\n");
642 
643 	/* NB: Caller must abort pipe first. */
644 	for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
645 		CTASSERT(sizeof(xfer) == sizeof(void *));
646 		xfer = atomic_swap_ptr(&usc->usc_rx_data[i].xfer, NULL);
647 		if (xfer != NULL)
648 			usbd_destroy_xfer(xfer);
649 	}
650 }
651 
652 Static int
653 athn_usb_alloc_tx_list(struct athn_usb_softc *usc)
654 {
655 	struct athn_usb_tx_data *data;
656 	size_t i;
657 	int error = 0;
658 
659 	DPRINTFN(DBG_FN, usc, "\n");
660 
661 	mutex_enter(&usc->usc_tx_mtx);
662 	TAILQ_INIT(&usc->usc_tx_free_list);
663 	for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) {
664 		data = &usc->usc_tx_data[i];
665 
666 		data->sc = usc;	/* Backpointer for callbacks. */
667 
668 		error = usbd_create_xfer(usc->usc_tx_data_pipe,
669 		    ATHN_USB_TXBUFSZ, USBD_SHORT_XFER_OK, 0, &data->xfer);
670 		if (error) {
671 			aprint_error_dev(usc->usc_dev,
672 			    "could not create xfer on TX pipe\n");
673 			break;
674 		}
675 		data->buf = usbd_get_buffer(data->xfer);
676 
677 		/* Append this Tx buffer to our free list. */
678 		TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
679 	}
680 	if (error != 0)
681 		athn_usb_free_tx_list(usc);
682 	mutex_exit(&usc->usc_tx_mtx);
683 	return error;
684 }
685 
686 Static void
687 athn_usb_free_tx_list(struct athn_usb_softc *usc)
688 {
689 	struct usbd_xfer *xfer;
690 	size_t i;
691 
692 	DPRINTFN(DBG_FN, usc, "\n");
693 
694 	/* NB: Caller must abort pipe first. */
695 	for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) {
696 		CTASSERT(sizeof(xfer) == sizeof(void *));
697 		xfer = atomic_swap_ptr(&usc->usc_tx_data[i].xfer, NULL);
698 		if (xfer != NULL)
699 			usbd_destroy_xfer(xfer);
700 	}
701 }
702 
703 Static int
704 athn_usb_alloc_tx_cmd(struct athn_usb_softc *usc)
705 {
706 	struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
707 
708 	DPRINTFN(DBG_FN, usc, "\n");
709 
710 	data->sc = usc;	/* Backpointer for callbacks. */
711 
712 	int err = usbd_create_xfer(usc->usc_tx_intr_pipe, ATHN_USB_TXCMDSZ,
713 	    0, 0, &data->xfer);
714 	if (err) {
715 		aprint_error_dev(usc->usc_dev,
716 		    "could not allocate command xfer\n");
717 		return err;
718 	}
719 
720 	return 0;
721 }
722 
723 Static void
724 athn_usb_free_tx_cmd(struct athn_usb_softc *usc)
725 {
726 	struct usbd_xfer *xfer;
727 
728 	DPRINTFN(DBG_FN, usc, "\n");
729 
730 	CTASSERT(sizeof(xfer) == sizeof(void *));
731 	xfer = atomic_swap_ptr(&usc->usc_tx_cmd.xfer, NULL);
732 	if (xfer != NULL)
733 		usbd_destroy_xfer(xfer);
734 }
735 
736 Static void
737 athn_usb_task(void *arg)
738 {
739 	struct athn_usb_softc *usc = arg;
740 	struct athn_usb_host_cmd_ring *ring = &usc->usc_cmdq;
741 	struct athn_usb_host_cmd *cmd;
742 	int s;
743 
744 	DPRINTFN(DBG_FN, usc, "\n");
745 
746 	/* Process host commands. */
747 	s = splusb();
748 	mutex_spin_enter(&usc->usc_task_mtx);
749 	while (ring->next != ring->cur) {
750 		cmd = &ring->cmd[ring->next];
751 		mutex_spin_exit(&usc->usc_task_mtx);
752 		splx(s);
753 
754 		/* Invoke callback. */
755 		if (!usc->usc_dying)
756 			cmd->cb(usc, cmd->data);
757 
758 		s = splusb();
759 		mutex_spin_enter(&usc->usc_task_mtx);
760 		ring->queued--;
761 		ring->next = (ring->next + 1) % ATHN_USB_HOST_CMD_RING_COUNT;
762 	}
763 	mutex_spin_exit(&usc->usc_task_mtx);
764 	wakeup(ring);
765 	splx(s);
766 }
767 
768 Static void
769 athn_usb_do_async(struct athn_usb_softc *usc,
770     void (*cb)(struct athn_usb_softc *, void *), void *arg, int len)
771 {
772 	struct athn_usb_host_cmd_ring *ring = &usc->usc_cmdq;
773 	struct athn_usb_host_cmd *cmd;
774 	int s;
775 
776 	if (usc->usc_dying)
777 		return;
778 
779 	DPRINTFN(DBG_FN, usc, "\n");
780 
781 	s = splusb();
782 	mutex_spin_enter(&usc->usc_task_mtx);
783 	cmd = &ring->cmd[ring->cur];
784 	cmd->cb = cb;
785 	KASSERT(len <= sizeof(cmd->data));
786 	memcpy(cmd->data, arg, len);
787 	ring->cur = (ring->cur + 1) % ATHN_USB_HOST_CMD_RING_COUNT;
788 
789 	/* If there is no pending command already, schedule a task. */
790 	if (++ring->queued == 1) {
791 		mutex_spin_exit(&usc->usc_task_mtx);
792 		usb_add_task(usc->usc_udev, &usc->usc_task, USB_TASKQ_DRIVER);
793 	}
794 	else
795 		mutex_spin_exit(&usc->usc_task_mtx);
796 	splx(s);
797 }
798 
799 Static void
800 athn_usb_wait_async(struct athn_usb_softc *usc)
801 {
802 
803 	DPRINTFN(DBG_FN, usc, "\n");
804 
805 	/* Wait for all queued asynchronous commands to complete. */
806 	mutex_spin_enter(&usc->usc_task_mtx);
807 	while (usc->usc_cmdq.queued > 0)
808 		cv_wait(&usc->usc_task_cv, &usc->usc_task_mtx);
809 	mutex_spin_exit(&usc->usc_task_mtx);
810 }
811 
812 Static int
813 athn_usb_load_firmware(struct athn_usb_softc *usc)
814 {
815 	struct athn_softc *sc = &usc->usc_sc;
816 	firmware_handle_t fwh;
817 	usb_device_descriptor_t *dd;
818 	usb_device_request_t req;
819 	const char *name;
820 	u_char *fw, *ptr;
821 	size_t size, remain;
822 	uint32_t addr;
823 	int s, mlen, error;
824 
825 	DPRINTFN(DBG_FN, sc, "\n");
826 
827 	/* Determine which firmware image to load. */
828 	if (usc->usc_flags & ATHN_USB_FLAG_AR7010) {
829 		dd = usbd_get_device_descriptor(usc->usc_udev);
830 		if (UGETW(dd->bcdDevice) == 0x0202)
831 			name = "athn-ar7010-11";
832 		else
833 			name = "athn-ar7010";
834 	}
835 	else
836 		name = "athn-ar9271";
837 
838 	/* Read firmware image from the filesystem. */
839 	if ((error = firmware_open("if_athn", name, &fwh)) != 0) {
840 		aprint_error_dev(sc->sc_dev,
841 		    "failed to open firmware file %s (%d)\n", name, error);
842 		return error;
843 	}
844 	size = firmware_get_size(fwh);
845 	fw = firmware_malloc(size);
846 	if (fw == NULL) {
847 		aprint_error_dev(usc->usc_dev,
848 		    "failed to allocate firmware memory\n");
849 		firmware_close(fwh);
850 		return ENOMEM;
851 	}
852 	error = firmware_read(fwh, 0, fw, size);
853 	firmware_close(fwh);
854 	if (error != 0) {
855 		aprint_error_dev(usc->usc_dev,
856 		    "failed to read firmware (error %d)\n", error);
857 		firmware_free(fw, size);
858 		return error;
859 	}
860 
861 	/* Load firmware image. */
862 	ptr = fw;
863 	addr = AR9271_FIRMWARE >> 8;
864 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
865 	req.bRequest = AR_FW_DOWNLOAD;
866 	USETW(req.wIndex, 0);
867 	remain = size;
868 	while (remain > 0) {
869 		mlen = MIN(remain, 4096);
870 
871 		USETW(req.wValue, addr);
872 		USETW(req.wLength, mlen);
873 		error = usbd_do_request(usc->usc_udev, &req, ptr);
874 		if (error != 0) {
875 			firmware_free(fw, size);
876 			return error;
877 		}
878 		addr   += mlen >> 8;
879 		ptr    += mlen;
880 		remain -= mlen;
881 	}
882 	firmware_free(fw, size);
883 
884 	/* Start firmware. */
885 	if (usc->usc_flags & ATHN_USB_FLAG_AR7010)
886 		addr = AR7010_FIRMWARE_TEXT >> 8;
887 	else
888 		addr = AR9271_FIRMWARE_TEXT >> 8;
889 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
890 	req.bRequest = AR_FW_DOWNLOAD_COMP;
891 	USETW(req.wIndex, 0);
892 	USETW(req.wValue, addr);
893 	USETW(req.wLength, 0);
894 
895 	s = splusb();
896 	usc->usc_wait_msg_id = AR_HTC_MSG_READY;
897 	error = usbd_do_request(usc->usc_udev, &req, NULL);
898 	/* Wait at most 1 second for firmware to boot. */
899 	if (error == 0 && usc->usc_wait_msg_id != 0)
900 		error = tsleep(&usc->usc_wait_msg_id, 0, "athnfw", hz);
901 	usc->usc_wait_msg_id = 0;
902 	splx(s);
903 	return error;
904 }
905 
906 Static int
907 athn_usb_htc_msg(struct athn_usb_softc *usc, uint16_t msg_id, void *buf,
908     int len)
909 {
910 	struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
911 	struct ar_htc_frame_hdr *htc;
912 	struct ar_htc_msg_hdr *msg;
913 
914 	if (usc->usc_dying)
915 		return USBD_CANCELLED;
916 
917 	DPRINTFN(DBG_FN, usc, "\n");
918 
919 	htc = (struct ar_htc_frame_hdr *)data->buf;
920 	memset(htc, 0, sizeof(*htc));
921 	htc->endpoint_id = 0;
922 	htc->payload_len = htobe16(sizeof(*msg) + len);
923 
924 	msg = (struct ar_htc_msg_hdr *)&htc[1];
925 	msg->msg_id = htobe16(msg_id);
926 
927 	memcpy(&msg[1], buf, len);
928 
929 	usbd_setup_xfer(data->xfer, NULL, data->buf,
930 	    sizeof(*htc) + sizeof(*msg) + len,
931 	    USBD_SHORT_XFER_OK, ATHN_USB_CMD_TIMEOUT, NULL);
932 	return usbd_sync_transfer(data->xfer);
933 }
934 
935 Static int
936 athn_usb_htc_setup(struct athn_usb_softc *usc)
937 {
938 	struct ar_htc_msg_config_pipe cfg;
939 	int s, error;
940 
941 	/*
942 	 * Connect WMI services to USB pipes.
943 	 */
944 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CONTROL,
945 	    AR_PIPE_TX_INTR, AR_PIPE_RX_INTR, &usc->usc_ep_ctrl);
946 	if (error != 0)
947 		return error;
948 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_BEACON,
949 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_bcn);
950 	if (error != 0)
951 		return error;
952 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CAB,
953 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_cab);
954 	if (error != 0)
955 		return error;
956 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_UAPSD,
957 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_uapsd);
958 	if (error != 0)
959 		return error;
960 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_MGMT,
961 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_mgmt);
962 	if (error != 0)
963 		return error;
964 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BE,
965 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_BE]);
966 	if (error != 0)
967 		return error;
968 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BK,
969 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_BK]);
970 	if (error != 0)
971 		return error;
972 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VI,
973 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_VI]);
974 	if (error != 0)
975 		return error;
976 	error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VO,
977 	    AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_VO]);
978 	if (error != 0)
979 		return error;
980 
981 	/* Set credits for WLAN Tx pipe. */
982 	memset(&cfg, 0, sizeof(cfg));
983 	cfg.pipe_id = UE_GET_ADDR(AR_PIPE_TX_DATA);
984 	cfg.credits = (usc->usc_flags & ATHN_USB_FLAG_AR7010) ? 45 : 33;
985 
986 	s = splusb();
987 
988 	usc->usc_wait_msg_id = AR_HTC_MSG_CONF_PIPE_RSP;
989 	error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONF_PIPE, &cfg, sizeof(cfg));
990 	if (error == 0 && usc->usc_wait_msg_id != 0)
991 		error = tsleep(&usc->usc_wait_msg_id, 0, "athnhtc", hz);
992 	usc->usc_wait_msg_id = 0;
993 
994 	splx(s);
995 
996 	if (error != 0) {
997 		aprint_error_dev(usc->usc_dev, "could not configure pipe\n");
998 		return error;
999 	}
1000 
1001 	error = athn_usb_htc_msg(usc, AR_HTC_MSG_SETUP_COMPLETE, NULL, 0);
1002 	if (error != 0) {
1003 		aprint_error_dev(usc->usc_dev, "could not complete setup\n");
1004 		return error;
1005 	}
1006 	return 0;
1007 }
1008 
1009 Static int
1010 athn_usb_htc_connect_svc(struct athn_usb_softc *usc, uint16_t svc_id,
1011     uint8_t ul_pipe, uint8_t dl_pipe, uint8_t *endpoint_id)
1012 {
1013 	struct ar_htc_msg_conn_svc msg;
1014 	struct ar_htc_msg_conn_svc_rsp rsp;
1015 	int s, error;
1016 
1017 	DPRINTFN(DBG_FN, usc, "\n");
1018 
1019 	memset(&msg, 0, sizeof(msg));
1020 	msg.svc_id = htobe16(svc_id);
1021 	msg.dl_pipeid = UE_GET_ADDR(dl_pipe);
1022 	msg.ul_pipeid = UE_GET_ADDR(ul_pipe);
1023 	s = splusb();
1024 
1025 	usc->usc_msg_conn_svc_rsp = &rsp;
1026 
1027 	usc->usc_wait_msg_id = AR_HTC_MSG_CONN_SVC_RSP;
1028 	error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONN_SVC, &msg, sizeof(msg));
1029 	if (error == 0 && usc->usc_wait_msg_id != 0)
1030 		error = tsleep(&usc->usc_wait_msg_id, 0, "athnhtc", hz);
1031 	usc->usc_wait_msg_id = 0;
1032 
1033 	splx(s);
1034 	if (error != 0) {
1035 		aprint_error_dev(usc->usc_dev,
1036 		    "error waiting for service %d connection\n", svc_id);
1037 		return error;
1038 	}
1039 	if (rsp.status != AR_HTC_SVC_SUCCESS) {
1040 		aprint_error_dev(usc->usc_dev,
1041 		    "service %d connection failed, error %d\n",
1042 		    svc_id, rsp.status);
1043 		return EIO;
1044 	}
1045 	DPRINTFN(DBG_INIT, usc,
1046 	    "service %d successfully connected to endpoint %d\n",
1047 	    svc_id, rsp.endpoint_id);
1048 
1049 	/* Return endpoint id. */
1050 	*endpoint_id = rsp.endpoint_id;
1051 	return 0;
1052 }
1053 
1054 Static void
1055 athn_usb_wait_msg(struct athn_usb_softc *usc)
1056 {
1057 
1058 	DPRINTFN(DBG_FN, usc, "\n");
1059 
1060 	while (__predict_false(usc->usc_wait_msg_id))
1061 		tsleep(&usc->usc_wait_msg_id, 0, "athnmsg", hz);
1062 }
1063 
1064 Static void
1065 athn_usb_wait_cmd(struct athn_usb_softc *usc)
1066 {
1067 
1068 	DPRINTFN(DBG_FN, usc, "\n");
1069 
1070 	while (__predict_false(usc->usc_wait_cmd_id))
1071 		tsleep(&usc->usc_wait_cmd_id, 0, "athncmd", hz);
1072 }
1073 
1074 Static void
1075 athn_usb_wmieof(struct usbd_xfer *xfer, void * priv,
1076     usbd_status status)
1077 {
1078 	struct athn_usb_softc *usc = priv;
1079 
1080 	DPRINTFN(DBG_FN, usc, "\n");
1081 
1082 	if (__predict_false(status == USBD_STALLED))
1083 		usbd_clear_endpoint_stall_async(usc->usc_tx_intr_pipe);
1084 
1085 	usc->usc_wmi_done = 1;
1086 	wakeup(&usc->usc_wmi_done);
1087 }
1088 
1089 Static int
1090 athn_usb_wmi_xcmd(struct athn_usb_softc *usc, uint16_t cmd_id, void *ibuf,
1091     int ilen, void *obuf)
1092 {
1093 	struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
1094 	struct ar_htc_frame_hdr *htc;
1095 	struct ar_wmi_cmd_hdr *wmi;
1096 	int s, error;
1097 
1098 	if (usc->usc_dying)
1099 		return EIO;
1100 
1101 	DPRINTFN(DBG_FN, usc, "\n");
1102 
1103 	htc = (struct ar_htc_frame_hdr *)data->buf;
1104 	memset(htc, 0, sizeof(*htc));
1105 	htc->endpoint_id = usc->usc_ep_ctrl;
1106 	htc->payload_len = htobe16(sizeof(*wmi) + ilen);
1107 
1108 	wmi = (struct ar_wmi_cmd_hdr *)&htc[1];
1109 	wmi->cmd_id = htobe16(cmd_id);
1110 	usc->usc_wmi_seq_no++;
1111 	wmi->seq_no = htobe16(usc->usc_wmi_seq_no);
1112 
1113 	memcpy(&wmi[1], ibuf, ilen);
1114 
1115 	usbd_setup_xfer(data->xfer, usc, data->buf,
1116 	    sizeof(*htc) + sizeof(*wmi) + ilen,
1117 	    USBD_SHORT_XFER_OK, ATHN_USB_CMD_TIMEOUT,
1118 	    athn_usb_wmieof);
1119 
1120 	s = splusb();
1121 	usc->usc_wmi_done = 0;
1122 	usc->usc_wait_cmd_id = cmd_id;
1123 	error = usbd_transfer(data->xfer);
1124 	if (__predict_true(error == 0 || error == USBD_IN_PROGRESS)) {
1125 		usc->usc_obuf = obuf;
1126 
1127 		/* Wait for WMI command to complete. */
1128 		error = tsleep(&usc->usc_wait_cmd_id, 0, "athnwmi", hz);
1129 		usc->usc_wait_cmd_id = 0;
1130 		athn_usb_wait_wmi(usc);
1131 	}
1132 	splx(s);
1133 	return error;
1134 }
1135 
1136 Static void
1137 athn_usb_wait_wmi(struct athn_usb_softc *usc)
1138 {
1139 
1140 	DPRINTFN(DBG_FN, usc, "\n");
1141 
1142 	while (__predict_false(!usc->usc_wmi_done))
1143 		tsleep(&usc->usc_wmi_done, 0, "athnwmi", 0);
1144 }
1145 
1146 #ifdef unused
1147 Static int
1148 athn_usb_read_rom(struct athn_softc *sc)
1149 {
1150 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1151 	uint32_t addrs[8], vals[8], addr;
1152 	uint16_t *eep;
1153 	size_t i, j;
1154 	int error = 0;
1155 
1156 	DPRINTFN(DBG_FN, sc, "\n");
1157 
1158 	/* Read EEPROM by blocks of 16 bytes. */
1159 	eep = sc->sc_eep;
1160 	addr = AR_EEPROM_OFFSET(sc->sc_eep_base);
1161 	for (i = 0; i < sc->sc_eep_size / 16; i++) {
1162 		for (j = 0; j < 8; j++, addr += 4)
1163 			addrs[j] = htobe32(addr);
1164 		error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ,
1165 		    addrs, sizeof(addrs), vals);
1166 		if (error != 0)
1167 			break;
1168 		for (j = 0; j < 8; j++)
1169 			*eep++ = be32toh(vals[j]);
1170 	}
1171 	return error;
1172 }
1173 #endif /* unused */
1174 
1175 Static uint32_t
1176 athn_usb_read(struct athn_softc *sc, uint32_t addr)
1177 {
1178 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1179 	uint32_t val;
1180 	int error;
1181 
1182 	if (usc->usc_dying)
1183 		return 0;
1184 
1185 	DPRINTFN(DBG_FN, sc, "\n");
1186 
1187 	/* Flush pending writes for strict consistency. */
1188 	athn_usb_write_barrier(sc);
1189 
1190 	addr = htobe32(addr);
1191 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ,
1192 	    &addr, sizeof(addr), &val);
1193 	if (error != 0)
1194 		return 0xdeadbeef;
1195 	return be32toh(val);
1196 }
1197 
1198 Static void
1199 athn_usb_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
1200 {
1201 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1202 
1203 	if (usc->usc_dying)
1204 		return;
1205 
1206 	DPRINTFN(DBG_FN, sc, "\n");
1207 
1208 	usc->usc_wbuf[usc->usc_wcount].addr = htobe32(addr);
1209 	usc->usc_wbuf[usc->usc_wcount].val  = htobe32(val);
1210 	if (++usc->usc_wcount == AR_MAX_WRITE_COUNT)
1211 		athn_usb_write_barrier(sc);
1212 }
1213 
1214 Static void
1215 athn_usb_write_barrier(struct athn_softc *sc)
1216 {
1217 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1218 
1219 	if (usc->usc_dying)
1220 		goto done;
1221 
1222 	DPRINTFN(DBG_FN, sc, "\n");
1223 
1224 	if (usc->usc_wcount == 0)
1225 		return;
1226 
1227 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_WRITE,
1228 	    usc->usc_wbuf, usc->usc_wcount * sizeof(usc->usc_wbuf[0]), NULL);
1229  done:
1230 	usc->usc_wcount = 0;	/* Always flush buffer. */
1231 }
1232 
1233 Static int
1234 athn_usb_media_change(struct ifnet *ifp)
1235 {
1236 	struct athn_softc *sc = ifp->if_softc;
1237 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1238 	int error;
1239 
1240 	if (usc->usc_dying)
1241 		return EIO;
1242 
1243 	DPRINTFN(DBG_FN, sc, "\n");
1244 
1245 	error = ieee80211_media_change(ifp);
1246 	if (error == ENETRESET && IS_UP_AND_RUNNING(ifp)) {
1247 		athn_usb_stop(ifp);
1248 		error = athn_usb_init(ifp);
1249 	}
1250 	return error;
1251 }
1252 
1253 Static int
1254 athn_usb_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
1255     int arg)
1256 {
1257 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1258 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1259 	struct athn_usb_cmd_newstate cmd;
1260 
1261 	DPRINTFN(DBG_FN, sc, "\n");
1262 
1263 	/* Do it in a process context. */
1264 	cmd.state = nstate;
1265 	cmd.arg = arg;
1266 	athn_usb_do_async(usc, athn_usb_newstate_cb, &cmd, sizeof(cmd));
1267 	return 0;
1268 }
1269 
1270 Static void
1271 athn_usb_newstate_cb(struct athn_usb_softc *usc, void *arg)
1272 {
1273 	struct athn_usb_cmd_newstate *cmd = arg;
1274 	struct athn_softc *sc = &usc->usc_sc;
1275 	struct ieee80211com *ic = &sc->sc_ic;
1276 	enum ieee80211_state ostate, nstate;
1277 	uint32_t reg, imask;
1278 	int s;
1279 
1280 	DPRINTFN(DBG_FN, sc, "\n");
1281 
1282 	callout_stop(&sc->sc_calib_to);
1283 
1284 	s = splnet();
1285 
1286 	ostate = ic->ic_state;
1287 	nstate = cmd->state;
1288 	DPRINTFN(DBG_STM, usc, "newstate %s(%d) -> %s(%d)\n",
1289 		    ieee80211_state_name[ostate], ostate,
1290 		    ieee80211_state_name[nstate], nstate);
1291 
1292 	if (ostate == IEEE80211_S_RUN) {
1293 		uint8_t sta_index;
1294 
1295 		sta_index = ATHN_NODE(ic->ic_bss)->sta_index;
1296 		DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
1297 		athn_usb_remove_hw_node(usc, &sta_index);
1298 	}
1299 
1300 	switch (nstate) {
1301 	case IEEE80211_S_INIT:
1302 		athn_set_led(sc, 0);
1303 		break;
1304 	case IEEE80211_S_SCAN:
1305 		/* Make the LED blink while scanning. */
1306 		athn_set_led(sc, !sc->sc_led_state);
1307 		(void)athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
1308 		if (!usc->usc_dying)
1309 			callout_schedule(&sc->sc_scan_to, hz / 5);
1310 		break;
1311 	case IEEE80211_S_AUTH:
1312 		athn_set_led(sc, 0);
1313 		athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
1314 		break;
1315 	case IEEE80211_S_ASSOC:
1316 		break;
1317 	case IEEE80211_S_RUN:
1318 		athn_set_led(sc, 1);
1319 
1320 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
1321 			break;
1322 
1323 		/* Create node entry for our BSS. */
1324 		DPRINTFN(DBG_NODES, sc, "create node for AID=0x%x\n",
1325 		    ic->ic_bss->ni_associd);
1326 		athn_usb_create_node(usc, ic->ic_bss);	/* XXX: handle error? */
1327 
1328 		athn_set_bss(sc, ic->ic_bss);
1329 		athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
1330 #ifndef IEEE80211_STA_ONLY
1331 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1332 			athn_set_hostap_timers(sc);
1333 			/* Enable software beacon alert interrupts. */
1334 			imask = htobe32(AR_IMR_SWBA);
1335 		}
1336 		else
1337 #endif
1338 		{
1339 			athn_set_sta_timers(sc);
1340 			/* Enable beacon miss interrupts. */
1341 			imask = htobe32(AR_IMR_BMISS);
1342 
1343 			/* Stop receiving beacons from other BSS. */
1344 			reg = AR_READ(sc, AR_RX_FILTER);
1345 			reg = (reg & ~AR_RX_FILTER_BEACON) |
1346 			    AR_RX_FILTER_MYBEACON;
1347 			AR_WRITE(sc, AR_RX_FILTER, reg);
1348 			AR_WRITE_BARRIER(sc);
1349 		}
1350 		athn_usb_wmi_xcmd(usc, AR_WMI_CMD_ENABLE_INTR,
1351 		    &imask, sizeof(imask), NULL);
1352 		break;
1353 	}
1354 	if (!usc->usc_dying)
1355 		(void)sc->sc_newstate(ic, nstate, cmd->arg);
1356 	splx(s);
1357 }
1358 
1359 Static void
1360 athn_usb_newassoc(struct ieee80211_node *ni, int isnew)
1361 {
1362 	struct ieee80211com *ic = ni->ni_ic;
1363 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1364 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1365 
1366 	DPRINTFN(DBG_FN, sc, "\n");
1367 
1368 	if (ic->ic_opmode != IEEE80211_M_HOSTAP || !isnew)
1369 		return;
1370 
1371 	/* Do it in a process context. */
1372 	ieee80211_ref_node(ni);
1373 	athn_usb_do_async(usc, athn_usb_newassoc_cb, &ni, sizeof(ni));
1374 }
1375 
1376 Static void
1377 athn_usb_newassoc_cb(struct athn_usb_softc *usc, void *arg)
1378 {
1379 	struct ieee80211_node *ni = *(void **)arg;
1380 	int s;
1381 
1382 	DPRINTFN(DBG_FN, usc, "\n");
1383 
1384 	s = splnet();
1385 	/* NB: Node may have left before we got scheduled. */
1386 	if (ni->ni_associd != 0) {
1387 		DPRINTFN(DBG_NODES, usc, "creating node for AID=0x%x\n",
1388 		    ni->ni_associd);
1389 		(void)athn_usb_create_node(usc, ni);	/* XXX: handle error? */
1390 	}
1391 	ieee80211_free_node(ni);
1392 	splx(s);
1393 }
1394 
1395 #ifdef notyet
1396 Static int
1397 athn_usb_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
1398     uint8_t tid)
1399 {
1400 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1401 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1402 	struct athn_node *an = ATHN_NODE(ni);
1403 	struct athn_usb_aggr_cmd cmd;
1404 
1405 	DPRINTFN(DBG_FN, sc, "\n");
1406 
1407 	/* Do it in a process context. */
1408 	cmd.sta_index = an->sta_index;
1409 	cmd.tid = tid;
1410 	athn_usb_do_async(usc, athn_usb_ampdu_tx_start_cb, &cmd, sizeof(cmd));
1411 	return 0;
1412 }
1413 
1414 Static void
1415 athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *usc, void *arg)
1416 {
1417 	struct athn_usb_aggr_cmd *cmd = arg;
1418 	struct ar_htc_target_aggr aggr;
1419 
1420 	DPRINTFN(DBG_FN, usc, "\n");
1421 
1422 	memset(&aggr, 0, sizeof(aggr));
1423 	aggr.sta_index = cmd->sta_index;
1424 	aggr.tidno = cmd->tid;
1425 	aggr.aggr_enable = 1;
1426 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE,
1427 	    &aggr, sizeof(aggr), NULL);
1428 }
1429 
1430 Static void
1431 athn_usb_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
1432     uint8_t tid)
1433 {
1434 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1435 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1436 	struct athn_node *an = ATHN_NODE(ni);
1437 	struct athn_usb_aggr_cmd cmd;
1438 
1439 	DPRINTFN(DBG_FN, sc, "\n");
1440 
1441 	/* Do it in a process context. */
1442 	cmd.sta_index = an->sta_index;
1443 	cmd.tid = tid;
1444 	athn_usb_do_async(usc, athn_usb_ampdu_tx_stop_cb, &cmd, sizeof(cmd));
1445 }
1446 
1447 Static void
1448 athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *usc, void *arg)
1449 {
1450 	struct athn_usb_aggr_cmd *cmd = arg;
1451 	struct ar_htc_target_aggr aggr;
1452 
1453 	DPRINTFN(DBG_FN, usc, "\n");
1454 
1455 	memset(&aggr, 0, sizeof(aggr));
1456 	aggr.sta_index = cmd->sta_index;
1457 	aggr.tidno = cmd->tid;
1458 	aggr.aggr_enable = 0;
1459 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE,
1460 	    &aggr, sizeof(aggr), NULL);
1461 }
1462 #endif /* notyet */
1463 
1464 Static int
1465 athn_usb_remove_hw_node(struct athn_usb_softc *usc, uint8_t *sta_idx)
1466 {
1467 	int error;
1468 
1469 	DPRINTFN(DBG_FN, usc, "\n");
1470 
1471 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_REMOVE,
1472 	    sta_idx, sizeof(*sta_idx), NULL);
1473 
1474 	DPRINTFN(DBG_NODES, usc, "node=%u error=%d\n",
1475 	    *sta_idx, error);
1476 	return error;
1477 }
1478 
1479 Static int
1480 athn_usb_create_hw_node(struct athn_usb_softc *usc,
1481     struct ar_htc_target_sta *sta)
1482 {
1483 	int error;
1484 
1485 	DPRINTFN(DBG_FN, usc, "\n");
1486 
1487 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_CREATE,
1488 	    sta, sizeof(*sta), NULL);
1489 
1490 	DPRINTFN(DBG_NODES, usc, "node=%u error=%d\n",
1491 	    sta->sta_index, error);
1492 
1493 	return error;
1494 }
1495 
1496 Static int
1497 athn_usb_create_node(struct athn_usb_softc *usc, struct ieee80211_node *ni)
1498 {
1499 	struct athn_node *an = ATHN_NODE(ni);
1500 	struct ar_htc_target_sta sta;
1501 	struct ar_htc_target_rate rate;
1502 	int error;
1503 
1504 	DPRINTFN(DBG_FN | DBG_NODES, usc, "AID=0x%x\n", ni->ni_associd);
1505 
1506 	/*
1507 	 * NB: this is called by ic_newstate and (in HOSTAP mode by)
1508 	 * ic_newassoc.
1509 	 *
1510 	 * The firmware has a limit of 8 nodes.  In HOSTAP mode, we
1511 	 * limit the AID to < 8 and use that value to index the
1512 	 * firmware node table.  Node zero is used for the BSS.
1513 	 *
1514 	 * In STA mode, we simply use node 1 for the BSS.
1515 	 */
1516 	if (ATHN_SOFTC(usc)->sc_ic.ic_opmode == IEEE80211_M_HOSTAP)
1517 		an->sta_index = IEEE80211_NODE_AID(ni);
1518 	else
1519 		an->sta_index = 1;
1520 
1521 	/* Create node entry on target. */
1522 	memset(&sta, 0, sizeof(sta));
1523 	IEEE80211_ADDR_COPY(sta.macaddr, ni->ni_macaddr);
1524 	IEEE80211_ADDR_COPY(sta.bssid, ni->ni_bssid);
1525 
1526 	sta.associd = htobe16(ni->ni_associd);
1527 	sta.valid = 1;
1528 	sta.sta_index = an->sta_index;
1529 
1530 	sta.maxampdu = 0xffff;
1531 #ifndef IEEE80211_NO_HT
1532 	if (ni->ni_flags & IEEE80211_NODE_HT)
1533 		sta.flags |= htobe16(AR_HTC_STA_HT);
1534 #endif
1535 	error = athn_usb_create_hw_node(usc, &sta);
1536 	if (error)
1537 		return error;
1538 
1539 	/* Setup supported rates. */
1540 	memset(&rate, 0, sizeof(rate));
1541 	rate.sta_index = sta.sta_index;
1542 	rate.isnew = 1;
1543 	rate.lg_rates.rs_nrates = ni->ni_rates.rs_nrates;
1544 	memcpy(rate.lg_rates.rs_rates, ni->ni_rates.rs_rates,
1545 	    ni->ni_rates.rs_nrates);
1546 
1547 #ifndef IEEE80211_NO_HT
1548 	if (ni->ni_flags & IEEE80211_NODE_HT) {
1549 		rate.capflags |= htobe32(AR_RC_HT_FLAG);
1550 #ifdef notyet
1551 		/* XXX setup HT rates */
1552 		if (ni->ni_htcaps & IEEE80211_HTCAP_CBW20_40)
1553 			rate.capflags |= htobe32(AR_RC_40_FLAG);
1554 		if (ni->ni_htcaps & IEEE80211_HTCAP_SGI40)
1555 			rate.capflags |= htobe32(AR_RC_SGI_FLAG);
1556 		if (ni->ni_htcaps & IEEE80211_HTCAP_SGI20)
1557 			rate.capflags |= htobe32(AR_RC_SGI_FLAG);
1558 #endif
1559 	}
1560 #endif
1561 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_RC_RATE_UPDATE,
1562 	    &rate, sizeof(rate), NULL);
1563 	return error;
1564 }
1565 
1566 Static void
1567 athn_usb_rx_enable(struct athn_softc *sc)
1568 {
1569 
1570 	DPRINTFN(DBG_FN, sc, "\n");
1571 
1572 	AR_WRITE(sc, AR_CR, AR_CR_RXE);
1573 	AR_WRITE_BARRIER(sc);
1574 }
1575 
1576 Static int
1577 athn_usb_switch_chan(struct athn_softc *sc, struct ieee80211_channel *curchan,
1578     struct ieee80211_channel *extchan)
1579 {
1580 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1581 	uint16_t mode;
1582 	int error;
1583 
1584 	DPRINTFN(DBG_FN, sc, "\n");
1585 
1586 	/* Disable interrupts. */
1587 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
1588 	if (error != 0)
1589 		goto reset;
1590 	/* Stop all Tx queues. */
1591 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL);
1592 	if (error != 0)
1593 		goto reset;
1594 	/* Stop Rx. */
1595 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV);
1596 	if (error != 0)
1597 		goto reset;
1598 
1599 	/* If band or bandwidth changes, we need to do a full reset. */
1600 	if (curchan->ic_flags != sc->sc_curchan->ic_flags ||
1601 	    ((extchan != NULL) ^ (sc->sc_curchanext != NULL))) {
1602 		DPRINTFN(DBG_RF, sc, "channel band switch\n");
1603 		goto reset;
1604 	}
1605 
1606 	error = athn_set_chan(sc, curchan, extchan);
1607 	if (AR_SREV_9271(sc) && error == 0)
1608 		ar9271_load_ani(sc);
1609 	if (error != 0) {
1610  reset:		/* Error found, try a full reset. */
1611 		DPRINTFN(DBG_RF, sc, "needs a full reset\n");
1612 		error = athn_hw_reset(sc, curchan, extchan, 0);
1613 		if (error != 0)	/* Hopeless case. */
1614 			return error;
1615 	}
1616 
1617 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV);
1618 	if (error != 0)
1619 		return error;
1620 	athn_rx_start(sc);
1621 
1622 	mode = htobe16(IEEE80211_IS_CHAN_2GHZ(curchan) ?
1623 	    AR_HTC_MODE_11NG : AR_HTC_MODE_11NA);
1624 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE,
1625 	    &mode, sizeof(mode), NULL);
1626 	if (error != 0)
1627 		return error;
1628 
1629 	/* Re-enable interrupts. */
1630 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ENABLE_INTR);
1631 	return error;
1632 }
1633 
1634 #ifdef notyet_edca
1635 Static void
1636 athn_usb_updateedca(struct ieee80211com *ic)
1637 {
1638 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1639 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1640 
1641 	DPRINTFN(DBG_FN, sc, "\n");
1642 
1643 	/* Do it in a process context. */
1644 	athn_usb_do_async(usc, athn_usb_updateedca_cb, NULL, 0);
1645 }
1646 
1647 Static void
1648 athn_usb_updateedca_cb(struct athn_usb_softc *usc, void *arg)
1649 {
1650 	int s;
1651 
1652 	DPRINTFN(DBG_FN, usc, "\n");
1653 
1654 	s = splnet();
1655 	athn_updateedca(&usc->usc_sc.sc_ic);
1656 	splx(s);
1657 }
1658 #endif /* notyet_edca */
1659 
1660 Static void
1661 athn_usb_updateslot(struct ifnet *ifp)
1662 {
1663 	struct athn_softc *sc = ifp->if_softc;
1664 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1665 
1666 	DPRINTFN(DBG_FN, sc, "\n");
1667 
1668 	/*
1669 	 * NB: athn_updateslog() needs to be done in a process context
1670 	 * to avoid being called by ieee80211_reset_erp() inside a
1671 	 * spinlock held by ieee80211_free_allnodes().
1672 	 *
1673 	 * XXX: calling this during the athn_attach() causes
1674 	 * usb_insert_transfer() to produce a bunch of "not busy"
1675 	 * messages.  Why?
1676 	 */
1677 	if (usc->usc_athn_attached)
1678 		athn_usb_do_async(usc, athn_usb_updateslot_cb, NULL, 0);
1679 }
1680 
1681 Static void
1682 athn_usb_updateslot_cb(struct athn_usb_softc *usc, void *arg)
1683 {
1684 	int s;
1685 
1686 	DPRINTFN(DBG_FN, usc, "\n");
1687 
1688 	s = splnet();
1689 	athn_updateslot(&usc->usc_sc.sc_if);
1690 	splx(s);
1691 }
1692 
1693 #ifdef notyet
1694 Static int
1695 athn_usb_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
1696     struct ieee80211_key *k)
1697 {
1698 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1699 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1700 	struct ifnet *ifp = &usc->usc_sc.sc_if;
1701 	struct athn_usb_cmd_key cmd;
1702 
1703 	DPRINTFN(DBG_FN, sc, "\n");
1704 
1705 	/* Defer setting of WEP keys until interface is brought up. */
1706 	if (!IS_UP_AND_RUNNING(ifp))
1707 		return 0;
1708 
1709 	/* Do it in a process context. */
1710 	cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL;
1711 	cmd.key = k;
1712 	athn_usb_do_async(usc, athn_usb_set_key_cb, &cmd, sizeof(cmd));
1713 	return 0;
1714 }
1715 
1716 Static void
1717 athn_usb_set_key_cb(struct athn_usb_softc *usc, void *arg)
1718 {
1719 	struct ieee80211com *ic = &usc->usc_sc.sc_ic;
1720 	struct athn_usb_cmd_key *cmd = arg;
1721 	int s;
1722 
1723 	DPRINTFN(DBG_FN, usc, "\n");
1724 
1725 	s = splnet();
1726 	athn_set_key(ic, cmd->ni, cmd->key);
1727 	if (cmd->ni != NULL)
1728 		ieee80211_free_node(cmd->ni);
1729 	splx(s);
1730 }
1731 
1732 Static void
1733 athn_usb_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
1734     struct ieee80211_key *k)
1735 {
1736 	struct athn_softc *sc = ic->ic_ifp->if_softc;
1737 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1738 	struct ifnet *ifp = &usc->usc_sc.sc_if;
1739 	struct athn_usb_cmd_key cmd;
1740 
1741 	DPRINTFN(DBG_FN, sc, "\n");
1742 
1743 	if (!(ifp->if_flags & IFF_RUNNING) ||
1744 	    ic->ic_state != IEEE80211_S_RUN)
1745 		return;	/* Nothing to do. */
1746 
1747 	/* Do it in a process context. */
1748 	cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL;
1749 	cmd.key = k;
1750 	athn_usb_do_async(usc, athn_usb_delete_key_cb, &cmd, sizeof(cmd));
1751 }
1752 
1753 Static void
1754 athn_usb_delete_key_cb(struct athn_usb_softc *usc, void *arg)
1755 {
1756 	struct ieee80211com *ic = &usc->usc_sc.sc_ic;
1757 	struct athn_usb_cmd_key *cmd = arg;
1758 	int s;
1759 
1760 	DPRINTFN(DBG_FN, usc, "\n");
1761 
1762 	s = splnet();
1763 	athn_delete_key(ic, cmd->ni, cmd->key);
1764 	if (cmd->ni != NULL)
1765 		ieee80211_free_node(cmd->ni);
1766 	splx(s);
1767 }
1768 #endif /* notyet */
1769 
1770 #ifndef IEEE80211_STA_ONLY
1771 Static void
1772 athn_usb_bcneof(struct usbd_xfer *xfer, void * priv,
1773     usbd_status status)
1774 {
1775 	struct athn_usb_tx_data *data = priv;
1776 	struct athn_usb_softc *usc = data->sc;
1777 
1778 	DPRINTFN(DBG_FN, usc, "\n");
1779 
1780 	if (__predict_false(status == USBD_STALLED))
1781 		usbd_clear_endpoint_stall_async(usc->usc_tx_data_pipe);
1782 	usc->usc_tx_bcn = data;
1783 }
1784 
1785 /*
1786  * Process Software Beacon Alert interrupts.
1787  */
1788 Static void
1789 athn_usb_swba(struct athn_usb_softc *usc)
1790 {
1791 	struct athn_softc *sc = &usc->usc_sc;
1792 	struct ieee80211com *ic = &sc->sc_ic;
1793 	struct athn_usb_tx_data *data;
1794 	struct ieee80211_frame *wh;
1795 	struct ieee80211_beacon_offsets bo;
1796 	struct ar_stream_hdr *hdr;
1797 	struct ar_htc_frame_hdr *htc;
1798 	struct ar_tx_bcn *bcn;
1799 	struct mbuf *m;
1800 	int error;
1801 
1802 	if (usc->usc_dying)
1803 		return;
1804 
1805 	DPRINTFN(DBG_FN, sc, "\n");
1806 
1807 	if (ic->ic_dtim_count == 0)
1808 		ic->ic_dtim_count = ic->ic_dtim_period - 1;
1809 	else
1810 		ic->ic_dtim_count--;
1811 
1812 	/* Make sure previous beacon has been sent. */
1813 	if (usc->usc_tx_bcn == NULL)
1814 		return;
1815 	data = usc->usc_tx_bcn;
1816 
1817 	/* Get new beacon. */
1818 #ifdef ATHN_DEBUG
1819 	memset(&bo, 0, sizeof(bo));
1820 #endif
1821 	m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
1822 	if (__predict_false(m == NULL))
1823 		return;
1824 	/* Assign sequence number. */
1825 	/* XXX: use non-QoS tid? */
1826 	wh = mtod(m, struct ieee80211_frame *);
1827 	*(uint16_t *)&wh->i_seq[0] =
1828 	    htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
1829 	ic->ic_bss->ni_txseqs[0]++;
1830 
1831 	hdr = (struct ar_stream_hdr *)data->buf;
1832 	hdr->tag = htole16(AR_USB_TX_STREAM_TAG);
1833 	hdr->len = htole16(sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len);
1834 
1835 	htc = (struct ar_htc_frame_hdr *)&hdr[1];
1836 	memset(htc, 0, sizeof(*htc));
1837 	htc->endpoint_id = usc->usc_ep_bcn;
1838 	htc->payload_len = htobe16(sizeof(*bcn) + m->m_pkthdr.len);
1839 
1840 	bcn = (struct ar_tx_bcn *)&htc[1];
1841 	memset(bcn, 0, sizeof(*bcn));
1842 	bcn->vif_idx = 0;
1843 
1844 	m_copydata(m, 0, m->m_pkthdr.len, (void *)&bcn[1]);
1845 
1846 	usbd_setup_xfer(data->xfer, data, data->buf,
1847 	    sizeof(*hdr) + sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len,
1848 	    USBD_SHORT_XFER_OK, ATHN_USB_TX_TIMEOUT,
1849 	    athn_usb_bcneof);
1850 
1851 	m_freem(m);
1852 	usc->usc_tx_bcn = NULL;
1853 	error = usbd_transfer(data->xfer);
1854 	if (__predict_false(error != USBD_IN_PROGRESS && error != 0))
1855 		usc->usc_tx_bcn = data;
1856 }
1857 #endif
1858 
1859 Static void
1860 athn_usb_rx_wmi_ctrl(struct athn_usb_softc *usc, uint8_t *buf, size_t len)
1861 {
1862 #ifdef ATHN_DEBUG
1863 	struct ar_wmi_evt_txrate *txrate;
1864 #endif
1865 	struct ar_wmi_cmd_hdr *wmi;
1866 	uint16_t cmd_id;
1867 
1868 	if (usc->usc_dying)
1869 		return;
1870 
1871 	DPRINTFN(DBG_FN, usc, "\n");
1872 
1873 	if (__predict_false(len < sizeof(*wmi)))
1874 		return;
1875 	wmi = (struct ar_wmi_cmd_hdr *)buf;
1876 	cmd_id = be16toh(wmi->cmd_id);
1877 
1878 	if (!(cmd_id & AR_WMI_EVT_FLAG)) {
1879 		if (usc->usc_wait_cmd_id != cmd_id)
1880 			return;	/* Unexpected reply. */
1881 		if (usc->usc_obuf != NULL) {
1882 			/* Copy answer into caller supplied buffer. */
1883 			memcpy(usc->usc_obuf, &wmi[1], len - sizeof(*wmi));
1884 		}
1885 		/* Notify caller of completion. */
1886 		usc->usc_wait_cmd_id = 0;
1887 		wakeup(&usc->usc_wait_cmd_id);
1888 		return;
1889 	}
1890 	/*
1891 	 * XXX: the Linux 2.6 and 3.7.4 kernels differ on the event numbers!
1892 	 * See the alternate defines in if_athn_usb.h.
1893 	 */
1894 	switch (cmd_id & 0xfff) {
1895 #ifndef IEEE80211_STA_ONLY
1896 	case AR_WMI_EVT_SWBA:
1897 		athn_usb_swba(usc);
1898 		break;
1899 #endif
1900 	case AR_WMI_EVT_FATAL:
1901 		aprint_error_dev(usc->usc_dev, "fatal firmware error\n");
1902 		break;
1903 	case AR_WMI_EVT_TXRATE:
1904 #ifdef ATHN_DEBUG
1905 		txrate = (struct ar_wmi_evt_txrate *)&wmi[1];
1906 		DPRINTFN(DBG_TX, usc, "txrate=%d\n", be32toh(txrate->txrate));
1907 #endif
1908 		break;
1909 	default:
1910 		DPRINTFN(DBG_TX, usc, "WMI event 0x%x (%d) ignored\n", cmd_id, cmd_id);
1911 		break;
1912 	}
1913 }
1914 
1915 Static void
1916 athn_usb_intr(struct usbd_xfer *xfer, void * priv,
1917     usbd_status status)
1918 {
1919 	struct athn_usb_softc *usc = priv;
1920 	struct ar_htc_frame_hdr *htc;
1921 	struct ar_htc_msg_hdr *msg;
1922 	uint8_t *buf = usc->usc_ibuf;
1923 	uint16_t msg_id;
1924 	int len;
1925 
1926 	if (usc->usc_dying)
1927 		return;
1928 
1929 	DPRINTFN(DBG_FN, usc, "\n");
1930 
1931 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
1932 		DPRINTFN(DBG_INTR, usc, "intr status=%d\n", status);
1933 		if (status == USBD_STALLED)
1934 			usbd_clear_endpoint_stall_async(usc->usc_rx_intr_pipe);
1935 		return;
1936 	}
1937 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1938 
1939 	/* Skip watchdog pattern if present. */
1940 	if (len >= 4 && *(uint32_t *)buf == htobe32(0x00c60000)) {
1941 		buf += 4;
1942 		len -= 4;
1943 	}
1944 	if (__predict_false(len < (int)sizeof(*htc)))
1945 		return;
1946 	htc = (struct ar_htc_frame_hdr *)buf;
1947 	/* Skip HTC header. */
1948 	buf += sizeof(*htc);
1949 	len -= sizeof(*htc);
1950 
1951 	if (htc->endpoint_id != 0) {
1952 		if (__predict_false(htc->endpoint_id != usc->usc_ep_ctrl))
1953 			return;
1954 		/* Remove trailer if present. */
1955 		if (htc->flags & AR_HTC_FLAG_TRAILER) {
1956 			if (__predict_false(len < htc->control[0]))
1957 				return;
1958 			len -= htc->control[0];
1959 		}
1960 		athn_usb_rx_wmi_ctrl(usc, buf, len);
1961 		return;
1962 	}
1963 
1964 	/*
1965 	 * Endpoint 0 carries HTC messages.
1966 	 */
1967 	if (__predict_false(len < (int)sizeof(*msg)))
1968 		return;
1969 	msg = (struct ar_htc_msg_hdr *)buf;
1970 	msg_id = be16toh(msg->msg_id);
1971 	DPRINTFN(DBG_RX, usc, "Rx HTC message %d\n", msg_id);
1972 	switch (msg_id) {
1973 	case AR_HTC_MSG_READY:
1974 	case AR_HTC_MSG_CONF_PIPE_RSP:
1975 		if (usc->usc_wait_msg_id != msg_id)
1976 			break;
1977 		usc->usc_wait_msg_id = 0;
1978 		wakeup(&usc->usc_wait_msg_id);
1979 		break;
1980 	case AR_HTC_MSG_CONN_SVC_RSP:
1981 		if (usc->usc_wait_msg_id != msg_id)
1982 			break;
1983 		if (usc->usc_msg_conn_svc_rsp != NULL) {
1984 			memcpy(usc->usc_msg_conn_svc_rsp, &msg[1],
1985 			    sizeof(*usc->usc_msg_conn_svc_rsp));
1986 		}
1987 		usc->usc_wait_msg_id = 0;
1988 		wakeup(&usc->usc_wait_msg_id);
1989 		break;
1990 	default:
1991 		DPRINTFN(DBG_RX, usc, "HTC message %d ignored\n", msg_id);
1992 		break;
1993 	}
1994 }
1995 
1996 Static void
1997 athn_usb_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
1998     struct ar_rx_status *rs)
1999 {
2000 	struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
2001 	struct ieee80211com *ic = &sc->sc_ic;
2002 	uint8_t rate;
2003 
2004 	DPRINTFN(DBG_FN, sc, "\n");
2005 
2006 	tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
2007 	tap->wr_tsft = htole64(be64toh(rs->rs_tstamp));
2008 	tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2009 	tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2010 	tap->wr_dbm_antsignal = rs->rs_rssi;
2011 	/* XXX noise. */
2012 	tap->wr_antenna = rs->rs_antenna;
2013 	rate = rs->rs_rate;
2014 	if (rate & 0x80) {		/* HT. */
2015 		/* Bit 7 set means HT MCS instead of rate. */
2016 		tap->wr_rate = rate;
2017 		if (!(rs->rs_flags & AR_RXS_FLAG_GI))
2018 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
2019 	}
2020 	else if (rate & 0x10) {	/* CCK. */
2021 		if (rate & 0x04)
2022 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2023 		switch (rate & ~0x14) {
2024 		case 0xb: tap->wr_rate =   2; break;
2025 		case 0xa: tap->wr_rate =   4; break;
2026 		case 0x9: tap->wr_rate =  11; break;
2027 		case 0x8: tap->wr_rate =  22; break;
2028 		default:  tap->wr_rate =   0; break;
2029 		}
2030 	}
2031 	else {			/* OFDM. */
2032 		switch (rate) {
2033 		case 0xb: tap->wr_rate =  12; break;
2034 		case 0xf: tap->wr_rate =  18; break;
2035 		case 0xa: tap->wr_rate =  24; break;
2036 		case 0xe: tap->wr_rate =  36; break;
2037 		case 0x9: tap->wr_rate =  48; break;
2038 		case 0xd: tap->wr_rate =  72; break;
2039 		case 0x8: tap->wr_rate =  96; break;
2040 		case 0xc: tap->wr_rate = 108; break;
2041 		default:  tap->wr_rate =   0; break;
2042 		}
2043 	}
2044 	bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
2045 }
2046 
2047 Static void
2048 athn_usb_rx_frame(struct athn_usb_softc *usc, struct mbuf *m)
2049 {
2050 	struct athn_softc *sc = &usc->usc_sc;
2051 	struct ieee80211com *ic = &sc->sc_ic;
2052 	struct ifnet *ifp = &sc->sc_if;
2053 	struct ieee80211_frame *wh;
2054 	struct ieee80211_node *ni;
2055 	struct ar_htc_frame_hdr *htc;
2056 	struct ar_rx_status *rs;
2057 	uint16_t datalen;
2058 	int s;
2059 
2060 	DPRINTFN(DBG_FN, sc, "\n");
2061 
2062 	if (__predict_false(m->m_len < (int)sizeof(*htc)))
2063 		goto skip;
2064 	htc = mtod(m, struct ar_htc_frame_hdr *);
2065 	if (__predict_false(htc->endpoint_id == 0)) {
2066 		DPRINTFN(DBG_RX, sc, "bad endpoint %d\n", htc->endpoint_id);
2067 		goto skip;
2068 	}
2069 	if (htc->flags & AR_HTC_FLAG_TRAILER) {
2070 		if (m->m_len < htc->control[0])
2071 			goto skip;
2072 		m_adj(m, -(int)htc->control[0]);
2073 	}
2074 	m_adj(m, sizeof(*htc));	/* Strip HTC header. */
2075 
2076 	if (__predict_false(m->m_len < (int)sizeof(*rs)))
2077 		goto skip;
2078 	rs = mtod(m, struct ar_rx_status *);
2079 
2080 	/* Make sure that payload fits. */
2081 	datalen = be16toh(rs->rs_datalen);
2082 	if (__predict_false(m->m_len < (int)sizeof(*rs) + datalen))
2083 		goto skip;
2084 
2085 	/* Ignore runt frames.  Let ACKs be seen by bpf */
2086 	if (__predict_false(datalen <
2087 		sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN))
2088 		goto skip;
2089 
2090 	m_adj(m, sizeof(*rs));	/* Strip Rx status. */
2091 	m_set_rcvif(m, ifp);
2092 
2093 	s = splnet();
2094 
2095 	/* Grab a reference to the source node. */
2096 	wh = mtod(m, struct ieee80211_frame *);
2097 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
2098 
2099 	/* Remove any HW padding after the 802.11 header. */
2100 	if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
2101 		u_int hdrlen = ieee80211_anyhdrsize(wh);
2102 		if (hdrlen & 3) {
2103 			ovbcopy(wh, (uint8_t *)wh + 2, hdrlen);
2104 			m_adj(m, 2);
2105 		}
2106 	}
2107 	if (__predict_false(sc->sc_drvbpf != NULL))
2108 		athn_usb_rx_radiotap(sc, m, rs);
2109 
2110 	/* Trim 802.11 FCS after radiotap. */
2111 	m_adj(m, -IEEE80211_CRC_LEN);
2112 
2113 	/* Send the frame to the 802.11 layer. */
2114 	ieee80211_input(ic, m, ni, rs->rs_rssi + AR_USB_DEFAULT_NF, 0);
2115 
2116 	/* Node is no longer needed. */
2117 	ieee80211_free_node(ni);
2118 	splx(s);
2119 	return;
2120  skip:
2121 	m_freem(m);
2122 }
2123 
2124 Static void
2125 athn_usb_rxeof(struct usbd_xfer *xfer, void * priv,
2126     usbd_status status)
2127 {
2128 	struct athn_usb_rx_data *data = priv;
2129 	struct athn_usb_softc *usc = data->sc;
2130 	struct athn_usb_rx_stream *stream = &usc->usc_rx_stream;
2131 	uint8_t *buf = data->buf;
2132 	struct ar_stream_hdr *hdr;
2133 	struct mbuf *m;
2134 	uint16_t pktlen;
2135 	int off, len;
2136 
2137 	if (usc->usc_dying)
2138 		return;
2139 
2140 	DPRINTFN(DBG_FN, usc, "\n");
2141 
2142 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
2143 		DPRINTFN(DBG_RX, usc, "RX status=%d\n", status);
2144 		if (status == USBD_STALLED)
2145 			usbd_clear_endpoint_stall_async(usc->usc_rx_data_pipe);
2146 		if (status != USBD_CANCELLED)
2147 			goto resubmit;
2148 		return;
2149 	}
2150 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2151 
2152 	if (stream->left > 0) {
2153 		if (len >= stream->left) {
2154 			/* We have all our pktlen bytes now. */
2155 			if (__predict_true(stream->m != NULL)) {
2156 				memcpy(mtod(stream->m, uint8_t *) +
2157 				    stream->moff, buf, stream->left);
2158 				athn_usb_rx_frame(usc, stream->m);
2159 				stream->m = NULL;
2160 			}
2161 			/* Next header is 32-bit aligned. */
2162 			off = (stream->left + 3) & ~3;
2163 			buf += off;
2164 			len -= off;
2165 			stream->left = 0;
2166 		}
2167 		else {
2168 			/* Still need more bytes, save what we have. */
2169 			if (__predict_true(stream->m != NULL)) {
2170 				memcpy(mtod(stream->m, uint8_t *) +
2171 				    stream->moff, buf, len);
2172 				stream->moff += len;
2173 			}
2174 			stream->left -= len;
2175 			goto resubmit;
2176 		}
2177 	}
2178 	KASSERT(stream->left == 0);
2179 	while (len >= (int)sizeof(*hdr)) {
2180 		hdr = (struct ar_stream_hdr *)buf;
2181 		if (hdr->tag != htole16(AR_USB_RX_STREAM_TAG)) {
2182 			DPRINTFN(DBG_RX, usc, "invalid tag 0x%x\n", hdr->tag);
2183 			break;
2184 		}
2185 		pktlen = le16toh(hdr->len);
2186 		buf += sizeof(*hdr);
2187 		len -= sizeof(*hdr);
2188 
2189 		if (__predict_true(pktlen <= MCLBYTES)) {
2190 			/* Allocate an mbuf to store the next pktlen bytes. */
2191 			MGETHDR(m, M_DONTWAIT, MT_DATA);
2192 			if (__predict_true(m != NULL)) {
2193 				m->m_pkthdr.len = m->m_len = pktlen;
2194 				if (pktlen > MHLEN) {
2195 					MCLGET(m, M_DONTWAIT);
2196 					if (!(m->m_flags & M_EXT)) {
2197 						m_free(m);
2198 						m = NULL;
2199 					}
2200 				}
2201 			}
2202 		}
2203 		else	/* Drop frames larger than MCLBYTES. */
2204 			m = NULL;
2205 		/*
2206 		 * NB: m can be NULL, in which case the next pktlen bytes
2207 		 * will be discarded from the Rx stream.
2208 		 */
2209 		if (pktlen > len) {
2210 			/* Need more bytes, save what we have. */
2211 			stream->m = m;	/* NB: m can be NULL. */
2212 			if (__predict_true(stream->m != NULL)) {
2213 				memcpy(mtod(stream->m, uint8_t *), buf, len);
2214 				stream->moff = len;
2215 			}
2216 			stream->left = pktlen - len;
2217 			goto resubmit;
2218 		}
2219 		if (__predict_true(m != NULL)) {
2220 			/* We have all the pktlen bytes in this xfer. */
2221 			memcpy(mtod(m, uint8_t *), buf, pktlen);
2222 			athn_usb_rx_frame(usc, m);
2223 		}
2224 
2225 		/* Next header is 32-bit aligned. */
2226 		off = (pktlen + 3) & ~3;
2227 		buf += off;
2228 		len -= off;
2229 	}
2230 
2231  resubmit:
2232 	/* Setup a new transfer. */
2233 	usbd_setup_xfer(xfer, data, data->buf, ATHN_USB_RXBUFSZ,
2234 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, athn_usb_rxeof);
2235 	(void)usbd_transfer(xfer);
2236 }
2237 
2238 Static void
2239 athn_usb_txeof(struct usbd_xfer *xfer, void * priv,
2240     usbd_status status)
2241 {
2242 	struct athn_usb_tx_data *data = priv;
2243 	struct athn_usb_softc *usc = data->sc;
2244 	struct athn_softc *sc = &usc->usc_sc;
2245 	struct ifnet *ifp = &sc->sc_if;
2246 	int s;
2247 
2248 	if (usc->usc_dying)
2249 		return;
2250 
2251 	DPRINTFN(DBG_FN, usc, "\n");
2252 
2253 	s = splnet();
2254 	/* Put this Tx buffer back to our free list. */
2255 	mutex_enter(&usc->usc_tx_mtx);
2256 	TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
2257 	mutex_exit(&usc->usc_tx_mtx);
2258 
2259 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
2260 		DPRINTFN(DBG_TX, sc, "TX status=%d\n", status);
2261 		if (status == USBD_STALLED)
2262 			usbd_clear_endpoint_stall_async(usc->usc_tx_data_pipe);
2263 		ifp->if_oerrors++;
2264 		splx(s);
2265 		/* XXX Why return? */
2266 		return;
2267 	}
2268 	sc->sc_tx_timer = 0;
2269 	ifp->if_opackets++;
2270 
2271 	/* We just released a Tx buffer, notify Tx. */
2272 	if (ifp->if_flags & IFF_OACTIVE) {
2273 		ifp->if_flags &= ~IFF_OACTIVE;
2274 		ifp->if_start(ifp);
2275 	}
2276 	splx(s);
2277 }
2278 
2279 Static int
2280 athn_usb_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2281     	struct athn_usb_tx_data *data)
2282 {
2283 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2284 	struct athn_node *an = ATHN_NODE(ni);
2285 	struct ieee80211com *ic = &sc->sc_ic;
2286 	struct ieee80211_frame *wh;
2287 	struct ieee80211_key *k = NULL;
2288 	struct ar_stream_hdr *hdr;
2289 	struct ar_htc_frame_hdr *htc;
2290 	struct ar_tx_frame *txf;
2291 	struct ar_tx_mgmt *txm;
2292 	uint8_t *frm;
2293 	uint8_t sta_index, qid, tid;
2294 	int error, s, xferlen;
2295 
2296 	DPRINTFN(DBG_FN, sc, "\n");
2297 
2298 	wh = mtod(m, struct ieee80211_frame *);
2299 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2300 		k = ieee80211_crypto_encap(ic, ni, m);
2301 		if (k == NULL)
2302 			return ENOBUFS;
2303 
2304 		/* packet header may have moved, reset our local pointer */
2305 		wh = mtod(m, struct ieee80211_frame *);
2306 	}
2307 #ifdef notyet_edca
2308 	if (ieee80211_has_qos(wh)) {
2309 		uint16_t qos;
2310 
2311 		qos = ieee80211_get_qos(wh);
2312 		tid = qos & IEEE80211_QOS_TID;
2313 		qid = ieee80211_up_to_ac(ic, tid);
2314 	}
2315 	else
2316 #endif /* notyet_edca */
2317 	{
2318 		tid = 0;
2319 		qid = WME_AC_BE;
2320 	}
2321 
2322 	/* XXX Change radiotap Tx header for USB (no txrate). */
2323 	if (__predict_false(sc->sc_drvbpf != NULL)) {
2324 		struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
2325 
2326 		tap->wt_flags = 0;
2327 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2328 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2329 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
2330 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2331 
2332 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
2333 	}
2334 	sta_index = an->sta_index;
2335 
2336 	/* NB: We don't take advantage of USB Tx stream mode for now. */
2337 	hdr = (struct ar_stream_hdr *)data->buf;
2338 	hdr->tag = htole16(AR_USB_TX_STREAM_TAG);
2339 
2340 	htc = (struct ar_htc_frame_hdr *)&hdr[1];
2341 	memset(htc, 0, sizeof(*htc));
2342 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2343 	    IEEE80211_FC0_TYPE_DATA) {
2344 		htc->endpoint_id = usc->usc_ep_data[qid];
2345 
2346 		txf = (struct ar_tx_frame *)&htc[1];
2347 		memset(txf, 0, sizeof(*txf));
2348 		txf->data_type = AR_HTC_NORMAL;
2349 		txf->node_idx = sta_index;
2350 		txf->vif_idx = 0;
2351 		txf->tid = tid;
2352 		if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
2353 			txf->flags |= htobe32(AR_HTC_TX_RTSCTS);
2354 		else if (ic->ic_flags & IEEE80211_F_USEPROT) {
2355 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2356 				txf->flags |= htobe32(AR_HTC_TX_CTSONLY);
2357 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2358 				txf->flags |= htobe32(AR_HTC_TX_RTSCTS);
2359 		}
2360 		txf->key_idx = 0xff;
2361 		frm = (uint8_t *)&txf[1];
2362 	}
2363 	else {
2364 		htc->endpoint_id = usc->usc_ep_mgmt;
2365 
2366 		txm = (struct ar_tx_mgmt *)&htc[1];
2367 		memset(txm, 0, sizeof(*txm));
2368 		txm->node_idx = sta_index;
2369 		txm->vif_idx = 0;
2370 		txm->key_idx = 0xff;
2371 		frm = (uint8_t *)&txm[1];
2372 	}
2373 	/* Copy payload. */
2374 	m_copydata(m, 0, m->m_pkthdr.len, (void *)frm);
2375 	frm += m->m_pkthdr.len;
2376 
2377 	/* Finalize headers. */
2378 	htc->payload_len = htobe16(frm - (uint8_t *)&htc[1]);
2379 	hdr->len = htole16(frm - (uint8_t *)&hdr[1]);
2380 	xferlen = frm - data->buf;
2381 
2382 	s = splnet();
2383 	usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
2384 	    USBD_FORCE_SHORT_XFER, ATHN_USB_TX_TIMEOUT, athn_usb_txeof);
2385 	error = usbd_transfer(data->xfer);
2386 	if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) {
2387 		splx(s);
2388 		return error;
2389 	}
2390 	splx(s);
2391 	return 0;
2392 }
2393 
2394 Static void
2395 athn_usb_start(struct ifnet *ifp)
2396 {
2397 	struct athn_softc *sc = ifp->if_softc;
2398 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2399 	struct ieee80211com *ic = &sc->sc_ic;
2400 	struct athn_usb_tx_data *data;
2401 	struct ether_header *eh;
2402 	struct ieee80211_node *ni;
2403 	struct mbuf *m;
2404 
2405 	if (usc->usc_dying)
2406 		return;
2407 
2408 	DPRINTFN(DBG_FN, sc, "\n");
2409 
2410 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2411 		return;
2412 
2413 	data = NULL;
2414 	for (;;) {
2415 		mutex_enter(&usc->usc_tx_mtx);
2416 		if (data == NULL && !TAILQ_EMPTY(&usc->usc_tx_free_list)) {
2417 			data = TAILQ_FIRST(&usc->usc_tx_free_list);
2418 			TAILQ_REMOVE(&usc->usc_tx_free_list, data, next);
2419 		}
2420 		mutex_exit(&usc->usc_tx_mtx);
2421 
2422 		if (data == NULL) {
2423 			ifp->if_flags |= IFF_OACTIVE;
2424 			return;
2425 		}
2426 
2427 		/* Send pending management frames first. */
2428 		IF_DEQUEUE(&ic->ic_mgtq, m);
2429 		if (m != NULL) {
2430 			ni = M_GETCTX(m, struct ieee80211_node *);
2431 			M_CLEARCTX(m);
2432 			goto sendit;
2433 		}
2434 		if (ic->ic_state != IEEE80211_S_RUN)
2435 			break;
2436 
2437 		/* Encapsulate and send data frames. */
2438 		IFQ_DEQUEUE(&ifp->if_snd, m);
2439 		if (m == NULL)
2440 			break;
2441 
2442 		if (m->m_len < (int)sizeof(*eh) &&
2443 		    (m = m_pullup(m, sizeof(*eh))) == NULL) {
2444 			ifp->if_oerrors++;
2445 			continue;
2446 		}
2447 		eh = mtod(m, struct ether_header *);
2448 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2449 		if (ni == NULL) {
2450 			m_freem(m);
2451 			ifp->if_oerrors++;
2452 			continue;
2453 		}
2454 
2455 		bpf_mtap(ifp, m);
2456 
2457 		if ((m = ieee80211_encap(ic, m, ni)) == NULL) {
2458 			ieee80211_free_node(ni);
2459 			ifp->if_oerrors++;
2460 			continue;
2461 		}
2462  sendit:
2463 		bpf_mtap3(ic->ic_rawbpf, m);
2464 
2465 		if (athn_usb_tx(sc, m, ni, data) != 0) {
2466 			m_freem(m);
2467 			ieee80211_free_node(ni);
2468 			ifp->if_oerrors++;
2469 			continue;
2470 		}
2471 		data = NULL;
2472 		m_freem(m);
2473 		ieee80211_free_node(ni);
2474 		sc->sc_tx_timer = 5;
2475 		ifp->if_timer = 1;
2476 	}
2477 
2478 	/* Return the Tx buffer to the free list */
2479 	mutex_enter(&usc->usc_tx_mtx);
2480 	TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
2481 	mutex_exit(&usc->usc_tx_mtx);
2482 }
2483 
2484 Static void
2485 athn_usb_watchdog(struct ifnet *ifp)
2486 {
2487 	struct athn_softc *sc = ifp->if_softc;
2488 
2489 	DPRINTFN(DBG_FN, sc, "\n");
2490 
2491 	ifp->if_timer = 0;
2492 
2493 	if (sc->sc_tx_timer > 0) {
2494 		if (--sc->sc_tx_timer == 0) {
2495 			aprint_error_dev(sc->sc_dev, "device timeout\n");
2496 			/* athn_usb_init(ifp); XXX needs a process context! */
2497 			ifp->if_oerrors++;
2498 			return;
2499 		}
2500 		ifp->if_timer = 1;
2501 	}
2502 	ieee80211_watchdog(&sc->sc_ic);
2503 }
2504 
2505 Static int
2506 athn_usb_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2507 {
2508 	struct athn_softc *sc = ifp->if_softc;
2509 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2510 	struct ieee80211com *ic = &sc->sc_ic;
2511 	int s, error = 0;
2512 
2513 	if (usc->usc_dying)
2514 		return EIO;
2515 
2516 	DPRINTFN(DBG_FN, sc, "cmd=0x%08lx\n", cmd);
2517 
2518 	s = splnet();
2519 
2520 	switch (cmd) {
2521 	case SIOCSIFFLAGS:
2522 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2523 			break;
2524 
2525 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
2526 		case IFF_UP | IFF_RUNNING:
2527 			break;
2528 		case IFF_UP:
2529 			error = athn_usb_init(ifp);
2530 			break;
2531 		case IFF_RUNNING:
2532 			athn_usb_stop(ifp);
2533 			break;
2534 		case 0:
2535 		default:
2536 			break;
2537 		}
2538 		break;
2539 
2540 	case SIOCADDMULTI:
2541 	case SIOCDELMULTI:
2542 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
2543 			/* setup multicast filter, etc */
2544 			error = 0;
2545 		}
2546 		break;
2547 
2548 	case SIOCS80211CHANNEL:
2549 		error = ieee80211_ioctl(ic, cmd, data);
2550 		if (error == ENETRESET &&
2551 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
2552 			if (IS_UP_AND_RUNNING(ifp))
2553 				athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
2554 			error = 0;
2555 		}
2556 		break;
2557 
2558 	default:
2559 		error = ieee80211_ioctl(ic, cmd, data);
2560 		break;
2561 	}
2562 	if (error == ENETRESET) {
2563 		error = 0;
2564 		if (IS_UP_AND_RUNNING(ifp) &&
2565 		    ic->ic_roaming != IEEE80211_ROAMING_MANUAL) {
2566 			athn_usb_stop(ifp);
2567 			error = athn_usb_init(ifp);
2568 		}
2569 	}
2570 	splx(s);
2571 	return error;
2572 }
2573 
2574 Static int
2575 athn_usb_init(struct ifnet *ifp)
2576 {
2577 	struct athn_softc *sc = ifp->if_softc;
2578 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2579 	struct athn_ops *ops = &sc->sc_ops;
2580 	struct ieee80211com *ic = &sc->sc_ic;
2581 	struct ieee80211_channel *curchan, *extchan;
2582 	struct athn_usb_rx_data *data;
2583 	struct ar_htc_target_vif hvif;
2584 	struct ar_htc_target_sta sta;
2585 	struct ar_htc_cap_target hic;
2586 	uint16_t mode;
2587 	size_t i;
2588 	int error;
2589 
2590 	if (usc->usc_dying)
2591 		return USBD_CANCELLED;
2592 
2593 	DPRINTFN(DBG_FN, sc, "\n");
2594 
2595 	/* Init host async commands ring. */
2596 	mutex_spin_enter(&usc->usc_task_mtx);
2597 	usc->usc_cmdq.cur = usc->usc_cmdq.next = usc->usc_cmdq.queued = 0;
2598 	mutex_spin_exit(&usc->usc_task_mtx);
2599 
2600 	/* Steal one buffer for beacons. */
2601 	mutex_enter(&usc->usc_tx_mtx);
2602 	usc->usc_tx_bcn = TAILQ_FIRST(&usc->usc_tx_free_list);
2603 	TAILQ_REMOVE(&usc->usc_tx_free_list, usc->usc_tx_bcn, next);
2604 	mutex_exit(&usc->usc_tx_mtx);
2605 
2606 	curchan = ic->ic_curchan;
2607 	extchan = NULL;
2608 
2609 	/* In case a new MAC address has been configured. */
2610 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2611 
2612 	error = athn_set_power_awake(sc);
2613 	if (error != 0)
2614 		goto fail;
2615 
2616 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_FLUSH_RECV);
2617 	if (error != 0)
2618 		goto fail;
2619 
2620 	error = athn_hw_reset(sc, curchan, extchan, 1);
2621 	if (error != 0)
2622 		goto fail;
2623 
2624 	ops->set_txpower(sc, curchan, extchan);
2625 
2626 	mode = htobe16(IEEE80211_IS_CHAN_2GHZ(curchan) ?
2627 	    AR_HTC_MODE_11NG : AR_HTC_MODE_11NA);
2628 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE,
2629 	    &mode, sizeof(mode), NULL);
2630 	if (error != 0)
2631 		goto fail;
2632 
2633 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ATH_INIT);
2634 	if (error != 0)
2635 		goto fail;
2636 
2637 	error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV);
2638 	if (error != 0)
2639 		goto fail;
2640 
2641 	athn_rx_start(sc);
2642 
2643 	/* Create main interface on target. */
2644 	memset(&hvif, 0, sizeof(hvif));
2645 	hvif.index = 0;
2646 	IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr);
2647 	switch (ic->ic_opmode) {
2648 	case IEEE80211_M_STA:
2649 		hvif.opmode = htobe32(AR_HTC_M_STA);
2650 		break;
2651 	case IEEE80211_M_MONITOR:
2652 		hvif.opmode = htobe32(AR_HTC_M_MONITOR);
2653 		break;
2654 #ifndef IEEE80211_STA_ONLY
2655 	case IEEE80211_M_IBSS:
2656 		hvif.opmode = htobe32(AR_HTC_M_IBSS);
2657 		break;
2658 	case IEEE80211_M_AHDEMO:
2659 		hvif.opmode = htobe32(AR_HTC_M_AHDEMO);
2660 		break;
2661 	case IEEE80211_M_HOSTAP:
2662 		hvif.opmode = htobe32(AR_HTC_M_HOSTAP);
2663 		break;
2664 #endif
2665 	}
2666 	hvif.rtsthreshold = htobe16(ic->ic_rtsthreshold);
2667 	DPRINTFN(DBG_INIT, sc, "creating VAP\n");
2668 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_CREATE,
2669 	    &hvif, sizeof(hvif), NULL);
2670 	if (error != 0)
2671 		goto fail;
2672 
2673 	/* Create a fake node to send management frames before assoc. */
2674 	memset(&sta, 0, sizeof(sta));
2675 	IEEE80211_ADDR_COPY(sta.macaddr, ic->ic_myaddr);
2676 	sta.sta_index = 0;
2677 	sta.is_vif_sta = 1;
2678 	sta.vif_index = hvif.index;
2679 	sta.maxampdu = 0xffff;
2680 
2681 	DPRINTFN(DBG_INIT | DBG_NODES, sc, "creating default node %u\n",
2682 	    sta.sta_index);
2683 	error = athn_usb_create_hw_node(usc, &sta);
2684 	if (error != 0)
2685 		goto fail;
2686 
2687 	/* Update target capabilities. */
2688 	memset(&hic, 0, sizeof(hic));
2689 	hic.flags = htobe32(0x400c2400);
2690 	hic.flags_ext = htobe32(0x00106080);
2691 	hic.ampdu_limit = htobe32(0x0000ffff);
2692 	hic.ampdu_subframes = 20;
2693 	hic.protmode = 1;	/* XXX */
2694 	hic.lg_txchainmask = sc->sc_txchainmask;
2695 	hic.ht_txchainmask = sc->sc_txchainmask;
2696 	DPRINTFN(DBG_INIT, sc, "updating target configuration\n");
2697 	error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TARGET_IC_UPDATE,
2698 	    &hic, sizeof(hic), NULL);
2699 	if (error != 0)
2700 		goto fail;
2701 
2702 	/* Queue Rx xfers. */
2703 	for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
2704 		data = &usc->usc_rx_data[i];
2705 
2706 		usbd_setup_xfer(data->xfer, data, data->buf,
2707 		    ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK,
2708 		    USBD_NO_TIMEOUT, athn_usb_rxeof);
2709 		error = usbd_transfer(data->xfer);
2710 		if (error != 0 && error != USBD_IN_PROGRESS)
2711 			goto fail;
2712 	}
2713 	/* We're ready to go. */
2714 	ifp->if_flags &= ~IFF_OACTIVE;
2715 	ifp->if_flags |= IFF_RUNNING;
2716 
2717 #ifdef notyet
2718 	if (ic->ic_flags & IEEE80211_F_WEPON) {
2719 		/* Install WEP keys. */
2720 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
2721 			athn_usb_set_key(ic, NULL, &ic->ic_nw_keys[i]);
2722 	}
2723 #endif
2724 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2725 		ic->ic_max_aid = AR_USB_MAX_STA;  /* Firmware is limited to 8 STA */
2726 	else
2727 		ic->ic_max_aid = sc->sc_max_aid;
2728 
2729 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2730 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2731 	else
2732 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2733 	athn_usb_wait_async(usc);
2734 	return 0;
2735  fail:
2736 	athn_usb_stop(ifp);
2737 	return error;
2738 }
2739 
2740 Static void
2741 athn_usb_stop(struct ifnet *ifp)
2742 {
2743 	struct athn_softc *sc = ifp->if_softc;
2744 	struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2745 	struct ieee80211com *ic = &sc->sc_ic;
2746 	struct ar_htc_target_vif hvif;
2747 	struct mbuf *m;
2748 	uint8_t sta_index;
2749 	int s;
2750 
2751 	DPRINTFN(DBG_FN, sc, "\n");
2752 
2753 	s = splusb();
2754 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2755 	athn_usb_wait_async(usc);
2756 	splx(s);
2757 
2758 	sc->sc_tx_timer = 0;
2759 	ifp->if_timer = 0;
2760 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2761 
2762 	callout_stop(&sc->sc_scan_to);
2763 	callout_stop(&sc->sc_calib_to);
2764 
2765 	/* Abort Tx/Rx. */
2766 	usbd_abort_pipe(usc->usc_tx_data_pipe);
2767 	usbd_abort_pipe(usc->usc_rx_data_pipe);
2768 
2769 	/* Free Tx/Rx buffers. */
2770 	athn_usb_free_tx_list(usc);
2771 	athn_usb_free_rx_list(usc);
2772 
2773 	/* Flush Rx stream. */
2774 	CTASSERT(sizeof(m) == sizeof(void *));
2775 	m = atomic_swap_ptr(&usc->usc_rx_stream.m, NULL);
2776 	m_freem(m);
2777 	usc->usc_rx_stream.left = 0;
2778 
2779 	/* Remove main interface. */
2780 	memset(&hvif, 0, sizeof(hvif));
2781 	hvif.index = 0;
2782 	IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr);
2783 	(void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_REMOVE,
2784 	    &hvif, sizeof(hvif), NULL);
2785 
2786 	/* Remove default node. */
2787 	sta_index = 0;
2788 	DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
2789 	(void)athn_usb_remove_hw_node(usc, &sta_index);
2790 
2791 	(void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
2792 	(void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL);
2793 	(void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV);
2794 
2795 	athn_reset(sc, 0);
2796 	athn_init_pll(sc, NULL);
2797 	athn_set_power_awake(sc);
2798 	athn_reset(sc, 1);
2799 	athn_init_pll(sc, NULL);
2800 	athn_set_power_sleep(sc);
2801 }
2802 
2803 MODULE(MODULE_CLASS_DRIVER, if_athn_usb, "bpf");
2804 
2805 #ifdef _MODULE
2806 #include "ioconf.c"
2807 #endif
2808 
2809 static int
2810 if_athn_usb_modcmd(modcmd_t cmd, void *aux)
2811 {
2812 	int error = 0;
2813 
2814 	switch (cmd) {
2815 	case MODULE_CMD_INIT:
2816 #ifdef _MODULE
2817 		error = config_init_component(cfdriver_ioconf_if_athn_usb,
2818 		    cfattach_ioconf_if_athn_usb, cfdata_ioconf_if_athn_usb);
2819 #endif
2820 		return error;
2821 	case MODULE_CMD_FINI:
2822 #ifdef _MODULE
2823 		error = config_fini_component(cfdriver_ioconf_if_athn_usb,
2824 		    cfattach_ioconf_if_athn_usb, cfdata_ioconf_if_athn_usb);
2825 #endif
2826 		return error;
2827 	default:
2828 		return ENOTTY;
2829 	}
2830 }
2831