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