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