if_uath.c revision 195916
1190688Sweongyo/*-
2190688Sweongyo * Copyright (c) 2006 Sam Leffler, Errno Consulting
3190688Sweongyo * Copyright (c) 2008-2009 Weongyo Jeong <weongyo@freebsd.org>
4190688Sweongyo * All rights reserved.
5190688Sweongyo *
6190688Sweongyo * Redistribution and use in source and binary forms, with or without
7190688Sweongyo * modification, are permitted provided that the following conditions
8190688Sweongyo * are met:
9190688Sweongyo * 1. Redistributions of source code must retain the above copyright
10190688Sweongyo *    notice, this list of conditions and the following disclaimer,
11190688Sweongyo *    without modification.
12190688Sweongyo * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13190688Sweongyo *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14190688Sweongyo *    redistribution must be conditioned upon including a substantially
15190688Sweongyo *    similar Disclaimer requirement for further binary redistribution.
16190688Sweongyo *
17190688Sweongyo * NO WARRANTY
18190688Sweongyo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19190688Sweongyo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20190688Sweongyo * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21190688Sweongyo * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22190688Sweongyo * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23190688Sweongyo * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24190688Sweongyo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25190688Sweongyo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26190688Sweongyo * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27190688Sweongyo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28190688Sweongyo * THE POSSIBILITY OF SUCH DAMAGES.
29190688Sweongyo */
30190688Sweongyo
31190688Sweongyo/*
32190688Sweongyo * This driver is distantly derived from a driver of the same name
33190688Sweongyo * by Damien Bergamini.  The original copyright is included below:
34190688Sweongyo *
35190688Sweongyo * Copyright (c) 2006
36190688Sweongyo *	Damien Bergamini <damien.bergamini@free.fr>
37190688Sweongyo *
38190688Sweongyo * Permission to use, copy, modify, and distribute this software for any
39190688Sweongyo * purpose with or without fee is hereby granted, provided that the above
40190688Sweongyo * copyright notice and this permission notice appear in all copies.
41190688Sweongyo *
42190688Sweongyo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
43190688Sweongyo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44190688Sweongyo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45190688Sweongyo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46190688Sweongyo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47190688Sweongyo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
48190688Sweongyo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49190688Sweongyo */
50190688Sweongyo
51190688Sweongyo#include <sys/cdefs.h>
52190688Sweongyo__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_uath.c 195916 2009-07-27 20:17:20Z weongyo $");
53190688Sweongyo
54190688Sweongyo/*-
55190688Sweongyo * Driver for Atheros AR5523 USB parts.
56190688Sweongyo *
57190688Sweongyo * The driver requires firmware to be loaded into the device.  This
58190688Sweongyo * is done on device discovery from a user application (uathload)
59190688Sweongyo * that is launched by devd when a device with suitable product ID
60190688Sweongyo * is recognized.  Once firmware has been loaded the device will
61190688Sweongyo * reset the USB port and re-attach with the original product ID+1
62190688Sweongyo * and this driver will be attached.  The firmware is licensed for
63190688Sweongyo * general use (royalty free) and may be incorporated in products.
64190688Sweongyo * Note that the firmware normally packaged with the NDIS drivers
65190688Sweongyo * for these devices does not work in this way and so does not work
66190688Sweongyo * with this driver.
67190688Sweongyo */
68190688Sweongyo#include <sys/param.h>
69190688Sweongyo#include <sys/sockio.h>
70190688Sweongyo#include <sys/sysctl.h>
71190688Sweongyo#include <sys/lock.h>
72190688Sweongyo#include <sys/mutex.h>
73190688Sweongyo#include <sys/mbuf.h>
74190688Sweongyo#include <sys/kernel.h>
75190688Sweongyo#include <sys/socket.h>
76190688Sweongyo#include <sys/systm.h>
77190688Sweongyo#include <sys/malloc.h>
78190688Sweongyo#include <sys/module.h>
79190688Sweongyo#include <sys/bus.h>
80190688Sweongyo#include <sys/endian.h>
81190688Sweongyo#include <sys/kdb.h>
82190688Sweongyo
83190688Sweongyo#include <machine/bus.h>
84190688Sweongyo#include <machine/resource.h>
85190688Sweongyo#include <sys/rman.h>
86190688Sweongyo
87190688Sweongyo#include <net/bpf.h>
88190688Sweongyo#include <net/if.h>
89190688Sweongyo#include <net/if_arp.h>
90190688Sweongyo#include <net/ethernet.h>
91190688Sweongyo#include <net/if_dl.h>
92190688Sweongyo#include <net/if_media.h>
93190688Sweongyo#include <net/if_types.h>
94190688Sweongyo
95190688Sweongyo#ifdef INET
96190688Sweongyo#include <netinet/in.h>
97190688Sweongyo#include <netinet/in_systm.h>
98190688Sweongyo#include <netinet/in_var.h>
99190688Sweongyo#include <netinet/if_ether.h>
100190688Sweongyo#include <netinet/ip.h>
101190688Sweongyo#endif
102190688Sweongyo
103190688Sweongyo#include <net80211/ieee80211_var.h>
104190688Sweongyo#include <net80211/ieee80211_regdomain.h>
105190688Sweongyo#include <net80211/ieee80211_radiotap.h>
106190688Sweongyo
107190688Sweongyo#include <dev/usb/usb.h>
108194677Sthompsa#include <dev/usb/usbdi.h>
109190688Sweongyo#include "usbdevs.h"
110190688Sweongyo
111190688Sweongyo#include <dev/usb/wlan/if_uathreg.h>
112190688Sweongyo#include <dev/usb/wlan/if_uathvar.h>
113190688Sweongyo
114192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, uath, CTLFLAG_RW, 0, "USB Atheros");
115190688Sweongyo
116190688Sweongyostatic	int uath_countrycode = CTRY_DEFAULT;	/* country code */
117192502SthompsaSYSCTL_INT(_hw_usb_uath, OID_AUTO, countrycode, CTLFLAG_RW, &uath_countrycode,
118190688Sweongyo    0, "country code");
119192502SthompsaTUNABLE_INT("hw.usb.uath.countrycode", &uath_countrycode);
120190688Sweongyostatic	int uath_regdomain = 0;			/* regulatory domain */
121192502SthompsaSYSCTL_INT(_hw_usb_uath, OID_AUTO, regdomain, CTLFLAG_RD, &uath_regdomain,
122190688Sweongyo    0, "regulatory domain");
123190688Sweongyo
124190688Sweongyo#ifdef UATH_DEBUG
125190688Sweongyoint uath_debug = 0;
126192502SthompsaSYSCTL_INT(_hw_usb_uath, OID_AUTO, debug, CTLFLAG_RW, &uath_debug, 0,
127190688Sweongyo    "uath debug level");
128190688SweongyoTUNABLE_INT("hw.usb.uath.debug", &uath_debug);
129190688Sweongyoenum {
130190688Sweongyo	UATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
131190688Sweongyo	UATH_DEBUG_XMIT_DUMP	= 0x00000002,	/* xmit dump */
132190688Sweongyo	UATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
133190688Sweongyo	UATH_DEBUG_TX_PROC	= 0x00000008,	/* tx ISR proc */
134190688Sweongyo	UATH_DEBUG_RX_PROC	= 0x00000010,	/* rx ISR proc */
135190688Sweongyo	UATH_DEBUG_RECV_ALL	= 0x00000020,	/* trace all frames (beacons) */
136190688Sweongyo	UATH_DEBUG_INIT		= 0x00000040,	/* initialization of dev */
137190688Sweongyo	UATH_DEBUG_DEVCAP	= 0x00000080,	/* dev caps */
138190688Sweongyo	UATH_DEBUG_CMDS		= 0x00000100,	/* commands */
139190688Sweongyo	UATH_DEBUG_CMDS_DUMP	= 0x00000200,	/* command buffer dump */
140190688Sweongyo	UATH_DEBUG_RESET	= 0x00000400,	/* reset processing */
141190688Sweongyo	UATH_DEBUG_STATE	= 0x00000800,	/* 802.11 state transitions */
142190688Sweongyo	UATH_DEBUG_MULTICAST	= 0x00001000,	/* multicast */
143190688Sweongyo	UATH_DEBUG_WME		= 0x00002000,	/* WME */
144190688Sweongyo	UATH_DEBUG_CHANNEL	= 0x00004000,	/* channel */
145190688Sweongyo	UATH_DEBUG_RATES	= 0x00008000,	/* rates */
146190688Sweongyo	UATH_DEBUG_CRYPTO	= 0x00010000,	/* crypto */
147190688Sweongyo	UATH_DEBUG_LED		= 0x00020000,	/* LED */
148190688Sweongyo	UATH_DEBUG_ANY		= 0xffffffff
149190688Sweongyo};
150190688Sweongyo#define	DPRINTF(sc, m, fmt, ...) do {				\
151190688Sweongyo	if (sc->sc_debug & (m))					\
152190688Sweongyo		printf(fmt, __VA_ARGS__);			\
153190688Sweongyo} while (0)
154190688Sweongyo#else
155190688Sweongyo#define	DPRINTF(sc, m, fmt, ...) do {				\
156190688Sweongyo	(void) sc;						\
157190688Sweongyo} while (0)
158190688Sweongyo#endif
159190688Sweongyo
160190688Sweongyo/* unaligned little endian access */
161190688Sweongyo#define LE_READ_2(p)							\
162190688Sweongyo	((u_int16_t)							\
163190688Sweongyo	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
164190688Sweongyo#define LE_READ_4(p)							\
165190688Sweongyo	((u_int32_t)							\
166190688Sweongyo	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
167190688Sweongyo	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
168190688Sweongyo
169190688Sweongyo/* recognized device vendors/products */
170192984Sthompsastatic const struct usb_device_id uath_devs[] = {
171190688Sweongyo#define	UATH_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
172193029Sweongyo	UATH_DEV(ACCTON,		SMCWUSBG),
173193029Sweongyo	UATH_DEV(ACCTON,		SMCWUSBTG2),
174190688Sweongyo	UATH_DEV(ATHEROS,		AR5523),
175190688Sweongyo	UATH_DEV(ATHEROS2,		AR5523_1),
176190688Sweongyo	UATH_DEV(ATHEROS2,		AR5523_2),
177190688Sweongyo	UATH_DEV(ATHEROS2,		AR5523_3),
178190688Sweongyo	UATH_DEV(CONCEPTRONIC,		AR5523_1),
179190688Sweongyo	UATH_DEV(CONCEPTRONIC,		AR5523_2),
180190688Sweongyo	UATH_DEV(DLINK,			DWLAG122),
181190688Sweongyo	UATH_DEV(DLINK,			DWLAG132),
182190688Sweongyo	UATH_DEV(DLINK,			DWLG132),
183195916Sweongyo	UATH_DEV(DLINK2,		DWA120),
184190688Sweongyo	UATH_DEV(GIGASET,		AR5523),
185190688Sweongyo	UATH_DEV(GIGASET,		SMCWUSBTG),
186190688Sweongyo	UATH_DEV(GLOBALSUN,		AR5523_1),
187190688Sweongyo	UATH_DEV(GLOBALSUN,		AR5523_2),
188190688Sweongyo	UATH_DEV(NETGEAR,		WG111U),
189190688Sweongyo	UATH_DEV(NETGEAR3,		WG111T),
190190688Sweongyo	UATH_DEV(NETGEAR3,		WPN111),
191190688Sweongyo	UATH_DEV(UMEDIA,		TEW444UBEU),
192190688Sweongyo	UATH_DEV(UMEDIA,		AR5523_2),
193192258Ssam	UATH_DEV(UMEDIA,		AR5523_3),
194190688Sweongyo	UATH_DEV(WISTRONNEWEB,		AR5523_1),
195190688Sweongyo	UATH_DEV(WISTRONNEWEB,		AR5523_2),
196190688Sweongyo	UATH_DEV(ZCOM,			AR5523)
197190688Sweongyo#undef UATH_DEV
198190688Sweongyo};
199190688Sweongyo
200193045Sthompsastatic usb_callback_t uath_intr_rx_callback;
201193045Sthompsastatic usb_callback_t uath_intr_tx_callback;
202193045Sthompsastatic usb_callback_t uath_bulk_rx_callback;
203193045Sthompsastatic usb_callback_t uath_bulk_tx_callback;
204190688Sweongyo
205192984Sthompsastatic const struct usb_config uath_usbconfig[UATH_N_XFERS] = {
206190688Sweongyo	[UATH_INTR_RX] = {
207190688Sweongyo		.type = UE_BULK,
208190688Sweongyo		.endpoint = 0x1,
209190688Sweongyo		.direction = UE_DIR_IN,
210190744Sthompsa		.bufsize = UATH_MAX_CMDSZ,
211190744Sthompsa		.flags = {
212190688Sweongyo			.pipe_bof = 1,
213190688Sweongyo			.short_xfer_ok = 1
214190688Sweongyo		},
215190744Sthompsa		.callback = uath_intr_rx_callback
216190688Sweongyo	},
217190688Sweongyo	[UATH_INTR_TX] = {
218190688Sweongyo		.type = UE_BULK,
219190688Sweongyo		.endpoint = 0x1,
220190688Sweongyo		.direction = UE_DIR_OUT,
221190744Sthompsa		.bufsize = UATH_MAX_CMDSZ,
222190744Sthompsa		.flags = {
223190688Sweongyo			.ext_buffer = 1,
224190688Sweongyo			.force_short_xfer = 1,
225190688Sweongyo			.pipe_bof = 1,
226190688Sweongyo		},
227190744Sthompsa		.callback = uath_intr_tx_callback,
228190744Sthompsa		.timeout = UATH_CMD_TIMEOUT
229190688Sweongyo	},
230190688Sweongyo	[UATH_BULK_RX] = {
231190688Sweongyo		.type = UE_BULK,
232190688Sweongyo		.endpoint = 0x2,
233190688Sweongyo		.direction = UE_DIR_IN,
234190744Sthompsa		.bufsize = MCLBYTES,
235190744Sthompsa		.flags = {
236190688Sweongyo			.ext_buffer = 1,
237190688Sweongyo			.pipe_bof = 1,
238190688Sweongyo			.short_xfer_ok = 1
239190688Sweongyo		},
240190744Sthompsa		.callback = uath_bulk_rx_callback
241190688Sweongyo	},
242190688Sweongyo	[UATH_BULK_TX] = {
243190688Sweongyo		.type = UE_BULK,
244190688Sweongyo		.endpoint = 0x2,
245190688Sweongyo		.direction = UE_DIR_OUT,
246190744Sthompsa		.bufsize = UATH_MAX_TXBUFSZ,
247190744Sthompsa		.flags = {
248190688Sweongyo			.ext_buffer = 1,
249190688Sweongyo			.force_short_xfer = 1,
250190688Sweongyo			.pipe_bof = 1
251190688Sweongyo		},
252190744Sthompsa		.callback = uath_bulk_tx_callback,
253190744Sthompsa		.timeout = UATH_DATA_TIMEOUT
254190688Sweongyo	}
255190688Sweongyo};
256190688Sweongyo
257190688Sweongyostatic struct ieee80211vap *uath_vap_create(struct ieee80211com *,
258190688Sweongyo		    const char name[IFNAMSIZ], int unit, int opmode,
259190688Sweongyo		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
260190688Sweongyo		    const uint8_t mac[IEEE80211_ADDR_LEN]);
261190688Sweongyostatic void	uath_vap_delete(struct ieee80211vap *);
262190688Sweongyostatic int	uath_alloc_cmd_list(struct uath_softc *, struct uath_cmd [],
263190688Sweongyo		    int, int);
264190688Sweongyostatic void	uath_free_cmd_list(struct uath_softc *, struct uath_cmd [],
265190688Sweongyo		    int);
266190688Sweongyostatic int	uath_host_available(struct uath_softc *);
267190688Sweongyostatic int	uath_get_capability(struct uath_softc *, uint32_t, uint32_t *);
268190688Sweongyostatic int	uath_get_devcap(struct uath_softc *);
269190688Sweongyostatic struct uath_cmd *
270190688Sweongyo		uath_get_cmdbuf(struct uath_softc *);
271190688Sweongyostatic int	uath_cmd_read(struct uath_softc *, uint32_t, const void *,
272190688Sweongyo		    int, void *, int, int);
273190688Sweongyostatic int	uath_cmd_write(struct uath_softc *, uint32_t, const void *,
274190688Sweongyo		    int, int);
275190688Sweongyostatic void	uath_stat(void *);
276190688Sweongyo#ifdef UATH_DEBUG
277190688Sweongyostatic void	uath_dump_cmd(const uint8_t *, int, char);
278190688Sweongyostatic const char *
279190688Sweongyo		uath_codename(int);
280190688Sweongyo#endif
281190688Sweongyostatic int	uath_get_devstatus(struct uath_softc *,
282190688Sweongyo		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
283190688Sweongyostatic int	uath_get_status(struct uath_softc *, uint32_t, void *, int);
284190688Sweongyostatic int	uath_alloc_rx_data_list(struct uath_softc *);
285190688Sweongyostatic int	uath_alloc_tx_data_list(struct uath_softc *);
286190688Sweongyostatic void	uath_free_rx_data_list(struct uath_softc *);
287190688Sweongyostatic void	uath_free_tx_data_list(struct uath_softc *);
288190688Sweongyostatic int	uath_init_locked(void *);
289190688Sweongyostatic void	uath_init(void *);
290190688Sweongyostatic void	uath_stop_locked(struct ifnet *);
291190688Sweongyostatic void	uath_stop(struct ifnet *);
292190688Sweongyostatic int	uath_ioctl(struct ifnet *, u_long, caddr_t);
293190688Sweongyostatic void	uath_start(struct ifnet *);
294190688Sweongyostatic int	uath_raw_xmit(struct ieee80211_node *, struct mbuf *,
295190688Sweongyo		    const struct ieee80211_bpf_params *);
296190688Sweongyostatic void	uath_scan_start(struct ieee80211com *);
297190688Sweongyostatic void	uath_scan_end(struct ieee80211com *);
298190688Sweongyostatic void	uath_set_channel(struct ieee80211com *);
299190688Sweongyostatic void	uath_update_mcast(struct ifnet *);
300190688Sweongyostatic void	uath_update_promisc(struct ifnet *);
301190688Sweongyostatic int	uath_config(struct uath_softc *, uint32_t, uint32_t);
302190688Sweongyostatic int	uath_config_multi(struct uath_softc *, uint32_t, const void *,
303190688Sweongyo		    int);
304190688Sweongyostatic int	uath_switch_channel(struct uath_softc *,
305190688Sweongyo		    struct ieee80211_channel *);
306190688Sweongyostatic int	uath_set_rxfilter(struct uath_softc *, uint32_t, uint32_t);
307190688Sweongyostatic void	uath_watchdog(void *);
308190688Sweongyostatic void	uath_abort_xfers(struct uath_softc *);
309190688Sweongyostatic int	uath_dataflush(struct uath_softc *);
310190688Sweongyostatic int	uath_cmdflush(struct uath_softc *);
311190688Sweongyostatic int	uath_flush(struct uath_softc *);
312190688Sweongyostatic int	uath_set_ledstate(struct uath_softc *, int);
313190688Sweongyostatic int	uath_set_chan(struct uath_softc *, struct ieee80211_channel *);
314190688Sweongyostatic int	uath_reset_tx_queues(struct uath_softc *);
315190688Sweongyostatic int	uath_wme_init(struct uath_softc *);
316190688Sweongyostatic struct uath_data *
317190688Sweongyo		uath_getbuf(struct uath_softc *);
318190688Sweongyostatic int	uath_newstate(struct ieee80211vap *, enum ieee80211_state,
319190688Sweongyo		    int);
320190688Sweongyostatic int	uath_set_key(struct uath_softc *,
321190688Sweongyo		    const struct ieee80211_key *, int);
322190688Sweongyostatic int	uath_set_keys(struct uath_softc *, struct ieee80211vap *);
323190688Sweongyostatic void	uath_sysctl_node(struct uath_softc *);
324190688Sweongyo
325190688Sweongyostatic int
326190688Sweongyouath_match(device_t dev)
327190688Sweongyo{
328192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
329190688Sweongyo
330192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
331190688Sweongyo		return (ENXIO);
332190688Sweongyo	if (uaa->info.bConfigIndex != UATH_CONFIG_INDEX)
333190688Sweongyo		return (ENXIO);
334190688Sweongyo	if (uaa->info.bIfaceIndex != UATH_IFACE_INDEX)
335190688Sweongyo		return (ENXIO);
336190688Sweongyo
337194228Sthompsa	return (usbd_lookup_id_by_uaa(uath_devs, sizeof(uath_devs), uaa));
338190688Sweongyo}
339190688Sweongyo
340190688Sweongyostatic int
341190688Sweongyouath_attach(device_t dev)
342190688Sweongyo{
343190688Sweongyo	struct uath_softc *sc = device_get_softc(dev);
344192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
345190688Sweongyo	struct ieee80211com *ic;
346190688Sweongyo	struct ifnet *ifp;
347190688Sweongyo	uint8_t bands, iface_index = UATH_IFACE_INDEX;		/* XXX */
348193045Sthompsa	usb_error_t error;
349190688Sweongyo	uint8_t macaddr[IEEE80211_ADDR_LEN];
350190688Sweongyo
351190688Sweongyo	sc->sc_dev = dev;
352190688Sweongyo	sc->sc_udev = uaa->device;
353190688Sweongyo#ifdef UATH_DEBUG
354190688Sweongyo	sc->sc_debug = uath_debug;
355190688Sweongyo#endif
356194228Sthompsa	device_set_usb_desc(dev);
357190688Sweongyo
358190688Sweongyo	/*
359190688Sweongyo	 * Only post-firmware devices here.
360190688Sweongyo	 */
361190688Sweongyo	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
362190688Sweongyo	    MTX_DEF);
363190688Sweongyo	callout_init(&sc->stat_ch, 0);
364190688Sweongyo	callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
365190688Sweongyo
366190688Sweongyo	/*
367190688Sweongyo	 * Allocate xfers for firmware commands.
368190688Sweongyo	 */
369190688Sweongyo	error = uath_alloc_cmd_list(sc, sc->sc_cmd, UATH_CMD_LIST_COUNT,
370190688Sweongyo	    UATH_MAX_CMDSZ);
371190688Sweongyo	if (error != 0) {
372190688Sweongyo		device_printf(sc->sc_dev,
373190688Sweongyo		    "could not allocate Tx command list\n");
374190688Sweongyo		goto fail;
375190688Sweongyo	}
376190688Sweongyo
377194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
378190688Sweongyo	    uath_usbconfig, UATH_N_XFERS, sc, &sc->sc_mtx);
379190688Sweongyo	if (error) {
380190688Sweongyo		device_printf(dev, "could not allocate USB transfers, "
381194228Sthompsa		    "err=%s\n", usbd_errstr(error));
382190688Sweongyo		goto fail1;
383190688Sweongyo	}
384190688Sweongyo
385190688Sweongyo	/*
386190688Sweongyo	 * We're now ready to send+receive firmware commands.
387190688Sweongyo	 */
388190688Sweongyo	UATH_LOCK(sc);
389190688Sweongyo	error = uath_host_available(sc);
390190688Sweongyo	if (error != 0) {
391190688Sweongyo		device_printf(sc->sc_dev, "could not initialize adapter\n");
392190688Sweongyo		goto fail3;
393190688Sweongyo	}
394190688Sweongyo	error = uath_get_devcap(sc);
395190688Sweongyo	if (error != 0) {
396190688Sweongyo		device_printf(sc->sc_dev,
397190688Sweongyo		    "could not get device capabilities\n");
398190688Sweongyo		goto fail3;
399190688Sweongyo	}
400190688Sweongyo	UATH_UNLOCK(sc);
401190688Sweongyo
402190688Sweongyo	/* Create device sysctl node. */
403190688Sweongyo	uath_sysctl_node(sc);
404190688Sweongyo
405190688Sweongyo	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
406190688Sweongyo	if (ifp == NULL) {
407190688Sweongyo		device_printf(sc->sc_dev, "can not allocate ifnet\n");
408190688Sweongyo		error = ENXIO;
409190688Sweongyo		goto fail2;
410190688Sweongyo	}
411190688Sweongyo
412190688Sweongyo	UATH_LOCK(sc);
413190688Sweongyo	error = uath_get_devstatus(sc, macaddr);
414190688Sweongyo	if (error != 0) {
415190688Sweongyo		device_printf(sc->sc_dev, "could not get device status\n");
416190688Sweongyo		goto fail4;
417190688Sweongyo	}
418190688Sweongyo
419190688Sweongyo	/*
420190688Sweongyo	 * Allocate xfers for Rx/Tx data pipes.
421190688Sweongyo	 */
422190688Sweongyo	error = uath_alloc_rx_data_list(sc);
423190688Sweongyo	if (error != 0) {
424190688Sweongyo		device_printf(sc->sc_dev, "could not allocate Rx data list\n");
425190688Sweongyo		goto fail4;
426190688Sweongyo	}
427190688Sweongyo	error = uath_alloc_tx_data_list(sc);
428190688Sweongyo	if (error != 0) {
429190688Sweongyo		device_printf(sc->sc_dev, "could not allocate Tx data list\n");
430190688Sweongyo		goto fail4;
431190688Sweongyo	}
432190688Sweongyo	UATH_UNLOCK(sc);
433190688Sweongyo
434190688Sweongyo	ifp->if_softc = sc;
435190688Sweongyo	if_initname(ifp, "uath", device_get_unit(sc->sc_dev));
436190688Sweongyo	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
437190688Sweongyo	ifp->if_init = uath_init;
438190688Sweongyo	ifp->if_ioctl = uath_ioctl;
439190688Sweongyo	ifp->if_start = uath_start;
440190688Sweongyo	/* XXX UATH_TX_DATA_LIST_COUNT */
441190688Sweongyo	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
442190688Sweongyo	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
443190688Sweongyo	IFQ_SET_READY(&ifp->if_snd);
444190688Sweongyo
445190688Sweongyo	ic = ifp->if_l2com;
446190688Sweongyo	ic->ic_ifp = ifp;
447190688Sweongyo	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
448190688Sweongyo	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
449190688Sweongyo
450190688Sweongyo	/* set device capabilities */
451190688Sweongyo	ic->ic_caps =
452190688Sweongyo	    IEEE80211_C_STA |		/* station mode */
453190688Sweongyo	    IEEE80211_C_MONITOR |	/* monitor mode supported */
454190688Sweongyo	    IEEE80211_C_TXPMGT |	/* tx power management */
455190688Sweongyo	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
456190688Sweongyo	    IEEE80211_C_SHSLOT |	/* short slot time supported */
457190688Sweongyo	    IEEE80211_C_WPA |		/* 802.11i */
458190688Sweongyo	    IEEE80211_C_BGSCAN |	/* capable of bg scanning */
459190688Sweongyo	    IEEE80211_C_TXFRAG;		/* handle tx frags */
460190688Sweongyo
461190688Sweongyo	/* put a regulatory domain to reveal informations.  */
462190688Sweongyo	uath_regdomain = sc->sc_devcap.regDomain;
463190688Sweongyo
464190688Sweongyo	bands = 0;
465190688Sweongyo	setbit(&bands, IEEE80211_MODE_11B);
466190688Sweongyo	setbit(&bands, IEEE80211_MODE_11G);
467190688Sweongyo	if ((sc->sc_devcap.analog5GhzRevision & 0xf0) == 0x30)
468190688Sweongyo		setbit(&bands, IEEE80211_MODE_11A);
469190688Sweongyo	/* XXX turbo */
470190688Sweongyo	ieee80211_init_channels(ic, NULL, &bands);
471190688Sweongyo
472190688Sweongyo	ieee80211_ifattach(ic, macaddr);
473190688Sweongyo	ic->ic_raw_xmit = uath_raw_xmit;
474190688Sweongyo	ic->ic_scan_start = uath_scan_start;
475190688Sweongyo	ic->ic_scan_end = uath_scan_end;
476190688Sweongyo	ic->ic_set_channel = uath_set_channel;
477190688Sweongyo
478190688Sweongyo	ic->ic_vap_create = uath_vap_create;
479190688Sweongyo	ic->ic_vap_delete = uath_vap_delete;
480190688Sweongyo	ic->ic_update_mcast = uath_update_mcast;
481190688Sweongyo	ic->ic_update_promisc = uath_update_promisc;
482190688Sweongyo
483192468Ssam	ieee80211_radiotap_attach(ic,
484192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
485192468Ssam		UATH_TX_RADIOTAP_PRESENT,
486192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
487192468Ssam		UATH_RX_RADIOTAP_PRESENT);
488190688Sweongyo
489190688Sweongyo	if (bootverbose)
490190688Sweongyo		ieee80211_announce(ic);
491190688Sweongyo
492190688Sweongyo	return (0);
493190688Sweongyo
494190688Sweongyofail4:	if_free(ifp);
495190688Sweongyofail3:	UATH_UNLOCK(sc);
496194228Sthompsafail2:	usbd_transfer_unsetup(sc->sc_xfer, UATH_N_XFERS);
497190688Sweongyofail1:	uath_free_cmd_list(sc, sc->sc_cmd, UATH_CMD_LIST_COUNT);
498190688Sweongyofail:
499190688Sweongyo	return (error);
500190688Sweongyo}
501190688Sweongyo
502190688Sweongyostatic int
503190688Sweongyouath_detach(device_t dev)
504190688Sweongyo{
505190688Sweongyo	struct uath_softc *sc = device_get_softc(dev);
506190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
507190688Sweongyo	struct ieee80211com *ic = ifp->if_l2com;
508190688Sweongyo
509190688Sweongyo	if (!device_is_attached(dev))
510190688Sweongyo		return (0);
511190688Sweongyo
512194329Sweongyo	UATH_LOCK(sc);
513190688Sweongyo	sc->sc_flags |= UATH_FLAG_INVALID;
514194329Sweongyo	UATH_UNLOCK(sc);
515194329Sweongyo
516194329Sweongyo	ieee80211_ifdetach(ic);
517190688Sweongyo	uath_stop(ifp);
518190688Sweongyo
519190688Sweongyo	callout_drain(&sc->stat_ch);
520190688Sweongyo	callout_drain(&sc->watchdog_ch);
521190688Sweongyo
522194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, UATH_N_XFERS);
523190688Sweongyo
524190688Sweongyo	/* free buffers */
525190688Sweongyo	UATH_LOCK(sc);
526190688Sweongyo	uath_free_rx_data_list(sc);
527190688Sweongyo	uath_free_tx_data_list(sc);
528190688Sweongyo	uath_free_cmd_list(sc, sc->sc_cmd, UATH_CMD_LIST_COUNT);
529190688Sweongyo	UATH_UNLOCK(sc);
530190688Sweongyo
531190688Sweongyo	if_free(ifp);
532190688Sweongyo	mtx_destroy(&sc->sc_mtx);
533190688Sweongyo	return (0);
534190688Sweongyo}
535190688Sweongyo
536190688Sweongyostatic void
537190688Sweongyouath_free_cmd_list(struct uath_softc *sc, struct uath_cmd cmds[], int ncmd)
538190688Sweongyo{
539190688Sweongyo	int i;
540190688Sweongyo
541190688Sweongyo	for (i = 0; i < ncmd; i++)
542190688Sweongyo		if (cmds[i].buf != NULL)
543190688Sweongyo			free(cmds[i].buf, M_USBDEV);
544190688Sweongyo}
545190688Sweongyo
546190688Sweongyostatic int
547190688Sweongyouath_alloc_cmd_list(struct uath_softc *sc, struct uath_cmd cmds[],
548190688Sweongyo	int ncmd, int maxsz)
549190688Sweongyo{
550190688Sweongyo	int i, error;
551190688Sweongyo
552190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_active);
553190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_pending);
554190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_waiting);
555190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_inactive);
556190688Sweongyo
557190688Sweongyo	for (i = 0; i < ncmd; i++) {
558190688Sweongyo		struct uath_cmd *cmd = &cmds[i];
559190688Sweongyo
560190688Sweongyo		cmd->sc = sc;	/* backpointer for callbacks */
561190688Sweongyo		cmd->msgid = i;
562190688Sweongyo		cmd->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
563190688Sweongyo		if (cmd->buf == NULL) {
564190688Sweongyo			device_printf(sc->sc_dev,
565190688Sweongyo			    "could not allocate xfer buffer\n");
566190688Sweongyo			error = ENOMEM;
567190688Sweongyo			goto fail;
568190688Sweongyo		}
569190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_cmd_inactive, cmd, next);
570190688Sweongyo		UATH_STAT_INC(sc, st_cmd_inactive);
571190688Sweongyo	}
572190688Sweongyo	return (0);
573190688Sweongyo
574190688Sweongyofail:	uath_free_cmd_list(sc, cmds, ncmd);
575190688Sweongyo	return (error);
576190688Sweongyo}
577190688Sweongyo
578190688Sweongyostatic int
579190688Sweongyouath_host_available(struct uath_softc *sc)
580190688Sweongyo{
581190688Sweongyo	struct uath_cmd_host_available setup;
582190688Sweongyo
583190688Sweongyo	UATH_ASSERT_LOCKED(sc);
584190688Sweongyo
585190688Sweongyo	/* inform target the host is available */
586190688Sweongyo	setup.sw_ver_major = htobe32(ATH_SW_VER_MAJOR);
587190688Sweongyo	setup.sw_ver_minor = htobe32(ATH_SW_VER_MINOR);
588190688Sweongyo	setup.sw_ver_patch = htobe32(ATH_SW_VER_PATCH);
589190688Sweongyo	setup.sw_ver_build = htobe32(ATH_SW_VER_BUILD);
590190688Sweongyo	return uath_cmd_read(sc, WDCMSG_HOST_AVAILABLE,
591190688Sweongyo		&setup, sizeof setup, NULL, 0, 0);
592190688Sweongyo}
593190688Sweongyo
594190688Sweongyo#ifdef UATH_DEBUG
595190688Sweongyostatic void
596190688Sweongyouath_dump_cmd(const uint8_t *buf, int len, char prefix)
597190688Sweongyo{
598190688Sweongyo	const char *sep = "";
599190688Sweongyo	int i;
600190688Sweongyo
601190688Sweongyo	for (i = 0; i < len; i++) {
602190688Sweongyo		if ((i % 16) == 0) {
603190688Sweongyo			printf("%s%c ", sep, prefix);
604190688Sweongyo			sep = "\n";
605190688Sweongyo		}
606190688Sweongyo		else if ((i % 4) == 0)
607190688Sweongyo			printf(" ");
608190688Sweongyo		printf("%02x", buf[i]);
609190688Sweongyo	}
610190688Sweongyo	printf("\n");
611190688Sweongyo}
612190688Sweongyo
613190688Sweongyostatic const char *
614190688Sweongyouath_codename(int code)
615190688Sweongyo{
616190688Sweongyo#define	N(a)	(sizeof(a)/sizeof(a[0]))
617190688Sweongyo	static const char *names[] = {
618190688Sweongyo	    "0x00",
619190688Sweongyo	    "HOST_AVAILABLE",
620190688Sweongyo	    "BIND",
621190688Sweongyo	    "TARGET_RESET",
622190688Sweongyo	    "TARGET_GET_CAPABILITY",
623190688Sweongyo	    "TARGET_SET_CONFIG",
624190688Sweongyo	    "TARGET_GET_STATUS",
625190688Sweongyo	    "TARGET_GET_STATS",
626190688Sweongyo	    "TARGET_START",
627190688Sweongyo	    "TARGET_STOP",
628190688Sweongyo	    "TARGET_ENABLE",
629190688Sweongyo	    "TARGET_DISABLE",
630190688Sweongyo	    "CREATE_CONNECTION",
631190688Sweongyo	    "UPDATE_CONNECT_ATTR",
632190688Sweongyo	    "DELETE_CONNECT",
633190688Sweongyo	    "SEND",
634190688Sweongyo	    "FLUSH",
635190688Sweongyo	    "STATS_UPDATE",
636190688Sweongyo	    "BMISS",
637190688Sweongyo	    "DEVICE_AVAIL",
638190688Sweongyo	    "SEND_COMPLETE",
639190688Sweongyo	    "DATA_AVAIL",
640190688Sweongyo	    "SET_PWR_MODE",
641190688Sweongyo	    "BMISS_ACK",
642190688Sweongyo	    "SET_LED_STEADY",
643190688Sweongyo	    "SET_LED_BLINK",
644190688Sweongyo	    "SETUP_BEACON_DESC",
645190688Sweongyo	    "BEACON_INIT",
646190688Sweongyo	    "RESET_KEY_CACHE",
647190688Sweongyo	    "RESET_KEY_CACHE_ENTRY",
648190688Sweongyo	    "SET_KEY_CACHE_ENTRY",
649190688Sweongyo	    "SET_DECOMP_MASK",
650190688Sweongyo	    "SET_REGULATORY_DOMAIN",
651190688Sweongyo	    "SET_LED_STATE",
652190688Sweongyo	    "WRITE_ASSOCID",
653190688Sweongyo	    "SET_STA_BEACON_TIMERS",
654190688Sweongyo	    "GET_TSF",
655190688Sweongyo	    "RESET_TSF",
656190688Sweongyo	    "SET_ADHOC_MODE",
657190688Sweongyo	    "SET_BASIC_RATE",
658190688Sweongyo	    "MIB_CONTROL",
659190688Sweongyo	    "GET_CHANNEL_DATA",
660190688Sweongyo	    "GET_CUR_RSSI",
661190688Sweongyo	    "SET_ANTENNA_SWITCH",
662190688Sweongyo	    "0x2c", "0x2d", "0x2e",
663190688Sweongyo	    "USE_SHORT_SLOT_TIME",
664190688Sweongyo	    "SET_POWER_MODE",
665190688Sweongyo	    "SETUP_PSPOLL_DESC",
666190688Sweongyo	    "SET_RX_MULTICAST_FILTER",
667190688Sweongyo	    "RX_FILTER",
668190688Sweongyo	    "PER_CALIBRATION",
669190688Sweongyo	    "RESET",
670190688Sweongyo	    "DISABLE",
671190688Sweongyo	    "PHY_DISABLE",
672190688Sweongyo	    "SET_TX_POWER_LIMIT",
673190688Sweongyo	    "SET_TX_QUEUE_PARAMS",
674190688Sweongyo	    "SETUP_TX_QUEUE",
675190688Sweongyo	    "RELEASE_TX_QUEUE",
676190688Sweongyo	};
677190688Sweongyo	static char buf[8];
678190688Sweongyo
679190688Sweongyo	if (code < N(names))
680190688Sweongyo		return names[code];
681190688Sweongyo	if (code == WDCMSG_SET_DEFAULT_KEY)
682190688Sweongyo		return "SET_DEFAULT_KEY";
683190688Sweongyo	snprintf(buf, sizeof(buf), "0x%02x", code);
684190688Sweongyo	return buf;
685190688Sweongyo#undef N
686190688Sweongyo}
687190688Sweongyo#endif
688190688Sweongyo
689190688Sweongyo/*
690190688Sweongyo * Low-level function to send read or write commands to the firmware.
691190688Sweongyo */
692190688Sweongyostatic int
693190688Sweongyouath_cmdsend(struct uath_softc *sc, uint32_t code, const void *idata, int ilen,
694190688Sweongyo    void *odata, int olen, int flags)
695190688Sweongyo{
696190688Sweongyo	struct uath_cmd_hdr *hdr;
697190688Sweongyo	struct uath_cmd *cmd;
698190688Sweongyo	int error;
699190688Sweongyo
700190688Sweongyo	UATH_ASSERT_LOCKED(sc);
701190688Sweongyo
702190688Sweongyo	/* grab a xfer */
703190688Sweongyo	cmd = uath_get_cmdbuf(sc);
704190688Sweongyo	if (cmd == NULL) {
705190688Sweongyo		device_printf(sc->sc_dev, "%s: empty inactive queue\n",
706190688Sweongyo		    __func__);
707190688Sweongyo		return (ENOBUFS);
708190688Sweongyo	}
709190688Sweongyo	cmd->flags = flags;
710190688Sweongyo	/* always bulk-out a multiple of 4 bytes */
711190688Sweongyo	cmd->buflen = roundup2(sizeof(struct uath_cmd_hdr) + ilen, 4);
712190688Sweongyo
713190688Sweongyo	hdr = (struct uath_cmd_hdr *)cmd->buf;
714190688Sweongyo	bzero(hdr, sizeof (struct uath_cmd_hdr));	/* XXX not needed */
715190688Sweongyo	hdr->len   = htobe32(cmd->buflen);
716190688Sweongyo	hdr->code  = htobe32(code);
717190688Sweongyo	hdr->msgid = cmd->msgid;	/* don't care about endianness */
718190688Sweongyo	hdr->magic = htobe32((cmd->flags & UATH_CMD_FLAG_MAGIC) ? 1 << 24 : 0);
719190688Sweongyo	bcopy(idata, (uint8_t *)(hdr + 1), ilen);
720190688Sweongyo
721190688Sweongyo#ifdef UATH_DEBUG
722190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_CMDS) {
723190688Sweongyo		printf("%s: send  %s [flags 0x%x] olen %d\n",
724190688Sweongyo		    __func__, uath_codename(code), cmd->flags, olen);
725190688Sweongyo		if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP)
726190688Sweongyo			uath_dump_cmd(cmd->buf, cmd->buflen, '+');
727190688Sweongyo	}
728190688Sweongyo#endif
729190688Sweongyo	cmd->odata = odata;
730190688Sweongyo	KASSERT(odata == NULL ||
731190688Sweongyo	    olen < UATH_MAX_CMDSZ - sizeof(*hdr) + sizeof(uint32_t),
732190688Sweongyo	    ("odata %p olen %u", odata, olen));
733190688Sweongyo	cmd->olen = olen;
734190688Sweongyo
735190688Sweongyo	STAILQ_INSERT_TAIL(&sc->sc_cmd_pending, cmd, next);
736190688Sweongyo	UATH_STAT_INC(sc, st_cmd_pending);
737194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_INTR_TX]);
738190688Sweongyo
739190688Sweongyo	if (cmd->flags & UATH_CMD_FLAG_READ) {
740194228Sthompsa		usbd_transfer_start(sc->sc_xfer[UATH_INTR_RX]);
741190688Sweongyo
742190688Sweongyo		/* wait at most two seconds for command reply */
743190688Sweongyo		error = mtx_sleep(cmd, &sc->sc_mtx, 0, "uathcmd", 2 * hz);
744190688Sweongyo		cmd->odata = NULL;	/* in case reply comes too late */
745190688Sweongyo		if (error != 0) {
746190688Sweongyo			device_printf(sc->sc_dev, "timeout waiting for reply "
747190688Sweongyo			    "to cmd 0x%x (%u)\n", code, code);
748190688Sweongyo		} else if (cmd->olen != olen) {
749190688Sweongyo			device_printf(sc->sc_dev, "unexpected reply data count "
750190688Sweongyo			    "to cmd 0x%x (%u), got %u, expected %u\n",
751190688Sweongyo			    code, code, cmd->olen, olen);
752190688Sweongyo			error = EINVAL;
753190688Sweongyo		}
754190688Sweongyo		return (error);
755190688Sweongyo	}
756190688Sweongyo	return (0);
757190688Sweongyo}
758190688Sweongyo
759190688Sweongyostatic int
760190688Sweongyouath_cmd_read(struct uath_softc *sc, uint32_t code, const void *idata,
761190688Sweongyo    int ilen, void *odata, int olen, int flags)
762190688Sweongyo{
763190688Sweongyo
764190688Sweongyo	flags |= UATH_CMD_FLAG_READ;
765190688Sweongyo	return uath_cmdsend(sc, code, idata, ilen, odata, olen, flags);
766190688Sweongyo}
767190688Sweongyo
768190688Sweongyostatic int
769190688Sweongyouath_cmd_write(struct uath_softc *sc, uint32_t code, const void *data, int len,
770190688Sweongyo    int flags)
771190688Sweongyo{
772190688Sweongyo
773190688Sweongyo	flags &= ~UATH_CMD_FLAG_READ;
774190688Sweongyo	return uath_cmdsend(sc, code, data, len, NULL, 0, flags);
775190688Sweongyo}
776190688Sweongyo
777190688Sweongyostatic struct uath_cmd *
778190688Sweongyouath_get_cmdbuf(struct uath_softc *sc)
779190688Sweongyo{
780190688Sweongyo	struct uath_cmd *uc;
781190688Sweongyo
782190688Sweongyo	UATH_ASSERT_LOCKED(sc);
783190688Sweongyo
784190688Sweongyo	uc = STAILQ_FIRST(&sc->sc_cmd_inactive);
785190688Sweongyo	if (uc != NULL) {
786190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_inactive, next);
787190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_inactive);
788190688Sweongyo	} else
789190688Sweongyo		uc = NULL;
790190688Sweongyo	if (uc == NULL)
791190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "%s: %s\n", __func__,
792190688Sweongyo		    "out of command xmit buffers");
793190688Sweongyo	return (uc);
794190688Sweongyo}
795190688Sweongyo
796190688Sweongyo/*
797190688Sweongyo * This function is called periodically (every second) when associated to
798190688Sweongyo * query device statistics.
799190688Sweongyo */
800190688Sweongyostatic void
801190688Sweongyouath_stat(void *arg)
802190688Sweongyo{
803190688Sweongyo	struct uath_softc *sc = arg;
804190688Sweongyo	int error;
805190688Sweongyo
806190688Sweongyo	UATH_LOCK(sc);
807190688Sweongyo	/*
808190688Sweongyo	 * Send request for statistics asynchronously. The timer will be
809190688Sweongyo	 * restarted when we'll get the stats notification.
810190688Sweongyo	 */
811190688Sweongyo	error = uath_cmd_write(sc, WDCMSG_TARGET_GET_STATS, NULL, 0,
812190688Sweongyo	    UATH_CMD_FLAG_ASYNC);
813190688Sweongyo	if (error != 0) {
814190688Sweongyo		device_printf(sc->sc_dev,
815190688Sweongyo		    "could not query stats, error %d\n", error);
816190688Sweongyo	}
817190688Sweongyo	UATH_UNLOCK(sc);
818190688Sweongyo}
819190688Sweongyo
820190688Sweongyostatic int
821190688Sweongyouath_get_capability(struct uath_softc *sc, uint32_t cap, uint32_t *val)
822190688Sweongyo{
823190688Sweongyo	int error;
824190688Sweongyo
825190688Sweongyo	cap = htobe32(cap);
826190688Sweongyo	error = uath_cmd_read(sc, WDCMSG_TARGET_GET_CAPABILITY,
827190688Sweongyo	    &cap, sizeof cap, val, sizeof(uint32_t), UATH_CMD_FLAG_MAGIC);
828190688Sweongyo	if (error != 0) {
829190688Sweongyo		device_printf(sc->sc_dev, "could not read capability %u\n",
830190688Sweongyo		    be32toh(cap));
831190688Sweongyo		return (error);
832190688Sweongyo	}
833190688Sweongyo	*val = be32toh(*val);
834190688Sweongyo	return (error);
835190688Sweongyo}
836190688Sweongyo
837190688Sweongyostatic int
838190688Sweongyouath_get_devcap(struct uath_softc *sc)
839190688Sweongyo{
840190688Sweongyo#define	GETCAP(x, v) do {				\
841190688Sweongyo	error = uath_get_capability(sc, x, &v);		\
842190688Sweongyo	if (error != 0)					\
843190688Sweongyo		return (error);				\
844190688Sweongyo	DPRINTF(sc, UATH_DEBUG_DEVCAP,			\
845190688Sweongyo	    "%s: %s=0x%08x\n", __func__, #x, v);	\
846190688Sweongyo} while (0)
847190688Sweongyo	struct uath_devcap *cap = &sc->sc_devcap;
848190688Sweongyo	int error;
849190688Sweongyo
850190688Sweongyo	/* collect device capabilities */
851190688Sweongyo	GETCAP(CAP_TARGET_VERSION, cap->targetVersion);
852190688Sweongyo	GETCAP(CAP_TARGET_REVISION, cap->targetRevision);
853190688Sweongyo	GETCAP(CAP_MAC_VERSION, cap->macVersion);
854190688Sweongyo	GETCAP(CAP_MAC_REVISION, cap->macRevision);
855190688Sweongyo	GETCAP(CAP_PHY_REVISION, cap->phyRevision);
856190688Sweongyo	GETCAP(CAP_ANALOG_5GHz_REVISION, cap->analog5GhzRevision);
857190688Sweongyo	GETCAP(CAP_ANALOG_2GHz_REVISION, cap->analog2GhzRevision);
858190688Sweongyo
859190688Sweongyo	GETCAP(CAP_REG_DOMAIN, cap->regDomain);
860190688Sweongyo	GETCAP(CAP_REG_CAP_BITS, cap->regCapBits);
861190688Sweongyo#if 0
862190688Sweongyo	/* NB: not supported in rev 1.5 */
863190688Sweongyo	GETCAP(CAP_COUNTRY_CODE, cap->countryCode);
864190688Sweongyo#endif
865190688Sweongyo	GETCAP(CAP_WIRELESS_MODES, cap->wirelessModes);
866190688Sweongyo	GETCAP(CAP_CHAN_SPREAD_SUPPORT, cap->chanSpreadSupport);
867190688Sweongyo	GETCAP(CAP_COMPRESS_SUPPORT, cap->compressSupport);
868190688Sweongyo	GETCAP(CAP_BURST_SUPPORT, cap->burstSupport);
869190688Sweongyo	GETCAP(CAP_FAST_FRAMES_SUPPORT, cap->fastFramesSupport);
870190688Sweongyo	GETCAP(CAP_CHAP_TUNING_SUPPORT, cap->chapTuningSupport);
871190688Sweongyo	GETCAP(CAP_TURBOG_SUPPORT, cap->turboGSupport);
872190688Sweongyo	GETCAP(CAP_TURBO_PRIME_SUPPORT, cap->turboPrimeSupport);
873190688Sweongyo	GETCAP(CAP_DEVICE_TYPE, cap->deviceType);
874190688Sweongyo	GETCAP(CAP_WME_SUPPORT, cap->wmeSupport);
875190688Sweongyo	GETCAP(CAP_TOTAL_QUEUES, cap->numTxQueues);
876190688Sweongyo	GETCAP(CAP_CONNECTION_ID_MAX, cap->connectionIdMax);
877190688Sweongyo
878190688Sweongyo	GETCAP(CAP_LOW_5GHZ_CHAN, cap->low5GhzChan);
879190688Sweongyo	GETCAP(CAP_HIGH_5GHZ_CHAN, cap->high5GhzChan);
880190688Sweongyo	GETCAP(CAP_LOW_2GHZ_CHAN, cap->low2GhzChan);
881190688Sweongyo	GETCAP(CAP_HIGH_2GHZ_CHAN, cap->high2GhzChan);
882190688Sweongyo	GETCAP(CAP_TWICE_ANTENNAGAIN_5G, cap->twiceAntennaGain5G);
883190688Sweongyo	GETCAP(CAP_TWICE_ANTENNAGAIN_2G, cap->twiceAntennaGain2G);
884190688Sweongyo
885190688Sweongyo	GETCAP(CAP_CIPHER_AES_CCM, cap->supportCipherAES_CCM);
886190688Sweongyo	GETCAP(CAP_CIPHER_TKIP, cap->supportCipherTKIP);
887190688Sweongyo	GETCAP(CAP_MIC_TKIP, cap->supportMicTKIP);
888190688Sweongyo
889190688Sweongyo	cap->supportCipherWEP = 1;	/* NB: always available */
890190688Sweongyo
891190688Sweongyo	return (0);
892190688Sweongyo}
893190688Sweongyo
894190688Sweongyostatic int
895190688Sweongyouath_get_devstatus(struct uath_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
896190688Sweongyo{
897190688Sweongyo	int error;
898190688Sweongyo
899190688Sweongyo	/* retrieve MAC address */
900190688Sweongyo	error = uath_get_status(sc, ST_MAC_ADDR, macaddr, IEEE80211_ADDR_LEN);
901190688Sweongyo	if (error != 0) {
902190688Sweongyo		device_printf(sc->sc_dev, "could not read MAC address\n");
903190688Sweongyo		return (error);
904190688Sweongyo	}
905190688Sweongyo
906190688Sweongyo	error = uath_get_status(sc, ST_SERIAL_NUMBER,
907190688Sweongyo	    &sc->sc_serial[0], sizeof(sc->sc_serial));
908190688Sweongyo	if (error != 0) {
909190688Sweongyo		device_printf(sc->sc_dev,
910190688Sweongyo		    "could not read device serial number\n");
911190688Sweongyo		return (error);
912190688Sweongyo	}
913190688Sweongyo	return (0);
914190688Sweongyo}
915190688Sweongyo
916190688Sweongyostatic int
917190688Sweongyouath_get_status(struct uath_softc *sc, uint32_t which, void *odata, int olen)
918190688Sweongyo{
919190688Sweongyo	int error;
920190688Sweongyo
921190688Sweongyo	which = htobe32(which);
922190688Sweongyo	error = uath_cmd_read(sc, WDCMSG_TARGET_GET_STATUS,
923190688Sweongyo	    &which, sizeof(which), odata, olen, UATH_CMD_FLAG_MAGIC);
924190688Sweongyo	if (error != 0)
925190688Sweongyo		device_printf(sc->sc_dev,
926190688Sweongyo		    "could not read EEPROM offset 0x%02x\n", be32toh(which));
927190688Sweongyo	return (error);
928190688Sweongyo}
929190688Sweongyo
930190688Sweongyostatic void
931190688Sweongyouath_free_data_list(struct uath_softc *sc, struct uath_data data[], int ndata,
932190688Sweongyo    int fillmbuf)
933190688Sweongyo{
934190688Sweongyo	int i;
935190688Sweongyo
936190688Sweongyo	for (i = 0; i < ndata; i++) {
937190688Sweongyo		struct uath_data *dp = &data[i];
938190688Sweongyo
939190688Sweongyo		if (fillmbuf == 1) {
940190688Sweongyo			if (dp->m != NULL) {
941190688Sweongyo				m_freem(dp->m);
942190688Sweongyo				dp->m = NULL;
943190688Sweongyo				dp->buf = NULL;
944190688Sweongyo			}
945190688Sweongyo		} else {
946190688Sweongyo			if (dp->buf != NULL) {
947190688Sweongyo				free(dp->buf, M_USBDEV);
948190688Sweongyo				dp->buf = NULL;
949190688Sweongyo			}
950190688Sweongyo		}
951190688Sweongyo#ifdef UATH_DEBUG
952190688Sweongyo		if (dp->ni != NULL)
953190688Sweongyo			device_printf(sc->sc_dev, "Node isn't NULL\n");
954190688Sweongyo#endif
955190688Sweongyo	}
956190688Sweongyo}
957190688Sweongyo
958190688Sweongyostatic int
959190688Sweongyouath_alloc_data_list(struct uath_softc *sc, struct uath_data data[],
960190688Sweongyo	int ndata, int maxsz, int fillmbuf)
961190688Sweongyo{
962190688Sweongyo	int i, error;
963190688Sweongyo
964190688Sweongyo	for (i = 0; i < ndata; i++) {
965190688Sweongyo		struct uath_data *dp = &data[i];
966190688Sweongyo
967190688Sweongyo		dp->sc = sc;
968190688Sweongyo		if (fillmbuf) {
969190688Sweongyo			/* XXX check maxsz */
970190688Sweongyo			dp->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
971190688Sweongyo			if (dp->m == NULL) {
972190688Sweongyo				device_printf(sc->sc_dev,
973190688Sweongyo				    "could not allocate rx mbuf\n");
974190688Sweongyo				error = ENOMEM;
975190688Sweongyo				goto fail;
976190688Sweongyo			}
977190688Sweongyo			dp->buf = mtod(dp->m, uint8_t *);
978190688Sweongyo		} else {
979190688Sweongyo			dp->m = NULL;
980190688Sweongyo			dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
981190688Sweongyo			if (dp->buf == NULL) {
982190688Sweongyo				device_printf(sc->sc_dev,
983190688Sweongyo				    "could not allocate buffer\n");
984190688Sweongyo				error = ENOMEM;
985190688Sweongyo				goto fail;
986190688Sweongyo			}
987190688Sweongyo		}
988190688Sweongyo		dp->ni = NULL;
989190688Sweongyo	}
990190688Sweongyo
991190688Sweongyo	return (0);
992190688Sweongyo
993190688Sweongyofail:	uath_free_data_list(sc, data, ndata, fillmbuf);
994190688Sweongyo	return (error);
995190688Sweongyo}
996190688Sweongyo
997190688Sweongyostatic int
998190688Sweongyouath_alloc_rx_data_list(struct uath_softc *sc)
999190688Sweongyo{
1000190688Sweongyo	int error, i;
1001190688Sweongyo
1002190688Sweongyo	/* XXX is it enough to store the RX packet with MCLBYTES bytes?  */
1003190688Sweongyo	error = uath_alloc_data_list(sc,
1004190688Sweongyo	    sc->sc_rx, UATH_RX_DATA_LIST_COUNT, MCLBYTES,
1005190688Sweongyo	    1 /* setup mbufs */);
1006190688Sweongyo	if (error != 0)
1007190688Sweongyo		return (error);
1008190688Sweongyo
1009190688Sweongyo	STAILQ_INIT(&sc->sc_rx_active);
1010190688Sweongyo	STAILQ_INIT(&sc->sc_rx_inactive);
1011190688Sweongyo
1012190688Sweongyo	for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) {
1013190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i],
1014190688Sweongyo		    next);
1015190688Sweongyo		UATH_STAT_INC(sc, st_rx_inactive);
1016190688Sweongyo	}
1017190688Sweongyo
1018190688Sweongyo	return (0);
1019190688Sweongyo}
1020190688Sweongyo
1021190688Sweongyostatic int
1022190688Sweongyouath_alloc_tx_data_list(struct uath_softc *sc)
1023190688Sweongyo{
1024190688Sweongyo	int error, i;
1025190688Sweongyo
1026190688Sweongyo	error = uath_alloc_data_list(sc,
1027190688Sweongyo	    sc->sc_tx, UATH_TX_DATA_LIST_COUNT, UATH_MAX_TXBUFSZ,
1028190688Sweongyo	    0 /* no mbufs */);
1029190688Sweongyo	if (error != 0)
1030190688Sweongyo		return (error);
1031190688Sweongyo
1032190688Sweongyo	STAILQ_INIT(&sc->sc_tx_active);
1033190688Sweongyo	STAILQ_INIT(&sc->sc_tx_inactive);
1034190688Sweongyo	STAILQ_INIT(&sc->sc_tx_pending);
1035190688Sweongyo
1036190688Sweongyo	for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) {
1037190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i],
1038190688Sweongyo		    next);
1039190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
1040190688Sweongyo	}
1041190688Sweongyo
1042190688Sweongyo	return (0);
1043190688Sweongyo}
1044190688Sweongyo
1045190688Sweongyostatic void
1046190688Sweongyouath_free_rx_data_list(struct uath_softc *sc)
1047190688Sweongyo{
1048190688Sweongyo
1049190688Sweongyo	STAILQ_INIT(&sc->sc_rx_active);
1050190688Sweongyo	STAILQ_INIT(&sc->sc_rx_inactive);
1051190688Sweongyo
1052190688Sweongyo	uath_free_data_list(sc, sc->sc_rx, UATH_RX_DATA_LIST_COUNT,
1053190688Sweongyo	    1 /* free mbufs */);
1054190688Sweongyo}
1055190688Sweongyo
1056190688Sweongyostatic void
1057190688Sweongyouath_free_tx_data_list(struct uath_softc *sc)
1058190688Sweongyo{
1059190688Sweongyo
1060190688Sweongyo	STAILQ_INIT(&sc->sc_tx_active);
1061190688Sweongyo	STAILQ_INIT(&sc->sc_tx_inactive);
1062190688Sweongyo	STAILQ_INIT(&sc->sc_tx_pending);
1063190688Sweongyo
1064190688Sweongyo	uath_free_data_list(sc, sc->sc_tx, UATH_TX_DATA_LIST_COUNT,
1065190688Sweongyo	    0 /* no mbufs */);
1066190688Sweongyo}
1067190688Sweongyo
1068190688Sweongyostatic struct ieee80211vap *
1069190688Sweongyouath_vap_create(struct ieee80211com *ic,
1070190688Sweongyo	const char name[IFNAMSIZ], int unit, int opmode, int flags,
1071190688Sweongyo	const uint8_t bssid[IEEE80211_ADDR_LEN],
1072190688Sweongyo	const uint8_t mac[IEEE80211_ADDR_LEN])
1073190688Sweongyo{
1074190688Sweongyo	struct uath_vap *uvp;
1075190688Sweongyo	struct ieee80211vap *vap;
1076190688Sweongyo
1077190688Sweongyo	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
1078190688Sweongyo		return (NULL);
1079190688Sweongyo	uvp = (struct uath_vap *) malloc(sizeof(struct uath_vap),
1080190688Sweongyo	    M_80211_VAP, M_NOWAIT | M_ZERO);
1081190688Sweongyo	if (uvp == NULL)
1082190688Sweongyo		return (NULL);
1083190688Sweongyo	vap = &uvp->vap;
1084190688Sweongyo	/* enable s/w bmiss handling for sta mode */
1085190688Sweongyo	ieee80211_vap_setup(ic, vap, name, unit, opmode,
1086190688Sweongyo	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
1087190688Sweongyo
1088190688Sweongyo	/* override state transition machine */
1089190688Sweongyo	uvp->newstate = vap->iv_newstate;
1090190688Sweongyo	vap->iv_newstate = uath_newstate;
1091190688Sweongyo
1092190688Sweongyo	/* complete setup */
1093190688Sweongyo	ieee80211_vap_attach(vap, ieee80211_media_change,
1094190688Sweongyo	    ieee80211_media_status);
1095190688Sweongyo	ic->ic_opmode = opmode;
1096190688Sweongyo	return (vap);
1097190688Sweongyo}
1098190688Sweongyo
1099190688Sweongyostatic void
1100190688Sweongyouath_vap_delete(struct ieee80211vap *vap)
1101190688Sweongyo{
1102190688Sweongyo	struct uath_vap *uvp = UATH_VAP(vap);
1103190688Sweongyo
1104190688Sweongyo	ieee80211_vap_detach(vap);
1105190688Sweongyo	free(uvp, M_80211_VAP);
1106190688Sweongyo}
1107190688Sweongyo
1108190688Sweongyostatic int
1109190688Sweongyouath_init_locked(void *arg)
1110190688Sweongyo{
1111190688Sweongyo	struct uath_softc *sc = arg;
1112190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
1113190688Sweongyo	struct ieee80211com *ic = ifp->if_l2com;
1114190688Sweongyo	uint32_t val;
1115190688Sweongyo	int error;
1116190688Sweongyo
1117190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1118190688Sweongyo
1119190688Sweongyo	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1120190688Sweongyo		uath_stop_locked(ifp);
1121190688Sweongyo
1122190688Sweongyo	/* reset variables */
1123190688Sweongyo	sc->sc_intrx_nextnum = sc->sc_msgid = 0;
1124190688Sweongyo
1125190688Sweongyo	val = htobe32(0);
1126190688Sweongyo	uath_cmd_write(sc, WDCMSG_BIND, &val, sizeof val, 0);
1127190688Sweongyo
1128190688Sweongyo	/* set MAC address */
1129190688Sweongyo	uath_config_multi(sc, CFG_MAC_ADDR, IF_LLADDR(ifp), IEEE80211_ADDR_LEN);
1130190688Sweongyo
1131190688Sweongyo	/* XXX honor net80211 state */
1132190688Sweongyo	uath_config(sc, CFG_RATE_CONTROL_ENABLE, 0x00000001);
1133190688Sweongyo	uath_config(sc, CFG_DIVERSITY_CTL, 0x00000001);
1134190688Sweongyo	uath_config(sc, CFG_ABOLT, 0x0000003f);
1135190688Sweongyo	uath_config(sc, CFG_WME_ENABLED, 0x00000001);
1136190688Sweongyo
1137190688Sweongyo	uath_config(sc, CFG_SERVICE_TYPE, 1);
1138190688Sweongyo	uath_config(sc, CFG_TP_SCALE, 0x00000000);
1139190688Sweongyo	uath_config(sc, CFG_TPC_HALF_DBM5, 0x0000003c);
1140190688Sweongyo	uath_config(sc, CFG_TPC_HALF_DBM2, 0x0000003c);
1141190688Sweongyo	uath_config(sc, CFG_OVERRD_TX_POWER, 0x00000000);
1142190688Sweongyo	uath_config(sc, CFG_GMODE_PROTECTION, 0x00000000);
1143190688Sweongyo	uath_config(sc, CFG_GMODE_PROTECT_RATE_INDEX, 0x00000003);
1144190688Sweongyo	uath_config(sc, CFG_PROTECTION_TYPE, 0x00000000);
1145190688Sweongyo	uath_config(sc, CFG_MODE_CTS, 0x00000002);
1146190688Sweongyo
1147190688Sweongyo	error = uath_cmd_read(sc, WDCMSG_TARGET_START, NULL, 0,
1148190688Sweongyo	    &val, sizeof(val), UATH_CMD_FLAG_MAGIC);
1149190688Sweongyo	if (error) {
1150190688Sweongyo		device_printf(sc->sc_dev,
1151190688Sweongyo		    "could not start target, error %d\n", error);
1152190688Sweongyo		goto fail;
1153190688Sweongyo	}
1154190688Sweongyo	DPRINTF(sc, UATH_DEBUG_INIT, "%s returns handle: 0x%x\n",
1155190688Sweongyo	    uath_codename(WDCMSG_TARGET_START), be32toh(val));
1156190688Sweongyo
1157190688Sweongyo	/* set default channel */
1158190688Sweongyo	error = uath_switch_channel(sc, ic->ic_curchan);
1159190688Sweongyo	if (error) {
1160190688Sweongyo		device_printf(sc->sc_dev,
1161190688Sweongyo		    "could not switch channel, error %d\n", error);
1162190688Sweongyo		goto fail;
1163190688Sweongyo	}
1164190688Sweongyo
1165190688Sweongyo	val = htobe32(TARGET_DEVICE_AWAKE);
1166190688Sweongyo	uath_cmd_write(sc, WDCMSG_SET_PWR_MODE, &val, sizeof val, 0);
1167190688Sweongyo	/* XXX? check */
1168190688Sweongyo	uath_cmd_write(sc, WDCMSG_RESET_KEY_CACHE, NULL, 0, 0);
1169190688Sweongyo
1170194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_BULK_RX]);
1171190688Sweongyo	/* enable Rx */
1172190688Sweongyo	uath_set_rxfilter(sc, 0x0, UATH_FILTER_OP_INIT);
1173190688Sweongyo	uath_set_rxfilter(sc,
1174190688Sweongyo	    UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
1175190688Sweongyo	    UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON,
1176190688Sweongyo	    UATH_FILTER_OP_SET);
1177190688Sweongyo
1178190688Sweongyo	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1179190688Sweongyo	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1180190688Sweongyo	sc->sc_flags |= UATH_FLAG_INITDONE;
1181190688Sweongyo
1182190688Sweongyo	callout_reset(&sc->watchdog_ch, hz, uath_watchdog, sc);
1183190688Sweongyo
1184190688Sweongyo	return (0);
1185190688Sweongyo
1186190688Sweongyofail:
1187190688Sweongyo	uath_stop_locked(ifp);
1188190688Sweongyo	return (error);
1189190688Sweongyo}
1190190688Sweongyo
1191190688Sweongyostatic void
1192190688Sweongyouath_init(void *arg)
1193190688Sweongyo{
1194190688Sweongyo	struct uath_softc *sc = arg;
1195190688Sweongyo
1196190688Sweongyo	UATH_LOCK(sc);
1197190688Sweongyo	(void)uath_init_locked(sc);
1198190688Sweongyo	UATH_UNLOCK(sc);
1199190688Sweongyo}
1200190688Sweongyo
1201190688Sweongyostatic void
1202190688Sweongyouath_stop_locked(struct ifnet *ifp)
1203190688Sweongyo{
1204190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1205190688Sweongyo
1206190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1207190688Sweongyo
1208190688Sweongyo	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1209190688Sweongyo	sc->sc_flags &= ~UATH_FLAG_INITDONE;
1210190688Sweongyo
1211190688Sweongyo	callout_stop(&sc->stat_ch);
1212190688Sweongyo	callout_stop(&sc->watchdog_ch);
1213190688Sweongyo	sc->sc_tx_timer = 0;
1214190688Sweongyo	/* abort pending transmits  */
1215190688Sweongyo	uath_abort_xfers(sc);
1216190688Sweongyo	/* flush data & control requests into the target  */
1217190688Sweongyo	(void)uath_flush(sc);
1218190688Sweongyo	/* set a LED status to the disconnected.  */
1219190688Sweongyo	uath_set_ledstate(sc, 0);
1220190688Sweongyo	/* stop the target  */
1221190688Sweongyo	uath_cmd_write(sc, WDCMSG_TARGET_STOP, NULL, 0, 0);
1222190688Sweongyo}
1223190688Sweongyo
1224190688Sweongyostatic void
1225190688Sweongyouath_stop(struct ifnet *ifp)
1226190688Sweongyo{
1227190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1228190688Sweongyo
1229190688Sweongyo	UATH_LOCK(sc);
1230190688Sweongyo	uath_stop_locked(ifp);
1231190688Sweongyo	UATH_UNLOCK(sc);
1232190688Sweongyo}
1233190688Sweongyo
1234190688Sweongyostatic int
1235190688Sweongyouath_config(struct uath_softc *sc, uint32_t reg, uint32_t val)
1236190688Sweongyo{
1237190688Sweongyo	struct uath_write_mac write;
1238190688Sweongyo	int error;
1239190688Sweongyo
1240190688Sweongyo	write.reg = htobe32(reg);
1241190688Sweongyo	write.len = htobe32(0);	/* 0 = single write */
1242190688Sweongyo	*(uint32_t *)write.data = htobe32(val);
1243190688Sweongyo
1244190688Sweongyo	error = uath_cmd_write(sc, WDCMSG_TARGET_SET_CONFIG, &write,
1245190688Sweongyo	    3 * sizeof (uint32_t), 0);
1246190688Sweongyo	if (error != 0) {
1247190688Sweongyo		device_printf(sc->sc_dev, "could not write register 0x%02x\n",
1248190688Sweongyo		    reg);
1249190688Sweongyo	}
1250190688Sweongyo	return (error);
1251190688Sweongyo}
1252190688Sweongyo
1253190688Sweongyostatic int
1254190688Sweongyouath_config_multi(struct uath_softc *sc, uint32_t reg, const void *data,
1255190688Sweongyo    int len)
1256190688Sweongyo{
1257190688Sweongyo	struct uath_write_mac write;
1258190688Sweongyo	int error;
1259190688Sweongyo
1260190688Sweongyo	write.reg = htobe32(reg);
1261190688Sweongyo	write.len = htobe32(len);
1262190688Sweongyo	bcopy(data, write.data, len);
1263190688Sweongyo
1264190688Sweongyo	/* properly handle the case where len is zero (reset) */
1265190688Sweongyo	error = uath_cmd_write(sc, WDCMSG_TARGET_SET_CONFIG, &write,
1266190688Sweongyo	    (len == 0) ? sizeof (uint32_t) : 2 * sizeof (uint32_t) + len, 0);
1267190688Sweongyo	if (error != 0) {
1268190688Sweongyo		device_printf(sc->sc_dev,
1269190688Sweongyo		    "could not write %d bytes to register 0x%02x\n", len, reg);
1270190688Sweongyo	}
1271190688Sweongyo	return (error);
1272190688Sweongyo}
1273190688Sweongyo
1274190688Sweongyostatic int
1275190688Sweongyouath_switch_channel(struct uath_softc *sc, struct ieee80211_channel *c)
1276190688Sweongyo{
1277190688Sweongyo	int error;
1278190688Sweongyo
1279190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1280190688Sweongyo
1281190688Sweongyo	/* set radio frequency */
1282190688Sweongyo	error = uath_set_chan(sc, c);
1283190688Sweongyo	if (error) {
1284190688Sweongyo		device_printf(sc->sc_dev,
1285190688Sweongyo		    "could not set channel, error %d\n", error);
1286190688Sweongyo		goto failed;
1287190688Sweongyo	}
1288190688Sweongyo	/* reset Tx rings */
1289190688Sweongyo	error = uath_reset_tx_queues(sc);
1290190688Sweongyo	if (error) {
1291190688Sweongyo		device_printf(sc->sc_dev,
1292190688Sweongyo		    "could not reset Tx queues, error %d\n", error);
1293190688Sweongyo		goto failed;
1294190688Sweongyo	}
1295190688Sweongyo	/* set Tx rings WME properties */
1296190688Sweongyo	error = uath_wme_init(sc);
1297190688Sweongyo	if (error) {
1298190688Sweongyo		device_printf(sc->sc_dev,
1299190688Sweongyo		    "could not init Tx queues, error %d\n", error);
1300190688Sweongyo		goto failed;
1301190688Sweongyo	}
1302190688Sweongyo	error = uath_set_ledstate(sc, 0);
1303190688Sweongyo	if (error) {
1304190688Sweongyo		device_printf(sc->sc_dev,
1305190688Sweongyo		    "could not set led state, error %d\n", error);
1306190688Sweongyo		goto failed;
1307190688Sweongyo	}
1308190688Sweongyo	error = uath_flush(sc);
1309190688Sweongyo	if (error) {
1310190688Sweongyo		device_printf(sc->sc_dev,
1311190688Sweongyo		    "could not flush pipes, error %d\n", error);
1312190688Sweongyo		goto failed;
1313190688Sweongyo	}
1314190688Sweongyofailed:
1315190688Sweongyo	return (error);
1316190688Sweongyo}
1317190688Sweongyo
1318190688Sweongyostatic int
1319190688Sweongyouath_set_rxfilter(struct uath_softc *sc, uint32_t bits, uint32_t op)
1320190688Sweongyo{
1321190688Sweongyo	struct uath_cmd_rx_filter rxfilter;
1322190688Sweongyo
1323190688Sweongyo	rxfilter.bits = htobe32(bits);
1324190688Sweongyo	rxfilter.op = htobe32(op);
1325190688Sweongyo
1326190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
1327190688Sweongyo	    "setting Rx filter=0x%x flags=0x%x\n", bits, op);
1328190688Sweongyo	return uath_cmd_write(sc, WDCMSG_RX_FILTER, &rxfilter,
1329190688Sweongyo	    sizeof rxfilter, 0);
1330190688Sweongyo}
1331190688Sweongyo
1332190688Sweongyostatic void
1333190688Sweongyouath_watchdog(void *arg)
1334190688Sweongyo{
1335190688Sweongyo	struct uath_softc *sc = arg;
1336190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
1337190688Sweongyo
1338190688Sweongyo	if (sc->sc_tx_timer > 0) {
1339190688Sweongyo		if (--sc->sc_tx_timer == 0) {
1340190688Sweongyo			device_printf(sc->sc_dev, "device timeout\n");
1341190688Sweongyo			/*uath_init(ifp); XXX needs a process context! */
1342190688Sweongyo			ifp->if_oerrors++;
1343190688Sweongyo			return;
1344190688Sweongyo		}
1345190688Sweongyo		callout_reset(&sc->watchdog_ch, hz, uath_watchdog, sc);
1346190688Sweongyo	}
1347190688Sweongyo}
1348190688Sweongyo
1349190688Sweongyostatic void
1350190688Sweongyouath_abort_xfers(struct uath_softc *sc)
1351190688Sweongyo{
1352190688Sweongyo	int i;
1353190688Sweongyo
1354190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1355190688Sweongyo	/* abort any pending transfers */
1356190688Sweongyo	for (i = 0; i < UATH_N_XFERS; i++)
1357194228Sthompsa		usbd_transfer_stop(sc->sc_xfer[i]);
1358190688Sweongyo}
1359190688Sweongyo
1360190688Sweongyostatic int
1361190688Sweongyouath_flush(struct uath_softc *sc)
1362190688Sweongyo{
1363190688Sweongyo	int error;
1364190688Sweongyo
1365190688Sweongyo	error = uath_dataflush(sc);
1366190688Sweongyo	if (error != 0)
1367190688Sweongyo		goto failed;
1368190688Sweongyo
1369190688Sweongyo	error = uath_cmdflush(sc);
1370190688Sweongyo	if (error != 0)
1371190688Sweongyo		goto failed;
1372190688Sweongyo
1373190688Sweongyofailed:
1374190688Sweongyo	return (error);
1375190688Sweongyo}
1376190688Sweongyo
1377190688Sweongyostatic int
1378190688Sweongyouath_cmdflush(struct uath_softc *sc)
1379190688Sweongyo{
1380190688Sweongyo
1381190688Sweongyo	return uath_cmd_write(sc, WDCMSG_FLUSH, NULL, 0, 0);
1382190688Sweongyo}
1383190688Sweongyo
1384190688Sweongyostatic int
1385190688Sweongyouath_dataflush(struct uath_softc *sc)
1386190688Sweongyo{
1387190688Sweongyo	struct uath_data *data;
1388190688Sweongyo	struct uath_chunk *chunk;
1389190688Sweongyo	struct uath_tx_desc *desc;
1390190688Sweongyo
1391190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1392190688Sweongyo
1393190688Sweongyo	data = uath_getbuf(sc);
1394190688Sweongyo	if (data == NULL)
1395190688Sweongyo		return (ENOBUFS);
1396190688Sweongyo	data->buflen = sizeof(struct uath_chunk) + sizeof(struct uath_tx_desc);
1397190688Sweongyo	data->m = NULL;
1398190688Sweongyo	data->ni = NULL;
1399190688Sweongyo	chunk = (struct uath_chunk *)data->buf;
1400190688Sweongyo	desc = (struct uath_tx_desc *)(chunk + 1);
1401190688Sweongyo
1402190688Sweongyo	/* one chunk only */
1403190688Sweongyo	chunk->seqnum = 0;
1404190688Sweongyo	chunk->flags = UATH_CFLAGS_FINAL;
1405190688Sweongyo	chunk->length = htobe16(sizeof (struct uath_tx_desc));
1406190688Sweongyo
1407190688Sweongyo	bzero(desc, sizeof(struct uath_tx_desc));
1408190688Sweongyo	desc->msglen = htobe32(sizeof(struct uath_tx_desc));
1409190688Sweongyo	desc->msgid  = (sc->sc_msgid++) + 1; /* don't care about endianness */
1410190688Sweongyo	desc->type   = htobe32(WDCMSG_FLUSH);
1411190688Sweongyo	desc->txqid  = htobe32(0);
1412190688Sweongyo	desc->connid = htobe32(0);
1413190688Sweongyo	desc->flags  = htobe32(0);
1414190688Sweongyo
1415190688Sweongyo#ifdef UATH_DEBUG
1416190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_CMDS) {
1417190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RESET, "send flush ix %d\n",
1418190688Sweongyo		    desc->msgid);
1419190688Sweongyo		if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP)
1420190688Sweongyo			uath_dump_cmd(data->buf, data->buflen, '+');
1421190688Sweongyo	}
1422190688Sweongyo#endif
1423190688Sweongyo
1424190688Sweongyo	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1425190688Sweongyo	UATH_STAT_INC(sc, st_tx_pending);
1426190688Sweongyo	sc->sc_tx_timer = 5;
1427194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_BULK_TX]);
1428190688Sweongyo
1429190688Sweongyo	return (0);
1430190688Sweongyo}
1431190688Sweongyo
1432190688Sweongyostatic struct uath_data *
1433190688Sweongyo_uath_getbuf(struct uath_softc *sc)
1434190688Sweongyo{
1435190688Sweongyo	struct uath_data *bf;
1436190688Sweongyo
1437190688Sweongyo	bf = STAILQ_FIRST(&sc->sc_tx_inactive);
1438190688Sweongyo	if (bf != NULL) {
1439190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
1440190688Sweongyo		UATH_STAT_DEC(sc, st_tx_inactive);
1441190688Sweongyo	} else
1442190688Sweongyo		bf = NULL;
1443190688Sweongyo	if (bf == NULL)
1444190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "%s: %s\n", __func__,
1445190688Sweongyo		    "out of xmit buffers");
1446190688Sweongyo	return (bf);
1447190688Sweongyo}
1448190688Sweongyo
1449190688Sweongyostatic struct uath_data *
1450190688Sweongyouath_getbuf(struct uath_softc *sc)
1451190688Sweongyo{
1452190688Sweongyo	struct uath_data *bf;
1453190688Sweongyo
1454190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1455190688Sweongyo
1456190688Sweongyo	bf = _uath_getbuf(sc);
1457190688Sweongyo	if (bf == NULL) {
1458190688Sweongyo		struct ifnet *ifp = sc->sc_ifp;
1459190688Sweongyo
1460190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
1461190688Sweongyo		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1462190688Sweongyo	}
1463190688Sweongyo	return (bf);
1464190688Sweongyo}
1465190688Sweongyo
1466190688Sweongyostatic int
1467190688Sweongyouath_set_ledstate(struct uath_softc *sc, int connected)
1468190688Sweongyo{
1469190688Sweongyo
1470190688Sweongyo	DPRINTF(sc, UATH_DEBUG_LED,
1471190688Sweongyo	    "set led state %sconnected\n", connected ? "" : "!");
1472190688Sweongyo	connected = htobe32(connected);
1473190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_LED_STATE,
1474190688Sweongyo	     &connected, sizeof connected, 0);
1475190688Sweongyo}
1476190688Sweongyo
1477190688Sweongyostatic int
1478190688Sweongyouath_set_chan(struct uath_softc *sc, struct ieee80211_channel *c)
1479190688Sweongyo{
1480190688Sweongyo#ifdef UATH_DEBUG
1481190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
1482190688Sweongyo	struct ieee80211com *ic = ifp->if_l2com;
1483190688Sweongyo#endif
1484190688Sweongyo	struct uath_cmd_reset reset;
1485190688Sweongyo
1486190688Sweongyo	bzero(&reset, sizeof reset);
1487190688Sweongyo	if (IEEE80211_IS_CHAN_2GHZ(c))
1488190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_2GHZ);
1489190688Sweongyo	if (IEEE80211_IS_CHAN_5GHZ(c))
1490190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_5GHZ);
1491190688Sweongyo	/* NB: 11g =>'s 11b so don't specify both OFDM and CCK */
1492192257Ssam	if (IEEE80211_IS_CHAN_OFDM(c))
1493190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_OFDM);
1494192257Ssam	else if (IEEE80211_IS_CHAN_CCK(c))
1495190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_CCK);
1496190688Sweongyo	/* turbo can be used in either 2GHz or 5GHz */
1497190688Sweongyo	if (c->ic_flags & IEEE80211_CHAN_TURBO)
1498190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_TURBO);
1499190688Sweongyo	reset.freq = htobe32(c->ic_freq);
1500190688Sweongyo	reset.maxrdpower = htobe32(50);	/* XXX */
1501190688Sweongyo	reset.channelchange = htobe32(1);
1502190688Sweongyo	reset.keeprccontent = htobe32(0);
1503190688Sweongyo
1504190688Sweongyo	DPRINTF(sc, UATH_DEBUG_CHANNEL, "set channel %d, flags 0x%x freq %u\n",
1505190688Sweongyo	    ieee80211_chan2ieee(ic, c),
1506190688Sweongyo	    be32toh(reset.flags), be32toh(reset.freq));
1507190688Sweongyo	return uath_cmd_write(sc, WDCMSG_RESET, &reset, sizeof reset, 0);
1508190688Sweongyo}
1509190688Sweongyo
1510190688Sweongyostatic int
1511190688Sweongyouath_reset_tx_queues(struct uath_softc *sc)
1512190688Sweongyo{
1513190688Sweongyo	int ac, error;
1514190688Sweongyo
1515190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RESET, "%s: reset Tx queues\n", __func__);
1516190688Sweongyo	for (ac = 0; ac < 4; ac++) {
1517190688Sweongyo		const uint32_t qid = htobe32(ac);
1518190688Sweongyo
1519190688Sweongyo		error = uath_cmd_write(sc, WDCMSG_RELEASE_TX_QUEUE, &qid,
1520190688Sweongyo		    sizeof qid, 0);
1521190688Sweongyo		if (error != 0)
1522190688Sweongyo			break;
1523190688Sweongyo	}
1524190688Sweongyo	return (error);
1525190688Sweongyo}
1526190688Sweongyo
1527190688Sweongyostatic int
1528190688Sweongyouath_wme_init(struct uath_softc *sc)
1529190688Sweongyo{
1530190688Sweongyo	/* XXX get from net80211 */
1531190688Sweongyo	static const struct uath_wme_settings uath_wme_11g[4] = {
1532190688Sweongyo		{ 7, 4, 10,  0, 0 },	/* Background */
1533190688Sweongyo		{ 3, 4, 10,  0, 0 },	/* Best-Effort */
1534190688Sweongyo		{ 3, 3,  4, 26, 0 },	/* Video */
1535190688Sweongyo		{ 2, 2,  3, 47, 0 }	/* Voice */
1536190688Sweongyo	};
1537190688Sweongyo	struct uath_cmd_txq_setup qinfo;
1538190688Sweongyo	int ac, error;
1539190688Sweongyo
1540190688Sweongyo	DPRINTF(sc, UATH_DEBUG_WME, "%s: setup Tx queues\n", __func__);
1541190688Sweongyo	for (ac = 0; ac < 4; ac++) {
1542190688Sweongyo		qinfo.qid		= htobe32(ac);
1543190688Sweongyo		qinfo.len		= htobe32(sizeof(qinfo.attr));
1544190688Sweongyo		qinfo.attr.priority	= htobe32(ac);	/* XXX */
1545190688Sweongyo		qinfo.attr.aifs		= htobe32(uath_wme_11g[ac].aifsn);
1546190688Sweongyo		qinfo.attr.logcwmin	= htobe32(uath_wme_11g[ac].logcwmin);
1547190688Sweongyo		qinfo.attr.logcwmax	= htobe32(uath_wme_11g[ac].logcwmax);
1548190688Sweongyo		qinfo.attr.bursttime	= htobe32(UATH_TXOP_TO_US(
1549190688Sweongyo					    uath_wme_11g[ac].txop));
1550190688Sweongyo		qinfo.attr.mode		= htobe32(uath_wme_11g[ac].acm);/*XXX? */
1551190688Sweongyo		qinfo.attr.qflags	= htobe32(1);	/* XXX? */
1552190688Sweongyo
1553190688Sweongyo		error = uath_cmd_write(sc, WDCMSG_SETUP_TX_QUEUE, &qinfo,
1554190688Sweongyo		    sizeof qinfo, 0);
1555190688Sweongyo		if (error != 0)
1556190688Sweongyo			break;
1557190688Sweongyo	}
1558190688Sweongyo	return (error);
1559190688Sweongyo}
1560190688Sweongyo
1561190688Sweongyostatic int
1562190688Sweongyouath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1563190688Sweongyo{
1564190688Sweongyo	struct ieee80211com *ic = ifp->if_l2com;
1565190688Sweongyo	struct ifreq *ifr = (struct ifreq *) data;
1566190688Sweongyo	int error = 0, startall = 0;
1567190688Sweongyo
1568190688Sweongyo	switch (cmd) {
1569190688Sweongyo	case SIOCSIFFLAGS:
1570190688Sweongyo		if (ifp->if_flags & IFF_UP) {
1571190688Sweongyo			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1572190688Sweongyo				uath_init(ifp->if_softc);
1573190688Sweongyo				startall = 1;
1574190688Sweongyo			}
1575190688Sweongyo		} else {
1576190688Sweongyo			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1577190688Sweongyo				uath_stop(ifp);
1578190688Sweongyo		}
1579190688Sweongyo		if (startall)
1580190688Sweongyo			ieee80211_start_all(ic);
1581190688Sweongyo		break;
1582190688Sweongyo	case SIOCGIFMEDIA:
1583190688Sweongyo		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1584190688Sweongyo		break;
1585190688Sweongyo	case SIOCGIFADDR:
1586190688Sweongyo		error = ether_ioctl(ifp, cmd, data);
1587190688Sweongyo		break;
1588190688Sweongyo	default:
1589190688Sweongyo		error = EINVAL;
1590190688Sweongyo		break;
1591190688Sweongyo	}
1592190688Sweongyo
1593190688Sweongyo	return (error);
1594190688Sweongyo}
1595190688Sweongyo
1596190688Sweongyostatic int
1597190688Sweongyouath_tx_start(struct uath_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1598190688Sweongyo    struct uath_data *data)
1599190688Sweongyo{
1600191746Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1601190688Sweongyo	struct uath_chunk *chunk;
1602190688Sweongyo	struct uath_tx_desc *desc;
1603190688Sweongyo	const struct ieee80211_frame *wh;
1604190688Sweongyo	struct ieee80211_key *k;
1605190688Sweongyo	int framelen, msglen;
1606190688Sweongyo
1607190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1608190688Sweongyo
1609190688Sweongyo	data->ni = ni;
1610190688Sweongyo	data->m = m0;
1611190688Sweongyo	chunk = (struct uath_chunk *)data->buf;
1612190688Sweongyo	desc = (struct uath_tx_desc *)(chunk + 1);
1613190688Sweongyo
1614192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1615190688Sweongyo		struct uath_tx_radiotap_header *tap = &sc->sc_txtap;
1616190688Sweongyo
1617190688Sweongyo		tap->wt_flags = 0;
1618190688Sweongyo		if (m0->m_flags & M_FRAG)
1619190688Sweongyo			tap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1620190688Sweongyo
1621192468Ssam		ieee80211_radiotap_tx(vap, m0);
1622190688Sweongyo	}
1623190688Sweongyo
1624190688Sweongyo	wh = mtod(m0, struct ieee80211_frame *);
1625190688Sweongyo	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1626190688Sweongyo		k = ieee80211_crypto_encap(ni, m0);
1627190688Sweongyo		if (k == NULL) {
1628190688Sweongyo			m_freem(m0);
1629190688Sweongyo			return (ENOBUFS);
1630190688Sweongyo		}
1631190688Sweongyo
1632190688Sweongyo		/* packet header may have moved, reset our local pointer */
1633190688Sweongyo		wh = mtod(m0, struct ieee80211_frame *);
1634190688Sweongyo	}
1635190688Sweongyo	m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(desc + 1));
1636190688Sweongyo
1637190688Sweongyo	framelen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
1638190688Sweongyo	msglen = framelen + sizeof (struct uath_tx_desc);
1639190688Sweongyo	data->buflen = msglen + sizeof (struct uath_chunk);
1640190688Sweongyo
1641190688Sweongyo	/* one chunk only for now */
1642190688Sweongyo	chunk->seqnum = sc->sc_seqnum++;
1643190688Sweongyo	chunk->flags = (m0->m_flags & M_FRAG) ? 0 : UATH_CFLAGS_FINAL;
1644190688Sweongyo	if (m0->m_flags & M_LASTFRAG)
1645190688Sweongyo		chunk->flags |= UATH_CFLAGS_FINAL;
1646190688Sweongyo	chunk->flags = UATH_CFLAGS_FINAL;
1647190688Sweongyo	chunk->length = htobe16(msglen);
1648190688Sweongyo
1649190688Sweongyo	/* fill Tx descriptor */
1650190688Sweongyo	desc->msglen = htobe32(msglen);
1651190688Sweongyo	/* NB: to get UATH_TX_NOTIFY reply, `msgid' must be larger than 0  */
1652190688Sweongyo	desc->msgid  = (sc->sc_msgid++) + 1; /* don't care about endianness */
1653190688Sweongyo	desc->type   = htobe32(WDCMSG_SEND);
1654190688Sweongyo	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1655190688Sweongyo	case IEEE80211_FC0_TYPE_CTL:
1656190688Sweongyo	case IEEE80211_FC0_TYPE_MGT:
1657190688Sweongyo		/* NB: force all management frames to highest queue */
1658190688Sweongyo		if (ni->ni_flags & IEEE80211_NODE_QOS) {
1659190688Sweongyo			/* NB: force all management frames to highest queue */
1660190688Sweongyo			desc->txqid = htobe32(WME_AC_VO | UATH_TXQID_MINRATE);
1661190688Sweongyo		} else
1662190688Sweongyo			desc->txqid = htobe32(WME_AC_BE | UATH_TXQID_MINRATE);
1663190688Sweongyo		break;
1664190688Sweongyo	case IEEE80211_FC0_TYPE_DATA:
1665190688Sweongyo		/* XXX multicast frames should honor mcastrate */
1666190688Sweongyo		desc->txqid = htobe32(M_WME_GETAC(m0));
1667190688Sweongyo		break;
1668190688Sweongyo	default:
1669190688Sweongyo		device_printf(sc->sc_dev, "bogus frame type 0x%x (%s)\n",
1670190688Sweongyo			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1671190688Sweongyo		m_freem(m0);
1672190688Sweongyo		return (EIO);
1673190688Sweongyo	}
1674191746Sthompsa	if (vap->iv_state == IEEE80211_S_AUTH ||
1675191746Sthompsa	    vap->iv_state == IEEE80211_S_ASSOC ||
1676191746Sthompsa	    vap->iv_state == IEEE80211_S_RUN)
1677190688Sweongyo		desc->connid = htobe32(UATH_ID_BSS);
1678190688Sweongyo	else
1679190688Sweongyo		desc->connid = htobe32(UATH_ID_INVALID);
1680190688Sweongyo	desc->flags  = htobe32(0 /* no UATH_TX_NOTIFY */);
1681190688Sweongyo	desc->buflen = htobe32(m0->m_pkthdr.len);
1682190688Sweongyo
1683190688Sweongyo#ifdef UATH_DEBUG
1684190688Sweongyo	DPRINTF(sc, UATH_DEBUG_XMIT,
1685190688Sweongyo	    "send frame ix %u framelen %d msglen %d connid 0x%x txqid 0x%x\n",
1686190688Sweongyo	    desc->msgid, framelen, msglen, be32toh(desc->connid),
1687190688Sweongyo	    be32toh(desc->txqid));
1688190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_XMIT_DUMP)
1689190688Sweongyo		uath_dump_cmd(data->buf, data->buflen, '+');
1690190688Sweongyo#endif
1691190688Sweongyo
1692190688Sweongyo	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1693190688Sweongyo	UATH_STAT_INC(sc, st_tx_pending);
1694194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_BULK_TX]);
1695190688Sweongyo
1696190688Sweongyo	return (0);
1697190688Sweongyo}
1698190688Sweongyo
1699190688Sweongyo/*
1700190688Sweongyo * Cleanup driver resources when we run out of buffers while processing
1701190688Sweongyo * fragments; return the tx buffers allocated and drop node references.
1702190688Sweongyo */
1703190688Sweongyostatic void
1704190688Sweongyouath_txfrag_cleanup(struct uath_softc *sc,
1705190688Sweongyo    uath_datahead *frags, struct ieee80211_node *ni)
1706190688Sweongyo{
1707190688Sweongyo	struct uath_data *bf, *next;
1708190688Sweongyo
1709190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1710190688Sweongyo
1711190688Sweongyo	STAILQ_FOREACH_SAFE(bf, frags, next, next) {
1712190688Sweongyo		/* NB: bf assumed clean */
1713190688Sweongyo		STAILQ_REMOVE_HEAD(frags, next);
1714190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1715190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
1716190688Sweongyo		ieee80211_node_decref(ni);
1717190688Sweongyo	}
1718190688Sweongyo}
1719190688Sweongyo
1720190688Sweongyo/*
1721190688Sweongyo * Setup xmit of a fragmented frame.  Allocate a buffer for each frag and bump
1722190688Sweongyo * the node reference count to reflect the held reference to be setup by
1723190688Sweongyo * uath_tx_start.
1724190688Sweongyo */
1725190688Sweongyostatic int
1726190688Sweongyouath_txfrag_setup(struct uath_softc *sc, uath_datahead *frags,
1727190688Sweongyo    struct mbuf *m0, struct ieee80211_node *ni)
1728190688Sweongyo{
1729190688Sweongyo	struct mbuf *m;
1730190688Sweongyo	struct uath_data *bf;
1731190688Sweongyo
1732190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1733190688Sweongyo	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
1734190688Sweongyo		bf = uath_getbuf(sc);
1735190688Sweongyo		if (bf == NULL) {       /* out of buffers, cleanup */
1736190688Sweongyo			uath_txfrag_cleanup(sc, frags, ni);
1737190688Sweongyo			break;
1738190688Sweongyo		}
1739190688Sweongyo		ieee80211_node_incref(ni);
1740190688Sweongyo		STAILQ_INSERT_TAIL(frags, bf, next);
1741190688Sweongyo	}
1742190688Sweongyo
1743190688Sweongyo	return !STAILQ_EMPTY(frags);
1744190688Sweongyo}
1745190688Sweongyo
1746190688Sweongyo/*
1747190688Sweongyo * Reclaim mbuf resources.  For fragmented frames we need to claim each frag
1748190688Sweongyo * chained with m_nextpkt.
1749190688Sweongyo */
1750190688Sweongyostatic void
1751190688Sweongyouath_freetx(struct mbuf *m)
1752190688Sweongyo{
1753190688Sweongyo	struct mbuf *next;
1754190688Sweongyo
1755190688Sweongyo	do {
1756190688Sweongyo		next = m->m_nextpkt;
1757190688Sweongyo		m->m_nextpkt = NULL;
1758190688Sweongyo		m_freem(m);
1759190688Sweongyo	} while ((m = next) != NULL);
1760190688Sweongyo}
1761190688Sweongyo
1762190688Sweongyostatic void
1763190688Sweongyouath_start(struct ifnet *ifp)
1764190688Sweongyo{
1765190688Sweongyo	struct uath_data *bf;
1766190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1767190688Sweongyo	struct ieee80211_node *ni;
1768190688Sweongyo	struct mbuf *m, *next;
1769190688Sweongyo	uath_datahead frags;
1770190688Sweongyo
1771190688Sweongyo	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1772190688Sweongyo	    (sc->sc_flags & UATH_FLAG_INVALID))
1773190688Sweongyo		return;
1774190688Sweongyo
1775190688Sweongyo	UATH_LOCK(sc);
1776190688Sweongyo	for (;;) {
1777190688Sweongyo		bf = uath_getbuf(sc);
1778190688Sweongyo		if (bf == NULL)
1779190688Sweongyo			break;
1780190688Sweongyo
1781190688Sweongyo		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1782190688Sweongyo		if (m == NULL) {
1783190688Sweongyo			STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1784190688Sweongyo			UATH_STAT_INC(sc, st_tx_inactive);
1785190688Sweongyo			break;
1786190688Sweongyo		}
1787190688Sweongyo		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1788190688Sweongyo		m->m_pkthdr.rcvif = NULL;
1789190688Sweongyo
1790190688Sweongyo		/*
1791190688Sweongyo		 * Check for fragmentation.  If this frame has been broken up
1792190688Sweongyo		 * verify we have enough buffers to send all the fragments
1793190688Sweongyo		 * so all go out or none...
1794190688Sweongyo		 */
1795190688Sweongyo		STAILQ_INIT(&frags);
1796190688Sweongyo		if ((m->m_flags & M_FRAG) &&
1797190688Sweongyo		    !uath_txfrag_setup(sc, &frags, m, ni)) {
1798190688Sweongyo			DPRINTF(sc, UATH_DEBUG_XMIT,
1799190688Sweongyo			    "%s: out of txfrag buffers\n", __func__);
1800190688Sweongyo			uath_freetx(m);
1801190688Sweongyo			goto bad;
1802190688Sweongyo		}
1803190688Sweongyo		sc->sc_seqnum = 0;
1804190688Sweongyo	nextfrag:
1805190688Sweongyo		/*
1806190688Sweongyo		 * Pass the frame to the h/w for transmission.
1807190688Sweongyo		 * Fragmented frames have each frag chained together
1808190688Sweongyo		 * with m_nextpkt.  We know there are sufficient uath_data's
1809190688Sweongyo		 * to send all the frags because of work done by
1810190688Sweongyo		 * uath_txfrag_setup.
1811190688Sweongyo		 */
1812190688Sweongyo		next = m->m_nextpkt;
1813190688Sweongyo		if (uath_tx_start(sc, m, ni, bf) != 0) {
1814190688Sweongyo	bad:
1815190688Sweongyo			ifp->if_oerrors++;
1816190688Sweongyo	reclaim:
1817190688Sweongyo			STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1818190688Sweongyo			UATH_STAT_INC(sc, st_tx_inactive);
1819190688Sweongyo			uath_txfrag_cleanup(sc, &frags, ni);
1820190688Sweongyo			ieee80211_free_node(ni);
1821190688Sweongyo			continue;
1822190688Sweongyo		}
1823190688Sweongyo
1824190688Sweongyo		if (next != NULL) {
1825190688Sweongyo			/*
1826190688Sweongyo			 * Beware of state changing between frags.
1827190688Sweongyo			 XXX check sta power-save state?
1828190688Sweongyo			*/
1829190688Sweongyo			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
1830190688Sweongyo				DPRINTF(sc, UATH_DEBUG_XMIT,
1831190688Sweongyo				    "%s: flush fragmented packet, state %s\n",
1832190688Sweongyo				    __func__,
1833190688Sweongyo				    ieee80211_state_name[ni->ni_vap->iv_state]);
1834190688Sweongyo				uath_freetx(next);
1835190688Sweongyo				goto reclaim;
1836190688Sweongyo			}
1837190688Sweongyo			m = next;
1838190688Sweongyo			bf = STAILQ_FIRST(&frags);
1839190688Sweongyo			KASSERT(bf != NULL, ("no buf for txfrag"));
1840190688Sweongyo			STAILQ_REMOVE_HEAD(&frags, next);
1841190688Sweongyo			goto nextfrag;
1842190688Sweongyo		}
1843190688Sweongyo
1844190688Sweongyo		sc->sc_tx_timer = 5;
1845190688Sweongyo	}
1846190688Sweongyo	UATH_UNLOCK(sc);
1847190688Sweongyo}
1848190688Sweongyo
1849190688Sweongyostatic int
1850190688Sweongyouath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1851190688Sweongyo    const struct ieee80211_bpf_params *params)
1852190688Sweongyo{
1853190688Sweongyo	struct ieee80211com *ic = ni->ni_ic;
1854190688Sweongyo	struct ifnet *ifp = ic->ic_ifp;
1855190688Sweongyo	struct uath_data *bf;
1856190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1857190688Sweongyo
1858190688Sweongyo	/* prevent management frames from being sent if we're not ready */
1859194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1860194329Sweongyo	    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1861190688Sweongyo		m_freem(m);
1862190688Sweongyo		ieee80211_free_node(ni);
1863190688Sweongyo		return (ENETDOWN);
1864190688Sweongyo	}
1865190688Sweongyo
1866190688Sweongyo	UATH_LOCK(sc);
1867190688Sweongyo	/* grab a TX buffer  */
1868190688Sweongyo	bf = uath_getbuf(sc);
1869190688Sweongyo	if (bf == NULL) {
1870190688Sweongyo		ieee80211_free_node(ni);
1871190688Sweongyo		m_freem(m);
1872190688Sweongyo		UATH_UNLOCK(sc);
1873190688Sweongyo		return (ENOBUFS);
1874190688Sweongyo	}
1875190688Sweongyo
1876190688Sweongyo	sc->sc_seqnum = 0;
1877190688Sweongyo	if (uath_tx_start(sc, m, ni, bf) != 0) {
1878190688Sweongyo		ieee80211_free_node(ni);
1879190688Sweongyo		ifp->if_oerrors++;
1880190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1881190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
1882190688Sweongyo		UATH_UNLOCK(sc);
1883190688Sweongyo		return (EIO);
1884190688Sweongyo	}
1885190688Sweongyo	UATH_UNLOCK(sc);
1886190688Sweongyo
1887190688Sweongyo	sc->sc_tx_timer = 5;
1888190688Sweongyo	return (0);
1889190688Sweongyo}
1890190688Sweongyo
1891190688Sweongyostatic void
1892190688Sweongyouath_scan_start(struct ieee80211com *ic)
1893190688Sweongyo{
1894190688Sweongyo	/* do nothing  */
1895190688Sweongyo}
1896190688Sweongyo
1897190688Sweongyostatic void
1898190688Sweongyouath_scan_end(struct ieee80211com *ic)
1899190688Sweongyo{
1900190688Sweongyo	/* do nothing  */
1901190688Sweongyo}
1902190688Sweongyo
1903190688Sweongyostatic void
1904190688Sweongyouath_set_channel(struct ieee80211com *ic)
1905190688Sweongyo{
1906190688Sweongyo	struct ifnet *ifp = ic->ic_ifp;
1907190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1908190688Sweongyo
1909190688Sweongyo	UATH_LOCK(sc);
1910194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1911194329Sweongyo	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1912194329Sweongyo		UATH_UNLOCK(sc);
1913194329Sweongyo		return;
1914194329Sweongyo	}
1915190688Sweongyo	(void)uath_switch_channel(sc, ic->ic_curchan);
1916190688Sweongyo	UATH_UNLOCK(sc);
1917190688Sweongyo}
1918190688Sweongyo
1919190688Sweongyostatic int
1920190688Sweongyouath_set_rxmulti_filter(struct uath_softc *sc)
1921190688Sweongyo{
1922192468Ssam	/* XXX broken */
1923190688Sweongyo	return (0);
1924190688Sweongyo}
1925190688Sweongyostatic void
1926190688Sweongyouath_update_mcast(struct ifnet *ifp)
1927190688Sweongyo{
1928190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1929190688Sweongyo
1930192468Ssam	UATH_LOCK(sc);
1931194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1932194329Sweongyo	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1933194329Sweongyo		UATH_UNLOCK(sc);
1934194329Sweongyo		return;
1935194329Sweongyo	}
1936190688Sweongyo	/*
1937190688Sweongyo	 * this is for avoiding the race condition when we're try to
1938190688Sweongyo	 * connect to the AP with WPA.
1939190688Sweongyo	 */
1940192468Ssam	if (sc->sc_flags & UATH_FLAG_INITDONE)
1941192468Ssam		(void)uath_set_rxmulti_filter(sc);
1942192468Ssam	UATH_UNLOCK(sc);
1943190688Sweongyo}
1944190688Sweongyo
1945190688Sweongyostatic void
1946190688Sweongyouath_update_promisc(struct ifnet *ifp)
1947190688Sweongyo{
1948190688Sweongyo	struct uath_softc *sc = ifp->if_softc;
1949190688Sweongyo
1950192468Ssam	UATH_LOCK(sc);
1951194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1952194329Sweongyo	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1953194329Sweongyo		UATH_UNLOCK(sc);
1954194329Sweongyo		return;
1955194329Sweongyo	}
1956192468Ssam	if (sc->sc_flags & UATH_FLAG_INITDONE) {
1957192468Ssam		uath_set_rxfilter(sc,
1958192468Ssam		    UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
1959192468Ssam		    UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON |
1960192468Ssam		    UATH_FILTER_RX_PROM, UATH_FILTER_OP_SET);
1961192468Ssam	}
1962192468Ssam	UATH_UNLOCK(sc);
1963190688Sweongyo}
1964190688Sweongyo
1965190688Sweongyostatic int
1966190688Sweongyouath_create_connection(struct uath_softc *sc, uint32_t connid)
1967190688Sweongyo{
1968190688Sweongyo	const struct ieee80211_rateset *rs;
1969190688Sweongyo	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1970190688Sweongyo	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1971190688Sweongyo	struct ieee80211_node *ni = vap->iv_bss;
1972190688Sweongyo	struct uath_cmd_create_connection create;
1973190688Sweongyo
1974190688Sweongyo	bzero(&create, sizeof create);
1975190688Sweongyo	create.connid = htobe32(connid);
1976190688Sweongyo	create.bssid = htobe32(0);
1977190688Sweongyo	/* XXX packed or not?  */
1978190688Sweongyo	create.size = htobe32(sizeof(struct uath_cmd_rateset));
1979190688Sweongyo
1980190688Sweongyo	rs = &ni->ni_rates;
1981190688Sweongyo	create.connattr.rateset.length = rs->rs_nrates;
1982190688Sweongyo	bcopy(rs->rs_rates, &create.connattr.rateset.set[0],
1983190688Sweongyo	    rs->rs_nrates);
1984190688Sweongyo
1985190688Sweongyo	/* XXX turbo */
1986190688Sweongyo	if (IEEE80211_IS_CHAN_A(ni->ni_chan))
1987190688Sweongyo		create.connattr.wlanmode = htobe32(WLAN_MODE_11a);
1988190688Sweongyo	else if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan))
1989190688Sweongyo		create.connattr.wlanmode = htobe32(WLAN_MODE_11g);
1990190688Sweongyo	else
1991190688Sweongyo		create.connattr.wlanmode = htobe32(WLAN_MODE_11b);
1992190688Sweongyo
1993190688Sweongyo	return uath_cmd_write(sc, WDCMSG_CREATE_CONNECTION, &create,
1994190688Sweongyo	    sizeof create, 0);
1995190688Sweongyo}
1996190688Sweongyo
1997190688Sweongyostatic int
1998190688Sweongyouath_set_rates(struct uath_softc *sc, const struct ieee80211_rateset *rs)
1999190688Sweongyo{
2000190688Sweongyo	struct uath_cmd_rates rates;
2001190688Sweongyo
2002190688Sweongyo	bzero(&rates, sizeof rates);
2003190688Sweongyo	rates.connid = htobe32(UATH_ID_BSS);		/* XXX */
2004190688Sweongyo	rates.size   = htobe32(sizeof(struct uath_cmd_rateset));
2005190688Sweongyo	/* XXX bounds check rs->rs_nrates */
2006190688Sweongyo	rates.rateset.length = rs->rs_nrates;
2007190688Sweongyo	bcopy(rs->rs_rates, &rates.rateset.set[0], rs->rs_nrates);
2008190688Sweongyo
2009190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RATES,
2010190688Sweongyo	    "setting supported rates nrates=%d\n", rs->rs_nrates);
2011190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_BASIC_RATE,
2012190688Sweongyo	    &rates, sizeof rates, 0);
2013190688Sweongyo}
2014190688Sweongyo
2015190688Sweongyostatic int
2016190688Sweongyouath_write_associd(struct uath_softc *sc)
2017190688Sweongyo{
2018190688Sweongyo	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2019190688Sweongyo	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2020190688Sweongyo	struct ieee80211_node *ni = vap->iv_bss;
2021190688Sweongyo	struct uath_cmd_set_associd associd;
2022190688Sweongyo
2023190688Sweongyo	bzero(&associd, sizeof associd);
2024190688Sweongyo	associd.defaultrateix = htobe32(1);	/* XXX */
2025190688Sweongyo	associd.associd = htobe32(ni->ni_associd);
2026190688Sweongyo	associd.timoffset = htobe32(0x3b);	/* XXX */
2027190688Sweongyo	IEEE80211_ADDR_COPY(associd.bssid, ni->ni_bssid);
2028190688Sweongyo	return uath_cmd_write(sc, WDCMSG_WRITE_ASSOCID, &associd,
2029190688Sweongyo	    sizeof associd, 0);
2030190688Sweongyo}
2031190688Sweongyo
2032190688Sweongyostatic int
2033190688Sweongyouath_set_ledsteady(struct uath_softc *sc, int lednum, int ledmode)
2034190688Sweongyo{
2035190688Sweongyo	struct uath_cmd_ledsteady led;
2036190688Sweongyo
2037190688Sweongyo	led.lednum = htobe32(lednum);
2038190688Sweongyo	led.ledmode = htobe32(ledmode);
2039190688Sweongyo
2040190688Sweongyo	DPRINTF(sc, UATH_DEBUG_LED, "set %s led %s (steady)\n",
2041190688Sweongyo	    (lednum == UATH_LED_LINK) ? "link" : "activity",
2042190688Sweongyo	    ledmode ? "on" : "off");
2043190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_LED_STEADY, &led, sizeof led, 0);
2044190688Sweongyo}
2045190688Sweongyo
2046190688Sweongyostatic int
2047190688Sweongyouath_set_ledblink(struct uath_softc *sc, int lednum, int ledmode,
2048190688Sweongyo	int blinkrate, int slowmode)
2049190688Sweongyo{
2050190688Sweongyo	struct uath_cmd_ledblink led;
2051190688Sweongyo
2052190688Sweongyo	led.lednum = htobe32(lednum);
2053190688Sweongyo	led.ledmode = htobe32(ledmode);
2054190688Sweongyo	led.blinkrate = htobe32(blinkrate);
2055190688Sweongyo	led.slowmode = htobe32(slowmode);
2056190688Sweongyo
2057190688Sweongyo	DPRINTF(sc, UATH_DEBUG_LED, "set %s led %s (blink)\n",
2058190688Sweongyo	    (lednum == UATH_LED_LINK) ? "link" : "activity",
2059190688Sweongyo	    ledmode ? "on" : "off");
2060190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_LED_BLINK, &led, sizeof led, 0);
2061190688Sweongyo}
2062190688Sweongyo
2063190688Sweongyostatic int
2064190688Sweongyouath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2065190688Sweongyo{
2066190688Sweongyo	enum ieee80211_state ostate = vap->iv_state;
2067190688Sweongyo	int error;
2068190688Sweongyo	struct ieee80211_node *ni = vap->iv_bss;
2069190688Sweongyo	struct ieee80211com *ic = vap->iv_ic;
2070190688Sweongyo	struct uath_softc *sc = ic->ic_ifp->if_softc;
2071190688Sweongyo	struct uath_vap *uvp = UATH_VAP(vap);
2072190688Sweongyo
2073190688Sweongyo	DPRINTF(sc, UATH_DEBUG_STATE,
2074190688Sweongyo	    "%s: %s -> %s\n", __func__, ieee80211_state_name[vap->iv_state],
2075190688Sweongyo	    ieee80211_state_name[nstate]);
2076190688Sweongyo
2077191746Sthompsa	IEEE80211_UNLOCK(ic);
2078190688Sweongyo	UATH_LOCK(sc);
2079190688Sweongyo	callout_stop(&sc->stat_ch);
2080190688Sweongyo	callout_stop(&sc->watchdog_ch);
2081190688Sweongyo
2082190688Sweongyo	switch (nstate) {
2083190688Sweongyo	case IEEE80211_S_INIT:
2084190688Sweongyo		if (ostate == IEEE80211_S_RUN) {
2085190688Sweongyo			/* turn link and activity LEDs off */
2086190688Sweongyo			uath_set_ledstate(sc, 0);
2087190688Sweongyo		}
2088190688Sweongyo		break;
2089190688Sweongyo
2090190688Sweongyo	case IEEE80211_S_SCAN:
2091190688Sweongyo		break;
2092190688Sweongyo
2093190688Sweongyo	case IEEE80211_S_AUTH:
2094190688Sweongyo		/* XXX good place?  set RTS threshold  */
2095190688Sweongyo		uath_config(sc, CFG_USER_RTS_THRESHOLD, vap->iv_rtsthreshold);
2096190688Sweongyo		/* XXX bad place  */
2097190688Sweongyo		error = uath_set_keys(sc, vap);
2098190688Sweongyo		if (error != 0) {
2099190688Sweongyo			device_printf(sc->sc_dev,
2100190688Sweongyo			    "could not set crypto keys, error %d\n", error);
2101190688Sweongyo			break;
2102190688Sweongyo		}
2103190688Sweongyo		if (uath_switch_channel(sc, ni->ni_chan) != 0) {
2104190688Sweongyo			device_printf(sc->sc_dev, "could not switch channel\n");
2105190688Sweongyo			break;
2106190688Sweongyo		}
2107190688Sweongyo		if (uath_create_connection(sc, UATH_ID_BSS) != 0) {
2108190688Sweongyo			device_printf(sc->sc_dev,
2109190688Sweongyo			    "could not create connection\n");
2110190688Sweongyo			break;
2111190688Sweongyo		}
2112190688Sweongyo		break;
2113190688Sweongyo
2114190688Sweongyo	case IEEE80211_S_ASSOC:
2115190688Sweongyo		if (uath_set_rates(sc, &ni->ni_rates) != 0) {
2116190688Sweongyo			device_printf(sc->sc_dev,
2117190688Sweongyo			    "could not set negotiated rate set\n");
2118190688Sweongyo			break;
2119190688Sweongyo		}
2120190688Sweongyo		break;
2121190688Sweongyo
2122190688Sweongyo	case IEEE80211_S_RUN:
2123190688Sweongyo		/* XXX monitor mode doesn't be tested  */
2124190688Sweongyo		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2125190688Sweongyo			uath_set_ledstate(sc, 1);
2126190688Sweongyo			break;
2127190688Sweongyo		}
2128190688Sweongyo
2129190688Sweongyo		/*
2130190688Sweongyo		 * Tx rate is controlled by firmware, report the maximum
2131190688Sweongyo		 * negotiated rate in ifconfig output.
2132190688Sweongyo		 */
2133190688Sweongyo		ni->ni_txrate = ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates-1];
2134190688Sweongyo
2135190688Sweongyo		if (uath_write_associd(sc) != 0) {
2136190688Sweongyo			device_printf(sc->sc_dev,
2137190688Sweongyo			    "could not write association id\n");
2138190688Sweongyo			break;
2139190688Sweongyo		}
2140190688Sweongyo		/* turn link LED on */
2141190688Sweongyo		uath_set_ledsteady(sc, UATH_LED_LINK, UATH_LED_ON);
2142190688Sweongyo		/* make activity LED blink */
2143190688Sweongyo		uath_set_ledblink(sc, UATH_LED_ACTIVITY, UATH_LED_ON, 1, 2);
2144190688Sweongyo		/* set state to associated */
2145190688Sweongyo		uath_set_ledstate(sc, 1);
2146190688Sweongyo
2147190688Sweongyo		/* start statistics timer */
2148190688Sweongyo		callout_reset(&sc->stat_ch, hz, uath_stat, sc);
2149190688Sweongyo		break;
2150190688Sweongyo	default:
2151190688Sweongyo		break;
2152190688Sweongyo	}
2153190688Sweongyo	UATH_UNLOCK(sc);
2154190688Sweongyo	IEEE80211_LOCK(ic);
2155191746Sthompsa	return (uvp->newstate(vap, nstate, arg));
2156190688Sweongyo}
2157190688Sweongyo
2158190688Sweongyostatic int
2159190688Sweongyouath_set_key(struct uath_softc *sc, const struct ieee80211_key *wk,
2160190688Sweongyo    int index)
2161190688Sweongyo{
2162190688Sweongyo#if 0
2163190688Sweongyo	struct uath_cmd_crypto crypto;
2164190688Sweongyo	int i;
2165190688Sweongyo
2166190688Sweongyo	bzero(&crypto, sizeof crypto);
2167190688Sweongyo	crypto.keyidx = htobe32(index);
2168190688Sweongyo	crypto.magic1 = htobe32(1);
2169190688Sweongyo	crypto.size   = htobe32(368);
2170190688Sweongyo	crypto.mask   = htobe32(0xffff);
2171190688Sweongyo	crypto.flags  = htobe32(0x80000068);
2172190688Sweongyo	if (index != UATH_DEFAULT_KEY)
2173190688Sweongyo		crypto.flags |= htobe32(index << 16);
2174190688Sweongyo	memset(crypto.magic2, 0xff, sizeof crypto.magic2);
2175190688Sweongyo
2176190688Sweongyo	/*
2177190688Sweongyo	 * Each byte of the key must be XOR'ed with 10101010 before being
2178190688Sweongyo	 * transmitted to the firmware.
2179190688Sweongyo	 */
2180190688Sweongyo	for (i = 0; i < wk->wk_keylen; i++)
2181190688Sweongyo		crypto.key[i] = wk->wk_key[i] ^ 0xaa;
2182190688Sweongyo
2183190688Sweongyo	DPRINTF(sc, UATH_DEBUG_CRYPTO,
2184190688Sweongyo	    "setting crypto key index=%d len=%d\n", index, wk->wk_keylen);
2185190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_KEY_CACHE_ENTRY, &crypto,
2186190688Sweongyo	    sizeof crypto, 0);
2187190688Sweongyo#else
2188190688Sweongyo	/* XXX support H/W cryto  */
2189190688Sweongyo	return (0);
2190190688Sweongyo#endif
2191190688Sweongyo}
2192190688Sweongyo
2193190688Sweongyostatic int
2194190688Sweongyouath_set_keys(struct uath_softc *sc, struct ieee80211vap *vap)
2195190688Sweongyo{
2196190688Sweongyo	int i, error;
2197190688Sweongyo
2198190688Sweongyo	error = 0;
2199190688Sweongyo	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2200190688Sweongyo		const struct ieee80211_key *wk = &vap->iv_nw_keys[i];
2201190688Sweongyo
2202190688Sweongyo		if (wk->wk_flags & (IEEE80211_KEY_XMIT|IEEE80211_KEY_RECV)) {
2203190688Sweongyo			error = uath_set_key(sc, wk, i);
2204190688Sweongyo			if (error)
2205190688Sweongyo				return (error);
2206190688Sweongyo		}
2207190688Sweongyo	}
2208190688Sweongyo	if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2209190688Sweongyo		error = uath_set_key(sc, &vap->iv_nw_keys[vap->iv_def_txkey],
2210190688Sweongyo			UATH_DEFAULT_KEY);
2211190688Sweongyo	}
2212190688Sweongyo	return (error);
2213190688Sweongyo}
2214190688Sweongyo
2215190688Sweongyo#define	UATH_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2216190688Sweongyo	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2217190688Sweongyo
2218190688Sweongyostatic void
2219190688Sweongyouath_sysctl_node(struct uath_softc *sc)
2220190688Sweongyo{
2221190688Sweongyo	struct sysctl_ctx_list *ctx;
2222190688Sweongyo	struct sysctl_oid_list *child;
2223190688Sweongyo	struct sysctl_oid *tree;
2224190688Sweongyo	struct uath_stat *stats;
2225190688Sweongyo
2226190688Sweongyo	stats = &sc->sc_stat;
2227190688Sweongyo	ctx = device_get_sysctl_ctx(sc->sc_dev);
2228190688Sweongyo	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2229190688Sweongyo
2230190688Sweongyo	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2231190688Sweongyo	    NULL, "UATH statistics");
2232190688Sweongyo	child = SYSCTL_CHILDREN(tree);
2233190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "badchunkseqnum",
2234190688Sweongyo	    &stats->st_badchunkseqnum, "Bad chunk sequence numbers");
2235190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "invalidlen", &stats->st_invalidlen,
2236190688Sweongyo	    "Invalid length");
2237190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "multichunk", &stats->st_multichunk,
2238190688Sweongyo	    "Multi chunks");
2239190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "toobigrxpkt",
2240190688Sweongyo	    &stats->st_toobigrxpkt, "Too big rx packets");
2241190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "stopinprogress",
2242190688Sweongyo	    &stats->st_stopinprogress, "Stop in progress");
2243190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "crcerrs", &stats->st_crcerr,
2244190688Sweongyo	    "CRC errors");
2245190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "phyerr", &stats->st_phyerr,
2246190688Sweongyo	    "PHY errors");
2247190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "decrypt_crcerr",
2248190688Sweongyo	    &stats->st_decrypt_crcerr, "Decryption CRC errors");
2249190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "decrypt_micerr",
2250190688Sweongyo	    &stats->st_decrypt_micerr, "Decryption Misc errors");
2251190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "decomperr", &stats->st_decomperr,
2252190688Sweongyo	    "Decomp errors");
2253190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "keyerr", &stats->st_keyerr,
2254190688Sweongyo	    "Key errors");
2255190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "err", &stats->st_err,
2256190688Sweongyo	    "Unknown errors");
2257190688Sweongyo
2258190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_active",
2259190688Sweongyo	    &stats->st_cmd_active, "Active numbers in Command queue");
2260190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_inactive",
2261190688Sweongyo	    &stats->st_cmd_inactive, "Inactive numbers in Command queue");
2262190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_pending",
2263190688Sweongyo	    &stats->st_cmd_pending, "Pending numbers in Command queue");
2264190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_waiting",
2265190688Sweongyo	    &stats->st_cmd_waiting, "Waiting numbers in Command queue");
2266190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "rx_active",
2267190688Sweongyo	    &stats->st_rx_active, "Active numbers in RX queue");
2268190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "rx_inactive",
2269190688Sweongyo	    &stats->st_rx_inactive, "Inactive numbers in RX queue");
2270190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2271190688Sweongyo	    &stats->st_tx_active, "Active numbers in TX queue");
2272190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2273190688Sweongyo	    &stats->st_tx_inactive, "Inactive numbers in TX queue");
2274190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2275190688Sweongyo	    &stats->st_tx_pending, "Pending numbers in TX queue");
2276190688Sweongyo}
2277190688Sweongyo
2278190688Sweongyo#undef UATH_SYSCTL_STAT_ADD32
2279190688Sweongyo
2280190688Sweongyostatic void
2281190688Sweongyouath_cmdeof(struct uath_softc *sc, struct uath_cmd *cmd)
2282190688Sweongyo{
2283190688Sweongyo	struct uath_cmd_hdr *hdr;
2284190688Sweongyo	int dlen;
2285190688Sweongyo
2286190688Sweongyo	hdr = (struct uath_cmd_hdr *)cmd->buf;
2287190688Sweongyo	/* NB: msgid is passed thru w/o byte swapping */
2288190688Sweongyo#ifdef UATH_DEBUG
2289190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_CMDS) {
2290190688Sweongyo		int len = be32toh(hdr->len);
2291190688Sweongyo		printf("%s: %s [ix %u] len %u status %u\n",
2292190688Sweongyo		    __func__, uath_codename(be32toh(hdr->code)),
2293190688Sweongyo		    hdr->msgid, len, be32toh(hdr->magic));
2294190688Sweongyo		if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP)
2295190688Sweongyo			uath_dump_cmd(cmd->buf,
2296190688Sweongyo			    len > UATH_MAX_CMDSZ ? sizeof(*hdr) : len, '-');
2297190688Sweongyo	}
2298190688Sweongyo#endif
2299190688Sweongyo	hdr->code = be32toh(hdr->code);
2300190688Sweongyo	hdr->len = be32toh(hdr->len);
2301190688Sweongyo	hdr->magic = be32toh(hdr->magic);	/* target status on return */
2302190688Sweongyo
2303190688Sweongyo	switch (hdr->code & 0xff) {
2304190688Sweongyo	/* reply to a read command */
2305190688Sweongyo	default:
2306190688Sweongyo		dlen = hdr->len - sizeof(*hdr);
2307190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL,
2308190688Sweongyo		    "%s: code %d data len %u\n",
2309190688Sweongyo		    __func__, hdr->code & 0xff, dlen);
2310190688Sweongyo		/*
2311190688Sweongyo		 * The first response from the target after the
2312190688Sweongyo		 * HOST_AVAILABLE has an invalid msgid so we must
2313190688Sweongyo		 * treat it specially.
2314190688Sweongyo		 */
2315190688Sweongyo		if (hdr->msgid < UATH_CMD_LIST_COUNT) {
2316190688Sweongyo			uint32_t *rp = (uint32_t *)(hdr+1);
2317190688Sweongyo			u_int olen;
2318190688Sweongyo
2319190688Sweongyo			if (!(sizeof(*hdr) <= hdr->len &&
2320190688Sweongyo			      hdr->len < UATH_MAX_CMDSZ)) {
2321190688Sweongyo				device_printf(sc->sc_dev,
2322190688Sweongyo				    "%s: invalid WDC msg length %u; "
2323190688Sweongyo				    "msg ignored\n", __func__, hdr->len);
2324190688Sweongyo				return;
2325190688Sweongyo			}
2326190688Sweongyo			/*
2327190688Sweongyo			 * Calculate return/receive payload size; the
2328190688Sweongyo			 * first word, if present, always gives the
2329190688Sweongyo			 * number of bytes--unless it's 0 in which
2330190688Sweongyo			 * case a single 32-bit word should be present.
2331190688Sweongyo			 */
2332190688Sweongyo			if (dlen >= sizeof(uint32_t)) {
2333190688Sweongyo				olen = be32toh(rp[0]);
2334190688Sweongyo				dlen -= sizeof(uint32_t);
2335190688Sweongyo				if (olen == 0) {
2336190688Sweongyo					/* convention is 0 =>'s one word */
2337190688Sweongyo					olen = sizeof(uint32_t);
2338190688Sweongyo					/* XXX KASSERT(olen == dlen ) */
2339190688Sweongyo				}
2340190688Sweongyo			} else
2341190688Sweongyo				olen = 0;
2342190688Sweongyo			if (cmd->odata != NULL) {
2343190688Sweongyo				/* NB: cmd->olen validated in uath_cmd */
2344190688Sweongyo				if (olen > cmd->olen) {
2345190688Sweongyo					/* XXX complain? */
2346190688Sweongyo					device_printf(sc->sc_dev,
2347190688Sweongyo					    "%s: cmd 0x%x olen %u cmd olen %u\n",
2348190688Sweongyo					    __func__, hdr->code, olen,
2349190688Sweongyo					    cmd->olen);
2350190688Sweongyo					olen = cmd->olen;
2351190688Sweongyo				}
2352190688Sweongyo				if (olen > dlen) {
2353190688Sweongyo					/* XXX complain, shouldn't happen */
2354190688Sweongyo					device_printf(sc->sc_dev,
2355190688Sweongyo					    "%s: cmd 0x%x olen %u dlen %u\n",
2356190688Sweongyo					    __func__, hdr->code, olen, dlen);
2357190688Sweongyo					olen = dlen;
2358190688Sweongyo				}
2359190688Sweongyo				/* XXX have submitter do this */
2360190688Sweongyo				/* copy answer into caller's supplied buffer */
2361190688Sweongyo				bcopy(&rp[1], cmd->odata, olen);
2362190688Sweongyo				cmd->olen = olen;
2363190688Sweongyo			}
2364190688Sweongyo		}
2365190688Sweongyo		wakeup_one(cmd);		/* wake up caller */
2366190688Sweongyo		break;
2367190688Sweongyo
2368190688Sweongyo	case WDCMSG_TARGET_START:
2369190688Sweongyo		if (hdr->msgid >= UATH_CMD_LIST_COUNT) {
2370190688Sweongyo			/* XXX */
2371190688Sweongyo			return;
2372190688Sweongyo		}
2373190688Sweongyo		dlen = hdr->len - sizeof(*hdr);
2374190688Sweongyo		if (dlen != sizeof(uint32_t)) {
2375190688Sweongyo			/* XXX something wrong */
2376190688Sweongyo			return;
2377190688Sweongyo		}
2378190688Sweongyo		/* XXX have submitter do this */
2379190688Sweongyo		/* copy answer into caller's supplied buffer */
2380190688Sweongyo		bcopy(hdr+1, cmd->odata, sizeof(uint32_t));
2381190688Sweongyo		cmd->olen = sizeof(uint32_t);
2382190688Sweongyo		wakeup_one(cmd);		/* wake up caller */
2383190688Sweongyo		break;
2384190688Sweongyo
2385190688Sweongyo	case WDCMSG_SEND_COMPLETE:
2386190688Sweongyo		/* this notification is sent when UATH_TX_NOTIFY is set */
2387190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL,
2388190688Sweongyo		    "%s: received Tx notification\n", __func__);
2389190688Sweongyo		break;
2390190688Sweongyo
2391190688Sweongyo	case WDCMSG_TARGET_GET_STATS:
2392190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL,
2393190688Sweongyo		    "%s: received device statistics\n", __func__);
2394190688Sweongyo		callout_reset(&sc->stat_ch, hz, uath_stat, sc);
2395190688Sweongyo		break;
2396190688Sweongyo	}
2397190688Sweongyo}
2398190688Sweongyo
2399190688Sweongyostatic void
2400194677Sthompsauath_intr_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2401190688Sweongyo{
2402194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2403190688Sweongyo	struct uath_cmd *cmd;
2404194677Sthompsa	struct usb_page_cache *pc;
2405194677Sthompsa	int actlen;
2406190688Sweongyo
2407194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2408194677Sthompsa
2409190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2410190688Sweongyo
2411190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2412190688Sweongyo	case USB_ST_TRANSFERRED:
2413190688Sweongyo		cmd = STAILQ_FIRST(&sc->sc_cmd_waiting);
2414190688Sweongyo		if (cmd == NULL)
2415190688Sweongyo			goto setup;
2416190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_waiting, next);
2417190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_waiting);
2418190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_cmd_inactive, cmd, next);
2419190688Sweongyo		UATH_STAT_INC(sc, st_cmd_inactive);
2420190688Sweongyo
2421194677Sthompsa		KASSERT(actlen >= sizeof(struct uath_cmd_hdr),
2422190688Sweongyo		    ("short xfer error"));
2423194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2424194677Sthompsa		usbd_copy_out(pc, 0, cmd->buf, actlen);
2425190688Sweongyo		uath_cmdeof(sc, cmd);
2426190688Sweongyo	case USB_ST_SETUP:
2427190688Sweongyosetup:
2428194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2429194228Sthompsa		usbd_transfer_submit(xfer);
2430190688Sweongyo		break;
2431190688Sweongyo	default:
2432194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2433194677Sthompsa			usbd_xfer_set_stall(xfer);
2434190688Sweongyo			goto setup;
2435190688Sweongyo		}
2436190688Sweongyo		break;
2437190688Sweongyo	}
2438190688Sweongyo}
2439190688Sweongyo
2440190688Sweongyostatic void
2441194677Sthompsauath_intr_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2442190688Sweongyo{
2443194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2444190688Sweongyo	struct uath_cmd *cmd;
2445190688Sweongyo
2446190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2447190688Sweongyo
2448190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2449190688Sweongyo	case USB_ST_TRANSFERRED:
2450190688Sweongyo		cmd = STAILQ_FIRST(&sc->sc_cmd_active);
2451190688Sweongyo		if (cmd == NULL)
2452190688Sweongyo			goto setup;
2453190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_active, next);
2454190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_active);
2455190688Sweongyo		STAILQ_INSERT_TAIL((cmd->flags & UATH_CMD_FLAG_READ) ?
2456190688Sweongyo		    &sc->sc_cmd_waiting : &sc->sc_cmd_inactive, cmd, next);
2457190688Sweongyo		if (cmd->flags & UATH_CMD_FLAG_READ)
2458190688Sweongyo			UATH_STAT_INC(sc, st_cmd_waiting);
2459190688Sweongyo		else
2460190688Sweongyo			UATH_STAT_INC(sc, st_cmd_inactive);
2461190688Sweongyo		/* FALLTHROUGH */
2462190688Sweongyo	case USB_ST_SETUP:
2463190688Sweongyosetup:
2464190688Sweongyo		cmd = STAILQ_FIRST(&sc->sc_cmd_pending);
2465190688Sweongyo		if (cmd == NULL) {
2466190688Sweongyo			DPRINTF(sc, UATH_DEBUG_XMIT, "%s: empty pending queue\n",
2467190688Sweongyo			    __func__);
2468190688Sweongyo			return;
2469190688Sweongyo		}
2470190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_pending, next);
2471190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_pending);
2472190688Sweongyo		STAILQ_INSERT_TAIL((cmd->flags & UATH_CMD_FLAG_ASYNC) ?
2473190688Sweongyo		    &sc->sc_cmd_inactive : &sc->sc_cmd_active, cmd, next);
2474190688Sweongyo		if (cmd->flags & UATH_CMD_FLAG_ASYNC)
2475190688Sweongyo			UATH_STAT_INC(sc, st_cmd_inactive);
2476190688Sweongyo		else
2477190688Sweongyo			UATH_STAT_INC(sc, st_cmd_active);
2478190688Sweongyo
2479194677Sthompsa		usbd_xfer_set_frame_data(xfer, 0, cmd->buf, cmd->buflen);
2480194228Sthompsa		usbd_transfer_submit(xfer);
2481190688Sweongyo		break;
2482190688Sweongyo	default:
2483194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2484194677Sthompsa			usbd_xfer_set_stall(xfer);
2485190688Sweongyo			goto setup;
2486190688Sweongyo		}
2487190688Sweongyo		break;
2488190688Sweongyo	}
2489190688Sweongyo}
2490190688Sweongyo
2491190688Sweongyostatic void
2492190688Sweongyouath_update_rxstat(struct uath_softc *sc, uint32_t status)
2493190688Sweongyo{
2494190688Sweongyo
2495190688Sweongyo	switch (status) {
2496190688Sweongyo	case UATH_STATUS_STOP_IN_PROGRESS:
2497190688Sweongyo		UATH_STAT_INC(sc, st_stopinprogress);
2498190688Sweongyo		break;
2499190688Sweongyo	case UATH_STATUS_CRC_ERR:
2500190688Sweongyo		UATH_STAT_INC(sc, st_crcerr);
2501190688Sweongyo		break;
2502190688Sweongyo	case UATH_STATUS_PHY_ERR:
2503190688Sweongyo		UATH_STAT_INC(sc, st_phyerr);
2504190688Sweongyo		break;
2505190688Sweongyo	case UATH_STATUS_DECRYPT_CRC_ERR:
2506190688Sweongyo		UATH_STAT_INC(sc, st_decrypt_crcerr);
2507190688Sweongyo		break;
2508190688Sweongyo	case UATH_STATUS_DECRYPT_MIC_ERR:
2509190688Sweongyo		UATH_STAT_INC(sc, st_decrypt_micerr);
2510190688Sweongyo		break;
2511190688Sweongyo	case UATH_STATUS_DECOMP_ERR:
2512190688Sweongyo		UATH_STAT_INC(sc, st_decomperr);
2513190688Sweongyo		break;
2514190688Sweongyo	case UATH_STATUS_KEY_ERR:
2515190688Sweongyo		UATH_STAT_INC(sc, st_keyerr);
2516190688Sweongyo		break;
2517190688Sweongyo	case UATH_STATUS_ERR:
2518190688Sweongyo		UATH_STAT_INC(sc, st_err);
2519190688Sweongyo		break;
2520190688Sweongyo	default:
2521190688Sweongyo		break;
2522190688Sweongyo	}
2523190688Sweongyo}
2524190688Sweongyo
2525190688Sweongyostatic struct mbuf *
2526192984Sthompsauath_data_rxeof(struct usb_xfer *xfer, struct uath_data *data,
2527190688Sweongyo    struct uath_rx_desc **pdesc)
2528190688Sweongyo{
2529194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2530190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
2531190688Sweongyo	struct ieee80211com *ic = ifp->if_l2com;
2532190688Sweongyo	struct uath_chunk *chunk;
2533190688Sweongyo	struct uath_rx_desc *desc;
2534190688Sweongyo	struct mbuf *m = data->m, *mnew, *mp;
2535190688Sweongyo	uint16_t chunklen;
2536194677Sthompsa	int actlen;
2537190688Sweongyo
2538194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2539194677Sthompsa
2540194677Sthompsa	if (actlen < UATH_MIN_RXBUFSZ) {
2541190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2542194677Sthompsa		    "%s: wrong xfer size (len=%d)\n", __func__, actlen);
2543190688Sweongyo		ifp->if_ierrors++;
2544190688Sweongyo		return (NULL);
2545190688Sweongyo	}
2546190688Sweongyo
2547190688Sweongyo	chunk = (struct uath_chunk *)data->buf;
2548190688Sweongyo	if (chunk->seqnum == 0 && chunk->flags == 0 && chunk->length == 0) {
2549190688Sweongyo		device_printf(sc->sc_dev, "%s: strange response\n", __func__);
2550190688Sweongyo		ifp->if_ierrors++;
2551190688Sweongyo		UATH_RESET_INTRX(sc);
2552190688Sweongyo		return (NULL);
2553190688Sweongyo	}
2554190688Sweongyo
2555190688Sweongyo	if (chunk->seqnum != sc->sc_intrx_nextnum) {
2556190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "invalid seqnum %d, expected %d\n",
2557190688Sweongyo		    chunk->seqnum, sc->sc_intrx_nextnum);
2558190688Sweongyo		UATH_STAT_INC(sc, st_badchunkseqnum);
2559190688Sweongyo		if (sc->sc_intrx_head != NULL)
2560190688Sweongyo			m_freem(sc->sc_intrx_head);
2561190688Sweongyo		UATH_RESET_INTRX(sc);
2562190688Sweongyo		return (NULL);
2563190688Sweongyo	}
2564190688Sweongyo
2565190688Sweongyo	/* check multi-chunk frames  */
2566190688Sweongyo	if ((chunk->seqnum == 0 && !(chunk->flags & UATH_CFLAGS_FINAL)) ||
2567190688Sweongyo	    (chunk->seqnum != 0 && (chunk->flags & UATH_CFLAGS_FINAL)) ||
2568190688Sweongyo	    chunk->flags & UATH_CFLAGS_RXMSG)
2569190688Sweongyo		UATH_STAT_INC(sc, st_multichunk);
2570190688Sweongyo
2571190688Sweongyo	chunklen = be16toh(chunk->length);
2572190688Sweongyo	if (chunk->flags & UATH_CFLAGS_FINAL)
2573190688Sweongyo		chunklen -= sizeof(struct uath_rx_desc);
2574190688Sweongyo
2575190688Sweongyo	if (chunklen > 0 &&
2576190688Sweongyo	    (!(chunk->flags & UATH_CFLAGS_FINAL) || !(chunk->seqnum == 0))) {
2577190688Sweongyo		/* we should use intermediate RX buffer  */
2578190688Sweongyo		if (chunk->seqnum == 0)
2579190688Sweongyo			UATH_RESET_INTRX(sc);
2580190688Sweongyo		if ((sc->sc_intrx_len + sizeof(struct uath_rx_desc) +
2581190688Sweongyo		    chunklen) > UATH_MAX_INTRX_SIZE) {
2582190688Sweongyo			UATH_STAT_INC(sc, st_invalidlen);
2583190688Sweongyo			ifp->if_iqdrops++;
2584190688Sweongyo			if (sc->sc_intrx_head != NULL)
2585190688Sweongyo				m_freem(sc->sc_intrx_head);
2586190688Sweongyo			UATH_RESET_INTRX(sc);
2587190688Sweongyo			return (NULL);
2588190688Sweongyo		}
2589190688Sweongyo
2590190688Sweongyo		m->m_len = chunklen;
2591190688Sweongyo		m->m_data += sizeof(struct uath_chunk);
2592190688Sweongyo
2593190688Sweongyo		if (sc->sc_intrx_head == NULL) {
2594190688Sweongyo			sc->sc_intrx_head = m;
2595190688Sweongyo			sc->sc_intrx_tail = m;
2596190688Sweongyo		} else {
2597190688Sweongyo			m->m_flags &= ~M_PKTHDR;
2598190688Sweongyo			sc->sc_intrx_tail->m_next = m;
2599190688Sweongyo			sc->sc_intrx_tail = m;
2600190688Sweongyo		}
2601190688Sweongyo	}
2602190688Sweongyo	sc->sc_intrx_len += chunklen;
2603190688Sweongyo
2604190688Sweongyo	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2605190688Sweongyo	if (mnew == NULL) {
2606190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2607190688Sweongyo		    "%s: can't get new mbuf, drop frame\n", __func__);
2608190688Sweongyo		ifp->if_ierrors++;
2609190688Sweongyo		if (sc->sc_intrx_head != NULL)
2610190688Sweongyo			m_freem(sc->sc_intrx_head);
2611190688Sweongyo		UATH_RESET_INTRX(sc);
2612190688Sweongyo		return (NULL);
2613190688Sweongyo	}
2614190688Sweongyo
2615190688Sweongyo	data->m = mnew;
2616190688Sweongyo	data->buf = mtod(mnew, uint8_t *);
2617190688Sweongyo
2618190688Sweongyo	/* if the frame is not final continue the transfer  */
2619190688Sweongyo	if (!(chunk->flags & UATH_CFLAGS_FINAL)) {
2620190688Sweongyo		sc->sc_intrx_nextnum++;
2621190688Sweongyo		UATH_RESET_INTRX(sc);
2622190688Sweongyo		return (NULL);
2623190688Sweongyo	}
2624190688Sweongyo
2625190688Sweongyo	/*
2626190688Sweongyo	 * if the frame is not set UATH_CFLAGS_RXMSG, then rx descriptor is
2627190688Sweongyo	 * located at the end, 32-bit aligned
2628190688Sweongyo	 */
2629190688Sweongyo	desc = (chunk->flags & UATH_CFLAGS_RXMSG) ?
2630190688Sweongyo		(struct uath_rx_desc *)(chunk + 1) :
2631190688Sweongyo		(struct uath_rx_desc *)(((uint8_t *)chunk) +
2632190688Sweongyo		    sizeof(struct uath_chunk) + be16toh(chunk->length) -
2633190688Sweongyo		    sizeof(struct uath_rx_desc));
2634190688Sweongyo	*pdesc = desc;
2635190688Sweongyo
2636190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2637190688Sweongyo	    "%s: frame len %u code %u status %u rate %u antenna %u "
2638190688Sweongyo	    "rssi %d channel %u phyerror %u connix %u decrypterror %u "
2639190688Sweongyo	    "keycachemiss %u\n", __func__, be32toh(desc->framelen)
2640190688Sweongyo	    , be32toh(desc->code), be32toh(desc->status), be32toh(desc->rate)
2641190688Sweongyo	    , be32toh(desc->antenna), be32toh(desc->rssi), be32toh(desc->channel)
2642190688Sweongyo	    , be32toh(desc->phyerror), be32toh(desc->connix)
2643190688Sweongyo	    , be32toh(desc->decrypterror), be32toh(desc->keycachemiss));
2644190688Sweongyo
2645190688Sweongyo	if (be32toh(desc->len) > MCLBYTES) {
2646190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2647190688Sweongyo		    "%s: bad descriptor (len=%d)\n", __func__,
2648190688Sweongyo		    be32toh(desc->len));
2649190688Sweongyo		ifp->if_iqdrops++;
2650190688Sweongyo		UATH_STAT_INC(sc, st_toobigrxpkt);
2651190688Sweongyo		if (sc->sc_intrx_head != NULL)
2652190688Sweongyo			m_freem(sc->sc_intrx_head);
2653190688Sweongyo		UATH_RESET_INTRX(sc);
2654190688Sweongyo		return (NULL);
2655190688Sweongyo	}
2656190688Sweongyo
2657190688Sweongyo	uath_update_rxstat(sc, be32toh(desc->status));
2658190688Sweongyo
2659190688Sweongyo	/* finalize mbuf */
2660190688Sweongyo	if (sc->sc_intrx_head == NULL) {
2661190688Sweongyo		m->m_pkthdr.rcvif = ifp;
2662190688Sweongyo		m->m_pkthdr.len = m->m_len =
2663190688Sweongyo			be32toh(desc->framelen) - UATH_RX_DUMMYSIZE;
2664190688Sweongyo		m->m_data += sizeof(struct uath_chunk);
2665190688Sweongyo	} else {
2666190688Sweongyo		mp = sc->sc_intrx_head;
2667190688Sweongyo		mp->m_pkthdr.rcvif = ifp;
2668190688Sweongyo		mp->m_flags |= M_PKTHDR;
2669190688Sweongyo		mp->m_pkthdr.len = sc->sc_intrx_len;
2670190688Sweongyo		m = mp;
2671190688Sweongyo	}
2672190688Sweongyo
2673190688Sweongyo	/* there are a lot more fields in the RX descriptor */
2674194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) == 0 &&
2675194329Sweongyo	    ieee80211_radiotap_active(ic)) {
2676190688Sweongyo		struct uath_rx_radiotap_header *tap = &sc->sc_rxtap;
2677192468Ssam		uint32_t tsf_hi = be32toh(desc->tstamp_high);
2678192468Ssam		uint32_t tsf_lo = be32toh(desc->tstamp_low);
2679190688Sweongyo
2680192468Ssam		/* XXX only get low order 24bits of tsf from h/w */
2681192468Ssam		tap->wr_tsf = htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
2682192468Ssam		tap->wr_flags = 0;
2683192468Ssam		if (be32toh(desc->status) == UATH_STATUS_CRC_ERR)
2684192468Ssam			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2685192468Ssam		/* XXX map other status to BADFCS? */
2686192468Ssam		/* XXX ath h/w rate code, need to map */
2687192468Ssam		tap->wr_rate = be32toh(desc->rate);
2688192468Ssam		tap->wr_antenna = be32toh(desc->antenna);
2689192468Ssam		tap->wr_antsignal = -95 + be32toh(desc->rssi);
2690192468Ssam		tap->wr_antnoise = -95;
2691190688Sweongyo	}
2692190688Sweongyo
2693190688Sweongyo	ifp->if_ipackets++;
2694190688Sweongyo	UATH_RESET_INTRX(sc);
2695190688Sweongyo
2696190688Sweongyo	return (m);
2697190688Sweongyo}
2698190688Sweongyo
2699190688Sweongyostatic void
2700194677Sthompsauath_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2701190688Sweongyo{
2702194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2703190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
2704190688Sweongyo	struct ieee80211com *ic = ifp->if_l2com;
2705190688Sweongyo	struct ieee80211_frame *wh;
2706190688Sweongyo	struct ieee80211_node *ni;
2707190688Sweongyo	struct mbuf *m = NULL;
2708190688Sweongyo	struct uath_data *data;
2709190688Sweongyo	struct uath_rx_desc *desc = NULL;
2710190688Sweongyo	int8_t nf;
2711190688Sweongyo
2712190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2713190688Sweongyo
2714190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2715190688Sweongyo	case USB_ST_TRANSFERRED:
2716190688Sweongyo		data = STAILQ_FIRST(&sc->sc_rx_active);
2717190688Sweongyo		if (data == NULL)
2718190688Sweongyo			goto setup;
2719190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2720190688Sweongyo		UATH_STAT_DEC(sc, st_rx_active);
2721190688Sweongyo		m = uath_data_rxeof(xfer, data, &desc);
2722190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2723190688Sweongyo		UATH_STAT_INC(sc, st_rx_inactive);
2724190688Sweongyo		/* FALLTHROUGH */
2725190688Sweongyo	case USB_ST_SETUP:
2726190688Sweongyosetup:
2727190688Sweongyo		data = STAILQ_FIRST(&sc->sc_rx_inactive);
2728190688Sweongyo		if (data == NULL)
2729190688Sweongyo			return;
2730190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2731190688Sweongyo		UATH_STAT_DEC(sc, st_rx_inactive);
2732190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2733190688Sweongyo		UATH_STAT_INC(sc, st_rx_active);
2734194677Sthompsa		usbd_xfer_set_frame_data(xfer, 0, data->buf,
2735194677Sthompsa		    usbd_xfer_max_len(xfer));
2736194228Sthompsa		usbd_transfer_submit(xfer);
2737190688Sweongyo
2738190688Sweongyo		/*
2739190688Sweongyo		 * To avoid LOR we should unlock our private mutex here to call
2740190688Sweongyo		 * ieee80211_input() because here is at the end of a USB
2741190688Sweongyo		 * callback and safe to unlock.
2742190688Sweongyo		 */
2743194329Sweongyo		if (sc->sc_flags & UATH_FLAG_INVALID) {
2744194329Sweongyo			if (m != NULL)
2745194329Sweongyo				m_freem(m);
2746194329Sweongyo			return;
2747194329Sweongyo		}
2748190688Sweongyo		UATH_UNLOCK(sc);
2749190688Sweongyo		if (m != NULL && desc != NULL) {
2750190688Sweongyo			wh = mtod(m, struct ieee80211_frame *);
2751190688Sweongyo			ni = ieee80211_find_rxnode(ic,
2752190688Sweongyo			    (struct ieee80211_frame_min *)wh);
2753190688Sweongyo			nf = -95;	/* XXX */
2754190688Sweongyo			if (ni != NULL) {
2755190688Sweongyo				(void) ieee80211_input(ni, m,
2756192468Ssam				    (int)be32toh(desc->rssi), nf);
2757190688Sweongyo				/* node is no longer needed */
2758190688Sweongyo				ieee80211_free_node(ni);
2759190688Sweongyo			} else
2760190688Sweongyo				(void) ieee80211_input_all(ic, m,
2761192468Ssam				    (int)be32toh(desc->rssi), nf);
2762190688Sweongyo			m = NULL;
2763190688Sweongyo			desc = NULL;
2764190688Sweongyo		}
2765190688Sweongyo		UATH_LOCK(sc);
2766190688Sweongyo		break;
2767190688Sweongyo	default:
2768190688Sweongyo		/* needs it to the inactive queue due to a error.  */
2769190688Sweongyo		data = STAILQ_FIRST(&sc->sc_rx_active);
2770190688Sweongyo		if (data != NULL) {
2771190688Sweongyo			STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2772190688Sweongyo			UATH_STAT_DEC(sc, st_rx_active);
2773190688Sweongyo			STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2774190688Sweongyo			UATH_STAT_INC(sc, st_rx_inactive);
2775190688Sweongyo		}
2776194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2777194677Sthompsa			usbd_xfer_set_stall(xfer);
2778190688Sweongyo			ifp->if_ierrors++;
2779190688Sweongyo			goto setup;
2780190688Sweongyo		}
2781190688Sweongyo		break;
2782190688Sweongyo	}
2783190688Sweongyo}
2784190688Sweongyo
2785190688Sweongyostatic void
2786192984Sthompsauath_data_txeof(struct usb_xfer *xfer, struct uath_data *data)
2787190688Sweongyo{
2788194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2789190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
2790190688Sweongyo	struct mbuf *m;
2791190688Sweongyo
2792190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2793190688Sweongyo
2794190688Sweongyo	/*
2795190688Sweongyo	 * Do any tx complete callback.  Note this must be done before releasing
2796190688Sweongyo	 * the node reference.
2797190688Sweongyo	 */
2798190688Sweongyo	if (data->m) {
2799190688Sweongyo		m = data->m;
2800194329Sweongyo		if (m->m_flags & M_TXCB &&
2801194329Sweongyo		    (sc->sc_flags & UATH_FLAG_INVALID) == 0) {
2802190688Sweongyo			/* XXX status? */
2803190688Sweongyo			ieee80211_process_callback(data->ni, m, 0);
2804190688Sweongyo		}
2805190688Sweongyo		m_freem(m);
2806190688Sweongyo		data->m = NULL;
2807190688Sweongyo	}
2808190688Sweongyo	if (data->ni) {
2809194329Sweongyo		if ((sc->sc_flags & UATH_FLAG_INVALID) == 0)
2810194329Sweongyo			ieee80211_free_node(data->ni);
2811190688Sweongyo		data->ni = NULL;
2812190688Sweongyo	}
2813190688Sweongyo	sc->sc_tx_timer = 0;
2814190688Sweongyo	ifp->if_opackets++;
2815190688Sweongyo	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2816190688Sweongyo}
2817190688Sweongyo
2818190688Sweongyostatic void
2819194677Sthompsauath_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2820190688Sweongyo{
2821194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2822190688Sweongyo	struct ifnet *ifp = sc->sc_ifp;
2823190688Sweongyo	struct uath_data *data;
2824190688Sweongyo
2825190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2826190688Sweongyo
2827190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2828190688Sweongyo	case USB_ST_TRANSFERRED:
2829190688Sweongyo		data = STAILQ_FIRST(&sc->sc_tx_active);
2830190688Sweongyo		if (data == NULL)
2831190688Sweongyo			goto setup;
2832190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2833190688Sweongyo		UATH_STAT_DEC(sc, st_tx_active);
2834190688Sweongyo		uath_data_txeof(xfer, data);
2835190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2836190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
2837190688Sweongyo		/* FALLTHROUGH */
2838190688Sweongyo	case USB_ST_SETUP:
2839190688Sweongyosetup:
2840190688Sweongyo		data = STAILQ_FIRST(&sc->sc_tx_pending);
2841190688Sweongyo		if (data == NULL) {
2842190688Sweongyo			DPRINTF(sc, UATH_DEBUG_XMIT, "%s: empty pending queue\n",
2843190688Sweongyo			    __func__);
2844190688Sweongyo			return;
2845190688Sweongyo		}
2846190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2847190688Sweongyo		UATH_STAT_DEC(sc, st_tx_pending);
2848190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2849190688Sweongyo		UATH_STAT_INC(sc, st_tx_active);
2850190688Sweongyo
2851194677Sthompsa		usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2852194228Sthompsa		usbd_transfer_submit(xfer);
2853190688Sweongyo
2854190688Sweongyo		UATH_UNLOCK(sc);
2855190688Sweongyo		uath_start(ifp);
2856190688Sweongyo		UATH_LOCK(sc);
2857190688Sweongyo		break;
2858190688Sweongyo	default:
2859190688Sweongyo		data = STAILQ_FIRST(&sc->sc_tx_active);
2860190688Sweongyo		if (data == NULL)
2861190688Sweongyo			goto setup;
2862190688Sweongyo		if (data->ni != NULL) {
2863194329Sweongyo			if ((sc->sc_flags & UATH_FLAG_INVALID) == 0)
2864194329Sweongyo				ieee80211_free_node(data->ni);
2865190688Sweongyo			data->ni = NULL;
2866190688Sweongyo			ifp->if_oerrors++;
2867190688Sweongyo		}
2868194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2869194677Sthompsa			usbd_xfer_set_stall(xfer);
2870190688Sweongyo			goto setup;
2871190688Sweongyo		}
2872190688Sweongyo		break;
2873190688Sweongyo	}
2874190688Sweongyo}
2875190688Sweongyo
2876190688Sweongyostatic device_method_t uath_methods[] = {
2877190688Sweongyo	DEVMETHOD(device_probe, uath_match),
2878190688Sweongyo	DEVMETHOD(device_attach, uath_attach),
2879190688Sweongyo	DEVMETHOD(device_detach, uath_detach),
2880190688Sweongyo	{ 0, 0 }
2881190688Sweongyo};
2882190688Sweongyostatic driver_t uath_driver = {
2883190688Sweongyo	"uath",
2884190688Sweongyo	uath_methods,
2885190688Sweongyo	sizeof(struct uath_softc)
2886190688Sweongyo};
2887190688Sweongyostatic devclass_t uath_devclass;
2888190688Sweongyo
2889190688SweongyoDRIVER_MODULE(uath, uhub, uath_driver, uath_devclass, NULL, 0);
2890190688SweongyoMODULE_DEPEND(uath, wlan, 1, 1, 1);
2891190688SweongyoMODULE_DEPEND(uath, usb, 1, 1, 1);
2892