xref: /freebsd/sys/dev/usb/wlan/if_upgt.c (revision b54cfe0ae93eb56cf92d969fd76ba17e4ca36a32)
1 /*	$OpenBSD: if_upgt.c,v 1.35 2008/04/16 18:32:15 damien Exp $ */
2 /*	$FreeBSD$ */
3 
4 /*
5  * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/endian.h>
24 #include <sys/firmware.h>
25 #include <sys/linker.h>
26 #include <sys/mbuf.h>
27 #include <sys/malloc.h>
28 #include <sys/module.h>
29 #include <sys/socket.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
32 
33 #include <net/if.h>
34 #include <net/if_var.h>
35 #include <net/if_arp.h>
36 #include <net/ethernet.h>
37 #include <net/if_dl.h>
38 #include <net/if_media.h>
39 #include <net/if_types.h>
40 
41 #include <sys/bus.h>
42 #include <machine/bus.h>
43 
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_phy.h>
46 #include <net80211/ieee80211_radiotap.h>
47 #include <net80211/ieee80211_regdomain.h>
48 
49 #include <net/bpf.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 #include "usbdevs.h"
54 
55 #include <dev/usb/wlan/if_upgtvar.h>
56 
57 /*
58  * Driver for the USB PrismGT devices.
59  *
60  * For now just USB 2.0 devices with the GW3887 chipset are supported.
61  * The driver has been written based on the firmware version 2.13.1.0_LM87.
62  *
63  * TODO's:
64  * - MONITOR mode test.
65  * - Add HOSTAP mode.
66  * - Add IBSS mode.
67  * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
68  *
69  * Parts of this driver has been influenced by reading the p54u driver
70  * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
71  * Sebastien Bourdeauducq <lekernel@prism54.org>.
72  */
73 
74 static SYSCTL_NODE(_hw, OID_AUTO, upgt, CTLFLAG_RD, 0,
75     "USB PrismGT GW3887 driver parameters");
76 
77 #ifdef UPGT_DEBUG
78 int upgt_debug = 0;
79 SYSCTL_INT(_hw_upgt, OID_AUTO, debug, CTLFLAG_RWTUN, &upgt_debug,
80 	    0, "control debugging printfs");
81 enum {
82 	UPGT_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
83 	UPGT_DEBUG_RECV		= 0x00000002,	/* basic recv operation */
84 	UPGT_DEBUG_RESET	= 0x00000004,	/* reset processing */
85 	UPGT_DEBUG_INTR		= 0x00000008,	/* INTR */
86 	UPGT_DEBUG_TX_PROC	= 0x00000010,	/* tx ISR proc */
87 	UPGT_DEBUG_RX_PROC	= 0x00000020,	/* rx ISR proc */
88 	UPGT_DEBUG_STATE	= 0x00000040,	/* 802.11 state transitions */
89 	UPGT_DEBUG_STAT		= 0x00000080,	/* statistic */
90 	UPGT_DEBUG_FW		= 0x00000100,	/* firmware */
91 	UPGT_DEBUG_ANY		= 0xffffffff
92 };
93 #define	DPRINTF(sc, m, fmt, ...) do {				\
94 	if (sc->sc_debug & (m))					\
95 		printf(fmt, __VA_ARGS__);			\
96 } while (0)
97 #else
98 #define	DPRINTF(sc, m, fmt, ...) do {				\
99 	(void) sc;						\
100 } while (0)
101 #endif
102 
103 /*
104  * Prototypes.
105  */
106 static device_probe_t upgt_match;
107 static device_attach_t upgt_attach;
108 static device_detach_t upgt_detach;
109 static int	upgt_alloc_tx(struct upgt_softc *);
110 static int	upgt_alloc_rx(struct upgt_softc *);
111 static int	upgt_device_reset(struct upgt_softc *);
112 static void	upgt_bulk_tx(struct upgt_softc *, struct upgt_data *);
113 static int	upgt_fw_verify(struct upgt_softc *);
114 static int	upgt_mem_init(struct upgt_softc *);
115 static int	upgt_fw_load(struct upgt_softc *);
116 static int	upgt_fw_copy(const uint8_t *, char *, int);
117 static uint32_t	upgt_crc32_le(const void *, size_t);
118 static struct mbuf *
119 		upgt_rxeof(struct usb_xfer *, struct upgt_data *, int *);
120 static struct mbuf *
121 		upgt_rx(struct upgt_softc *, uint8_t *, int, int *);
122 static void	upgt_txeof(struct usb_xfer *, struct upgt_data *);
123 static int	upgt_eeprom_read(struct upgt_softc *);
124 static int	upgt_eeprom_parse(struct upgt_softc *);
125 static void	upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
126 static void	upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
127 static void	upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
128 static void	upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
129 static uint32_t	upgt_chksum_le(const uint32_t *, size_t);
130 static void	upgt_tx_done(struct upgt_softc *, uint8_t *);
131 static void	upgt_init(struct upgt_softc *);
132 static void	upgt_parent(struct ieee80211com *);
133 static int	upgt_transmit(struct ieee80211com *, struct mbuf *);
134 static void	upgt_start(struct upgt_softc *);
135 static int	upgt_raw_xmit(struct ieee80211_node *, struct mbuf *,
136 		    const struct ieee80211_bpf_params *);
137 static void	upgt_scan_start(struct ieee80211com *);
138 static void	upgt_scan_end(struct ieee80211com *);
139 static void	upgt_set_channel(struct ieee80211com *);
140 static struct ieee80211vap *upgt_vap_create(struct ieee80211com *,
141 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
142 		    const uint8_t [IEEE80211_ADDR_LEN],
143 		    const uint8_t [IEEE80211_ADDR_LEN]);
144 static void	upgt_vap_delete(struct ieee80211vap *);
145 static void	upgt_update_mcast(struct ieee80211com *);
146 static uint8_t	upgt_rx_rate(struct upgt_softc *, const int);
147 static void	upgt_set_multi(void *);
148 static void	upgt_stop(struct upgt_softc *);
149 static void	upgt_setup_rates(struct ieee80211vap *, struct ieee80211com *);
150 static int	upgt_set_macfilter(struct upgt_softc *, uint8_t);
151 static int	upgt_newstate(struct ieee80211vap *, enum ieee80211_state, int);
152 static void	upgt_set_chan(struct upgt_softc *, struct ieee80211_channel *);
153 static void	upgt_set_led(struct upgt_softc *, int);
154 static void	upgt_set_led_blink(void *);
155 static void	upgt_get_stats(struct upgt_softc *);
156 static void	upgt_mem_free(struct upgt_softc *, uint32_t);
157 static uint32_t	upgt_mem_alloc(struct upgt_softc *);
158 static void	upgt_free_tx(struct upgt_softc *);
159 static void	upgt_free_rx(struct upgt_softc *);
160 static void	upgt_watchdog(void *);
161 static void	upgt_abort_xfers(struct upgt_softc *);
162 static void	upgt_abort_xfers_locked(struct upgt_softc *);
163 static void	upgt_sysctl_node(struct upgt_softc *);
164 static struct upgt_data *
165 		upgt_getbuf(struct upgt_softc *);
166 static struct upgt_data *
167 		upgt_gettxbuf(struct upgt_softc *);
168 static int	upgt_tx_start(struct upgt_softc *, struct mbuf *,
169 		    struct ieee80211_node *, struct upgt_data *);
170 
171 static const char *upgt_fwname = "upgt-gw3887";
172 
173 static const STRUCT_USB_HOST_ID upgt_devs[] = {
174 #define	UPGT_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
175 	/* version 2 devices */
176 	UPGT_DEV(ACCTON,	PRISM_GT),
177 	UPGT_DEV(BELKIN,	F5D7050),
178 	UPGT_DEV(CISCOLINKSYS,	WUSB54AG),
179 	UPGT_DEV(CONCEPTRONIC,	PRISM_GT),
180 	UPGT_DEV(DELL,		PRISM_GT_1),
181 	UPGT_DEV(DELL,		PRISM_GT_2),
182 	UPGT_DEV(FSC,		E5400),
183 	UPGT_DEV(GLOBESPAN,	PRISM_GT_1),
184 	UPGT_DEV(GLOBESPAN,	PRISM_GT_2),
185 	UPGT_DEV(NETGEAR,	WG111V1_2),
186 	UPGT_DEV(INTERSIL,	PRISM_GT),
187 	UPGT_DEV(SMC,		2862WG),
188 	UPGT_DEV(USR,		USR5422),
189 	UPGT_DEV(WISTRONNEWEB,	UR045G),
190 	UPGT_DEV(XYRATEX,	PRISM_GT_1),
191 	UPGT_DEV(XYRATEX,	PRISM_GT_2),
192 	UPGT_DEV(ZCOM,		XG703A),
193 	UPGT_DEV(ZCOM,		XM142)
194 };
195 
196 static usb_callback_t upgt_bulk_rx_callback;
197 static usb_callback_t upgt_bulk_tx_callback;
198 
199 static const struct usb_config upgt_config[UPGT_N_XFERS] = {
200 	[UPGT_BULK_TX] = {
201 		.type = UE_BULK,
202 		.endpoint = UE_ADDR_ANY,
203 		.direction = UE_DIR_OUT,
204 		.bufsize = MCLBYTES * UPGT_TX_MAXCOUNT,
205 		.flags = {
206 			.force_short_xfer = 1,
207 			.pipe_bof = 1
208 		},
209 		.callback = upgt_bulk_tx_callback,
210 		.timeout = UPGT_USB_TIMEOUT,	/* ms */
211 	},
212 	[UPGT_BULK_RX] = {
213 		.type = UE_BULK,
214 		.endpoint = UE_ADDR_ANY,
215 		.direction = UE_DIR_IN,
216 		.bufsize = MCLBYTES * UPGT_RX_MAXCOUNT,
217 		.flags = {
218 			.pipe_bof = 1,
219 			.short_xfer_ok = 1
220 		},
221 		.callback = upgt_bulk_rx_callback,
222 	},
223 };
224 
225 static int
226 upgt_match(device_t dev)
227 {
228 	struct usb_attach_arg *uaa = device_get_ivars(dev);
229 
230 	if (uaa->usb_mode != USB_MODE_HOST)
231 		return (ENXIO);
232 	if (uaa->info.bConfigIndex != UPGT_CONFIG_INDEX)
233 		return (ENXIO);
234 	if (uaa->info.bIfaceIndex != UPGT_IFACE_INDEX)
235 		return (ENXIO);
236 
237 	return (usbd_lookup_id_by_uaa(upgt_devs, sizeof(upgt_devs), uaa));
238 }
239 
240 static int
241 upgt_attach(device_t dev)
242 {
243 	struct upgt_softc *sc = device_get_softc(dev);
244 	struct ieee80211com *ic = &sc->sc_ic;
245 	struct usb_attach_arg *uaa = device_get_ivars(dev);
246 	uint8_t bands, iface_index = UPGT_IFACE_INDEX;
247 	int error;
248 
249 	sc->sc_dev = dev;
250 	sc->sc_udev = uaa->device;
251 #ifdef UPGT_DEBUG
252 	sc->sc_debug = upgt_debug;
253 #endif
254 	device_set_usb_desc(dev);
255 
256 	mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
257 	    MTX_DEF);
258 	callout_init(&sc->sc_led_ch, 0);
259 	callout_init(&sc->sc_watchdog_ch, 0);
260 	mbufq_init(&sc->sc_snd, ifqmaxlen);
261 
262 	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
263 	    upgt_config, UPGT_N_XFERS, sc, &sc->sc_mtx);
264 	if (error) {
265 		device_printf(dev, "could not allocate USB transfers, "
266 		    "err=%s\n", usbd_errstr(error));
267 		goto fail1;
268 	}
269 
270 	sc->sc_rx_dma_buf = usbd_xfer_get_frame_buffer(
271 	    sc->sc_xfer[UPGT_BULK_RX], 0);
272 	sc->sc_tx_dma_buf = usbd_xfer_get_frame_buffer(
273 	    sc->sc_xfer[UPGT_BULK_TX], 0);
274 
275 	/* Setup TX and RX buffers */
276 	error = upgt_alloc_tx(sc);
277 	if (error)
278 		goto fail2;
279 	error = upgt_alloc_rx(sc);
280 	if (error)
281 		goto fail3;
282 
283 	/* Initialize the device.  */
284 	error = upgt_device_reset(sc);
285 	if (error)
286 		goto fail4;
287 	/* Verify the firmware.  */
288 	error = upgt_fw_verify(sc);
289 	if (error)
290 		goto fail4;
291 	/* Calculate device memory space.  */
292 	if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
293 		device_printf(dev,
294 		    "could not find memory space addresses on FW\n");
295 		error = EIO;
296 		goto fail4;
297 	}
298 	sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
299 	sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
300 
301 	DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame start=0x%08x\n",
302 	    sc->sc_memaddr_frame_start);
303 	DPRINTF(sc, UPGT_DEBUG_FW, "memory address frame end=0x%08x\n",
304 	    sc->sc_memaddr_frame_end);
305 	DPRINTF(sc, UPGT_DEBUG_FW, "memory address rx start=0x%08x\n",
306 	    sc->sc_memaddr_rx_start);
307 
308 	upgt_mem_init(sc);
309 
310 	/* Load the firmware.  */
311 	error = upgt_fw_load(sc);
312 	if (error)
313 		goto fail4;
314 
315 	/* Read the whole EEPROM content and parse it.  */
316 	error = upgt_eeprom_read(sc);
317 	if (error)
318 		goto fail4;
319 	error = upgt_eeprom_parse(sc);
320 	if (error)
321 		goto fail4;
322 
323 	/* all works related with the device have done here. */
324 	upgt_abort_xfers(sc);
325 
326 	ic->ic_softc = sc;
327 	ic->ic_name = device_get_nameunit(dev);
328 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
329 	ic->ic_opmode = IEEE80211_M_STA;
330 	/* set device capabilities */
331 	ic->ic_caps =
332 		  IEEE80211_C_STA		/* station mode */
333 		| IEEE80211_C_MONITOR		/* monitor mode */
334 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
335 	        | IEEE80211_C_SHSLOT		/* short slot time supported */
336 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
337 	        | IEEE80211_C_WPA		/* 802.11i */
338 		;
339 
340 	bands = 0;
341 	setbit(&bands, IEEE80211_MODE_11B);
342 	setbit(&bands, IEEE80211_MODE_11G);
343 	ieee80211_init_channels(ic, NULL, &bands);
344 
345 	ieee80211_ifattach(ic);
346 	ic->ic_raw_xmit = upgt_raw_xmit;
347 	ic->ic_scan_start = upgt_scan_start;
348 	ic->ic_scan_end = upgt_scan_end;
349 	ic->ic_set_channel = upgt_set_channel;
350 	ic->ic_vap_create = upgt_vap_create;
351 	ic->ic_vap_delete = upgt_vap_delete;
352 	ic->ic_update_mcast = upgt_update_mcast;
353 	ic->ic_transmit = upgt_transmit;
354 	ic->ic_parent = upgt_parent;
355 
356 	ieee80211_radiotap_attach(ic,
357 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
358 		UPGT_TX_RADIOTAP_PRESENT,
359 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
360 		UPGT_RX_RADIOTAP_PRESENT);
361 
362 	upgt_sysctl_node(sc);
363 
364 	if (bootverbose)
365 		ieee80211_announce(ic);
366 
367 	return (0);
368 
369 fail4:	upgt_free_rx(sc);
370 fail3:	upgt_free_tx(sc);
371 fail2:	usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
372 fail1:	mtx_destroy(&sc->sc_mtx);
373 
374 	return (error);
375 }
376 
377 static void
378 upgt_txeof(struct usb_xfer *xfer, struct upgt_data *data)
379 {
380 
381 	if (data->m) {
382 		/* XXX status? */
383 		ieee80211_tx_complete(data->ni, data->m, 0);
384 		data->m = NULL;
385 		data->ni = NULL;
386 	}
387 }
388 
389 static void
390 upgt_get_stats(struct upgt_softc *sc)
391 {
392 	struct upgt_data *data_cmd;
393 	struct upgt_lmac_mem *mem;
394 	struct upgt_lmac_stats *stats;
395 
396 	data_cmd = upgt_getbuf(sc);
397 	if (data_cmd == NULL) {
398 		device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
399 		return;
400 	}
401 
402 	/*
403 	 * Transmit the URB containing the CMD data.
404 	 */
405 	memset(data_cmd->buf, 0, MCLBYTES);
406 
407 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
408 	mem->addr = htole32(sc->sc_memaddr_frame_start +
409 	    UPGT_MEMSIZE_FRAME_HEAD);
410 
411 	stats = (struct upgt_lmac_stats *)(mem + 1);
412 
413 	stats->header1.flags = 0;
414 	stats->header1.type = UPGT_H1_TYPE_CTRL;
415 	stats->header1.len = htole16(
416 	    sizeof(struct upgt_lmac_stats) - sizeof(struct upgt_lmac_header));
417 
418 	stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
419 	stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
420 	stats->header2.flags = 0;
421 
422 	data_cmd->buflen = sizeof(*mem) + sizeof(*stats);
423 
424 	mem->chksum = upgt_chksum_le((uint32_t *)stats,
425 	    data_cmd->buflen - sizeof(*mem));
426 
427 	upgt_bulk_tx(sc, data_cmd);
428 }
429 
430 static void
431 upgt_parent(struct ieee80211com *ic)
432 {
433 	struct upgt_softc *sc = ic->ic_softc;
434 	int startall = 0;
435 
436 	UPGT_LOCK(sc);
437 	if (sc->sc_flags & UPGT_FLAG_DETACHED) {
438 		UPGT_UNLOCK(sc);
439 		return;
440 	}
441 	if (ic->ic_nrunning > 0) {
442 		if (sc->sc_flags & UPGT_FLAG_INITDONE) {
443 			if (ic->ic_allmulti > 0 || ic->ic_promisc > 0)
444 				upgt_set_multi(sc);
445 		} else {
446 			upgt_init(sc);
447 			startall = 1;
448 		}
449 	} else if (sc->sc_flags & UPGT_FLAG_INITDONE)
450 		upgt_stop(sc);
451 	UPGT_UNLOCK(sc);
452 	if (startall)
453 		ieee80211_start_all(ic);
454 }
455 
456 static void
457 upgt_stop(struct upgt_softc *sc)
458 {
459 
460 	UPGT_ASSERT_LOCKED(sc);
461 
462 	if (sc->sc_flags & UPGT_FLAG_INITDONE)
463 		upgt_set_macfilter(sc, IEEE80211_S_INIT);
464 	upgt_abort_xfers_locked(sc);
465 	/* device down */
466 	sc->sc_tx_timer = 0;
467 	sc->sc_flags &= ~UPGT_FLAG_INITDONE;
468 }
469 
470 static void
471 upgt_set_led(struct upgt_softc *sc, int action)
472 {
473 	struct upgt_data *data_cmd;
474 	struct upgt_lmac_mem *mem;
475 	struct upgt_lmac_led *led;
476 
477 	data_cmd = upgt_getbuf(sc);
478 	if (data_cmd == NULL) {
479 		device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
480 		return;
481 	}
482 
483 	/*
484 	 * Transmit the URB containing the CMD data.
485 	 */
486 	memset(data_cmd->buf, 0, MCLBYTES);
487 
488 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
489 	mem->addr = htole32(sc->sc_memaddr_frame_start +
490 	    UPGT_MEMSIZE_FRAME_HEAD);
491 
492 	led = (struct upgt_lmac_led *)(mem + 1);
493 
494 	led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
495 	led->header1.type = UPGT_H1_TYPE_CTRL;
496 	led->header1.len = htole16(
497 	    sizeof(struct upgt_lmac_led) -
498 	    sizeof(struct upgt_lmac_header));
499 
500 	led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
501 	led->header2.type = htole16(UPGT_H2_TYPE_LED);
502 	led->header2.flags = 0;
503 
504 	switch (action) {
505 	case UPGT_LED_OFF:
506 		led->mode = htole16(UPGT_LED_MODE_SET);
507 		led->action_fix = 0;
508 		led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
509 		led->action_tmp_dur = 0;
510 		break;
511 	case UPGT_LED_ON:
512 		led->mode = htole16(UPGT_LED_MODE_SET);
513 		led->action_fix = 0;
514 		led->action_tmp = htole16(UPGT_LED_ACTION_ON);
515 		led->action_tmp_dur = 0;
516 		break;
517 	case UPGT_LED_BLINK:
518 		if (sc->sc_state != IEEE80211_S_RUN) {
519 			STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
520 			return;
521 		}
522 		if (sc->sc_led_blink) {
523 			/* previous blink was not finished */
524 			STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
525 			return;
526 		}
527 		led->mode = htole16(UPGT_LED_MODE_SET);
528 		led->action_fix = htole16(UPGT_LED_ACTION_OFF);
529 		led->action_tmp = htole16(UPGT_LED_ACTION_ON);
530 		led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
531 		/* lock blink */
532 		sc->sc_led_blink = 1;
533 		callout_reset(&sc->sc_led_ch, hz, upgt_set_led_blink, sc);
534 		break;
535 	default:
536 		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data_cmd, next);
537 		return;
538 	}
539 
540 	data_cmd->buflen = sizeof(*mem) + sizeof(*led);
541 
542 	mem->chksum = upgt_chksum_le((uint32_t *)led,
543 	    data_cmd->buflen - sizeof(*mem));
544 
545 	upgt_bulk_tx(sc, data_cmd);
546 }
547 
548 static void
549 upgt_set_led_blink(void *arg)
550 {
551 	struct upgt_softc *sc = arg;
552 
553 	/* blink finished, we are ready for a next one */
554 	sc->sc_led_blink = 0;
555 }
556 
557 static void
558 upgt_init(struct upgt_softc *sc)
559 {
560 
561 	UPGT_ASSERT_LOCKED(sc);
562 
563 	if (sc->sc_flags & UPGT_FLAG_INITDONE)
564 		upgt_stop(sc);
565 
566 	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
567 
568 	(void)upgt_set_macfilter(sc, IEEE80211_S_SCAN);
569 
570 	sc->sc_flags |= UPGT_FLAG_INITDONE;
571 
572 	callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
573 }
574 
575 static int
576 upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
577 {
578 	struct ieee80211com *ic = &sc->sc_ic;
579 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
580 	struct ieee80211_node *ni;
581 	struct upgt_data *data_cmd;
582 	struct upgt_lmac_mem *mem;
583 	struct upgt_lmac_filter *filter;
584 
585 	UPGT_ASSERT_LOCKED(sc);
586 
587 	data_cmd = upgt_getbuf(sc);
588 	if (data_cmd == NULL) {
589 		device_printf(sc->sc_dev, "out of TX buffers.\n");
590 		return (ENOBUFS);
591 	}
592 
593 	/*
594 	 * Transmit the URB containing the CMD data.
595 	 */
596 	memset(data_cmd->buf, 0, MCLBYTES);
597 
598 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
599 	mem->addr = htole32(sc->sc_memaddr_frame_start +
600 	    UPGT_MEMSIZE_FRAME_HEAD);
601 
602 	filter = (struct upgt_lmac_filter *)(mem + 1);
603 
604 	filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
605 	filter->header1.type = UPGT_H1_TYPE_CTRL;
606 	filter->header1.len = htole16(
607 	    sizeof(struct upgt_lmac_filter) -
608 	    sizeof(struct upgt_lmac_header));
609 
610 	filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
611 	filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
612 	filter->header2.flags = 0;
613 
614 	switch (state) {
615 	case IEEE80211_S_INIT:
616 		DPRINTF(sc, UPGT_DEBUG_STATE, "%s: set MAC filter to INIT\n",
617 		    __func__);
618 		filter->type = htole16(UPGT_FILTER_TYPE_RESET);
619 		break;
620 	case IEEE80211_S_SCAN:
621 		DPRINTF(sc, UPGT_DEBUG_STATE,
622 		    "set MAC filter to SCAN (bssid %s)\n",
623 		    ether_sprintf(ieee80211broadcastaddr));
624 		filter->type = htole16(UPGT_FILTER_TYPE_NONE);
625 		IEEE80211_ADDR_COPY(filter->dst,
626 		    vap ? vap->iv_myaddr : ic->ic_macaddr);
627 		IEEE80211_ADDR_COPY(filter->src, ieee80211broadcastaddr);
628 		filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
629 		filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
630 		filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
631 		filter->rxhw = htole32(sc->sc_eeprom_hwrx);
632 		filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
633 		break;
634 	case IEEE80211_S_RUN:
635 		ni = ieee80211_ref_node(vap->iv_bss);
636 		/* XXX monitor mode isn't tested yet.  */
637 		if (vap->iv_opmode == IEEE80211_M_MONITOR) {
638 			filter->type = htole16(UPGT_FILTER_TYPE_MONITOR);
639 			IEEE80211_ADDR_COPY(filter->dst,
640 			    vap ? vap->iv_myaddr : ic->ic_macaddr);
641 			IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
642 			filter->unknown1 = htole16(UPGT_FILTER_MONITOR_UNKNOWN1);
643 			filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
644 			filter->unknown2 = htole16(UPGT_FILTER_MONITOR_UNKNOWN2);
645 			filter->rxhw = htole32(sc->sc_eeprom_hwrx);
646 			filter->unknown3 = htole16(UPGT_FILTER_MONITOR_UNKNOWN3);
647 		} else {
648 			DPRINTF(sc, UPGT_DEBUG_STATE,
649 			    "set MAC filter to RUN (bssid %s)\n",
650 			    ether_sprintf(ni->ni_bssid));
651 			filter->type = htole16(UPGT_FILTER_TYPE_STA);
652 			IEEE80211_ADDR_COPY(filter->dst,
653 			    vap ? vap->iv_myaddr : ic->ic_macaddr);
654 			IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
655 			filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
656 			filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
657 			filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
658 			filter->rxhw = htole32(sc->sc_eeprom_hwrx);
659 			filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
660 		}
661 		ieee80211_free_node(ni);
662 		break;
663 	default:
664 		device_printf(sc->sc_dev,
665 		    "MAC filter does not know that state\n");
666 		break;
667 	}
668 
669 	data_cmd->buflen = sizeof(*mem) + sizeof(*filter);
670 
671 	mem->chksum = upgt_chksum_le((uint32_t *)filter,
672 	    data_cmd->buflen - sizeof(*mem));
673 
674 	upgt_bulk_tx(sc, data_cmd);
675 
676 	return (0);
677 }
678 
679 static void
680 upgt_setup_rates(struct ieee80211vap *vap, struct ieee80211com *ic)
681 {
682 	struct upgt_softc *sc = ic->ic_softc;
683 	const struct ieee80211_txparam *tp;
684 
685 	/*
686 	 * 0x01 = OFMD6   0x10 = DS1
687 	 * 0x04 = OFDM9   0x11 = DS2
688 	 * 0x06 = OFDM12  0x12 = DS5
689 	 * 0x07 = OFDM18  0x13 = DS11
690 	 * 0x08 = OFDM24
691 	 * 0x09 = OFDM36
692 	 * 0x0a = OFDM48
693 	 * 0x0b = OFDM54
694 	 */
695 	const uint8_t rateset_auto_11b[] =
696 	    { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 };
697 	const uint8_t rateset_auto_11g[] =
698 	    { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 };
699 	const uint8_t rateset_fix_11bg[] =
700 	    { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07,
701 	      0x08, 0x09, 0x0a, 0x0b };
702 
703 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
704 
705 	/* XXX */
706 	if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
707 		/*
708 		 * Automatic rate control is done by the device.
709 		 * We just pass the rateset from which the device
710 		 * will pickup a rate.
711 		 */
712 		if (ic->ic_curmode == IEEE80211_MODE_11B)
713 			memcpy(sc->sc_cur_rateset, rateset_auto_11b,
714 			    sizeof(sc->sc_cur_rateset));
715 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
716 		    ic->ic_curmode == IEEE80211_MODE_AUTO)
717 			memcpy(sc->sc_cur_rateset, rateset_auto_11g,
718 			    sizeof(sc->sc_cur_rateset));
719 	} else {
720 		/* set a fixed rate */
721 		memset(sc->sc_cur_rateset, rateset_fix_11bg[tp->ucastrate],
722 		    sizeof(sc->sc_cur_rateset));
723 	}
724 }
725 
726 static void
727 upgt_set_multi(void *arg)
728 {
729 
730 	/* XXX don't know how to set a device.  Lack of docs. */
731 }
732 
733 static int
734 upgt_transmit(struct ieee80211com *ic, struct mbuf *m)
735 {
736 	struct upgt_softc *sc = ic->ic_softc;
737 	int error;
738 
739 	UPGT_LOCK(sc);
740 	if ((sc->sc_flags & UPGT_FLAG_INITDONE) == 0) {
741 		UPGT_UNLOCK(sc);
742 		return (ENXIO);
743 	}
744 	error = mbufq_enqueue(&sc->sc_snd, m);
745 	if (error) {
746 		UPGT_UNLOCK(sc);
747 		return (error);
748 	}
749 	upgt_start(sc);
750 	UPGT_UNLOCK(sc);
751 
752 	return (0);
753 }
754 
755 static void
756 upgt_start(struct upgt_softc *sc)
757 {
758 	struct upgt_data *data_tx;
759 	struct ieee80211_node *ni;
760 	struct mbuf *m;
761 
762 	UPGT_ASSERT_LOCKED(sc);
763 
764 	if ((sc->sc_flags & UPGT_FLAG_INITDONE) == 0)
765 		return;
766 
767 	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
768 		data_tx = upgt_gettxbuf(sc);
769 		if (data_tx == NULL) {
770 			mbufq_prepend(&sc->sc_snd, m);
771 			break;
772 		}
773 
774 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
775 		m->m_pkthdr.rcvif = NULL;
776 
777 		if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
778 			if_inc_counter(ni->ni_vap->iv_ifp,
779 			    IFCOUNTER_OERRORS, 1);
780 			STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
781 			UPGT_STAT_INC(sc, st_tx_inactive);
782 			ieee80211_free_node(ni);
783 			continue;
784 		}
785 		sc->sc_tx_timer = 5;
786 	}
787 }
788 
789 static int
790 upgt_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
791 	const struct ieee80211_bpf_params *params)
792 {
793 	struct ieee80211com *ic = ni->ni_ic;
794 	struct upgt_softc *sc = ic->ic_softc;
795 	struct upgt_data *data_tx = NULL;
796 
797 	UPGT_LOCK(sc);
798 	/* prevent management frames from being sent if we're not ready */
799 	if (!(sc->sc_flags & UPGT_FLAG_INITDONE)) {
800 		m_freem(m);
801 		UPGT_UNLOCK(sc);
802 		return ENETDOWN;
803 	}
804 
805 	data_tx = upgt_gettxbuf(sc);
806 	if (data_tx == NULL) {
807 		m_freem(m);
808 		UPGT_UNLOCK(sc);
809 		return (ENOBUFS);
810 	}
811 
812 	if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
813 		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
814 		UPGT_STAT_INC(sc, st_tx_inactive);
815 		UPGT_UNLOCK(sc);
816 		return (EIO);
817 	}
818 	UPGT_UNLOCK(sc);
819 
820 	sc->sc_tx_timer = 5;
821 	return (0);
822 }
823 
824 static void
825 upgt_watchdog(void *arg)
826 {
827 	struct upgt_softc *sc = arg;
828 	struct ieee80211com *ic = &sc->sc_ic;
829 
830 	if (sc->sc_tx_timer > 0) {
831 		if (--sc->sc_tx_timer == 0) {
832 			device_printf(sc->sc_dev, "watchdog timeout\n");
833 			/* upgt_init(sc); XXX needs a process context ? */
834 			counter_u64_add(ic->ic_oerrors, 1);
835 			return;
836 		}
837 		callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
838 	}
839 }
840 
841 static uint32_t
842 upgt_mem_alloc(struct upgt_softc *sc)
843 {
844 	int i;
845 
846 	for (i = 0; i < sc->sc_memory.pages; i++) {
847 		if (sc->sc_memory.page[i].used == 0) {
848 			sc->sc_memory.page[i].used = 1;
849 			return (sc->sc_memory.page[i].addr);
850 		}
851 	}
852 
853 	return (0);
854 }
855 
856 static void
857 upgt_scan_start(struct ieee80211com *ic)
858 {
859 	/* do nothing.  */
860 }
861 
862 static void
863 upgt_scan_end(struct ieee80211com *ic)
864 {
865 	/* do nothing.  */
866 }
867 
868 static void
869 upgt_set_channel(struct ieee80211com *ic)
870 {
871 	struct upgt_softc *sc = ic->ic_softc;
872 
873 	UPGT_LOCK(sc);
874 	upgt_set_chan(sc, ic->ic_curchan);
875 	UPGT_UNLOCK(sc);
876 }
877 
878 static void
879 upgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c)
880 {
881 	struct ieee80211com *ic = &sc->sc_ic;
882 	struct upgt_data *data_cmd;
883 	struct upgt_lmac_mem *mem;
884 	struct upgt_lmac_channel *chan;
885 	int channel;
886 
887 	UPGT_ASSERT_LOCKED(sc);
888 
889 	channel = ieee80211_chan2ieee(ic, c);
890 	if (channel == 0 || channel == IEEE80211_CHAN_ANY) {
891 		/* XXX should NEVER happen */
892 		device_printf(sc->sc_dev,
893 		    "%s: invalid channel %x\n", __func__, channel);
894 		return;
895 	}
896 
897 	DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel);
898 
899 	data_cmd = upgt_getbuf(sc);
900 	if (data_cmd == NULL) {
901 		device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
902 		return;
903 	}
904 	/*
905 	 * Transmit the URB containing the CMD data.
906 	 */
907 	memset(data_cmd->buf, 0, MCLBYTES);
908 
909 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
910 	mem->addr = htole32(sc->sc_memaddr_frame_start +
911 	    UPGT_MEMSIZE_FRAME_HEAD);
912 
913 	chan = (struct upgt_lmac_channel *)(mem + 1);
914 
915 	chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
916 	chan->header1.type = UPGT_H1_TYPE_CTRL;
917 	chan->header1.len = htole16(
918 	    sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header));
919 
920 	chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
921 	chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
922 	chan->header2.flags = 0;
923 
924 	chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
925 	chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
926 	chan->freq6 = sc->sc_eeprom_freq6[channel];
927 	chan->settings = sc->sc_eeprom_freq6_settings;
928 	chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
929 
930 	memcpy(chan->freq3_1, &sc->sc_eeprom_freq3[channel].data,
931 	    sizeof(chan->freq3_1));
932 	memcpy(chan->freq4, &sc->sc_eeprom_freq4[channel],
933 	    sizeof(sc->sc_eeprom_freq4[channel]));
934 	memcpy(chan->freq3_2, &sc->sc_eeprom_freq3[channel].data,
935 	    sizeof(chan->freq3_2));
936 
937 	data_cmd->buflen = sizeof(*mem) + sizeof(*chan);
938 
939 	mem->chksum = upgt_chksum_le((uint32_t *)chan,
940 	    data_cmd->buflen - sizeof(*mem));
941 
942 	upgt_bulk_tx(sc, data_cmd);
943 }
944 
945 static struct ieee80211vap *
946 upgt_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
947     enum ieee80211_opmode opmode, int flags,
948     const uint8_t bssid[IEEE80211_ADDR_LEN],
949     const uint8_t mac[IEEE80211_ADDR_LEN])
950 {
951 	struct upgt_vap *uvp;
952 	struct ieee80211vap *vap;
953 
954 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
955 		return NULL;
956 	uvp = malloc(sizeof(struct upgt_vap), M_80211_VAP, M_WAITOK | M_ZERO);
957 	vap = &uvp->vap;
958 	/* enable s/w bmiss handling for sta mode */
959 
960 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
961 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
962 		/* out of memory */
963 		free(uvp, M_80211_VAP);
964 		return (NULL);
965 	}
966 
967 	/* override state transition machine */
968 	uvp->newstate = vap->iv_newstate;
969 	vap->iv_newstate = upgt_newstate;
970 
971 	/* setup device rates */
972 	upgt_setup_rates(vap, ic);
973 
974 	/* complete setup */
975 	ieee80211_vap_attach(vap, ieee80211_media_change,
976 	    ieee80211_media_status, mac);
977 	ic->ic_opmode = opmode;
978 	return vap;
979 }
980 
981 static int
982 upgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
983 {
984 	struct upgt_vap *uvp = UPGT_VAP(vap);
985 	struct ieee80211com *ic = vap->iv_ic;
986 	struct upgt_softc *sc = ic->ic_softc;
987 
988 	/* do it in a process context */
989 	sc->sc_state = nstate;
990 
991 	IEEE80211_UNLOCK(ic);
992 	UPGT_LOCK(sc);
993 	callout_stop(&sc->sc_led_ch);
994 	callout_stop(&sc->sc_watchdog_ch);
995 
996 	switch (nstate) {
997 	case IEEE80211_S_INIT:
998 		/* do not accept any frames if the device is down */
999 		(void)upgt_set_macfilter(sc, sc->sc_state);
1000 		upgt_set_led(sc, UPGT_LED_OFF);
1001 		break;
1002 	case IEEE80211_S_SCAN:
1003 		upgt_set_chan(sc, ic->ic_curchan);
1004 		break;
1005 	case IEEE80211_S_AUTH:
1006 		upgt_set_chan(sc, ic->ic_curchan);
1007 		break;
1008 	case IEEE80211_S_ASSOC:
1009 		break;
1010 	case IEEE80211_S_RUN:
1011 		upgt_set_macfilter(sc, sc->sc_state);
1012 		upgt_set_led(sc, UPGT_LED_ON);
1013 		break;
1014 	default:
1015 		break;
1016 	}
1017 	UPGT_UNLOCK(sc);
1018 	IEEE80211_LOCK(ic);
1019 	return (uvp->newstate(vap, nstate, arg));
1020 }
1021 
1022 static void
1023 upgt_vap_delete(struct ieee80211vap *vap)
1024 {
1025 	struct upgt_vap *uvp = UPGT_VAP(vap);
1026 
1027 	ieee80211_vap_detach(vap);
1028 	free(uvp, M_80211_VAP);
1029 }
1030 
1031 static void
1032 upgt_update_mcast(struct ieee80211com *ic)
1033 {
1034 	struct upgt_softc *sc = ic->ic_softc;
1035 
1036 	upgt_set_multi(sc);
1037 }
1038 
1039 static int
1040 upgt_eeprom_parse(struct upgt_softc *sc)
1041 {
1042 	struct ieee80211com *ic = &sc->sc_ic;
1043 	struct upgt_eeprom_header *eeprom_header;
1044 	struct upgt_eeprom_option *eeprom_option;
1045 	uint16_t option_len;
1046 	uint16_t option_type;
1047 	uint16_t preamble_len;
1048 	int option_end = 0;
1049 
1050 	/* calculate eeprom options start offset */
1051 	eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
1052 	preamble_len = le16toh(eeprom_header->preamble_len);
1053 	eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
1054 	    (sizeof(struct upgt_eeprom_header) + preamble_len));
1055 
1056 	while (!option_end) {
1057 
1058 		/* sanity check */
1059 		if (eeprom_option >= (struct upgt_eeprom_option *)
1060 		    (sc->sc_eeprom + UPGT_EEPROM_SIZE)) {
1061 			return (EINVAL);
1062 		}
1063 
1064 		/* the eeprom option length is stored in words */
1065 		option_len =
1066 		    (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t);
1067 		option_type =
1068 		    le16toh(eeprom_option->type);
1069 
1070 		/* sanity check */
1071 		if (option_len == 0 || option_len >= UPGT_EEPROM_SIZE)
1072 			return (EINVAL);
1073 
1074 		switch (option_type) {
1075 		case UPGT_EEPROM_TYPE_NAME:
1076 			DPRINTF(sc, UPGT_DEBUG_FW,
1077 			    "EEPROM name len=%d\n", option_len);
1078 			break;
1079 		case UPGT_EEPROM_TYPE_SERIAL:
1080 			DPRINTF(sc, UPGT_DEBUG_FW,
1081 			    "EEPROM serial len=%d\n", option_len);
1082 			break;
1083 		case UPGT_EEPROM_TYPE_MAC:
1084 			DPRINTF(sc, UPGT_DEBUG_FW,
1085 			    "EEPROM mac len=%d\n", option_len);
1086 
1087 			IEEE80211_ADDR_COPY(ic->ic_macaddr,
1088 			    eeprom_option->data);
1089 			break;
1090 		case UPGT_EEPROM_TYPE_HWRX:
1091 			DPRINTF(sc, UPGT_DEBUG_FW,
1092 			    "EEPROM hwrx len=%d\n", option_len);
1093 
1094 			upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1095 			break;
1096 		case UPGT_EEPROM_TYPE_CHIP:
1097 			DPRINTF(sc, UPGT_DEBUG_FW,
1098 			    "EEPROM chip len=%d\n", option_len);
1099 			break;
1100 		case UPGT_EEPROM_TYPE_FREQ3:
1101 			DPRINTF(sc, UPGT_DEBUG_FW,
1102 			    "EEPROM freq3 len=%d\n", option_len);
1103 
1104 			upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1105 			    option_len);
1106 			break;
1107 		case UPGT_EEPROM_TYPE_FREQ4:
1108 			DPRINTF(sc, UPGT_DEBUG_FW,
1109 			    "EEPROM freq4 len=%d\n", option_len);
1110 
1111 			upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1112 			    option_len);
1113 			break;
1114 		case UPGT_EEPROM_TYPE_FREQ5:
1115 			DPRINTF(sc, UPGT_DEBUG_FW,
1116 			    "EEPROM freq5 len=%d\n", option_len);
1117 			break;
1118 		case UPGT_EEPROM_TYPE_FREQ6:
1119 			DPRINTF(sc, UPGT_DEBUG_FW,
1120 			    "EEPROM freq6 len=%d\n", option_len);
1121 
1122 			upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1123 			    option_len);
1124 			break;
1125 		case UPGT_EEPROM_TYPE_END:
1126 			DPRINTF(sc, UPGT_DEBUG_FW,
1127 			    "EEPROM end len=%d\n", option_len);
1128 			option_end = 1;
1129 			break;
1130 		case UPGT_EEPROM_TYPE_OFF:
1131 			DPRINTF(sc, UPGT_DEBUG_FW,
1132 			    "%s: EEPROM off without end option\n", __func__);
1133 			return (EIO);
1134 		default:
1135 			DPRINTF(sc, UPGT_DEBUG_FW,
1136 			    "EEPROM unknown type 0x%04x len=%d\n",
1137 			    option_type, option_len);
1138 			break;
1139 		}
1140 
1141 		/* jump to next EEPROM option */
1142 		eeprom_option = (struct upgt_eeprom_option *)
1143 		    (eeprom_option->data + option_len);
1144 	}
1145 	return (0);
1146 }
1147 
1148 static void
1149 upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1150 {
1151 	struct upgt_eeprom_freq3_header *freq3_header;
1152 	struct upgt_lmac_freq3 *freq3;
1153 	int i;
1154 	int elements;
1155 	int flags;
1156 	unsigned channel;
1157 
1158 	freq3_header = (struct upgt_eeprom_freq3_header *)data;
1159 	freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1160 
1161 	flags = freq3_header->flags;
1162 	elements = freq3_header->elements;
1163 
1164 	DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n",
1165 	    flags, elements);
1166 
1167 	if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq3[0])))
1168 		return;
1169 
1170 	for (i = 0; i < elements; i++) {
1171 		channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0);
1172 		if (channel >= IEEE80211_CHAN_MAX)
1173 			continue;
1174 
1175 		sc->sc_eeprom_freq3[channel] = freq3[i];
1176 
1177 		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1178 		    le16toh(sc->sc_eeprom_freq3[channel].freq), channel);
1179 	}
1180 }
1181 
1182 void
1183 upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1184 {
1185 	struct upgt_eeprom_freq4_header *freq4_header;
1186 	struct upgt_eeprom_freq4_1 *freq4_1;
1187 	struct upgt_eeprom_freq4_2 *freq4_2;
1188 	int i;
1189 	int j;
1190 	int elements;
1191 	int settings;
1192 	int flags;
1193 	unsigned channel;
1194 
1195 	freq4_header = (struct upgt_eeprom_freq4_header *)data;
1196 	freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1197 	flags = freq4_header->flags;
1198 	elements = freq4_header->elements;
1199 	settings = freq4_header->settings;
1200 
1201 	/* we need this value later */
1202 	sc->sc_eeprom_freq6_settings = freq4_header->settings;
1203 
1204 	DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n",
1205 	    flags, elements, settings);
1206 
1207 	if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq4_1[0])))
1208 		return;
1209 
1210 	for (i = 0; i < elements; i++) {
1211 		channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0);
1212 		if (channel >= IEEE80211_CHAN_MAX)
1213 			continue;
1214 
1215 		freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1216 		for (j = 0; j < settings; j++) {
1217 			sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1218 			sc->sc_eeprom_freq4[channel][j].pad = 0;
1219 		}
1220 
1221 		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1222 		    le16toh(freq4_1[i].freq), channel);
1223 	}
1224 }
1225 
1226 void
1227 upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1228 {
1229 	struct upgt_lmac_freq6 *freq6;
1230 	int i;
1231 	int elements;
1232 	unsigned channel;
1233 
1234 	freq6 = (struct upgt_lmac_freq6 *)data;
1235 	elements = len / sizeof(struct upgt_lmac_freq6);
1236 
1237 	DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements);
1238 
1239 	if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq6[0])))
1240 		return;
1241 
1242 	for (i = 0; i < elements; i++) {
1243 		channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0);
1244 		if (channel >= IEEE80211_CHAN_MAX)
1245 			continue;
1246 
1247 		sc->sc_eeprom_freq6[channel] = freq6[i];
1248 
1249 		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1250 		    le16toh(sc->sc_eeprom_freq6[channel].freq), channel);
1251 	}
1252 }
1253 
1254 static void
1255 upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1256 {
1257 	struct upgt_eeprom_option_hwrx *option_hwrx;
1258 
1259 	option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1260 
1261 	sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1262 
1263 	DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n",
1264 	    sc->sc_eeprom_hwrx);
1265 }
1266 
1267 static int
1268 upgt_eeprom_read(struct upgt_softc *sc)
1269 {
1270 	struct upgt_data *data_cmd;
1271 	struct upgt_lmac_mem *mem;
1272 	struct upgt_lmac_eeprom	*eeprom;
1273 	int block, error, offset;
1274 
1275 	UPGT_LOCK(sc);
1276 	usb_pause_mtx(&sc->sc_mtx, 100);
1277 
1278 	offset = 0;
1279 	block = UPGT_EEPROM_BLOCK_SIZE;
1280 	while (offset < UPGT_EEPROM_SIZE) {
1281 		DPRINTF(sc, UPGT_DEBUG_FW,
1282 		    "request EEPROM block (offset=%d, len=%d)\n", offset, block);
1283 
1284 		data_cmd = upgt_getbuf(sc);
1285 		if (data_cmd == NULL) {
1286 			UPGT_UNLOCK(sc);
1287 			return (ENOBUFS);
1288 		}
1289 
1290 		/*
1291 		 * Transmit the URB containing the CMD data.
1292 		 */
1293 		memset(data_cmd->buf, 0, MCLBYTES);
1294 
1295 		mem = (struct upgt_lmac_mem *)data_cmd->buf;
1296 		mem->addr = htole32(sc->sc_memaddr_frame_start +
1297 		    UPGT_MEMSIZE_FRAME_HEAD);
1298 
1299 		eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
1300 		eeprom->header1.flags = 0;
1301 		eeprom->header1.type = UPGT_H1_TYPE_CTRL;
1302 		eeprom->header1.len = htole16((
1303 		    sizeof(struct upgt_lmac_eeprom) -
1304 		    sizeof(struct upgt_lmac_header)) + block);
1305 
1306 		eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1307 		eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
1308 		eeprom->header2.flags = 0;
1309 
1310 		eeprom->offset = htole16(offset);
1311 		eeprom->len = htole16(block);
1312 
1313 		data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block;
1314 
1315 		mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
1316 		    data_cmd->buflen - sizeof(*mem));
1317 		upgt_bulk_tx(sc, data_cmd);
1318 
1319 		error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz);
1320 		if (error != 0) {
1321 			device_printf(sc->sc_dev,
1322 			    "timeout while waiting for EEPROM data\n");
1323 			UPGT_UNLOCK(sc);
1324 			return (EIO);
1325 		}
1326 
1327 		offset += block;
1328 		if (UPGT_EEPROM_SIZE - offset < block)
1329 			block = UPGT_EEPROM_SIZE - offset;
1330 	}
1331 
1332 	UPGT_UNLOCK(sc);
1333 	return (0);
1334 }
1335 
1336 /*
1337  * When a rx data came in the function returns a mbuf and a rssi values.
1338  */
1339 static struct mbuf *
1340 upgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi)
1341 {
1342 	struct mbuf *m = NULL;
1343 	struct upgt_softc *sc = usbd_xfer_softc(xfer);
1344 	struct upgt_lmac_header *header;
1345 	struct upgt_lmac_eeprom *eeprom;
1346 	uint8_t h1_type;
1347 	uint16_t h2_type;
1348 	int actlen, sumlen;
1349 
1350 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1351 
1352 	UPGT_ASSERT_LOCKED(sc);
1353 
1354 	if (actlen < 1)
1355 		return (NULL);
1356 
1357 	/* Check only at the very beginning.  */
1358 	if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) &&
1359 	    (memcmp(data->buf, "OK", 2) == 0)) {
1360 		sc->sc_flags |= UPGT_FLAG_FWLOADED;
1361 		wakeup_one(sc);
1362 		return (NULL);
1363 	}
1364 
1365 	if (actlen < (int)UPGT_RX_MINSZ)
1366 		return (NULL);
1367 
1368 	/*
1369 	 * Check what type of frame came in.
1370 	 */
1371 	header = (struct upgt_lmac_header *)(data->buf + 4);
1372 
1373 	h1_type = header->header1.type;
1374 	h2_type = le16toh(header->header2.type);
1375 
1376 	if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) {
1377 		eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4);
1378 		uint16_t eeprom_offset = le16toh(eeprom->offset);
1379 		uint16_t eeprom_len = le16toh(eeprom->len);
1380 
1381 		DPRINTF(sc, UPGT_DEBUG_FW,
1382 		    "received EEPROM block (offset=%d, len=%d)\n",
1383 		    eeprom_offset, eeprom_len);
1384 
1385 		memcpy(sc->sc_eeprom + eeprom_offset,
1386 		    data->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1387 		    eeprom_len);
1388 
1389 		/* EEPROM data has arrived in time, wakeup.  */
1390 		wakeup(sc);
1391 	} else if (h1_type == UPGT_H1_TYPE_CTRL &&
1392 	    h2_type == UPGT_H2_TYPE_TX_DONE) {
1393 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n",
1394 		    __func__);
1395 		upgt_tx_done(sc, data->buf + 4);
1396 	} else if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1397 	    h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1398 		DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n",
1399 		    __func__);
1400 		m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len),
1401 		    rssi);
1402 	} else if (h1_type == UPGT_H1_TYPE_CTRL &&
1403 	    h2_type == UPGT_H2_TYPE_STATS) {
1404 		DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n",
1405 		    __func__);
1406 		/* TODO: what could we do with the statistic data? */
1407 	} else {
1408 		/* ignore unknown frame types */
1409 		DPRINTF(sc, UPGT_DEBUG_INTR,
1410 		    "received unknown frame type 0x%02x\n",
1411 		    header->header1.type);
1412 	}
1413 	return (m);
1414 }
1415 
1416 /*
1417  * The firmware awaits a checksum for each frame we send to it.
1418  * The algorithm used therefor is uncommon but somehow similar to CRC32.
1419  */
1420 static uint32_t
1421 upgt_chksum_le(const uint32_t *buf, size_t size)
1422 {
1423 	size_t i;
1424 	uint32_t crc = 0;
1425 
1426 	for (i = 0; i < size; i += sizeof(uint32_t)) {
1427 		crc = htole32(crc ^ *buf++);
1428 		crc = htole32((crc >> 5) ^ (crc << 3));
1429 	}
1430 
1431 	return (crc);
1432 }
1433 
1434 static struct mbuf *
1435 upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi)
1436 {
1437 	struct ieee80211com *ic = &sc->sc_ic;
1438 	struct upgt_lmac_rx_desc *rxdesc;
1439 	struct mbuf *m;
1440 
1441 	/*
1442 	 * don't pass packets to the ieee80211 framework if the driver isn't
1443 	 * RUNNING.
1444 	 */
1445 	if (!(sc->sc_flags & UPGT_FLAG_INITDONE))
1446 		return (NULL);
1447 
1448 	/* access RX packet descriptor */
1449 	rxdesc = (struct upgt_lmac_rx_desc *)data;
1450 
1451 	/* create mbuf which is suitable for strict alignment archs */
1452 	KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES,
1453 	    ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN));
1454 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1455 	if (m == NULL) {
1456 		device_printf(sc->sc_dev, "could not create RX mbuf\n");
1457 		return (NULL);
1458 	}
1459 	m_adj(m, ETHER_ALIGN);
1460 	memcpy(mtod(m, char *), rxdesc->data, pkglen);
1461 	/* trim FCS */
1462 	m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN;
1463 
1464 	if (ieee80211_radiotap_active(ic)) {
1465 		struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1466 
1467 		tap->wr_flags = 0;
1468 		tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1469 		tap->wr_antsignal = rxdesc->rssi;
1470 	}
1471 
1472 	DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__);
1473 	*rssi = rxdesc->rssi;
1474 	return (m);
1475 }
1476 
1477 static uint8_t
1478 upgt_rx_rate(struct upgt_softc *sc, const int rate)
1479 {
1480 	struct ieee80211com *ic = &sc->sc_ic;
1481 	static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 };
1482 	static const uint8_t ofdm_upgt2rate[12] =
1483 	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
1484 
1485 	if (ic->ic_curmode == IEEE80211_MODE_11B &&
1486 	    !(rate < 0 || rate > 3))
1487 		return cck_upgt2rate[rate & 0xf];
1488 
1489 	if (ic->ic_curmode == IEEE80211_MODE_11G &&
1490 	    !(rate < 0 || rate > 11))
1491 		return ofdm_upgt2rate[rate & 0xf];
1492 
1493 	return (0);
1494 }
1495 
1496 static void
1497 upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1498 {
1499 	struct upgt_lmac_tx_done_desc *desc;
1500 	int i, freed = 0;
1501 
1502 	UPGT_ASSERT_LOCKED(sc);
1503 
1504 	desc = (struct upgt_lmac_tx_done_desc *)data;
1505 
1506 	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1507 		struct upgt_data *data_tx = &sc->sc_tx_data[i];
1508 
1509 		if (data_tx->addr == le32toh(desc->header2.reqid)) {
1510 			upgt_mem_free(sc, data_tx->addr);
1511 			data_tx->ni = NULL;
1512 			data_tx->addr = 0;
1513 			data_tx->m = NULL;
1514 
1515 			DPRINTF(sc, UPGT_DEBUG_TX_PROC,
1516 			    "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1517 			    le32toh(desc->header2.reqid),
1518 			    le16toh(desc->status), le16toh(desc->rssi));
1519 			DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n",
1520 			    le16toh(desc->seq));
1521 
1522 			freed++;
1523 		}
1524 	}
1525 
1526 	if (freed != 0) {
1527 		UPGT_UNLOCK(sc);
1528 		sc->sc_tx_timer = 0;
1529 		upgt_start(sc);
1530 		UPGT_LOCK(sc);
1531 	}
1532 }
1533 
1534 static void
1535 upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
1536 {
1537 	int i;
1538 
1539 	for (i = 0; i < sc->sc_memory.pages; i++) {
1540 		if (sc->sc_memory.page[i].addr == addr) {
1541 			sc->sc_memory.page[i].used = 0;
1542 			return;
1543 		}
1544 	}
1545 
1546 	device_printf(sc->sc_dev,
1547 	    "could not free memory address 0x%08x\n", addr);
1548 }
1549 
1550 static int
1551 upgt_fw_load(struct upgt_softc *sc)
1552 {
1553 	const struct firmware *fw;
1554 	struct upgt_data *data_cmd;
1555 	struct upgt_fw_x2_header *x2;
1556 	char start_fwload_cmd[] = { 0x3c, 0x0d };
1557 	int error = 0;
1558 	size_t offset;
1559 	int bsize;
1560 	int n;
1561 	uint32_t crc32;
1562 
1563 	fw = firmware_get(upgt_fwname);
1564 	if (fw == NULL) {
1565 		device_printf(sc->sc_dev, "could not read microcode %s\n",
1566 		    upgt_fwname);
1567 		return (EIO);
1568 	}
1569 
1570 	UPGT_LOCK(sc);
1571 
1572 	/* send firmware start load command */
1573 	data_cmd = upgt_getbuf(sc);
1574 	if (data_cmd == NULL) {
1575 		error = ENOBUFS;
1576 		goto fail;
1577 	}
1578 	data_cmd->buflen = sizeof(start_fwload_cmd);
1579 	memcpy(data_cmd->buf, start_fwload_cmd, data_cmd->buflen);
1580 	upgt_bulk_tx(sc, data_cmd);
1581 
1582 	/* send X2 header */
1583 	data_cmd = upgt_getbuf(sc);
1584 	if (data_cmd == NULL) {
1585 		error = ENOBUFS;
1586 		goto fail;
1587 	}
1588 	data_cmd->buflen = sizeof(struct upgt_fw_x2_header);
1589 	x2 = (struct upgt_fw_x2_header *)data_cmd->buf;
1590 	memcpy(x2->signature, UPGT_X2_SIGNATURE, UPGT_X2_SIGNATURE_SIZE);
1591 	x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
1592 	x2->len = htole32(fw->datasize);
1593 	x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf +
1594 	    UPGT_X2_SIGNATURE_SIZE,
1595 	    sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
1596 	    sizeof(uint32_t));
1597 	upgt_bulk_tx(sc, data_cmd);
1598 
1599 	/* download firmware */
1600 	for (offset = 0; offset < fw->datasize; offset += bsize) {
1601 		if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE)
1602 			bsize = UPGT_FW_BLOCK_SIZE;
1603 		else
1604 			bsize = fw->datasize - offset;
1605 
1606 		data_cmd = upgt_getbuf(sc);
1607 		if (data_cmd == NULL) {
1608 			error = ENOBUFS;
1609 			goto fail;
1610 		}
1611 		n = upgt_fw_copy((const uint8_t *)fw->data + offset,
1612 		    data_cmd->buf, bsize);
1613 		data_cmd->buflen = bsize;
1614 		upgt_bulk_tx(sc, data_cmd);
1615 
1616 		DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n",
1617 		    offset, n, bsize);
1618 		bsize = n;
1619 	}
1620 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__);
1621 
1622 	/* load firmware */
1623 	data_cmd = upgt_getbuf(sc);
1624 	if (data_cmd == NULL) {
1625 		error = ENOBUFS;
1626 		goto fail;
1627 	}
1628 	crc32 = upgt_crc32_le(fw->data, fw->datasize);
1629 	*((uint32_t *)(data_cmd->buf)    ) = crc32;
1630 	*((uint8_t  *)(data_cmd->buf) + 4) = 'g';
1631 	*((uint8_t  *)(data_cmd->buf) + 5) = '\r';
1632 	data_cmd->buflen = 6;
1633 	upgt_bulk_tx(sc, data_cmd);
1634 
1635 	/* waiting 'OK' response.  */
1636 	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
1637 	error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz);
1638 	if (error != 0) {
1639 		device_printf(sc->sc_dev, "firmware load failed\n");
1640 		error = EIO;
1641 	}
1642 
1643 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__);
1644 fail:
1645 	UPGT_UNLOCK(sc);
1646 	firmware_put(fw, FIRMWARE_UNLOAD);
1647 	return (error);
1648 }
1649 
1650 static uint32_t
1651 upgt_crc32_le(const void *buf, size_t size)
1652 {
1653 	uint32_t crc;
1654 
1655 	crc = ether_crc32_le(buf, size);
1656 
1657 	/* apply final XOR value as common for CRC-32 */
1658 	crc = htole32(crc ^ 0xffffffffU);
1659 
1660 	return (crc);
1661 }
1662 
1663 /*
1664  * While copying the version 2 firmware, we need to replace two characters:
1665  *
1666  * 0x7e -> 0x7d 0x5e
1667  * 0x7d -> 0x7d 0x5d
1668  */
1669 static int
1670 upgt_fw_copy(const uint8_t *src, char *dst, int size)
1671 {
1672 	int i, j;
1673 
1674 	for (i = 0, j = 0; i < size && j < size; i++) {
1675 		switch (src[i]) {
1676 		case 0x7e:
1677 			dst[j] = 0x7d;
1678 			j++;
1679 			dst[j] = 0x5e;
1680 			j++;
1681 			break;
1682 		case 0x7d:
1683 			dst[j] = 0x7d;
1684 			j++;
1685 			dst[j] = 0x5d;
1686 			j++;
1687 			break;
1688 		default:
1689 			dst[j] = src[i];
1690 			j++;
1691 			break;
1692 		}
1693 	}
1694 
1695 	return (i);
1696 }
1697 
1698 static int
1699 upgt_mem_init(struct upgt_softc *sc)
1700 {
1701 	int i;
1702 
1703 	for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
1704 		sc->sc_memory.page[i].used = 0;
1705 
1706 		if (i == 0) {
1707 			/*
1708 			 * The first memory page is always reserved for
1709 			 * command data.
1710 			 */
1711 			sc->sc_memory.page[i].addr =
1712 			    sc->sc_memaddr_frame_start + MCLBYTES;
1713 		} else {
1714 			sc->sc_memory.page[i].addr =
1715 			    sc->sc_memory.page[i - 1].addr + MCLBYTES;
1716 		}
1717 
1718 		if (sc->sc_memory.page[i].addr + MCLBYTES >=
1719 		    sc->sc_memaddr_frame_end)
1720 			break;
1721 
1722 		DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n",
1723 		    i, sc->sc_memory.page[i].addr);
1724 	}
1725 
1726 	sc->sc_memory.pages = i;
1727 
1728 	DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages);
1729 	return (0);
1730 }
1731 
1732 static int
1733 upgt_fw_verify(struct upgt_softc *sc)
1734 {
1735 	const struct firmware *fw;
1736 	const struct upgt_fw_bra_option *bra_opt;
1737 	const struct upgt_fw_bra_descr *descr;
1738 	const uint8_t *p;
1739 	const uint32_t *uc;
1740 	uint32_t bra_option_type, bra_option_len;
1741 	size_t offset;
1742 	int bra_end = 0;
1743 	int error = 0;
1744 
1745 	fw = firmware_get(upgt_fwname);
1746 	if (fw == NULL) {
1747 		device_printf(sc->sc_dev, "could not read microcode %s\n",
1748 		    upgt_fwname);
1749 		return EIO;
1750 	}
1751 
1752 	/*
1753 	 * Seek to beginning of Boot Record Area (BRA).
1754 	 */
1755 	for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) {
1756 		uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1757 		if (*uc == 0)
1758 			break;
1759 	}
1760 	for (; offset < fw->datasize; offset += sizeof(*uc)) {
1761 		uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1762 		if (*uc != 0)
1763 			break;
1764 	}
1765 	if (offset == fw->datasize) {
1766 		device_printf(sc->sc_dev,
1767 		    "firmware Boot Record Area not found\n");
1768 		error = EIO;
1769 		goto fail;
1770 	}
1771 
1772 	DPRINTF(sc, UPGT_DEBUG_FW,
1773 	    "firmware Boot Record Area found at offset %d\n", offset);
1774 
1775 	/*
1776 	 * Parse Boot Record Area (BRA) options.
1777 	 */
1778 	while (offset < fw->datasize && bra_end == 0) {
1779 		/* get current BRA option */
1780 		p = (const uint8_t *)fw->data + offset;
1781 		bra_opt = (const struct upgt_fw_bra_option *)p;
1782 		bra_option_type = le32toh(bra_opt->type);
1783 		bra_option_len = le32toh(bra_opt->len) * sizeof(*uc);
1784 
1785 		switch (bra_option_type) {
1786 		case UPGT_BRA_TYPE_FW:
1787 			DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n",
1788 			    bra_option_len);
1789 
1790 			if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
1791 				device_printf(sc->sc_dev,
1792 				    "wrong UPGT_BRA_TYPE_FW len\n");
1793 				error = EIO;
1794 				goto fail;
1795 			}
1796 			if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data,
1797 			    bra_option_len) == 0) {
1798 				sc->sc_fw_type = UPGT_FWTYPE_LM86;
1799 				break;
1800 			}
1801 			if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data,
1802 			    bra_option_len) == 0) {
1803 				sc->sc_fw_type = UPGT_FWTYPE_LM87;
1804 				break;
1805 			}
1806 			device_printf(sc->sc_dev,
1807 			    "unsupported firmware type\n");
1808 			error = EIO;
1809 			goto fail;
1810 		case UPGT_BRA_TYPE_VERSION:
1811 			DPRINTF(sc, UPGT_DEBUG_FW,
1812 			    "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len);
1813 			break;
1814 		case UPGT_BRA_TYPE_DEPIF:
1815 			DPRINTF(sc, UPGT_DEBUG_FW,
1816 			    "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len);
1817 			break;
1818 		case UPGT_BRA_TYPE_EXPIF:
1819 			DPRINTF(sc, UPGT_DEBUG_FW,
1820 			    "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len);
1821 			break;
1822 		case UPGT_BRA_TYPE_DESCR:
1823 			DPRINTF(sc, UPGT_DEBUG_FW,
1824 			    "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len);
1825 
1826 			descr = (const struct upgt_fw_bra_descr *)bra_opt->data;
1827 
1828 			sc->sc_memaddr_frame_start =
1829 			    le32toh(descr->memaddr_space_start);
1830 			sc->sc_memaddr_frame_end =
1831 			    le32toh(descr->memaddr_space_end);
1832 
1833 			DPRINTF(sc, UPGT_DEBUG_FW,
1834 			    "memory address space start=0x%08x\n",
1835 			    sc->sc_memaddr_frame_start);
1836 			DPRINTF(sc, UPGT_DEBUG_FW,
1837 			    "memory address space end=0x%08x\n",
1838 			    sc->sc_memaddr_frame_end);
1839 			break;
1840 		case UPGT_BRA_TYPE_END:
1841 			DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n",
1842 			    bra_option_len);
1843 			bra_end = 1;
1844 			break;
1845 		default:
1846 			DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n",
1847 			    bra_option_len);
1848 			error = EIO;
1849 			goto fail;
1850 		}
1851 
1852 		/* jump to next BRA option */
1853 		offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
1854 	}
1855 
1856 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__);
1857 fail:
1858 	firmware_put(fw, FIRMWARE_UNLOAD);
1859 	return (error);
1860 }
1861 
1862 static void
1863 upgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data)
1864 {
1865 
1866 	UPGT_ASSERT_LOCKED(sc);
1867 
1868 	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1869 	UPGT_STAT_INC(sc, st_tx_pending);
1870 	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]);
1871 }
1872 
1873 static int
1874 upgt_device_reset(struct upgt_softc *sc)
1875 {
1876 	struct upgt_data *data;
1877 	char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
1878 
1879 	UPGT_LOCK(sc);
1880 
1881 	data = upgt_getbuf(sc);
1882 	if (data == NULL) {
1883 		UPGT_UNLOCK(sc);
1884 		return (ENOBUFS);
1885 	}
1886 	memcpy(data->buf, init_cmd, sizeof(init_cmd));
1887 	data->buflen = sizeof(init_cmd);
1888 	upgt_bulk_tx(sc, data);
1889 	usb_pause_mtx(&sc->sc_mtx, 100);
1890 
1891 	UPGT_UNLOCK(sc);
1892 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__);
1893 	return (0);
1894 }
1895 
1896 static int
1897 upgt_alloc_tx(struct upgt_softc *sc)
1898 {
1899 	int i;
1900 
1901 	STAILQ_INIT(&sc->sc_tx_active);
1902 	STAILQ_INIT(&sc->sc_tx_inactive);
1903 	STAILQ_INIT(&sc->sc_tx_pending);
1904 
1905 	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1906 		struct upgt_data *data = &sc->sc_tx_data[i];
1907 		data->buf = ((uint8_t *)sc->sc_tx_dma_buf) + (i * MCLBYTES);
1908 		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1909 		UPGT_STAT_INC(sc, st_tx_inactive);
1910 	}
1911 
1912 	return (0);
1913 }
1914 
1915 static int
1916 upgt_alloc_rx(struct upgt_softc *sc)
1917 {
1918 	int i;
1919 
1920 	STAILQ_INIT(&sc->sc_rx_active);
1921 	STAILQ_INIT(&sc->sc_rx_inactive);
1922 
1923 	for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1924 		struct upgt_data *data = &sc->sc_rx_data[i];
1925 		data->buf = ((uint8_t *)sc->sc_rx_dma_buf) + (i * MCLBYTES);
1926 		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1927 	}
1928 	return (0);
1929 }
1930 
1931 static int
1932 upgt_detach(device_t dev)
1933 {
1934 	struct upgt_softc *sc = device_get_softc(dev);
1935 	struct ieee80211com *ic = &sc->sc_ic;
1936 	unsigned int x;
1937 
1938 	/*
1939 	 * Prevent further allocations from RX/TX/CMD
1940 	 * data lists and ioctls
1941 	 */
1942 	UPGT_LOCK(sc);
1943 	sc->sc_flags |= UPGT_FLAG_DETACHED;
1944 
1945 	STAILQ_INIT(&sc->sc_tx_active);
1946 	STAILQ_INIT(&sc->sc_tx_inactive);
1947 	STAILQ_INIT(&sc->sc_tx_pending);
1948 
1949 	STAILQ_INIT(&sc->sc_rx_active);
1950 	STAILQ_INIT(&sc->sc_rx_inactive);
1951 
1952 	upgt_stop(sc);
1953 	UPGT_UNLOCK(sc);
1954 
1955 	callout_drain(&sc->sc_led_ch);
1956 	callout_drain(&sc->sc_watchdog_ch);
1957 
1958 	/* drain USB transfers */
1959 	for (x = 0; x != UPGT_N_XFERS; x++)
1960 		usbd_transfer_drain(sc->sc_xfer[x]);
1961 
1962 	/* free data buffers */
1963 	UPGT_LOCK(sc);
1964 	upgt_free_rx(sc);
1965 	upgt_free_tx(sc);
1966 	UPGT_UNLOCK(sc);
1967 
1968 	/* free USB transfers and some data buffers */
1969 	usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
1970 
1971 	ieee80211_ifdetach(ic);
1972 	mbufq_drain(&sc->sc_snd);
1973 	mtx_destroy(&sc->sc_mtx);
1974 
1975 	return (0);
1976 }
1977 
1978 static void
1979 upgt_free_rx(struct upgt_softc *sc)
1980 {
1981 	int i;
1982 
1983 	for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1984 		struct upgt_data *data = &sc->sc_rx_data[i];
1985 
1986 		data->buf = NULL;
1987 		data->ni = NULL;
1988 	}
1989 }
1990 
1991 static void
1992 upgt_free_tx(struct upgt_softc *sc)
1993 {
1994 	int i;
1995 
1996 	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1997 		struct upgt_data *data = &sc->sc_tx_data[i];
1998 
1999 		if (data->ni != NULL)
2000 			ieee80211_free_node(data->ni);
2001 
2002 		data->buf = NULL;
2003 		data->ni = NULL;
2004 	}
2005 }
2006 
2007 static void
2008 upgt_abort_xfers_locked(struct upgt_softc *sc)
2009 {
2010 	int i;
2011 
2012 	UPGT_ASSERT_LOCKED(sc);
2013 	/* abort any pending transfers */
2014 	for (i = 0; i < UPGT_N_XFERS; i++)
2015 		usbd_transfer_stop(sc->sc_xfer[i]);
2016 }
2017 
2018 static void
2019 upgt_abort_xfers(struct upgt_softc *sc)
2020 {
2021 
2022 	UPGT_LOCK(sc);
2023 	upgt_abort_xfers_locked(sc);
2024 	UPGT_UNLOCK(sc);
2025 }
2026 
2027 #define	UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2028 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2029 
2030 static void
2031 upgt_sysctl_node(struct upgt_softc *sc)
2032 {
2033 	struct sysctl_ctx_list *ctx;
2034 	struct sysctl_oid_list *child;
2035 	struct sysctl_oid *tree;
2036 	struct upgt_stat *stats;
2037 
2038 	stats = &sc->sc_stat;
2039 	ctx = device_get_sysctl_ctx(sc->sc_dev);
2040 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2041 
2042 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2043 	    NULL, "UPGT statistics");
2044 	child = SYSCTL_CHILDREN(tree);
2045 	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2046 	    &stats->st_tx_active, "Active numbers in TX queue");
2047 	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2048 	    &stats->st_tx_inactive, "Inactive numbers in TX queue");
2049 	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2050 	    &stats->st_tx_pending, "Pending numbers in TX queue");
2051 }
2052 
2053 #undef UPGT_SYSCTL_STAT_ADD32
2054 
2055 static struct upgt_data *
2056 _upgt_getbuf(struct upgt_softc *sc)
2057 {
2058 	struct upgt_data *bf;
2059 
2060 	bf = STAILQ_FIRST(&sc->sc_tx_inactive);
2061 	if (bf != NULL) {
2062 		STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
2063 		UPGT_STAT_DEC(sc, st_tx_inactive);
2064 	} else
2065 		bf = NULL;
2066 	if (bf == NULL)
2067 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__,
2068 		    "out of xmit buffers");
2069 	return (bf);
2070 }
2071 
2072 static struct upgt_data *
2073 upgt_getbuf(struct upgt_softc *sc)
2074 {
2075 	struct upgt_data *bf;
2076 
2077 	UPGT_ASSERT_LOCKED(sc);
2078 
2079 	bf = _upgt_getbuf(sc);
2080 	if (bf == NULL)
2081 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__);
2082 
2083 	return (bf);
2084 }
2085 
2086 static struct upgt_data *
2087 upgt_gettxbuf(struct upgt_softc *sc)
2088 {
2089 	struct upgt_data *bf;
2090 
2091 	UPGT_ASSERT_LOCKED(sc);
2092 
2093 	bf = upgt_getbuf(sc);
2094 	if (bf == NULL)
2095 		return (NULL);
2096 
2097 	bf->addr = upgt_mem_alloc(sc);
2098 	if (bf->addr == 0) {
2099 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n",
2100 		    __func__);
2101 		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
2102 		UPGT_STAT_INC(sc, st_tx_inactive);
2103 		return (NULL);
2104 	}
2105 	return (bf);
2106 }
2107 
2108 static int
2109 upgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2110     struct upgt_data *data)
2111 {
2112 	struct ieee80211vap *vap = ni->ni_vap;
2113 	int error = 0, len;
2114 	struct ieee80211_frame *wh;
2115 	struct ieee80211_key *k;
2116 	struct upgt_lmac_mem *mem;
2117 	struct upgt_lmac_tx_desc *txdesc;
2118 
2119 	UPGT_ASSERT_LOCKED(sc);
2120 
2121 	upgt_set_led(sc, UPGT_LED_BLINK);
2122 
2123 	/*
2124 	 * Software crypto.
2125 	 */
2126 	wh = mtod(m, struct ieee80211_frame *);
2127 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2128 		k = ieee80211_crypto_encap(ni, m);
2129 		if (k == NULL) {
2130 			device_printf(sc->sc_dev,
2131 			    "ieee80211_crypto_encap returns NULL.\n");
2132 			error = EIO;
2133 			goto done;
2134 		}
2135 
2136 		/* in case packet header moved, reset pointer */
2137 		wh = mtod(m, struct ieee80211_frame *);
2138 	}
2139 
2140 	/* Transmit the URB containing the TX data.  */
2141 	memset(data->buf, 0, MCLBYTES);
2142 	mem = (struct upgt_lmac_mem *)data->buf;
2143 	mem->addr = htole32(data->addr);
2144 	txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
2145 
2146 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2147 	    IEEE80211_FC0_TYPE_MGT) {
2148 		/* mgmt frames  */
2149 		txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT;
2150 		/* always send mgmt frames at lowest rate (DS1) */
2151 		memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
2152 	} else {
2153 		/* data frames  */
2154 		txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
2155 		memcpy(txdesc->rates, sc->sc_cur_rateset, sizeof(txdesc->rates));
2156 	}
2157 	txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
2158 	txdesc->header1.len = htole16(m->m_pkthdr.len);
2159 	txdesc->header2.reqid = htole32(data->addr);
2160 	txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
2161 	txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
2162 	txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
2163 	txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
2164 
2165 	if (ieee80211_radiotap_active_vap(vap)) {
2166 		struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
2167 
2168 		tap->wt_flags = 0;
2169 		tap->wt_rate = 0;	/* XXX where to get from? */
2170 
2171 		ieee80211_radiotap_tx(vap, m);
2172 	}
2173 
2174 	/* copy frame below our TX descriptor header */
2175 	m_copydata(m, 0, m->m_pkthdr.len,
2176 	    data->buf + (sizeof(*mem) + sizeof(*txdesc)));
2177 	/* calculate frame size */
2178 	len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
2179 	/* we need to align the frame to a 4 byte boundary */
2180 	len = (len + 3) & ~3;
2181 	/* calculate frame checksum */
2182 	mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem));
2183 	data->ni = ni;
2184 	data->m = m;
2185 	data->buflen = len;
2186 
2187 	DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n",
2188 	    __func__, len);
2189 	KASSERT(len <= MCLBYTES, ("mbuf is small for saving data"));
2190 
2191 	upgt_bulk_tx(sc, data);
2192 done:
2193 	/*
2194 	 * If we don't regulary read the device statistics, the RX queue
2195 	 * will stall.  It's strange, but it works, so we keep reading
2196 	 * the statistics here.  *shrug*
2197 	 */
2198 	if (!(vap->iv_ifp->if_get_counter(vap->iv_ifp, IFCOUNTER_OPACKETS) %
2199 	    UPGT_TX_STAT_INTERVAL))
2200 		upgt_get_stats(sc);
2201 
2202 	return (error);
2203 }
2204 
2205 static void
2206 upgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2207 {
2208 	struct upgt_softc *sc = usbd_xfer_softc(xfer);
2209 	struct ieee80211com *ic = &sc->sc_ic;
2210 	struct ieee80211_frame *wh;
2211 	struct ieee80211_node *ni;
2212 	struct mbuf *m = NULL;
2213 	struct upgt_data *data;
2214 	int8_t nf;
2215 	int rssi = -1;
2216 
2217 	UPGT_ASSERT_LOCKED(sc);
2218 
2219 	switch (USB_GET_STATE(xfer)) {
2220 	case USB_ST_TRANSFERRED:
2221 		data = STAILQ_FIRST(&sc->sc_rx_active);
2222 		if (data == NULL)
2223 			goto setup;
2224 		STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2225 		m = upgt_rxeof(xfer, data, &rssi);
2226 		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2227 		/* FALLTHROUGH */
2228 	case USB_ST_SETUP:
2229 setup:
2230 		data = STAILQ_FIRST(&sc->sc_rx_inactive);
2231 		if (data == NULL)
2232 			return;
2233 		STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2234 		STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2235 		usbd_xfer_set_frame_data(xfer, 0, data->buf, MCLBYTES);
2236 		usbd_transfer_submit(xfer);
2237 
2238 		/*
2239 		 * To avoid LOR we should unlock our private mutex here to call
2240 		 * ieee80211_input() because here is at the end of a USB
2241 		 * callback and safe to unlock.
2242 		 */
2243 		UPGT_UNLOCK(sc);
2244 		if (m != NULL) {
2245 			wh = mtod(m, struct ieee80211_frame *);
2246 			ni = ieee80211_find_rxnode(ic,
2247 			    (struct ieee80211_frame_min *)wh);
2248 			nf = -95;	/* XXX */
2249 			if (ni != NULL) {
2250 				(void) ieee80211_input(ni, m, rssi, nf);
2251 				/* node is no longer needed */
2252 				ieee80211_free_node(ni);
2253 			} else
2254 				(void) ieee80211_input_all(ic, m, rssi, nf);
2255 			m = NULL;
2256 		}
2257 		UPGT_LOCK(sc);
2258 		upgt_start(sc);
2259 		break;
2260 	default:
2261 		/* needs it to the inactive queue due to a error.  */
2262 		data = STAILQ_FIRST(&sc->sc_rx_active);
2263 		if (data != NULL) {
2264 			STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2265 			STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2266 		}
2267 		if (error != USB_ERR_CANCELLED) {
2268 			usbd_xfer_set_stall(xfer);
2269 			counter_u64_add(ic->ic_ierrors, 1);
2270 			goto setup;
2271 		}
2272 		break;
2273 	}
2274 }
2275 
2276 static void
2277 upgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2278 {
2279 	struct upgt_softc *sc = usbd_xfer_softc(xfer);
2280 	struct upgt_data *data;
2281 
2282 	UPGT_ASSERT_LOCKED(sc);
2283 	switch (USB_GET_STATE(xfer)) {
2284 	case USB_ST_TRANSFERRED:
2285 		data = STAILQ_FIRST(&sc->sc_tx_active);
2286 		if (data == NULL)
2287 			goto setup;
2288 		STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2289 		UPGT_STAT_DEC(sc, st_tx_active);
2290 		upgt_txeof(xfer, data);
2291 		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2292 		UPGT_STAT_INC(sc, st_tx_inactive);
2293 		/* FALLTHROUGH */
2294 	case USB_ST_SETUP:
2295 setup:
2296 		data = STAILQ_FIRST(&sc->sc_tx_pending);
2297 		if (data == NULL) {
2298 			DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n",
2299 			    __func__);
2300 			return;
2301 		}
2302 		STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2303 		UPGT_STAT_DEC(sc, st_tx_pending);
2304 		STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2305 		UPGT_STAT_INC(sc, st_tx_active);
2306 
2307 		usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2308 		usbd_transfer_submit(xfer);
2309 		upgt_start(sc);
2310 		break;
2311 	default:
2312 		data = STAILQ_FIRST(&sc->sc_tx_active);
2313 		if (data == NULL)
2314 			goto setup;
2315 		if (data->ni != NULL) {
2316 			if_inc_counter(data->ni->ni_vap->iv_ifp,
2317 			    IFCOUNTER_OERRORS, 1);
2318 			ieee80211_free_node(data->ni);
2319 			data->ni = NULL;
2320 		}
2321 		if (error != USB_ERR_CANCELLED) {
2322 			usbd_xfer_set_stall(xfer);
2323 			goto setup;
2324 		}
2325 		break;
2326 	}
2327 }
2328 
2329 static device_method_t upgt_methods[] = {
2330         /* Device interface */
2331         DEVMETHOD(device_probe, upgt_match),
2332         DEVMETHOD(device_attach, upgt_attach),
2333         DEVMETHOD(device_detach, upgt_detach),
2334 	DEVMETHOD_END
2335 };
2336 
2337 static driver_t upgt_driver = {
2338 	.name = "upgt",
2339 	.methods = upgt_methods,
2340 	.size = sizeof(struct upgt_softc)
2341 };
2342 
2343 static devclass_t upgt_devclass;
2344 
2345 DRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0);
2346 MODULE_VERSION(if_upgt, 1);
2347 MODULE_DEPEND(if_upgt, usb, 1, 1, 1);
2348 MODULE_DEPEND(if_upgt, wlan, 1, 1, 1);
2349 MODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1);
2350