if_upgt.c revision 196497
1100978Srwatson/*	$OpenBSD: if_upgt.c,v 1.35 2008/04/16 18:32:15 damien Exp $ */
2100978Srwatson/*	$FreeBSD: head/sys/dev/usb/wlan/if_upgt.c 196497 2009-08-24 05:03:59Z alfred $ */
3100978Srwatson
4100978Srwatson/*
5100978Srwatson * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
6100978Srwatson *
7100978Srwatson * Permission to use, copy, modify, and distribute this software for any
8106392Srwatson * purpose with or without fee is hereby granted, provided that the above
9106392Srwatson * copyright notice and this permission notice appear in all copies.
10106392Srwatson *
11106392Srwatson * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12100978Srwatson * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13100978Srwatson * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14100978Srwatson * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15100978Srwatson * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16100978Srwatson * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17100978Srwatson * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18100978Srwatson */
19100978Srwatson
20100978Srwatson#include <sys/param.h>
21100978Srwatson#include <sys/systm.h>
22100978Srwatson#include <sys/kernel.h>
23100978Srwatson#include <sys/endian.h>
24100978Srwatson#include <sys/firmware.h>
25100978Srwatson#include <sys/linker.h>
26100978Srwatson#include <sys/mbuf.h>
27100978Srwatson#include <sys/malloc.h>
28100978Srwatson#include <sys/module.h>
29100978Srwatson#include <sys/socket.h>
30100978Srwatson#include <sys/sockio.h>
31100978Srwatson#include <sys/sysctl.h>
32100978Srwatson
33100978Srwatson#include <net/if.h>
34100978Srwatson#include <net/if_arp.h>
35100978Srwatson#include <net/ethernet.h>
36100978Srwatson#include <net/if_dl.h>
37100978Srwatson#include <net/if_media.h>
38100978Srwatson#include <net/if_types.h>
39100978Srwatson
40100978Srwatson#include <sys/bus.h>
41100978Srwatson#include <machine/bus.h>
42100978Srwatson
43100978Srwatson#include <net80211/ieee80211_var.h>
44100978Srwatson#include <net80211/ieee80211_phy.h>
45101826Srwatson#include <net80211/ieee80211_radiotap.h>
46101826Srwatson#include <net80211/ieee80211_regdomain.h>
47100978Srwatson
48100978Srwatson#include <net/bpf.h>
49100978Srwatson
50100978Srwatson#include <dev/usb/usb.h>
51100978Srwatson#include <dev/usb/usbdi.h>
52100978Srwatson#include "usbdevs.h"
53100978Srwatson
54100978Srwatson#include <dev/usb/wlan/if_upgtvar.h>
55100978Srwatson
56100978Srwatson/*
57100978Srwatson * Driver for the USB PrismGT devices.
58100978Srwatson *
59100978Srwatson * For now just USB 2.0 devices with the GW3887 chipset are supported.
60102123Srwatson * The driver has been written based on the firmware version 2.13.1.0_LM87.
61102123Srwatson *
62105693Srwatson * TODO's:
63105693Srwatson * - MONITOR mode test.
64105693Srwatson * - Add HOSTAP mode.
65105693Srwatson * - Add IBSS mode.
66102123Srwatson * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
67105693Srwatson *
68105693Srwatson * Parts of this driver has been influenced by reading the p54u driver
69105693Srwatson * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
70105693Srwatson * Sebastien Bourdeauducq <lekernel@prism54.org>.
71105693Srwatson */
72105693Srwatson
73105693SrwatsonSYSCTL_NODE(_hw, OID_AUTO, upgt, CTLFLAG_RD, 0,
74105693Srwatson    "USB PrismGT GW3887 driver parameters");
75105693Srwatson
76102123Srwatson#ifdef UPGT_DEBUG
77105693Srwatsonint upgt_debug = 0;
78105693SrwatsonSYSCTL_INT(_hw_upgt, OID_AUTO, debug, CTLFLAG_RW, &upgt_debug,
79105693Srwatson	    0, "control debugging printfs");
80100978SrwatsonTUNABLE_INT("hw.upgt.debug", &upgt_debug);
81105693Srwatsonenum {
82100978Srwatson	UPGT_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
83100978Srwatson	UPGT_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
84105693Srwatson	UPGT_DEBUG_RESET	= 0x00000004,	/* reset processing */
85105693Srwatson	UPGT_DEBUG_INTR		= 0x00000008,	/* INTR */
86100978Srwatson	UPGT_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
87106468Srwatson	UPGT_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
88106468Srwatson	UPGT_DEBUG_STATE	= 0x00000040,	/* 802.11 state transitions */
89105693Srwatson	UPGT_DEBUG_STAT		= 0x00000080,	/* statistic */
90105693Srwatson	UPGT_DEBUG_FW		= 0x00000100,	/* firmware */
91105693Srwatson	UPGT_DEBUG_ANY		= 0xffffffff
92105693Srwatson};
93105693Srwatson#define	DPRINTF(sc, m, fmt, ...) do {				\
94105693Srwatson	if (sc->sc_debug & (m))					\
95105693Srwatson		printf(fmt, __VA_ARGS__);			\
96105693Srwatson} while (0)
97105693Srwatson#else
98105693Srwatson#define	DPRINTF(sc, m, fmt, ...) do {				\
99105693Srwatson	(void) sc;						\
100105693Srwatson} while (0)
101105693Srwatson#endif
102105693Srwatson
103105693Srwatson/*
104105693Srwatson * Prototypes.
105105693Srwatson */
106105693Srwatsonstatic device_probe_t upgt_match;
107105693Srwatsonstatic device_attach_t upgt_attach;
108106093Srwatsonstatic device_detach_t upgt_detach;
109105693Srwatsonstatic int	upgt_alloc_tx(struct upgt_softc *);
110105693Srwatsonstatic int	upgt_alloc_rx(struct upgt_softc *);
111100978Srwatsonstatic int	upgt_device_reset(struct upgt_softc *);
112100978Srwatsonstatic void	upgt_bulk_tx(struct upgt_softc *, struct upgt_data *);
113100978Srwatsonstatic int	upgt_fw_verify(struct upgt_softc *);
114100978Srwatsonstatic int	upgt_mem_init(struct upgt_softc *);
115100978Srwatsonstatic int	upgt_fw_load(struct upgt_softc *);
116100978Srwatsonstatic int	upgt_fw_copy(const uint8_t *, char *, int);
117100978Srwatsonstatic uint32_t	upgt_crc32_le(const void *, size_t);
118106468Srwatsonstatic struct mbuf *
119100978Srwatson		upgt_rxeof(struct usb_xfer *, struct upgt_data *, int *);
120100978Srwatsonstatic struct mbuf *
121100978Srwatson		upgt_rx(struct upgt_softc *, uint8_t *, int, int *);
122100978Srwatsonstatic void	upgt_txeof(struct usb_xfer *, struct upgt_data *);
123100978Srwatsonstatic int	upgt_eeprom_read(struct upgt_softc *);
124100978Srwatsonstatic int	upgt_eeprom_parse(struct upgt_softc *);
125100978Srwatsonstatic void	upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
126104338Srwatsonstatic void	upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
127100978Srwatsonstatic void	upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
128100978Srwatsonstatic void	upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
129100978Srwatsonstatic uint32_t	upgt_chksum_le(const uint32_t *, size_t);
130100978Srwatsonstatic void	upgt_tx_done(struct upgt_softc *, uint8_t *);
131100978Srwatsonstatic void	upgt_init(void *);
132100978Srwatsonstatic void	upgt_init_locked(struct upgt_softc *);
133100978Srwatsonstatic int	upgt_ioctl(struct ifnet *, u_long, caddr_t);
134100978Srwatsonstatic void	upgt_start(struct ifnet *);
135100978Srwatsonstatic int	upgt_raw_xmit(struct ieee80211_node *, struct mbuf *,
136100978Srwatson		    const struct ieee80211_bpf_params *);
137100978Srwatsonstatic void	upgt_scan_start(struct ieee80211com *);
138100978Srwatsonstatic void	upgt_scan_end(struct ieee80211com *);
139100978Srwatsonstatic void	upgt_set_channel(struct ieee80211com *);
140100978Srwatsonstatic struct ieee80211vap *upgt_vap_create(struct ieee80211com *,
141100978Srwatson		    const char name[IFNAMSIZ], int unit, int opmode,
142100978Srwatson		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
143100978Srwatson		    const uint8_t mac[IEEE80211_ADDR_LEN]);
144100978Srwatsonstatic void	upgt_vap_delete(struct ieee80211vap *);
145104541Srwatsonstatic void	upgt_update_mcast(struct ifnet *);
146100978Srwatsonstatic uint8_t	upgt_rx_rate(struct upgt_softc *, const int);
147104541Srwatsonstatic void	upgt_set_multi(void *);
148100978Srwatsonstatic void	upgt_stop(struct upgt_softc *);
149107105Srwatsonstatic void	upgt_setup_rates(struct ieee80211vap *, struct ieee80211com *);
150100978Srwatsonstatic int	upgt_set_macfilter(struct upgt_softc *, uint8_t);
151105988Srwatsonstatic int	upgt_newstate(struct ieee80211vap *, enum ieee80211_state, int);
152105988Srwatsonstatic void	upgt_set_chan(struct upgt_softc *, struct ieee80211_channel *);
153100978Srwatsonstatic void	upgt_set_led(struct upgt_softc *, int);
154100978Srwatsonstatic void	upgt_set_led_blink(void *);
155100978Srwatsonstatic void	upgt_get_stats(struct upgt_softc *);
156100978Srwatsonstatic void	upgt_mem_free(struct upgt_softc *, uint32_t);
157100978Srwatsonstatic uint32_t	upgt_mem_alloc(struct upgt_softc *);
158100978Srwatsonstatic void	upgt_free_tx(struct upgt_softc *);
159100978Srwatsonstatic void	upgt_free_rx(struct upgt_softc *);
160107105Srwatsonstatic void	upgt_watchdog(void *);
161100978Srwatsonstatic void	upgt_abort_xfers(struct upgt_softc *);
162100978Srwatsonstatic void	upgt_abort_xfers_locked(struct upgt_softc *);
163100978Srwatsonstatic void	upgt_sysctl_node(struct upgt_softc *);
164105988Srwatsonstatic struct upgt_data *
165100978Srwatson		upgt_getbuf(struct upgt_softc *);
166100978Srwatsonstatic struct upgt_data *
167100978Srwatson		upgt_gettxbuf(struct upgt_softc *);
168100978Srwatsonstatic int	upgt_tx_start(struct upgt_softc *, struct mbuf *,
169100978Srwatson		    struct ieee80211_node *, struct upgt_data *);
170105988Srwatson
171105988Srwatsonstatic const char *upgt_fwname = "upgt-gw3887";
172105988Srwatson
173105988Srwatsonstatic const struct usb_device_id upgt_devs_2[] = {
174100978Srwatson#define	UPGT_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
175100978Srwatson	/* version 2 devices */
176100978Srwatson	UPGT_DEV(ACCTON,	PRISM_GT),
177104533Srwatson	UPGT_DEV(BELKIN,	F5D7050),
178104533Srwatson	UPGT_DEV(CISCOLINKSYS,	WUSB54AG),
179105988Srwatson	UPGT_DEV(CONCEPTRONIC,	PRISM_GT),
180105988Srwatson	UPGT_DEV(DELL,		PRISM_GT_1),
181100978Srwatson	UPGT_DEV(DELL,		PRISM_GT_2),
182100978Srwatson	UPGT_DEV(FSC,		E5400),
183100978Srwatson	UPGT_DEV(GLOBESPAN,	PRISM_GT_1),
184100978Srwatson	UPGT_DEV(GLOBESPAN,	PRISM_GT_2),
185100978Srwatson	UPGT_DEV(INTERSIL,	PRISM_GT),
186100978Srwatson	UPGT_DEV(SMC,		2862WG),
187100978Srwatson	UPGT_DEV(WISTRONNEWEB,	UR045G),
188100978Srwatson	UPGT_DEV(XYRATEX,	PRISM_GT_1),
189100978Srwatson	UPGT_DEV(XYRATEX,	PRISM_GT_2),
190100978Srwatson	UPGT_DEV(ZCOM,		XG703A),
191100978Srwatson	UPGT_DEV(ZCOM,		XM142)
192100978Srwatson};
193100978Srwatson
194100978Srwatsonstatic usb_callback_t upgt_bulk_rx_callback;
195100978Srwatsonstatic usb_callback_t upgt_bulk_tx_callback;
196100978Srwatson
197100978Srwatsonstatic const struct usb_config upgt_config[UPGT_N_XFERS] = {
198100978Srwatson	[UPGT_BULK_TX] = {
199100978Srwatson		.type = UE_BULK,
200100978Srwatson		.endpoint = UE_ADDR_ANY,
201100978Srwatson		.direction = UE_DIR_OUT,
202100978Srwatson		.bufsize = MCLBYTES,
203100978Srwatson		.flags = {
204100978Srwatson			.ext_buffer = 1,
205100978Srwatson			.force_short_xfer = 1,
206100978Srwatson			.pipe_bof = 1
207100978Srwatson		},
208100978Srwatson		.callback = upgt_bulk_tx_callback,
209100978Srwatson		.timeout = UPGT_USB_TIMEOUT,	/* ms */
210100978Srwatson	},
211100978Srwatson	[UPGT_BULK_RX] = {
212100978Srwatson		.type = UE_BULK,
213100978Srwatson		.endpoint = UE_ADDR_ANY,
214100978Srwatson		.direction = UE_DIR_IN,
215100978Srwatson		.bufsize = MCLBYTES,
216100978Srwatson		.flags = {
217100978Srwatson			.ext_buffer = 1,
218100978Srwatson			.pipe_bof = 1,
219100978Srwatson			.short_xfer_ok = 1
220100978Srwatson		},
221100978Srwatson		.callback = upgt_bulk_rx_callback,
222106468Srwatson	},
223106468Srwatson};
224106468Srwatson
225100978Srwatsonstatic int
226106468Srwatsonupgt_match(device_t dev)
227106468Srwatson{
228106468Srwatson	struct usb_attach_arg *uaa = device_get_ivars(dev);
229106468Srwatson
230100978Srwatson	if (uaa->usb_mode != USB_MODE_HOST)
231100978Srwatson		return (ENXIO);
232104338Srwatson	if (uaa->info.bConfigIndex != UPGT_CONFIG_INDEX)
233100978Srwatson		return (ENXIO);
234100978Srwatson	if (uaa->info.bIfaceIndex != UPGT_IFACE_INDEX)
235100978Srwatson		return (ENXIO);
236100978Srwatson
237100978Srwatson	return (usbd_lookup_id_by_uaa(upgt_devs_2, sizeof(upgt_devs_2), uaa));
238106308Srwatson}
239106308Srwatson
240106308Srwatsonstatic int
241106308Srwatsonupgt_attach(device_t dev)
242107089Srwatson{
243107089Srwatson	int error;
244107089Srwatson	struct ieee80211com *ic;
245100978Srwatson	struct ifnet *ifp;
246100978Srwatson	struct upgt_softc *sc = device_get_softc(dev);
247100978Srwatson	struct usb_attach_arg *uaa = device_get_ivars(dev);
248102115Srwatson	uint8_t bands, iface_index = UPGT_IFACE_INDEX;
249102115Srwatson
250102115Srwatson	sc->sc_dev = dev;
251102115Srwatson	sc->sc_udev = uaa->device;
252100978Srwatson#ifdef UPGT_DEBUG
253100978Srwatson	sc->sc_debug = upgt_debug;
254100978Srwatson#endif
255100978Srwatson	device_set_usb_desc(dev);
256100978Srwatson
257100978Srwatson	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
258100978Srwatson	    MTX_DEF);
259100978Srwatson	callout_init(&sc->sc_led_ch, 0);
260101934Srwatson	callout_init(&sc->sc_watchdog_ch, 0);
261100978Srwatson
262104571Srwatson	/* Allocate TX and RX xfers.  */
263104571Srwatson	error = upgt_alloc_tx(sc);
264100978Srwatson	if (error)
265106412Srwatson		goto fail1;
266106412Srwatson	error = upgt_alloc_rx(sc);
267106024Srwatson	if (error)
268106369Srwatson		goto fail2;
269106023Srwatson
270106025Srwatson	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
271106025Srwatson	    upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx);
272106025Srwatson	if (error) {
273100978Srwatson		device_printf(dev, "could not allocate USB transfers, "
274106212Srwatson		    "err=%s\n", usbd_errstr(error));
275100978Srwatson		goto fail3;
276100978Srwatson	}
277100978Srwatson
278100978Srwatson	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
279100978Srwatson	if (ifp == NULL) {
280100978Srwatson		device_printf(dev, "can not if_alloc()\n");
281100978Srwatson		goto fail4;
282100978Srwatson	}
283106468Srwatson
284106468Srwatson	/* Initialize the device.  */
285100978Srwatson	error = upgt_device_reset(sc);
286100978Srwatson	if (error)
287100978Srwatson		goto fail5;
288100978Srwatson	/* Verify the firmware.  */
289104529Srwatson	error = upgt_fw_verify(sc);
290104529Srwatson	if (error)
291100978Srwatson		goto fail5;
292100978Srwatson	/* Calculate device memory space.  */
293104546Srwatson	if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
294104546Srwatson		device_printf(dev,
295104546Srwatson		    "could not find memory space addresses on FW!\n");
296104546Srwatson		error = EIO;
297100978Srwatson		goto fail5;
298106212Srwatson	}
299102129Srwatson	sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
300102129Srwatson	sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
301102129Srwatson
302102129Srwatson	DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n",
303100978Srwatson	    sc->sc_memaddr_frame_start);
304100978Srwatson	DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n",
305100978Srwatson	    sc->sc_memaddr_frame_end);
306100978Srwatson	DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n",
307100978Srwatson	    sc->sc_memaddr_rx_start);
308100978Srwatson
309100978Srwatson	upgt_mem_init(sc);
310100978Srwatson
311100978Srwatson	/* Load the firmware.  */
312100978Srwatson	error = upgt_fw_load(sc);
313100978Srwatson	if (error)
314100978Srwatson		goto fail5;
315100978Srwatson
316100978Srwatson	/* Read the whole EEPROM content and parse it.  */
317100978Srwatson	error = upgt_eeprom_read(sc);
318100978Srwatson	if (error)
319100978Srwatson		goto fail5;
320100978Srwatson	error = upgt_eeprom_parse(sc);
321100978Srwatson	if (error)
322102129Srwatson		goto fail5;
323102129Srwatson
324102129Srwatson	/* all works related with the device have done here. */
325102129Srwatson	upgt_abort_xfers(sc);
326100978Srwatson
327100978Srwatson	/* Setup the 802.11 device.  */
328100978Srwatson	ifp->if_softc = sc;
329100978Srwatson	if_initname(ifp, "upgt", device_get_unit(sc->sc_dev));
330100978Srwatson	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
331100978Srwatson	ifp->if_init = upgt_init;
332100978Srwatson	ifp->if_ioctl = upgt_ioctl;
333100978Srwatson	ifp->if_start = upgt_start;
334100978Srwatson	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
335100978Srwatson	IFQ_SET_READY(&ifp->if_snd);
336100978Srwatson
337100978Srwatson	ic = ifp->if_l2com;
338107271Srwatson	ic->ic_ifp = ifp;
339100978Srwatson	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
340100978Srwatson	ic->ic_opmode = IEEE80211_M_STA;
341100978Srwatson	/* set device capabilities */
342100978Srwatson	ic->ic_caps =
343100978Srwatson		  IEEE80211_C_STA		/* station mode */
344100978Srwatson		| IEEE80211_C_MONITOR		/* monitor mode */
345100978Srwatson		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
346106093Srwatson	        | IEEE80211_C_SHSLOT		/* short slot time supported */
347100978Srwatson		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
348100978Srwatson	        | IEEE80211_C_WPA		/* 802.11i */
349		;
350
351	bands = 0;
352	setbit(&bands, IEEE80211_MODE_11B);
353	setbit(&bands, IEEE80211_MODE_11G);
354	ieee80211_init_channels(ic, NULL, &bands);
355
356	ieee80211_ifattach(ic, sc->sc_myaddr);
357	ic->ic_raw_xmit = upgt_raw_xmit;
358	ic->ic_scan_start = upgt_scan_start;
359	ic->ic_scan_end = upgt_scan_end;
360	ic->ic_set_channel = upgt_set_channel;
361
362	ic->ic_vap_create = upgt_vap_create;
363	ic->ic_vap_delete = upgt_vap_delete;
364	ic->ic_update_mcast = upgt_update_mcast;
365
366	ieee80211_radiotap_attach(ic,
367	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
368		UPGT_TX_RADIOTAP_PRESENT,
369	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
370		UPGT_RX_RADIOTAP_PRESENT);
371
372	upgt_sysctl_node(sc);
373
374	if (bootverbose)
375		ieee80211_announce(ic);
376
377	return (0);
378
379fail5:	if_free(ifp);
380fail4:	usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
381fail3:	upgt_free_rx(sc);
382fail2:	upgt_free_tx(sc);
383fail1:	mtx_destroy(&sc->sc_mtx);
384
385	return (error);
386}
387
388static void
389upgt_txeof(struct usb_xfer *xfer, struct upgt_data *data)
390{
391	struct upgt_softc *sc = usbd_xfer_softc(xfer);
392	struct ifnet *ifp = sc->sc_ifp;
393	struct mbuf *m;
394
395	UPGT_ASSERT_LOCKED(sc);
396
397	/*
398	 * Do any tx complete callback.  Note this must be done before releasing
399	 * the node reference.
400	 */
401	if (data->m) {
402		m = data->m;
403		if (m->m_flags & M_TXCB) {
404			/* XXX status? */
405			ieee80211_process_callback(data->ni, m, 0);
406		}
407		m_freem(m);
408		data->m = NULL;
409	}
410	if (data->ni) {
411		ieee80211_free_node(data->ni);
412		data->ni = NULL;
413	}
414	ifp->if_opackets++;
415}
416
417static void
418upgt_get_stats(struct upgt_softc *sc)
419{
420	struct upgt_data *data_cmd;
421	struct upgt_lmac_mem *mem;
422	struct upgt_lmac_stats *stats;
423
424	data_cmd = upgt_getbuf(sc);
425	if (data_cmd == NULL) {
426		device_printf(sc->sc_dev, "%s: out of buffer.\n", __func__);
427		return;
428	}
429
430	/*
431	 * Transmit the URB containing the CMD data.
432	 */
433	bzero(data_cmd->buf, MCLBYTES);
434
435	mem = (struct upgt_lmac_mem *)data_cmd->buf;
436	mem->addr = htole32(sc->sc_memaddr_frame_start +
437	    UPGT_MEMSIZE_FRAME_HEAD);
438
439	stats = (struct upgt_lmac_stats *)(mem + 1);
440
441	stats->header1.flags = 0;
442	stats->header1.type = UPGT_H1_TYPE_CTRL;
443	stats->header1.len = htole16(
444	    sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header));
445
446	stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
447	stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
448	stats->header2.flags = 0;
449
450	data_cmd->buflen = sizeof(*mem) + sizeof(*stats);
451
452	mem->chksum = upgt_chksum_le((uint32_t *)stats,
453	    data_cmd->buflen - sizeof(*mem));
454
455	upgt_bulk_tx(sc, data_cmd);
456}
457
458static int
459upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
460{
461	struct upgt_softc *sc = ifp->if_softc;
462	struct ieee80211com *ic = ifp->if_l2com;
463	struct ifreq *ifr = (struct ifreq *) data;
464	int error = 0, startall = 0;
465
466	switch (cmd) {
467	case SIOCSIFFLAGS:
468		if (ifp->if_flags & IFF_UP) {
469			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
470				if ((ifp->if_flags ^ sc->sc_if_flags) &
471				    (IFF_ALLMULTI | IFF_PROMISC))
472					upgt_set_multi(sc);
473			} else {
474				upgt_init(sc);
475				startall = 1;
476			}
477		} else {
478			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
479				upgt_stop(sc);
480		}
481		sc->sc_if_flags = ifp->if_flags;
482		if (startall)
483			ieee80211_start_all(ic);
484		break;
485	case SIOCGIFMEDIA:
486		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
487		break;
488	case SIOCGIFADDR:
489		error = ether_ioctl(ifp, cmd, data);
490		break;
491	default:
492		error = EINVAL;
493		break;
494	}
495	return error;
496}
497
498static void
499upgt_stop_locked(struct upgt_softc *sc)
500{
501	struct ifnet *ifp = sc->sc_ifp;
502
503	UPGT_ASSERT_LOCKED(sc);
504
505	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
506		upgt_set_macfilter(sc, IEEE80211_S_INIT);
507	upgt_abort_xfers_locked(sc);
508}
509
510static void
511upgt_stop(struct upgt_softc *sc)
512{
513	struct ifnet *ifp = sc->sc_ifp;
514
515	UPGT_LOCK(sc);
516	upgt_stop_locked(sc);
517	UPGT_UNLOCK(sc);
518
519	/* device down */
520	sc->sc_tx_timer = 0;
521	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
522	sc->sc_flags &= ~UPGT_FLAG_INITDONE;
523}
524
525static void
526upgt_set_led(struct upgt_softc *sc, int action)
527{
528	struct upgt_data *data_cmd;
529	struct upgt_lmac_mem *mem;
530	struct upgt_lmac_led *led;
531
532	data_cmd = upgt_getbuf(sc);
533	if (data_cmd == NULL) {
534		device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
535		return;
536	}
537
538	/*
539	 * Transmit the URB containing the CMD data.
540	 */
541	bzero(data_cmd->buf, MCLBYTES);
542
543	mem = (struct upgt_lmac_mem *)data_cmd->buf;
544	mem->addr = htole32(sc->sc_memaddr_frame_start +
545	    UPGT_MEMSIZE_FRAME_HEAD);
546
547	led = (struct upgt_lmac_led *)(mem + 1);
548
549	led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
550	led->header1.type = UPGT_H1_TYPE_CTRL;
551	led->header1.len = htole16(
552	    sizeof(struct upgt_lmac_led) -
553	    sizeof(struct upgt_lmac_header));
554
555	led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
556	led->header2.type = htole16(UPGT_H2_TYPE_LED);
557	led->header2.flags = 0;
558
559	switch (action) {
560	case UPGT_LED_OFF:
561		led->mode = htole16(UPGT_LED_MODE_SET);
562		led->action_fix = 0;
563		led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
564		led->action_tmp_dur = 0;
565		break;
566	case UPGT_LED_ON:
567		led->mode = htole16(UPGT_LED_MODE_SET);
568		led->action_fix = 0;
569		led->action_tmp = htole16(UPGT_LED_ACTION_ON);
570		led->action_tmp_dur = 0;
571		break;
572	case UPGT_LED_BLINK:
573		if (sc->sc_state != IEEE80211_S_RUN) {
574			STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
575			return;
576		}
577		if (sc->sc_led_blink) {
578			/* previous blink was not finished */
579			STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
580			return;
581		}
582		led->mode = htole16(UPGT_LED_MODE_SET);
583		led->action_fix = htole16(UPGT_LED_ACTION_OFF);
584		led->action_tmp = htole16(UPGT_LED_ACTION_ON);
585		led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
586		/* lock blink */
587		sc->sc_led_blink = 1;
588		callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc);
589		break;
590	default:
591		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
592		return;
593	}
594
595	data_cmd->buflen = sizeof(*mem) + sizeof(*led);
596
597	mem->chksum = upgt_chksum_le((uint32_t *)led,
598	    data_cmd->buflen - sizeof(*mem));
599
600	upgt_bulk_tx(sc, data_cmd);
601}
602
603static void
604upgt_set_led_blink(void *arg)
605{
606	struct upgt_softc *sc = arg;
607
608	/* blink finished, we are ready for a next one */
609	sc->sc_led_blink = 0;
610}
611
612static void
613upgt_init(void *priv)
614{
615	struct upgt_softc *sc = priv;
616	struct ifnet *ifp = sc->sc_ifp;
617	struct ieee80211com *ic = ifp->if_l2com;
618
619	UPGT_LOCK(sc);
620	upgt_init_locked(sc);
621	UPGT_UNLOCK(sc);
622
623	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
624		ieee80211_start_all(ic);		/* start all vap's */
625}
626
627static void
628upgt_init_locked(struct upgt_softc *sc)
629{
630	struct ifnet *ifp = sc->sc_ifp;
631
632	UPGT_ASSERT_LOCKED(sc);
633
634	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
635		upgt_stop_locked(sc);
636
637	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
638
639	(void)upgt_set_macfilter(sc, IEEE80211_S_SCAN);
640
641	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
642	ifp->if_drv_flags |= IFF_DRV_RUNNING;
643	sc->sc_flags |= UPGT_FLAG_INITDONE;
644
645	callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
646}
647
648static int
649upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
650{
651	struct ifnet *ifp = sc->sc_ifp;
652	struct ieee80211com *ic = ifp->if_l2com;
653	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
654	struct ieee80211_node *ni = vap->iv_bss;
655	struct upgt_data *data_cmd;
656	struct upgt_lmac_mem *mem;
657	struct upgt_lmac_filter *filter;
658	uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
659
660	UPGT_ASSERT_LOCKED(sc);
661
662	data_cmd = upgt_getbuf(sc);
663	if (data_cmd == NULL) {
664		device_printf(sc->sc_dev, "out of TX buffers.\n");
665		return (ENOBUFS);
666	}
667
668	/*
669	 * Transmit the URB containing the CMD data.
670	 */
671	bzero(data_cmd->buf, MCLBYTES);
672
673	mem = (struct upgt_lmac_mem *)data_cmd->buf;
674	mem->addr = htole32(sc->sc_memaddr_frame_start +
675	    UPGT_MEMSIZE_FRAME_HEAD);
676
677	filter = (struct upgt_lmac_filter *)(mem + 1);
678
679	filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
680	filter->header1.type = UPGT_H1_TYPE_CTRL;
681	filter->header1.len = htole16(
682	    sizeof(struct upgt_lmac_filter) -
683	    sizeof(struct upgt_lmac_header));
684
685	filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
686	filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
687	filter->header2.flags = 0;
688
689	switch (state) {
690	case IEEE80211_S_INIT:
691		DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n",
692		    __func__);
693		filter->type = htole16(UPGT_FILTER_TYPE_RESET);
694		break;
695	case IEEE80211_S_SCAN:
696		DPRINTF(sc, UPGT_DEBUG_STATE,
697		    "set MAC filter to SCAN (bssid %s)\n",
698		    ether_sprintf(broadcast));
699		filter->type = htole16(UPGT_FILTER_TYPE_NONE);
700		IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
701		IEEE80211_ADDR_COPY(filter->src, broadcast);
702		filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
703		filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
704		filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
705		filter->rxhw = htole32(sc->sc_eeprom_hwrx);
706		filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
707		break;
708	case IEEE80211_S_RUN:
709		/* XXX monitor mode isn't tested yet.  */
710		if (vap->iv_opmode == IEEE80211_M_MONITOR) {
711			filter->type = htole16(UPGT_FILTER_TYPE_MONITOR);
712			IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
713			IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
714			filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1);
715			filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
716			filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2);
717			filter->rxhw = htole32(sc->sc_eeprom_hwrx);
718			filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3);
719		} else {
720			DPRINTF(sc, UPGT_DEBUG_STATE,
721			    "set MAC filter to RUN (bssid %s)\n",
722			    ether_sprintf(ni->ni_bssid));
723			filter->type = htole16(UPGT_FILTER_TYPE_STA);
724			IEEE80211_ADDR_COPY(filter->dst, sc->sc_myaddr);
725			IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
726			filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
727			filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
728			filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
729			filter->rxhw = htole32(sc->sc_eeprom_hwrx);
730			filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
731		}
732		break;
733	default:
734		device_printf(sc->sc_dev,
735		    "MAC filter does not know that state!\n");
736		break;
737	}
738
739	data_cmd->buflen = sizeof(*mem) + sizeof(*filter);
740
741	mem->chksum = upgt_chksum_le((uint32_t *)filter,
742	    data_cmd->buflen - sizeof(*mem));
743
744	upgt_bulk_tx(sc, data_cmd);
745
746	return (0);
747}
748
749static void
750upgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic)
751{
752	struct ifnet *ifp = ic->ic_ifp;
753	struct upgt_softc *sc = ifp->if_softc;
754	const struct ieee80211_txparam *tp;
755
756	/*
757	 * 0x01 = OFMD6   0x10 = DS1
758	 * 0x04 = OFDM9   0x11 = DS2
759	 * 0x06 = OFDM12  0x12 = DS5
760	 * 0x07 = OFDM18  0x13 = DS11
761	 * 0x08 = OFDM24
762	 * 0x09 = OFDM36
763	 * 0x0a = OFDM48
764	 * 0x0b = OFDM54
765	 */
766	const uint8_t rateset_auto_11b[] =
767	    { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 };
768	const uint8_t rateset_auto_11g[] =
769	    { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 };
770	const uint8_t rateset_fix_11bg[] =
771	    { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07,
772	      0x08, 0x09, 0x0a, 0x0b };
773
774	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
775
776	/* XXX */
777	if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
778		/*
779		 * Automatic rate control is done by the device.
780		 * We just pass the rateset from which the device
781		 * will pickup a rate.
782		 */
783		if (ic->ic_curmode == IEEE80211_MODE_11B)
784			bcopy(rateset_auto_11b, sc->sc_cur_rateset,
785			    sizeof(sc->sc_cur_rateset));
786		if (ic->ic_curmode == IEEE80211_MODE_11G ||
787		    ic->ic_curmode == IEEE80211_MODE_AUTO)
788			bcopy(rateset_auto_11g, sc->sc_cur_rateset,
789			    sizeof(sc->sc_cur_rateset));
790	} else {
791		/* set a fixed rate */
792		memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate],
793		    sizeof(sc->sc_cur_rateset));
794	}
795}
796
797static void
798upgt_set_multi(void *arg)
799{
800	struct upgt_softc *sc = arg;
801	struct ifnet *ifp = sc->sc_ifp;
802
803	if (!(ifp->if_flags & IFF_UP))
804		return;
805
806	/*
807	 * XXX don't know how to set a device.  Lack of docs.  Just try to set
808	 * IFF_ALLMULTI flag here.
809	 */
810	ifp->if_flags |= IFF_ALLMULTI;
811}
812
813static void
814upgt_start(struct ifnet *ifp)
815{
816	struct upgt_softc *sc = ifp->if_softc;
817	struct upgt_data *data_tx;
818	struct ieee80211_node *ni;
819	struct mbuf *m;
820
821	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
822		return;
823
824	UPGT_LOCK(sc);
825	for (;;) {
826		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
827		if (m == NULL)
828			break;
829
830		data_tx = upgt_gettxbuf(sc);
831		if (data_tx == NULL) {
832			IFQ_DRV_PREPEND(&ifp->if_snd, m);
833			break;
834		}
835
836		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
837		m->m_pkthdr.rcvif = NULL;
838
839		if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
840			STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
841			UPGT_STAT_INC(sc, st_tx_inactive);
842			ieee80211_free_node(ni);
843			ifp->if_oerrors++;
844			continue;
845		}
846		sc->sc_tx_timer = 5;
847	}
848	UPGT_UNLOCK(sc);
849}
850
851static int
852upgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
853	const struct ieee80211_bpf_params *params)
854{
855	struct ieee80211com *ic = ni->ni_ic;
856	struct ifnet *ifp = ic->ic_ifp;
857	struct upgt_softc *sc = ifp->if_softc;
858	struct upgt_data *data_tx = NULL;
859
860	/* prevent management frames from being sent if we're not ready */
861	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
862		m_freem(m);
863		ieee80211_free_node(ni);
864		return ENETDOWN;
865	}
866
867	UPGT_LOCK(sc);
868	data_tx = upgt_gettxbuf(sc);
869	if (data_tx == NULL) {
870		ieee80211_free_node(ni);
871		m_freem(m);
872		UPGT_UNLOCK(sc);
873		return (ENOBUFS);
874	}
875
876	if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
877		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
878		UPGT_STAT_INC(sc, st_tx_inactive);
879		ieee80211_free_node(ni);
880		ifp->if_oerrors++;
881		UPGT_UNLOCK(sc);
882		return (EIO);
883	}
884	UPGT_UNLOCK(sc);
885
886	sc->sc_tx_timer = 5;
887	return (0);
888}
889
890static void
891upgt_watchdog(void *arg)
892{
893	struct upgt_softc *sc = arg;
894	struct ifnet *ifp = sc->sc_ifp;
895
896	if (sc->sc_tx_timer > 0) {
897		if (--sc->sc_tx_timer == 0) {
898			device_printf(sc->sc_dev, "watchdog timeout\n");
899			/* upgt_init(ifp); XXX needs a process context ? */
900			ifp->if_oerrors++;
901			return;
902		}
903		callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
904	}
905}
906
907static uint32_t
908upgt_mem_alloc(struct upgt_softc *sc)
909{
910	int i;
911
912	for (i = 0; i < sc->sc_memory.pages; i++) {
913		if (sc->sc_memory.page[i].used == 0) {
914			sc->sc_memory.page[i].used = 1;
915			return (sc->sc_memory.page[i].addr);
916		}
917	}
918
919	return (0);
920}
921
922static void
923upgt_scan_start(struct ieee80211com *ic)
924{
925	/* do nothing.  */
926}
927
928static void
929upgt_scan_end(struct ieee80211com *ic)
930{
931	/* do nothing.  */
932}
933
934static void
935upgt_set_channel(struct ieee80211com *ic)
936{
937	struct upgt_softc *sc = ic->ic_ifp->if_softc;
938
939	UPGT_LOCK(sc);
940	upgt_set_chan(sc, ic->ic_curchan);
941	UPGT_UNLOCK(sc);
942}
943
944static void
945upgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c)
946{
947	struct ifnet *ifp = sc->sc_ifp;
948	struct ieee80211com *ic = ifp->if_l2com;
949	struct upgt_data *data_cmd;
950	struct upgt_lmac_mem *mem;
951	struct upgt_lmac_channel *chan;
952	int channel;
953
954	UPGT_ASSERT_LOCKED(sc);
955
956	channel = ieee80211_chan2ieee(ic, c);
957	if (channel == 0 || channel == IEEE80211_CHAN_ANY) {
958		/* XXX should NEVER happen */
959		device_printf(sc->sc_dev,
960		    "%s: invalid channel %x\n", __func__, channel);
961		return;
962	}
963
964	DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel);
965
966	data_cmd = upgt_getbuf(sc);
967	if (data_cmd == NULL) {
968		device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
969		return;
970	}
971	/*
972	 * Transmit the URB containing the CMD data.
973	 */
974	bzero(data_cmd->buf, MCLBYTES);
975
976	mem = (struct upgt_lmac_mem *)data_cmd->buf;
977	mem->addr = htole32(sc->sc_memaddr_frame_start +
978	    UPGT_MEMSIZE_FRAME_HEAD);
979
980	chan = (struct upgt_lmac_channel *)(mem + 1);
981
982	chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
983	chan->header1.type = UPGT_H1_TYPE_CTRL;
984	chan->header1.len = htole16(
985	    sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header));
986
987	chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
988	chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
989	chan->header2.flags = 0;
990
991	chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
992	chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
993	chan->freq6 = sc->sc_eeprom_freq6[channel];
994	chan->settings = sc->sc_eeprom_freq6_settings;
995	chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
996
997	bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_1,
998	    sizeof(chan->freq3_1));
999	bcopy(&sc->sc_eeprom_freq4[channel], chan->freq4,
1000	    sizeof(sc->sc_eeprom_freq4[channel]));
1001	bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_2,
1002	    sizeof(chan->freq3_2));
1003
1004	data_cmd->buflen = sizeof(*mem) + sizeof(*chan);
1005
1006	mem->chksum = upgt_chksum_le((uint32_t *)chan,
1007	    data_cmd->buflen - sizeof(*mem));
1008
1009	upgt_bulk_tx(sc, data_cmd);
1010}
1011
1012static struct ieee80211vap *
1013upgt_vap_create(struct ieee80211com *ic,
1014	const char name[IFNAMSIZ], int unit, int opmode, int flags,
1015	const uint8_t bssid[IEEE80211_ADDR_LEN],
1016	const uint8_t mac[IEEE80211_ADDR_LEN])
1017{
1018	struct upgt_vap *uvp;
1019	struct ieee80211vap *vap;
1020
1021	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
1022		return NULL;
1023	uvp = (struct upgt_vap *) malloc(sizeof(struct upgt_vap),
1024	    M_80211_VAP, M_NOWAIT | M_ZERO);
1025	if (uvp == NULL)
1026		return NULL;
1027	vap = &uvp->vap;
1028	/* enable s/w bmiss handling for sta mode */
1029	ieee80211_vap_setup(ic, vap, name, unit, opmode,
1030	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
1031
1032	/* override state transition machine */
1033	uvp->newstate = vap->iv_newstate;
1034	vap->iv_newstate = upgt_newstate;
1035
1036	/* setup device rates */
1037	upgt_setup_rates(vap, ic);
1038
1039	/* complete setup */
1040	ieee80211_vap_attach(vap, ieee80211_media_change,
1041	    ieee80211_media_status);
1042	ic->ic_opmode = opmode;
1043	return vap;
1044}
1045
1046static int
1047upgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1048{
1049	struct upgt_vap *uvp = UPGT_VAP(vap);
1050	struct ieee80211com *ic = vap->iv_ic;
1051	struct upgt_softc *sc = ic->ic_ifp->if_softc;
1052
1053	/* do it in a process context */
1054	sc->sc_state = nstate;
1055
1056	IEEE80211_UNLOCK(ic);
1057	UPGT_LOCK(sc);
1058	callout_stop(&sc->sc_led_ch);
1059	callout_stop(&sc->sc_watchdog_ch);
1060
1061	switch (nstate) {
1062	case IEEE80211_S_INIT:
1063		/* do not accept any frames if the device is down */
1064		(void)upgt_set_macfilter(sc, sc->sc_state);
1065		upgt_set_led(sc, UPGT_LED_OFF);
1066		break;
1067	case IEEE80211_S_SCAN:
1068		upgt_set_chan(sc, ic->ic_curchan);
1069		break;
1070	case IEEE80211_S_AUTH:
1071		upgt_set_chan(sc, ic->ic_curchan);
1072		break;
1073	case IEEE80211_S_ASSOC:
1074		break;
1075	case IEEE80211_S_RUN:
1076		upgt_set_macfilter(sc, sc->sc_state);
1077		upgt_set_led(sc, UPGT_LED_ON);
1078		break;
1079	default:
1080		break;
1081	}
1082	UPGT_UNLOCK(sc);
1083	IEEE80211_LOCK(ic);
1084	return (uvp->newstate(vap, nstate, arg));
1085}
1086
1087static void
1088upgt_vap_delete(struct ieee80211vap *vap)
1089{
1090	struct upgt_vap *uvp = UPGT_VAP(vap);
1091
1092	ieee80211_vap_detach(vap);
1093	free(uvp, M_80211_VAP);
1094}
1095
1096static void
1097upgt_update_mcast(struct ifnet *ifp)
1098{
1099	struct upgt_softc *sc = ifp->if_softc;
1100
1101	upgt_set_multi(sc);
1102}
1103
1104static int
1105upgt_eeprom_parse(struct upgt_softc *sc)
1106{
1107	struct upgt_eeprom_header *eeprom_header;
1108	struct upgt_eeprom_option *eeprom_option;
1109	uint16_t option_len;
1110	uint16_t option_type;
1111	uint16_t preamble_len;
1112	int option_end = 0;
1113
1114	/* calculate eeprom options start offset */
1115	eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
1116	preamble_len = le16toh(eeprom_header->preamble_len);
1117	eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
1118	    (sizeof(struct upgt_eeprom_header) + preamble_len));
1119
1120	while (!option_end) {
1121		/* the eeprom option length is stored in words */
1122		option_len =
1123		    (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t);
1124		option_type =
1125		    le16toh(eeprom_option->type);
1126
1127		switch (option_type) {
1128		case UPGT_EEPROM_TYPE_NAME:
1129			DPRINTF(sc, UPGT_DEBUG_FW,
1130			    "EEPROM name len=%d\n", option_len);
1131			break;
1132		case UPGT_EEPROM_TYPE_SERIAL:
1133			DPRINTF(sc, UPGT_DEBUG_FW,
1134			    "EEPROM serial len=%d\n", option_len);
1135			break;
1136		case UPGT_EEPROM_TYPE_MAC:
1137			DPRINTF(sc, UPGT_DEBUG_FW,
1138			    "EEPROM mac len=%d\n", option_len);
1139
1140			IEEE80211_ADDR_COPY(sc->sc_myaddr, eeprom_option->data);
1141			break;
1142		case UPGT_EEPROM_TYPE_HWRX:
1143			DPRINTF(sc, UPGT_DEBUG_FW,
1144			    "EEPROM hwrx len=%d\n", option_len);
1145
1146			upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1147			break;
1148		case UPGT_EEPROM_TYPE_CHIP:
1149			DPRINTF(sc, UPGT_DEBUG_FW,
1150			    "EEPROM chip len=%d\n", option_len);
1151			break;
1152		case UPGT_EEPROM_TYPE_FREQ3:
1153			DPRINTF(sc, UPGT_DEBUG_FW,
1154			    "EEPROM freq3 len=%d\n", option_len);
1155
1156			upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1157			    option_len);
1158			break;
1159		case UPGT_EEPROM_TYPE_FREQ4:
1160			DPRINTF(sc, UPGT_DEBUG_FW,
1161			    "EEPROM freq4 len=%d\n", option_len);
1162
1163			upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1164			    option_len);
1165			break;
1166		case UPGT_EEPROM_TYPE_FREQ5:
1167			DPRINTF(sc, UPGT_DEBUG_FW,
1168			    "EEPROM freq5 len=%d\n", option_len);
1169			break;
1170		case UPGT_EEPROM_TYPE_FREQ6:
1171			DPRINTF(sc, UPGT_DEBUG_FW,
1172			    "EEPROM freq6 len=%d\n", option_len);
1173
1174			upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1175			    option_len);
1176			break;
1177		case UPGT_EEPROM_TYPE_END:
1178			DPRINTF(sc, UPGT_DEBUG_FW,
1179			    "EEPROM end len=%d\n", option_len);
1180			option_end = 1;
1181			break;
1182		case UPGT_EEPROM_TYPE_OFF:
1183			DPRINTF(sc, UPGT_DEBUG_FW,
1184			    "%s: EEPROM off without end option!\n", __func__);
1185			return (EIO);
1186		default:
1187			DPRINTF(sc, UPGT_DEBUG_FW,
1188			    "EEPROM unknown type 0x%04x len=%d\n",
1189			    option_type, option_len);
1190			break;
1191		}
1192
1193		/* jump to next EEPROM option */
1194		eeprom_option = (struct upgt_eeprom_option *)
1195		    (eeprom_option->data + option_len);
1196	}
1197
1198	return (0);
1199}
1200
1201static void
1202upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1203{
1204	struct upgt_eeprom_freq3_header *freq3_header;
1205	struct upgt_lmac_freq3 *freq3;
1206	int i, elements, flags;
1207	unsigned channel;
1208
1209	freq3_header = (struct upgt_eeprom_freq3_header *)data;
1210	freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1211
1212	flags = freq3_header->flags;
1213	elements = freq3_header->elements;
1214
1215	DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n",
1216	    flags, elements);
1217
1218	for (i = 0; i < elements; i++) {
1219		channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0);
1220		if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1221			continue;
1222
1223		sc->sc_eeprom_freq3[channel] = freq3[i];
1224
1225		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1226		    le16toh(sc->sc_eeprom_freq3[channel].freq), channel);
1227	}
1228}
1229
1230void
1231upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1232{
1233	struct upgt_eeprom_freq4_header *freq4_header;
1234	struct upgt_eeprom_freq4_1 *freq4_1;
1235	struct upgt_eeprom_freq4_2 *freq4_2;
1236	int i, j, elements, settings, flags;
1237	unsigned channel;
1238
1239	freq4_header = (struct upgt_eeprom_freq4_header *)data;
1240	freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1241	flags = freq4_header->flags;
1242	elements = freq4_header->elements;
1243	settings = freq4_header->settings;
1244
1245	/* we need this value later */
1246	sc->sc_eeprom_freq6_settings = freq4_header->settings;
1247
1248	DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n",
1249	    flags, elements, settings);
1250
1251	for (i = 0; i < elements; i++) {
1252		channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0);
1253		if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1254			continue;
1255
1256		freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1257		for (j = 0; j < settings; j++) {
1258			sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1259			sc->sc_eeprom_freq4[channel][j].pad = 0;
1260		}
1261
1262		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1263		    le16toh(freq4_1[i].freq), channel);
1264	}
1265}
1266
1267void
1268upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1269{
1270	struct upgt_lmac_freq6 *freq6;
1271	int i, elements;
1272	unsigned channel;
1273
1274	freq6 = (struct upgt_lmac_freq6 *)data;
1275	elements = len / sizeof(struct upgt_lmac_freq6);
1276
1277	DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements);
1278
1279	for (i = 0; i < elements; i++) {
1280		channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0);
1281		if (!(channel >= 0 && channel < IEEE80211_CHAN_MAX))
1282			continue;
1283
1284		sc->sc_eeprom_freq6[channel] = freq6[i];
1285
1286		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1287		    le16toh(sc->sc_eeprom_freq6[channel].freq), channel);
1288	}
1289}
1290
1291static void
1292upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1293{
1294	struct upgt_eeprom_option_hwrx *option_hwrx;
1295
1296	option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1297
1298	sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1299
1300	DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n",
1301	    sc->sc_eeprom_hwrx);
1302}
1303
1304static int
1305upgt_eeprom_read(struct upgt_softc *sc)
1306{
1307	struct upgt_data *data_cmd;
1308	struct upgt_lmac_mem *mem;
1309	struct upgt_lmac_eeprom	*eeprom;
1310	int block, error, offset;
1311
1312	UPGT_LOCK(sc);
1313	usb_pause_mtx(&sc->sc_mtx, 100);
1314
1315	offset = 0;
1316	block = UPGT_EEPROM_BLOCK_SIZE;
1317	while (offset < UPGT_EEPROM_SIZE) {
1318		DPRINTF(sc, UPGT_DEBUG_FW,
1319		    "request EEPROM block (offset=%d, len=%d)\n", offset, block);
1320
1321		data_cmd = upgt_getbuf(sc);
1322		if (data_cmd == NULL) {
1323			UPGT_UNLOCK(sc);
1324			return (ENOBUFS);
1325		}
1326
1327		/*
1328		 * Transmit the URB containing the CMD data.
1329		 */
1330		bzero(data_cmd->buf, MCLBYTES);
1331
1332		mem = (struct upgt_lmac_mem *)data_cmd->buf;
1333		mem->addr = htole32(sc->sc_memaddr_frame_start +
1334		    UPGT_MEMSIZE_FRAME_HEAD);
1335
1336		eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
1337		eeprom->header1.flags = 0;
1338		eeprom->header1.type = UPGT_H1_TYPE_CTRL;
1339		eeprom->header1.len = htole16((
1340		    sizeof(struct upgt_lmac_eeprom) -
1341		    sizeof(struct upgt_lmac_header)) + block);
1342
1343		eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1344		eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
1345		eeprom->header2.flags = 0;
1346
1347		eeprom->offset = htole16(offset);
1348		eeprom->len = htole16(block);
1349
1350		data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block;
1351
1352		mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
1353		    data_cmd->buflen - sizeof(*mem));
1354		upgt_bulk_tx(sc, data_cmd);
1355
1356		error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz);
1357		if (error != 0) {
1358			device_printf(sc->sc_dev,
1359			    "timeout while waiting for EEPROM data!\n");
1360			UPGT_UNLOCK(sc);
1361			return (EIO);
1362		}
1363
1364		offset += block;
1365		if (UPGT_EEPROM_SIZE - offset < block)
1366			block = UPGT_EEPROM_SIZE - offset;
1367	}
1368
1369	UPGT_UNLOCK(sc);
1370	return (0);
1371}
1372
1373/*
1374 * When a rx data came in the function returns a mbuf and a rssi values.
1375 */
1376static struct mbuf *
1377upgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi)
1378{
1379	struct mbuf *m = NULL;
1380	struct upgt_softc *sc = usbd_xfer_softc(xfer);
1381	struct upgt_lmac_header *header;
1382	struct upgt_lmac_eeprom *eeprom;
1383	uint8_t h1_type;
1384	uint16_t h2_type;
1385	int actlen, sumlen;
1386
1387	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1388
1389	UPGT_ASSERT_LOCKED(sc);
1390
1391	if (actlen < 1)
1392		return (NULL);
1393
1394	/* Check only at the very beginning.  */
1395	if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) &&
1396	    (memcmp(data->buf, "OK", 2) == 0)) {
1397		sc->sc_flags |= UPGT_FLAG_FWLOADED;
1398		wakeup_one(sc);
1399		return (NULL);
1400	}
1401
1402	if (actlen < UPGT_RX_MINSZ)
1403		return (NULL);
1404
1405	/*
1406	 * Check what type of frame came in.
1407	 */
1408	header = (struct upgt_lmac_header *)(data->buf + 4);
1409
1410	h1_type = header->header1.type;
1411	h2_type = le16toh(header->header2.type);
1412
1413	if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) {
1414		eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4);
1415		uint16_t eeprom_offset = le16toh(eeprom->offset);
1416		uint16_t eeprom_len = le16toh(eeprom->len);
1417
1418		DPRINTF(sc, UPGT_DEBUG_FW,
1419		    "received EEPROM block (offset=%d, len=%d)\n",
1420		    eeprom_offset, eeprom_len);
1421
1422		bcopy(data->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1423			sc->sc_eeprom + eeprom_offset, eeprom_len);
1424
1425		/* EEPROM data has arrived in time, wakeup.  */
1426		wakeup(sc);
1427	} else if (h1_type == UPGT_H1_TYPE_CTRL &&
1428	    h2_type == UPGT_H2_TYPE_TX_DONE) {
1429		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n",
1430		    __func__);
1431		upgt_tx_done(sc, data->buf + 4);
1432	} else if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1433	    h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1434		DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n",
1435		    __func__);
1436		m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len),
1437		    rssi);
1438	} else if (h1_type == UPGT_H1_TYPE_CTRL &&
1439	    h2_type == UPGT_H2_TYPE_STATS) {
1440		DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n",
1441		    __func__);
1442		/* TODO: what could we do with the statistic data? */
1443	} else {
1444		/* ignore unknown frame types */
1445		DPRINTF(sc, UPGT_DEBUG_INTR,
1446		    "received unknown frame type 0x%02x\n",
1447		    header->header1.type);
1448	}
1449	return (m);
1450}
1451
1452/*
1453 * The firmware awaits a checksum for each frame we send to it.
1454 * The algorithm used therefor is uncommon but somehow similar to CRC32.
1455 */
1456static uint32_t
1457upgt_chksum_le(const uint32_t *buf, size_t size)
1458{
1459	int i;
1460	uint32_t crc = 0;
1461
1462	for (i = 0; i < size; i += sizeof(uint32_t)) {
1463		crc = htole32(crc ^ *buf++);
1464		crc = htole32((crc >> 5) ^ (crc << 3));
1465	}
1466
1467	return (crc);
1468}
1469
1470static struct mbuf *
1471upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi)
1472{
1473	struct ifnet *ifp = sc->sc_ifp;
1474	struct ieee80211com *ic = ifp->if_l2com;
1475	struct upgt_lmac_rx_desc *rxdesc;
1476	struct mbuf *m;
1477
1478	/*
1479	 * don't pass packets to the ieee80211 framework if the driver isn't
1480	 * RUNNING.
1481	 */
1482	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1483		return (NULL);
1484
1485	/* access RX packet descriptor */
1486	rxdesc = (struct upgt_lmac_rx_desc *)data;
1487
1488	/* create mbuf which is suitable for strict alignment archs */
1489	KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES,
1490	    ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN));
1491	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1492	if (m == NULL) {
1493		device_printf(sc->sc_dev, "could not create RX mbuf!\n");
1494		return (NULL);
1495	}
1496	m_adj(m, ETHER_ALIGN);
1497	bcopy(rxdesc->data, mtod(m, char *), pkglen);
1498	/* trim FCS */
1499	m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN;
1500	m->m_pkthdr.rcvif = ifp;
1501
1502	if (ieee80211_radiotap_active(ic)) {
1503		struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1504
1505		tap->wr_flags = 0;
1506		tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1507		tap->wr_antsignal = rxdesc->rssi;
1508	}
1509	ifp->if_ipackets++;
1510
1511	DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__);
1512	*rssi = rxdesc->rssi;
1513	return (m);
1514}
1515
1516static uint8_t
1517upgt_rx_rate(struct upgt_softc *sc, const int rate)
1518{
1519	struct ifnet *ifp = sc->sc_ifp;
1520	struct ieee80211com *ic = ifp->if_l2com;
1521	static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 };
1522	static const uint8_t ofdm_upgt2rate[12] =
1523	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
1524
1525	if (ic->ic_curmode == IEEE80211_MODE_11B &&
1526	    !(rate < 0 || rate > 3))
1527		return cck_upgt2rate[rate & 0xf];
1528
1529	if (ic->ic_curmode == IEEE80211_MODE_11G &&
1530	    !(rate < 0 || rate > 11))
1531		return ofdm_upgt2rate[rate & 0xf];
1532
1533	return (0);
1534}
1535
1536static void
1537upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1538{
1539	struct ifnet *ifp = sc->sc_ifp;
1540	struct upgt_lmac_tx_done_desc *desc;
1541	int i, freed = 0;
1542
1543	UPGT_ASSERT_LOCKED(sc);
1544
1545	desc = (struct upgt_lmac_tx_done_desc *)data;
1546
1547	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1548		struct upgt_data *data_tx = &sc->sc_tx_data[i];
1549
1550		if (data_tx->addr == le32toh(desc->header2.reqid)) {
1551			upgt_mem_free(sc, data_tx->addr);
1552			data_tx->ni = NULL;
1553			data_tx->addr = 0;
1554			data_tx->m = NULL;
1555			data_tx->use = 0;
1556
1557			DPRINTF(sc, UPGT_DEBUG_TX_PROC,
1558			    "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1559			    le32toh(desc->header2.reqid),
1560			    le16toh(desc->status), le16toh(desc->rssi));
1561			DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n",
1562			    le16toh(desc->seq));
1563
1564			freed++;
1565		}
1566	}
1567
1568	if (freed != 0) {
1569		sc->sc_tx_timer = 0;
1570		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1571		UPGT_UNLOCK(sc);
1572		upgt_start(ifp);
1573		UPGT_LOCK(sc);
1574	}
1575}
1576
1577static void
1578upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
1579{
1580	int i;
1581
1582	for (i = 0; i < sc->sc_memory.pages; i++) {
1583		if (sc->sc_memory.page[i].addr == addr) {
1584			sc->sc_memory.page[i].used = 0;
1585			return;
1586		}
1587	}
1588
1589	device_printf(sc->sc_dev,
1590	    "could not free memory address 0x%08x!\n", addr);
1591}
1592
1593static int
1594upgt_fw_load(struct upgt_softc *sc)
1595{
1596	const struct firmware *fw;
1597	struct upgt_data *data_cmd;
1598	struct upgt_fw_x2_header *x2;
1599	char start_fwload_cmd[] = { 0x3c, 0x0d };
1600	int error = 0, offset, bsize, n;
1601	uint32_t crc32;
1602
1603	fw = firmware_get(upgt_fwname);
1604	if (fw == NULL) {
1605		device_printf(sc->sc_dev, "could not read microcode %s!\n",
1606		    upgt_fwname);
1607		return (EIO);
1608	}
1609
1610	UPGT_LOCK(sc);
1611
1612	/* send firmware start load command */
1613	data_cmd = upgt_getbuf(sc);
1614	if (data_cmd == NULL) {
1615		error = ENOBUFS;
1616		goto fail;
1617	}
1618	data_cmd->buflen = sizeof(start_fwload_cmd);
1619	bcopy(start_fwload_cmd, data_cmd->buf, data_cmd->buflen);
1620	upgt_bulk_tx(sc, data_cmd);
1621
1622	/* send X2 header */
1623	data_cmd = upgt_getbuf(sc);
1624	if (data_cmd == NULL) {
1625		error = ENOBUFS;
1626		goto fail;
1627	}
1628	data_cmd->buflen = sizeof(struct upgt_fw_x2_header);
1629	x2 = (struct upgt_fw_x2_header *)data_cmd->buf;
1630	bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE);
1631	x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
1632	x2->len = htole32(fw->datasize);
1633	x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf +
1634	    UPGT_X2_SIGNATURE_SIZE,
1635	    sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
1636	    sizeof(uint32_t));
1637	upgt_bulk_tx(sc, data_cmd);
1638
1639	/* download firmware */
1640	for (offset = 0; offset < fw->datasize; offset += bsize) {
1641		if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE)
1642			bsize = UPGT_FW_BLOCK_SIZE;
1643		else
1644			bsize = fw->datasize - offset;
1645
1646		data_cmd = upgt_getbuf(sc);
1647		if (data_cmd == NULL) {
1648			error = ENOBUFS;
1649			goto fail;
1650		}
1651		n = upgt_fw_copy((const uint8_t *)fw->data + offset,
1652		    data_cmd->buf, bsize);
1653		data_cmd->buflen = bsize;
1654		upgt_bulk_tx(sc, data_cmd);
1655
1656		DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n",
1657		    offset, n, bsize);
1658		bsize = n;
1659	}
1660	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__);
1661
1662	/* load firmware */
1663	data_cmd = upgt_getbuf(sc);
1664	if (data_cmd == NULL) {
1665		error = ENOBUFS;
1666		goto fail;
1667	}
1668	crc32 = upgt_crc32_le(fw->data, fw->datasize);
1669	*((uint32_t *)(data_cmd->buf)    ) = crc32;
1670	*((uint8_t  *)(data_cmd->buf) + 4) = 'g';
1671	*((uint8_t  *)(data_cmd->buf) + 5) = '\r';
1672	data_cmd->buflen = 6;
1673	upgt_bulk_tx(sc, data_cmd);
1674
1675	/* waiting 'OK' response.  */
1676	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
1677	error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz);
1678	if (error != 0) {
1679		device_printf(sc->sc_dev, "firmware load failed!\n");
1680		error = EIO;
1681	}
1682
1683	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__);
1684fail:
1685	UPGT_UNLOCK(sc);
1686	firmware_put(fw, FIRMWARE_UNLOAD);
1687	return (error);
1688}
1689
1690static uint32_t
1691upgt_crc32_le(const void *buf, size_t size)
1692{
1693	uint32_t crc;
1694
1695	crc = ether_crc32_le(buf, size);
1696
1697	/* apply final XOR value as common for CRC-32 */
1698	crc = htole32(crc ^ 0xffffffffU);
1699
1700	return (crc);
1701}
1702
1703/*
1704 * While copying the version 2 firmware, we need to replace two characters:
1705 *
1706 * 0x7e -> 0x7d 0x5e
1707 * 0x7d -> 0x7d 0x5d
1708 */
1709static int
1710upgt_fw_copy(const uint8_t *src, char *dst, int size)
1711{
1712	int i, j;
1713
1714	for (i = 0, j = 0; i < size && j < size; i++) {
1715		switch (src[i]) {
1716		case 0x7e:
1717			dst[j] = 0x7d;
1718			j++;
1719			dst[j] = 0x5e;
1720			j++;
1721			break;
1722		case 0x7d:
1723			dst[j] = 0x7d;
1724			j++;
1725			dst[j] = 0x5d;
1726			j++;
1727			break;
1728		default:
1729			dst[j] = src[i];
1730			j++;
1731			break;
1732		}
1733	}
1734
1735	return (i);
1736}
1737
1738static int
1739upgt_mem_init(struct upgt_softc *sc)
1740{
1741	int i;
1742
1743	for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
1744		sc->sc_memory.page[i].used = 0;
1745
1746		if (i == 0) {
1747			/*
1748			 * The first memory page is always reserved for
1749			 * command data.
1750			 */
1751			sc->sc_memory.page[i].addr =
1752			    sc->sc_memaddr_frame_start + MCLBYTES;
1753		} else {
1754			sc->sc_memory.page[i].addr =
1755			    sc->sc_memory.page[i - 1].addr + MCLBYTES;
1756		}
1757
1758		if (sc->sc_memory.page[i].addr + MCLBYTES >=
1759		    sc->sc_memaddr_frame_end)
1760			break;
1761
1762		DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n",
1763		    i, sc->sc_memory.page[i].addr);
1764	}
1765
1766	sc->sc_memory.pages = i;
1767
1768	DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages);
1769	return (0);
1770}
1771
1772static int
1773upgt_fw_verify(struct upgt_softc *sc)
1774{
1775	const struct firmware *fw;
1776	const struct upgt_fw_bra_option *bra_opt;
1777	const struct upgt_fw_bra_descr *descr;
1778	const uint8_t *p;
1779	const uint32_t *uc;
1780	uint32_t bra_option_type, bra_option_len;
1781	int offset, bra_end = 0, error = 0;
1782
1783	fw = firmware_get(upgt_fwname);
1784	if (fw == NULL) {
1785		device_printf(sc->sc_dev, "could not read microcode %s!\n",
1786		    upgt_fwname);
1787		return EIO;
1788	}
1789
1790	/*
1791	 * Seek to beginning of Boot Record Area (BRA).
1792	 */
1793	for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) {
1794		uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1795		if (*uc == 0)
1796			break;
1797	}
1798	for (; offset < fw->datasize; offset += sizeof(*uc)) {
1799		uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1800		if (*uc != 0)
1801			break;
1802	}
1803	if (offset == fw->datasize) {
1804		device_printf(sc->sc_dev,
1805		    "firmware Boot Record Area not found!\n");
1806		error = EIO;
1807		goto fail;
1808	}
1809
1810	DPRINTF(sc, UPGT_DEBUG_FW,
1811	    "firmware Boot Record Area found at offset %d\n", offset);
1812
1813	/*
1814	 * Parse Boot Record Area (BRA) options.
1815	 */
1816	while (offset < fw->datasize && bra_end == 0) {
1817		/* get current BRA option */
1818		p = (const uint8_t *)fw->data + offset;
1819		bra_opt = (const struct upgt_fw_bra_option *)p;
1820		bra_option_type = le32toh(bra_opt->type);
1821		bra_option_len = le32toh(bra_opt->len) * sizeof(*uc);
1822
1823		switch (bra_option_type) {
1824		case UPGT_BRA_TYPE_FW:
1825			DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n",
1826			    bra_option_len);
1827
1828			if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
1829				device_printf(sc->sc_dev,
1830				    "wrong UPGT_BRA_TYPE_FW len!\n");
1831				error = EIO;
1832				goto fail;
1833			}
1834			if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data,
1835			    bra_option_len) == 0) {
1836				sc->sc_fw_type = UPGT_FWTYPE_LM86;
1837				break;
1838			}
1839			if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data,
1840			    bra_option_len) == 0) {
1841				sc->sc_fw_type = UPGT_FWTYPE_LM87;
1842				break;
1843			}
1844			device_printf(sc->sc_dev,
1845			    "unsupported firmware type!\n");
1846			error = EIO;
1847			goto fail;
1848		case UPGT_BRA_TYPE_VERSION:
1849			DPRINTF(sc, UPGT_DEBUG_FW,
1850			    "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len);
1851			break;
1852		case UPGT_BRA_TYPE_DEPIF:
1853			DPRINTF(sc, UPGT_DEBUG_FW,
1854			    "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len);
1855			break;
1856		case UPGT_BRA_TYPE_EXPIF:
1857			DPRINTF(sc, UPGT_DEBUG_FW,
1858			    "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len);
1859			break;
1860		case UPGT_BRA_TYPE_DESCR:
1861			DPRINTF(sc, UPGT_DEBUG_FW,
1862			    "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len);
1863
1864			descr = (const struct upgt_fw_bra_descr *)bra_opt->data;
1865
1866			sc->sc_memaddr_frame_start =
1867			    le32toh(descr->memaddr_space_start);
1868			sc->sc_memaddr_frame_end =
1869			    le32toh(descr->memaddr_space_end);
1870
1871			DPRINTF(sc, UPGT_DEBUG_FW,
1872			    "memory address space start=0x%08x\n",
1873			    sc->sc_memaddr_frame_start);
1874			DPRINTF(sc, UPGT_DEBUG_FW,
1875			    "memory address space end=0x%08x\n",
1876			    sc->sc_memaddr_frame_end);
1877			break;
1878		case UPGT_BRA_TYPE_END:
1879			DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n",
1880			    bra_option_len);
1881			bra_end = 1;
1882			break;
1883		default:
1884			DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n",
1885			    bra_option_len);
1886			error = EIO;
1887			goto fail;
1888		}
1889
1890		/* jump to next BRA option */
1891		offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
1892	}
1893
1894	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__);
1895fail:
1896	firmware_put(fw, FIRMWARE_UNLOAD);
1897	return (error);
1898}
1899
1900static void
1901upgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data)
1902{
1903
1904	UPGT_ASSERT_LOCKED(sc);
1905
1906	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1907	UPGT_STAT_INC(sc, st_tx_pending);
1908	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]);
1909}
1910
1911static int
1912upgt_device_reset(struct upgt_softc *sc)
1913{
1914	struct upgt_data *data;
1915	char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
1916
1917	UPGT_LOCK(sc);
1918
1919	data = upgt_getbuf(sc);
1920	if (data == NULL) {
1921		UPGT_UNLOCK(sc);
1922		return (ENOBUFS);
1923	}
1924	bcopy(init_cmd, data->buf, sizeof(init_cmd));
1925	data->buflen = sizeof(init_cmd);
1926	upgt_bulk_tx(sc, data);
1927	usb_pause_mtx(&sc->sc_mtx, 100);
1928
1929	UPGT_UNLOCK(sc);
1930	DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__);
1931	return (0);
1932}
1933
1934static int
1935upgt_alloc_tx(struct upgt_softc *sc)
1936{
1937	int i;
1938
1939	STAILQ_INIT(&sc->sc_tx_active);
1940	STAILQ_INIT(&sc->sc_tx_inactive);
1941	STAILQ_INIT(&sc->sc_tx_pending);
1942
1943	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1944		struct upgt_data *data = &sc->sc_tx_data[i];
1945
1946		data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO);
1947		if (data->buf == NULL) {
1948			device_printf(sc->sc_dev,
1949			    "could not allocate TX buffer!\n");
1950			return (ENOMEM);
1951		}
1952		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1953		UPGT_STAT_INC(sc, st_tx_inactive);
1954	}
1955
1956	return (0);
1957}
1958
1959static int
1960upgt_alloc_rx(struct upgt_softc *sc)
1961{
1962	int i;
1963
1964	STAILQ_INIT(&sc->sc_rx_active);
1965	STAILQ_INIT(&sc->sc_rx_inactive);
1966
1967	for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1968		struct upgt_data *data = &sc->sc_rx_data[i];
1969
1970		data->buf = malloc(MCLBYTES, M_USBDEV, M_NOWAIT | M_ZERO);
1971		if (data->buf == NULL) {
1972			device_printf(sc->sc_dev,
1973			    "could not allocate RX buffer!\n");
1974			return (ENOMEM);
1975		}
1976		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1977	}
1978
1979	return (0);
1980}
1981
1982static int
1983upgt_detach(device_t dev)
1984{
1985	struct upgt_softc *sc = device_get_softc(dev);
1986	struct ifnet *ifp = sc->sc_ifp;
1987	struct ieee80211com *ic = ifp->if_l2com;
1988
1989	if (!device_is_attached(dev))
1990		return 0;
1991
1992	upgt_stop(sc);
1993
1994	callout_drain(&sc->sc_led_ch);
1995	callout_drain(&sc->sc_watchdog_ch);
1996
1997	usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
1998	ieee80211_ifdetach(ic);
1999	upgt_free_rx(sc);
2000	upgt_free_tx(sc);
2001
2002	if_free(ifp);
2003	mtx_destroy(&sc->sc_mtx);
2004
2005	return (0);
2006}
2007
2008static void
2009upgt_free_rx(struct upgt_softc *sc)
2010{
2011	int i;
2012
2013	for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
2014		struct upgt_data *data = &sc->sc_rx_data[i];
2015
2016		free(data->buf, M_USBDEV);
2017		data->ni = NULL;
2018	}
2019}
2020
2021static void
2022upgt_free_tx(struct upgt_softc *sc)
2023{
2024	int i;
2025
2026	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
2027		struct upgt_data *data = &sc->sc_tx_data[i];
2028
2029		free(data->buf, M_USBDEV);
2030		data->ni = NULL;
2031	}
2032}
2033
2034static void
2035upgt_abort_xfers_locked(struct upgt_softc *sc)
2036{
2037	int i;
2038
2039	UPGT_ASSERT_LOCKED(sc);
2040	/* abort any pending transfers */
2041	for (i = 0; i < UPGT_N_XFERS; i++)
2042		usbd_transfer_stop(sc->sc_xfer[i]);
2043}
2044
2045static void
2046upgt_abort_xfers(struct upgt_softc *sc)
2047{
2048
2049	UPGT_LOCK(sc);
2050	upgt_abort_xfers_locked(sc);
2051	UPGT_UNLOCK(sc);
2052}
2053
2054#define	UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2055	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2056
2057static void
2058upgt_sysctl_node(struct upgt_softc *sc)
2059{
2060	struct sysctl_ctx_list *ctx;
2061	struct sysctl_oid_list *child;
2062	struct sysctl_oid *tree;
2063	struct upgt_stat *stats;
2064
2065	stats = &sc->sc_stat;
2066	ctx = device_get_sysctl_ctx(sc->sc_dev);
2067	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2068
2069	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2070	    NULL, "UPGT statistics");
2071	child = SYSCTL_CHILDREN(tree);
2072	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2073	    &stats->st_tx_active, "Active numbers in TX queue");
2074	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2075	    &stats->st_tx_inactive, "Inactive numbers in TX queue");
2076	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2077	    &stats->st_tx_pending, "Pending numbers in TX queue");
2078}
2079
2080#undef UPGT_SYSCTL_STAT_ADD32
2081
2082static struct upgt_data *
2083_upgt_getbuf(struct upgt_softc *sc)
2084{
2085	struct upgt_data *bf;
2086
2087	bf = STAILQ_FIRST(&sc->sc_tx_inactive);
2088	if (bf != NULL) {
2089		STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
2090		UPGT_STAT_DEC(sc, st_tx_inactive);
2091	} else
2092		bf = NULL;
2093	if (bf == NULL)
2094		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__,
2095		    "out of xmit buffers");
2096	return (bf);
2097}
2098
2099static struct upgt_data *
2100upgt_getbuf(struct upgt_softc *sc)
2101{
2102	struct upgt_data *bf;
2103
2104	UPGT_ASSERT_LOCKED(sc);
2105
2106	bf = _upgt_getbuf(sc);
2107	if (bf == NULL) {
2108		struct ifnet *ifp = sc->sc_ifp;
2109
2110		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__);
2111		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2112	}
2113
2114	return (bf);
2115}
2116
2117static struct upgt_data *
2118upgt_gettxbuf(struct upgt_softc *sc)
2119{
2120	struct upgt_data *bf;
2121
2122	UPGT_ASSERT_LOCKED(sc);
2123
2124	bf = upgt_getbuf(sc);
2125	if (bf == NULL)
2126		return (NULL);
2127
2128	bf->addr = upgt_mem_alloc(sc);
2129	if (bf->addr == 0) {
2130		struct ifnet *ifp = sc->sc_ifp;
2131
2132		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n",
2133		    __func__);
2134		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
2135		UPGT_STAT_INC(sc, st_tx_inactive);
2136		if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE))
2137			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2138		return (NULL);
2139	}
2140	return (bf);
2141}
2142
2143static int
2144upgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2145    struct upgt_data *data)
2146{
2147	struct ieee80211vap *vap = ni->ni_vap;
2148	int error = 0, len;
2149	struct ieee80211_frame *wh;
2150	struct ieee80211_key *k;
2151	struct ifnet *ifp = sc->sc_ifp;
2152	struct upgt_lmac_mem *mem;
2153	struct upgt_lmac_tx_desc *txdesc;
2154
2155	UPGT_ASSERT_LOCKED(sc);
2156
2157	upgt_set_led(sc, UPGT_LED_BLINK);
2158
2159	/*
2160	 * Software crypto.
2161	 */
2162	wh = mtod(m, struct ieee80211_frame *);
2163	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2164		k = ieee80211_crypto_encap(ni, m);
2165		if (k == NULL) {
2166			device_printf(sc->sc_dev,
2167			    "ieee80211_crypto_encap returns NULL.\n");
2168			error = EIO;
2169			goto done;
2170		}
2171
2172		/* in case packet header moved, reset pointer */
2173		wh = mtod(m, struct ieee80211_frame *);
2174	}
2175
2176	/* Transmit the URB containing the TX data.  */
2177	bzero(data->buf, MCLBYTES);
2178	mem = (struct upgt_lmac_mem *)data->buf;
2179	mem->addr = htole32(data->addr);
2180	txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
2181
2182	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2183	    IEEE80211_FC0_TYPE_MGT) {
2184		/* mgmt frames  */
2185		txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT;
2186		/* always send mgmt frames at lowest rate (DS1) */
2187		memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
2188	} else {
2189		/* data frames  */
2190		txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
2191		bcopy(sc->sc_cur_rateset, txdesc->rates, sizeof(txdesc->rates));
2192	}
2193	txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
2194	txdesc->header1.len = htole16(m->m_pkthdr.len);
2195	txdesc->header2.reqid = htole32(data->addr);
2196	txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
2197	txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
2198	txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
2199	txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
2200
2201	if (ieee80211_radiotap_active_vap(vap)) {
2202		struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
2203
2204		tap->wt_flags = 0;
2205		tap->wt_rate = 0;	/* XXX where to get from? */
2206
2207		ieee80211_radiotap_tx(vap, m);
2208	}
2209
2210	/* copy frame below our TX descriptor header */
2211	m_copydata(m, 0, m->m_pkthdr.len,
2212	    data->buf + (sizeof(*mem) + sizeof(*txdesc)));
2213	/* calculate frame size */
2214	len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
2215	/* we need to align the frame to a 4 byte boundary */
2216	len = (len + 3) & ~3;
2217	/* calculate frame checksum */
2218	mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem));
2219	data->ni = ni;
2220	data->m = m;
2221	data->buflen = len;
2222
2223	DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n",
2224	    __func__, len);
2225	KASSERT(len <= MCLBYTES, ("mbuf is small for saving data"));
2226
2227	upgt_bulk_tx(sc, data);
2228done:
2229	/*
2230	 * If we don't regulary read the device statistics, the RX queue
2231	 * will stall.  It's strange, but it works, so we keep reading
2232	 * the statistics here.  *shrug*
2233	 */
2234	if (!(ifp->if_opackets % UPGT_TX_STAT_INTERVAL))
2235		upgt_get_stats(sc);
2236
2237	return (error);
2238}
2239
2240static void
2241upgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2242{
2243	struct upgt_softc *sc = usbd_xfer_softc(xfer);
2244	struct ifnet *ifp = sc->sc_ifp;
2245	struct ieee80211com *ic = ifp->if_l2com;
2246	struct ieee80211_frame *wh;
2247	struct ieee80211_node *ni;
2248	struct mbuf *m = NULL;
2249	struct upgt_data *data;
2250	int8_t nf;
2251	int rssi = -1;
2252
2253	UPGT_ASSERT_LOCKED(sc);
2254
2255	switch (USB_GET_STATE(xfer)) {
2256	case USB_ST_TRANSFERRED:
2257		data = STAILQ_FIRST(&sc->sc_rx_active);
2258		if (data == NULL)
2259			goto setup;
2260		STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2261		m = upgt_rxeof(xfer, data, &rssi);
2262		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2263		/* FALLTHROUGH */
2264	case USB_ST_SETUP:
2265setup:
2266		data = STAILQ_FIRST(&sc->sc_rx_inactive);
2267		if (data == NULL)
2268			return;
2269		STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2270		STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2271		usbd_xfer_set_frame_data(xfer, 0, data->buf,
2272		    usbd_xfer_max_len(xfer));
2273		usbd_transfer_submit(xfer);
2274
2275		/*
2276		 * To avoid LOR we should unlock our private mutex here to call
2277		 * ieee80211_input() because here is at the end of a USB
2278		 * callback and safe to unlock.
2279		 */
2280		UPGT_UNLOCK(sc);
2281		if (m != NULL) {
2282			wh = mtod(m, struct ieee80211_frame *);
2283			ni = ieee80211_find_rxnode(ic,
2284			    (struct ieee80211_frame_min *)wh);
2285			nf = -95;	/* XXX */
2286			if (ni != NULL) {
2287				(void) ieee80211_input(ni, m, rssi, nf);
2288				/* node is no longer needed */
2289				ieee80211_free_node(ni);
2290			} else
2291				(void) ieee80211_input_all(ic, m, rssi, nf);
2292			m = NULL;
2293		}
2294		UPGT_LOCK(sc);
2295		break;
2296	default:
2297		/* needs it to the inactive queue due to a error.  */
2298		data = STAILQ_FIRST(&sc->sc_rx_active);
2299		if (data != NULL) {
2300			STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2301			STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2302		}
2303		if (error != USB_ERR_CANCELLED) {
2304			usbd_xfer_set_stall(xfer);
2305			ifp->if_ierrors++;
2306			goto setup;
2307		}
2308		break;
2309	}
2310}
2311
2312static void
2313upgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2314{
2315	struct upgt_softc *sc = usbd_xfer_softc(xfer);
2316	struct ifnet *ifp = sc->sc_ifp;
2317	struct upgt_data *data;
2318
2319	UPGT_ASSERT_LOCKED(sc);
2320	switch (USB_GET_STATE(xfer)) {
2321	case USB_ST_TRANSFERRED:
2322		data = STAILQ_FIRST(&sc->sc_tx_active);
2323		if (data == NULL)
2324			goto setup;
2325		STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2326		UPGT_STAT_DEC(sc, st_tx_active);
2327		upgt_txeof(xfer, data);
2328		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2329		UPGT_STAT_INC(sc, st_tx_inactive);
2330		/* FALLTHROUGH */
2331	case USB_ST_SETUP:
2332setup:
2333		data = STAILQ_FIRST(&sc->sc_tx_pending);
2334		if (data == NULL) {
2335			DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n",
2336			    __func__);
2337			return;
2338		}
2339		STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2340		UPGT_STAT_DEC(sc, st_tx_pending);
2341		STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2342		UPGT_STAT_INC(sc, st_tx_active);
2343
2344		usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2345		usbd_transfer_submit(xfer);
2346		UPGT_UNLOCK(sc);
2347		upgt_start(ifp);
2348		UPGT_LOCK(sc);
2349		break;
2350	default:
2351		data = STAILQ_FIRST(&sc->sc_tx_active);
2352		if (data == NULL)
2353			goto setup;
2354		if (data->ni != NULL) {
2355			ieee80211_free_node(data->ni);
2356			data->ni = NULL;
2357			ifp->if_oerrors++;
2358		}
2359		if (error != USB_ERR_CANCELLED) {
2360			usbd_xfer_set_stall(xfer);
2361			goto setup;
2362		}
2363		break;
2364	}
2365}
2366
2367static device_method_t upgt_methods[] = {
2368        /* Device interface */
2369        DEVMETHOD(device_probe, upgt_match),
2370        DEVMETHOD(device_attach, upgt_attach),
2371        DEVMETHOD(device_detach, upgt_detach),
2372
2373	{ 0, 0 }
2374};
2375
2376static driver_t upgt_driver = {
2377        "upgt",
2378        upgt_methods,
2379        sizeof(struct upgt_softc)
2380};
2381
2382static devclass_t upgt_devclass;
2383
2384DRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0);
2385MODULE_VERSION(if_upgt, 1);
2386MODULE_DEPEND(if_upgt, usb, 1, 1, 1);
2387MODULE_DEPEND(if_upgt, wlan, 1, 1, 1);
2388MODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1);
2389