xref: /openbsd-src/sys/dev/usb/if_upgt.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: if_upgt.c,v 1.56 2011/07/03 15:47:17 matthew Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Marcus Glocker <mglocker@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "bpfilter.h"
20 
21 #include <sys/param.h>
22 #include <sys/sockio.h>
23 #include <sys/mbuf.h>
24 #include <sys/kernel.h>
25 #include <sys/socket.h>
26 #include <sys/systm.h>
27 #include <sys/timeout.h>
28 #include <sys/conf.h>
29 #include <sys/device.h>
30 
31 #include <machine/bus.h>
32 #include <machine/endian.h>
33 #include <machine/intr.h>
34 
35 #if NBPFILTER > 0
36 #include <net/bpf.h>
37 #endif
38 #include <net/if.h>
39 #include <net/if_arp.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/if_types.h>
43 
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in_var.h>
47 #include <netinet/if_ether.h>
48 #include <netinet/ip.h>
49 
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_radiotap.h>
52 
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdevs.h>
57 
58 #include <dev/usb/if_upgtvar.h>
59 
60 /*
61  * Driver for the USB PrismGT devices.
62  *
63  * For now just USB 2.0 devices with the GW3887 chipset are supported.
64  * The driver has been written based on the firmware version 2.13.1.0_LM87.
65  *
66  * TODO's:
67  * - Fix MONITOR mode (MAC filter).
68  * - Add HOSTAP mode.
69  * - Add IBSS mode.
70  * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
71  *
72  * Parts of this driver has been influenced by reading the p54u driver
73  * written by Jean-Baptiste Note <jean-baptiste.note@m4x.org> and
74  * Sebastien Bourdeauducq <lekernel@prism54.org>.
75  */
76 
77 #ifdef UPGT_DEBUG
78 int upgt_debug = 2;
79 #define DPRINTF(l, x...) do { if ((l) <= upgt_debug) printf(x); } while (0)
80 #else
81 #define DPRINTF(l, x...)
82 #endif
83 
84 /*
85  * Prototypes.
86  */
87 int		upgt_match(struct device *, void *, void *);
88 void		upgt_attach(struct device *, struct device *, void *);
89 void		upgt_attach_hook(void *);
90 int		upgt_detach(struct device *, int);
91 int		upgt_activate(struct device *, int);
92 
93 int		upgt_device_type(struct upgt_softc *, uint16_t, uint16_t);
94 int		upgt_device_init(struct upgt_softc *);
95 int		upgt_mem_init(struct upgt_softc *);
96 uint32_t	upgt_mem_alloc(struct upgt_softc *);
97 void		upgt_mem_free(struct upgt_softc *, uint32_t);
98 int		upgt_fw_alloc(struct upgt_softc *);
99 void		upgt_fw_free(struct upgt_softc *);
100 int		upgt_fw_verify(struct upgt_softc *);
101 int		upgt_fw_load(struct upgt_softc *);
102 int		upgt_fw_copy(char *, char *, int);
103 int		upgt_eeprom_read(struct upgt_softc *);
104 int		upgt_eeprom_parse(struct upgt_softc *);
105 void		upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
106 void		upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
107 void		upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
108 void		upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
109 
110 int		upgt_ioctl(struct ifnet *, u_long, caddr_t);
111 int		upgt_init(struct ifnet *);
112 void		upgt_stop(struct upgt_softc *);
113 int		upgt_media_change(struct ifnet *);
114 void		upgt_newassoc(struct ieee80211com *, struct ieee80211_node *,
115 		    int);
116 int		upgt_newstate(struct ieee80211com *, enum ieee80211_state, int);
117 void		upgt_newstate_task(void *);
118 void		upgt_next_scan(void *);
119 void		upgt_start(struct ifnet *);
120 void		upgt_watchdog(struct ifnet *);
121 void		upgt_tx_task(void *);
122 void		upgt_tx_done(struct upgt_softc *, uint8_t *);
123 void		upgt_rx_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
124 void		upgt_rx(struct upgt_softc *, uint8_t *, int);
125 void		upgt_setup_rates(struct upgt_softc *);
126 uint8_t		upgt_rx_rate(struct upgt_softc *, const int);
127 int		upgt_set_macfilter(struct upgt_softc *, uint8_t state);
128 int		upgt_set_channel(struct upgt_softc *, unsigned);
129 void		upgt_set_led(struct upgt_softc *, int);
130 void		upgt_set_led_blink(void *);
131 int		upgt_get_stats(struct upgt_softc *);
132 
133 int		upgt_alloc_tx(struct upgt_softc *);
134 int		upgt_alloc_rx(struct upgt_softc *);
135 int		upgt_alloc_cmd(struct upgt_softc *);
136 void		upgt_free_tx(struct upgt_softc *);
137 void		upgt_free_rx(struct upgt_softc *);
138 void		upgt_free_cmd(struct upgt_softc *);
139 int		upgt_bulk_xmit(struct upgt_softc *, struct upgt_data *,
140 		    usbd_pipe_handle, uint32_t *, int);
141 
142 void		upgt_hexdump(void *, int);
143 uint32_t	upgt_crc32_le(const void *, size_t);
144 uint32_t	upgt_chksum_le(const uint32_t *, size_t);
145 
146 struct cfdriver upgt_cd = {
147 	NULL, "upgt", DV_IFNET
148 };
149 
150 const struct cfattach upgt_ca = {
151 	sizeof(struct upgt_softc),
152 	upgt_match,
153 	upgt_attach,
154 	upgt_detach,
155 	upgt_activate,
156 };
157 
158 static const struct usb_devno upgt_devs_1[] = {
159 	/* version 1 devices */
160 	{ USB_VENDOR_ALCATELT,		USB_PRODUCT_ALCATELT_ST120G }
161 };
162 
163 static const struct usb_devno upgt_devs_2[] = {
164 	/* version 2 devices */
165 	{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_PRISM_GT },
166 	{ USB_VENDOR_ALCATELT,		USB_PRODUCT_ALCATELT_ST121G },
167 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D7050 },
168 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54AG },
169 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GV2 },
170 	{ USB_VENDOR_CONCEPTRONIC,	USB_PRODUCT_CONCEPTRONIC_PRISM_GT },
171 	{ USB_VENDOR_DELL,		USB_PRODUCT_DELL_PRISM_GT_1 },
172 	{ USB_VENDOR_DELL,		USB_PRODUCT_DELL_PRISM_GT_2 },
173 	{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DWLG122A2 },
174 	{ USB_VENDOR_FSC,		USB_PRODUCT_FSC_E5400 },
175 	{ USB_VENDOR_GLOBESPAN,		USB_PRODUCT_GLOBESPAN_PRISM_GT_1 },
176 	{ USB_VENDOR_GLOBESPAN,		USB_PRODUCT_GLOBESPAN_PRISM_GT_2 },
177 	{ USB_VENDOR_INTERSIL,		USB_PRODUCT_INTERSIL_PRISM_GT },
178 	{ USB_VENDOR_PHEENET,		USB_PRODUCT_PHEENET_GWU513 },
179 	{ USB_VENDOR_PHILIPS,		USB_PRODUCT_PHILIPS_CPWUA054 },
180 	{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2862WG },
181 	{ USB_VENDOR_USR,		USB_PRODUCT_USR_USR5422 },
182 	{ USB_VENDOR_WISTRONNEWEB,	USB_PRODUCT_WISTRONNEWEB_UR045G },
183 	{ USB_VENDOR_XYRATEX,		USB_PRODUCT_XYRATEX_PRISM_GT_1 },
184 	{ USB_VENDOR_XYRATEX,		USB_PRODUCT_XYRATEX_PRISM_GT_2 },
185 	{ USB_VENDOR_ZCOM,		USB_PRODUCT_ZCOM_MD40900 },
186 	{ USB_VENDOR_ZCOM,		USB_PRODUCT_ZCOM_XG703A }
187 };
188 
189 int
190 upgt_match(struct device *parent, void *match, void *aux)
191 {
192 	struct usb_attach_arg *uaa = aux;
193 
194 	if (uaa->iface != NULL)
195 		return (UMATCH_NONE);
196 
197 	if (usb_lookup(upgt_devs_1, uaa->vendor, uaa->product) != NULL)
198 		return (UMATCH_VENDOR_PRODUCT);
199 
200 	if (usb_lookup(upgt_devs_2, uaa->vendor, uaa->product) != NULL)
201 		return (UMATCH_VENDOR_PRODUCT);
202 
203 	return (UMATCH_NONE);
204 }
205 
206 void
207 upgt_attach(struct device *parent, struct device *self, void *aux)
208 {
209 	struct upgt_softc *sc = (struct upgt_softc *)self;
210 	struct usb_attach_arg *uaa = aux;
211 	usb_interface_descriptor_t *id;
212 	usb_endpoint_descriptor_t *ed;
213 	usbd_status error;
214 	int i;
215 
216 	/*
217 	 * Attach USB device.
218 	 */
219 	sc->sc_udev = uaa->device;
220 
221 	/* check device type */
222 	if (upgt_device_type(sc, uaa->vendor, uaa->product) != 0)
223 		return;
224 
225 	/* set configuration number */
226 	if (usbd_set_config_no(sc->sc_udev, UPGT_CONFIG_NO, 0) != 0) {
227 		printf("%s: could not set configuration no!\n",
228 		    sc->sc_dev.dv_xname);
229 		return;
230 	}
231 
232 	/* get the first interface handle */
233 	error = usbd_device2interface_handle(sc->sc_udev, UPGT_IFACE_INDEX,
234 	    &sc->sc_iface);
235 	if (error != 0) {
236 		printf("%s: could not get interface handle!\n",
237 		    sc->sc_dev.dv_xname);
238 		return;
239 	}
240 
241 	/* find endpoints */
242 	id = usbd_get_interface_descriptor(sc->sc_iface);
243 	sc->sc_rx_no = sc->sc_tx_no = -1;
244 	for (i = 0; i < id->bNumEndpoints; i++) {
245 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
246 		if (ed == NULL) {
247 			printf("%s: no endpoint descriptor for iface %d!\n",
248 			    sc->sc_dev.dv_xname, i);
249 			return;
250 		}
251 
252 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
253 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
254 			sc->sc_tx_no = ed->bEndpointAddress;
255 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
256 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
257 			sc->sc_rx_no = ed->bEndpointAddress;
258 
259 		/*
260 		 * 0x01 TX pipe
261 		 * 0x81 RX pipe
262 		 *
263 		 * Deprecated scheme (not used with fw version >2.5.6.x):
264 		 * 0x02 TX MGMT pipe
265 		 * 0x82 TX MGMT pipe
266 		 */
267 		if (sc->sc_tx_no != -1 && sc->sc_rx_no != -1)
268 			break;
269 	}
270 	if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
271 		printf("%s: missing endpoint!\n", sc->sc_dev.dv_xname);
272 		return;
273 	}
274 
275 	/* setup tasks and timeouts */
276 	usb_init_task(&sc->sc_task_newstate, upgt_newstate_task, sc,
277 	    USB_TASK_TYPE_GENERIC);
278 	usb_init_task(&sc->sc_task_tx, upgt_tx_task, sc, USB_TASK_TYPE_GENERIC);
279 	timeout_set(&sc->scan_to, upgt_next_scan, sc);
280 	timeout_set(&sc->led_to, upgt_set_led_blink, sc);
281 
282 	/*
283 	 * Open TX and RX USB bulk pipes.
284 	 */
285 	error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
286 	    &sc->sc_tx_pipeh);
287 	if (error != 0) {
288 		printf("%s: could not open TX pipe: %s!\n",
289 		    sc->sc_dev.dv_xname, usbd_errstr(error));
290 		goto fail;
291 	}
292 	error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
293 	    &sc->sc_rx_pipeh);
294 	if (error != 0) {
295 		printf("%s: could not open RX pipe: %s!\n",
296 		    sc->sc_dev.dv_xname, usbd_errstr(error));
297 		goto fail;
298 	}
299 
300 	/*
301 	 * Allocate TX, RX, and CMD xfers.
302 	 */
303 	if (upgt_alloc_tx(sc) != 0)
304 		goto fail;
305 	if (upgt_alloc_rx(sc) != 0)
306 		goto fail;
307 	if (upgt_alloc_cmd(sc) != 0)
308 		goto fail;
309 
310 	/*
311 	 * We need the firmware loaded to complete the attach.
312 	 */
313 	if (rootvp == NULL)
314 		mountroothook_establish(upgt_attach_hook, sc);
315 	else
316 		upgt_attach_hook(sc);
317 
318 	return;
319 fail:
320 	printf("%s: %s failed!\n", sc->sc_dev.dv_xname, __func__);
321 }
322 
323 void
324 upgt_attach_hook(void *arg)
325 {
326 	struct upgt_softc *sc = arg;
327 	struct ieee80211com *ic = &sc->sc_ic;
328 	struct ifnet *ifp = &ic->ic_if;
329 	usbd_status error;
330 	int i;
331 
332 	/*
333 	 * Load firmware file into memory.
334 	 */
335 	if (upgt_fw_alloc(sc) != 0)
336 		goto fail;
337 
338 	/*
339 	 * Initialize the device.
340 	 */
341 	if (upgt_device_init(sc) != 0)
342 		goto fail;
343 
344 	/*
345 	 * Verify the firmware.
346 	 */
347 	if (upgt_fw_verify(sc) != 0)
348 		goto fail;
349 
350 	/*
351 	 * Calculate device memory space.
352 	 */
353 	if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
354 		printf("%s: could not find memory space addresses on FW!\n",
355 		    sc->sc_dev.dv_xname);
356 		goto fail;
357 	}
358 	sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
359 	sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
360 
361 	DPRINTF(1, "%s: memory address frame start=0x%08x\n",
362 	    sc->sc_dev.dv_xname, sc->sc_memaddr_frame_start);
363 	DPRINTF(1, "%s: memory address frame end=0x%08x\n",
364 	    sc->sc_dev.dv_xname, sc->sc_memaddr_frame_end);
365 	DPRINTF(1, "%s: memory address rx start=0x%08x\n",
366 	    sc->sc_dev.dv_xname, sc->sc_memaddr_rx_start);
367 
368 	upgt_mem_init(sc);
369 
370 	/*
371 	 * Load the firmware.
372 	 */
373 	if (upgt_fw_load(sc) != 0)
374 		goto fail;
375 
376 	/*
377 	 * Startup the RX pipe.
378 	 */
379 	struct upgt_data *data_rx = &sc->rx_data;
380 
381 	usbd_setup_xfer(data_rx->xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf,
382 	    MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb);
383 	error = usbd_transfer(data_rx->xfer);
384 	if (error != 0 && error != USBD_IN_PROGRESS) {
385 		printf("%s: could not queue RX transfer!\n",
386 		    sc->sc_dev.dv_xname);
387 		goto fail;
388 	}
389 	usbd_delay_ms(sc->sc_udev, 100);
390 
391 	/*
392 	 * Read the whole EEPROM content and parse it.
393 	 */
394 	if (upgt_eeprom_read(sc) != 0)
395 		goto fail;
396 	if (upgt_eeprom_parse(sc) != 0)
397 		goto fail;
398 
399 	/*
400 	 * Setup the 802.11 device.
401 	 */
402 	ic->ic_phytype = IEEE80211_T_OFDM;
403 	ic->ic_opmode = IEEE80211_M_STA;
404 	ic->ic_state = IEEE80211_S_INIT;
405 	ic->ic_caps =
406 	    IEEE80211_C_MONITOR |
407 	    IEEE80211_C_SHPREAMBLE |
408 	    IEEE80211_C_SHSLOT |
409 	    IEEE80211_C_WEP |
410 	    IEEE80211_C_RSN;
411 
412 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
413 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
414 
415 	for (i = 1; i <= 14; i++) {
416 		ic->ic_channels[i].ic_freq =
417 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
418 		ic->ic_channels[i].ic_flags =
419 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
420 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
421 	}
422 
423 	ifp->if_softc = sc;
424 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
425 	ifp->if_ioctl = upgt_ioctl;
426 	ifp->if_start = upgt_start;
427 	ifp->if_watchdog = upgt_watchdog;
428 	IFQ_SET_READY(&ifp->if_snd);
429 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
430 
431 	if_attach(ifp);
432 	ieee80211_ifattach(ifp);
433 	ic->ic_newassoc = upgt_newassoc;
434 
435 	sc->sc_newstate = ic->ic_newstate;
436 	ic->ic_newstate = upgt_newstate;
437 	ieee80211_media_init(ifp, upgt_media_change, ieee80211_media_status);
438 
439 #if NBPFILTER > 0
440 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
441 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
442 
443 	sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
444 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
445 	sc->sc_rxtap.wr_ihdr.it_present = htole32(UPGT_RX_RADIOTAP_PRESENT);
446 
447 	sc->sc_txtap_len = sizeof(sc->sc_txtapu);
448 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
449 	sc->sc_txtap.wt_ihdr.it_present = htole32(UPGT_TX_RADIOTAP_PRESENT);
450 #endif
451 
452 	printf("%s: address %s\n",
453 	    sc->sc_dev.dv_xname, ether_sprintf(ic->ic_myaddr));
454 
455 	return;
456 fail:
457 	printf("%s: %s failed!\n", sc->sc_dev.dv_xname, __func__);
458 }
459 
460 int
461 upgt_detach(struct device *self, int flags)
462 {
463 	struct upgt_softc *sc = (struct upgt_softc *)self;
464 	struct ifnet *ifp = &sc->sc_ic.ic_if;
465 	int s;
466 
467 	DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
468 
469 	s = splusb();
470 
471 	/* abort and close TX / RX pipes */
472 	if (sc->sc_tx_pipeh != NULL) {
473 		usbd_abort_pipe(sc->sc_tx_pipeh);
474 		usbd_close_pipe(sc->sc_tx_pipeh);
475 	}
476 	if (sc->sc_rx_pipeh != NULL) {
477 		usbd_abort_pipe(sc->sc_rx_pipeh);
478 		usbd_close_pipe(sc->sc_rx_pipeh);
479 	}
480 
481 	/* remove tasks and timeouts */
482 	usb_rem_task(sc->sc_udev, &sc->sc_task_newstate);
483 	usb_rem_task(sc->sc_udev, &sc->sc_task_tx);
484 	if (timeout_initialized(&sc->scan_to))
485 		timeout_del(&sc->scan_to);
486 	if (timeout_initialized(&sc->led_to))
487 		timeout_del(&sc->led_to);
488 
489 	/* free xfers */
490 	upgt_free_tx(sc);
491 	upgt_free_rx(sc);
492 	upgt_free_cmd(sc);
493 
494 	/* free firmware */
495 	upgt_fw_free(sc);
496 
497 	if (ifp->if_softc != NULL) {
498 		/* detach interface */
499 		ieee80211_ifdetach(ifp);
500 		if_detach(ifp);
501 	}
502 
503 	splx(s);
504 
505 	return (0);
506 }
507 
508 int
509 upgt_activate(struct device *self, int act)
510 {
511 	struct upgt_softc *sc = (struct upgt_softc *)self;
512 
513 	switch (act) {
514 	case DVACT_DEACTIVATE:
515 		usbd_deactivate(sc->sc_udev);
516 		break;
517 	}
518 
519 	return (0);
520 }
521 
522 int
523 upgt_device_type(struct upgt_softc *sc, uint16_t vendor, uint16_t product)
524 {
525 	if (usb_lookup(upgt_devs_1, vendor, product) != NULL) {
526 		sc->sc_device_type = 1;
527 		/* XXX */
528 		printf("%s: version 1 devices not supported yet!\n",
529 		    sc->sc_dev.dv_xname);
530 		return (1);
531 	} else {
532 		sc->sc_device_type = 2;
533 	}
534 
535 	return (0);
536 }
537 
538 int
539 upgt_device_init(struct upgt_softc *sc)
540 {
541 	struct upgt_data *data_cmd = &sc->cmd_data;
542 	char init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
543 	int len;
544 
545 	len = sizeof(init_cmd);
546 	bcopy(init_cmd, data_cmd->buf, len);
547 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
548 		printf("%s: could not send device init string!\n",
549 		    sc->sc_dev.dv_xname);
550 		return (EIO);
551 	}
552 	usbd_delay_ms(sc->sc_udev, 100);
553 
554 	DPRINTF(1, "%s: device initialized\n", sc->sc_dev.dv_xname);
555 
556 	return (0);
557 }
558 
559 int
560 upgt_mem_init(struct upgt_softc *sc)
561 {
562 	int i;
563 
564 	for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
565 		sc->sc_memory.page[i].used = 0;
566 
567 		if (i == 0) {
568 			/*
569 			 * The first memory page is always reserved for
570 			 * command data.
571 			 */
572 			sc->sc_memory.page[i].addr =
573 			    sc->sc_memaddr_frame_start + MCLBYTES;
574 		} else {
575 			sc->sc_memory.page[i].addr =
576 			    sc->sc_memory.page[i - 1].addr + MCLBYTES;
577 		}
578 
579 		if (sc->sc_memory.page[i].addr + MCLBYTES >=
580 		    sc->sc_memaddr_frame_end)
581 			break;
582 
583 		DPRINTF(2, "%s: memory address page %d=0x%08x\n",
584 		    sc->sc_dev.dv_xname, i, sc->sc_memory.page[i].addr);
585 	}
586 
587 	sc->sc_memory.pages = i;
588 
589 	DPRINTF(2, "%s: memory pages=%d\n",
590 	    sc->sc_dev.dv_xname, sc->sc_memory.pages);
591 
592 	return (0);
593 }
594 
595 uint32_t
596 upgt_mem_alloc(struct upgt_softc *sc)
597 {
598 	int i;
599 
600 	for (i = 0; i < sc->sc_memory.pages; i++) {
601 		if (sc->sc_memory.page[i].used == 0) {
602 			sc->sc_memory.page[i].used = 1;
603 			return (sc->sc_memory.page[i].addr);
604 		}
605 	}
606 
607 	return (0);
608 }
609 
610 void
611 upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
612 {
613 	int i;
614 
615 	for (i = 0; i < sc->sc_memory.pages; i++) {
616 		if (sc->sc_memory.page[i].addr == addr) {
617 			sc->sc_memory.page[i].used = 0;
618 			return;
619 		}
620 	}
621 
622 	printf("%s: could not free memory address 0x%08x!\n",
623 	    sc->sc_dev.dv_xname, addr);
624 }
625 
626 
627 int
628 upgt_fw_alloc(struct upgt_softc *sc)
629 {
630 	const char *name = "upgt-gw3887";
631 	int error;
632 
633 	if (sc->sc_fw == NULL) {
634 		error = loadfirmware(name, &sc->sc_fw, &sc->sc_fw_size);
635 		if (error != 0) {
636 			printf("%s: error %d, could not read firmware %s!\n",
637 			    sc->sc_dev.dv_xname, error, name);
638 			return (EIO);
639 		}
640 	}
641 
642 	DPRINTF(1, "%s: firmware %s allocated\n", sc->sc_dev.dv_xname, name);
643 
644 	return (0);
645 }
646 
647 void
648 upgt_fw_free(struct upgt_softc *sc)
649 {
650 	if (sc->sc_fw != NULL) {
651 		free(sc->sc_fw, M_DEVBUF);
652 		sc->sc_fw = NULL;
653 		DPRINTF(1, "%s: firmware freed\n", sc->sc_dev.dv_xname);
654 	}
655 }
656 
657 int
658 upgt_fw_verify(struct upgt_softc *sc)
659 {
660 	struct upgt_fw_bra_option *bra_option;
661 	uint32_t bra_option_type, bra_option_len;
662 	uint32_t *uc;
663 	int offset, bra_end = 0;
664 
665 	/*
666 	 * Seek to beginning of Boot Record Area (BRA).
667 	 */
668 	for (offset = 0; offset < sc->sc_fw_size; offset += sizeof(*uc)) {
669 		uc = (uint32_t *)(sc->sc_fw + offset);
670 		if (*uc == 0)
671 			break;
672 	}
673 	for (; offset < sc->sc_fw_size; offset += sizeof(*uc)) {
674 		uc = (uint32_t *)(sc->sc_fw + offset);
675 		if (*uc != 0)
676 			break;
677 	}
678 	if (offset == sc->sc_fw_size) {
679 		printf("%s: firmware Boot Record Area not found!\n",
680 		    sc->sc_dev.dv_xname);
681 		return (EIO);
682 	}
683 	DPRINTF(1, "%s: firmware Boot Record Area found at offset %d\n",
684 	    sc->sc_dev.dv_xname, offset);
685 
686 	/*
687 	 * Parse Boot Record Area (BRA) options.
688 	 */
689 	while (offset < sc->sc_fw_size && bra_end == 0) {
690 		/* get current BRA option */
691 		bra_option = (struct upgt_fw_bra_option *)(sc->sc_fw + offset);
692 		bra_option_type = letoh32(bra_option->type);
693 		bra_option_len = letoh32(bra_option->len) * sizeof(*uc);
694 
695 		switch (bra_option_type) {
696 		case UPGT_BRA_TYPE_FW:
697 			DPRINTF(1, "%s: UPGT_BRA_TYPE_FW len=%d\n",
698 			    sc->sc_dev.dv_xname, bra_option_len);
699 
700 			if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
701 				printf("%s: wrong UPGT_BRA_TYPE_FW len!\n",
702 				    sc->sc_dev.dv_xname);
703 				return (EIO);
704 			}
705 			if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_option->data,
706 			    bra_option_len) == 0) {
707 				sc->sc_fw_type = UPGT_FWTYPE_LM86;
708 				break;
709 			}
710 			if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_option->data,
711 			    bra_option_len) == 0) {
712 				sc->sc_fw_type = UPGT_FWTYPE_LM87;
713 				break;
714 			}
715 			if (memcmp(UPGT_BRA_FWTYPE_FMAC, bra_option->data,
716 			    bra_option_len) == 0) {
717 				sc->sc_fw_type = UPGT_FWTYPE_FMAC;
718 				break;
719 			}
720 			printf("%s: unsupported firmware type!\n",
721 			    sc->sc_dev.dv_xname);
722 			return (EIO);
723 		case UPGT_BRA_TYPE_VERSION:
724 			DPRINTF(1, "%s: UPGT_BRA_TYPE_VERSION len=%d\n",
725 			    sc->sc_dev.dv_xname, bra_option_len);
726 			break;
727 		case UPGT_BRA_TYPE_DEPIF:
728 			DPRINTF(1, "%s: UPGT_BRA_TYPE_DEPIF len=%d\n",
729 			    sc->sc_dev.dv_xname, bra_option_len);
730 			break;
731 		case UPGT_BRA_TYPE_EXPIF:
732 			DPRINTF(1, "%s: UPGT_BRA_TYPE_EXPIF len=%d\n",
733 			    sc->sc_dev.dv_xname, bra_option_len);
734 			break;
735 		case UPGT_BRA_TYPE_DESCR:
736 			DPRINTF(1, "%s: UPGT_BRA_TYPE_DESCR len=%d\n",
737 			    sc->sc_dev.dv_xname, bra_option_len);
738 
739 			struct upgt_fw_bra_descr *descr =
740 				(struct upgt_fw_bra_descr *)bra_option->data;
741 
742 			sc->sc_memaddr_frame_start =
743 			    letoh32(descr->memaddr_space_start);
744 			sc->sc_memaddr_frame_end =
745 			    letoh32(descr->memaddr_space_end);
746 
747 			DPRINTF(2, "%s: memory address space start=0x%08x\n",
748 			    sc->sc_dev.dv_xname, sc->sc_memaddr_frame_start);
749 			DPRINTF(2, "%s: memory address space end=0x%08x\n",
750 			    sc->sc_dev.dv_xname, sc->sc_memaddr_frame_end);
751 			break;
752 		case UPGT_BRA_TYPE_END:
753 			DPRINTF(1, "%s: UPGT_BRA_TYPE_END len=%d\n",
754 			    sc->sc_dev.dv_xname, bra_option_len);
755 			bra_end = 1;
756 			break;
757 		default:
758 			DPRINTF(1, "%s: unknown BRA option len=%d\n",
759 			    sc->sc_dev.dv_xname, bra_option_len);
760 			return (EIO);
761 		}
762 
763 		/* jump to next BRA option */
764 		offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
765 	}
766 
767 	DPRINTF(1, "%s: firmware verified\n", sc->sc_dev.dv_xname);
768 
769 	return (0);
770 }
771 
772 int
773 upgt_fw_load(struct upgt_softc *sc)
774 {
775 	struct upgt_data *data_cmd = &sc->cmd_data;
776 	struct upgt_data *data_rx = &sc->rx_data;
777 	char start_fwload_cmd[] = { 0x3c, 0x0d };
778 	int offset, bsize, n, i, len;
779 	uint32_t crc32;
780 
781 	/* send firmware start load command */
782 	len = sizeof(start_fwload_cmd);
783 	bcopy(start_fwload_cmd, data_cmd->buf, len);
784 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
785 		printf("%s: could not send start_firmware_load command!\n",
786 		    sc->sc_dev.dv_xname);
787 		return (EIO);
788 	}
789 
790 	/* send X2 header */
791 	len = sizeof(struct upgt_fw_x2_header);
792 	struct upgt_fw_x2_header *x2 = data_cmd->buf;
793 	bcopy(UPGT_X2_SIGNATURE, x2->signature, UPGT_X2_SIGNATURE_SIZE);
794 	x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
795 	x2->len = htole32(sc->sc_fw_size);
796 	x2->crc = upgt_crc32_le(data_cmd->buf + UPGT_X2_SIGNATURE_SIZE,
797 	    sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
798 	    sizeof(uint32_t));
799 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
800 		printf("%s: could not send firmware X2 header!\n",
801 		    sc->sc_dev.dv_xname);
802 		return (EIO);
803 	}
804 
805 	/* download firmware */
806 	for (offset = 0; offset < sc->sc_fw_size; offset += bsize) {
807 		if (sc->sc_fw_size - offset > UPGT_FW_BLOCK_SIZE)
808 			bsize = UPGT_FW_BLOCK_SIZE;
809 		else
810 			bsize = sc->sc_fw_size - offset;
811 
812 		n = upgt_fw_copy(sc->sc_fw + offset, data_cmd->buf, bsize);
813 
814 		DPRINTF(1, "%s: FW offset=%d, read=%d, sent=%d\n",
815 		    sc->sc_dev.dv_xname, offset, n, bsize);
816 
817 		if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &bsize, 0)
818 		    != 0) {
819 			printf("%s: error while downloading firmware block!\n",
820 			    sc->sc_dev.dv_xname);
821 			return (EIO);
822 		}
823 
824 		bsize = n;
825 	}
826 	DPRINTF(1, "%s: firmware downloaded\n", sc->sc_dev.dv_xname);
827 
828 	/* load firmware */
829 	crc32 = upgt_crc32_le(sc->sc_fw, sc->sc_fw_size);
830 	*((uint32_t *)(data_cmd->buf)    ) = crc32;
831 	*((uint8_t  *)(data_cmd->buf) + 4) = 'g';
832 	*((uint8_t  *)(data_cmd->buf) + 5) = '\r';
833 	len = 6;
834 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
835 		printf("%s: could not send load_firmware command!\n",
836 		    sc->sc_dev.dv_xname);
837 		return (EIO);
838 	}
839 
840 	for (i = 0; i < UPGT_FIRMWARE_TIMEOUT; i++) {
841 		len = UPGT_FW_BLOCK_SIZE;
842 		bzero(data_rx->buf, MCLBYTES);
843 		if (upgt_bulk_xmit(sc, data_rx, sc->sc_rx_pipeh, &len,
844 		    USBD_SHORT_XFER_OK) != 0) {
845 			printf("%s: could not read firmware response!\n",
846 			    sc->sc_dev.dv_xname);
847 			return (EIO);
848 		}
849 
850 		if (memcmp(data_rx->buf, "OK", 2) == 0)
851 			break;	/* firmware load was successful */
852 	}
853 	if (i == UPGT_FIRMWARE_TIMEOUT) {
854 		printf("%s: firmware load failed!\n", sc->sc_dev.dv_xname);
855 		return (EIO);
856 	}
857 	DPRINTF(1, "%s: firmware loaded\n", sc->sc_dev.dv_xname);
858 
859 	return (0);
860 }
861 
862 /*
863  * While copying the version 2 firmware, we need to replace two characters:
864  *
865  * 0x7e -> 0x7d 0x5e
866  * 0x7d -> 0x7d 0x5d
867  */
868 int
869 upgt_fw_copy(char *src, char *dst, int size)
870 {
871 	int i, j;
872 
873 	for (i = 0, j = 0; i < size && j < size; i++) {
874 		switch (src[i]) {
875 		case 0x7e:
876 			dst[j] = 0x7d;
877 			j++;
878 			dst[j] = 0x5e;
879 			j++;
880 			break;
881 		case 0x7d:
882 			dst[j] = 0x7d;
883 			j++;
884 			dst[j] = 0x5d;
885 			j++;
886 			break;
887 		default:
888 			dst[j] = src[i];
889 			j++;
890 			break;
891 		}
892 	}
893 
894 	return (i);
895 }
896 
897 int
898 upgt_eeprom_read(struct upgt_softc *sc)
899 {
900 	struct upgt_data *data_cmd = &sc->cmd_data;
901 	struct upgt_lmac_mem *mem;
902 	struct upgt_lmac_eeprom	*eeprom;
903 	int offset, block, len;
904 
905 	offset = 0;
906 	block = UPGT_EEPROM_BLOCK_SIZE;
907 	while (offset < UPGT_EEPROM_SIZE) {
908 		DPRINTF(1, "%s: request EEPROM block (offset=%d, len=%d)\n",
909 		    sc->sc_dev.dv_xname, offset, block);
910 
911 		/*
912 		 * Transmit the URB containing the CMD data.
913 		 */
914 		bzero(data_cmd->buf, MCLBYTES);
915 
916 		mem = (struct upgt_lmac_mem *)data_cmd->buf;
917 		mem->addr = htole32(sc->sc_memaddr_frame_start +
918 		    UPGT_MEMSIZE_FRAME_HEAD);
919 
920 		eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
921 		eeprom->header1.flags = 0;
922 		eeprom->header1.type = UPGT_H1_TYPE_CTRL;
923 		eeprom->header1.len = htole16((
924 		    sizeof(struct upgt_lmac_eeprom) -
925 		    sizeof(struct upgt_lmac_header)) + block);
926 
927 		eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
928 		eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
929 		eeprom->header2.flags = 0;
930 
931 		eeprom->offset = htole16(offset);
932 		eeprom->len = htole16(block);
933 
934 		len = sizeof(*mem) + sizeof(*eeprom) + block;
935 
936 		mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
937 		    len - sizeof(*mem));
938 
939 		if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len,
940 		    USBD_FORCE_SHORT_XFER) != 0) {
941 			printf("%s: could not transmit EEPROM data URB!\n",
942 			    sc->sc_dev.dv_xname);
943 			return (EIO);
944 		}
945 		if (tsleep(sc, 0, "eeprom_request", UPGT_USB_TIMEOUT)) {
946 			printf("%s: timeout while waiting for EEPROM data!\n",
947 			    sc->sc_dev.dv_xname);
948 			return (EIO);
949 		}
950 
951 		offset += block;
952 		if (UPGT_EEPROM_SIZE - offset < block)
953 			block = UPGT_EEPROM_SIZE - offset;
954 	}
955 
956 	return (0);
957 }
958 
959 int
960 upgt_eeprom_parse(struct upgt_softc *sc)
961 {
962 	struct ieee80211com *ic = &sc->sc_ic;
963 	struct upgt_eeprom_header *eeprom_header;
964 	struct upgt_eeprom_option *eeprom_option;
965 	uint16_t option_len;
966 	uint16_t option_type;
967 	uint16_t preamble_len;
968 	int option_end = 0;
969 
970 	/* calculate eeprom options start offset */
971 	eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
972 	preamble_len = letoh16(eeprom_header->preamble_len);
973 	eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
974 	    (sizeof(struct upgt_eeprom_header) + preamble_len));
975 
976 	while (!option_end) {
977 		/* the eeprom option length is stored in words */
978 		option_len =
979 		    (letoh16(eeprom_option->len) - 1) * sizeof(uint16_t);
980 		option_type =
981 		    letoh16(eeprom_option->type);
982 
983 		switch (option_type) {
984 		case UPGT_EEPROM_TYPE_NAME:
985 			DPRINTF(1, "%s: EEPROM name len=%d\n",
986 			    sc->sc_dev.dv_xname, option_len);
987 			break;
988 		case UPGT_EEPROM_TYPE_SERIAL:
989 			DPRINTF(1, "%s: EEPROM serial len=%d\n",
990 			    sc->sc_dev.dv_xname, option_len);
991 			break;
992 		case UPGT_EEPROM_TYPE_MAC:
993 			DPRINTF(1, "%s: EEPROM mac len=%d\n",
994 			    sc->sc_dev.dv_xname, option_len);
995 
996 			IEEE80211_ADDR_COPY(ic->ic_myaddr, eeprom_option->data);
997 			break;
998 		case UPGT_EEPROM_TYPE_HWRX:
999 			DPRINTF(1, "%s: EEPROM hwrx len=%d\n",
1000 			    sc->sc_dev.dv_xname, option_len);
1001 
1002 			upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1003 			break;
1004 		case UPGT_EEPROM_TYPE_CHIP:
1005 			DPRINTF(1, "%s: EEPROM chip len=%d\n",
1006 			    sc->sc_dev.dv_xname, option_len);
1007 			break;
1008 		case UPGT_EEPROM_TYPE_FREQ3:
1009 			DPRINTF(1, "%s: EEPROM freq3 len=%d\n",
1010 			    sc->sc_dev.dv_xname, option_len);
1011 
1012 			upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1013 			    option_len);
1014 			break;
1015 		case UPGT_EEPROM_TYPE_FREQ4:
1016 			DPRINTF(1, "%s: EEPROM freq4 len=%d\n",
1017 			    sc->sc_dev.dv_xname, option_len);
1018 
1019 			upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1020 			    option_len);
1021 			break;
1022 		case UPGT_EEPROM_TYPE_FREQ5:
1023 			DPRINTF(1, "%s: EEPROM freq5 len=%d\n",
1024 			    sc->sc_dev.dv_xname, option_len);
1025 			break;
1026 		case UPGT_EEPROM_TYPE_FREQ6:
1027 			DPRINTF(1, "%s: EEPROM freq6 len=%d\n",
1028 			    sc->sc_dev.dv_xname, option_len);
1029 
1030 			upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1031 			    option_len);
1032 			break;
1033 		case UPGT_EEPROM_TYPE_END:
1034 			DPRINTF(1, "%s: EEPROM end len=%d\n",
1035 			    sc->sc_dev.dv_xname, option_len);
1036 			option_end = 1;
1037 			break;
1038 		case UPGT_EEPROM_TYPE_OFF:
1039 			DPRINTF(1, "%s: EEPROM off without end option!\n",
1040 			    sc->sc_dev.dv_xname);
1041 			return (EIO);
1042 		default:
1043 			DPRINTF(1, "%s: EEPROM unknown type 0x%04x len=%d\n",
1044 			    sc->sc_dev.dv_xname, option_type, option_len);
1045 			break;
1046 		}
1047 
1048 		/* jump to next EEPROM option */
1049 		eeprom_option = (struct upgt_eeprom_option *)
1050 		    (eeprom_option->data + option_len);
1051 	}
1052 
1053 	return (0);
1054 }
1055 
1056 void
1057 upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1058 {
1059 	struct upgt_eeprom_option_hwrx *option_hwrx;
1060 
1061 	option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1062 
1063 	sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1064 
1065 	DPRINTF(2, "%s: hwrx option value=0x%04x\n",
1066 	    sc->sc_dev.dv_xname, sc->sc_eeprom_hwrx);
1067 }
1068 
1069 void
1070 upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1071 {
1072 	struct upgt_eeprom_freq3_header *freq3_header;
1073 	struct upgt_lmac_freq3 *freq3;
1074 	int i, elements, flags;
1075 	unsigned channel;
1076 
1077 	freq3_header = (struct upgt_eeprom_freq3_header *)data;
1078 	freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1079 
1080 	flags = freq3_header->flags;
1081 	elements = freq3_header->elements;
1082 
1083 	DPRINTF(2, "%s: flags=0x%02x\n", sc->sc_dev.dv_xname, flags);
1084 	DPRINTF(2, "%s: elements=%d\n", sc->sc_dev.dv_xname, elements);
1085 
1086 	for (i = 0; i < elements; i++) {
1087 		channel = ieee80211_mhz2ieee(letoh16(freq3[i].freq), 0);
1088 
1089 		sc->sc_eeprom_freq3[channel] = freq3[i];
1090 
1091 		DPRINTF(2, "%s: frequence=%d, channel=%d\n",
1092 		    sc->sc_dev.dv_xname,
1093 		    letoh16(sc->sc_eeprom_freq3[channel].freq), channel);
1094 	}
1095 }
1096 
1097 void
1098 upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1099 {
1100 	struct upgt_eeprom_freq4_header *freq4_header;
1101 	struct upgt_eeprom_freq4_1 *freq4_1;
1102 	struct upgt_eeprom_freq4_2 *freq4_2;
1103 	int i, j, elements, settings, flags;
1104 	unsigned channel;
1105 
1106 	freq4_header = (struct upgt_eeprom_freq4_header *)data;
1107 	freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1108 
1109 	flags = freq4_header->flags;
1110 	elements = freq4_header->elements;
1111 	settings = freq4_header->settings;
1112 
1113 	/* we need this value later */
1114 	sc->sc_eeprom_freq6_settings = freq4_header->settings;
1115 
1116 	DPRINTF(2, "%s: flags=0x%02x\n", sc->sc_dev.dv_xname, flags);
1117 	DPRINTF(2, "%s: elements=%d\n", sc->sc_dev.dv_xname, elements);
1118 	DPRINTF(2, "%s: settings=%d\n", sc->sc_dev.dv_xname, settings);
1119 
1120 	for (i = 0; i < elements; i++) {
1121 		channel = ieee80211_mhz2ieee(letoh16(freq4_1[i].freq), 0);
1122 
1123 		freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1124 
1125 		for (j = 0; j < settings; j++) {
1126 			sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1127 			sc->sc_eeprom_freq4[channel][j].pad = 0;
1128 		}
1129 
1130 		DPRINTF(2, "%s: frequence=%d, channel=%d\n",
1131 		    sc->sc_dev.dv_xname,
1132 		    letoh16(freq4_1[i].freq), channel);
1133 	}
1134 }
1135 
1136 void
1137 upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1138 {
1139 	struct upgt_lmac_freq6 *freq6;
1140 	int i, elements;
1141 	unsigned channel;
1142 
1143 	freq6 = (struct upgt_lmac_freq6 *)data;
1144 
1145 	elements = len / sizeof(struct upgt_lmac_freq6);
1146 
1147 	DPRINTF(2, "%s: elements=%d\n", sc->sc_dev.dv_xname, elements);
1148 
1149 	for (i = 0; i < elements; i++) {
1150 		channel = ieee80211_mhz2ieee(letoh16(freq6[i].freq), 0);
1151 
1152 		sc->sc_eeprom_freq6[channel] = freq6[i];
1153 
1154 		DPRINTF(2, "%s: frequence=%d, channel=%d\n",
1155 		    sc->sc_dev.dv_xname,
1156 		    letoh16(sc->sc_eeprom_freq6[channel].freq), channel);
1157 	}
1158 }
1159 
1160 int
1161 upgt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1162 {
1163 	struct upgt_softc *sc = ifp->if_softc;
1164 	struct ieee80211com *ic = &sc->sc_ic;
1165 	struct ifaddr *ifa;
1166 	struct ifreq *ifr;
1167 	int s, error = 0;
1168 	uint8_t chan;
1169 
1170 	s = splnet();
1171 
1172 	switch (cmd) {
1173 	case SIOCSIFADDR:
1174 		ifa = (struct ifaddr *)data;
1175 		ifp->if_flags |= IFF_UP;
1176 #ifdef INET
1177 		if (ifa->ifa_addr->sa_family == AF_INET)
1178 			arp_ifinit(&ic->ic_ac, ifa);
1179 #endif
1180 		/* FALLTHROUGH */
1181 	case SIOCSIFFLAGS:
1182 		if (ifp->if_flags & IFF_UP) {
1183 			if ((ifp->if_flags & IFF_RUNNING) == 0)
1184 				upgt_init(ifp);
1185 		} else {
1186 			if (ifp->if_flags & IFF_RUNNING)
1187 				upgt_stop(sc);
1188 		}
1189 		break;
1190 	case SIOCADDMULTI:
1191 	case SIOCDELMULTI:
1192 		ifr = (struct ifreq *)data;
1193 		error = (cmd == SIOCADDMULTI) ?
1194 		    ether_addmulti(ifr, &ic->ic_ac) :
1195 		    ether_delmulti(ifr, &ic->ic_ac);
1196 		if (error == ENETRESET)
1197 			error = 0;
1198 		break;
1199 	case SIOCS80211CHANNEL:
1200 		/* allow fast channel switching in monitor mode */
1201 		error = ieee80211_ioctl(ifp, cmd, data);
1202 		if (error == ENETRESET &&
1203 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
1204 			if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1205 			    (IFF_UP | IFF_RUNNING)) {
1206 				ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1207 				chan = ieee80211_chan2ieee(ic,
1208 				    ic->ic_bss->ni_chan);
1209 				upgt_set_channel(sc, chan);
1210 			}
1211 			error = 0;
1212 		}
1213 		break;
1214 	default:
1215 		error = ieee80211_ioctl(ifp, cmd, data);
1216 		break;
1217 	}
1218 
1219 	if (error == ENETRESET) {
1220 		if (ifp->if_flags & (IFF_UP | IFF_RUNNING))
1221 			upgt_init(ifp);
1222 		error = 0;
1223 	}
1224 
1225 	splx(s);
1226 
1227 	return (error);
1228 }
1229 
1230 int
1231 upgt_init(struct ifnet *ifp)
1232 {
1233 	struct upgt_softc *sc = ifp->if_softc;
1234 	struct ieee80211com *ic = &sc->sc_ic;
1235 
1236 	DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
1237 
1238 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1239 
1240 	/* select default channel */
1241 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1242 	sc->sc_cur_chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
1243 
1244 	/* setup device rates */
1245 	upgt_setup_rates(sc);
1246 
1247 	ifp->if_flags |= IFF_RUNNING;
1248 	ifp->if_flags &= ~IFF_OACTIVE;
1249 
1250 	upgt_set_macfilter(sc, IEEE80211_S_SCAN);
1251 
1252 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1253 		upgt_set_channel(sc, sc->sc_cur_chan);
1254 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1255 	} else
1256 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1257 
1258 	return (0);
1259 }
1260 
1261 void
1262 upgt_stop(struct upgt_softc *sc)
1263 {
1264 	struct ieee80211com *ic = &sc->sc_ic;
1265 	struct ifnet *ifp = &ic->ic_if;
1266 
1267 	DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
1268 
1269 	/* device down */
1270 	ifp->if_timer = 0;
1271 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1272 
1273 	upgt_set_led(sc, UPGT_LED_OFF);
1274 
1275 	/* change device back to initial state */
1276 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1277 }
1278 
1279 int
1280 upgt_media_change(struct ifnet *ifp)
1281 {
1282 	struct upgt_softc *sc = ifp->if_softc;
1283 	int error;
1284 
1285 	DPRINTF(1, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
1286 
1287 	if ((error = ieee80211_media_change(ifp) != ENETRESET))
1288 		return (error);
1289 
1290 	if (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1291 		/* give pending USB transfers a chance to finish */
1292 		usbd_delay_ms(sc->sc_udev, 100);
1293 		upgt_init(ifp);
1294 	}
1295 
1296 	return (0);
1297 }
1298 
1299 void
1300 upgt_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
1301 {
1302 	ni->ni_txrate = 0;
1303 }
1304 
1305 int
1306 upgt_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1307 {
1308 	struct upgt_softc *sc = ic->ic_if.if_softc;
1309 
1310 	usb_rem_task(sc->sc_udev, &sc->sc_task_newstate);
1311 	timeout_del(&sc->scan_to);
1312 
1313 	/* do it in a process context */
1314 	sc->sc_state = nstate;
1315 	sc->sc_arg = arg;
1316 	usb_add_task(sc->sc_udev, &sc->sc_task_newstate);
1317 
1318 	return (0);
1319 }
1320 
1321 void
1322 upgt_newstate_task(void *arg)
1323 {
1324 	struct upgt_softc *sc = arg;
1325 	struct ieee80211com *ic = &sc->sc_ic;
1326 	struct ieee80211_node *ni;
1327 	unsigned channel;
1328 
1329 	switch (sc->sc_state) {
1330 	case IEEE80211_S_INIT:
1331 		DPRINTF(1, "%s: newstate is IEEE80211_S_INIT\n",
1332 		    sc->sc_dev.dv_xname);
1333 
1334 		/* do not accept any frames if the device is down */
1335 		upgt_set_macfilter(sc, IEEE80211_S_INIT);
1336 		upgt_set_led(sc, UPGT_LED_OFF);
1337 		break;
1338 	case IEEE80211_S_SCAN:
1339 		DPRINTF(1, "%s: newstate is IEEE80211_S_SCAN\n",
1340 		    sc->sc_dev.dv_xname);
1341 
1342 		channel = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
1343 		upgt_set_channel(sc, channel);
1344 		timeout_add_msec(&sc->scan_to, 200);
1345 		break;
1346 	case IEEE80211_S_AUTH:
1347 		DPRINTF(1, "%s: newstate is IEEE80211_S_AUTH\n",
1348 		    sc->sc_dev.dv_xname);
1349 
1350 		channel = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
1351 		upgt_set_channel(sc, channel);
1352 		break;
1353 	case IEEE80211_S_ASSOC:
1354 		DPRINTF(1, "%s: newstate is IEEE80211_S_ASSOC\n",
1355 		    sc->sc_dev.dv_xname);
1356 		break;
1357 	case IEEE80211_S_RUN:
1358 		DPRINTF(1, "%s: newstate is IEEE80211_S_RUN\n",
1359 		    sc->sc_dev.dv_xname);
1360 
1361 		ni = ic->ic_bss;
1362 
1363 		/*
1364 		 * TX rate control is done by the firmware.
1365 		 * Report the maximum rate which is available therefore.
1366 		 */
1367 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
1368 
1369 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
1370 			upgt_set_macfilter(sc, IEEE80211_S_RUN);
1371 		upgt_set_led(sc, UPGT_LED_ON);
1372 		break;
1373 	}
1374 
1375 	sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
1376 }
1377 
1378 void
1379 upgt_next_scan(void *arg)
1380 {
1381 	struct upgt_softc *sc = arg;
1382 	struct ieee80211com *ic = &sc->sc_ic;
1383 	struct ifnet *ifp = &ic->ic_if;
1384 
1385 	DPRINTF(2, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
1386 
1387 	if (ic->ic_state == IEEE80211_S_SCAN)
1388 		ieee80211_next_scan(ifp);
1389 }
1390 
1391 void
1392 upgt_start(struct ifnet *ifp)
1393 {
1394 	struct upgt_softc *sc = ifp->if_softc;
1395 	struct ieee80211com *ic = &sc->sc_ic;
1396 	struct ieee80211_node *ni;
1397 	struct mbuf *m;
1398 	int i;
1399 
1400 	/* don't transmit packets if interface is busy or down */
1401 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1402 		return;
1403 
1404 	DPRINTF(2, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
1405 
1406 	for (i = 0; i < UPGT_TX_COUNT; i++) {
1407 		struct upgt_data *data_tx = &sc->tx_data[i];
1408 
1409 		IF_POLL(&ic->ic_mgtq, m);
1410 		if (m != NULL) {
1411 			/* management frame */
1412 			IF_DEQUEUE(&ic->ic_mgtq, m);
1413 
1414 			ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1415 			m->m_pkthdr.rcvif = NULL;
1416 #if NBPFILTER > 0
1417 			if (ic->ic_rawbpf != NULL)
1418 				bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
1419 #endif
1420 			if ((data_tx->addr = upgt_mem_alloc(sc)) == 0) {
1421 				printf("%s: no free prism memory!\n",
1422 				    sc->sc_dev.dv_xname);
1423 				return;
1424 			}
1425 			data_tx->ni = ni;
1426 			data_tx->m = m;
1427 			sc->tx_queued++;
1428 		} else {
1429 			/* data frame */
1430 			if (ic->ic_state != IEEE80211_S_RUN)
1431 				break;
1432 
1433 			IFQ_POLL(&ifp->if_snd, m);
1434 			if (m == NULL)
1435 				break;
1436 
1437 			IFQ_DEQUEUE(&ifp->if_snd, m);
1438 #if NBPFILTER > 0
1439 			if (ifp->if_bpf != NULL)
1440 				bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1441 #endif
1442 			m = ieee80211_encap(ifp, m, &ni);
1443 			if (m == NULL)
1444 				continue;
1445 #if NBPFILTER > 0
1446 			if (ic->ic_rawbpf != NULL)
1447 				bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
1448 #endif
1449 			if ((data_tx->addr = upgt_mem_alloc(sc)) == 0) {
1450 				printf("%s: no free prism memory!\n",
1451 				    sc->sc_dev.dv_xname);
1452 				return;
1453 			}
1454 			data_tx->ni = ni;
1455 			data_tx->m = m;
1456 			sc->tx_queued++;
1457 		}
1458 	}
1459 
1460 	if (sc->tx_queued > 0) {
1461 		DPRINTF(2, "%s: tx_queued=%d\n",
1462 		    sc->sc_dev.dv_xname, sc->tx_queued);
1463 		/* process the TX queue in process context */
1464 		ifp->if_timer = 5;
1465 		ifp->if_flags |= IFF_OACTIVE;
1466 		usb_rem_task(sc->sc_udev, &sc->sc_task_tx);
1467 		usb_add_task(sc->sc_udev, &sc->sc_task_tx);
1468 	}
1469 }
1470 
1471 void
1472 upgt_watchdog(struct ifnet *ifp)
1473 {
1474 	struct upgt_softc *sc = ifp->if_softc;
1475 	struct ieee80211com *ic = &sc->sc_ic;
1476 
1477 	if (ic->ic_state == IEEE80211_S_INIT)
1478 		return;
1479 
1480 	printf("%s: watchdog timeout!\n", sc->sc_dev.dv_xname);
1481 
1482 	/* TODO: what shall we do on TX timeout? */
1483 
1484 	ieee80211_watchdog(ifp);
1485 }
1486 
1487 void
1488 upgt_tx_task(void *arg)
1489 {
1490 	struct upgt_softc *sc = arg;
1491 	struct ieee80211com *ic = &sc->sc_ic;
1492 	struct ieee80211_frame *wh;
1493 	struct ieee80211_key *k;
1494 	struct upgt_lmac_mem *mem;
1495 	struct upgt_lmac_tx_desc *txdesc;
1496 	struct mbuf *m;
1497 	uint32_t addr;
1498 	int len, i, s;
1499 	usbd_status error;
1500 
1501 	s = splusb();
1502 
1503 	upgt_set_led(sc, UPGT_LED_BLINK);
1504 
1505 	for (i = 0; i < UPGT_TX_COUNT; i++) {
1506 		struct upgt_data *data_tx = &sc->tx_data[i];
1507 
1508 		if (data_tx->m == NULL) {
1509 			DPRINTF(2, "%s: %d: m is NULL\n",
1510 			    sc->sc_dev.dv_xname, i);
1511 			continue;
1512 		}
1513 
1514 		m = data_tx->m;
1515 		addr = data_tx->addr + UPGT_MEMSIZE_FRAME_HEAD;
1516 
1517 		/*
1518 		 * Software crypto.
1519 		 */
1520 		wh = mtod(m, struct ieee80211_frame *);
1521 
1522 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1523 			k = ieee80211_get_txkey(ic, wh, ic->ic_bss);
1524 
1525 			if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
1526 				return;
1527 
1528 			/* in case packet header moved, reset pointer */
1529 			wh = mtod(m, struct ieee80211_frame *);
1530 		}
1531 
1532 		/*
1533 		 * Transmit the URB containing the TX data.
1534 		 */
1535 		bzero(data_tx->buf, MCLBYTES);
1536 
1537 		mem = (struct upgt_lmac_mem *)data_tx->buf;
1538 		mem->addr = htole32(addr);
1539 
1540 		txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
1541 
1542 		/* XXX differ between data and mgmt frames? */
1543 		txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
1544 		txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
1545 		txdesc->header1.len = htole16(m->m_pkthdr.len);
1546 
1547 		txdesc->header2.reqid = htole32(data_tx->addr);
1548 		txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
1549 		txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
1550 
1551 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1552 		    IEEE80211_FC0_TYPE_MGT) {
1553 			/* always send mgmt frames at lowest rate (DS1) */
1554 			memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
1555 		} else {
1556 			bcopy(sc->sc_cur_rateset, txdesc->rates,
1557 			    sizeof(txdesc->rates));
1558 		}
1559 		txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
1560 		txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
1561 
1562 #if NBPFILTER > 0
1563 		if (sc->sc_drvbpf != NULL) {
1564 			struct mbuf mb;
1565 			struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
1566 
1567 			tap->wt_flags = 0;
1568 			tap->wt_rate = 0;	/* TODO: where to get from? */
1569 			tap->wt_chan_freq =
1570 			    htole16(ic->ic_bss->ni_chan->ic_freq);
1571 			tap->wt_chan_flags =
1572 			    htole16(ic->ic_bss->ni_chan->ic_flags);
1573 
1574 			mb.m_data = (caddr_t)tap;
1575 			mb.m_len = sc->sc_txtap_len;
1576 			mb.m_next = m;
1577 			mb.m_nextpkt = NULL;
1578 			mb.m_type = 0;
1579 			mb.m_flags = 0;
1580 			bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1581 		}
1582 #endif
1583 		/* copy frame below our TX descriptor header */
1584 		m_copydata(m, 0, m->m_pkthdr.len,
1585 		    data_tx->buf + (sizeof(*mem) + sizeof(*txdesc)));
1586 
1587 		/* calculate frame size */
1588 		len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
1589 
1590 		/* we need to align the frame to a 4 byte boundary */
1591 		len = (len + 3) & ~3;
1592 
1593 		/* calculate frame checksum */
1594 		mem->chksum = upgt_chksum_le((uint32_t *)txdesc,
1595 		    len - sizeof(*mem));
1596 
1597 		/* we do not need the mbuf anymore */
1598 		m_freem(m);
1599 		data_tx->m = NULL;
1600 
1601 		DPRINTF(2, "%s: TX start data sending\n", sc->sc_dev.dv_xname);
1602 
1603 		usbd_setup_xfer(data_tx->xfer, sc->sc_tx_pipeh, data_tx,
1604 		    data_tx->buf, len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1605 		    UPGT_USB_TIMEOUT, NULL);
1606 		error = usbd_transfer(data_tx->xfer);
1607 		if (error != 0 && error != USBD_IN_PROGRESS) {
1608 			printf("%s: could not transmit TX data URB!\n",
1609 			    sc->sc_dev.dv_xname);
1610 			return;
1611 		}
1612 
1613 		DPRINTF(2, "%s: TX sent (%d bytes)\n",
1614 		    sc->sc_dev.dv_xname, len);
1615 	}
1616 
1617 	/*
1618 	 * If we don't regulary read the device statistics, the RX queue
1619 	 * will stall.  It's strange, but it works, so we keep reading
1620 	 * the statistics here.  *shrug*
1621 	 */
1622 	upgt_get_stats(sc);
1623 
1624 	splx(s);
1625 }
1626 
1627 void
1628 upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1629 {
1630 	struct ieee80211com *ic = &sc->sc_ic;
1631 	struct ifnet *ifp = &ic->ic_if;
1632 	struct upgt_lmac_tx_done_desc *desc;
1633 	int i, s;
1634 
1635 	s = splnet();
1636 
1637 	desc = (struct upgt_lmac_tx_done_desc *)data;
1638 
1639 	for (i = 0; i < UPGT_TX_COUNT; i++) {
1640 		struct upgt_data *data_tx = &sc->tx_data[i];
1641 
1642 		if (data_tx->addr == letoh32(desc->header2.reqid)) {
1643 			upgt_mem_free(sc, data_tx->addr);
1644 			ieee80211_release_node(ic, data_tx->ni);
1645 			data_tx->ni = NULL;
1646 			data_tx->addr = 0;
1647 
1648 			sc->tx_queued--;
1649 			ifp->if_opackets++;
1650 
1651 			DPRINTF(2, "%s: TX done: ", sc->sc_dev.dv_xname);
1652 			DPRINTF(2, "memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1653 			    letoh32(desc->header2.reqid),
1654 			    letoh16(desc->status),
1655 			    letoh16(desc->rssi));
1656 			DPRINTF(2, "seq=%d\n", letoh16(desc->seq));
1657 			break;
1658 		}
1659 	}
1660 
1661 	if (sc->tx_queued == 0) {
1662 		/* TX queued was processed, continue */
1663 		ifp->if_timer = 0;
1664 		ifp->if_flags &= ~IFF_OACTIVE;
1665 		upgt_start(ifp);
1666 	}
1667 
1668 	splx(s);
1669 }
1670 
1671 void
1672 upgt_rx_cb(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1673 {
1674 	struct upgt_data *data_rx = priv;
1675 	struct upgt_softc *sc = data_rx->sc;
1676 	int len;
1677 	struct upgt_lmac_header *header;
1678 	struct upgt_lmac_eeprom *eeprom;
1679 	uint8_t h1_type;
1680 	uint16_t h2_type;
1681 
1682 	DPRINTF(3, "%s: %s\n", sc->sc_dev.dv_xname, __func__);
1683 
1684 	if (status != USBD_NORMAL_COMPLETION) {
1685 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1686 			return;
1687 		if (status == USBD_STALLED)
1688 			usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
1689 		goto skip;
1690 	}
1691 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1692 
1693 	/*
1694 	 * Check what type of frame came in.
1695 	 */
1696 	header = (struct upgt_lmac_header *)(data_rx->buf + 4);
1697 
1698 	h1_type = header->header1.type;
1699 	h2_type = letoh16(header->header2.type);
1700 
1701 	if (h1_type == UPGT_H1_TYPE_CTRL &&
1702 	    h2_type == UPGT_H2_TYPE_EEPROM) {
1703 		eeprom = (struct upgt_lmac_eeprom *)(data_rx->buf + 4);
1704 		uint16_t eeprom_offset = letoh16(eeprom->offset);
1705 		uint16_t eeprom_len = letoh16(eeprom->len);
1706 
1707 		DPRINTF(2, "%s: received EEPROM block (offset=%d, len=%d)\n",
1708 			sc->sc_dev.dv_xname, eeprom_offset, eeprom_len);
1709 
1710 		bcopy(data_rx->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1711 			sc->sc_eeprom + eeprom_offset, eeprom_len);
1712 
1713 		/* EEPROM data has arrived in time, wakeup tsleep() */
1714 		wakeup(sc);
1715 	} else
1716 	if (h1_type == UPGT_H1_TYPE_CTRL &&
1717 	    h2_type == UPGT_H2_TYPE_TX_DONE) {
1718 		DPRINTF(2, "%s: received 802.11 TX done\n",
1719 		    sc->sc_dev.dv_xname);
1720 
1721 		upgt_tx_done(sc, data_rx->buf + 4);
1722 	} else
1723 	if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1724 	    h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1725 		DPRINTF(3, "%s: received 802.11 RX data\n",
1726 		    sc->sc_dev.dv_xname);
1727 
1728 		upgt_rx(sc, data_rx->buf + 4, letoh16(header->header1.len));
1729 	} else
1730 	if (h1_type == UPGT_H1_TYPE_CTRL &&
1731 	    h2_type == UPGT_H2_TYPE_STATS) {
1732 		DPRINTF(2, "%s: received statistic data\n",
1733 		    sc->sc_dev.dv_xname);
1734 
1735 		/* TODO: what could we do with the statistic data? */
1736 	} else {
1737 		/* ignore unknown frame types */
1738 		DPRINTF(1, "%s: received unknown frame type 0x%02x\n",
1739 		    sc->sc_dev.dv_xname, header->header1.type);
1740 	}
1741 
1742 skip:	/* setup new transfer */
1743 	usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf, MCLBYTES,
1744 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb);
1745 	(void)usbd_transfer(xfer);
1746 }
1747 
1748 void
1749 upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen)
1750 {
1751 	struct ieee80211com *ic = &sc->sc_ic;
1752 	struct ifnet *ifp = &ic->ic_if;
1753 	struct upgt_lmac_rx_desc *rxdesc;
1754 	struct ieee80211_frame *wh;
1755 	struct ieee80211_rxinfo rxi;
1756 	struct ieee80211_node *ni;
1757 	struct mbuf *m;
1758 	int s;
1759 
1760 	/* access RX packet descriptor */
1761 	rxdesc = (struct upgt_lmac_rx_desc *)data;
1762 
1763 	/* create mbuf which is suitable for strict alignment archs */
1764 	m = m_devget(rxdesc->data, pkglen, ETHER_ALIGN, ifp, NULL);
1765 	if (m == NULL) {
1766 		DPRINTF(1, "%s: could not create RX mbuf!\n", sc->sc_dev.dv_xname);
1767 		ifp->if_ierrors++;
1768 		return;
1769 	}
1770 
1771 	s = splnet();
1772 
1773 #if NBPFILTER > 0
1774 	if (sc->sc_drvbpf != NULL) {
1775 		struct mbuf mb;
1776 		struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1777 
1778 		tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
1779 		tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1780 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1781 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1782 		tap->wr_antsignal = rxdesc->rssi;
1783 
1784 		mb.m_data = (caddr_t)tap;
1785 		mb.m_len = sc->sc_rxtap_len;
1786 		mb.m_next = m;
1787 		mb.m_nextpkt = NULL;
1788 		mb.m_type = 0;
1789 		mb.m_flags = 0;
1790 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
1791 	}
1792 #endif
1793 	/* trim FCS */
1794 	m_adj(m, -IEEE80211_CRC_LEN);
1795 
1796 	wh = mtod(m, struct ieee80211_frame *);
1797 	ni = ieee80211_find_rxnode(ic, wh);
1798 
1799 	/* push the frame up to the 802.11 stack */
1800 	rxi.rxi_flags = 0;
1801 	rxi.rxi_rssi = rxdesc->rssi;
1802 	rxi.rxi_tstamp = 0;	/* unused */
1803 	ieee80211_input(ifp, m, ni, &rxi);
1804 
1805 	/* node is no longer needed */
1806 	ieee80211_release_node(ic, ni);
1807 
1808 	splx(s);
1809 
1810 	DPRINTF(3, "%s: RX done\n", sc->sc_dev.dv_xname);
1811 }
1812 
1813 void
1814 upgt_setup_rates(struct upgt_softc *sc)
1815 {
1816 	struct ieee80211com *ic = &sc->sc_ic;
1817 
1818 	/*
1819 	 * 0x01 = OFMD6   0x10 = DS1
1820 	 * 0x04 = OFDM9   0x11 = DS2
1821 	 * 0x06 = OFDM12  0x12 = DS5
1822 	 * 0x07 = OFDM18  0x13 = DS11
1823 	 * 0x08 = OFDM24
1824 	 * 0x09 = OFDM36
1825 	 * 0x0a = OFDM48
1826 	 * 0x0b = OFDM54
1827 	 */
1828 	const uint8_t rateset_auto_11b[] =
1829 	    { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 };
1830 	const uint8_t rateset_auto_11g[] =
1831 	    { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 };
1832 	const uint8_t rateset_fix_11bg[] =
1833 	    { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07,
1834 	      0x08, 0x09, 0x0a, 0x0b };
1835 
1836 	if (ic->ic_fixed_rate == -1) {
1837 		/*
1838 		 * Automatic rate control is done by the device.
1839 		 * We just pass the rateset from which the device
1840 		 * will pickup a rate.
1841 		 */
1842 		if (ic->ic_curmode == IEEE80211_MODE_11B)
1843 			bcopy(rateset_auto_11b, sc->sc_cur_rateset,
1844 			    sizeof(sc->sc_cur_rateset));
1845 		if (ic->ic_curmode == IEEE80211_MODE_11G ||
1846 		    ic->ic_curmode == IEEE80211_MODE_AUTO)
1847 			bcopy(rateset_auto_11g, sc->sc_cur_rateset,
1848 			    sizeof(sc->sc_cur_rateset));
1849 	} else {
1850 		/* set a fixed rate */
1851 		memset(sc->sc_cur_rateset, rateset_fix_11bg[ic->ic_fixed_rate],
1852 		    sizeof(sc->sc_cur_rateset));
1853 	}
1854 }
1855 
1856 uint8_t
1857 upgt_rx_rate(struct upgt_softc *sc, const int rate)
1858 {
1859 	struct ieee80211com *ic = &sc->sc_ic;
1860 
1861 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1862 		if (rate < 0 || rate > 3)
1863 			/* invalid rate */
1864 			return (0);
1865 
1866 		switch (rate) {
1867 		case 0:
1868 			return (2);
1869 		case 1:
1870 			return (4);
1871 		case 2:
1872 			return (11);
1873 		case 3:
1874 			return (22);
1875 		default:
1876 			return (0);
1877 		}
1878 	}
1879 
1880 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
1881 		if (rate < 0 || rate > 11)
1882 			/* invalid rate */
1883 			return (0);
1884 
1885 		switch (rate) {
1886 		case 0:
1887 			return (2);
1888 		case 1:
1889 			return (4);
1890 		case 2:
1891 			return (11);
1892 		case 3:
1893 			return (22);
1894 		case 4:
1895 			return (12);
1896 		case 5:
1897 			return (18);
1898 		case 6:
1899 			return (24);
1900 		case 7:
1901 			return (36);
1902 		case 8:
1903 			return (48);
1904 		case 9:
1905 			return (72);
1906 		case 10:
1907 			return (96);
1908 		case 11:
1909 			return (108);
1910 		default:
1911 			return (0);
1912 		}
1913 	}
1914 
1915 	return (0);
1916 }
1917 
1918 int
1919 upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
1920 {
1921 	struct ieee80211com *ic = &sc->sc_ic;
1922 	struct ieee80211_node *ni = ic->ic_bss;
1923 	struct upgt_data *data_cmd = &sc->cmd_data;
1924 	struct upgt_lmac_mem *mem;
1925 	struct upgt_lmac_filter *filter;
1926 	int len;
1927 	uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1928 
1929 	/*
1930 	 * Transmit the URB containing the CMD data.
1931 	 */
1932 	bzero(data_cmd->buf, MCLBYTES);
1933 
1934 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
1935 	mem->addr = htole32(sc->sc_memaddr_frame_start +
1936 	    UPGT_MEMSIZE_FRAME_HEAD);
1937 
1938 	filter = (struct upgt_lmac_filter *)(mem + 1);
1939 
1940 	filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
1941 	filter->header1.type = UPGT_H1_TYPE_CTRL;
1942 	filter->header1.len = htole16(
1943 	    sizeof(struct upgt_lmac_filter) -
1944 	    sizeof(struct upgt_lmac_header));
1945 
1946 	filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1947 	filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
1948 	filter->header2.flags = 0;
1949 
1950 	switch (state) {
1951 	case IEEE80211_S_INIT:
1952 		DPRINTF(1, "%s: set MAC filter to INIT\n",
1953 		    sc->sc_dev.dv_xname);
1954 
1955 		filter->type = htole16(UPGT_FILTER_TYPE_RESET);
1956 		break;
1957 	case IEEE80211_S_SCAN:
1958 		DPRINTF(1, "%s: set MAC filter to SCAN (bssid %s)\n",
1959 		    sc->sc_dev.dv_xname, ether_sprintf(broadcast));
1960 
1961 		filter->type = htole16(UPGT_FILTER_TYPE_NONE);
1962 		IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr);
1963 		IEEE80211_ADDR_COPY(filter->src, broadcast);
1964 		filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
1965 		filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
1966 		filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
1967 		filter->rxhw = htole32(sc->sc_eeprom_hwrx);
1968 		filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
1969 		break;
1970 	case IEEE80211_S_RUN:
1971 		DPRINTF(1, "%s: set MAC filter to RUN (bssid %s)\n",
1972 		    sc->sc_dev.dv_xname, ether_sprintf(ni->ni_bssid));
1973 
1974 		filter->type = htole16(UPGT_FILTER_TYPE_STA);
1975 		IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr);
1976 		IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
1977 		filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
1978 		filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
1979 		filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
1980 		filter->rxhw = htole32(sc->sc_eeprom_hwrx);
1981 		filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
1982 		break;
1983 	default:
1984 		printf("%s: MAC filter does not know that state!\n",
1985 		    sc->sc_dev.dv_xname);
1986 		break;
1987 	}
1988 
1989 	len = sizeof(*mem) + sizeof(*filter);
1990 
1991 	mem->chksum = upgt_chksum_le((uint32_t *)filter,
1992 	    len - sizeof(*mem));
1993 
1994 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
1995 		printf("%s: could not transmit macfilter CMD data URB!\n",
1996 		    sc->sc_dev.dv_xname);
1997 		return (EIO);
1998 	}
1999 
2000 	return (0);
2001 }
2002 
2003 int
2004 upgt_set_channel(struct upgt_softc *sc, unsigned channel)
2005 {
2006 	struct upgt_data *data_cmd = &sc->cmd_data;
2007 	struct upgt_lmac_mem *mem;
2008 	struct upgt_lmac_channel *chan;
2009 	int len;
2010 
2011 	DPRINTF(1, "%s: %s: %d\n", sc->sc_dev.dv_xname, __func__, channel);
2012 
2013 	/*
2014 	 * Transmit the URB containing the CMD data.
2015 	 */
2016 	bzero(data_cmd->buf, MCLBYTES);
2017 
2018 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
2019 	mem->addr = htole32(sc->sc_memaddr_frame_start +
2020 	    UPGT_MEMSIZE_FRAME_HEAD);
2021 
2022 	chan = (struct upgt_lmac_channel *)(mem + 1);
2023 
2024 	chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
2025 	chan->header1.type = UPGT_H1_TYPE_CTRL;
2026 	chan->header1.len = htole16(
2027 	    sizeof(struct upgt_lmac_channel) -
2028 	    sizeof(struct upgt_lmac_header));
2029 
2030 	chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
2031 	chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
2032 	chan->header2.flags = 0;
2033 
2034 	chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
2035 	chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
2036 	chan->freq6 = sc->sc_eeprom_freq6[channel];
2037 	chan->settings = sc->sc_eeprom_freq6_settings;
2038 	chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
2039 
2040 	bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_1,
2041 	    sizeof(chan->freq3_1));
2042 
2043 	bcopy(&sc->sc_eeprom_freq4[channel], chan->freq4,
2044 	    sizeof(sc->sc_eeprom_freq4[channel]));
2045 
2046 	bcopy(&sc->sc_eeprom_freq3[channel].data, chan->freq3_2,
2047 	    sizeof(chan->freq3_2));
2048 
2049 	len = sizeof(*mem) + sizeof(*chan);
2050 
2051 	mem->chksum = upgt_chksum_le((uint32_t *)chan,
2052 	    len - sizeof(*mem));
2053 
2054 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2055 		printf("%s: could not transmit channel CMD data URB!\n",
2056 		    sc->sc_dev.dv_xname);
2057 		return (EIO);
2058 	}
2059 
2060 	return (0);
2061 }
2062 
2063 void
2064 upgt_set_led(struct upgt_softc *sc, int action)
2065 {
2066 	struct ieee80211com *ic = &sc->sc_ic;
2067 	struct upgt_data *data_cmd = &sc->cmd_data;
2068 	struct upgt_lmac_mem *mem;
2069 	struct upgt_lmac_led *led;
2070 	struct timeval t;
2071 	int len;
2072 
2073 	/*
2074 	 * Transmit the URB containing the CMD data.
2075 	 */
2076 	bzero(data_cmd->buf, MCLBYTES);
2077 
2078 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
2079 	mem->addr = htole32(sc->sc_memaddr_frame_start +
2080 	    UPGT_MEMSIZE_FRAME_HEAD);
2081 
2082 	led = (struct upgt_lmac_led *)(mem + 1);
2083 
2084 	led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
2085 	led->header1.type = UPGT_H1_TYPE_CTRL;
2086 	led->header1.len = htole16(
2087 	    sizeof(struct upgt_lmac_led) -
2088 	    sizeof(struct upgt_lmac_header));
2089 
2090 	led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
2091 	led->header2.type = htole16(UPGT_H2_TYPE_LED);
2092 	led->header2.flags = 0;
2093 
2094 	switch (action) {
2095 	case UPGT_LED_OFF:
2096 		led->mode = htole16(UPGT_LED_MODE_SET);
2097 		led->action_fix = 0;
2098 		led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
2099 		led->action_tmp_dur = 0;
2100 		break;
2101 	case UPGT_LED_ON:
2102 		led->mode = htole16(UPGT_LED_MODE_SET);
2103 		led->action_fix = 0;
2104 		led->action_tmp = htole16(UPGT_LED_ACTION_ON);
2105 		led->action_tmp_dur = 0;
2106 		break;
2107 	case UPGT_LED_BLINK:
2108 		if (ic->ic_state != IEEE80211_S_RUN)
2109 			return;
2110 		if (sc->sc_led_blink)
2111 			/* previous blink was not finished */
2112 			return;
2113 		led->mode = htole16(UPGT_LED_MODE_SET);
2114 		led->action_fix = htole16(UPGT_LED_ACTION_OFF);
2115 		led->action_tmp = htole16(UPGT_LED_ACTION_ON);
2116 		led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
2117 		/* lock blink */
2118 		sc->sc_led_blink = 1;
2119 		t.tv_sec = 0;
2120 		t.tv_usec = UPGT_LED_ACTION_TMP_DUR * 1000L;
2121 		timeout_add(&sc->led_to, tvtohz(&t));
2122 		break;
2123 	default:
2124 		return;
2125 	}
2126 
2127 	len = sizeof(*mem) + sizeof(*led);
2128 
2129 	mem->chksum = upgt_chksum_le((uint32_t *)led,
2130 	    len - sizeof(*mem));
2131 
2132 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2133 		printf("%s: could not transmit led CMD URB!\n",
2134 		    sc->sc_dev.dv_xname);
2135 	}
2136 }
2137 
2138 void
2139 upgt_set_led_blink(void *arg)
2140 {
2141 	struct upgt_softc *sc = arg;
2142 
2143 	/* blink finished, we are ready for a next one */
2144 	sc->sc_led_blink = 0;
2145 	timeout_del(&sc->led_to);
2146 }
2147 
2148 int
2149 upgt_get_stats(struct upgt_softc *sc)
2150 {
2151 	struct upgt_data *data_cmd = &sc->cmd_data;
2152 	struct upgt_lmac_mem *mem;
2153 	struct upgt_lmac_stats *stats;
2154 	int len;
2155 
2156 	/*
2157 	 * Transmit the URB containing the CMD data.
2158 	 */
2159 	bzero(data_cmd->buf, MCLBYTES);
2160 
2161 	mem = (struct upgt_lmac_mem *)data_cmd->buf;
2162 	mem->addr = htole32(sc->sc_memaddr_frame_start +
2163 	    UPGT_MEMSIZE_FRAME_HEAD);
2164 
2165 	stats = (struct upgt_lmac_stats *)(mem + 1);
2166 
2167 	stats->header1.flags = 0;
2168 	stats->header1.type = UPGT_H1_TYPE_CTRL;
2169 	stats->header1.len = htole16(
2170 	    sizeof(struct upgt_lmac_stats) -
2171 	    sizeof(struct upgt_lmac_header));
2172 
2173 	stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
2174 	stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
2175 	stats->header2.flags = 0;
2176 
2177 	len = sizeof(*mem) + sizeof(*stats);
2178 
2179 	mem->chksum = upgt_chksum_le((uint32_t *)stats,
2180 	    len - sizeof(*mem));
2181 
2182 	if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2183 		printf("%s: could not transmit statistics CMD data URB!\n",
2184 		    sc->sc_dev.dv_xname);
2185 		return (EIO);
2186 	}
2187 
2188 	return (0);
2189 
2190 }
2191 
2192 int
2193 upgt_alloc_tx(struct upgt_softc *sc)
2194 {
2195 	int i;
2196 
2197 	sc->tx_queued = 0;
2198 
2199 	for (i = 0; i < UPGT_TX_COUNT; i++) {
2200 		struct upgt_data *data_tx = &sc->tx_data[i];
2201 
2202 		data_tx->sc = sc;
2203 
2204 		data_tx->xfer = usbd_alloc_xfer(sc->sc_udev);
2205 		if (data_tx->xfer == NULL) {
2206 			printf("%s: could not allocate TX xfer!\n",
2207 			    sc->sc_dev.dv_xname);
2208 			return (ENOMEM);
2209 		}
2210 
2211 		data_tx->buf = usbd_alloc_buffer(data_tx->xfer, MCLBYTES);
2212 		if (data_tx->buf == NULL) {
2213 			printf("%s: could not allocate TX buffer!\n",
2214 			    sc->sc_dev.dv_xname);
2215 			return (ENOMEM);
2216 		}
2217 
2218 		bzero(data_tx->buf, MCLBYTES);
2219 	}
2220 
2221 	return (0);
2222 }
2223 
2224 int
2225 upgt_alloc_rx(struct upgt_softc *sc)
2226 {
2227 	struct upgt_data *data_rx = &sc->rx_data;
2228 
2229 	data_rx->sc = sc;
2230 
2231 	data_rx->xfer = usbd_alloc_xfer(sc->sc_udev);
2232 	if (data_rx->xfer == NULL) {
2233 		printf("%s: could not allocate RX xfer!\n",
2234 		    sc->sc_dev.dv_xname);
2235 		return (ENOMEM);
2236 	}
2237 
2238 	data_rx->buf = usbd_alloc_buffer(data_rx->xfer, MCLBYTES);
2239 	if (data_rx->buf == NULL) {
2240 		printf("%s: could not allocate RX buffer!\n",
2241 		    sc->sc_dev.dv_xname);
2242 		return (ENOMEM);
2243 	}
2244 
2245 	bzero(data_rx->buf, MCLBYTES);
2246 
2247 	return (0);
2248 }
2249 
2250 int
2251 upgt_alloc_cmd(struct upgt_softc *sc)
2252 {
2253 	struct upgt_data *data_cmd = &sc->cmd_data;
2254 
2255 	data_cmd->sc = sc;
2256 
2257 	data_cmd->xfer = usbd_alloc_xfer(sc->sc_udev);
2258 	if (data_cmd->xfer == NULL) {
2259 		printf("%s: could not allocate RX xfer!\n",
2260 		    sc->sc_dev.dv_xname);
2261 		return (ENOMEM);
2262 	}
2263 
2264 	data_cmd->buf = usbd_alloc_buffer(data_cmd->xfer, MCLBYTES);
2265 	if (data_cmd->buf == NULL) {
2266 		printf("%s: could not allocate RX buffer!\n",
2267 		    sc->sc_dev.dv_xname);
2268 		return (ENOMEM);
2269 	}
2270 
2271 	bzero(data_cmd->buf, MCLBYTES);
2272 
2273 	return (0);
2274 }
2275 
2276 void
2277 upgt_free_tx(struct upgt_softc *sc)
2278 {
2279 	int i;
2280 
2281 	for (i = 0; i < UPGT_TX_COUNT; i++) {
2282 		struct upgt_data *data_tx = &sc->tx_data[i];
2283 
2284 		if (data_tx->xfer != NULL) {
2285 			usbd_free_xfer(data_tx->xfer);
2286 			data_tx->xfer = NULL;
2287 		}
2288 
2289 		data_tx->ni = NULL;
2290 	}
2291 }
2292 
2293 void
2294 upgt_free_rx(struct upgt_softc *sc)
2295 {
2296 	struct upgt_data *data_rx = &sc->rx_data;
2297 
2298 	if (data_rx->xfer != NULL) {
2299 		usbd_free_xfer(data_rx->xfer);
2300 		data_rx->xfer = NULL;
2301 	}
2302 
2303 	data_rx->ni = NULL;
2304 }
2305 
2306 void
2307 upgt_free_cmd(struct upgt_softc *sc)
2308 {
2309 	struct upgt_data *data_cmd = &sc->cmd_data;
2310 
2311 	if (data_cmd->xfer != NULL) {
2312 		usbd_free_xfer(data_cmd->xfer);
2313 		data_cmd->xfer = NULL;
2314 	}
2315 }
2316 
2317 int
2318 upgt_bulk_xmit(struct upgt_softc *sc, struct upgt_data *data,
2319     usbd_pipe_handle pipeh, uint32_t *size, int flags)
2320 {
2321         usbd_status status;
2322 
2323 	status = usbd_bulk_transfer(data->xfer, pipeh,
2324 	    USBD_NO_COPY | flags, UPGT_USB_TIMEOUT, data->buf, size,
2325 	    "upgt_bulk_xmit");
2326 	if (status != USBD_NORMAL_COMPLETION) {
2327 		printf("%s: %s: error %s!\n",
2328 		    sc->sc_dev.dv_xname, __func__, usbd_errstr(status));
2329 		return (EIO);
2330 	}
2331 
2332 	return (0);
2333 }
2334 
2335 void
2336 upgt_hexdump(void *buf, int len)
2337 {
2338 	int i;
2339 
2340 	for (i = 0; i < len; i++) {
2341 		if (i % 16 == 0)
2342 			printf("%s%5i:", i ? "\n" : "", i);
2343 		if (i % 4 == 0)
2344 			printf(" ");
2345 		printf("%02x", (int)*((u_char *)buf + i));
2346 	}
2347 	printf("\n");
2348 }
2349 
2350 uint32_t
2351 upgt_crc32_le(const void *buf, size_t size)
2352 {
2353 	uint32_t crc;
2354 
2355 	crc = ether_crc32_le(buf, size);
2356 
2357 	/* apply final XOR value as common for CRC-32 */
2358 	crc = htole32(crc ^ 0xffffffffU);
2359 
2360 	return (crc);
2361 }
2362 
2363 /*
2364  * The firmware awaits a checksum for each frame we send to it.
2365  * The algorithm used therefor is uncommon but somehow similar to CRC32.
2366  */
2367 uint32_t
2368 upgt_chksum_le(const uint32_t *buf, size_t size)
2369 {
2370 	int i;
2371 	uint32_t crc = 0;
2372 
2373 	for (i = 0; i < size; i += sizeof(uint32_t)) {
2374 		crc = htole32(crc ^ *buf++);
2375 		crc = htole32((crc >> 5) ^ (crc << 3));
2376 	}
2377 
2378 	return (crc);
2379 }
2380