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