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