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: stable/11/sys/dev/usb/wlan/if_uath.c 346005 2019-04-07 13:11:40Z avos $");
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 <net/bpf.h>
84190688Sweongyo#include <net/if.h>
85257176Sglebius#include <net/if_var.h>
86190688Sweongyo#include <net/if_arp.h>
87190688Sweongyo#include <net/ethernet.h>
88190688Sweongyo#include <net/if_dl.h>
89190688Sweongyo#include <net/if_media.h>
90190688Sweongyo#include <net/if_types.h>
91190688Sweongyo
92190688Sweongyo#ifdef INET
93190688Sweongyo#include <netinet/in.h>
94190688Sweongyo#include <netinet/in_systm.h>
95190688Sweongyo#include <netinet/in_var.h>
96190688Sweongyo#include <netinet/if_ether.h>
97190688Sweongyo#include <netinet/ip.h>
98190688Sweongyo#endif
99190688Sweongyo
100190688Sweongyo#include <net80211/ieee80211_var.h>
101288088Sadrian#include <net80211/ieee80211_input.h>
102190688Sweongyo#include <net80211/ieee80211_regdomain.h>
103190688Sweongyo#include <net80211/ieee80211_radiotap.h>
104190688Sweongyo
105190688Sweongyo#include <dev/usb/usb.h>
106194677Sthompsa#include <dev/usb/usbdi.h>
107190688Sweongyo#include "usbdevs.h"
108190688Sweongyo
109190688Sweongyo#include <dev/usb/wlan/if_uathreg.h>
110190688Sweongyo#include <dev/usb/wlan/if_uathvar.h>
111190688Sweongyo
112227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, uath, CTLFLAG_RW, 0, "USB Atheros");
113190688Sweongyo
114190688Sweongyostatic	int uath_countrycode = CTRY_DEFAULT;	/* country code */
115267992ShselaskySYSCTL_INT(_hw_usb_uath, OID_AUTO, countrycode, CTLFLAG_RWTUN, &uath_countrycode,
116190688Sweongyo    0, "country code");
117190688Sweongyostatic	int uath_regdomain = 0;			/* regulatory domain */
118192502SthompsaSYSCTL_INT(_hw_usb_uath, OID_AUTO, regdomain, CTLFLAG_RD, &uath_regdomain,
119190688Sweongyo    0, "regulatory domain");
120190688Sweongyo
121190688Sweongyo#ifdef UATH_DEBUG
122190688Sweongyoint uath_debug = 0;
123267992ShselaskySYSCTL_INT(_hw_usb_uath, OID_AUTO, debug, CTLFLAG_RWTUN, &uath_debug, 0,
124190688Sweongyo    "uath debug level");
125190688Sweongyoenum {
126190688Sweongyo	UATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
127190688Sweongyo	UATH_DEBUG_XMIT_DUMP	= 0x00000002,	/* xmit dump */
128190688Sweongyo	UATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
129190688Sweongyo	UATH_DEBUG_TX_PROC	= 0x00000008,	/* tx ISR proc */
130190688Sweongyo	UATH_DEBUG_RX_PROC	= 0x00000010,	/* rx ISR proc */
131190688Sweongyo	UATH_DEBUG_RECV_ALL	= 0x00000020,	/* trace all frames (beacons) */
132190688Sweongyo	UATH_DEBUG_INIT		= 0x00000040,	/* initialization of dev */
133190688Sweongyo	UATH_DEBUG_DEVCAP	= 0x00000080,	/* dev caps */
134190688Sweongyo	UATH_DEBUG_CMDS		= 0x00000100,	/* commands */
135190688Sweongyo	UATH_DEBUG_CMDS_DUMP	= 0x00000200,	/* command buffer dump */
136190688Sweongyo	UATH_DEBUG_RESET	= 0x00000400,	/* reset processing */
137190688Sweongyo	UATH_DEBUG_STATE	= 0x00000800,	/* 802.11 state transitions */
138190688Sweongyo	UATH_DEBUG_MULTICAST	= 0x00001000,	/* multicast */
139190688Sweongyo	UATH_DEBUG_WME		= 0x00002000,	/* WME */
140190688Sweongyo	UATH_DEBUG_CHANNEL	= 0x00004000,	/* channel */
141190688Sweongyo	UATH_DEBUG_RATES	= 0x00008000,	/* rates */
142190688Sweongyo	UATH_DEBUG_CRYPTO	= 0x00010000,	/* crypto */
143190688Sweongyo	UATH_DEBUG_LED		= 0x00020000,	/* LED */
144190688Sweongyo	UATH_DEBUG_ANY		= 0xffffffff
145190688Sweongyo};
146190688Sweongyo#define	DPRINTF(sc, m, fmt, ...) do {				\
147190688Sweongyo	if (sc->sc_debug & (m))					\
148190688Sweongyo		printf(fmt, __VA_ARGS__);			\
149190688Sweongyo} while (0)
150190688Sweongyo#else
151190688Sweongyo#define	DPRINTF(sc, m, fmt, ...) do {				\
152190688Sweongyo	(void) sc;						\
153190688Sweongyo} while (0)
154190688Sweongyo#endif
155190688Sweongyo
156190688Sweongyo/* recognized device vendors/products */
157223486Shselaskystatic const STRUCT_USB_HOST_ID uath_devs[] = {
158190688Sweongyo#define	UATH_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
159193029Sweongyo	UATH_DEV(ACCTON,		SMCWUSBTG2),
160190688Sweongyo	UATH_DEV(ATHEROS,		AR5523),
161190688Sweongyo	UATH_DEV(ATHEROS2,		AR5523_1),
162190688Sweongyo	UATH_DEV(ATHEROS2,		AR5523_2),
163190688Sweongyo	UATH_DEV(ATHEROS2,		AR5523_3),
164190688Sweongyo	UATH_DEV(CONCEPTRONIC,		AR5523_1),
165190688Sweongyo	UATH_DEV(CONCEPTRONIC,		AR5523_2),
166190688Sweongyo	UATH_DEV(DLINK,			DWLAG122),
167190688Sweongyo	UATH_DEV(DLINK,			DWLAG132),
168190688Sweongyo	UATH_DEV(DLINK,			DWLG132),
169195916Sweongyo	UATH_DEV(DLINK2,		DWA120),
170190688Sweongyo	UATH_DEV(GIGASET,		AR5523),
171190688Sweongyo	UATH_DEV(GIGASET,		SMCWUSBTG),
172190688Sweongyo	UATH_DEV(GLOBALSUN,		AR5523_1),
173190688Sweongyo	UATH_DEV(GLOBALSUN,		AR5523_2),
174190688Sweongyo	UATH_DEV(NETGEAR,		WG111U),
175190688Sweongyo	UATH_DEV(NETGEAR3,		WG111T),
176190688Sweongyo	UATH_DEV(NETGEAR3,		WPN111),
177203143Sthompsa	UATH_DEV(NETGEAR3,		WPN111_2),
178190688Sweongyo	UATH_DEV(UMEDIA,		TEW444UBEU),
179190688Sweongyo	UATH_DEV(UMEDIA,		AR5523_2),
180190688Sweongyo	UATH_DEV(WISTRONNEWEB,		AR5523_1),
181190688Sweongyo	UATH_DEV(WISTRONNEWEB,		AR5523_2),
182190688Sweongyo	UATH_DEV(ZCOM,			AR5523)
183190688Sweongyo#undef UATH_DEV
184190688Sweongyo};
185190688Sweongyo
186193045Sthompsastatic usb_callback_t uath_intr_rx_callback;
187193045Sthompsastatic usb_callback_t uath_intr_tx_callback;
188193045Sthompsastatic usb_callback_t uath_bulk_rx_callback;
189193045Sthompsastatic usb_callback_t uath_bulk_tx_callback;
190190688Sweongyo
191192984Sthompsastatic const struct usb_config uath_usbconfig[UATH_N_XFERS] = {
192190688Sweongyo	[UATH_INTR_RX] = {
193190688Sweongyo		.type = UE_BULK,
194190688Sweongyo		.endpoint = 0x1,
195190688Sweongyo		.direction = UE_DIR_IN,
196190744Sthompsa		.bufsize = UATH_MAX_CMDSZ,
197190744Sthompsa		.flags = {
198190688Sweongyo			.pipe_bof = 1,
199190688Sweongyo			.short_xfer_ok = 1
200190688Sweongyo		},
201190744Sthompsa		.callback = uath_intr_rx_callback
202190688Sweongyo	},
203190688Sweongyo	[UATH_INTR_TX] = {
204190688Sweongyo		.type = UE_BULK,
205190688Sweongyo		.endpoint = 0x1,
206190688Sweongyo		.direction = UE_DIR_OUT,
207244503Shselasky		.bufsize = UATH_MAX_CMDSZ * UATH_CMD_LIST_COUNT,
208190744Sthompsa		.flags = {
209190688Sweongyo			.force_short_xfer = 1,
210190688Sweongyo			.pipe_bof = 1,
211190688Sweongyo		},
212190744Sthompsa		.callback = uath_intr_tx_callback,
213190744Sthompsa		.timeout = UATH_CMD_TIMEOUT
214190688Sweongyo	},
215190688Sweongyo	[UATH_BULK_RX] = {
216190688Sweongyo		.type = UE_BULK,
217190688Sweongyo		.endpoint = 0x2,
218190688Sweongyo		.direction = UE_DIR_IN,
219190744Sthompsa		.bufsize = MCLBYTES,
220190744Sthompsa		.flags = {
221190688Sweongyo			.ext_buffer = 1,
222190688Sweongyo			.pipe_bof = 1,
223190688Sweongyo			.short_xfer_ok = 1
224190688Sweongyo		},
225190744Sthompsa		.callback = uath_bulk_rx_callback
226190688Sweongyo	},
227190688Sweongyo	[UATH_BULK_TX] = {
228190688Sweongyo		.type = UE_BULK,
229190688Sweongyo		.endpoint = 0x2,
230190688Sweongyo		.direction = UE_DIR_OUT,
231244503Shselasky		.bufsize = UATH_MAX_TXBUFSZ * UATH_TX_DATA_LIST_COUNT,
232190744Sthompsa		.flags = {
233190688Sweongyo			.force_short_xfer = 1,
234190688Sweongyo			.pipe_bof = 1
235190688Sweongyo		},
236190744Sthompsa		.callback = uath_bulk_tx_callback,
237190744Sthompsa		.timeout = UATH_DATA_TIMEOUT
238190688Sweongyo	}
239190688Sweongyo};
240190688Sweongyo
241190688Sweongyostatic struct ieee80211vap *uath_vap_create(struct ieee80211com *,
242228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
243228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
244228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
245190688Sweongyostatic void	uath_vap_delete(struct ieee80211vap *);
246244503Shselaskystatic int	uath_alloc_cmd_list(struct uath_softc *, struct uath_cmd []);
247244503Shselaskystatic void	uath_free_cmd_list(struct uath_softc *, struct uath_cmd []);
248190688Sweongyostatic int	uath_host_available(struct uath_softc *);
249190688Sweongyostatic int	uath_get_capability(struct uath_softc *, uint32_t, uint32_t *);
250190688Sweongyostatic int	uath_get_devcap(struct uath_softc *);
251190688Sweongyostatic struct uath_cmd *
252190688Sweongyo		uath_get_cmdbuf(struct uath_softc *);
253190688Sweongyostatic int	uath_cmd_read(struct uath_softc *, uint32_t, const void *,
254190688Sweongyo		    int, void *, int, int);
255190688Sweongyostatic int	uath_cmd_write(struct uath_softc *, uint32_t, const void *,
256190688Sweongyo		    int, int);
257190688Sweongyostatic void	uath_stat(void *);
258190688Sweongyo#ifdef UATH_DEBUG
259190688Sweongyostatic void	uath_dump_cmd(const uint8_t *, int, char);
260190688Sweongyostatic const char *
261190688Sweongyo		uath_codename(int);
262190688Sweongyo#endif
263190688Sweongyostatic int	uath_get_devstatus(struct uath_softc *,
264190688Sweongyo		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
265190688Sweongyostatic int	uath_get_status(struct uath_softc *, uint32_t, void *, int);
266190688Sweongyostatic int	uath_alloc_rx_data_list(struct uath_softc *);
267190688Sweongyostatic int	uath_alloc_tx_data_list(struct uath_softc *);
268190688Sweongyostatic void	uath_free_rx_data_list(struct uath_softc *);
269190688Sweongyostatic void	uath_free_tx_data_list(struct uath_softc *);
270287197Sglebiusstatic int	uath_init(struct uath_softc *);
271287197Sglebiusstatic void	uath_stop(struct uath_softc *);
272287197Sglebiusstatic void	uath_parent(struct ieee80211com *);
273287197Sglebiusstatic int	uath_transmit(struct ieee80211com *, struct mbuf *);
274287197Sglebiusstatic void	uath_start(struct uath_softc *);
275190688Sweongyostatic int	uath_raw_xmit(struct ieee80211_node *, struct mbuf *,
276190688Sweongyo		    const struct ieee80211_bpf_params *);
277190688Sweongyostatic void	uath_scan_start(struct ieee80211com *);
278190688Sweongyostatic void	uath_scan_end(struct ieee80211com *);
279190688Sweongyostatic void	uath_set_channel(struct ieee80211com *);
280283540Sglebiusstatic void	uath_update_mcast(struct ieee80211com *);
281283540Sglebiusstatic void	uath_update_promisc(struct ieee80211com *);
282190688Sweongyostatic int	uath_config(struct uath_softc *, uint32_t, uint32_t);
283190688Sweongyostatic int	uath_config_multi(struct uath_softc *, uint32_t, const void *,
284190688Sweongyo		    int);
285190688Sweongyostatic int	uath_switch_channel(struct uath_softc *,
286190688Sweongyo		    struct ieee80211_channel *);
287190688Sweongyostatic int	uath_set_rxfilter(struct uath_softc *, uint32_t, uint32_t);
288190688Sweongyostatic void	uath_watchdog(void *);
289190688Sweongyostatic void	uath_abort_xfers(struct uath_softc *);
290190688Sweongyostatic int	uath_dataflush(struct uath_softc *);
291190688Sweongyostatic int	uath_cmdflush(struct uath_softc *);
292190688Sweongyostatic int	uath_flush(struct uath_softc *);
293190688Sweongyostatic int	uath_set_ledstate(struct uath_softc *, int);
294190688Sweongyostatic int	uath_set_chan(struct uath_softc *, struct ieee80211_channel *);
295190688Sweongyostatic int	uath_reset_tx_queues(struct uath_softc *);
296190688Sweongyostatic int	uath_wme_init(struct uath_softc *);
297190688Sweongyostatic struct uath_data *
298190688Sweongyo		uath_getbuf(struct uath_softc *);
299190688Sweongyostatic int	uath_newstate(struct ieee80211vap *, enum ieee80211_state,
300190688Sweongyo		    int);
301190688Sweongyostatic int	uath_set_key(struct uath_softc *,
302190688Sweongyo		    const struct ieee80211_key *, int);
303190688Sweongyostatic int	uath_set_keys(struct uath_softc *, struct ieee80211vap *);
304190688Sweongyostatic void	uath_sysctl_node(struct uath_softc *);
305190688Sweongyo
306190688Sweongyostatic int
307190688Sweongyouath_match(device_t dev)
308190688Sweongyo{
309192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
310190688Sweongyo
311192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
312190688Sweongyo		return (ENXIO);
313190688Sweongyo	if (uaa->info.bConfigIndex != UATH_CONFIG_INDEX)
314190688Sweongyo		return (ENXIO);
315190688Sweongyo	if (uaa->info.bIfaceIndex != UATH_IFACE_INDEX)
316190688Sweongyo		return (ENXIO);
317190688Sweongyo
318194228Sthompsa	return (usbd_lookup_id_by_uaa(uath_devs, sizeof(uath_devs), uaa));
319190688Sweongyo}
320190688Sweongyo
321190688Sweongyostatic int
322190688Sweongyouath_attach(device_t dev)
323190688Sweongyo{
324190688Sweongyo	struct uath_softc *sc = device_get_softc(dev);
325192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
326287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
327298818Savos	uint8_t bands[IEEE80211_MODE_BYTES];
328293339Savos	uint8_t iface_index = UATH_IFACE_INDEX;		/* XXX */
329193045Sthompsa	usb_error_t error;
330190688Sweongyo
331190688Sweongyo	sc->sc_dev = dev;
332190688Sweongyo	sc->sc_udev = uaa->device;
333190688Sweongyo#ifdef UATH_DEBUG
334190688Sweongyo	sc->sc_debug = uath_debug;
335190688Sweongyo#endif
336194228Sthompsa	device_set_usb_desc(dev);
337190688Sweongyo
338190688Sweongyo	/*
339190688Sweongyo	 * Only post-firmware devices here.
340190688Sweongyo	 */
341190688Sweongyo	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
342190688Sweongyo	    MTX_DEF);
343190688Sweongyo	callout_init(&sc->stat_ch, 0);
344190688Sweongyo	callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
345287197Sglebius	mbufq_init(&sc->sc_snd, ifqmaxlen);
346190688Sweongyo
347194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
348190688Sweongyo	    uath_usbconfig, UATH_N_XFERS, sc, &sc->sc_mtx);
349190688Sweongyo	if (error) {
350190688Sweongyo		device_printf(dev, "could not allocate USB transfers, "
351194228Sthompsa		    "err=%s\n", usbd_errstr(error));
352246565Shselasky		goto fail;
353190688Sweongyo	}
354190688Sweongyo
355244503Shselasky	sc->sc_cmd_dma_buf =
356244503Shselasky	    usbd_xfer_get_frame_buffer(sc->sc_xfer[UATH_INTR_TX], 0);
357244503Shselasky	sc->sc_tx_dma_buf =
358244503Shselasky	    usbd_xfer_get_frame_buffer(sc->sc_xfer[UATH_BULK_TX], 0);
359244503Shselasky
360190688Sweongyo	/*
361246565Shselasky	 * Setup buffers for firmware commands.
362246565Shselasky	 */
363246565Shselasky	error = uath_alloc_cmd_list(sc, sc->sc_cmd);
364246565Shselasky	if (error != 0) {
365246565Shselasky		device_printf(sc->sc_dev,
366246565Shselasky		    "could not allocate Tx command list\n");
367246565Shselasky		goto fail1;
368246565Shselasky	}
369246565Shselasky
370246565Shselasky	/*
371190688Sweongyo	 * We're now ready to send+receive firmware commands.
372190688Sweongyo	 */
373190688Sweongyo	UATH_LOCK(sc);
374190688Sweongyo	error = uath_host_available(sc);
375190688Sweongyo	if (error != 0) {
376190688Sweongyo		device_printf(sc->sc_dev, "could not initialize adapter\n");
377287197Sglebius		goto fail2;
378190688Sweongyo	}
379190688Sweongyo	error = uath_get_devcap(sc);
380190688Sweongyo	if (error != 0) {
381190688Sweongyo		device_printf(sc->sc_dev,
382190688Sweongyo		    "could not get device capabilities\n");
383287197Sglebius		goto fail2;
384190688Sweongyo	}
385190688Sweongyo	UATH_UNLOCK(sc);
386190688Sweongyo
387190688Sweongyo	/* Create device sysctl node. */
388190688Sweongyo	uath_sysctl_node(sc);
389190688Sweongyo
390190688Sweongyo	UATH_LOCK(sc);
391287197Sglebius	error = uath_get_devstatus(sc, ic->ic_macaddr);
392190688Sweongyo	if (error != 0) {
393190688Sweongyo		device_printf(sc->sc_dev, "could not get device status\n");
394287197Sglebius		goto fail2;
395190688Sweongyo	}
396190688Sweongyo
397190688Sweongyo	/*
398190688Sweongyo	 * Allocate xfers for Rx/Tx data pipes.
399190688Sweongyo	 */
400190688Sweongyo	error = uath_alloc_rx_data_list(sc);
401190688Sweongyo	if (error != 0) {
402190688Sweongyo		device_printf(sc->sc_dev, "could not allocate Rx data list\n");
403287197Sglebius		goto fail2;
404190688Sweongyo	}
405190688Sweongyo	error = uath_alloc_tx_data_list(sc);
406190688Sweongyo	if (error != 0) {
407190688Sweongyo		device_printf(sc->sc_dev, "could not allocate Tx data list\n");
408287197Sglebius		goto fail2;
409190688Sweongyo	}
410190688Sweongyo	UATH_UNLOCK(sc);
411190688Sweongyo
412283537Sglebius	ic->ic_softc = sc;
413283527Sglebius	ic->ic_name = device_get_nameunit(dev);
414190688Sweongyo	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
415190688Sweongyo	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
416190688Sweongyo
417190688Sweongyo	/* set device capabilities */
418190688Sweongyo	ic->ic_caps =
419190688Sweongyo	    IEEE80211_C_STA |		/* station mode */
420190688Sweongyo	    IEEE80211_C_MONITOR |	/* monitor mode supported */
421190688Sweongyo	    IEEE80211_C_TXPMGT |	/* tx power management */
422190688Sweongyo	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
423190688Sweongyo	    IEEE80211_C_SHSLOT |	/* short slot time supported */
424190688Sweongyo	    IEEE80211_C_WPA |		/* 802.11i */
425190688Sweongyo	    IEEE80211_C_BGSCAN |	/* capable of bg scanning */
426190688Sweongyo	    IEEE80211_C_TXFRAG;		/* handle tx frags */
427190688Sweongyo
428190688Sweongyo	/* put a regulatory domain to reveal informations.  */
429190688Sweongyo	uath_regdomain = sc->sc_devcap.regDomain;
430190688Sweongyo
431293339Savos	memset(bands, 0, sizeof(bands));
432293339Savos	setbit(bands, IEEE80211_MODE_11B);
433293339Savos	setbit(bands, IEEE80211_MODE_11G);
434190688Sweongyo	if ((sc->sc_devcap.analog5GhzRevision & 0xf0) == 0x30)
435293339Savos		setbit(bands, IEEE80211_MODE_11A);
436190688Sweongyo	/* XXX turbo */
437293339Savos	ieee80211_init_channels(ic, NULL, bands);
438190688Sweongyo
439287197Sglebius	ieee80211_ifattach(ic);
440190688Sweongyo	ic->ic_raw_xmit = uath_raw_xmit;
441190688Sweongyo	ic->ic_scan_start = uath_scan_start;
442190688Sweongyo	ic->ic_scan_end = uath_scan_end;
443190688Sweongyo	ic->ic_set_channel = uath_set_channel;
444190688Sweongyo	ic->ic_vap_create = uath_vap_create;
445190688Sweongyo	ic->ic_vap_delete = uath_vap_delete;
446190688Sweongyo	ic->ic_update_mcast = uath_update_mcast;
447190688Sweongyo	ic->ic_update_promisc = uath_update_promisc;
448287197Sglebius	ic->ic_transmit = uath_transmit;
449287197Sglebius	ic->ic_parent = uath_parent;
450190688Sweongyo
451192468Ssam	ieee80211_radiotap_attach(ic,
452192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
453192468Ssam		UATH_TX_RADIOTAP_PRESENT,
454192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
455192468Ssam		UATH_RX_RADIOTAP_PRESENT);
456190688Sweongyo
457190688Sweongyo	if (bootverbose)
458190688Sweongyo		ieee80211_announce(ic);
459190688Sweongyo
460190688Sweongyo	return (0);
461190688Sweongyo
462287197Sglebiusfail2:	UATH_UNLOCK(sc);
463287197Sglebius	uath_free_cmd_list(sc, sc->sc_cmd);
464246565Shselaskyfail1:	usbd_transfer_unsetup(sc->sc_xfer, UATH_N_XFERS);
465190688Sweongyofail:
466190688Sweongyo	return (error);
467190688Sweongyo}
468190688Sweongyo
469190688Sweongyostatic int
470190688Sweongyouath_detach(device_t dev)
471190688Sweongyo{
472190688Sweongyo	struct uath_softc *sc = device_get_softc(dev);
473287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
474246614Shselasky	unsigned int x;
475190688Sweongyo
476246614Shselasky	/*
477246614Shselasky	 * Prevent further allocations from RX/TX/CMD
478246614Shselasky	 * data lists and ioctls
479246614Shselasky	 */
480194329Sweongyo	UATH_LOCK(sc);
481190688Sweongyo	sc->sc_flags |= UATH_FLAG_INVALID;
482246614Shselasky
483246614Shselasky	STAILQ_INIT(&sc->sc_rx_active);
484246614Shselasky	STAILQ_INIT(&sc->sc_rx_inactive);
485246614Shselasky
486246614Shselasky	STAILQ_INIT(&sc->sc_tx_active);
487246614Shselasky	STAILQ_INIT(&sc->sc_tx_inactive);
488246614Shselasky	STAILQ_INIT(&sc->sc_tx_pending);
489246614Shselasky
490246614Shselasky	STAILQ_INIT(&sc->sc_cmd_active);
491246614Shselasky	STAILQ_INIT(&sc->sc_cmd_pending);
492246614Shselasky	STAILQ_INIT(&sc->sc_cmd_waiting);
493246614Shselasky	STAILQ_INIT(&sc->sc_cmd_inactive);
494287197Sglebius
495287197Sglebius	uath_stop(sc);
496194329Sweongyo	UATH_UNLOCK(sc);
497194329Sweongyo
498190688Sweongyo	callout_drain(&sc->stat_ch);
499190688Sweongyo	callout_drain(&sc->watchdog_ch);
500190688Sweongyo
501246614Shselasky	/* drain USB transfers */
502246614Shselasky	for (x = 0; x != UATH_N_XFERS; x++)
503246614Shselasky		usbd_transfer_drain(sc->sc_xfer[x]);
504190688Sweongyo
505246614Shselasky	/* free data buffers */
506190688Sweongyo	UATH_LOCK(sc);
507190688Sweongyo	uath_free_rx_data_list(sc);
508190688Sweongyo	uath_free_tx_data_list(sc);
509244503Shselasky	uath_free_cmd_list(sc, sc->sc_cmd);
510190688Sweongyo	UATH_UNLOCK(sc);
511190688Sweongyo
512246614Shselasky	/* free USB transfers and some data buffers */
513246614Shselasky	usbd_transfer_unsetup(sc->sc_xfer, UATH_N_XFERS);
514246614Shselasky
515246614Shselasky	ieee80211_ifdetach(ic);
516287197Sglebius	mbufq_drain(&sc->sc_snd);
517190688Sweongyo	mtx_destroy(&sc->sc_mtx);
518190688Sweongyo	return (0);
519190688Sweongyo}
520190688Sweongyo
521190688Sweongyostatic void
522244503Shselaskyuath_free_cmd_list(struct uath_softc *sc, struct uath_cmd cmds[])
523190688Sweongyo{
524190688Sweongyo	int i;
525190688Sweongyo
526244503Shselasky	for (i = 0; i != UATH_CMD_LIST_COUNT; i++)
527244503Shselasky		cmds[i].buf = NULL;
528190688Sweongyo}
529190688Sweongyo
530190688Sweongyostatic int
531244503Shselaskyuath_alloc_cmd_list(struct uath_softc *sc, struct uath_cmd cmds[])
532190688Sweongyo{
533244503Shselasky	int i;
534190688Sweongyo
535190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_active);
536190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_pending);
537190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_waiting);
538190688Sweongyo	STAILQ_INIT(&sc->sc_cmd_inactive);
539190688Sweongyo
540244503Shselasky	for (i = 0; i != UATH_CMD_LIST_COUNT; i++) {
541190688Sweongyo		struct uath_cmd *cmd = &cmds[i];
542190688Sweongyo
543190688Sweongyo		cmd->sc = sc;	/* backpointer for callbacks */
544190688Sweongyo		cmd->msgid = i;
545244503Shselasky		cmd->buf = ((uint8_t *)sc->sc_cmd_dma_buf) +
546244503Shselasky		    (i * UATH_MAX_CMDSZ);
547190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_cmd_inactive, cmd, next);
548190688Sweongyo		UATH_STAT_INC(sc, st_cmd_inactive);
549190688Sweongyo	}
550190688Sweongyo	return (0);
551190688Sweongyo}
552190688Sweongyo
553190688Sweongyostatic int
554190688Sweongyouath_host_available(struct uath_softc *sc)
555190688Sweongyo{
556190688Sweongyo	struct uath_cmd_host_available setup;
557190688Sweongyo
558190688Sweongyo	UATH_ASSERT_LOCKED(sc);
559190688Sweongyo
560190688Sweongyo	/* inform target the host is available */
561190688Sweongyo	setup.sw_ver_major = htobe32(ATH_SW_VER_MAJOR);
562190688Sweongyo	setup.sw_ver_minor = htobe32(ATH_SW_VER_MINOR);
563190688Sweongyo	setup.sw_ver_patch = htobe32(ATH_SW_VER_PATCH);
564190688Sweongyo	setup.sw_ver_build = htobe32(ATH_SW_VER_BUILD);
565190688Sweongyo	return uath_cmd_read(sc, WDCMSG_HOST_AVAILABLE,
566190688Sweongyo		&setup, sizeof setup, NULL, 0, 0);
567190688Sweongyo}
568190688Sweongyo
569190688Sweongyo#ifdef UATH_DEBUG
570190688Sweongyostatic void
571190688Sweongyouath_dump_cmd(const uint8_t *buf, int len, char prefix)
572190688Sweongyo{
573190688Sweongyo	const char *sep = "";
574190688Sweongyo	int i;
575190688Sweongyo
576190688Sweongyo	for (i = 0; i < len; i++) {
577190688Sweongyo		if ((i % 16) == 0) {
578190688Sweongyo			printf("%s%c ", sep, prefix);
579190688Sweongyo			sep = "\n";
580190688Sweongyo		}
581190688Sweongyo		else if ((i % 4) == 0)
582190688Sweongyo			printf(" ");
583190688Sweongyo		printf("%02x", buf[i]);
584190688Sweongyo	}
585190688Sweongyo	printf("\n");
586190688Sweongyo}
587190688Sweongyo
588190688Sweongyostatic const char *
589190688Sweongyouath_codename(int code)
590190688Sweongyo{
591190688Sweongyo	static const char *names[] = {
592190688Sweongyo	    "0x00",
593190688Sweongyo	    "HOST_AVAILABLE",
594190688Sweongyo	    "BIND",
595190688Sweongyo	    "TARGET_RESET",
596190688Sweongyo	    "TARGET_GET_CAPABILITY",
597190688Sweongyo	    "TARGET_SET_CONFIG",
598190688Sweongyo	    "TARGET_GET_STATUS",
599190688Sweongyo	    "TARGET_GET_STATS",
600190688Sweongyo	    "TARGET_START",
601190688Sweongyo	    "TARGET_STOP",
602190688Sweongyo	    "TARGET_ENABLE",
603190688Sweongyo	    "TARGET_DISABLE",
604190688Sweongyo	    "CREATE_CONNECTION",
605190688Sweongyo	    "UPDATE_CONNECT_ATTR",
606190688Sweongyo	    "DELETE_CONNECT",
607190688Sweongyo	    "SEND",
608190688Sweongyo	    "FLUSH",
609190688Sweongyo	    "STATS_UPDATE",
610190688Sweongyo	    "BMISS",
611190688Sweongyo	    "DEVICE_AVAIL",
612190688Sweongyo	    "SEND_COMPLETE",
613190688Sweongyo	    "DATA_AVAIL",
614190688Sweongyo	    "SET_PWR_MODE",
615190688Sweongyo	    "BMISS_ACK",
616190688Sweongyo	    "SET_LED_STEADY",
617190688Sweongyo	    "SET_LED_BLINK",
618190688Sweongyo	    "SETUP_BEACON_DESC",
619190688Sweongyo	    "BEACON_INIT",
620190688Sweongyo	    "RESET_KEY_CACHE",
621190688Sweongyo	    "RESET_KEY_CACHE_ENTRY",
622190688Sweongyo	    "SET_KEY_CACHE_ENTRY",
623190688Sweongyo	    "SET_DECOMP_MASK",
624190688Sweongyo	    "SET_REGULATORY_DOMAIN",
625190688Sweongyo	    "SET_LED_STATE",
626190688Sweongyo	    "WRITE_ASSOCID",
627190688Sweongyo	    "SET_STA_BEACON_TIMERS",
628190688Sweongyo	    "GET_TSF",
629190688Sweongyo	    "RESET_TSF",
630190688Sweongyo	    "SET_ADHOC_MODE",
631190688Sweongyo	    "SET_BASIC_RATE",
632190688Sweongyo	    "MIB_CONTROL",
633190688Sweongyo	    "GET_CHANNEL_DATA",
634190688Sweongyo	    "GET_CUR_RSSI",
635190688Sweongyo	    "SET_ANTENNA_SWITCH",
636190688Sweongyo	    "0x2c", "0x2d", "0x2e",
637190688Sweongyo	    "USE_SHORT_SLOT_TIME",
638190688Sweongyo	    "SET_POWER_MODE",
639190688Sweongyo	    "SETUP_PSPOLL_DESC",
640190688Sweongyo	    "SET_RX_MULTICAST_FILTER",
641190688Sweongyo	    "RX_FILTER",
642190688Sweongyo	    "PER_CALIBRATION",
643190688Sweongyo	    "RESET",
644190688Sweongyo	    "DISABLE",
645190688Sweongyo	    "PHY_DISABLE",
646190688Sweongyo	    "SET_TX_POWER_LIMIT",
647190688Sweongyo	    "SET_TX_QUEUE_PARAMS",
648190688Sweongyo	    "SETUP_TX_QUEUE",
649190688Sweongyo	    "RELEASE_TX_QUEUE",
650190688Sweongyo	};
651190688Sweongyo	static char buf[8];
652190688Sweongyo
653288088Sadrian	if (code < nitems(names))
654190688Sweongyo		return names[code];
655190688Sweongyo	if (code == WDCMSG_SET_DEFAULT_KEY)
656190688Sweongyo		return "SET_DEFAULT_KEY";
657190688Sweongyo	snprintf(buf, sizeof(buf), "0x%02x", code);
658190688Sweongyo	return buf;
659190688Sweongyo}
660190688Sweongyo#endif
661190688Sweongyo
662190688Sweongyo/*
663190688Sweongyo * Low-level function to send read or write commands to the firmware.
664190688Sweongyo */
665190688Sweongyostatic int
666190688Sweongyouath_cmdsend(struct uath_softc *sc, uint32_t code, const void *idata, int ilen,
667190688Sweongyo    void *odata, int olen, int flags)
668190688Sweongyo{
669190688Sweongyo	struct uath_cmd_hdr *hdr;
670190688Sweongyo	struct uath_cmd *cmd;
671190688Sweongyo	int error;
672190688Sweongyo
673190688Sweongyo	UATH_ASSERT_LOCKED(sc);
674190688Sweongyo
675190688Sweongyo	/* grab a xfer */
676190688Sweongyo	cmd = uath_get_cmdbuf(sc);
677190688Sweongyo	if (cmd == NULL) {
678190688Sweongyo		device_printf(sc->sc_dev, "%s: empty inactive queue\n",
679190688Sweongyo		    __func__);
680190688Sweongyo		return (ENOBUFS);
681190688Sweongyo	}
682190688Sweongyo	cmd->flags = flags;
683190688Sweongyo	/* always bulk-out a multiple of 4 bytes */
684190688Sweongyo	cmd->buflen = roundup2(sizeof(struct uath_cmd_hdr) + ilen, 4);
685190688Sweongyo
686190688Sweongyo	hdr = (struct uath_cmd_hdr *)cmd->buf;
687227461Shselasky	memset(hdr, 0, sizeof(struct uath_cmd_hdr));
688190688Sweongyo	hdr->len   = htobe32(cmd->buflen);
689190688Sweongyo	hdr->code  = htobe32(code);
690190688Sweongyo	hdr->msgid = cmd->msgid;	/* don't care about endianness */
691190688Sweongyo	hdr->magic = htobe32((cmd->flags & UATH_CMD_FLAG_MAGIC) ? 1 << 24 : 0);
692227461Shselasky	memcpy((uint8_t *)(hdr + 1), idata, ilen);
693190688Sweongyo
694190688Sweongyo#ifdef UATH_DEBUG
695190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_CMDS) {
696190688Sweongyo		printf("%s: send  %s [flags 0x%x] olen %d\n",
697190688Sweongyo		    __func__, uath_codename(code), cmd->flags, olen);
698190688Sweongyo		if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP)
699190688Sweongyo			uath_dump_cmd(cmd->buf, cmd->buflen, '+');
700190688Sweongyo	}
701190688Sweongyo#endif
702190688Sweongyo	cmd->odata = odata;
703190688Sweongyo	KASSERT(odata == NULL ||
704190688Sweongyo	    olen < UATH_MAX_CMDSZ - sizeof(*hdr) + sizeof(uint32_t),
705190688Sweongyo	    ("odata %p olen %u", odata, olen));
706190688Sweongyo	cmd->olen = olen;
707190688Sweongyo
708190688Sweongyo	STAILQ_INSERT_TAIL(&sc->sc_cmd_pending, cmd, next);
709190688Sweongyo	UATH_STAT_INC(sc, st_cmd_pending);
710194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_INTR_TX]);
711190688Sweongyo
712190688Sweongyo	if (cmd->flags & UATH_CMD_FLAG_READ) {
713194228Sthompsa		usbd_transfer_start(sc->sc_xfer[UATH_INTR_RX]);
714190688Sweongyo
715190688Sweongyo		/* wait at most two seconds for command reply */
716190688Sweongyo		error = mtx_sleep(cmd, &sc->sc_mtx, 0, "uathcmd", 2 * hz);
717190688Sweongyo		cmd->odata = NULL;	/* in case reply comes too late */
718190688Sweongyo		if (error != 0) {
719190688Sweongyo			device_printf(sc->sc_dev, "timeout waiting for reply "
720190688Sweongyo			    "to cmd 0x%x (%u)\n", code, code);
721190688Sweongyo		} else if (cmd->olen != olen) {
722190688Sweongyo			device_printf(sc->sc_dev, "unexpected reply data count "
723190688Sweongyo			    "to cmd 0x%x (%u), got %u, expected %u\n",
724190688Sweongyo			    code, code, cmd->olen, olen);
725190688Sweongyo			error = EINVAL;
726190688Sweongyo		}
727190688Sweongyo		return (error);
728190688Sweongyo	}
729190688Sweongyo	return (0);
730190688Sweongyo}
731190688Sweongyo
732190688Sweongyostatic int
733190688Sweongyouath_cmd_read(struct uath_softc *sc, uint32_t code, const void *idata,
734190688Sweongyo    int ilen, void *odata, int olen, int flags)
735190688Sweongyo{
736190688Sweongyo
737190688Sweongyo	flags |= UATH_CMD_FLAG_READ;
738190688Sweongyo	return uath_cmdsend(sc, code, idata, ilen, odata, olen, flags);
739190688Sweongyo}
740190688Sweongyo
741190688Sweongyostatic int
742190688Sweongyouath_cmd_write(struct uath_softc *sc, uint32_t code, const void *data, int len,
743190688Sweongyo    int flags)
744190688Sweongyo{
745190688Sweongyo
746190688Sweongyo	flags &= ~UATH_CMD_FLAG_READ;
747190688Sweongyo	return uath_cmdsend(sc, code, data, len, NULL, 0, flags);
748190688Sweongyo}
749190688Sweongyo
750190688Sweongyostatic struct uath_cmd *
751190688Sweongyouath_get_cmdbuf(struct uath_softc *sc)
752190688Sweongyo{
753190688Sweongyo	struct uath_cmd *uc;
754190688Sweongyo
755190688Sweongyo	UATH_ASSERT_LOCKED(sc);
756190688Sweongyo
757190688Sweongyo	uc = STAILQ_FIRST(&sc->sc_cmd_inactive);
758190688Sweongyo	if (uc != NULL) {
759190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_inactive, next);
760190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_inactive);
761190688Sweongyo	} else
762190688Sweongyo		uc = NULL;
763190688Sweongyo	if (uc == NULL)
764190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "%s: %s\n", __func__,
765190688Sweongyo		    "out of command xmit buffers");
766190688Sweongyo	return (uc);
767190688Sweongyo}
768190688Sweongyo
769190688Sweongyo/*
770190688Sweongyo * This function is called periodically (every second) when associated to
771190688Sweongyo * query device statistics.
772190688Sweongyo */
773190688Sweongyostatic void
774190688Sweongyouath_stat(void *arg)
775190688Sweongyo{
776190688Sweongyo	struct uath_softc *sc = arg;
777190688Sweongyo	int error;
778190688Sweongyo
779190688Sweongyo	UATH_LOCK(sc);
780190688Sweongyo	/*
781190688Sweongyo	 * Send request for statistics asynchronously. The timer will be
782190688Sweongyo	 * restarted when we'll get the stats notification.
783190688Sweongyo	 */
784190688Sweongyo	error = uath_cmd_write(sc, WDCMSG_TARGET_GET_STATS, NULL, 0,
785190688Sweongyo	    UATH_CMD_FLAG_ASYNC);
786190688Sweongyo	if (error != 0) {
787190688Sweongyo		device_printf(sc->sc_dev,
788190688Sweongyo		    "could not query stats, error %d\n", error);
789190688Sweongyo	}
790190688Sweongyo	UATH_UNLOCK(sc);
791190688Sweongyo}
792190688Sweongyo
793190688Sweongyostatic int
794190688Sweongyouath_get_capability(struct uath_softc *sc, uint32_t cap, uint32_t *val)
795190688Sweongyo{
796190688Sweongyo	int error;
797190688Sweongyo
798190688Sweongyo	cap = htobe32(cap);
799190688Sweongyo	error = uath_cmd_read(sc, WDCMSG_TARGET_GET_CAPABILITY,
800190688Sweongyo	    &cap, sizeof cap, val, sizeof(uint32_t), UATH_CMD_FLAG_MAGIC);
801190688Sweongyo	if (error != 0) {
802190688Sweongyo		device_printf(sc->sc_dev, "could not read capability %u\n",
803190688Sweongyo		    be32toh(cap));
804190688Sweongyo		return (error);
805190688Sweongyo	}
806190688Sweongyo	*val = be32toh(*val);
807190688Sweongyo	return (error);
808190688Sweongyo}
809190688Sweongyo
810190688Sweongyostatic int
811190688Sweongyouath_get_devcap(struct uath_softc *sc)
812190688Sweongyo{
813190688Sweongyo#define	GETCAP(x, v) do {				\
814190688Sweongyo	error = uath_get_capability(sc, x, &v);		\
815190688Sweongyo	if (error != 0)					\
816190688Sweongyo		return (error);				\
817190688Sweongyo	DPRINTF(sc, UATH_DEBUG_DEVCAP,			\
818190688Sweongyo	    "%s: %s=0x%08x\n", __func__, #x, v);	\
819190688Sweongyo} while (0)
820190688Sweongyo	struct uath_devcap *cap = &sc->sc_devcap;
821190688Sweongyo	int error;
822190688Sweongyo
823190688Sweongyo	/* collect device capabilities */
824190688Sweongyo	GETCAP(CAP_TARGET_VERSION, cap->targetVersion);
825190688Sweongyo	GETCAP(CAP_TARGET_REVISION, cap->targetRevision);
826190688Sweongyo	GETCAP(CAP_MAC_VERSION, cap->macVersion);
827190688Sweongyo	GETCAP(CAP_MAC_REVISION, cap->macRevision);
828190688Sweongyo	GETCAP(CAP_PHY_REVISION, cap->phyRevision);
829190688Sweongyo	GETCAP(CAP_ANALOG_5GHz_REVISION, cap->analog5GhzRevision);
830190688Sweongyo	GETCAP(CAP_ANALOG_2GHz_REVISION, cap->analog2GhzRevision);
831190688Sweongyo
832190688Sweongyo	GETCAP(CAP_REG_DOMAIN, cap->regDomain);
833190688Sweongyo	GETCAP(CAP_REG_CAP_BITS, cap->regCapBits);
834190688Sweongyo#if 0
835190688Sweongyo	/* NB: not supported in rev 1.5 */
836190688Sweongyo	GETCAP(CAP_COUNTRY_CODE, cap->countryCode);
837190688Sweongyo#endif
838190688Sweongyo	GETCAP(CAP_WIRELESS_MODES, cap->wirelessModes);
839190688Sweongyo	GETCAP(CAP_CHAN_SPREAD_SUPPORT, cap->chanSpreadSupport);
840190688Sweongyo	GETCAP(CAP_COMPRESS_SUPPORT, cap->compressSupport);
841190688Sweongyo	GETCAP(CAP_BURST_SUPPORT, cap->burstSupport);
842190688Sweongyo	GETCAP(CAP_FAST_FRAMES_SUPPORT, cap->fastFramesSupport);
843190688Sweongyo	GETCAP(CAP_CHAP_TUNING_SUPPORT, cap->chapTuningSupport);
844190688Sweongyo	GETCAP(CAP_TURBOG_SUPPORT, cap->turboGSupport);
845190688Sweongyo	GETCAP(CAP_TURBO_PRIME_SUPPORT, cap->turboPrimeSupport);
846190688Sweongyo	GETCAP(CAP_DEVICE_TYPE, cap->deviceType);
847190688Sweongyo	GETCAP(CAP_WME_SUPPORT, cap->wmeSupport);
848190688Sweongyo	GETCAP(CAP_TOTAL_QUEUES, cap->numTxQueues);
849190688Sweongyo	GETCAP(CAP_CONNECTION_ID_MAX, cap->connectionIdMax);
850190688Sweongyo
851190688Sweongyo	GETCAP(CAP_LOW_5GHZ_CHAN, cap->low5GhzChan);
852190688Sweongyo	GETCAP(CAP_HIGH_5GHZ_CHAN, cap->high5GhzChan);
853190688Sweongyo	GETCAP(CAP_LOW_2GHZ_CHAN, cap->low2GhzChan);
854190688Sweongyo	GETCAP(CAP_HIGH_2GHZ_CHAN, cap->high2GhzChan);
855190688Sweongyo	GETCAP(CAP_TWICE_ANTENNAGAIN_5G, cap->twiceAntennaGain5G);
856190688Sweongyo	GETCAP(CAP_TWICE_ANTENNAGAIN_2G, cap->twiceAntennaGain2G);
857190688Sweongyo
858190688Sweongyo	GETCAP(CAP_CIPHER_AES_CCM, cap->supportCipherAES_CCM);
859190688Sweongyo	GETCAP(CAP_CIPHER_TKIP, cap->supportCipherTKIP);
860190688Sweongyo	GETCAP(CAP_MIC_TKIP, cap->supportMicTKIP);
861190688Sweongyo
862190688Sweongyo	cap->supportCipherWEP = 1;	/* NB: always available */
863190688Sweongyo
864190688Sweongyo	return (0);
865190688Sweongyo}
866190688Sweongyo
867190688Sweongyostatic int
868190688Sweongyouath_get_devstatus(struct uath_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
869190688Sweongyo{
870190688Sweongyo	int error;
871190688Sweongyo
872190688Sweongyo	/* retrieve MAC address */
873190688Sweongyo	error = uath_get_status(sc, ST_MAC_ADDR, macaddr, IEEE80211_ADDR_LEN);
874190688Sweongyo	if (error != 0) {
875190688Sweongyo		device_printf(sc->sc_dev, "could not read MAC address\n");
876190688Sweongyo		return (error);
877190688Sweongyo	}
878190688Sweongyo
879190688Sweongyo	error = uath_get_status(sc, ST_SERIAL_NUMBER,
880190688Sweongyo	    &sc->sc_serial[0], sizeof(sc->sc_serial));
881190688Sweongyo	if (error != 0) {
882190688Sweongyo		device_printf(sc->sc_dev,
883190688Sweongyo		    "could not read device serial number\n");
884190688Sweongyo		return (error);
885190688Sweongyo	}
886190688Sweongyo	return (0);
887190688Sweongyo}
888190688Sweongyo
889190688Sweongyostatic int
890190688Sweongyouath_get_status(struct uath_softc *sc, uint32_t which, void *odata, int olen)
891190688Sweongyo{
892190688Sweongyo	int error;
893190688Sweongyo
894190688Sweongyo	which = htobe32(which);
895190688Sweongyo	error = uath_cmd_read(sc, WDCMSG_TARGET_GET_STATUS,
896190688Sweongyo	    &which, sizeof(which), odata, olen, UATH_CMD_FLAG_MAGIC);
897190688Sweongyo	if (error != 0)
898190688Sweongyo		device_printf(sc->sc_dev,
899190688Sweongyo		    "could not read EEPROM offset 0x%02x\n", be32toh(which));
900190688Sweongyo	return (error);
901190688Sweongyo}
902190688Sweongyo
903190688Sweongyostatic void
904190688Sweongyouath_free_data_list(struct uath_softc *sc, struct uath_data data[], int ndata,
905190688Sweongyo    int fillmbuf)
906190688Sweongyo{
907190688Sweongyo	int i;
908190688Sweongyo
909190688Sweongyo	for (i = 0; i < ndata; i++) {
910190688Sweongyo		struct uath_data *dp = &data[i];
911190688Sweongyo
912190688Sweongyo		if (fillmbuf == 1) {
913190688Sweongyo			if (dp->m != NULL) {
914190688Sweongyo				m_freem(dp->m);
915190688Sweongyo				dp->m = NULL;
916190688Sweongyo				dp->buf = NULL;
917190688Sweongyo			}
918190688Sweongyo		} else {
919244503Shselasky			dp->buf = NULL;
920190688Sweongyo		}
921246614Shselasky		if (dp->ni != NULL) {
922246614Shselasky			ieee80211_free_node(dp->ni);
923246614Shselasky			dp->ni = NULL;
924246614Shselasky		}
925190688Sweongyo	}
926190688Sweongyo}
927190688Sweongyo
928190688Sweongyostatic int
929190688Sweongyouath_alloc_data_list(struct uath_softc *sc, struct uath_data data[],
930244503Shselasky    int ndata, int maxsz, void *dma_buf)
931190688Sweongyo{
932190688Sweongyo	int i, error;
933190688Sweongyo
934190688Sweongyo	for (i = 0; i < ndata; i++) {
935190688Sweongyo		struct uath_data *dp = &data[i];
936190688Sweongyo
937190688Sweongyo		dp->sc = sc;
938244503Shselasky		if (dma_buf == NULL) {
939190688Sweongyo			/* XXX check maxsz */
940243857Sglebius			dp->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
941190688Sweongyo			if (dp->m == NULL) {
942190688Sweongyo				device_printf(sc->sc_dev,
943190688Sweongyo				    "could not allocate rx mbuf\n");
944190688Sweongyo				error = ENOMEM;
945190688Sweongyo				goto fail;
946190688Sweongyo			}
947190688Sweongyo			dp->buf = mtod(dp->m, uint8_t *);
948190688Sweongyo		} else {
949190688Sweongyo			dp->m = NULL;
950244503Shselasky			dp->buf = ((uint8_t *)dma_buf) + (i * maxsz);
951190688Sweongyo		}
952190688Sweongyo		dp->ni = NULL;
953190688Sweongyo	}
954190688Sweongyo
955190688Sweongyo	return (0);
956190688Sweongyo
957244503Shselaskyfail:	uath_free_data_list(sc, data, ndata, 1 /* free mbufs */);
958190688Sweongyo	return (error);
959190688Sweongyo}
960190688Sweongyo
961190688Sweongyostatic int
962190688Sweongyouath_alloc_rx_data_list(struct uath_softc *sc)
963190688Sweongyo{
964190688Sweongyo	int error, i;
965190688Sweongyo
966190688Sweongyo	/* XXX is it enough to store the RX packet with MCLBYTES bytes?  */
967190688Sweongyo	error = uath_alloc_data_list(sc,
968190688Sweongyo	    sc->sc_rx, UATH_RX_DATA_LIST_COUNT, MCLBYTES,
969244503Shselasky	    NULL /* setup mbufs */);
970190688Sweongyo	if (error != 0)
971190688Sweongyo		return (error);
972190688Sweongyo
973190688Sweongyo	STAILQ_INIT(&sc->sc_rx_active);
974190688Sweongyo	STAILQ_INIT(&sc->sc_rx_inactive);
975190688Sweongyo
976190688Sweongyo	for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) {
977190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i],
978190688Sweongyo		    next);
979190688Sweongyo		UATH_STAT_INC(sc, st_rx_inactive);
980190688Sweongyo	}
981190688Sweongyo
982190688Sweongyo	return (0);
983190688Sweongyo}
984190688Sweongyo
985190688Sweongyostatic int
986190688Sweongyouath_alloc_tx_data_list(struct uath_softc *sc)
987190688Sweongyo{
988190688Sweongyo	int error, i;
989190688Sweongyo
990190688Sweongyo	error = uath_alloc_data_list(sc,
991190688Sweongyo	    sc->sc_tx, UATH_TX_DATA_LIST_COUNT, UATH_MAX_TXBUFSZ,
992244503Shselasky	    sc->sc_tx_dma_buf);
993190688Sweongyo	if (error != 0)
994190688Sweongyo		return (error);
995190688Sweongyo
996190688Sweongyo	STAILQ_INIT(&sc->sc_tx_active);
997190688Sweongyo	STAILQ_INIT(&sc->sc_tx_inactive);
998190688Sweongyo	STAILQ_INIT(&sc->sc_tx_pending);
999190688Sweongyo
1000190688Sweongyo	for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) {
1001190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i],
1002190688Sweongyo		    next);
1003190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
1004190688Sweongyo	}
1005190688Sweongyo
1006190688Sweongyo	return (0);
1007190688Sweongyo}
1008190688Sweongyo
1009190688Sweongyostatic void
1010190688Sweongyouath_free_rx_data_list(struct uath_softc *sc)
1011190688Sweongyo{
1012190688Sweongyo	uath_free_data_list(sc, sc->sc_rx, UATH_RX_DATA_LIST_COUNT,
1013190688Sweongyo	    1 /* free mbufs */);
1014190688Sweongyo}
1015190688Sweongyo
1016190688Sweongyostatic void
1017190688Sweongyouath_free_tx_data_list(struct uath_softc *sc)
1018190688Sweongyo{
1019190688Sweongyo	uath_free_data_list(sc, sc->sc_tx, UATH_TX_DATA_LIST_COUNT,
1020190688Sweongyo	    0 /* no mbufs */);
1021190688Sweongyo}
1022190688Sweongyo
1023190688Sweongyostatic struct ieee80211vap *
1024228621Sbschmidtuath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1025228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
1026228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
1027228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
1028190688Sweongyo{
1029190688Sweongyo	struct uath_vap *uvp;
1030190688Sweongyo	struct ieee80211vap *vap;
1031190688Sweongyo
1032190688Sweongyo	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
1033190688Sweongyo		return (NULL);
1034287197Sglebius	uvp =  malloc(sizeof(struct uath_vap), M_80211_VAP, M_WAITOK | M_ZERO);
1035190688Sweongyo	vap = &uvp->vap;
1036190688Sweongyo	/* enable s/w bmiss handling for sta mode */
1037190688Sweongyo
1038257743Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
1039287197Sglebius	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
1040257743Shselasky		/* out of memory */
1041257743Shselasky		free(uvp, M_80211_VAP);
1042257743Shselasky		return (NULL);
1043257743Shselasky	}
1044257743Shselasky
1045190688Sweongyo	/* override state transition machine */
1046190688Sweongyo	uvp->newstate = vap->iv_newstate;
1047190688Sweongyo	vap->iv_newstate = uath_newstate;
1048190688Sweongyo
1049190688Sweongyo	/* complete setup */
1050190688Sweongyo	ieee80211_vap_attach(vap, ieee80211_media_change,
1051287197Sglebius	    ieee80211_media_status, mac);
1052190688Sweongyo	ic->ic_opmode = opmode;
1053190688Sweongyo	return (vap);
1054190688Sweongyo}
1055190688Sweongyo
1056190688Sweongyostatic void
1057190688Sweongyouath_vap_delete(struct ieee80211vap *vap)
1058190688Sweongyo{
1059190688Sweongyo	struct uath_vap *uvp = UATH_VAP(vap);
1060190688Sweongyo
1061190688Sweongyo	ieee80211_vap_detach(vap);
1062190688Sweongyo	free(uvp, M_80211_VAP);
1063190688Sweongyo}
1064190688Sweongyo
1065190688Sweongyostatic int
1066287197Sglebiusuath_init(struct uath_softc *sc)
1067190688Sweongyo{
1068287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1069287197Sglebius	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1070190688Sweongyo	uint32_t val;
1071190688Sweongyo	int error;
1072190688Sweongyo
1073190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1074190688Sweongyo
1075287197Sglebius	if (sc->sc_flags & UATH_FLAG_INITDONE)
1076287197Sglebius		uath_stop(sc);
1077190688Sweongyo
1078190688Sweongyo	/* reset variables */
1079190688Sweongyo	sc->sc_intrx_nextnum = sc->sc_msgid = 0;
1080190688Sweongyo
1081190688Sweongyo	val = htobe32(0);
1082190688Sweongyo	uath_cmd_write(sc, WDCMSG_BIND, &val, sizeof val, 0);
1083190688Sweongyo
1084190688Sweongyo	/* set MAC address */
1085287197Sglebius	uath_config_multi(sc, CFG_MAC_ADDR,
1086287197Sglebius	    vap ? vap->iv_myaddr : ic->ic_macaddr, IEEE80211_ADDR_LEN);
1087190688Sweongyo
1088190688Sweongyo	/* XXX honor net80211 state */
1089190688Sweongyo	uath_config(sc, CFG_RATE_CONTROL_ENABLE, 0x00000001);
1090190688Sweongyo	uath_config(sc, CFG_DIVERSITY_CTL, 0x00000001);
1091190688Sweongyo	uath_config(sc, CFG_ABOLT, 0x0000003f);
1092190688Sweongyo	uath_config(sc, CFG_WME_ENABLED, 0x00000001);
1093190688Sweongyo
1094190688Sweongyo	uath_config(sc, CFG_SERVICE_TYPE, 1);
1095190688Sweongyo	uath_config(sc, CFG_TP_SCALE, 0x00000000);
1096190688Sweongyo	uath_config(sc, CFG_TPC_HALF_DBM5, 0x0000003c);
1097190688Sweongyo	uath_config(sc, CFG_TPC_HALF_DBM2, 0x0000003c);
1098190688Sweongyo	uath_config(sc, CFG_OVERRD_TX_POWER, 0x00000000);
1099190688Sweongyo	uath_config(sc, CFG_GMODE_PROTECTION, 0x00000000);
1100190688Sweongyo	uath_config(sc, CFG_GMODE_PROTECT_RATE_INDEX, 0x00000003);
1101190688Sweongyo	uath_config(sc, CFG_PROTECTION_TYPE, 0x00000000);
1102190688Sweongyo	uath_config(sc, CFG_MODE_CTS, 0x00000002);
1103190688Sweongyo
1104190688Sweongyo	error = uath_cmd_read(sc, WDCMSG_TARGET_START, NULL, 0,
1105190688Sweongyo	    &val, sizeof(val), UATH_CMD_FLAG_MAGIC);
1106190688Sweongyo	if (error) {
1107190688Sweongyo		device_printf(sc->sc_dev,
1108190688Sweongyo		    "could not start target, error %d\n", error);
1109190688Sweongyo		goto fail;
1110190688Sweongyo	}
1111190688Sweongyo	DPRINTF(sc, UATH_DEBUG_INIT, "%s returns handle: 0x%x\n",
1112190688Sweongyo	    uath_codename(WDCMSG_TARGET_START), be32toh(val));
1113190688Sweongyo
1114190688Sweongyo	/* set default channel */
1115190688Sweongyo	error = uath_switch_channel(sc, ic->ic_curchan);
1116190688Sweongyo	if (error) {
1117190688Sweongyo		device_printf(sc->sc_dev,
1118190688Sweongyo		    "could not switch channel, error %d\n", error);
1119190688Sweongyo		goto fail;
1120190688Sweongyo	}
1121190688Sweongyo
1122190688Sweongyo	val = htobe32(TARGET_DEVICE_AWAKE);
1123190688Sweongyo	uath_cmd_write(sc, WDCMSG_SET_PWR_MODE, &val, sizeof val, 0);
1124190688Sweongyo	/* XXX? check */
1125190688Sweongyo	uath_cmd_write(sc, WDCMSG_RESET_KEY_CACHE, NULL, 0, 0);
1126190688Sweongyo
1127194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_BULK_RX]);
1128190688Sweongyo	/* enable Rx */
1129190688Sweongyo	uath_set_rxfilter(sc, 0x0, UATH_FILTER_OP_INIT);
1130190688Sweongyo	uath_set_rxfilter(sc,
1131190688Sweongyo	    UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
1132190688Sweongyo	    UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON,
1133190688Sweongyo	    UATH_FILTER_OP_SET);
1134190688Sweongyo
1135190688Sweongyo	sc->sc_flags |= UATH_FLAG_INITDONE;
1136190688Sweongyo
1137190688Sweongyo	callout_reset(&sc->watchdog_ch, hz, uath_watchdog, sc);
1138190688Sweongyo
1139190688Sweongyo	return (0);
1140190688Sweongyo
1141190688Sweongyofail:
1142287197Sglebius	uath_stop(sc);
1143190688Sweongyo	return (error);
1144190688Sweongyo}
1145190688Sweongyo
1146190688Sweongyostatic void
1147287197Sglebiusuath_stop(struct uath_softc *sc)
1148190688Sweongyo{
1149190688Sweongyo
1150190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1151190688Sweongyo
1152190688Sweongyo	sc->sc_flags &= ~UATH_FLAG_INITDONE;
1153190688Sweongyo
1154190688Sweongyo	callout_stop(&sc->stat_ch);
1155190688Sweongyo	callout_stop(&sc->watchdog_ch);
1156190688Sweongyo	sc->sc_tx_timer = 0;
1157190688Sweongyo	/* abort pending transmits  */
1158190688Sweongyo	uath_abort_xfers(sc);
1159190688Sweongyo	/* flush data & control requests into the target  */
1160190688Sweongyo	(void)uath_flush(sc);
1161190688Sweongyo	/* set a LED status to the disconnected.  */
1162190688Sweongyo	uath_set_ledstate(sc, 0);
1163190688Sweongyo	/* stop the target  */
1164190688Sweongyo	uath_cmd_write(sc, WDCMSG_TARGET_STOP, NULL, 0, 0);
1165190688Sweongyo}
1166190688Sweongyo
1167190688Sweongyostatic int
1168190688Sweongyouath_config(struct uath_softc *sc, uint32_t reg, uint32_t val)
1169190688Sweongyo{
1170190688Sweongyo	struct uath_write_mac write;
1171190688Sweongyo	int error;
1172190688Sweongyo
1173190688Sweongyo	write.reg = htobe32(reg);
1174190688Sweongyo	write.len = htobe32(0);	/* 0 = single write */
1175190688Sweongyo	*(uint32_t *)write.data = htobe32(val);
1176190688Sweongyo
1177190688Sweongyo	error = uath_cmd_write(sc, WDCMSG_TARGET_SET_CONFIG, &write,
1178190688Sweongyo	    3 * sizeof (uint32_t), 0);
1179190688Sweongyo	if (error != 0) {
1180190688Sweongyo		device_printf(sc->sc_dev, "could not write register 0x%02x\n",
1181190688Sweongyo		    reg);
1182190688Sweongyo	}
1183190688Sweongyo	return (error);
1184190688Sweongyo}
1185190688Sweongyo
1186190688Sweongyostatic int
1187190688Sweongyouath_config_multi(struct uath_softc *sc, uint32_t reg, const void *data,
1188190688Sweongyo    int len)
1189190688Sweongyo{
1190190688Sweongyo	struct uath_write_mac write;
1191190688Sweongyo	int error;
1192190688Sweongyo
1193190688Sweongyo	write.reg = htobe32(reg);
1194190688Sweongyo	write.len = htobe32(len);
1195190688Sweongyo	bcopy(data, write.data, len);
1196190688Sweongyo
1197190688Sweongyo	/* properly handle the case where len is zero (reset) */
1198190688Sweongyo	error = uath_cmd_write(sc, WDCMSG_TARGET_SET_CONFIG, &write,
1199190688Sweongyo	    (len == 0) ? sizeof (uint32_t) : 2 * sizeof (uint32_t) + len, 0);
1200190688Sweongyo	if (error != 0) {
1201190688Sweongyo		device_printf(sc->sc_dev,
1202190688Sweongyo		    "could not write %d bytes to register 0x%02x\n", len, reg);
1203190688Sweongyo	}
1204190688Sweongyo	return (error);
1205190688Sweongyo}
1206190688Sweongyo
1207190688Sweongyostatic int
1208190688Sweongyouath_switch_channel(struct uath_softc *sc, struct ieee80211_channel *c)
1209190688Sweongyo{
1210190688Sweongyo	int error;
1211190688Sweongyo
1212190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1213190688Sweongyo
1214190688Sweongyo	/* set radio frequency */
1215190688Sweongyo	error = uath_set_chan(sc, c);
1216190688Sweongyo	if (error) {
1217190688Sweongyo		device_printf(sc->sc_dev,
1218190688Sweongyo		    "could not set channel, error %d\n", error);
1219190688Sweongyo		goto failed;
1220190688Sweongyo	}
1221190688Sweongyo	/* reset Tx rings */
1222190688Sweongyo	error = uath_reset_tx_queues(sc);
1223190688Sweongyo	if (error) {
1224190688Sweongyo		device_printf(sc->sc_dev,
1225190688Sweongyo		    "could not reset Tx queues, error %d\n", error);
1226190688Sweongyo		goto failed;
1227190688Sweongyo	}
1228190688Sweongyo	/* set Tx rings WME properties */
1229190688Sweongyo	error = uath_wme_init(sc);
1230190688Sweongyo	if (error) {
1231190688Sweongyo		device_printf(sc->sc_dev,
1232190688Sweongyo		    "could not init Tx queues, error %d\n", error);
1233190688Sweongyo		goto failed;
1234190688Sweongyo	}
1235190688Sweongyo	error = uath_set_ledstate(sc, 0);
1236190688Sweongyo	if (error) {
1237190688Sweongyo		device_printf(sc->sc_dev,
1238190688Sweongyo		    "could not set led state, error %d\n", error);
1239190688Sweongyo		goto failed;
1240190688Sweongyo	}
1241190688Sweongyo	error = uath_flush(sc);
1242190688Sweongyo	if (error) {
1243190688Sweongyo		device_printf(sc->sc_dev,
1244190688Sweongyo		    "could not flush pipes, error %d\n", error);
1245190688Sweongyo		goto failed;
1246190688Sweongyo	}
1247190688Sweongyofailed:
1248190688Sweongyo	return (error);
1249190688Sweongyo}
1250190688Sweongyo
1251190688Sweongyostatic int
1252190688Sweongyouath_set_rxfilter(struct uath_softc *sc, uint32_t bits, uint32_t op)
1253190688Sweongyo{
1254190688Sweongyo	struct uath_cmd_rx_filter rxfilter;
1255190688Sweongyo
1256190688Sweongyo	rxfilter.bits = htobe32(bits);
1257190688Sweongyo	rxfilter.op = htobe32(op);
1258190688Sweongyo
1259190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
1260190688Sweongyo	    "setting Rx filter=0x%x flags=0x%x\n", bits, op);
1261190688Sweongyo	return uath_cmd_write(sc, WDCMSG_RX_FILTER, &rxfilter,
1262190688Sweongyo	    sizeof rxfilter, 0);
1263190688Sweongyo}
1264190688Sweongyo
1265190688Sweongyostatic void
1266190688Sweongyouath_watchdog(void *arg)
1267190688Sweongyo{
1268190688Sweongyo	struct uath_softc *sc = arg;
1269287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1270190688Sweongyo
1271190688Sweongyo	if (sc->sc_tx_timer > 0) {
1272190688Sweongyo		if (--sc->sc_tx_timer == 0) {
1273190688Sweongyo			device_printf(sc->sc_dev, "device timeout\n");
1274287197Sglebius			counter_u64_add(ic->ic_oerrors, 1);
1275346005Savos			ieee80211_restart_all(ic);
1276190688Sweongyo			return;
1277190688Sweongyo		}
1278190688Sweongyo		callout_reset(&sc->watchdog_ch, hz, uath_watchdog, sc);
1279190688Sweongyo	}
1280190688Sweongyo}
1281190688Sweongyo
1282190688Sweongyostatic void
1283190688Sweongyouath_abort_xfers(struct uath_softc *sc)
1284190688Sweongyo{
1285190688Sweongyo	int i;
1286190688Sweongyo
1287190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1288190688Sweongyo	/* abort any pending transfers */
1289190688Sweongyo	for (i = 0; i < UATH_N_XFERS; i++)
1290194228Sthompsa		usbd_transfer_stop(sc->sc_xfer[i]);
1291190688Sweongyo}
1292190688Sweongyo
1293190688Sweongyostatic int
1294190688Sweongyouath_flush(struct uath_softc *sc)
1295190688Sweongyo{
1296190688Sweongyo	int error;
1297190688Sweongyo
1298190688Sweongyo	error = uath_dataflush(sc);
1299190688Sweongyo	if (error != 0)
1300190688Sweongyo		goto failed;
1301190688Sweongyo
1302190688Sweongyo	error = uath_cmdflush(sc);
1303190688Sweongyo	if (error != 0)
1304190688Sweongyo		goto failed;
1305190688Sweongyo
1306190688Sweongyofailed:
1307190688Sweongyo	return (error);
1308190688Sweongyo}
1309190688Sweongyo
1310190688Sweongyostatic int
1311190688Sweongyouath_cmdflush(struct uath_softc *sc)
1312190688Sweongyo{
1313190688Sweongyo
1314190688Sweongyo	return uath_cmd_write(sc, WDCMSG_FLUSH, NULL, 0, 0);
1315190688Sweongyo}
1316190688Sweongyo
1317190688Sweongyostatic int
1318190688Sweongyouath_dataflush(struct uath_softc *sc)
1319190688Sweongyo{
1320190688Sweongyo	struct uath_data *data;
1321190688Sweongyo	struct uath_chunk *chunk;
1322190688Sweongyo	struct uath_tx_desc *desc;
1323190688Sweongyo
1324190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1325190688Sweongyo
1326190688Sweongyo	data = uath_getbuf(sc);
1327190688Sweongyo	if (data == NULL)
1328190688Sweongyo		return (ENOBUFS);
1329190688Sweongyo	data->buflen = sizeof(struct uath_chunk) + sizeof(struct uath_tx_desc);
1330190688Sweongyo	data->m = NULL;
1331190688Sweongyo	data->ni = NULL;
1332190688Sweongyo	chunk = (struct uath_chunk *)data->buf;
1333190688Sweongyo	desc = (struct uath_tx_desc *)(chunk + 1);
1334190688Sweongyo
1335190688Sweongyo	/* one chunk only */
1336190688Sweongyo	chunk->seqnum = 0;
1337190688Sweongyo	chunk->flags = UATH_CFLAGS_FINAL;
1338190688Sweongyo	chunk->length = htobe16(sizeof (struct uath_tx_desc));
1339190688Sweongyo
1340227461Shselasky	memset(desc, 0, sizeof(struct uath_tx_desc));
1341190688Sweongyo	desc->msglen = htobe32(sizeof(struct uath_tx_desc));
1342190688Sweongyo	desc->msgid  = (sc->sc_msgid++) + 1; /* don't care about endianness */
1343190688Sweongyo	desc->type   = htobe32(WDCMSG_FLUSH);
1344190688Sweongyo	desc->txqid  = htobe32(0);
1345190688Sweongyo	desc->connid = htobe32(0);
1346190688Sweongyo	desc->flags  = htobe32(0);
1347190688Sweongyo
1348190688Sweongyo#ifdef UATH_DEBUG
1349190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_CMDS) {
1350190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RESET, "send flush ix %d\n",
1351190688Sweongyo		    desc->msgid);
1352190688Sweongyo		if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP)
1353190688Sweongyo			uath_dump_cmd(data->buf, data->buflen, '+');
1354190688Sweongyo	}
1355190688Sweongyo#endif
1356190688Sweongyo
1357190688Sweongyo	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1358190688Sweongyo	UATH_STAT_INC(sc, st_tx_pending);
1359190688Sweongyo	sc->sc_tx_timer = 5;
1360194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_BULK_TX]);
1361190688Sweongyo
1362190688Sweongyo	return (0);
1363190688Sweongyo}
1364190688Sweongyo
1365190688Sweongyostatic struct uath_data *
1366190688Sweongyo_uath_getbuf(struct uath_softc *sc)
1367190688Sweongyo{
1368190688Sweongyo	struct uath_data *bf;
1369190688Sweongyo
1370190688Sweongyo	bf = STAILQ_FIRST(&sc->sc_tx_inactive);
1371190688Sweongyo	if (bf != NULL) {
1372190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
1373190688Sweongyo		UATH_STAT_DEC(sc, st_tx_inactive);
1374190688Sweongyo	} else
1375190688Sweongyo		bf = NULL;
1376190688Sweongyo	if (bf == NULL)
1377190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "%s: %s\n", __func__,
1378190688Sweongyo		    "out of xmit buffers");
1379190688Sweongyo	return (bf);
1380190688Sweongyo}
1381190688Sweongyo
1382190688Sweongyostatic struct uath_data *
1383190688Sweongyouath_getbuf(struct uath_softc *sc)
1384190688Sweongyo{
1385190688Sweongyo	struct uath_data *bf;
1386190688Sweongyo
1387190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1388190688Sweongyo
1389190688Sweongyo	bf = _uath_getbuf(sc);
1390287197Sglebius	if (bf == NULL)
1391190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
1392190688Sweongyo	return (bf);
1393190688Sweongyo}
1394190688Sweongyo
1395190688Sweongyostatic int
1396190688Sweongyouath_set_ledstate(struct uath_softc *sc, int connected)
1397190688Sweongyo{
1398190688Sweongyo
1399190688Sweongyo	DPRINTF(sc, UATH_DEBUG_LED,
1400190688Sweongyo	    "set led state %sconnected\n", connected ? "" : "!");
1401190688Sweongyo	connected = htobe32(connected);
1402190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_LED_STATE,
1403190688Sweongyo	     &connected, sizeof connected, 0);
1404190688Sweongyo}
1405190688Sweongyo
1406190688Sweongyostatic int
1407190688Sweongyouath_set_chan(struct uath_softc *sc, struct ieee80211_channel *c)
1408190688Sweongyo{
1409190688Sweongyo#ifdef UATH_DEBUG
1410287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1411190688Sweongyo#endif
1412190688Sweongyo	struct uath_cmd_reset reset;
1413190688Sweongyo
1414227461Shselasky	memset(&reset, 0, sizeof(reset));
1415190688Sweongyo	if (IEEE80211_IS_CHAN_2GHZ(c))
1416190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_2GHZ);
1417190688Sweongyo	if (IEEE80211_IS_CHAN_5GHZ(c))
1418190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_5GHZ);
1419190688Sweongyo	/* NB: 11g =>'s 11b so don't specify both OFDM and CCK */
1420192257Ssam	if (IEEE80211_IS_CHAN_OFDM(c))
1421190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_OFDM);
1422192257Ssam	else if (IEEE80211_IS_CHAN_CCK(c))
1423190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_CCK);
1424190688Sweongyo	/* turbo can be used in either 2GHz or 5GHz */
1425190688Sweongyo	if (c->ic_flags & IEEE80211_CHAN_TURBO)
1426190688Sweongyo		reset.flags |= htobe32(UATH_CHAN_TURBO);
1427190688Sweongyo	reset.freq = htobe32(c->ic_freq);
1428190688Sweongyo	reset.maxrdpower = htobe32(50);	/* XXX */
1429190688Sweongyo	reset.channelchange = htobe32(1);
1430190688Sweongyo	reset.keeprccontent = htobe32(0);
1431190688Sweongyo
1432190688Sweongyo	DPRINTF(sc, UATH_DEBUG_CHANNEL, "set channel %d, flags 0x%x freq %u\n",
1433190688Sweongyo	    ieee80211_chan2ieee(ic, c),
1434190688Sweongyo	    be32toh(reset.flags), be32toh(reset.freq));
1435190688Sweongyo	return uath_cmd_write(sc, WDCMSG_RESET, &reset, sizeof reset, 0);
1436190688Sweongyo}
1437190688Sweongyo
1438190688Sweongyostatic int
1439190688Sweongyouath_reset_tx_queues(struct uath_softc *sc)
1440190688Sweongyo{
1441190688Sweongyo	int ac, error;
1442190688Sweongyo
1443190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RESET, "%s: reset Tx queues\n", __func__);
1444190688Sweongyo	for (ac = 0; ac < 4; ac++) {
1445190688Sweongyo		const uint32_t qid = htobe32(ac);
1446190688Sweongyo
1447190688Sweongyo		error = uath_cmd_write(sc, WDCMSG_RELEASE_TX_QUEUE, &qid,
1448190688Sweongyo		    sizeof qid, 0);
1449190688Sweongyo		if (error != 0)
1450190688Sweongyo			break;
1451190688Sweongyo	}
1452190688Sweongyo	return (error);
1453190688Sweongyo}
1454190688Sweongyo
1455190688Sweongyostatic int
1456190688Sweongyouath_wme_init(struct uath_softc *sc)
1457190688Sweongyo{
1458190688Sweongyo	/* XXX get from net80211 */
1459190688Sweongyo	static const struct uath_wme_settings uath_wme_11g[4] = {
1460190688Sweongyo		{ 7, 4, 10,  0, 0 },	/* Background */
1461190688Sweongyo		{ 3, 4, 10,  0, 0 },	/* Best-Effort */
1462190688Sweongyo		{ 3, 3,  4, 26, 0 },	/* Video */
1463190688Sweongyo		{ 2, 2,  3, 47, 0 }	/* Voice */
1464190688Sweongyo	};
1465190688Sweongyo	struct uath_cmd_txq_setup qinfo;
1466190688Sweongyo	int ac, error;
1467190688Sweongyo
1468190688Sweongyo	DPRINTF(sc, UATH_DEBUG_WME, "%s: setup Tx queues\n", __func__);
1469190688Sweongyo	for (ac = 0; ac < 4; ac++) {
1470190688Sweongyo		qinfo.qid		= htobe32(ac);
1471190688Sweongyo		qinfo.len		= htobe32(sizeof(qinfo.attr));
1472190688Sweongyo		qinfo.attr.priority	= htobe32(ac);	/* XXX */
1473190688Sweongyo		qinfo.attr.aifs		= htobe32(uath_wme_11g[ac].aifsn);
1474190688Sweongyo		qinfo.attr.logcwmin	= htobe32(uath_wme_11g[ac].logcwmin);
1475190688Sweongyo		qinfo.attr.logcwmax	= htobe32(uath_wme_11g[ac].logcwmax);
1476288088Sadrian		qinfo.attr.bursttime	= htobe32(IEEE80211_TXOP_TO_US(
1477190688Sweongyo					    uath_wme_11g[ac].txop));
1478190688Sweongyo		qinfo.attr.mode		= htobe32(uath_wme_11g[ac].acm);/*XXX? */
1479190688Sweongyo		qinfo.attr.qflags	= htobe32(1);	/* XXX? */
1480190688Sweongyo
1481190688Sweongyo		error = uath_cmd_write(sc, WDCMSG_SETUP_TX_QUEUE, &qinfo,
1482190688Sweongyo		    sizeof qinfo, 0);
1483190688Sweongyo		if (error != 0)
1484190688Sweongyo			break;
1485190688Sweongyo	}
1486190688Sweongyo	return (error);
1487190688Sweongyo}
1488190688Sweongyo
1489287197Sglebiusstatic void
1490287197Sglebiusuath_parent(struct ieee80211com *ic)
1491190688Sweongyo{
1492286950Sadrian	struct uath_softc *sc = ic->ic_softc;
1493246614Shselasky	int startall = 0;
1494190688Sweongyo
1495246614Shselasky	UATH_LOCK(sc);
1496287197Sglebius	if (sc->sc_flags & UATH_FLAG_INVALID) {
1497287197Sglebius		UATH_UNLOCK(sc);
1498287197Sglebius		return;
1499287197Sglebius	}
1500286437Sadrian
1501287197Sglebius	if (ic->ic_nrunning > 0) {
1502287197Sglebius		if (!(sc->sc_flags & UATH_FLAG_INITDONE)) {
1503287197Sglebius			uath_init(sc);
1504287197Sglebius			startall = 1;
1505286437Sadrian		}
1506287197Sglebius	} else if (sc->sc_flags & UATH_FLAG_INITDONE)
1507287197Sglebius		uath_stop(sc);
1508287197Sglebius	UATH_UNLOCK(sc);
1509287197Sglebius	if (startall)
1510287197Sglebius		ieee80211_start_all(ic);
1511190688Sweongyo}
1512190688Sweongyo
1513190688Sweongyostatic int
1514190688Sweongyouath_tx_start(struct uath_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1515190688Sweongyo    struct uath_data *data)
1516190688Sweongyo{
1517191746Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1518190688Sweongyo	struct uath_chunk *chunk;
1519190688Sweongyo	struct uath_tx_desc *desc;
1520190688Sweongyo	const struct ieee80211_frame *wh;
1521190688Sweongyo	struct ieee80211_key *k;
1522190688Sweongyo	int framelen, msglen;
1523190688Sweongyo
1524190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1525190688Sweongyo
1526190688Sweongyo	data->ni = ni;
1527190688Sweongyo	data->m = m0;
1528190688Sweongyo	chunk = (struct uath_chunk *)data->buf;
1529190688Sweongyo	desc = (struct uath_tx_desc *)(chunk + 1);
1530190688Sweongyo
1531192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1532190688Sweongyo		struct uath_tx_radiotap_header *tap = &sc->sc_txtap;
1533190688Sweongyo
1534190688Sweongyo		tap->wt_flags = 0;
1535190688Sweongyo		if (m0->m_flags & M_FRAG)
1536190688Sweongyo			tap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1537190688Sweongyo
1538192468Ssam		ieee80211_radiotap_tx(vap, m0);
1539190688Sweongyo	}
1540190688Sweongyo
1541190688Sweongyo	wh = mtod(m0, struct ieee80211_frame *);
1542260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1543190688Sweongyo		k = ieee80211_crypto_encap(ni, m0);
1544190688Sweongyo		if (k == NULL) {
1545190688Sweongyo			m_freem(m0);
1546190688Sweongyo			return (ENOBUFS);
1547190688Sweongyo		}
1548190688Sweongyo
1549190688Sweongyo		/* packet header may have moved, reset our local pointer */
1550190688Sweongyo		wh = mtod(m0, struct ieee80211_frame *);
1551190688Sweongyo	}
1552190688Sweongyo	m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(desc + 1));
1553190688Sweongyo
1554190688Sweongyo	framelen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
1555190688Sweongyo	msglen = framelen + sizeof (struct uath_tx_desc);
1556190688Sweongyo	data->buflen = msglen + sizeof (struct uath_chunk);
1557190688Sweongyo
1558190688Sweongyo	/* one chunk only for now */
1559190688Sweongyo	chunk->seqnum = sc->sc_seqnum++;
1560190688Sweongyo	chunk->flags = (m0->m_flags & M_FRAG) ? 0 : UATH_CFLAGS_FINAL;
1561190688Sweongyo	if (m0->m_flags & M_LASTFRAG)
1562190688Sweongyo		chunk->flags |= UATH_CFLAGS_FINAL;
1563190688Sweongyo	chunk->flags = UATH_CFLAGS_FINAL;
1564190688Sweongyo	chunk->length = htobe16(msglen);
1565190688Sweongyo
1566190688Sweongyo	/* fill Tx descriptor */
1567190688Sweongyo	desc->msglen = htobe32(msglen);
1568190688Sweongyo	/* NB: to get UATH_TX_NOTIFY reply, `msgid' must be larger than 0  */
1569190688Sweongyo	desc->msgid  = (sc->sc_msgid++) + 1; /* don't care about endianness */
1570190688Sweongyo	desc->type   = htobe32(WDCMSG_SEND);
1571190688Sweongyo	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1572190688Sweongyo	case IEEE80211_FC0_TYPE_CTL:
1573190688Sweongyo	case IEEE80211_FC0_TYPE_MGT:
1574190688Sweongyo		/* NB: force all management frames to highest queue */
1575190688Sweongyo		if (ni->ni_flags & IEEE80211_NODE_QOS) {
1576190688Sweongyo			/* NB: force all management frames to highest queue */
1577190688Sweongyo			desc->txqid = htobe32(WME_AC_VO | UATH_TXQID_MINRATE);
1578190688Sweongyo		} else
1579190688Sweongyo			desc->txqid = htobe32(WME_AC_BE | UATH_TXQID_MINRATE);
1580190688Sweongyo		break;
1581190688Sweongyo	case IEEE80211_FC0_TYPE_DATA:
1582190688Sweongyo		/* XXX multicast frames should honor mcastrate */
1583190688Sweongyo		desc->txqid = htobe32(M_WME_GETAC(m0));
1584190688Sweongyo		break;
1585190688Sweongyo	default:
1586190688Sweongyo		device_printf(sc->sc_dev, "bogus frame type 0x%x (%s)\n",
1587190688Sweongyo			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1588190688Sweongyo		m_freem(m0);
1589190688Sweongyo		return (EIO);
1590190688Sweongyo	}
1591191746Sthompsa	if (vap->iv_state == IEEE80211_S_AUTH ||
1592191746Sthompsa	    vap->iv_state == IEEE80211_S_ASSOC ||
1593191746Sthompsa	    vap->iv_state == IEEE80211_S_RUN)
1594190688Sweongyo		desc->connid = htobe32(UATH_ID_BSS);
1595190688Sweongyo	else
1596190688Sweongyo		desc->connid = htobe32(UATH_ID_INVALID);
1597190688Sweongyo	desc->flags  = htobe32(0 /* no UATH_TX_NOTIFY */);
1598190688Sweongyo	desc->buflen = htobe32(m0->m_pkthdr.len);
1599190688Sweongyo
1600190688Sweongyo#ifdef UATH_DEBUG
1601190688Sweongyo	DPRINTF(sc, UATH_DEBUG_XMIT,
1602190688Sweongyo	    "send frame ix %u framelen %d msglen %d connid 0x%x txqid 0x%x\n",
1603190688Sweongyo	    desc->msgid, framelen, msglen, be32toh(desc->connid),
1604190688Sweongyo	    be32toh(desc->txqid));
1605190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_XMIT_DUMP)
1606190688Sweongyo		uath_dump_cmd(data->buf, data->buflen, '+');
1607190688Sweongyo#endif
1608190688Sweongyo
1609190688Sweongyo	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1610190688Sweongyo	UATH_STAT_INC(sc, st_tx_pending);
1611194228Sthompsa	usbd_transfer_start(sc->sc_xfer[UATH_BULK_TX]);
1612190688Sweongyo
1613190688Sweongyo	return (0);
1614190688Sweongyo}
1615190688Sweongyo
1616190688Sweongyo/*
1617190688Sweongyo * Cleanup driver resources when we run out of buffers while processing
1618190688Sweongyo * fragments; return the tx buffers allocated and drop node references.
1619190688Sweongyo */
1620190688Sweongyostatic void
1621190688Sweongyouath_txfrag_cleanup(struct uath_softc *sc,
1622190688Sweongyo    uath_datahead *frags, struct ieee80211_node *ni)
1623190688Sweongyo{
1624190688Sweongyo	struct uath_data *bf, *next;
1625190688Sweongyo
1626190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1627190688Sweongyo
1628190688Sweongyo	STAILQ_FOREACH_SAFE(bf, frags, next, next) {
1629190688Sweongyo		/* NB: bf assumed clean */
1630190688Sweongyo		STAILQ_REMOVE_HEAD(frags, next);
1631190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1632190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
1633190688Sweongyo		ieee80211_node_decref(ni);
1634190688Sweongyo	}
1635190688Sweongyo}
1636190688Sweongyo
1637190688Sweongyo/*
1638190688Sweongyo * Setup xmit of a fragmented frame.  Allocate a buffer for each frag and bump
1639190688Sweongyo * the node reference count to reflect the held reference to be setup by
1640190688Sweongyo * uath_tx_start.
1641190688Sweongyo */
1642190688Sweongyostatic int
1643190688Sweongyouath_txfrag_setup(struct uath_softc *sc, uath_datahead *frags,
1644190688Sweongyo    struct mbuf *m0, struct ieee80211_node *ni)
1645190688Sweongyo{
1646190688Sweongyo	struct mbuf *m;
1647190688Sweongyo	struct uath_data *bf;
1648190688Sweongyo
1649190688Sweongyo	UATH_ASSERT_LOCKED(sc);
1650190688Sweongyo	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
1651190688Sweongyo		bf = uath_getbuf(sc);
1652190688Sweongyo		if (bf == NULL) {       /* out of buffers, cleanup */
1653190688Sweongyo			uath_txfrag_cleanup(sc, frags, ni);
1654190688Sweongyo			break;
1655190688Sweongyo		}
1656190688Sweongyo		ieee80211_node_incref(ni);
1657190688Sweongyo		STAILQ_INSERT_TAIL(frags, bf, next);
1658190688Sweongyo	}
1659190688Sweongyo
1660190688Sweongyo	return !STAILQ_EMPTY(frags);
1661190688Sweongyo}
1662190688Sweongyo
1663287197Sglebiusstatic int
1664287197Sglebiusuath_transmit(struct ieee80211com *ic, struct mbuf *m)
1665287197Sglebius{
1666287197Sglebius	struct uath_softc *sc = ic->ic_softc;
1667287197Sglebius	int error;
1668287197Sglebius
1669287197Sglebius	UATH_LOCK(sc);
1670287197Sglebius	if ((sc->sc_flags & UATH_FLAG_INITDONE) == 0) {
1671287197Sglebius		UATH_UNLOCK(sc);
1672287197Sglebius		return (ENXIO);
1673287197Sglebius	}
1674287197Sglebius	error = mbufq_enqueue(&sc->sc_snd, m);
1675287197Sglebius	if (error) {
1676287197Sglebius		UATH_UNLOCK(sc);
1677287197Sglebius		return (error);
1678287197Sglebius	}
1679287197Sglebius	uath_start(sc);
1680287197Sglebius	UATH_UNLOCK(sc);
1681287197Sglebius
1682287197Sglebius	return (0);
1683287197Sglebius}
1684287197Sglebius
1685190688Sweongyostatic void
1686287197Sglebiusuath_start(struct uath_softc *sc)
1687190688Sweongyo{
1688190688Sweongyo	struct uath_data *bf;
1689190688Sweongyo	struct ieee80211_node *ni;
1690190688Sweongyo	struct mbuf *m, *next;
1691190688Sweongyo	uath_datahead frags;
1692190688Sweongyo
1693287197Sglebius	UATH_ASSERT_LOCKED(sc);
1694287197Sglebius
1695287197Sglebius	if ((sc->sc_flags & UATH_FLAG_INITDONE) == 0 ||
1696190688Sweongyo	    (sc->sc_flags & UATH_FLAG_INVALID))
1697190688Sweongyo		return;
1698190688Sweongyo
1699287197Sglebius	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1700190688Sweongyo		bf = uath_getbuf(sc);
1701287197Sglebius		if (bf == NULL) {
1702287197Sglebius			mbufq_prepend(&sc->sc_snd, m);
1703190688Sweongyo			break;
1704287197Sglebius		}
1705286437Sadrian
1706190688Sweongyo		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1707190688Sweongyo		m->m_pkthdr.rcvif = NULL;
1708190688Sweongyo
1709190688Sweongyo		/*
1710190688Sweongyo		 * Check for fragmentation.  If this frame has been broken up
1711190688Sweongyo		 * verify we have enough buffers to send all the fragments
1712190688Sweongyo		 * so all go out or none...
1713190688Sweongyo		 */
1714190688Sweongyo		STAILQ_INIT(&frags);
1715190688Sweongyo		if ((m->m_flags & M_FRAG) &&
1716190688Sweongyo		    !uath_txfrag_setup(sc, &frags, m, ni)) {
1717190688Sweongyo			DPRINTF(sc, UATH_DEBUG_XMIT,
1718190688Sweongyo			    "%s: out of txfrag buffers\n", __func__);
1719289162Sadrian			ieee80211_free_mbuf(m);
1720190688Sweongyo			goto bad;
1721190688Sweongyo		}
1722190688Sweongyo		sc->sc_seqnum = 0;
1723190688Sweongyo	nextfrag:
1724190688Sweongyo		/*
1725190688Sweongyo		 * Pass the frame to the h/w for transmission.
1726190688Sweongyo		 * Fragmented frames have each frag chained together
1727190688Sweongyo		 * with m_nextpkt.  We know there are sufficient uath_data's
1728190688Sweongyo		 * to send all the frags because of work done by
1729190688Sweongyo		 * uath_txfrag_setup.
1730190688Sweongyo		 */
1731190688Sweongyo		next = m->m_nextpkt;
1732190688Sweongyo		if (uath_tx_start(sc, m, ni, bf) != 0) {
1733190688Sweongyo	bad:
1734287197Sglebius			if_inc_counter(ni->ni_vap->iv_ifp,
1735287197Sglebius			    IFCOUNTER_OERRORS, 1);
1736190688Sweongyo	reclaim:
1737190688Sweongyo			STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1738190688Sweongyo			UATH_STAT_INC(sc, st_tx_inactive);
1739190688Sweongyo			uath_txfrag_cleanup(sc, &frags, ni);
1740190688Sweongyo			ieee80211_free_node(ni);
1741190688Sweongyo			continue;
1742190688Sweongyo		}
1743190688Sweongyo
1744190688Sweongyo		if (next != NULL) {
1745190688Sweongyo			/*
1746190688Sweongyo			 * Beware of state changing between frags.
1747190688Sweongyo			 XXX check sta power-save state?
1748190688Sweongyo			*/
1749190688Sweongyo			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
1750190688Sweongyo				DPRINTF(sc, UATH_DEBUG_XMIT,
1751190688Sweongyo				    "%s: flush fragmented packet, state %s\n",
1752190688Sweongyo				    __func__,
1753190688Sweongyo				    ieee80211_state_name[ni->ni_vap->iv_state]);
1754289162Sadrian				ieee80211_free_mbuf(next);
1755190688Sweongyo				goto reclaim;
1756190688Sweongyo			}
1757190688Sweongyo			m = next;
1758190688Sweongyo			bf = STAILQ_FIRST(&frags);
1759190688Sweongyo			KASSERT(bf != NULL, ("no buf for txfrag"));
1760190688Sweongyo			STAILQ_REMOVE_HEAD(&frags, next);
1761190688Sweongyo			goto nextfrag;
1762190688Sweongyo		}
1763190688Sweongyo
1764190688Sweongyo		sc->sc_tx_timer = 5;
1765190688Sweongyo	}
1766190688Sweongyo}
1767190688Sweongyo
1768190688Sweongyostatic int
1769190688Sweongyouath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1770190688Sweongyo    const struct ieee80211_bpf_params *params)
1771190688Sweongyo{
1772190688Sweongyo	struct ieee80211com *ic = ni->ni_ic;
1773190688Sweongyo	struct uath_data *bf;
1774286950Sadrian	struct uath_softc *sc = ic->ic_softc;
1775190688Sweongyo
1776287197Sglebius	UATH_LOCK(sc);
1777190688Sweongyo	/* prevent management frames from being sent if we're not ready */
1778194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1779287197Sglebius	    !(sc->sc_flags & UATH_FLAG_INITDONE)) {
1780190688Sweongyo		m_freem(m);
1781287197Sglebius		UATH_UNLOCK(sc);
1782190688Sweongyo		return (ENETDOWN);
1783190688Sweongyo	}
1784190688Sweongyo
1785190688Sweongyo	/* grab a TX buffer  */
1786190688Sweongyo	bf = uath_getbuf(sc);
1787190688Sweongyo	if (bf == NULL) {
1788190688Sweongyo		m_freem(m);
1789190688Sweongyo		UATH_UNLOCK(sc);
1790190688Sweongyo		return (ENOBUFS);
1791190688Sweongyo	}
1792190688Sweongyo
1793190688Sweongyo	sc->sc_seqnum = 0;
1794190688Sweongyo	if (uath_tx_start(sc, m, ni, bf) != 0) {
1795190688Sweongyo		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1796190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
1797190688Sweongyo		UATH_UNLOCK(sc);
1798190688Sweongyo		return (EIO);
1799190688Sweongyo	}
1800190688Sweongyo	UATH_UNLOCK(sc);
1801190688Sweongyo
1802190688Sweongyo	sc->sc_tx_timer = 5;
1803190688Sweongyo	return (0);
1804190688Sweongyo}
1805190688Sweongyo
1806190688Sweongyostatic void
1807190688Sweongyouath_scan_start(struct ieee80211com *ic)
1808190688Sweongyo{
1809190688Sweongyo	/* do nothing  */
1810190688Sweongyo}
1811190688Sweongyo
1812190688Sweongyostatic void
1813190688Sweongyouath_scan_end(struct ieee80211com *ic)
1814190688Sweongyo{
1815190688Sweongyo	/* do nothing  */
1816190688Sweongyo}
1817190688Sweongyo
1818190688Sweongyostatic void
1819190688Sweongyouath_set_channel(struct ieee80211com *ic)
1820190688Sweongyo{
1821286950Sadrian	struct uath_softc *sc = ic->ic_softc;
1822190688Sweongyo
1823190688Sweongyo	UATH_LOCK(sc);
1824194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1825287197Sglebius	    (sc->sc_flags & UATH_FLAG_INITDONE) == 0) {
1826194329Sweongyo		UATH_UNLOCK(sc);
1827194329Sweongyo		return;
1828194329Sweongyo	}
1829190688Sweongyo	(void)uath_switch_channel(sc, ic->ic_curchan);
1830190688Sweongyo	UATH_UNLOCK(sc);
1831190688Sweongyo}
1832190688Sweongyo
1833190688Sweongyostatic int
1834190688Sweongyouath_set_rxmulti_filter(struct uath_softc *sc)
1835190688Sweongyo{
1836192468Ssam	/* XXX broken */
1837190688Sweongyo	return (0);
1838190688Sweongyo}
1839190688Sweongyostatic void
1840283540Sglebiusuath_update_mcast(struct ieee80211com *ic)
1841190688Sweongyo{
1842283540Sglebius	struct uath_softc *sc = ic->ic_softc;
1843190688Sweongyo
1844192468Ssam	UATH_LOCK(sc);
1845194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1846287197Sglebius	    (sc->sc_flags & UATH_FLAG_INITDONE) == 0) {
1847194329Sweongyo		UATH_UNLOCK(sc);
1848194329Sweongyo		return;
1849194329Sweongyo	}
1850190688Sweongyo	/*
1851190688Sweongyo	 * this is for avoiding the race condition when we're try to
1852190688Sweongyo	 * connect to the AP with WPA.
1853190688Sweongyo	 */
1854192468Ssam	if (sc->sc_flags & UATH_FLAG_INITDONE)
1855192468Ssam		(void)uath_set_rxmulti_filter(sc);
1856192468Ssam	UATH_UNLOCK(sc);
1857190688Sweongyo}
1858190688Sweongyo
1859190688Sweongyostatic void
1860283540Sglebiusuath_update_promisc(struct ieee80211com *ic)
1861190688Sweongyo{
1862283540Sglebius	struct uath_softc *sc = ic->ic_softc;
1863190688Sweongyo
1864192468Ssam	UATH_LOCK(sc);
1865194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) ||
1866287197Sglebius	    (sc->sc_flags & UATH_FLAG_INITDONE) == 0) {
1867194329Sweongyo		UATH_UNLOCK(sc);
1868194329Sweongyo		return;
1869194329Sweongyo	}
1870192468Ssam	if (sc->sc_flags & UATH_FLAG_INITDONE) {
1871192468Ssam		uath_set_rxfilter(sc,
1872192468Ssam		    UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
1873192468Ssam		    UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON |
1874192468Ssam		    UATH_FILTER_RX_PROM, UATH_FILTER_OP_SET);
1875192468Ssam	}
1876192468Ssam	UATH_UNLOCK(sc);
1877190688Sweongyo}
1878190688Sweongyo
1879190688Sweongyostatic int
1880190688Sweongyouath_create_connection(struct uath_softc *sc, uint32_t connid)
1881190688Sweongyo{
1882190688Sweongyo	const struct ieee80211_rateset *rs;
1883287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1884190688Sweongyo	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1885212127Sthompsa	struct ieee80211_node *ni;
1886190688Sweongyo	struct uath_cmd_create_connection create;
1887190688Sweongyo
1888212127Sthompsa	ni = ieee80211_ref_node(vap->iv_bss);
1889227461Shselasky	memset(&create, 0, sizeof(create));
1890190688Sweongyo	create.connid = htobe32(connid);
1891190688Sweongyo	create.bssid = htobe32(0);
1892190688Sweongyo	/* XXX packed or not?  */
1893190688Sweongyo	create.size = htobe32(sizeof(struct uath_cmd_rateset));
1894190688Sweongyo
1895190688Sweongyo	rs = &ni->ni_rates;
1896190688Sweongyo	create.connattr.rateset.length = rs->rs_nrates;
1897190688Sweongyo	bcopy(rs->rs_rates, &create.connattr.rateset.set[0],
1898190688Sweongyo	    rs->rs_nrates);
1899190688Sweongyo
1900190688Sweongyo	/* XXX turbo */
1901190688Sweongyo	if (IEEE80211_IS_CHAN_A(ni->ni_chan))
1902190688Sweongyo		create.connattr.wlanmode = htobe32(WLAN_MODE_11a);
1903190688Sweongyo	else if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan))
1904190688Sweongyo		create.connattr.wlanmode = htobe32(WLAN_MODE_11g);
1905190688Sweongyo	else
1906190688Sweongyo		create.connattr.wlanmode = htobe32(WLAN_MODE_11b);
1907212127Sthompsa	ieee80211_free_node(ni);
1908190688Sweongyo
1909190688Sweongyo	return uath_cmd_write(sc, WDCMSG_CREATE_CONNECTION, &create,
1910190688Sweongyo	    sizeof create, 0);
1911190688Sweongyo}
1912190688Sweongyo
1913190688Sweongyostatic int
1914190688Sweongyouath_set_rates(struct uath_softc *sc, const struct ieee80211_rateset *rs)
1915190688Sweongyo{
1916190688Sweongyo	struct uath_cmd_rates rates;
1917190688Sweongyo
1918227461Shselasky	memset(&rates, 0, sizeof(rates));
1919190688Sweongyo	rates.connid = htobe32(UATH_ID_BSS);		/* XXX */
1920190688Sweongyo	rates.size   = htobe32(sizeof(struct uath_cmd_rateset));
1921190688Sweongyo	/* XXX bounds check rs->rs_nrates */
1922190688Sweongyo	rates.rateset.length = rs->rs_nrates;
1923190688Sweongyo	bcopy(rs->rs_rates, &rates.rateset.set[0], rs->rs_nrates);
1924190688Sweongyo
1925190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RATES,
1926190688Sweongyo	    "setting supported rates nrates=%d\n", rs->rs_nrates);
1927190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_BASIC_RATE,
1928190688Sweongyo	    &rates, sizeof rates, 0);
1929190688Sweongyo}
1930190688Sweongyo
1931190688Sweongyostatic int
1932190688Sweongyouath_write_associd(struct uath_softc *sc)
1933190688Sweongyo{
1934287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1935190688Sweongyo	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1936212127Sthompsa	struct ieee80211_node *ni;
1937190688Sweongyo	struct uath_cmd_set_associd associd;
1938190688Sweongyo
1939212127Sthompsa	ni = ieee80211_ref_node(vap->iv_bss);
1940227461Shselasky	memset(&associd, 0, sizeof(associd));
1941190688Sweongyo	associd.defaultrateix = htobe32(1);	/* XXX */
1942190688Sweongyo	associd.associd = htobe32(ni->ni_associd);
1943190688Sweongyo	associd.timoffset = htobe32(0x3b);	/* XXX */
1944190688Sweongyo	IEEE80211_ADDR_COPY(associd.bssid, ni->ni_bssid);
1945212127Sthompsa	ieee80211_free_node(ni);
1946190688Sweongyo	return uath_cmd_write(sc, WDCMSG_WRITE_ASSOCID, &associd,
1947190688Sweongyo	    sizeof associd, 0);
1948190688Sweongyo}
1949190688Sweongyo
1950190688Sweongyostatic int
1951190688Sweongyouath_set_ledsteady(struct uath_softc *sc, int lednum, int ledmode)
1952190688Sweongyo{
1953190688Sweongyo	struct uath_cmd_ledsteady led;
1954190688Sweongyo
1955190688Sweongyo	led.lednum = htobe32(lednum);
1956190688Sweongyo	led.ledmode = htobe32(ledmode);
1957190688Sweongyo
1958190688Sweongyo	DPRINTF(sc, UATH_DEBUG_LED, "set %s led %s (steady)\n",
1959190688Sweongyo	    (lednum == UATH_LED_LINK) ? "link" : "activity",
1960190688Sweongyo	    ledmode ? "on" : "off");
1961190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_LED_STEADY, &led, sizeof led, 0);
1962190688Sweongyo}
1963190688Sweongyo
1964190688Sweongyostatic int
1965190688Sweongyouath_set_ledblink(struct uath_softc *sc, int lednum, int ledmode,
1966190688Sweongyo	int blinkrate, int slowmode)
1967190688Sweongyo{
1968190688Sweongyo	struct uath_cmd_ledblink led;
1969190688Sweongyo
1970190688Sweongyo	led.lednum = htobe32(lednum);
1971190688Sweongyo	led.ledmode = htobe32(ledmode);
1972190688Sweongyo	led.blinkrate = htobe32(blinkrate);
1973190688Sweongyo	led.slowmode = htobe32(slowmode);
1974190688Sweongyo
1975190688Sweongyo	DPRINTF(sc, UATH_DEBUG_LED, "set %s led %s (blink)\n",
1976190688Sweongyo	    (lednum == UATH_LED_LINK) ? "link" : "activity",
1977190688Sweongyo	    ledmode ? "on" : "off");
1978190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_LED_BLINK, &led, sizeof led, 0);
1979190688Sweongyo}
1980190688Sweongyo
1981190688Sweongyostatic int
1982190688Sweongyouath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1983190688Sweongyo{
1984190688Sweongyo	enum ieee80211_state ostate = vap->iv_state;
1985190688Sweongyo	int error;
1986212127Sthompsa	struct ieee80211_node *ni;
1987190688Sweongyo	struct ieee80211com *ic = vap->iv_ic;
1988286950Sadrian	struct uath_softc *sc = ic->ic_softc;
1989190688Sweongyo	struct uath_vap *uvp = UATH_VAP(vap);
1990190688Sweongyo
1991190688Sweongyo	DPRINTF(sc, UATH_DEBUG_STATE,
1992190688Sweongyo	    "%s: %s -> %s\n", __func__, ieee80211_state_name[vap->iv_state],
1993190688Sweongyo	    ieee80211_state_name[nstate]);
1994190688Sweongyo
1995191746Sthompsa	IEEE80211_UNLOCK(ic);
1996190688Sweongyo	UATH_LOCK(sc);
1997190688Sweongyo	callout_stop(&sc->stat_ch);
1998190688Sweongyo	callout_stop(&sc->watchdog_ch);
1999212127Sthompsa	ni = ieee80211_ref_node(vap->iv_bss);
2000190688Sweongyo
2001190688Sweongyo	switch (nstate) {
2002190688Sweongyo	case IEEE80211_S_INIT:
2003190688Sweongyo		if (ostate == IEEE80211_S_RUN) {
2004190688Sweongyo			/* turn link and activity LEDs off */
2005190688Sweongyo			uath_set_ledstate(sc, 0);
2006190688Sweongyo		}
2007190688Sweongyo		break;
2008190688Sweongyo
2009190688Sweongyo	case IEEE80211_S_SCAN:
2010190688Sweongyo		break;
2011190688Sweongyo
2012190688Sweongyo	case IEEE80211_S_AUTH:
2013190688Sweongyo		/* XXX good place?  set RTS threshold  */
2014190688Sweongyo		uath_config(sc, CFG_USER_RTS_THRESHOLD, vap->iv_rtsthreshold);
2015190688Sweongyo		/* XXX bad place  */
2016190688Sweongyo		error = uath_set_keys(sc, vap);
2017190688Sweongyo		if (error != 0) {
2018190688Sweongyo			device_printf(sc->sc_dev,
2019190688Sweongyo			    "could not set crypto keys, error %d\n", error);
2020190688Sweongyo			break;
2021190688Sweongyo		}
2022190688Sweongyo		if (uath_switch_channel(sc, ni->ni_chan) != 0) {
2023190688Sweongyo			device_printf(sc->sc_dev, "could not switch channel\n");
2024190688Sweongyo			break;
2025190688Sweongyo		}
2026190688Sweongyo		if (uath_create_connection(sc, UATH_ID_BSS) != 0) {
2027190688Sweongyo			device_printf(sc->sc_dev,
2028190688Sweongyo			    "could not create connection\n");
2029190688Sweongyo			break;
2030190688Sweongyo		}
2031190688Sweongyo		break;
2032190688Sweongyo
2033190688Sweongyo	case IEEE80211_S_ASSOC:
2034190688Sweongyo		if (uath_set_rates(sc, &ni->ni_rates) != 0) {
2035190688Sweongyo			device_printf(sc->sc_dev,
2036190688Sweongyo			    "could not set negotiated rate set\n");
2037190688Sweongyo			break;
2038190688Sweongyo		}
2039190688Sweongyo		break;
2040190688Sweongyo
2041190688Sweongyo	case IEEE80211_S_RUN:
2042190688Sweongyo		/* XXX monitor mode doesn't be tested  */
2043190688Sweongyo		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2044190688Sweongyo			uath_set_ledstate(sc, 1);
2045190688Sweongyo			break;
2046190688Sweongyo		}
2047190688Sweongyo
2048190688Sweongyo		/*
2049190688Sweongyo		 * Tx rate is controlled by firmware, report the maximum
2050190688Sweongyo		 * negotiated rate in ifconfig output.
2051190688Sweongyo		 */
2052190688Sweongyo		ni->ni_txrate = ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates-1];
2053190688Sweongyo
2054190688Sweongyo		if (uath_write_associd(sc) != 0) {
2055190688Sweongyo			device_printf(sc->sc_dev,
2056190688Sweongyo			    "could not write association id\n");
2057190688Sweongyo			break;
2058190688Sweongyo		}
2059190688Sweongyo		/* turn link LED on */
2060190688Sweongyo		uath_set_ledsteady(sc, UATH_LED_LINK, UATH_LED_ON);
2061190688Sweongyo		/* make activity LED blink */
2062190688Sweongyo		uath_set_ledblink(sc, UATH_LED_ACTIVITY, UATH_LED_ON, 1, 2);
2063190688Sweongyo		/* set state to associated */
2064190688Sweongyo		uath_set_ledstate(sc, 1);
2065190688Sweongyo
2066190688Sweongyo		/* start statistics timer */
2067190688Sweongyo		callout_reset(&sc->stat_ch, hz, uath_stat, sc);
2068190688Sweongyo		break;
2069190688Sweongyo	default:
2070190688Sweongyo		break;
2071190688Sweongyo	}
2072212127Sthompsa	ieee80211_free_node(ni);
2073190688Sweongyo	UATH_UNLOCK(sc);
2074190688Sweongyo	IEEE80211_LOCK(ic);
2075191746Sthompsa	return (uvp->newstate(vap, nstate, arg));
2076190688Sweongyo}
2077190688Sweongyo
2078190688Sweongyostatic int
2079190688Sweongyouath_set_key(struct uath_softc *sc, const struct ieee80211_key *wk,
2080190688Sweongyo    int index)
2081190688Sweongyo{
2082190688Sweongyo#if 0
2083190688Sweongyo	struct uath_cmd_crypto crypto;
2084190688Sweongyo	int i;
2085190688Sweongyo
2086227461Shselasky	memset(&crypto, 0, sizeof(crypto));
2087190688Sweongyo	crypto.keyidx = htobe32(index);
2088190688Sweongyo	crypto.magic1 = htobe32(1);
2089190688Sweongyo	crypto.size   = htobe32(368);
2090190688Sweongyo	crypto.mask   = htobe32(0xffff);
2091190688Sweongyo	crypto.flags  = htobe32(0x80000068);
2092190688Sweongyo	if (index != UATH_DEFAULT_KEY)
2093190688Sweongyo		crypto.flags |= htobe32(index << 16);
2094227461Shselasky	memset(crypto.magic2, 0xff, sizeof(crypto.magic2));
2095190688Sweongyo
2096190688Sweongyo	/*
2097190688Sweongyo	 * Each byte of the key must be XOR'ed with 10101010 before being
2098190688Sweongyo	 * transmitted to the firmware.
2099190688Sweongyo	 */
2100190688Sweongyo	for (i = 0; i < wk->wk_keylen; i++)
2101190688Sweongyo		crypto.key[i] = wk->wk_key[i] ^ 0xaa;
2102190688Sweongyo
2103190688Sweongyo	DPRINTF(sc, UATH_DEBUG_CRYPTO,
2104190688Sweongyo	    "setting crypto key index=%d len=%d\n", index, wk->wk_keylen);
2105190688Sweongyo	return uath_cmd_write(sc, WDCMSG_SET_KEY_CACHE_ENTRY, &crypto,
2106190688Sweongyo	    sizeof crypto, 0);
2107190688Sweongyo#else
2108190688Sweongyo	/* XXX support H/W cryto  */
2109190688Sweongyo	return (0);
2110190688Sweongyo#endif
2111190688Sweongyo}
2112190688Sweongyo
2113190688Sweongyostatic int
2114190688Sweongyouath_set_keys(struct uath_softc *sc, struct ieee80211vap *vap)
2115190688Sweongyo{
2116190688Sweongyo	int i, error;
2117190688Sweongyo
2118190688Sweongyo	error = 0;
2119190688Sweongyo	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2120190688Sweongyo		const struct ieee80211_key *wk = &vap->iv_nw_keys[i];
2121190688Sweongyo
2122190688Sweongyo		if (wk->wk_flags & (IEEE80211_KEY_XMIT|IEEE80211_KEY_RECV)) {
2123190688Sweongyo			error = uath_set_key(sc, wk, i);
2124190688Sweongyo			if (error)
2125190688Sweongyo				return (error);
2126190688Sweongyo		}
2127190688Sweongyo	}
2128190688Sweongyo	if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2129190688Sweongyo		error = uath_set_key(sc, &vap->iv_nw_keys[vap->iv_def_txkey],
2130190688Sweongyo			UATH_DEFAULT_KEY);
2131190688Sweongyo	}
2132190688Sweongyo	return (error);
2133190688Sweongyo}
2134190688Sweongyo
2135190688Sweongyo#define	UATH_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2136190688Sweongyo	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2137190688Sweongyo
2138190688Sweongyostatic void
2139190688Sweongyouath_sysctl_node(struct uath_softc *sc)
2140190688Sweongyo{
2141190688Sweongyo	struct sysctl_ctx_list *ctx;
2142190688Sweongyo	struct sysctl_oid_list *child;
2143190688Sweongyo	struct sysctl_oid *tree;
2144190688Sweongyo	struct uath_stat *stats;
2145190688Sweongyo
2146190688Sweongyo	stats = &sc->sc_stat;
2147190688Sweongyo	ctx = device_get_sysctl_ctx(sc->sc_dev);
2148190688Sweongyo	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2149190688Sweongyo
2150190688Sweongyo	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2151190688Sweongyo	    NULL, "UATH statistics");
2152190688Sweongyo	child = SYSCTL_CHILDREN(tree);
2153190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "badchunkseqnum",
2154190688Sweongyo	    &stats->st_badchunkseqnum, "Bad chunk sequence numbers");
2155190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "invalidlen", &stats->st_invalidlen,
2156190688Sweongyo	    "Invalid length");
2157190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "multichunk", &stats->st_multichunk,
2158190688Sweongyo	    "Multi chunks");
2159190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "toobigrxpkt",
2160190688Sweongyo	    &stats->st_toobigrxpkt, "Too big rx packets");
2161190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "stopinprogress",
2162190688Sweongyo	    &stats->st_stopinprogress, "Stop in progress");
2163190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "crcerrs", &stats->st_crcerr,
2164190688Sweongyo	    "CRC errors");
2165190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "phyerr", &stats->st_phyerr,
2166190688Sweongyo	    "PHY errors");
2167190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "decrypt_crcerr",
2168190688Sweongyo	    &stats->st_decrypt_crcerr, "Decryption CRC errors");
2169190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "decrypt_micerr",
2170190688Sweongyo	    &stats->st_decrypt_micerr, "Decryption Misc errors");
2171190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "decomperr", &stats->st_decomperr,
2172190688Sweongyo	    "Decomp errors");
2173190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "keyerr", &stats->st_keyerr,
2174190688Sweongyo	    "Key errors");
2175190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "err", &stats->st_err,
2176190688Sweongyo	    "Unknown errors");
2177190688Sweongyo
2178190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_active",
2179190688Sweongyo	    &stats->st_cmd_active, "Active numbers in Command queue");
2180190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_inactive",
2181190688Sweongyo	    &stats->st_cmd_inactive, "Inactive numbers in Command queue");
2182190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_pending",
2183190688Sweongyo	    &stats->st_cmd_pending, "Pending numbers in Command queue");
2184190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_waiting",
2185190688Sweongyo	    &stats->st_cmd_waiting, "Waiting numbers in Command queue");
2186190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "rx_active",
2187190688Sweongyo	    &stats->st_rx_active, "Active numbers in RX queue");
2188190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "rx_inactive",
2189190688Sweongyo	    &stats->st_rx_inactive, "Inactive numbers in RX queue");
2190190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2191190688Sweongyo	    &stats->st_tx_active, "Active numbers in TX queue");
2192190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2193190688Sweongyo	    &stats->st_tx_inactive, "Inactive numbers in TX queue");
2194190688Sweongyo	UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2195190688Sweongyo	    &stats->st_tx_pending, "Pending numbers in TX queue");
2196190688Sweongyo}
2197190688Sweongyo
2198190688Sweongyo#undef UATH_SYSCTL_STAT_ADD32
2199190688Sweongyo
2200190688Sweongyostatic void
2201190688Sweongyouath_cmdeof(struct uath_softc *sc, struct uath_cmd *cmd)
2202190688Sweongyo{
2203190688Sweongyo	struct uath_cmd_hdr *hdr;
2204190688Sweongyo	int dlen;
2205190688Sweongyo
2206190688Sweongyo	hdr = (struct uath_cmd_hdr *)cmd->buf;
2207190688Sweongyo	/* NB: msgid is passed thru w/o byte swapping */
2208190688Sweongyo#ifdef UATH_DEBUG
2209190688Sweongyo	if (sc->sc_debug & UATH_DEBUG_CMDS) {
2210190688Sweongyo		int len = be32toh(hdr->len);
2211190688Sweongyo		printf("%s: %s [ix %u] len %u status %u\n",
2212190688Sweongyo		    __func__, uath_codename(be32toh(hdr->code)),
2213190688Sweongyo		    hdr->msgid, len, be32toh(hdr->magic));
2214190688Sweongyo		if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP)
2215190688Sweongyo			uath_dump_cmd(cmd->buf,
2216190688Sweongyo			    len > UATH_MAX_CMDSZ ? sizeof(*hdr) : len, '-');
2217190688Sweongyo	}
2218190688Sweongyo#endif
2219190688Sweongyo	hdr->code = be32toh(hdr->code);
2220190688Sweongyo	hdr->len = be32toh(hdr->len);
2221190688Sweongyo	hdr->magic = be32toh(hdr->magic);	/* target status on return */
2222190688Sweongyo
2223190688Sweongyo	switch (hdr->code & 0xff) {
2224190688Sweongyo	/* reply to a read command */
2225190688Sweongyo	default:
2226190688Sweongyo		dlen = hdr->len - sizeof(*hdr);
2227233774Shselasky		if (dlen < 0) {
2228233774Shselasky			device_printf(sc->sc_dev,
2229233774Shselasky			    "Invalid header length %d\n", dlen);
2230233774Shselasky			return;
2231233774Shselasky		}
2232190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL,
2233190688Sweongyo		    "%s: code %d data len %u\n",
2234190688Sweongyo		    __func__, hdr->code & 0xff, dlen);
2235190688Sweongyo		/*
2236190688Sweongyo		 * The first response from the target after the
2237190688Sweongyo		 * HOST_AVAILABLE has an invalid msgid so we must
2238190688Sweongyo		 * treat it specially.
2239190688Sweongyo		 */
2240190688Sweongyo		if (hdr->msgid < UATH_CMD_LIST_COUNT) {
2241190688Sweongyo			uint32_t *rp = (uint32_t *)(hdr+1);
2242190688Sweongyo			u_int olen;
2243190688Sweongyo
2244190688Sweongyo			if (!(sizeof(*hdr) <= hdr->len &&
2245190688Sweongyo			      hdr->len < UATH_MAX_CMDSZ)) {
2246190688Sweongyo				device_printf(sc->sc_dev,
2247190688Sweongyo				    "%s: invalid WDC msg length %u; "
2248190688Sweongyo				    "msg ignored\n", __func__, hdr->len);
2249190688Sweongyo				return;
2250190688Sweongyo			}
2251190688Sweongyo			/*
2252190688Sweongyo			 * Calculate return/receive payload size; the
2253190688Sweongyo			 * first word, if present, always gives the
2254190688Sweongyo			 * number of bytes--unless it's 0 in which
2255190688Sweongyo			 * case a single 32-bit word should be present.
2256190688Sweongyo			 */
2257233774Shselasky			if (dlen >= (int)sizeof(uint32_t)) {
2258190688Sweongyo				olen = be32toh(rp[0]);
2259190688Sweongyo				dlen -= sizeof(uint32_t);
2260190688Sweongyo				if (olen == 0) {
2261190688Sweongyo					/* convention is 0 =>'s one word */
2262190688Sweongyo					olen = sizeof(uint32_t);
2263190688Sweongyo					/* XXX KASSERT(olen == dlen ) */
2264190688Sweongyo				}
2265190688Sweongyo			} else
2266190688Sweongyo				olen = 0;
2267190688Sweongyo			if (cmd->odata != NULL) {
2268190688Sweongyo				/* NB: cmd->olen validated in uath_cmd */
2269233774Shselasky				if (olen > (u_int)cmd->olen) {
2270190688Sweongyo					/* XXX complain? */
2271190688Sweongyo					device_printf(sc->sc_dev,
2272190688Sweongyo					    "%s: cmd 0x%x olen %u cmd olen %u\n",
2273190688Sweongyo					    __func__, hdr->code, olen,
2274190688Sweongyo					    cmd->olen);
2275190688Sweongyo					olen = cmd->olen;
2276190688Sweongyo				}
2277233774Shselasky				if (olen > (u_int)dlen) {
2278190688Sweongyo					/* XXX complain, shouldn't happen */
2279190688Sweongyo					device_printf(sc->sc_dev,
2280190688Sweongyo					    "%s: cmd 0x%x olen %u dlen %u\n",
2281190688Sweongyo					    __func__, hdr->code, olen, dlen);
2282190688Sweongyo					olen = dlen;
2283190688Sweongyo				}
2284190688Sweongyo				/* XXX have submitter do this */
2285190688Sweongyo				/* copy answer into caller's supplied buffer */
2286190688Sweongyo				bcopy(&rp[1], cmd->odata, olen);
2287190688Sweongyo				cmd->olen = olen;
2288190688Sweongyo			}
2289190688Sweongyo		}
2290190688Sweongyo		wakeup_one(cmd);		/* wake up caller */
2291190688Sweongyo		break;
2292190688Sweongyo
2293190688Sweongyo	case WDCMSG_TARGET_START:
2294190688Sweongyo		if (hdr->msgid >= UATH_CMD_LIST_COUNT) {
2295190688Sweongyo			/* XXX */
2296190688Sweongyo			return;
2297190688Sweongyo		}
2298190688Sweongyo		dlen = hdr->len - sizeof(*hdr);
2299233774Shselasky		if (dlen != (int)sizeof(uint32_t)) {
2300190688Sweongyo			/* XXX something wrong */
2301190688Sweongyo			return;
2302190688Sweongyo		}
2303190688Sweongyo		/* XXX have submitter do this */
2304190688Sweongyo		/* copy answer into caller's supplied buffer */
2305190688Sweongyo		bcopy(hdr+1, cmd->odata, sizeof(uint32_t));
2306190688Sweongyo		cmd->olen = sizeof(uint32_t);
2307190688Sweongyo		wakeup_one(cmd);		/* wake up caller */
2308190688Sweongyo		break;
2309190688Sweongyo
2310190688Sweongyo	case WDCMSG_SEND_COMPLETE:
2311190688Sweongyo		/* this notification is sent when UATH_TX_NOTIFY is set */
2312190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL,
2313190688Sweongyo		    "%s: received Tx notification\n", __func__);
2314190688Sweongyo		break;
2315190688Sweongyo
2316190688Sweongyo	case WDCMSG_TARGET_GET_STATS:
2317190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL,
2318190688Sweongyo		    "%s: received device statistics\n", __func__);
2319190688Sweongyo		callout_reset(&sc->stat_ch, hz, uath_stat, sc);
2320190688Sweongyo		break;
2321190688Sweongyo	}
2322190688Sweongyo}
2323190688Sweongyo
2324190688Sweongyostatic void
2325194677Sthompsauath_intr_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2326190688Sweongyo{
2327194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2328190688Sweongyo	struct uath_cmd *cmd;
2329194677Sthompsa	struct usb_page_cache *pc;
2330194677Sthompsa	int actlen;
2331190688Sweongyo
2332194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2333194677Sthompsa
2334190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2335190688Sweongyo
2336190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2337190688Sweongyo	case USB_ST_TRANSFERRED:
2338190688Sweongyo		cmd = STAILQ_FIRST(&sc->sc_cmd_waiting);
2339190688Sweongyo		if (cmd == NULL)
2340190688Sweongyo			goto setup;
2341190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_waiting, next);
2342190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_waiting);
2343190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_cmd_inactive, cmd, next);
2344190688Sweongyo		UATH_STAT_INC(sc, st_cmd_inactive);
2345190688Sweongyo
2346233774Shselasky		KASSERT(actlen >= (int)sizeof(struct uath_cmd_hdr),
2347190688Sweongyo		    ("short xfer error"));
2348194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
2349194677Sthompsa		usbd_copy_out(pc, 0, cmd->buf, actlen);
2350190688Sweongyo		uath_cmdeof(sc, cmd);
2351190688Sweongyo	case USB_ST_SETUP:
2352190688Sweongyosetup:
2353194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2354194228Sthompsa		usbd_transfer_submit(xfer);
2355190688Sweongyo		break;
2356190688Sweongyo	default:
2357194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2358194677Sthompsa			usbd_xfer_set_stall(xfer);
2359190688Sweongyo			goto setup;
2360190688Sweongyo		}
2361190688Sweongyo		break;
2362190688Sweongyo	}
2363190688Sweongyo}
2364190688Sweongyo
2365190688Sweongyostatic void
2366194677Sthompsauath_intr_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2367190688Sweongyo{
2368194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2369190688Sweongyo	struct uath_cmd *cmd;
2370190688Sweongyo
2371190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2372190688Sweongyo
2373246570Shselasky	cmd = STAILQ_FIRST(&sc->sc_cmd_active);
2374246570Shselasky	if (cmd != NULL && USB_GET_STATE(xfer) != USB_ST_SETUP) {
2375190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_active, next);
2376190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_active);
2377190688Sweongyo		STAILQ_INSERT_TAIL((cmd->flags & UATH_CMD_FLAG_READ) ?
2378190688Sweongyo		    &sc->sc_cmd_waiting : &sc->sc_cmd_inactive, cmd, next);
2379190688Sweongyo		if (cmd->flags & UATH_CMD_FLAG_READ)
2380190688Sweongyo			UATH_STAT_INC(sc, st_cmd_waiting);
2381190688Sweongyo		else
2382190688Sweongyo			UATH_STAT_INC(sc, st_cmd_inactive);
2383246570Shselasky	}
2384246570Shselasky
2385246570Shselasky	switch (USB_GET_STATE(xfer)) {
2386246570Shselasky	case USB_ST_TRANSFERRED:
2387190688Sweongyo	case USB_ST_SETUP:
2388190688Sweongyosetup:
2389190688Sweongyo		cmd = STAILQ_FIRST(&sc->sc_cmd_pending);
2390190688Sweongyo		if (cmd == NULL) {
2391190688Sweongyo			DPRINTF(sc, UATH_DEBUG_XMIT, "%s: empty pending queue\n",
2392190688Sweongyo			    __func__);
2393190688Sweongyo			return;
2394190688Sweongyo		}
2395190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_cmd_pending, next);
2396190688Sweongyo		UATH_STAT_DEC(sc, st_cmd_pending);
2397190688Sweongyo		STAILQ_INSERT_TAIL((cmd->flags & UATH_CMD_FLAG_ASYNC) ?
2398190688Sweongyo		    &sc->sc_cmd_inactive : &sc->sc_cmd_active, cmd, next);
2399190688Sweongyo		if (cmd->flags & UATH_CMD_FLAG_ASYNC)
2400190688Sweongyo			UATH_STAT_INC(sc, st_cmd_inactive);
2401190688Sweongyo		else
2402190688Sweongyo			UATH_STAT_INC(sc, st_cmd_active);
2403190688Sweongyo
2404194677Sthompsa		usbd_xfer_set_frame_data(xfer, 0, cmd->buf, cmd->buflen);
2405194228Sthompsa		usbd_transfer_submit(xfer);
2406190688Sweongyo		break;
2407190688Sweongyo	default:
2408194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2409194677Sthompsa			usbd_xfer_set_stall(xfer);
2410190688Sweongyo			goto setup;
2411190688Sweongyo		}
2412190688Sweongyo		break;
2413190688Sweongyo	}
2414190688Sweongyo}
2415190688Sweongyo
2416190688Sweongyostatic void
2417190688Sweongyouath_update_rxstat(struct uath_softc *sc, uint32_t status)
2418190688Sweongyo{
2419190688Sweongyo
2420190688Sweongyo	switch (status) {
2421190688Sweongyo	case UATH_STATUS_STOP_IN_PROGRESS:
2422190688Sweongyo		UATH_STAT_INC(sc, st_stopinprogress);
2423190688Sweongyo		break;
2424190688Sweongyo	case UATH_STATUS_CRC_ERR:
2425190688Sweongyo		UATH_STAT_INC(sc, st_crcerr);
2426190688Sweongyo		break;
2427190688Sweongyo	case UATH_STATUS_PHY_ERR:
2428190688Sweongyo		UATH_STAT_INC(sc, st_phyerr);
2429190688Sweongyo		break;
2430190688Sweongyo	case UATH_STATUS_DECRYPT_CRC_ERR:
2431190688Sweongyo		UATH_STAT_INC(sc, st_decrypt_crcerr);
2432190688Sweongyo		break;
2433190688Sweongyo	case UATH_STATUS_DECRYPT_MIC_ERR:
2434190688Sweongyo		UATH_STAT_INC(sc, st_decrypt_micerr);
2435190688Sweongyo		break;
2436190688Sweongyo	case UATH_STATUS_DECOMP_ERR:
2437190688Sweongyo		UATH_STAT_INC(sc, st_decomperr);
2438190688Sweongyo		break;
2439190688Sweongyo	case UATH_STATUS_KEY_ERR:
2440190688Sweongyo		UATH_STAT_INC(sc, st_keyerr);
2441190688Sweongyo		break;
2442190688Sweongyo	case UATH_STATUS_ERR:
2443190688Sweongyo		UATH_STAT_INC(sc, st_err);
2444190688Sweongyo		break;
2445190688Sweongyo	default:
2446190688Sweongyo		break;
2447190688Sweongyo	}
2448190688Sweongyo}
2449190688Sweongyo
2450190688Sweongyostatic struct mbuf *
2451192984Sthompsauath_data_rxeof(struct usb_xfer *xfer, struct uath_data *data,
2452190688Sweongyo    struct uath_rx_desc **pdesc)
2453190688Sweongyo{
2454194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2455287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2456190688Sweongyo	struct uath_chunk *chunk;
2457190688Sweongyo	struct uath_rx_desc *desc;
2458190688Sweongyo	struct mbuf *m = data->m, *mnew, *mp;
2459190688Sweongyo	uint16_t chunklen;
2460194677Sthompsa	int actlen;
2461190688Sweongyo
2462194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2463194677Sthompsa
2464233774Shselasky	if (actlen < (int)UATH_MIN_RXBUFSZ) {
2465190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2466194677Sthompsa		    "%s: wrong xfer size (len=%d)\n", __func__, actlen);
2467287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2468190688Sweongyo		return (NULL);
2469190688Sweongyo	}
2470190688Sweongyo
2471190688Sweongyo	chunk = (struct uath_chunk *)data->buf;
2472190688Sweongyo	if (chunk->seqnum == 0 && chunk->flags == 0 && chunk->length == 0) {
2473190688Sweongyo		device_printf(sc->sc_dev, "%s: strange response\n", __func__);
2474287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2475190688Sweongyo		UATH_RESET_INTRX(sc);
2476190688Sweongyo		return (NULL);
2477190688Sweongyo	}
2478190688Sweongyo
2479190688Sweongyo	if (chunk->seqnum != sc->sc_intrx_nextnum) {
2480190688Sweongyo		DPRINTF(sc, UATH_DEBUG_XMIT, "invalid seqnum %d, expected %d\n",
2481190688Sweongyo		    chunk->seqnum, sc->sc_intrx_nextnum);
2482190688Sweongyo		UATH_STAT_INC(sc, st_badchunkseqnum);
2483190688Sweongyo		if (sc->sc_intrx_head != NULL)
2484190688Sweongyo			m_freem(sc->sc_intrx_head);
2485190688Sweongyo		UATH_RESET_INTRX(sc);
2486190688Sweongyo		return (NULL);
2487190688Sweongyo	}
2488190688Sweongyo
2489190688Sweongyo	/* check multi-chunk frames  */
2490190688Sweongyo	if ((chunk->seqnum == 0 && !(chunk->flags & UATH_CFLAGS_FINAL)) ||
2491190688Sweongyo	    (chunk->seqnum != 0 && (chunk->flags & UATH_CFLAGS_FINAL)) ||
2492190688Sweongyo	    chunk->flags & UATH_CFLAGS_RXMSG)
2493190688Sweongyo		UATH_STAT_INC(sc, st_multichunk);
2494190688Sweongyo
2495190688Sweongyo	chunklen = be16toh(chunk->length);
2496190688Sweongyo	if (chunk->flags & UATH_CFLAGS_FINAL)
2497190688Sweongyo		chunklen -= sizeof(struct uath_rx_desc);
2498190688Sweongyo
2499190688Sweongyo	if (chunklen > 0 &&
2500190688Sweongyo	    (!(chunk->flags & UATH_CFLAGS_FINAL) || !(chunk->seqnum == 0))) {
2501190688Sweongyo		/* we should use intermediate RX buffer  */
2502190688Sweongyo		if (chunk->seqnum == 0)
2503190688Sweongyo			UATH_RESET_INTRX(sc);
2504190688Sweongyo		if ((sc->sc_intrx_len + sizeof(struct uath_rx_desc) +
2505190688Sweongyo		    chunklen) > UATH_MAX_INTRX_SIZE) {
2506190688Sweongyo			UATH_STAT_INC(sc, st_invalidlen);
2507287197Sglebius			counter_u64_add(ic->ic_ierrors, 1);
2508190688Sweongyo			if (sc->sc_intrx_head != NULL)
2509190688Sweongyo				m_freem(sc->sc_intrx_head);
2510190688Sweongyo			UATH_RESET_INTRX(sc);
2511190688Sweongyo			return (NULL);
2512190688Sweongyo		}
2513190688Sweongyo
2514190688Sweongyo		m->m_len = chunklen;
2515190688Sweongyo		m->m_data += sizeof(struct uath_chunk);
2516190688Sweongyo
2517190688Sweongyo		if (sc->sc_intrx_head == NULL) {
2518190688Sweongyo			sc->sc_intrx_head = m;
2519190688Sweongyo			sc->sc_intrx_tail = m;
2520190688Sweongyo		} else {
2521190688Sweongyo			m->m_flags &= ~M_PKTHDR;
2522190688Sweongyo			sc->sc_intrx_tail->m_next = m;
2523190688Sweongyo			sc->sc_intrx_tail = m;
2524190688Sweongyo		}
2525190688Sweongyo	}
2526190688Sweongyo	sc->sc_intrx_len += chunklen;
2527190688Sweongyo
2528243857Sglebius	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2529190688Sweongyo	if (mnew == NULL) {
2530190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2531190688Sweongyo		    "%s: can't get new mbuf, drop frame\n", __func__);
2532287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2533190688Sweongyo		if (sc->sc_intrx_head != NULL)
2534190688Sweongyo			m_freem(sc->sc_intrx_head);
2535190688Sweongyo		UATH_RESET_INTRX(sc);
2536190688Sweongyo		return (NULL);
2537190688Sweongyo	}
2538190688Sweongyo
2539190688Sweongyo	data->m = mnew;
2540190688Sweongyo	data->buf = mtod(mnew, uint8_t *);
2541190688Sweongyo
2542190688Sweongyo	/* if the frame is not final continue the transfer  */
2543190688Sweongyo	if (!(chunk->flags & UATH_CFLAGS_FINAL)) {
2544190688Sweongyo		sc->sc_intrx_nextnum++;
2545190688Sweongyo		UATH_RESET_INTRX(sc);
2546190688Sweongyo		return (NULL);
2547190688Sweongyo	}
2548190688Sweongyo
2549190688Sweongyo	/*
2550190688Sweongyo	 * if the frame is not set UATH_CFLAGS_RXMSG, then rx descriptor is
2551190688Sweongyo	 * located at the end, 32-bit aligned
2552190688Sweongyo	 */
2553190688Sweongyo	desc = (chunk->flags & UATH_CFLAGS_RXMSG) ?
2554190688Sweongyo		(struct uath_rx_desc *)(chunk + 1) :
2555190688Sweongyo		(struct uath_rx_desc *)(((uint8_t *)chunk) +
2556190688Sweongyo		    sizeof(struct uath_chunk) + be16toh(chunk->length) -
2557190688Sweongyo		    sizeof(struct uath_rx_desc));
2558190688Sweongyo	*pdesc = desc;
2559190688Sweongyo
2560190688Sweongyo	DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2561190688Sweongyo	    "%s: frame len %u code %u status %u rate %u antenna %u "
2562190688Sweongyo	    "rssi %d channel %u phyerror %u connix %u decrypterror %u "
2563190688Sweongyo	    "keycachemiss %u\n", __func__, be32toh(desc->framelen)
2564190688Sweongyo	    , be32toh(desc->code), be32toh(desc->status), be32toh(desc->rate)
2565190688Sweongyo	    , be32toh(desc->antenna), be32toh(desc->rssi), be32toh(desc->channel)
2566190688Sweongyo	    , be32toh(desc->phyerror), be32toh(desc->connix)
2567190688Sweongyo	    , be32toh(desc->decrypterror), be32toh(desc->keycachemiss));
2568190688Sweongyo
2569190688Sweongyo	if (be32toh(desc->len) > MCLBYTES) {
2570190688Sweongyo		DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL,
2571190688Sweongyo		    "%s: bad descriptor (len=%d)\n", __func__,
2572190688Sweongyo		    be32toh(desc->len));
2573287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
2574190688Sweongyo		UATH_STAT_INC(sc, st_toobigrxpkt);
2575190688Sweongyo		if (sc->sc_intrx_head != NULL)
2576190688Sweongyo			m_freem(sc->sc_intrx_head);
2577190688Sweongyo		UATH_RESET_INTRX(sc);
2578190688Sweongyo		return (NULL);
2579190688Sweongyo	}
2580190688Sweongyo
2581190688Sweongyo	uath_update_rxstat(sc, be32toh(desc->status));
2582190688Sweongyo
2583190688Sweongyo	/* finalize mbuf */
2584190688Sweongyo	if (sc->sc_intrx_head == NULL) {
2585190688Sweongyo		m->m_pkthdr.len = m->m_len =
2586190688Sweongyo			be32toh(desc->framelen) - UATH_RX_DUMMYSIZE;
2587190688Sweongyo		m->m_data += sizeof(struct uath_chunk);
2588190688Sweongyo	} else {
2589190688Sweongyo		mp = sc->sc_intrx_head;
2590190688Sweongyo		mp->m_flags |= M_PKTHDR;
2591190688Sweongyo		mp->m_pkthdr.len = sc->sc_intrx_len;
2592190688Sweongyo		m = mp;
2593190688Sweongyo	}
2594190688Sweongyo
2595190688Sweongyo	/* there are a lot more fields in the RX descriptor */
2596194329Sweongyo	if ((sc->sc_flags & UATH_FLAG_INVALID) == 0 &&
2597194329Sweongyo	    ieee80211_radiotap_active(ic)) {
2598190688Sweongyo		struct uath_rx_radiotap_header *tap = &sc->sc_rxtap;
2599192468Ssam		uint32_t tsf_hi = be32toh(desc->tstamp_high);
2600192468Ssam		uint32_t tsf_lo = be32toh(desc->tstamp_low);
2601190688Sweongyo
2602192468Ssam		/* XXX only get low order 24bits of tsf from h/w */
2603192468Ssam		tap->wr_tsf = htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
2604192468Ssam		tap->wr_flags = 0;
2605192468Ssam		if (be32toh(desc->status) == UATH_STATUS_CRC_ERR)
2606192468Ssam			tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2607192468Ssam		/* XXX map other status to BADFCS? */
2608192468Ssam		/* XXX ath h/w rate code, need to map */
2609192468Ssam		tap->wr_rate = be32toh(desc->rate);
2610192468Ssam		tap->wr_antenna = be32toh(desc->antenna);
2611192468Ssam		tap->wr_antsignal = -95 + be32toh(desc->rssi);
2612192468Ssam		tap->wr_antnoise = -95;
2613190688Sweongyo	}
2614190688Sweongyo
2615190688Sweongyo	UATH_RESET_INTRX(sc);
2616190688Sweongyo
2617190688Sweongyo	return (m);
2618190688Sweongyo}
2619190688Sweongyo
2620190688Sweongyostatic void
2621194677Sthompsauath_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2622190688Sweongyo{
2623194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2624287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2625190688Sweongyo	struct ieee80211_frame *wh;
2626190688Sweongyo	struct ieee80211_node *ni;
2627190688Sweongyo	struct mbuf *m = NULL;
2628190688Sweongyo	struct uath_data *data;
2629190688Sweongyo	struct uath_rx_desc *desc = NULL;
2630190688Sweongyo	int8_t nf;
2631190688Sweongyo
2632190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2633190688Sweongyo
2634190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2635190688Sweongyo	case USB_ST_TRANSFERRED:
2636190688Sweongyo		data = STAILQ_FIRST(&sc->sc_rx_active);
2637190688Sweongyo		if (data == NULL)
2638190688Sweongyo			goto setup;
2639190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2640190688Sweongyo		UATH_STAT_DEC(sc, st_rx_active);
2641190688Sweongyo		m = uath_data_rxeof(xfer, data, &desc);
2642190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2643190688Sweongyo		UATH_STAT_INC(sc, st_rx_inactive);
2644190688Sweongyo		/* FALLTHROUGH */
2645190688Sweongyo	case USB_ST_SETUP:
2646190688Sweongyosetup:
2647190688Sweongyo		data = STAILQ_FIRST(&sc->sc_rx_inactive);
2648190688Sweongyo		if (data == NULL)
2649190688Sweongyo			return;
2650190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2651190688Sweongyo		UATH_STAT_DEC(sc, st_rx_inactive);
2652190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2653190688Sweongyo		UATH_STAT_INC(sc, st_rx_active);
2654244503Shselasky		usbd_xfer_set_frame_data(xfer, 0, data->buf, MCLBYTES);
2655194228Sthompsa		usbd_transfer_submit(xfer);
2656190688Sweongyo
2657190688Sweongyo		/*
2658190688Sweongyo		 * To avoid LOR we should unlock our private mutex here to call
2659190688Sweongyo		 * ieee80211_input() because here is at the end of a USB
2660190688Sweongyo		 * callback and safe to unlock.
2661190688Sweongyo		 */
2662194329Sweongyo		if (sc->sc_flags & UATH_FLAG_INVALID) {
2663194329Sweongyo			if (m != NULL)
2664194329Sweongyo				m_freem(m);
2665194329Sweongyo			return;
2666194329Sweongyo		}
2667190688Sweongyo		UATH_UNLOCK(sc);
2668190688Sweongyo		if (m != NULL && desc != NULL) {
2669190688Sweongyo			wh = mtod(m, struct ieee80211_frame *);
2670190688Sweongyo			ni = ieee80211_find_rxnode(ic,
2671190688Sweongyo			    (struct ieee80211_frame_min *)wh);
2672190688Sweongyo			nf = -95;	/* XXX */
2673190688Sweongyo			if (ni != NULL) {
2674190688Sweongyo				(void) ieee80211_input(ni, m,
2675192468Ssam				    (int)be32toh(desc->rssi), nf);
2676190688Sweongyo				/* node is no longer needed */
2677190688Sweongyo				ieee80211_free_node(ni);
2678190688Sweongyo			} else
2679190688Sweongyo				(void) ieee80211_input_all(ic, m,
2680192468Ssam				    (int)be32toh(desc->rssi), nf);
2681190688Sweongyo			m = NULL;
2682190688Sweongyo			desc = NULL;
2683190688Sweongyo		}
2684190688Sweongyo		UATH_LOCK(sc);
2685287197Sglebius		uath_start(sc);
2686190688Sweongyo		break;
2687190688Sweongyo	default:
2688190688Sweongyo		/* needs it to the inactive queue due to a error.  */
2689190688Sweongyo		data = STAILQ_FIRST(&sc->sc_rx_active);
2690190688Sweongyo		if (data != NULL) {
2691190688Sweongyo			STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2692190688Sweongyo			UATH_STAT_DEC(sc, st_rx_active);
2693190688Sweongyo			STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2694190688Sweongyo			UATH_STAT_INC(sc, st_rx_inactive);
2695190688Sweongyo		}
2696194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2697194677Sthompsa			usbd_xfer_set_stall(xfer);
2698287197Sglebius			counter_u64_add(ic->ic_ierrors, 1);
2699190688Sweongyo			goto setup;
2700190688Sweongyo		}
2701190688Sweongyo		break;
2702190688Sweongyo	}
2703190688Sweongyo}
2704190688Sweongyo
2705190688Sweongyostatic void
2706192984Sthompsauath_data_txeof(struct usb_xfer *xfer, struct uath_data *data)
2707190688Sweongyo{
2708194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2709190688Sweongyo
2710190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2711190688Sweongyo
2712190688Sweongyo	if (data->m) {
2713287197Sglebius		/* XXX status? */
2714287197Sglebius		ieee80211_tx_complete(data->ni, data->m, 0);
2715190688Sweongyo		data->m = NULL;
2716190688Sweongyo		data->ni = NULL;
2717190688Sweongyo	}
2718190688Sweongyo	sc->sc_tx_timer = 0;
2719190688Sweongyo}
2720190688Sweongyo
2721190688Sweongyostatic void
2722194677Sthompsauath_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2723190688Sweongyo{
2724194677Sthompsa	struct uath_softc *sc = usbd_xfer_softc(xfer);
2725190688Sweongyo	struct uath_data *data;
2726190688Sweongyo
2727190688Sweongyo	UATH_ASSERT_LOCKED(sc);
2728190688Sweongyo
2729190688Sweongyo	switch (USB_GET_STATE(xfer)) {
2730190688Sweongyo	case USB_ST_TRANSFERRED:
2731190688Sweongyo		data = STAILQ_FIRST(&sc->sc_tx_active);
2732190688Sweongyo		if (data == NULL)
2733190688Sweongyo			goto setup;
2734190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2735190688Sweongyo		UATH_STAT_DEC(sc, st_tx_active);
2736190688Sweongyo		uath_data_txeof(xfer, data);
2737190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2738190688Sweongyo		UATH_STAT_INC(sc, st_tx_inactive);
2739190688Sweongyo		/* FALLTHROUGH */
2740190688Sweongyo	case USB_ST_SETUP:
2741190688Sweongyosetup:
2742190688Sweongyo		data = STAILQ_FIRST(&sc->sc_tx_pending);
2743190688Sweongyo		if (data == NULL) {
2744190688Sweongyo			DPRINTF(sc, UATH_DEBUG_XMIT, "%s: empty pending queue\n",
2745190688Sweongyo			    __func__);
2746190688Sweongyo			return;
2747190688Sweongyo		}
2748190688Sweongyo		STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2749190688Sweongyo		UATH_STAT_DEC(sc, st_tx_pending);
2750190688Sweongyo		STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2751190688Sweongyo		UATH_STAT_INC(sc, st_tx_active);
2752190688Sweongyo
2753194677Sthompsa		usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2754194228Sthompsa		usbd_transfer_submit(xfer);
2755190688Sweongyo
2756287197Sglebius		uath_start(sc);
2757190688Sweongyo		break;
2758190688Sweongyo	default:
2759190688Sweongyo		data = STAILQ_FIRST(&sc->sc_tx_active);
2760190688Sweongyo		if (data == NULL)
2761190688Sweongyo			goto setup;
2762190688Sweongyo		if (data->ni != NULL) {
2763287197Sglebius			if_inc_counter(data->ni->ni_vap->iv_ifp,
2764287197Sglebius			    IFCOUNTER_OERRORS, 1);
2765194329Sweongyo			if ((sc->sc_flags & UATH_FLAG_INVALID) == 0)
2766194329Sweongyo				ieee80211_free_node(data->ni);
2767190688Sweongyo			data->ni = NULL;
2768190688Sweongyo		}
2769194677Sthompsa		if (error != USB_ERR_CANCELLED) {
2770194677Sthompsa			usbd_xfer_set_stall(xfer);
2771190688Sweongyo			goto setup;
2772190688Sweongyo		}
2773190688Sweongyo		break;
2774190688Sweongyo	}
2775190688Sweongyo}
2776190688Sweongyo
2777190688Sweongyostatic device_method_t uath_methods[] = {
2778190688Sweongyo	DEVMETHOD(device_probe, uath_match),
2779190688Sweongyo	DEVMETHOD(device_attach, uath_attach),
2780190688Sweongyo	DEVMETHOD(device_detach, uath_detach),
2781244503Shselasky	DEVMETHOD_END
2782190688Sweongyo};
2783190688Sweongyostatic driver_t uath_driver = {
2784233774Shselasky	.name = "uath",
2785233774Shselasky	.methods = uath_methods,
2786233774Shselasky	.size = sizeof(struct uath_softc)
2787190688Sweongyo};
2788190688Sweongyostatic devclass_t uath_devclass;
2789190688Sweongyo
2790190688SweongyoDRIVER_MODULE(uath, uhub, uath_driver, uath_devclass, NULL, 0);
2791190688SweongyoMODULE_DEPEND(uath, wlan, 1, 1, 1);
2792190688SweongyoMODULE_DEPEND(uath, usb, 1, 1, 1);
2793212122SthompsaMODULE_VERSION(uath, 1);
2794292080SimpUSB_PNP_HOST_INFO(uath_devs);
2795