xref: /freebsd/sys/dev/usb/wlan/if_upgt.c (revision 5ca34122ecdd5abc62bdae39663fec9ac8523d87)
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 		ieee80211_free_node(ni);
802 		UPGT_UNLOCK(sc);
803 		return ENETDOWN;
804 	}
805 
806 	data_tx = upgt_gettxbuf(sc);
807 	if (data_tx == NULL) {
808 		ieee80211_free_node(ni);
809 		m_freem(m);
810 		UPGT_UNLOCK(sc);
811 		return (ENOBUFS);
812 	}
813 
814 	if (upgt_tx_start(sc, m, ni, data_tx) != 0) {
815 		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, data_tx, next);
816 		UPGT_STAT_INC(sc, st_tx_inactive);
817 		ieee80211_free_node(ni);
818 		UPGT_UNLOCK(sc);
819 		return (EIO);
820 	}
821 	UPGT_UNLOCK(sc);
822 
823 	sc->sc_tx_timer = 5;
824 	return (0);
825 }
826 
827 static void
828 upgt_watchdog(void *arg)
829 {
830 	struct upgt_softc *sc = arg;
831 	struct ieee80211com *ic = &sc->sc_ic;
832 
833 	if (sc->sc_tx_timer > 0) {
834 		if (--sc->sc_tx_timer == 0) {
835 			device_printf(sc->sc_dev, "watchdog timeout\n");
836 			/* upgt_init(sc); XXX needs a process context ? */
837 			counter_u64_add(ic->ic_oerrors, 1);
838 			return;
839 		}
840 		callout_reset(&sc->sc_watchdog_ch, hz, upgt_watchdog, sc);
841 	}
842 }
843 
844 static uint32_t
845 upgt_mem_alloc(struct upgt_softc *sc)
846 {
847 	int i;
848 
849 	for (i = 0; i < sc->sc_memory.pages; i++) {
850 		if (sc->sc_memory.page[i].used == 0) {
851 			sc->sc_memory.page[i].used = 1;
852 			return (sc->sc_memory.page[i].addr);
853 		}
854 	}
855 
856 	return (0);
857 }
858 
859 static void
860 upgt_scan_start(struct ieee80211com *ic)
861 {
862 	/* do nothing.  */
863 }
864 
865 static void
866 upgt_scan_end(struct ieee80211com *ic)
867 {
868 	/* do nothing.  */
869 }
870 
871 static void
872 upgt_set_channel(struct ieee80211com *ic)
873 {
874 	struct upgt_softc *sc = ic->ic_softc;
875 
876 	UPGT_LOCK(sc);
877 	upgt_set_chan(sc, ic->ic_curchan);
878 	UPGT_UNLOCK(sc);
879 }
880 
881 static void
882 upgt_set_chan(struct upgt_softc *sc, struct ieee80211_channel *c)
883 {
884 	struct ieee80211com *ic = &sc->sc_ic;
885 	struct upgt_data *data_cmd;
886 	struct upgt_lmac_mem *mem;
887 	struct upgt_lmac_channel *chan;
888 	int channel;
889 
890 	UPGT_ASSERT_LOCKED(sc);
891 
892 	channel = ieee80211_chan2ieee(ic, c);
893 	if (channel == 0 || channel == IEEE80211_CHAN_ANY) {
894 		/* XXX should NEVER happen */
895 		device_printf(sc->sc_dev,
896 		    "%s: invalid channel %x\n", __func__, channel);
897 		return;
898 	}
899 
900 	DPRINTF(sc, UPGT_DEBUG_STATE, "%s: channel %d\n", __func__, channel);
901 
902 	data_cmd = upgt_getbuf(sc);
903 	if (data_cmd == NULL) {
904 		device_printf(sc->sc_dev, "%s: out of buffers.\n", __func__);
905 		return;
906 	}
907 	/*
908 	 * Transmit the URB containing the CMD data.
909 	 */
910 	memset(data_cmd->buf, 0, MCLBYTES);
911 
912 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
913 	mem->addr = htole32(sc->sc_memaddr_frame_start +
914 	    UPGT_MEMSIZE_FRAME_HEAD);
915 
916 	chan = (struct upgt_lmac_channel *)(mem + 1);
917 
918 	chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
919 	chan->header1.type = UPGT_H1_TYPE_CTRL;
920 	chan->header1.len = htole16(
921 	    sizeof(struct upgt_lmac_channel) - sizeof(struct upgt_lmac_header));
922 
923 	chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
924 	chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
925 	chan->header2.flags = 0;
926 
927 	chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
928 	chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
929 	chan->freq6 = sc->sc_eeprom_freq6[channel];
930 	chan->settings = sc->sc_eeprom_freq6_settings;
931 	chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
932 
933 	memcpy(chan->freq3_1, &sc->sc_eeprom_freq3[channel].data,
934 	    sizeof(chan->freq3_1));
935 	memcpy(chan->freq4, &sc->sc_eeprom_freq4[channel],
936 	    sizeof(sc->sc_eeprom_freq4[channel]));
937 	memcpy(chan->freq3_2, &sc->sc_eeprom_freq3[channel].data,
938 	    sizeof(chan->freq3_2));
939 
940 	data_cmd->buflen = sizeof(*mem) + sizeof(*chan);
941 
942 	mem->chksum = upgt_chksum_le((uint32_t *)chan,
943 	    data_cmd->buflen - sizeof(*mem));
944 
945 	upgt_bulk_tx(sc, data_cmd);
946 }
947 
948 static struct ieee80211vap *
949 upgt_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
950     enum ieee80211_opmode opmode, int flags,
951     const uint8_t bssid[IEEE80211_ADDR_LEN],
952     const uint8_t mac[IEEE80211_ADDR_LEN])
953 {
954 	struct upgt_vap *uvp;
955 	struct ieee80211vap *vap;
956 
957 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
958 		return NULL;
959 	uvp = (struct upgt_vap *) malloc(sizeof(struct upgt_vap),
960 	    M_80211_VAP, M_NOWAIT | M_ZERO);
961 	if (uvp == NULL)
962 		return NULL;
963 	vap = &uvp->vap;
964 	/* enable s/w bmiss handling for sta mode */
965 
966 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
967 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
968 		/* out of memory */
969 		free(uvp, M_80211_VAP);
970 		return (NULL);
971 	}
972 
973 	/* override state transition machine */
974 	uvp->newstate = vap->iv_newstate;
975 	vap->iv_newstate = upgt_newstate;
976 
977 	/* setup device rates */
978 	upgt_setup_rates(vap, ic);
979 
980 	/* complete setup */
981 	ieee80211_vap_attach(vap, ieee80211_media_change,
982 	    ieee80211_media_status, mac);
983 	ic->ic_opmode = opmode;
984 	return vap;
985 }
986 
987 static int
988 upgt_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
989 {
990 	struct upgt_vap *uvp = UPGT_VAP(vap);
991 	struct ieee80211com *ic = vap->iv_ic;
992 	struct upgt_softc *sc = ic->ic_softc;
993 
994 	/* do it in a process context */
995 	sc->sc_state = nstate;
996 
997 	IEEE80211_UNLOCK(ic);
998 	UPGT_LOCK(sc);
999 	callout_stop(&sc->sc_led_ch);
1000 	callout_stop(&sc->sc_watchdog_ch);
1001 
1002 	switch (nstate) {
1003 	case IEEE80211_S_INIT:
1004 		/* do not accept any frames if the device is down */
1005 		(void)upgt_set_macfilter(sc, sc->sc_state);
1006 		upgt_set_led(sc, UPGT_LED_OFF);
1007 		break;
1008 	case IEEE80211_S_SCAN:
1009 		upgt_set_chan(sc, ic->ic_curchan);
1010 		break;
1011 	case IEEE80211_S_AUTH:
1012 		upgt_set_chan(sc, ic->ic_curchan);
1013 		break;
1014 	case IEEE80211_S_ASSOC:
1015 		break;
1016 	case IEEE80211_S_RUN:
1017 		upgt_set_macfilter(sc, sc->sc_state);
1018 		upgt_set_led(sc, UPGT_LED_ON);
1019 		break;
1020 	default:
1021 		break;
1022 	}
1023 	UPGT_UNLOCK(sc);
1024 	IEEE80211_LOCK(ic);
1025 	return (uvp->newstate(vap, nstate, arg));
1026 }
1027 
1028 static void
1029 upgt_vap_delete(struct ieee80211vap *vap)
1030 {
1031 	struct upgt_vap *uvp = UPGT_VAP(vap);
1032 
1033 	ieee80211_vap_detach(vap);
1034 	free(uvp, M_80211_VAP);
1035 }
1036 
1037 static void
1038 upgt_update_mcast(struct ieee80211com *ic)
1039 {
1040 	struct upgt_softc *sc = ic->ic_softc;
1041 
1042 	upgt_set_multi(sc);
1043 }
1044 
1045 static int
1046 upgt_eeprom_parse(struct upgt_softc *sc)
1047 {
1048 	struct ieee80211com *ic = &sc->sc_ic;
1049 	struct upgt_eeprom_header *eeprom_header;
1050 	struct upgt_eeprom_option *eeprom_option;
1051 	uint16_t option_len;
1052 	uint16_t option_type;
1053 	uint16_t preamble_len;
1054 	int option_end = 0;
1055 
1056 	/* calculate eeprom options start offset */
1057 	eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
1058 	preamble_len = le16toh(eeprom_header->preamble_len);
1059 	eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
1060 	    (sizeof(struct upgt_eeprom_header) + preamble_len));
1061 
1062 	while (!option_end) {
1063 
1064 		/* sanity check */
1065 		if (eeprom_option >= (struct upgt_eeprom_option *)
1066 		    (sc->sc_eeprom + UPGT_EEPROM_SIZE)) {
1067 			return (EINVAL);
1068 		}
1069 
1070 		/* the eeprom option length is stored in words */
1071 		option_len =
1072 		    (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t);
1073 		option_type =
1074 		    le16toh(eeprom_option->type);
1075 
1076 		/* sanity check */
1077 		if (option_len == 0 || option_len >= UPGT_EEPROM_SIZE)
1078 			return (EINVAL);
1079 
1080 		switch (option_type) {
1081 		case UPGT_EEPROM_TYPE_NAME:
1082 			DPRINTF(sc, UPGT_DEBUG_FW,
1083 			    "EEPROM name len=%d\n", option_len);
1084 			break;
1085 		case UPGT_EEPROM_TYPE_SERIAL:
1086 			DPRINTF(sc, UPGT_DEBUG_FW,
1087 			    "EEPROM serial len=%d\n", option_len);
1088 			break;
1089 		case UPGT_EEPROM_TYPE_MAC:
1090 			DPRINTF(sc, UPGT_DEBUG_FW,
1091 			    "EEPROM mac len=%d\n", option_len);
1092 
1093 			IEEE80211_ADDR_COPY(ic->ic_macaddr,
1094 			    eeprom_option->data);
1095 			break;
1096 		case UPGT_EEPROM_TYPE_HWRX:
1097 			DPRINTF(sc, UPGT_DEBUG_FW,
1098 			    "EEPROM hwrx len=%d\n", option_len);
1099 
1100 			upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1101 			break;
1102 		case UPGT_EEPROM_TYPE_CHIP:
1103 			DPRINTF(sc, UPGT_DEBUG_FW,
1104 			    "EEPROM chip len=%d\n", option_len);
1105 			break;
1106 		case UPGT_EEPROM_TYPE_FREQ3:
1107 			DPRINTF(sc, UPGT_DEBUG_FW,
1108 			    "EEPROM freq3 len=%d\n", option_len);
1109 
1110 			upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1111 			    option_len);
1112 			break;
1113 		case UPGT_EEPROM_TYPE_FREQ4:
1114 			DPRINTF(sc, UPGT_DEBUG_FW,
1115 			    "EEPROM freq4 len=%d\n", option_len);
1116 
1117 			upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1118 			    option_len);
1119 			break;
1120 		case UPGT_EEPROM_TYPE_FREQ5:
1121 			DPRINTF(sc, UPGT_DEBUG_FW,
1122 			    "EEPROM freq5 len=%d\n", option_len);
1123 			break;
1124 		case UPGT_EEPROM_TYPE_FREQ6:
1125 			DPRINTF(sc, UPGT_DEBUG_FW,
1126 			    "EEPROM freq6 len=%d\n", option_len);
1127 
1128 			upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1129 			    option_len);
1130 			break;
1131 		case UPGT_EEPROM_TYPE_END:
1132 			DPRINTF(sc, UPGT_DEBUG_FW,
1133 			    "EEPROM end len=%d\n", option_len);
1134 			option_end = 1;
1135 			break;
1136 		case UPGT_EEPROM_TYPE_OFF:
1137 			DPRINTF(sc, UPGT_DEBUG_FW,
1138 			    "%s: EEPROM off without end option\n", __func__);
1139 			return (EIO);
1140 		default:
1141 			DPRINTF(sc, UPGT_DEBUG_FW,
1142 			    "EEPROM unknown type 0x%04x len=%d\n",
1143 			    option_type, option_len);
1144 			break;
1145 		}
1146 
1147 		/* jump to next EEPROM option */
1148 		eeprom_option = (struct upgt_eeprom_option *)
1149 		    (eeprom_option->data + option_len);
1150 	}
1151 	return (0);
1152 }
1153 
1154 static void
1155 upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1156 {
1157 	struct upgt_eeprom_freq3_header *freq3_header;
1158 	struct upgt_lmac_freq3 *freq3;
1159 	int i;
1160 	int elements;
1161 	int flags;
1162 	unsigned channel;
1163 
1164 	freq3_header = (struct upgt_eeprom_freq3_header *)data;
1165 	freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1166 
1167 	flags = freq3_header->flags;
1168 	elements = freq3_header->elements;
1169 
1170 	DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d\n",
1171 	    flags, elements);
1172 
1173 	if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq3[0])))
1174 		return;
1175 
1176 	for (i = 0; i < elements; i++) {
1177 		channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0);
1178 		if (channel >= IEEE80211_CHAN_MAX)
1179 			continue;
1180 
1181 		sc->sc_eeprom_freq3[channel] = freq3[i];
1182 
1183 		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1184 		    le16toh(sc->sc_eeprom_freq3[channel].freq), channel);
1185 	}
1186 }
1187 
1188 void
1189 upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1190 {
1191 	struct upgt_eeprom_freq4_header *freq4_header;
1192 	struct upgt_eeprom_freq4_1 *freq4_1;
1193 	struct upgt_eeprom_freq4_2 *freq4_2;
1194 	int i;
1195 	int j;
1196 	int elements;
1197 	int settings;
1198 	int flags;
1199 	unsigned channel;
1200 
1201 	freq4_header = (struct upgt_eeprom_freq4_header *)data;
1202 	freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1203 	flags = freq4_header->flags;
1204 	elements = freq4_header->elements;
1205 	settings = freq4_header->settings;
1206 
1207 	/* we need this value later */
1208 	sc->sc_eeprom_freq6_settings = freq4_header->settings;
1209 
1210 	DPRINTF(sc, UPGT_DEBUG_FW, "flags=0x%02x elements=%d settings=%d\n",
1211 	    flags, elements, settings);
1212 
1213 	if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq4_1[0])))
1214 		return;
1215 
1216 	for (i = 0; i < elements; i++) {
1217 		channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0);
1218 		if (channel >= IEEE80211_CHAN_MAX)
1219 			continue;
1220 
1221 		freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1222 		for (j = 0; j < settings; j++) {
1223 			sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1224 			sc->sc_eeprom_freq4[channel][j].pad = 0;
1225 		}
1226 
1227 		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1228 		    le16toh(freq4_1[i].freq), channel);
1229 	}
1230 }
1231 
1232 void
1233 upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1234 {
1235 	struct upgt_lmac_freq6 *freq6;
1236 	int i;
1237 	int elements;
1238 	unsigned channel;
1239 
1240 	freq6 = (struct upgt_lmac_freq6 *)data;
1241 	elements = len / sizeof(struct upgt_lmac_freq6);
1242 
1243 	DPRINTF(sc, UPGT_DEBUG_FW, "elements=%d\n", elements);
1244 
1245 	if (elements >= (int)(UPGT_EEPROM_SIZE / sizeof(freq6[0])))
1246 		return;
1247 
1248 	for (i = 0; i < elements; i++) {
1249 		channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0);
1250 		if (channel >= IEEE80211_CHAN_MAX)
1251 			continue;
1252 
1253 		sc->sc_eeprom_freq6[channel] = freq6[i];
1254 
1255 		DPRINTF(sc, UPGT_DEBUG_FW, "frequence=%d, channel=%d\n",
1256 		    le16toh(sc->sc_eeprom_freq6[channel].freq), channel);
1257 	}
1258 }
1259 
1260 static void
1261 upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1262 {
1263 	struct upgt_eeprom_option_hwrx *option_hwrx;
1264 
1265 	option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1266 
1267 	sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1268 
1269 	DPRINTF(sc, UPGT_DEBUG_FW, "hwrx option value=0x%04x\n",
1270 	    sc->sc_eeprom_hwrx);
1271 }
1272 
1273 static int
1274 upgt_eeprom_read(struct upgt_softc *sc)
1275 {
1276 	struct upgt_data *data_cmd;
1277 	struct upgt_lmac_mem *mem;
1278 	struct upgt_lmac_eeprom	*eeprom;
1279 	int block, error, offset;
1280 
1281 	UPGT_LOCK(sc);
1282 	usb_pause_mtx(&sc->sc_mtx, 100);
1283 
1284 	offset = 0;
1285 	block = UPGT_EEPROM_BLOCK_SIZE;
1286 	while (offset < UPGT_EEPROM_SIZE) {
1287 		DPRINTF(sc, UPGT_DEBUG_FW,
1288 		    "request EEPROM block (offset=%d, len=%d)\n", offset, block);
1289 
1290 		data_cmd = upgt_getbuf(sc);
1291 		if (data_cmd == NULL) {
1292 			UPGT_UNLOCK(sc);
1293 			return (ENOBUFS);
1294 		}
1295 
1296 		/*
1297 		 * Transmit the URB containing the CMD data.
1298 		 */
1299 		memset(data_cmd->buf, 0, MCLBYTES);
1300 
1301 		mem = (struct upgt_lmac_mem *)data_cmd->buf;
1302 		mem->addr = htole32(sc->sc_memaddr_frame_start +
1303 		    UPGT_MEMSIZE_FRAME_HEAD);
1304 
1305 		eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
1306 		eeprom->header1.flags = 0;
1307 		eeprom->header1.type = UPGT_H1_TYPE_CTRL;
1308 		eeprom->header1.len = htole16((
1309 		    sizeof(struct upgt_lmac_eeprom) -
1310 		    sizeof(struct upgt_lmac_header)) + block);
1311 
1312 		eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1313 		eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
1314 		eeprom->header2.flags = 0;
1315 
1316 		eeprom->offset = htole16(offset);
1317 		eeprom->len = htole16(block);
1318 
1319 		data_cmd->buflen = sizeof(*mem) + sizeof(*eeprom) + block;
1320 
1321 		mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
1322 		    data_cmd->buflen - sizeof(*mem));
1323 		upgt_bulk_tx(sc, data_cmd);
1324 
1325 		error = mtx_sleep(sc, &sc->sc_mtx, 0, "eeprom_request", hz);
1326 		if (error != 0) {
1327 			device_printf(sc->sc_dev,
1328 			    "timeout while waiting for EEPROM data\n");
1329 			UPGT_UNLOCK(sc);
1330 			return (EIO);
1331 		}
1332 
1333 		offset += block;
1334 		if (UPGT_EEPROM_SIZE - offset < block)
1335 			block = UPGT_EEPROM_SIZE - offset;
1336 	}
1337 
1338 	UPGT_UNLOCK(sc);
1339 	return (0);
1340 }
1341 
1342 /*
1343  * When a rx data came in the function returns a mbuf and a rssi values.
1344  */
1345 static struct mbuf *
1346 upgt_rxeof(struct usb_xfer *xfer, struct upgt_data *data, int *rssi)
1347 {
1348 	struct mbuf *m = NULL;
1349 	struct upgt_softc *sc = usbd_xfer_softc(xfer);
1350 	struct upgt_lmac_header *header;
1351 	struct upgt_lmac_eeprom *eeprom;
1352 	uint8_t h1_type;
1353 	uint16_t h2_type;
1354 	int actlen, sumlen;
1355 
1356 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1357 
1358 	UPGT_ASSERT_LOCKED(sc);
1359 
1360 	if (actlen < 1)
1361 		return (NULL);
1362 
1363 	/* Check only at the very beginning.  */
1364 	if (!(sc->sc_flags & UPGT_FLAG_FWLOADED) &&
1365 	    (memcmp(data->buf, "OK", 2) == 0)) {
1366 		sc->sc_flags |= UPGT_FLAG_FWLOADED;
1367 		wakeup_one(sc);
1368 		return (NULL);
1369 	}
1370 
1371 	if (actlen < (int)UPGT_RX_MINSZ)
1372 		return (NULL);
1373 
1374 	/*
1375 	 * Check what type of frame came in.
1376 	 */
1377 	header = (struct upgt_lmac_header *)(data->buf + 4);
1378 
1379 	h1_type = header->header1.type;
1380 	h2_type = le16toh(header->header2.type);
1381 
1382 	if (h1_type == UPGT_H1_TYPE_CTRL && h2_type == UPGT_H2_TYPE_EEPROM) {
1383 		eeprom = (struct upgt_lmac_eeprom *)(data->buf + 4);
1384 		uint16_t eeprom_offset = le16toh(eeprom->offset);
1385 		uint16_t eeprom_len = le16toh(eeprom->len);
1386 
1387 		DPRINTF(sc, UPGT_DEBUG_FW,
1388 		    "received EEPROM block (offset=%d, len=%d)\n",
1389 		    eeprom_offset, eeprom_len);
1390 
1391 		memcpy(sc->sc_eeprom + eeprom_offset,
1392 		    data->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1393 		    eeprom_len);
1394 
1395 		/* EEPROM data has arrived in time, wakeup.  */
1396 		wakeup(sc);
1397 	} else if (h1_type == UPGT_H1_TYPE_CTRL &&
1398 	    h2_type == UPGT_H2_TYPE_TX_DONE) {
1399 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: received 802.11 TX done\n",
1400 		    __func__);
1401 		upgt_tx_done(sc, data->buf + 4);
1402 	} else if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1403 	    h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1404 		DPRINTF(sc, UPGT_DEBUG_RECV, "%s: received 802.11 RX data\n",
1405 		    __func__);
1406 		m = upgt_rx(sc, data->buf + 4, le16toh(header->header1.len),
1407 		    rssi);
1408 	} else if (h1_type == UPGT_H1_TYPE_CTRL &&
1409 	    h2_type == UPGT_H2_TYPE_STATS) {
1410 		DPRINTF(sc, UPGT_DEBUG_STAT, "%s: received statistic data\n",
1411 		    __func__);
1412 		/* TODO: what could we do with the statistic data? */
1413 	} else {
1414 		/* ignore unknown frame types */
1415 		DPRINTF(sc, UPGT_DEBUG_INTR,
1416 		    "received unknown frame type 0x%02x\n",
1417 		    header->header1.type);
1418 	}
1419 	return (m);
1420 }
1421 
1422 /*
1423  * The firmware awaits a checksum for each frame we send to it.
1424  * The algorithm used therefor is uncommon but somehow similar to CRC32.
1425  */
1426 static uint32_t
1427 upgt_chksum_le(const uint32_t *buf, size_t size)
1428 {
1429 	size_t i;
1430 	uint32_t crc = 0;
1431 
1432 	for (i = 0; i < size; i += sizeof(uint32_t)) {
1433 		crc = htole32(crc ^ *buf++);
1434 		crc = htole32((crc >> 5) ^ (crc << 3));
1435 	}
1436 
1437 	return (crc);
1438 }
1439 
1440 static struct mbuf *
1441 upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen, int *rssi)
1442 {
1443 	struct ieee80211com *ic = &sc->sc_ic;
1444 	struct upgt_lmac_rx_desc *rxdesc;
1445 	struct mbuf *m;
1446 
1447 	/*
1448 	 * don't pass packets to the ieee80211 framework if the driver isn't
1449 	 * RUNNING.
1450 	 */
1451 	if (!(sc->sc_flags & UPGT_FLAG_INITDONE))
1452 		return (NULL);
1453 
1454 	/* access RX packet descriptor */
1455 	rxdesc = (struct upgt_lmac_rx_desc *)data;
1456 
1457 	/* create mbuf which is suitable for strict alignment archs */
1458 	KASSERT((pkglen + ETHER_ALIGN) < MCLBYTES,
1459 	    ("A current mbuf storage is small (%d)", pkglen + ETHER_ALIGN));
1460 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1461 	if (m == NULL) {
1462 		device_printf(sc->sc_dev, "could not create RX mbuf\n");
1463 		return (NULL);
1464 	}
1465 	m_adj(m, ETHER_ALIGN);
1466 	memcpy(mtod(m, char *), rxdesc->data, pkglen);
1467 	/* trim FCS */
1468 	m->m_len = m->m_pkthdr.len = pkglen - IEEE80211_CRC_LEN;
1469 
1470 	if (ieee80211_radiotap_active(ic)) {
1471 		struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1472 
1473 		tap->wr_flags = 0;
1474 		tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1475 		tap->wr_antsignal = rxdesc->rssi;
1476 	}
1477 
1478 	DPRINTF(sc, UPGT_DEBUG_RX_PROC, "%s: RX done\n", __func__);
1479 	*rssi = rxdesc->rssi;
1480 	return (m);
1481 }
1482 
1483 static uint8_t
1484 upgt_rx_rate(struct upgt_softc *sc, const int rate)
1485 {
1486 	struct ieee80211com *ic = &sc->sc_ic;
1487 	static const uint8_t cck_upgt2rate[4] = { 2, 4, 11, 22 };
1488 	static const uint8_t ofdm_upgt2rate[12] =
1489 	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
1490 
1491 	if (ic->ic_curmode == IEEE80211_MODE_11B &&
1492 	    !(rate < 0 || rate > 3))
1493 		return cck_upgt2rate[rate & 0xf];
1494 
1495 	if (ic->ic_curmode == IEEE80211_MODE_11G &&
1496 	    !(rate < 0 || rate > 11))
1497 		return ofdm_upgt2rate[rate & 0xf];
1498 
1499 	return (0);
1500 }
1501 
1502 static void
1503 upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1504 {
1505 	struct upgt_lmac_tx_done_desc *desc;
1506 	int i, freed = 0;
1507 
1508 	UPGT_ASSERT_LOCKED(sc);
1509 
1510 	desc = (struct upgt_lmac_tx_done_desc *)data;
1511 
1512 	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1513 		struct upgt_data *data_tx = &sc->sc_tx_data[i];
1514 
1515 		if (data_tx->addr == le32toh(desc->header2.reqid)) {
1516 			upgt_mem_free(sc, data_tx->addr);
1517 			data_tx->ni = NULL;
1518 			data_tx->addr = 0;
1519 			data_tx->m = NULL;
1520 
1521 			DPRINTF(sc, UPGT_DEBUG_TX_PROC,
1522 			    "TX done: memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1523 			    le32toh(desc->header2.reqid),
1524 			    le16toh(desc->status), le16toh(desc->rssi));
1525 			DPRINTF(sc, UPGT_DEBUG_TX_PROC, "seq=%d\n",
1526 			    le16toh(desc->seq));
1527 
1528 			freed++;
1529 		}
1530 	}
1531 
1532 	if (freed != 0) {
1533 		UPGT_UNLOCK(sc);
1534 		sc->sc_tx_timer = 0;
1535 		upgt_start(sc);
1536 		UPGT_LOCK(sc);
1537 	}
1538 }
1539 
1540 static void
1541 upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
1542 {
1543 	int i;
1544 
1545 	for (i = 0; i < sc->sc_memory.pages; i++) {
1546 		if (sc->sc_memory.page[i].addr == addr) {
1547 			sc->sc_memory.page[i].used = 0;
1548 			return;
1549 		}
1550 	}
1551 
1552 	device_printf(sc->sc_dev,
1553 	    "could not free memory address 0x%08x\n", addr);
1554 }
1555 
1556 static int
1557 upgt_fw_load(struct upgt_softc *sc)
1558 {
1559 	const struct firmware *fw;
1560 	struct upgt_data *data_cmd;
1561 	struct upgt_fw_x2_header *x2;
1562 	char start_fwload_cmd[] = { 0x3c, 0x0d };
1563 	int error = 0;
1564 	size_t offset;
1565 	int bsize;
1566 	int n;
1567 	uint32_t crc32;
1568 
1569 	fw = firmware_get(upgt_fwname);
1570 	if (fw == NULL) {
1571 		device_printf(sc->sc_dev, "could not read microcode %s\n",
1572 		    upgt_fwname);
1573 		return (EIO);
1574 	}
1575 
1576 	UPGT_LOCK(sc);
1577 
1578 	/* send firmware start load command */
1579 	data_cmd = upgt_getbuf(sc);
1580 	if (data_cmd == NULL) {
1581 		error = ENOBUFS;
1582 		goto fail;
1583 	}
1584 	data_cmd->buflen = sizeof(start_fwload_cmd);
1585 	memcpy(data_cmd->buf, start_fwload_cmd, data_cmd->buflen);
1586 	upgt_bulk_tx(sc, data_cmd);
1587 
1588 	/* send X2 header */
1589 	data_cmd = upgt_getbuf(sc);
1590 	if (data_cmd == NULL) {
1591 		error = ENOBUFS;
1592 		goto fail;
1593 	}
1594 	data_cmd->buflen = sizeof(struct upgt_fw_x2_header);
1595 	x2 = (struct upgt_fw_x2_header *)data_cmd->buf;
1596 	memcpy(x2->signature, UPGT_X2_SIGNATURE, UPGT_X2_SIGNATURE_SIZE);
1597 	x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
1598 	x2->len = htole32(fw->datasize);
1599 	x2->crc = upgt_crc32_le((uint8_t *)data_cmd->buf +
1600 	    UPGT_X2_SIGNATURE_SIZE,
1601 	    sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
1602 	    sizeof(uint32_t));
1603 	upgt_bulk_tx(sc, data_cmd);
1604 
1605 	/* download firmware */
1606 	for (offset = 0; offset < fw->datasize; offset += bsize) {
1607 		if (fw->datasize - offset > UPGT_FW_BLOCK_SIZE)
1608 			bsize = UPGT_FW_BLOCK_SIZE;
1609 		else
1610 			bsize = fw->datasize - offset;
1611 
1612 		data_cmd = upgt_getbuf(sc);
1613 		if (data_cmd == NULL) {
1614 			error = ENOBUFS;
1615 			goto fail;
1616 		}
1617 		n = upgt_fw_copy((const uint8_t *)fw->data + offset,
1618 		    data_cmd->buf, bsize);
1619 		data_cmd->buflen = bsize;
1620 		upgt_bulk_tx(sc, data_cmd);
1621 
1622 		DPRINTF(sc, UPGT_DEBUG_FW, "FW offset=%d, read=%d, sent=%d\n",
1623 		    offset, n, bsize);
1624 		bsize = n;
1625 	}
1626 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware downloaded\n", __func__);
1627 
1628 	/* load firmware */
1629 	data_cmd = upgt_getbuf(sc);
1630 	if (data_cmd == NULL) {
1631 		error = ENOBUFS;
1632 		goto fail;
1633 	}
1634 	crc32 = upgt_crc32_le(fw->data, fw->datasize);
1635 	*((uint32_t *)(data_cmd->buf)    ) = crc32;
1636 	*((uint8_t  *)(data_cmd->buf) + 4) = 'g';
1637 	*((uint8_t  *)(data_cmd->buf) + 5) = '\r';
1638 	data_cmd->buflen = 6;
1639 	upgt_bulk_tx(sc, data_cmd);
1640 
1641 	/* waiting 'OK' response.  */
1642 	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_RX]);
1643 	error = mtx_sleep(sc, &sc->sc_mtx, 0, "upgtfw", 2 * hz);
1644 	if (error != 0) {
1645 		device_printf(sc->sc_dev, "firmware load failed\n");
1646 		error = EIO;
1647 	}
1648 
1649 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware loaded\n", __func__);
1650 fail:
1651 	UPGT_UNLOCK(sc);
1652 	firmware_put(fw, FIRMWARE_UNLOAD);
1653 	return (error);
1654 }
1655 
1656 static uint32_t
1657 upgt_crc32_le(const void *buf, size_t size)
1658 {
1659 	uint32_t crc;
1660 
1661 	crc = ether_crc32_le(buf, size);
1662 
1663 	/* apply final XOR value as common for CRC-32 */
1664 	crc = htole32(crc ^ 0xffffffffU);
1665 
1666 	return (crc);
1667 }
1668 
1669 /*
1670  * While copying the version 2 firmware, we need to replace two characters:
1671  *
1672  * 0x7e -> 0x7d 0x5e
1673  * 0x7d -> 0x7d 0x5d
1674  */
1675 static int
1676 upgt_fw_copy(const uint8_t *src, char *dst, int size)
1677 {
1678 	int i, j;
1679 
1680 	for (i = 0, j = 0; i < size && j < size; i++) {
1681 		switch (src[i]) {
1682 		case 0x7e:
1683 			dst[j] = 0x7d;
1684 			j++;
1685 			dst[j] = 0x5e;
1686 			j++;
1687 			break;
1688 		case 0x7d:
1689 			dst[j] = 0x7d;
1690 			j++;
1691 			dst[j] = 0x5d;
1692 			j++;
1693 			break;
1694 		default:
1695 			dst[j] = src[i];
1696 			j++;
1697 			break;
1698 		}
1699 	}
1700 
1701 	return (i);
1702 }
1703 
1704 static int
1705 upgt_mem_init(struct upgt_softc *sc)
1706 {
1707 	int i;
1708 
1709 	for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
1710 		sc->sc_memory.page[i].used = 0;
1711 
1712 		if (i == 0) {
1713 			/*
1714 			 * The first memory page is always reserved for
1715 			 * command data.
1716 			 */
1717 			sc->sc_memory.page[i].addr =
1718 			    sc->sc_memaddr_frame_start + MCLBYTES;
1719 		} else {
1720 			sc->sc_memory.page[i].addr =
1721 			    sc->sc_memory.page[i - 1].addr + MCLBYTES;
1722 		}
1723 
1724 		if (sc->sc_memory.page[i].addr + MCLBYTES >=
1725 		    sc->sc_memaddr_frame_end)
1726 			break;
1727 
1728 		DPRINTF(sc, UPGT_DEBUG_FW, "memory address page %d=0x%08x\n",
1729 		    i, sc->sc_memory.page[i].addr);
1730 	}
1731 
1732 	sc->sc_memory.pages = i;
1733 
1734 	DPRINTF(sc, UPGT_DEBUG_FW, "memory pages=%d\n", sc->sc_memory.pages);
1735 	return (0);
1736 }
1737 
1738 static int
1739 upgt_fw_verify(struct upgt_softc *sc)
1740 {
1741 	const struct firmware *fw;
1742 	const struct upgt_fw_bra_option *bra_opt;
1743 	const struct upgt_fw_bra_descr *descr;
1744 	const uint8_t *p;
1745 	const uint32_t *uc;
1746 	uint32_t bra_option_type, bra_option_len;
1747 	size_t offset;
1748 	int bra_end = 0;
1749 	int error = 0;
1750 
1751 	fw = firmware_get(upgt_fwname);
1752 	if (fw == NULL) {
1753 		device_printf(sc->sc_dev, "could not read microcode %s\n",
1754 		    upgt_fwname);
1755 		return EIO;
1756 	}
1757 
1758 	/*
1759 	 * Seek to beginning of Boot Record Area (BRA).
1760 	 */
1761 	for (offset = 0; offset < fw->datasize; offset += sizeof(*uc)) {
1762 		uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1763 		if (*uc == 0)
1764 			break;
1765 	}
1766 	for (; offset < fw->datasize; offset += sizeof(*uc)) {
1767 		uc = (const uint32_t *)((const uint8_t *)fw->data + offset);
1768 		if (*uc != 0)
1769 			break;
1770 	}
1771 	if (offset == fw->datasize) {
1772 		device_printf(sc->sc_dev,
1773 		    "firmware Boot Record Area not found\n");
1774 		error = EIO;
1775 		goto fail;
1776 	}
1777 
1778 	DPRINTF(sc, UPGT_DEBUG_FW,
1779 	    "firmware Boot Record Area found at offset %d\n", offset);
1780 
1781 	/*
1782 	 * Parse Boot Record Area (BRA) options.
1783 	 */
1784 	while (offset < fw->datasize && bra_end == 0) {
1785 		/* get current BRA option */
1786 		p = (const uint8_t *)fw->data + offset;
1787 		bra_opt = (const struct upgt_fw_bra_option *)p;
1788 		bra_option_type = le32toh(bra_opt->type);
1789 		bra_option_len = le32toh(bra_opt->len) * sizeof(*uc);
1790 
1791 		switch (bra_option_type) {
1792 		case UPGT_BRA_TYPE_FW:
1793 			DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_FW len=%d\n",
1794 			    bra_option_len);
1795 
1796 			if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
1797 				device_printf(sc->sc_dev,
1798 				    "wrong UPGT_BRA_TYPE_FW len\n");
1799 				error = EIO;
1800 				goto fail;
1801 			}
1802 			if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_opt->data,
1803 			    bra_option_len) == 0) {
1804 				sc->sc_fw_type = UPGT_FWTYPE_LM86;
1805 				break;
1806 			}
1807 			if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_opt->data,
1808 			    bra_option_len) == 0) {
1809 				sc->sc_fw_type = UPGT_FWTYPE_LM87;
1810 				break;
1811 			}
1812 			device_printf(sc->sc_dev,
1813 			    "unsupported firmware type\n");
1814 			error = EIO;
1815 			goto fail;
1816 		case UPGT_BRA_TYPE_VERSION:
1817 			DPRINTF(sc, UPGT_DEBUG_FW,
1818 			    "UPGT_BRA_TYPE_VERSION len=%d\n", bra_option_len);
1819 			break;
1820 		case UPGT_BRA_TYPE_DEPIF:
1821 			DPRINTF(sc, UPGT_DEBUG_FW,
1822 			    "UPGT_BRA_TYPE_DEPIF len=%d\n", bra_option_len);
1823 			break;
1824 		case UPGT_BRA_TYPE_EXPIF:
1825 			DPRINTF(sc, UPGT_DEBUG_FW,
1826 			    "UPGT_BRA_TYPE_EXPIF len=%d\n", bra_option_len);
1827 			break;
1828 		case UPGT_BRA_TYPE_DESCR:
1829 			DPRINTF(sc, UPGT_DEBUG_FW,
1830 			    "UPGT_BRA_TYPE_DESCR len=%d\n", bra_option_len);
1831 
1832 			descr = (const struct upgt_fw_bra_descr *)bra_opt->data;
1833 
1834 			sc->sc_memaddr_frame_start =
1835 			    le32toh(descr->memaddr_space_start);
1836 			sc->sc_memaddr_frame_end =
1837 			    le32toh(descr->memaddr_space_end);
1838 
1839 			DPRINTF(sc, UPGT_DEBUG_FW,
1840 			    "memory address space start=0x%08x\n",
1841 			    sc->sc_memaddr_frame_start);
1842 			DPRINTF(sc, UPGT_DEBUG_FW,
1843 			    "memory address space end=0x%08x\n",
1844 			    sc->sc_memaddr_frame_end);
1845 			break;
1846 		case UPGT_BRA_TYPE_END:
1847 			DPRINTF(sc, UPGT_DEBUG_FW, "UPGT_BRA_TYPE_END len=%d\n",
1848 			    bra_option_len);
1849 			bra_end = 1;
1850 			break;
1851 		default:
1852 			DPRINTF(sc, UPGT_DEBUG_FW, "unknown BRA option len=%d\n",
1853 			    bra_option_len);
1854 			error = EIO;
1855 			goto fail;
1856 		}
1857 
1858 		/* jump to next BRA option */
1859 		offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
1860 	}
1861 
1862 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: firmware verified", __func__);
1863 fail:
1864 	firmware_put(fw, FIRMWARE_UNLOAD);
1865 	return (error);
1866 }
1867 
1868 static void
1869 upgt_bulk_tx(struct upgt_softc *sc, struct upgt_data *data)
1870 {
1871 
1872 	UPGT_ASSERT_LOCKED(sc);
1873 
1874 	STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1875 	UPGT_STAT_INC(sc, st_tx_pending);
1876 	usbd_transfer_start(sc->sc_xfer[UPGT_BULK_TX]);
1877 }
1878 
1879 static int
1880 upgt_device_reset(struct upgt_softc *sc)
1881 {
1882 	struct upgt_data *data;
1883 	char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
1884 
1885 	UPGT_LOCK(sc);
1886 
1887 	data = upgt_getbuf(sc);
1888 	if (data == NULL) {
1889 		UPGT_UNLOCK(sc);
1890 		return (ENOBUFS);
1891 	}
1892 	memcpy(data->buf, init_cmd, sizeof(init_cmd));
1893 	data->buflen = sizeof(init_cmd);
1894 	upgt_bulk_tx(sc, data);
1895 	usb_pause_mtx(&sc->sc_mtx, 100);
1896 
1897 	UPGT_UNLOCK(sc);
1898 	DPRINTF(sc, UPGT_DEBUG_FW, "%s: device initialized\n", __func__);
1899 	return (0);
1900 }
1901 
1902 static int
1903 upgt_alloc_tx(struct upgt_softc *sc)
1904 {
1905 	int i;
1906 
1907 	STAILQ_INIT(&sc->sc_tx_active);
1908 	STAILQ_INIT(&sc->sc_tx_inactive);
1909 	STAILQ_INIT(&sc->sc_tx_pending);
1910 
1911 	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
1912 		struct upgt_data *data = &sc->sc_tx_data[i];
1913 		data->buf = ((uint8_t *)sc->sc_tx_dma_buf) + (i * MCLBYTES);
1914 		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1915 		UPGT_STAT_INC(sc, st_tx_inactive);
1916 	}
1917 
1918 	return (0);
1919 }
1920 
1921 static int
1922 upgt_alloc_rx(struct upgt_softc *sc)
1923 {
1924 	int i;
1925 
1926 	STAILQ_INIT(&sc->sc_rx_active);
1927 	STAILQ_INIT(&sc->sc_rx_inactive);
1928 
1929 	for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1930 		struct upgt_data *data = &sc->sc_rx_data[i];
1931 		data->buf = ((uint8_t *)sc->sc_rx_dma_buf) + (i * MCLBYTES);
1932 		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1933 	}
1934 	return (0);
1935 }
1936 
1937 static int
1938 upgt_detach(device_t dev)
1939 {
1940 	struct upgt_softc *sc = device_get_softc(dev);
1941 	struct ieee80211com *ic = &sc->sc_ic;
1942 	unsigned int x;
1943 
1944 	/*
1945 	 * Prevent further allocations from RX/TX/CMD
1946 	 * data lists and ioctls
1947 	 */
1948 	UPGT_LOCK(sc);
1949 	sc->sc_flags |= UPGT_FLAG_DETACHED;
1950 
1951 	STAILQ_INIT(&sc->sc_tx_active);
1952 	STAILQ_INIT(&sc->sc_tx_inactive);
1953 	STAILQ_INIT(&sc->sc_tx_pending);
1954 
1955 	STAILQ_INIT(&sc->sc_rx_active);
1956 	STAILQ_INIT(&sc->sc_rx_inactive);
1957 
1958 	upgt_stop(sc);
1959 	UPGT_UNLOCK(sc);
1960 
1961 	callout_drain(&sc->sc_led_ch);
1962 	callout_drain(&sc->sc_watchdog_ch);
1963 
1964 	/* drain USB transfers */
1965 	for (x = 0; x != UPGT_N_XFERS; x++)
1966 		usbd_transfer_drain(sc->sc_xfer[x]);
1967 
1968 	/* free data buffers */
1969 	UPGT_LOCK(sc);
1970 	upgt_free_rx(sc);
1971 	upgt_free_tx(sc);
1972 	UPGT_UNLOCK(sc);
1973 
1974 	/* free USB transfers and some data buffers */
1975 	usbd_transfer_unsetup(sc->sc_xfer, UPGT_N_XFERS);
1976 
1977 	ieee80211_ifdetach(ic);
1978 	mbufq_drain(&sc->sc_snd);
1979 	mtx_destroy(&sc->sc_mtx);
1980 
1981 	return (0);
1982 }
1983 
1984 static void
1985 upgt_free_rx(struct upgt_softc *sc)
1986 {
1987 	int i;
1988 
1989 	for (i = 0; i < UPGT_RX_MAXCOUNT; i++) {
1990 		struct upgt_data *data = &sc->sc_rx_data[i];
1991 
1992 		data->buf = NULL;
1993 		data->ni = NULL;
1994 	}
1995 }
1996 
1997 static void
1998 upgt_free_tx(struct upgt_softc *sc)
1999 {
2000 	int i;
2001 
2002 	for (i = 0; i < UPGT_TX_MAXCOUNT; i++) {
2003 		struct upgt_data *data = &sc->sc_tx_data[i];
2004 
2005 		if (data->ni != NULL)
2006 			ieee80211_free_node(data->ni);
2007 
2008 		data->buf = NULL;
2009 		data->ni = NULL;
2010 	}
2011 }
2012 
2013 static void
2014 upgt_abort_xfers_locked(struct upgt_softc *sc)
2015 {
2016 	int i;
2017 
2018 	UPGT_ASSERT_LOCKED(sc);
2019 	/* abort any pending transfers */
2020 	for (i = 0; i < UPGT_N_XFERS; i++)
2021 		usbd_transfer_stop(sc->sc_xfer[i]);
2022 }
2023 
2024 static void
2025 upgt_abort_xfers(struct upgt_softc *sc)
2026 {
2027 
2028 	UPGT_LOCK(sc);
2029 	upgt_abort_xfers_locked(sc);
2030 	UPGT_UNLOCK(sc);
2031 }
2032 
2033 #define	UPGT_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2034 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2035 
2036 static void
2037 upgt_sysctl_node(struct upgt_softc *sc)
2038 {
2039 	struct sysctl_ctx_list *ctx;
2040 	struct sysctl_oid_list *child;
2041 	struct sysctl_oid *tree;
2042 	struct upgt_stat *stats;
2043 
2044 	stats = &sc->sc_stat;
2045 	ctx = device_get_sysctl_ctx(sc->sc_dev);
2046 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev));
2047 
2048 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2049 	    NULL, "UPGT statistics");
2050 	child = SYSCTL_CHILDREN(tree);
2051 	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_active",
2052 	    &stats->st_tx_active, "Active numbers in TX queue");
2053 	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive",
2054 	    &stats->st_tx_inactive, "Inactive numbers in TX queue");
2055 	UPGT_SYSCTL_STAT_ADD32(ctx, child, "tx_pending",
2056 	    &stats->st_tx_pending, "Pending numbers in TX queue");
2057 }
2058 
2059 #undef UPGT_SYSCTL_STAT_ADD32
2060 
2061 static struct upgt_data *
2062 _upgt_getbuf(struct upgt_softc *sc)
2063 {
2064 	struct upgt_data *bf;
2065 
2066 	bf = STAILQ_FIRST(&sc->sc_tx_inactive);
2067 	if (bf != NULL) {
2068 		STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
2069 		UPGT_STAT_DEC(sc, st_tx_inactive);
2070 	} else
2071 		bf = NULL;
2072 	if (bf == NULL)
2073 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: %s\n", __func__,
2074 		    "out of xmit buffers");
2075 	return (bf);
2076 }
2077 
2078 static struct upgt_data *
2079 upgt_getbuf(struct upgt_softc *sc)
2080 {
2081 	struct upgt_data *bf;
2082 
2083 	UPGT_ASSERT_LOCKED(sc);
2084 
2085 	bf = _upgt_getbuf(sc);
2086 	if (bf == NULL)
2087 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: stop queue\n", __func__);
2088 
2089 	return (bf);
2090 }
2091 
2092 static struct upgt_data *
2093 upgt_gettxbuf(struct upgt_softc *sc)
2094 {
2095 	struct upgt_data *bf;
2096 
2097 	UPGT_ASSERT_LOCKED(sc);
2098 
2099 	bf = upgt_getbuf(sc);
2100 	if (bf == NULL)
2101 		return (NULL);
2102 
2103 	bf->addr = upgt_mem_alloc(sc);
2104 	if (bf->addr == 0) {
2105 		DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: no free prism memory!\n",
2106 		    __func__);
2107 		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
2108 		UPGT_STAT_INC(sc, st_tx_inactive);
2109 		return (NULL);
2110 	}
2111 	return (bf);
2112 }
2113 
2114 static int
2115 upgt_tx_start(struct upgt_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2116     struct upgt_data *data)
2117 {
2118 	struct ieee80211vap *vap = ni->ni_vap;
2119 	int error = 0, len;
2120 	struct ieee80211_frame *wh;
2121 	struct ieee80211_key *k;
2122 	struct upgt_lmac_mem *mem;
2123 	struct upgt_lmac_tx_desc *txdesc;
2124 
2125 	UPGT_ASSERT_LOCKED(sc);
2126 
2127 	upgt_set_led(sc, UPGT_LED_BLINK);
2128 
2129 	/*
2130 	 * Software crypto.
2131 	 */
2132 	wh = mtod(m, struct ieee80211_frame *);
2133 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2134 		k = ieee80211_crypto_encap(ni, m);
2135 		if (k == NULL) {
2136 			device_printf(sc->sc_dev,
2137 			    "ieee80211_crypto_encap returns NULL.\n");
2138 			error = EIO;
2139 			goto done;
2140 		}
2141 
2142 		/* in case packet header moved, reset pointer */
2143 		wh = mtod(m, struct ieee80211_frame *);
2144 	}
2145 
2146 	/* Transmit the URB containing the TX data.  */
2147 	memset(data->buf, 0, MCLBYTES);
2148 	mem = (struct upgt_lmac_mem *)data->buf;
2149 	mem->addr = htole32(data->addr);
2150 	txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
2151 
2152 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2153 	    IEEE80211_FC0_TYPE_MGT) {
2154 		/* mgmt frames  */
2155 		txdesc->header1.flags = UPGT_H1_FLAGS_TX_MGMT;
2156 		/* always send mgmt frames at lowest rate (DS1) */
2157 		memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
2158 	} else {
2159 		/* data frames  */
2160 		txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
2161 		memcpy(txdesc->rates, sc->sc_cur_rateset, sizeof(txdesc->rates));
2162 	}
2163 	txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
2164 	txdesc->header1.len = htole16(m->m_pkthdr.len);
2165 	txdesc->header2.reqid = htole32(data->addr);
2166 	txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
2167 	txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
2168 	txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
2169 	txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
2170 
2171 	if (ieee80211_radiotap_active_vap(vap)) {
2172 		struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
2173 
2174 		tap->wt_flags = 0;
2175 		tap->wt_rate = 0;	/* XXX where to get from? */
2176 
2177 		ieee80211_radiotap_tx(vap, m);
2178 	}
2179 
2180 	/* copy frame below our TX descriptor header */
2181 	m_copydata(m, 0, m->m_pkthdr.len,
2182 	    data->buf + (sizeof(*mem) + sizeof(*txdesc)));
2183 	/* calculate frame size */
2184 	len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
2185 	/* we need to align the frame to a 4 byte boundary */
2186 	len = (len + 3) & ~3;
2187 	/* calculate frame checksum */
2188 	mem->chksum = upgt_chksum_le((uint32_t *)txdesc, len - sizeof(*mem));
2189 	data->ni = ni;
2190 	data->m = m;
2191 	data->buflen = len;
2192 
2193 	DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: TX start data sending (%d bytes)\n",
2194 	    __func__, len);
2195 	KASSERT(len <= MCLBYTES, ("mbuf is small for saving data"));
2196 
2197 	upgt_bulk_tx(sc, data);
2198 done:
2199 	/*
2200 	 * If we don't regulary read the device statistics, the RX queue
2201 	 * will stall.  It's strange, but it works, so we keep reading
2202 	 * the statistics here.  *shrug*
2203 	 */
2204 	if (!(vap->iv_ifp->if_get_counter(vap->iv_ifp, IFCOUNTER_OPACKETS) %
2205 	    UPGT_TX_STAT_INTERVAL))
2206 		upgt_get_stats(sc);
2207 
2208 	return (error);
2209 }
2210 
2211 static void
2212 upgt_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2213 {
2214 	struct upgt_softc *sc = usbd_xfer_softc(xfer);
2215 	struct ieee80211com *ic = &sc->sc_ic;
2216 	struct ieee80211_frame *wh;
2217 	struct ieee80211_node *ni;
2218 	struct mbuf *m = NULL;
2219 	struct upgt_data *data;
2220 	int8_t nf;
2221 	int rssi = -1;
2222 
2223 	UPGT_ASSERT_LOCKED(sc);
2224 
2225 	switch (USB_GET_STATE(xfer)) {
2226 	case USB_ST_TRANSFERRED:
2227 		data = STAILQ_FIRST(&sc->sc_rx_active);
2228 		if (data == NULL)
2229 			goto setup;
2230 		STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2231 		m = upgt_rxeof(xfer, data, &rssi);
2232 		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2233 		/* FALLTHROUGH */
2234 	case USB_ST_SETUP:
2235 setup:
2236 		data = STAILQ_FIRST(&sc->sc_rx_inactive);
2237 		if (data == NULL)
2238 			return;
2239 		STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2240 		STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2241 		usbd_xfer_set_frame_data(xfer, 0, data->buf, MCLBYTES);
2242 		usbd_transfer_submit(xfer);
2243 
2244 		/*
2245 		 * To avoid LOR we should unlock our private mutex here to call
2246 		 * ieee80211_input() because here is at the end of a USB
2247 		 * callback and safe to unlock.
2248 		 */
2249 		UPGT_UNLOCK(sc);
2250 		if (m != NULL) {
2251 			wh = mtod(m, struct ieee80211_frame *);
2252 			ni = ieee80211_find_rxnode(ic,
2253 			    (struct ieee80211_frame_min *)wh);
2254 			nf = -95;	/* XXX */
2255 			if (ni != NULL) {
2256 				(void) ieee80211_input(ni, m, rssi, nf);
2257 				/* node is no longer needed */
2258 				ieee80211_free_node(ni);
2259 			} else
2260 				(void) ieee80211_input_all(ic, m, rssi, nf);
2261 			m = NULL;
2262 		}
2263 		UPGT_LOCK(sc);
2264 		upgt_start(sc);
2265 		break;
2266 	default:
2267 		/* needs it to the inactive queue due to a error.  */
2268 		data = STAILQ_FIRST(&sc->sc_rx_active);
2269 		if (data != NULL) {
2270 			STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2271 			STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2272 		}
2273 		if (error != USB_ERR_CANCELLED) {
2274 			usbd_xfer_set_stall(xfer);
2275 			counter_u64_add(ic->ic_ierrors, 1);
2276 			goto setup;
2277 		}
2278 		break;
2279 	}
2280 }
2281 
2282 static void
2283 upgt_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
2284 {
2285 	struct upgt_softc *sc = usbd_xfer_softc(xfer);
2286 	struct upgt_data *data;
2287 
2288 	UPGT_ASSERT_LOCKED(sc);
2289 	switch (USB_GET_STATE(xfer)) {
2290 	case USB_ST_TRANSFERRED:
2291 		data = STAILQ_FIRST(&sc->sc_tx_active);
2292 		if (data == NULL)
2293 			goto setup;
2294 		STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
2295 		UPGT_STAT_DEC(sc, st_tx_active);
2296 		upgt_txeof(xfer, data);
2297 		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
2298 		UPGT_STAT_INC(sc, st_tx_inactive);
2299 		/* FALLTHROUGH */
2300 	case USB_ST_SETUP:
2301 setup:
2302 		data = STAILQ_FIRST(&sc->sc_tx_pending);
2303 		if (data == NULL) {
2304 			DPRINTF(sc, UPGT_DEBUG_XMIT, "%s: empty pending queue\n",
2305 			    __func__);
2306 			return;
2307 		}
2308 		STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
2309 		UPGT_STAT_DEC(sc, st_tx_pending);
2310 		STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
2311 		UPGT_STAT_INC(sc, st_tx_active);
2312 
2313 		usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2314 		usbd_transfer_submit(xfer);
2315 		upgt_start(sc);
2316 		break;
2317 	default:
2318 		data = STAILQ_FIRST(&sc->sc_tx_active);
2319 		if (data == NULL)
2320 			goto setup;
2321 		if (data->ni != NULL) {
2322 			if_inc_counter(data->ni->ni_vap->iv_ifp,
2323 			    IFCOUNTER_OERRORS, 1);
2324 			ieee80211_free_node(data->ni);
2325 			data->ni = NULL;
2326 		}
2327 		if (error != USB_ERR_CANCELLED) {
2328 			usbd_xfer_set_stall(xfer);
2329 			goto setup;
2330 		}
2331 		break;
2332 	}
2333 }
2334 
2335 static device_method_t upgt_methods[] = {
2336         /* Device interface */
2337         DEVMETHOD(device_probe, upgt_match),
2338         DEVMETHOD(device_attach, upgt_attach),
2339         DEVMETHOD(device_detach, upgt_detach),
2340 	DEVMETHOD_END
2341 };
2342 
2343 static driver_t upgt_driver = {
2344 	.name = "upgt",
2345 	.methods = upgt_methods,
2346 	.size = sizeof(struct upgt_softc)
2347 };
2348 
2349 static devclass_t upgt_devclass;
2350 
2351 DRIVER_MODULE(if_upgt, uhub, upgt_driver, upgt_devclass, NULL, 0);
2352 MODULE_VERSION(if_upgt, 1);
2353 MODULE_DEPEND(if_upgt, usb, 1, 1, 1);
2354 MODULE_DEPEND(if_upgt, wlan, 1, 1, 1);
2355 MODULE_DEPEND(if_upgt, upgtfw_fw, 1, 1, 1);
2356