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