xref: /openbsd/sys/dev/usb/if_atu.c (revision 9b7c3dbb)
1 /*	$OpenBSD: if_atu.c,v 1.119 2016/04/13 11:03:37 mpi Exp $ */
2 /*
3  * Copyright (c) 2003, 2004
4  *	Daan Vreeken <Danovitsch@Vitsch.net>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Daan Vreeken.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Daan Vreeken AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Daan Vreeken OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Atmel AT76c503 / AT76c503a / AT76c505 / AT76c505a  USB WLAN driver
36  * version 0.5 - 2004-08-03
37  *
38  * Originally written by Daan Vreeken <Danovitsch @ Vitsch . net>
39  *  http://vitsch.net/bsd/atuwi
40  *
41  * Contributed to by :
42  *  Chris Whitehouse, Alistair Phillips, Peter Pilka, Martijn van Buul,
43  *  Suihong Liang, Arjan van Leeuwen, Stuart Walsh
44  *
45  * Ported to OpenBSD by Theo de Raadt and David Gwynne.
46  */
47 
48 #include "bpfilter.h"
49 
50 #include <sys/param.h>
51 #include <sys/sockio.h>
52 #include <sys/mbuf.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/systm.h>
56 #include <sys/timeout.h>
57 #include <sys/queue.h>
58 #include <sys/device.h>
59 
60 #include <machine/bus.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdivar.h>
66 
67 #include <dev/usb/usbdevs.h>
68 
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72 
73 #include <net/if.h>
74 #include <net/if_media.h>
75 
76 #include <netinet/in.h>
77 #include <netinet/if_ether.h>
78 
79 #include <net80211/ieee80211_var.h>
80 #include <net80211/ieee80211_radiotap.h>
81 
82 #include <dev/usb/if_atureg.h>
83 
84 #ifdef ATU_DEBUG
85 #define DPRINTF(x)	do { if (atudebug) printf x; } while (0)
86 #define DPRINTFN(n,x)	do { if (atudebug>(n)) printf x; } while (0)
87 int atudebug = 1;
88 #else
89 #define DPRINTF(x)
90 #define DPRINTFN(n,x)
91 #endif
92 
93 int atu_match(struct device *, void *, void *);
94 void atu_attach(struct device *, struct device *, void *);
95 int atu_detach(struct device *, int);
96 
97 struct cfdriver atu_cd = {
98 	NULL, "atu", DV_IFNET
99 };
100 
101 const struct cfattach atu_ca = {
102 	sizeof(struct atu_softc), atu_match, atu_attach, atu_detach
103 };
104 
105 /*
106  * Various supported device vendors/products/radio type.
107  */
108 struct atu_type atu_devs[] = {
109 	{ USB_VENDOR_3COM,	USB_PRODUCT_3COM_3CRSHEW696,
110 	  RadioRFMD,		ATU_NO_QUIRK },
111 	{ USB_VENDOR_ABOCOM,	USB_PRODUCT_ABOCOM_BWU613,
112 	  RadioRFMD,		ATU_NO_QUIRK },
113 	{ USB_VENDOR_ACCTON,	USB_PRODUCT_ACCTON_2664W,
114 	  AT76C503_rfmd_acc,	ATU_NO_QUIRK },
115 	{ USB_VENDOR_ACERP,	USB_PRODUCT_ACERP_AWL300,
116 	  RadioIntersil,	ATU_NO_QUIRK },
117 	{ USB_VENDOR_ACERP,	USB_PRODUCT_ACERP_AWL400,
118 	  RadioRFMD,		ATU_NO_QUIRK },
119 	{ USB_VENDOR_ACTIONTEC,	USB_PRODUCT_ACTIONTEC_802UAT1,
120 	  RadioRFMD,		ATU_NO_QUIRK },
121 	{ USB_VENDOR_ADDTRON,	USB_PRODUCT_ADDTRON_AWU120,
122 	  RadioIntersil,	ATU_NO_QUIRK },
123 	{ USB_VENDOR_AINCOMM,	USB_PRODUCT_AINCOMM_AWU2000B,
124 	  RadioRFMD2958,	ATU_NO_QUIRK },
125 	{ USB_VENDOR_ASKEY,	USB_PRODUCT_ASKEY_VOYAGER1010,
126 	  RadioIntersil,	ATU_NO_QUIRK },
127 	{ USB_VENDOR_ASKEY,	USB_PRODUCT_ASKEY_WLL013I,
128 	  RadioIntersil,	ATU_NO_QUIRK },
129 	{ USB_VENDOR_ASKEY,	USB_PRODUCT_ASKEY_WLL013,
130 	  RadioRFMD,		ATU_NO_QUIRK },
131 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C503I1,
132 	  RadioIntersil,	ATU_NO_QUIRK },
133 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C503I2,
134 	  AT76C503_i3863,	ATU_NO_QUIRK },
135 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C503RFMD,
136 	  RadioRFMD,		ATU_NO_QUIRK },
137 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C505RFMD,
138 	  AT76C505_rfmd,	ATU_NO_QUIRK },
139 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C505RFMD2958,
140 	  RadioRFMD2958,	ATU_NO_QUIRK },
141 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C505A, /* SMC2662 V.4 */
142 	  RadioRFMD2958_SMC,	ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
143 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_AT76C505AS, /* quirk? */
144 	  RadioRFMD2958_SMC,	ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
145 	{ USB_VENDOR_ATMEL,	USB_PRODUCT_ATMEL_WN210,
146 	  RadioRFMD,		ATU_NO_QUIRK },
147 	{ USB_VENDOR_BELKIN,	USB_PRODUCT_BELKIN_F5D6050,
148 	  RadioRFMD,		ATU_NO_QUIRK },
149 	{ USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C11U,
150 	  RadioIntersil,	ATU_NO_QUIRK },
151 	{ USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_WL210,
152 	  RadioIntersil,	ATU_NO_QUIRK },
153 	{ USB_VENDOR_COMPAQ,	USB_PRODUCT_COMPAQ_IPAQWLAN,
154 	  RadioRFMD,		ATU_NO_QUIRK },
155 	{ USB_VENDOR_COREGA,	USB_PRODUCT_COREGA_WLUSB_11_STICK,
156 	  RadioRFMD2958,	ATU_NO_QUIRK },
157 	{ USB_VENDOR_DICKSMITH,	USB_PRODUCT_DICKSMITH_CHUSB611G,
158 	  RadioRFMD2958,	ATU_NO_QUIRK },
159 	{ USB_VENDOR_DICKSMITH,	USB_PRODUCT_DICKSMITH_WL200U,
160 	  RadioRFMD,		ATU_NO_QUIRK },
161 	{ USB_VENDOR_DICKSMITH,	USB_PRODUCT_DICKSMITH_WL240U,
162 	  RadioRFMD2958,	ATU_NO_QUIRK },
163 	{ USB_VENDOR_DICKSMITH,	USB_PRODUCT_DICKSMITH_XH1153,
164 	  RadioRFMD,		ATU_NO_QUIRK },
165 	{ USB_VENDOR_DLINK,	USB_PRODUCT_DLINK_DWL120E,
166 	  RadioRFMD,		ATU_NO_QUIRK },
167 	{ USB_VENDOR_GIGABYTE,	USB_PRODUCT_GIGABYTE_GNWLBM101,
168 	  RadioRFMD,		ATU_NO_QUIRK },
169 	{ USB_VENDOR_GIGASET,	USB_PRODUCT_GIGASET_WLAN, /* quirk? */
170 	  RadioRFMD2958_SMC,	ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
171 	{ USB_VENDOR_HP,	USB_PRODUCT_HP_HN210W,
172 	  RadioIntersil,	ATU_NO_QUIRK },
173 	{ USB_VENDOR_INTEL,	USB_PRODUCT_INTEL_AP310,
174 	  RadioIntersil,	ATU_NO_QUIRK },
175 	{ USB_VENDOR_IODATA,	USB_PRODUCT_IODATA_USBWNB11A,
176 	  RadioIntersil,	ATU_NO_QUIRK },
177 	{ USB_VENDOR_LEXAR,	USB_PRODUCT_LEXAR_2662WAR,
178 	  RadioRFMD,		ATU_NO_QUIRK },
179 	{ USB_VENDOR_LINKSYS,	USB_PRODUCT_LINKSYS_WUSB11,
180 	  RadioIntersil,	ATU_NO_QUIRK },
181 	{ USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_WUSB11,
182 	  RadioRFMD,		ATU_NO_QUIRK },
183 	{ USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_NWU11B,
184 	  RadioRFMD,		ATU_NO_QUIRK },
185 	{ USB_VENDOR_LINKSYS3,	USB_PRODUCT_LINKSYS3_WUSB11V28,
186 	  RadioRFMD2958,	ATU_NO_QUIRK },
187 	{ USB_VENDOR_MSI,	USB_PRODUCT_MSI_WLAN,
188 	  RadioRFMD2958,	ATU_NO_QUIRK },
189 	{ USB_VENDOR_NETGEAR2,	USB_PRODUCT_NETGEAR2_MA101,
190 	  RadioIntersil,	ATU_NO_QUIRK },
191 	{ USB_VENDOR_NETGEAR2,	USB_PRODUCT_NETGEAR2_MA101B,
192 	  RadioRFMD,		ATU_NO_QUIRK },
193 	{ USB_VENDOR_OQO,	USB_PRODUCT_OQO_WIFI01,
194 	  RadioRFMD2958_SMC,	ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
195 	{ USB_VENDOR_PLANEX2,	USB_PRODUCT_PLANEX2_GW_US11S,
196 	  RadioRFMD,		ATU_NO_QUIRK },
197 	{ USB_VENDOR_SAMSUNG,	USB_PRODUCT_SAMSUNG_SWL2100W,
198 	  AT76C503_i3863,	ATU_NO_QUIRK },
199 	{ USB_VENDOR_SIEMENS2,	USB_PRODUCT_SIEMENS2_WLL013,
200 	  RadioRFMD,		ATU_NO_QUIRK },
201 	{ USB_VENDOR_SMC3,	USB_PRODUCT_SMC3_2662WV1,
202 	  RadioIntersil,	ATU_NO_QUIRK },
203 	{ USB_VENDOR_SMC3,	USB_PRODUCT_SMC3_2662WV2,
204 	  AT76C503_rfmd_acc,	ATU_NO_QUIRK },
205 	{ USB_VENDOR_TEKRAM,	USB_PRODUCT_TEKRAM_U300C,
206 	  RadioIntersil,	ATU_NO_QUIRK },
207 	{ USB_VENDOR_ZCOM,	USB_PRODUCT_ZCOM_M4Y750,
208 	  RadioIntersil,	ATU_NO_QUIRK },
209 };
210 
211 struct atu_radfirm {
212 	enum	atu_radio_type atur_type;
213 	char	*atur_internal;
214 	char	*atur_external;
215 	u_int8_t max_rssi;
216 } atu_radfirm[] = {
217 	{ RadioRFMD,		"atu-rfmd-int",		"atu-rfmd-ext",	0 },
218 	{ RadioRFMD2958,	"atu-rfmd2958-int",	"atu-rfmd2958-ext", 81 },
219 	{ RadioRFMD2958_SMC,	"atu-rfmd2958smc-int",	"atu-rfmd2958smc-ext", 0 },
220 	{ RadioIntersil,	"atu-intersil-int",	"atu-intersil-ext", 0 },
221 	{
222 		AT76C503_i3863,
223 		"atu-at76c503-i3863-int",
224 		"atu-at76c503-i3863-ext",
225 		0
226 	},
227 	{
228 		AT76C503_rfmd_acc,
229 		"atu-at76c503-rfmd-acc-int",
230 		"atu-at76c503-rfmd-acc-ext",
231 		0
232 	},
233 	{
234 		AT76C505_rfmd,
235 		"atu-at76c505-rfmd-int",
236 		"atu-at76c505-rfmd-ext",
237 		0
238 	}
239 };
240 
241 int	atu_newbuf(struct atu_softc *, struct atu_chain *, struct mbuf *);
242 void	atu_rxeof(struct usbd_xfer *, void *, usbd_status);
243 void	atu_txeof(struct usbd_xfer *, void *, usbd_status);
244 void	atu_start(struct ifnet *);
245 int	atu_ioctl(struct ifnet *, u_long, caddr_t);
246 int	atu_init(struct ifnet *);
247 void	atu_stop(struct ifnet *, int);
248 void	atu_watchdog(struct ifnet *);
249 usbd_status atu_usb_request(struct atu_softc *sc, u_int8_t type,
250 	    u_int8_t request, u_int16_t value, u_int16_t index,
251 	    u_int16_t length, u_int8_t *data);
252 int	atu_send_command(struct atu_softc *sc, u_int8_t *command, int size);
253 int	atu_get_cmd_status(struct atu_softc *sc, u_int8_t cmd,
254 	    u_int8_t *status);
255 int	atu_wait_completion(struct atu_softc *sc, u_int8_t cmd,
256 	    u_int8_t *status);
257 int	atu_send_mib(struct atu_softc *sc, u_int8_t type,
258 	    u_int8_t size, u_int8_t index, void *data);
259 int	atu_get_mib(struct atu_softc *sc, u_int8_t type,
260 	    u_int8_t size, u_int8_t index, u_int8_t *buf);
261 #if 0
262 int	atu_start_ibss(struct atu_softc *sc);
263 #endif
264 int	atu_start_scan(struct atu_softc *sc);
265 int	atu_switch_radio(struct atu_softc *sc, int state);
266 int	atu_initial_config(struct atu_softc *sc);
267 int	atu_join(struct atu_softc *sc, struct ieee80211_node *node);
268 int8_t	atu_get_dfu_state(struct atu_softc *sc);
269 u_int8_t atu_get_opmode(struct atu_softc *sc, u_int8_t *mode);
270 void	atu_internal_firmware(struct device *);
271 void	atu_external_firmware(struct device *);
272 int	atu_get_card_config(struct atu_softc *sc);
273 int	atu_media_change(struct ifnet *ifp);
274 void	atu_media_status(struct ifnet *ifp, struct ifmediareq *req);
275 int	atu_tx_list_init(struct atu_softc *);
276 int	atu_rx_list_init(struct atu_softc *);
277 void	atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch,
278 	    int listlen);
279 
280 void atu_task(void *);
281 int atu_newstate(struct ieee80211com *, enum ieee80211_state, int);
282 int atu_tx_start(struct atu_softc *, struct ieee80211_node *,
283     struct atu_chain *, struct mbuf *);
284 void atu_complete_attach(struct atu_softc *);
285 u_int8_t atu_calculate_padding(int);
286 
287 usbd_status
288 atu_usb_request(struct atu_softc *sc, u_int8_t type,
289     u_int8_t request, u_int16_t value, u_int16_t index, u_int16_t length,
290     u_int8_t *data)
291 {
292 	usb_device_request_t	req;
293 	struct usbd_xfer	*xfer;
294 	usbd_status		err;
295 	int			total_len = 0, s;
296 
297 	req.bmRequestType = type;
298 	req.bRequest = request;
299 	USETW(req.wValue, value);
300 	USETW(req.wIndex, index);
301 	USETW(req.wLength, length);
302 
303 #ifdef ATU_DEBUG
304 	if (atudebug) {
305 		if ((data == NULL) || (type & UT_READ)) {
306 			DPRINTFN(20, ("%s: req=%02x val=%02x ind=%02x "
307 			    "len=%02x\n", sc->atu_dev.dv_xname, request,
308 			    value, index, length));
309 		} else {
310 			DPRINTFN(20, ("%s: req=%02x val=%02x ind=%02x "
311 			    "len=%02x [%8D]\n", sc->atu_dev.dv_xname,
312 			    request, value, index, length, data, " "));
313 		}
314 	}
315 #endif /* ATU_DEBUG */
316 
317 	s = splnet();
318 
319 	xfer = usbd_alloc_xfer(sc->atu_udev);
320 	usbd_setup_default_xfer(xfer, sc->atu_udev, 0, 500000, &req, data,
321 	    length, USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS, 0);
322 
323 	err = usbd_transfer(xfer);
324 
325 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
326 
327 #ifdef ATU_DEBUG
328 	if (atudebug) {
329 		if (type & UT_READ) {
330 			DPRINTFN(20, ("%s: transferred 0x%x bytes in\n",
331 			    sc->atu_dev.dv_xname, total_len));
332 			DPRINTFN(20, ("%s: dump [%10D]\n",
333 			    sc->atu_dev.dv_xname, data, " "));
334 		} else {
335 			if (total_len != length)
336 				DPRINTF(("%s: ARG! wrote only %x bytes\n",
337 				    sc->atu_dev.dv_xname, total_len));
338 		}
339 	}
340 #endif /* ATU_DEBUG */
341 
342 	usbd_free_xfer(xfer);
343 
344 	splx(s);
345 	return(err);
346 }
347 
348 int
349 atu_send_command(struct atu_softc *sc, u_int8_t *command, int size)
350 {
351 	return atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
352 	    0x0000, size, command);
353 }
354 
355 int
356 atu_get_cmd_status(struct atu_softc *sc, u_int8_t cmd, u_int8_t *status)
357 {
358 	/*
359 	 * all other drivers (including Windoze) request 40 bytes of status
360 	 * and get a short-xfer of just 6 bytes. we can save 34 bytes of
361 	 * buffer if we just request those 6 bytes in the first place :)
362 	 */
363 	/*
364 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
365 	    0x0000, 40, status);
366 	*/
367 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
368 	    0x0000, 6, status);
369 }
370 
371 int
372 atu_wait_completion(struct atu_softc *sc, u_int8_t cmd, u_int8_t *status)
373 {
374 	int			idle_count = 0, err;
375 	u_int8_t		statusreq[6];
376 
377 	DPRINTFN(15, ("%s: wait-completion: cmd=%02x\n",
378 	    sc->atu_dev.dv_xname, cmd));
379 
380 	while (1) {
381 		err = atu_get_cmd_status(sc, cmd, statusreq);
382 		if (err)
383 			return err;
384 
385 #ifdef ATU_DEBUG
386 		if (atudebug) {
387 			DPRINTFN(20, ("%s: status=%s cmd=%02x\n",
388 			    sc->atu_dev.dv_xname,
389 			ether_sprintf(statusreq), cmd));
390 		}
391 #endif /* ATU_DEBUG */
392 
393 		/*
394 		 * during normal operations waiting on STATUS_IDLE
395 		 * will never happen more than once
396 		 */
397 		if ((statusreq[5] == STATUS_IDLE) && (idle_count++ > 20)) {
398 			DPRINTF(("%s: AAARRGGG!!! FIX ME!\n",
399 			    sc->atu_dev.dv_xname));
400 			return 0;
401 		}
402 
403 		if ((statusreq[5] != STATUS_IN_PROGRESS) &&
404 		    (statusreq[5] != STATUS_IDLE)) {
405 			if (status != NULL)
406 				*status = statusreq[5];
407 			return 0;
408 		}
409 		usbd_delay_ms(sc->atu_udev, 25);
410 	}
411 }
412 
413 int
414 atu_send_mib(struct atu_softc *sc, u_int8_t type, u_int8_t size,
415     u_int8_t index, void *data)
416 {
417 	int				err;
418 	struct atu_cmd_set_mib		request;
419 
420 	/*
421 	 * We don't construct a MIB packet first and then memcpy it into an
422 	 * Atmel-command-packet, we just construct it the right way at once :)
423 	 */
424 
425 	memset(&request, 0, sizeof(request));
426 
427 	request.AtCmd = CMD_SET_MIB;
428 	USETW(request.AtSize, size + 4);
429 
430 	request.MIBType = type;
431 	request.MIBSize = size;
432 	request.MIBIndex = index;
433 	request.MIBReserved = 0;
434 
435 	/*
436 	 * For 1 and 2 byte requests we assume a direct value,
437 	 * everything bigger than 2 bytes we assume a pointer to the data
438 	 */
439 	switch (size) {
440 	case 0:
441 		break;
442 	case 1:
443 		request.data[0]=(long)data & 0x000000ff;
444 		break;
445 	case 2:
446 		request.data[0]=(long)data & 0x000000ff;
447 		request.data[1]=(long)data >> 8;
448 		break;
449 	default:
450 		memcpy(request.data, data, size);
451 		break;
452 	}
453 
454 	err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
455 	    0x0000, size+8, (uByte *)&request);
456 	if (err)
457 		return (err);
458 
459 	DPRINTFN(15, ("%s: sendmib : waitcompletion...\n",
460 	    sc->atu_dev.dv_xname));
461 	return atu_wait_completion(sc, CMD_SET_MIB, NULL);
462 }
463 
464 int
465 atu_get_mib(struct atu_softc *sc, u_int8_t type, u_int8_t size,
466     u_int8_t index, u_int8_t *buf)
467 {
468 
469 	/* linux/at76c503.c - 478 */
470 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x033,
471 	    type << 8, index, size, buf);
472 }
473 
474 #if 0
475 int
476 atu_start_ibss(struct atu_softc *sc)
477 {
478 	int				err;
479 	struct atu_cmd_start_ibss	Request;
480 
481 	Request.Cmd = CMD_START_IBSS;
482 	Request.Reserved = 0;
483 	Request.Size = sizeof(Request) - 4;
484 
485 	memset(Request.BSSID, 0x00, sizeof(Request.BSSID));
486 	memset(Request.SSID, 0x00, sizeof(Request.SSID));
487 	memcpy(Request.SSID, sc->atu_ssid, sc->atu_ssidlen);
488 	Request.SSIDSize = sc->atu_ssidlen;
489 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
490 		Request.Channel = (u_int8_t)sc->atu_desired_channel;
491 	else
492 		Request.Channel = ATU_DEFAULT_CHANNEL;
493 	Request.BSSType = AD_HOC_MODE;
494 	memset(Request.Res, 0x00, sizeof(Request.Res));
495 
496 	/* Write config to adapter */
497 	err = atu_send_command(sc, (u_int8_t *)&Request, sizeof(Request));
498 	if (err) {
499 		DPRINTF(("%s: start ibss failed!\n",
500 		    sc->atu_dev.dv_xname));
501 		return err;
502 	}
503 
504 	/* Wait for the adapter to do its thing */
505 	err = atu_wait_completion(sc, CMD_START_IBSS, NULL);
506 	if (err) {
507 		DPRINTF(("%s: error waiting for start_ibss\n",
508 		    sc->atu_dev.dv_xname));
509 		return err;
510 	}
511 
512 	/* Get the current BSSID */
513 	err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, sc->atu_bssid);
514 	if (err) {
515 		DPRINTF(("%s: could not get BSSID!\n",
516 		    sc->atu_dev.dv_xname));
517 		return err;
518 	}
519 
520 	DPRINTF(("%s: started a new IBSS (BSSID=%s)\n",
521 	    sc->atu_dev.dv_xname, ether_sprintf(sc->atu_bssid)));
522 	return 0;
523 }
524 #endif
525 
526 int
527 atu_start_scan(struct atu_softc *sc)
528 {
529 	struct ieee80211com		*ic = &sc->sc_ic;
530 	struct atu_cmd_do_scan		Scan;
531 	usbd_status			err;
532 	int				Cnt;
533 
534 	memset(&Scan, 0, sizeof(Scan));
535 
536 	Scan.Cmd = CMD_START_SCAN;
537 	Scan.Reserved = 0;
538 	USETW(Scan.Size, sizeof(Scan) - 4);
539 
540 	/* use the broadcast BSSID (in active scan) */
541 	for (Cnt=0; Cnt<6; Cnt++)
542 		Scan.BSSID[Cnt] = 0xff;
543 
544 	memcpy(Scan.SSID, ic->ic_des_essid, ic->ic_des_esslen);
545 	Scan.SSID_Len = ic->ic_des_esslen;
546 
547 	/* default values for scan */
548 	Scan.ScanType = ATU_SCAN_ACTIVE;
549 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
550 		Scan.Channel = (u_int8_t)sc->atu_desired_channel;
551 	else
552 		Scan.Channel = sc->atu_channel;
553 
554 	/* we like scans to be quick :) */
555 	/* the time we wait before sending probe's */
556 	USETW(Scan.ProbeDelay, 0);
557 	/* the time we stay on one channel */
558 	USETW(Scan.MinChannelTime, 100);
559 	USETW(Scan.MaxChannelTime, 200);
560 	/* whether or not we scan all channels */
561 	Scan.InternationalScan = 0xc1;
562 
563 #ifdef ATU_DEBUG
564 	if (atudebug) {
565 		DPRINTFN(20, ("%s: scan cmd len=%02x\n",
566 		    sc->atu_dev.dv_xname, sizeof(Scan)));
567 		DPRINTFN(20, ("%s: scan cmd: %52D\n", sc->atu_dev.dv_xname,
568 		    (u_int8_t *)&Scan, " "));
569 	}
570 #endif /* ATU_DEBUG */
571 
572 	/* Write config to adapter */
573 	err = atu_send_command(sc, (u_int8_t *)&Scan, sizeof(Scan));
574 	if (err)
575 		return err;
576 
577 	/*
578 	 * We don't wait for the command to finish... the mgmt-thread will do
579 	 * that for us
580 	 */
581 	/*
582 	err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
583 	if (err)
584 		return err;
585 	*/
586 	return 0;
587 }
588 
589 int
590 atu_switch_radio(struct atu_softc *sc, int state)
591 {
592 	usbd_status		err;
593 	struct atu_cmd		CmdRadio;
594 
595 	if (sc->atu_radio == RadioIntersil) {
596 		/*
597 		 * Intersil doesn't seem to need/support switching the radio
598 		 * on/off
599 		 */
600 		return 0;
601 	}
602 
603 	memset(&CmdRadio, 0, sizeof(CmdRadio));
604 	CmdRadio.Cmd = CMD_RADIO_ON;
605 
606 	if (sc->atu_radio_on != state) {
607 		if (state == 0)
608 			CmdRadio.Cmd = CMD_RADIO_OFF;
609 
610 		err = atu_send_command(sc, (u_int8_t *)&CmdRadio,
611 		    sizeof(CmdRadio));
612 		if (err)
613 			return err;
614 
615 		err = atu_wait_completion(sc, CmdRadio.Cmd, NULL);
616 		if (err)
617 			return err;
618 
619 		DPRINTFN(10, ("%s: radio turned %s\n",
620 		    sc->atu_dev.dv_xname, state ? "on" : "off"));
621 		sc->atu_radio_on = state;
622 	}
623 	return 0;
624 }
625 
626 int
627 atu_initial_config(struct atu_softc *sc)
628 {
629 	struct ieee80211com		*ic = &sc->sc_ic;
630 	u_int32_t			i;
631 	usbd_status			err;
632 /*	u_int8_t			rates[4] = {0x82, 0x84, 0x8B, 0x96};*/
633 	u_int8_t			rates[4] = {0x82, 0x04, 0x0B, 0x16};
634 	struct atu_cmd_card_config	cmd;
635 	u_int8_t			reg_domain;
636 
637 	DPRINTFN(10, ("%s: sending mac-addr\n", sc->atu_dev.dv_xname));
638 	err = atu_send_mib(sc, MIB_MAC_ADDR__ADDR, ic->ic_myaddr);
639 	if (err) {
640 		DPRINTF(("%s: error setting mac-addr\n",
641 		    sc->atu_dev.dv_xname));
642 		return err;
643 	}
644 
645 	/*
646 	DPRINTF(("%s: sending reg-domain\n", sc->atu_dev.dv_xname));
647 	err = atu_send_mib(sc, MIB_PHY__REG_DOMAIN, NR(0x30));
648 	if (err) {
649 		DPRINTF(("%s: error setting mac-addr\n",
650 		    sc->atu_dev.dv_xname));
651 		return err;
652 	}
653 	*/
654 
655 	memset(&cmd, 0, sizeof(cmd));
656 	cmd.Cmd = CMD_STARTUP;
657 	cmd.Reserved = 0;
658 	USETW(cmd.Size, sizeof(cmd) - 4);
659 
660 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
661 		cmd.Channel = (u_int8_t)sc->atu_desired_channel;
662 	else
663 		cmd.Channel = sc->atu_channel;
664 	cmd.AutoRateFallback = 1;
665 	memcpy(cmd.BasicRateSet, rates, 4);
666 
667 	/* ShortRetryLimit should be 7 according to 802.11 spec */
668 	cmd.ShortRetryLimit = 7;
669 	USETW(cmd.RTS_Threshold, 2347);
670 	USETW(cmd.FragThreshold, 2346);
671 
672 	/* Doesn't seem to work, but we'll set it to 1 anyway */
673 	cmd.PromiscuousMode = 1;
674 
675 	/* this goes into the beacon we transmit */
676 	cmd.PrivacyInvoked = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
677 
678 	cmd.ExcludeUnencrypted = 0;
679 	switch (ic->ic_nw_keys[ic->ic_wep_txkey].k_cipher) {
680 	case IEEE80211_CIPHER_WEP40:
681 		cmd.EncryptionType = ATU_WEP_40BITS;
682 		break;
683 	case IEEE80211_CIPHER_WEP104:
684 		cmd.EncryptionType = ATU_WEP_104BITS;
685 		break;
686 	default:
687 		cmd.EncryptionType = ATU_WEP_OFF;
688 		break;
689 	}
690 
691 	cmd.WEP_DefaultKeyID = ic->ic_wep_txkey;
692 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
693 		memcpy(cmd.WEP_DefaultKey[i], ic->ic_nw_keys[i].k_key,
694 		    ic->ic_nw_keys[i].k_len);
695 	}
696 
697 	/* Setting the SSID here doesn't seem to do anything */
698 	memcpy(cmd.SSID, ic->ic_des_essid, ic->ic_des_esslen);
699 	cmd.SSID_Len = ic->ic_des_esslen;
700 
701 	cmd.ShortPreamble = 0;
702 	USETW(cmd.BeaconPeriod, 100);
703 	/* cmd.BeaconPeriod = 65535; */
704 
705 	/*
706 	 * TODO:
707 	 * read reg domain MIB_PHY @ 0x17 (1 byte), (reply = 0x30)
708 	 * we should do something useful with this info. right now it's just
709 	 * ignored
710 	 */
711 	err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, &reg_domain);
712 	if (err) {
713 		DPRINTF(("%s: could not get regdomain!\n",
714 		    sc->atu_dev.dv_xname));
715 	} else {
716 		DPRINTF(("%s: we're in reg domain 0x%x according to the "
717 		    "adapter\n", sc->atu_dev.dv_xname, reg_domain));
718 	}
719 
720 #ifdef ATU_DEBUG
721 	if (atudebug) {
722 		DPRINTFN(20, ("%s: configlen=%02x\n", sc->atu_dev.dv_xname,
723 		    sizeof(cmd)));
724 		DPRINTFN(20, ("%s: configdata= %108D\n",
725 		    sc->atu_dev.dv_xname, (u_int8_t *)&cmd, " "));
726 	}
727 #endif /* ATU_DEBUG */
728 
729 	/* Windoze : driver says exclude-unencrypted=1 & encr-type=1 */
730 
731 	err = atu_send_command(sc, (u_int8_t *)&cmd, sizeof(cmd));
732 	if (err)
733 		return err;
734 	err = atu_wait_completion(sc, CMD_STARTUP, NULL);
735 	if (err)
736 		return err;
737 
738 	/* Turn on radio now */
739 	err = atu_switch_radio(sc, 1);
740 	if (err)
741 		return err;
742 
743 	/* preamble type = short */
744 	err = atu_send_mib(sc, MIB_LOCAL__PREAMBLE, NR(PREAMBLE_SHORT));
745 	if (err)
746 		return err;
747 
748 	/* frag = 1536 */
749 	err = atu_send_mib(sc, MIB_MAC__FRAG, NR(2346));
750 	if (err)
751 		return err;
752 
753 	/* rts = 1536 */
754 	err = atu_send_mib(sc, MIB_MAC__RTS, NR(2347));
755 	if (err)
756 		return err;
757 
758 	/* auto rate fallback = 1 */
759 	err = atu_send_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, NR(1));
760 	if (err)
761 		return err;
762 
763 	/* power mode = full on, no power saving */
764 	err = atu_send_mib(sc, MIB_MAC_MGMT__POWER_MODE,
765 	    NR(POWER_MODE_ACTIVE));
766 	if (err)
767 		return err;
768 
769 	DPRINTFN(10, ("%s: completed initial config\n",
770 	   sc->atu_dev.dv_xname));
771 	return 0;
772 }
773 
774 int
775 atu_join(struct atu_softc *sc, struct ieee80211_node *node)
776 {
777 	struct atu_cmd_join		join;
778 	u_int8_t			status;
779 	usbd_status			err;
780 
781 	memset(&join, 0, sizeof(join));
782 
783 	join.Cmd = CMD_JOIN;
784 	join.Reserved = 0x00;
785 	USETW(join.Size, sizeof(join) - 4);
786 
787 	DPRINTFN(15, ("%s: pre-join sc->atu_bssid=%s\n",
788 	    sc->atu_dev.dv_xname, ether_sprintf(sc->atu_bssid)));
789 	DPRINTFN(15, ("%s: mode=%d\n", sc->atu_dev.dv_xname,
790 	    sc->atu_mode));
791 	memcpy(join.bssid, node->ni_bssid, IEEE80211_ADDR_LEN);
792 	memcpy(join.essid, node->ni_essid, node->ni_esslen);
793 	join.essid_size = node->ni_esslen;
794 	if (node->ni_capinfo & IEEE80211_CAPINFO_IBSS)
795 		join.bss_type = AD_HOC_MODE;
796 	else
797 		join.bss_type = INFRASTRUCTURE_MODE;
798 	join.channel = ieee80211_chan2ieee(&sc->sc_ic, node->ni_chan);
799 
800 	USETW(join.timeout, ATU_JOIN_TIMEOUT);
801 	join.reserved = 0x00;
802 
803 	DPRINTFN(10, ("%s: trying to join BSSID=%s\n",
804 	    sc->atu_dev.dv_xname, ether_sprintf(join.bssid)));
805 	err = atu_send_command(sc, (u_int8_t *)&join, sizeof(join));
806 	if (err) {
807 		DPRINTF(("%s: ERROR trying to join IBSS\n",
808 		    sc->atu_dev.dv_xname));
809 		return err;
810 	}
811 	err = atu_wait_completion(sc, CMD_JOIN, &status);
812 	if (err) {
813 		DPRINTF(("%s: error joining BSS!\n",
814 		    sc->atu_dev.dv_xname));
815 		return err;
816 	}
817 	if (status != STATUS_COMPLETE) {
818 		DPRINTF(("%s: error joining... [status=%02x]\n",
819 		    sc->atu_dev.dv_xname, status));
820 		return status;
821 	} else {
822 		DPRINTFN(10, ("%s: joined BSS\n", sc->atu_dev.dv_xname));
823 	}
824 	return err;
825 }
826 
827 /*
828  * Get the state of the DFU unit
829  */
830 int8_t
831 atu_get_dfu_state(struct atu_softc *sc)
832 {
833 	u_int8_t	state;
834 
835 	if (atu_usb_request(sc, DFU_GETSTATE, 0, 0, 1, &state))
836 		return -1;
837 	return state;
838 }
839 
840 /*
841  * Get MAC opmode
842  */
843 u_int8_t
844 atu_get_opmode(struct atu_softc *sc, u_int8_t *mode)
845 {
846 
847 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 0x0001,
848 	    0x0000, 1, mode);
849 }
850 
851 /*
852  * Upload the internal firmware into the device
853  */
854 void
855 atu_internal_firmware(struct device *self)
856 {
857 	struct atu_softc *sc = (struct atu_softc *)self;
858 	u_char	state, *ptr = NULL, *firm = NULL, status[6];
859 	int block_size, block = 0, err, i;
860 	size_t	bytes_left = 0;
861 	char	*name = "unknown-device";
862 
863 	/*
864 	 * Uploading firmware is done with the DFU (Device Firmware Upgrade)
865 	 * interface. See "Universal Serial Bus - Device Class Specification
866 	 * for Device Firmware Upgrade" pdf for details of the protocol.
867 	 * Maybe this could be moved to a separate 'firmware driver' once more
868 	 * device drivers need it... For now we'll just do it here.
869 	 *
870 	 * Just for your information, the Atmel's DFU descriptor looks like
871 	 * this:
872 	 *
873 	 * 07		size
874 	 * 21		type
875 	 * 01		capabilities : only firmware download, need reset
876 	 *		  after download
877 	 * 13 05	detach timeout : max 1299ms between DFU_DETACH and
878 	 *		  reset
879 	 * 00 04	max bytes of firmware per transaction : 1024
880 	 */
881 
882 	/* Choose the right firmware for the device */
883 	for (i = 0; i < nitems(atu_radfirm); i++)
884 		if (sc->atu_radio == atu_radfirm[i].atur_type)
885 			name = atu_radfirm[i].atur_internal;
886 
887 	DPRINTF(("%s: loading firmware %s...\n",
888 	    sc->atu_dev.dv_xname, name));
889 	err = loadfirmware(name, &firm, &bytes_left);
890 	if (err != 0) {
891 		printf("%s: %s loadfirmware error %d\n",
892 		    sc->atu_dev.dv_xname, name, err);
893 		goto fail;
894 	}
895 
896 	ptr = firm;
897 	state = atu_get_dfu_state(sc);
898 
899 	while (block >= 0 && state > 0) {
900 		switch (state) {
901 		case DFUState_DnLoadSync:
902 			/* get DFU status */
903 			err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0 , 6,
904 			    status);
905 			if (err) {
906 				DPRINTF(("%s: dfu_getstatus failed!\n",
907 				    sc->atu_dev.dv_xname));
908 				free(firm, M_DEVBUF, 0);
909 				goto fail;
910 			}
911 			/* success means state => DnLoadIdle */
912 			state = DFUState_DnLoadIdle;
913 			continue;
914 			break;
915 
916 		case DFUState_DFUIdle:
917 		case DFUState_DnLoadIdle:
918 			if (bytes_left>=DFU_MaxBlockSize)
919 				block_size = DFU_MaxBlockSize;
920 			else
921 				block_size = bytes_left;
922 			DPRINTFN(15, ("%s: firmware block %d\n",
923 			    sc->atu_dev.dv_xname, block));
924 
925 			err = atu_usb_request(sc, DFU_DNLOAD, block++, 0,
926 			    block_size, ptr);
927 			if (err) {
928 				DPRINTF(("%s: dfu_dnload failed\n",
929 				    sc->atu_dev.dv_xname));
930 				free(firm, M_DEVBUF, 0);
931 				goto fail;
932 			}
933 
934 			ptr += block_size;
935 			bytes_left -= block_size;
936 			if (block_size == 0)
937 				block = -1;
938 			break;
939 
940 		default:
941 			DPRINTFN(20, ("%s: sleeping for a while\n",
942 			    sc->atu_dev.dv_xname));
943 			usbd_delay_ms(sc->atu_udev, 100);
944 			break;
945 		}
946 
947 		state = atu_get_dfu_state(sc);
948 	}
949 	free(firm, M_DEVBUF, 0);
950 
951 	if (state != DFUState_ManifestSync) {
952 		DPRINTF(("%s: state != manifestsync... eek!\n",
953 		    sc->atu_dev.dv_xname));
954 	}
955 
956 	err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0, 6, status);
957 	if (err) {
958 		DPRINTF(("%s: dfu_getstatus failed!\n",
959 		    sc->atu_dev.dv_xname));
960 		goto fail;
961 	}
962 
963 	DPRINTFN(15, ("%s: sending remap\n", sc->atu_dev.dv_xname));
964 	err = atu_usb_request(sc, DFU_REMAP, 0, 0, 0, NULL);
965 	if ((err) && (!ISSET(sc->atu_quirk, ATU_QUIRK_NO_REMAP))) {
966 		DPRINTF(("%s: remap failed!\n", sc->atu_dev.dv_xname));
967 		goto fail;
968 	}
969 
970 	/* after a lot of trying and measuring I found out the device needs
971 	 * about 56 milliseconds after sending the remap command before
972 	 * it's ready to communicate again. So we'll wait just a little bit
973 	 * longer than that to be sure...
974 	 */
975 	usbd_delay_ms(sc->atu_udev, 56+100);
976 
977 	printf("%s: reattaching after firmware upload\n",
978 	    sc->atu_dev.dv_xname);
979 	usb_needs_reattach(sc->atu_udev);
980 
981 fail:
982 	usbd_deactivate(sc->atu_udev);
983 }
984 
985 void
986 atu_external_firmware(struct device *self)
987 {
988 	struct atu_softc *sc = (struct atu_softc *)self;
989 	u_char	*ptr = NULL, *firm = NULL;
990 	int	block_size, block = 0, err, i;
991 	size_t	bytes_left = 0;
992 	char	*name = "unknown-device";
993 
994 	for (i = 0; i < nitems(atu_radfirm); i++)
995 		if (sc->atu_radio == atu_radfirm[i].atur_type)
996 			name = atu_radfirm[i].atur_external;
997 
998 	DPRINTF(("%s: loading external firmware %s\n",
999 	    sc->atu_dev.dv_xname, name));
1000 	err = loadfirmware(name, &firm, &bytes_left);
1001 	if (err != 0) {
1002 		printf("%s: %s loadfirmware error %d\n",
1003 		    sc->atu_dev.dv_xname, name, err);
1004 		return;
1005 	}
1006 	ptr = firm;
1007 
1008 	while (bytes_left) {
1009 		if (bytes_left > 1024)
1010 			block_size = 1024;
1011 		else
1012 			block_size = bytes_left;
1013 
1014 		DPRINTFN(15, ("%s: block:%d size:%d\n",
1015 		    sc->atu_dev.dv_xname, block, block_size));
1016 		err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e,
1017 		    0x0802, block, block_size, ptr);
1018 		if (err) {
1019 			DPRINTF(("%s: could not load external firmware "
1020 			    "block\n", sc->atu_dev.dv_xname));
1021 			free(firm, M_DEVBUF, 0);
1022 			return;
1023 		}
1024 
1025 		ptr += block_size;
1026 		block++;
1027 		bytes_left -= block_size;
1028 	}
1029 	free(firm, M_DEVBUF, 0);
1030 
1031 	err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0802,
1032 	    block, 0, NULL);
1033 	if (err) {
1034 		DPRINTF(("%s: could not load last zero-length firmware "
1035 		    "block\n", sc->atu_dev.dv_xname));
1036 		return;
1037 	}
1038 
1039 	/*
1040 	 * The SMC2662w V.4 seems to require some time to do its thing with
1041 	 * the external firmware... 20 ms isn't enough, but 21 ms works 100
1042 	 * times out of 100 tries. We'll wait a bit longer just to be sure
1043 	 */
1044 	if (sc->atu_quirk & ATU_QUIRK_FW_DELAY)
1045 		usbd_delay_ms(sc->atu_udev, 21 + 100);
1046 
1047 	DPRINTFN(10, ("%s: external firmware upload done\n",
1048 	    sc->atu_dev.dv_xname));
1049 	/* complete configuration after the firmwares have been uploaded */
1050 	atu_complete_attach(sc);
1051 }
1052 
1053 int
1054 atu_get_card_config(struct atu_softc *sc)
1055 {
1056 	struct ieee80211com		*ic = &sc->sc_ic;
1057 	struct atu_rfmd_conf		rfmd_conf;
1058 	struct atu_intersil_conf	intersil_conf;
1059 	int				err;
1060 
1061 	switch (sc->atu_radio) {
1062 
1063 	case RadioRFMD:
1064 	case RadioRFMD2958:
1065 	case RadioRFMD2958_SMC:
1066 	case AT76C503_rfmd_acc:
1067 	case AT76C505_rfmd:
1068 		err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
1069 		    0x0a02, 0x0000, sizeof(rfmd_conf),
1070 		    (u_int8_t *)&rfmd_conf);
1071 		if (err) {
1072 			DPRINTF(("%s: could not get rfmd config!\n",
1073 			    sc->atu_dev.dv_xname));
1074 			return err;
1075 		}
1076 		memcpy(ic->ic_myaddr, rfmd_conf.MACAddr, IEEE80211_ADDR_LEN);
1077 		break;
1078 
1079 	case RadioIntersil:
1080 	case AT76C503_i3863:
1081 		err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
1082 		    0x0902, 0x0000, sizeof(intersil_conf),
1083 		    (u_int8_t *)&intersil_conf);
1084 		if (err) {
1085 			DPRINTF(("%s: could not get intersil config!\n",
1086 			    sc->atu_dev.dv_xname));
1087 			return err;
1088 		}
1089 		memcpy(ic->ic_myaddr, intersil_conf.MACAddr,
1090 		    IEEE80211_ADDR_LEN);
1091 		break;
1092 	}
1093 	return 0;
1094 }
1095 
1096 /*
1097  * Probe for an AT76c503 chip.
1098  */
1099 int
1100 atu_match(struct device *parent, void *match, void *aux)
1101 {
1102 	struct usb_attach_arg	*uaa = aux;
1103 	int			i;
1104 
1105 	if (!uaa->iface)
1106 		return(UMATCH_NONE);
1107 
1108 	for (i = 0; i < nitems(atu_devs); i++) {
1109 		struct atu_type *t = &atu_devs[i];
1110 
1111 		if (uaa->vendor == t->atu_vid &&
1112 		    uaa->product == t->atu_pid) {
1113 			return(UMATCH_VENDOR_PRODUCT);
1114 		}
1115 	}
1116 	return(UMATCH_NONE);
1117 }
1118 
1119 int
1120 atu_media_change(struct ifnet *ifp)
1121 {
1122 #ifdef ATU_DEBUG
1123 	struct atu_softc	*sc = ifp->if_softc;
1124 #endif /* ATU_DEBUG */
1125 	int			err;
1126 
1127 	DPRINTFN(10, ("%s: atu_media_change\n", sc->atu_dev.dv_xname));
1128 
1129 	err = ieee80211_media_change(ifp);
1130 	if (err == ENETRESET) {
1131 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
1132 		    (IFF_RUNNING|IFF_UP))
1133 			atu_init(ifp);
1134 		err = 0;
1135 	}
1136 
1137 	return (err);
1138 }
1139 
1140 void
1141 atu_media_status(struct ifnet *ifp, struct ifmediareq *req)
1142 {
1143 #ifdef ATU_DEBUG
1144 	struct atu_softc	*sc = ifp->if_softc;
1145 #endif /* ATU_DEBUG */
1146 
1147 	DPRINTFN(10, ("%s: atu_media_status\n", sc->atu_dev.dv_xname));
1148 
1149 	ieee80211_media_status(ifp, req);
1150 }
1151 
1152 void
1153 atu_task(void *arg)
1154 {
1155 	struct atu_softc	*sc = (struct atu_softc *)arg;
1156 	struct ieee80211com	*ic = &sc->sc_ic;
1157 	struct ifnet		*ifp = &ic->ic_if;
1158 	usbd_status		err;
1159 	int			s;
1160 
1161 	DPRINTFN(10, ("%s: atu_task\n", sc->atu_dev.dv_xname));
1162 
1163 	if (usbd_is_dying(sc->atu_udev))
1164 		return;
1165 
1166 	switch (sc->sc_cmd) {
1167 	case ATU_C_SCAN:
1168 
1169 		err = atu_start_scan(sc);
1170 		if (err) {
1171 			DPRINTFN(1, ("%s: atu_init: couldn't start scan!\n",
1172 			    sc->atu_dev.dv_xname));
1173 			return;
1174 		}
1175 
1176 		err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
1177 		if (err) {
1178 			DPRINTF(("%s: atu_init: error waiting for scan\n",
1179 			    sc->atu_dev.dv_xname));
1180 			return;
1181 		}
1182 
1183 		DPRINTF(("%s: ==========================> END OF SCAN!\n",
1184 		    sc->atu_dev.dv_xname));
1185 
1186 		s = splnet();
1187 		/* ieee80211_next_scan(ifp); */
1188 		ieee80211_end_scan(ifp);
1189 		splx(s);
1190 
1191 		DPRINTF(("%s: ----------------------======> END OF SCAN2!\n",
1192 		    sc->atu_dev.dv_xname));
1193 		break;
1194 
1195 	case ATU_C_JOIN:
1196 		atu_join(sc, ic->ic_bss);
1197 	}
1198 }
1199 
1200 int
1201 atu_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1202 {
1203 	struct ifnet		*ifp = &ic->ic_if;
1204 	struct atu_softc	*sc = ifp->if_softc;
1205 	enum ieee80211_state	ostate = ic->ic_state;
1206 
1207 	DPRINTFN(10, ("%s: atu_newstate: %s -> %s\n", sc->atu_dev.dv_xname,
1208 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
1209 
1210 	switch (nstate) {
1211 	case IEEE80211_S_SCAN:
1212 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
1213 		    sizeof(ic->ic_chan_active));
1214 		ieee80211_free_allnodes(ic);
1215 
1216 		/* tell the event thread that we want a scan */
1217 		sc->sc_cmd = ATU_C_SCAN;
1218 		usb_add_task(sc->atu_udev, &sc->sc_task);
1219 
1220 		/* handle this ourselves */
1221 		ic->ic_state = nstate;
1222 		return (0);
1223 
1224 	case IEEE80211_S_AUTH:
1225 	case IEEE80211_S_RUN:
1226 		if (ostate == IEEE80211_S_SCAN) {
1227 			sc->sc_cmd = ATU_C_JOIN;
1228 			usb_add_task(sc->atu_udev, &sc->sc_task);
1229 		}
1230 		break;
1231 	default:
1232 		/* nothing to do */
1233 		break;
1234 	}
1235 
1236 	return (*sc->sc_newstate)(ic, nstate, arg);
1237 }
1238 
1239 /*
1240  * Attach the interface. Allocate softc structures, do
1241  * setup and ethernet/BPF attach.
1242  */
1243 void
1244 atu_attach(struct device *parent, struct device *self, void *aux)
1245 {
1246 	struct atu_softc		*sc = (struct atu_softc *)self;
1247 	struct usb_attach_arg		*uaa = aux;
1248 	usbd_status			err;
1249 	struct usbd_device		*dev = uaa->device;
1250 	u_int8_t			mode, channel;
1251 	int i;
1252 
1253 	sc->atu_unit = self->dv_unit;
1254 	sc->atu_udev = dev;
1255 
1256 	err = usbd_set_config_no(dev, ATU_CONFIG_NO, 1);
1257 	if (err) {
1258 		printf("%s: setting config no failed\n",
1259 		    sc->atu_dev.dv_xname);
1260 		goto fail;
1261 	}
1262 
1263 	err = usbd_device2interface_handle(dev, ATU_IFACE_IDX, &sc->atu_iface);
1264 	if (err) {
1265 		printf("%s: getting interface handle failed\n",
1266 		    sc->atu_dev.dv_xname);
1267 		goto fail;
1268 	}
1269 
1270 	/*
1271 	 * look up the radio_type for the device
1272 	 * basically does the same as USB_MATCH
1273 	 */
1274 	for (i = 0; i < nitems(atu_devs); i++) {
1275 		struct atu_type *t = &atu_devs[i];
1276 
1277 		if (uaa->vendor == t->atu_vid &&
1278 		    uaa->product == t->atu_pid) {
1279 			sc->atu_radio = t->atu_radio;
1280 			sc->atu_quirk = t->atu_quirk;
1281 		}
1282 	}
1283 
1284 	/*
1285 	 * Check in the interface descriptor if we're in DFU mode
1286 	 * If we're in DFU mode, we upload the external firmware
1287 	 * If we're not, the PC must have rebooted without power-cycling
1288 	 * the device.. I've tried this out, a reboot only requeres the
1289 	 * external firmware to be reloaded :)
1290 	 *
1291 	 * Hmm. The at76c505a doesn't report a DFU descriptor when it's
1292 	 * in DFU mode... Let's just try to get the opmode
1293 	 */
1294 	err = atu_get_opmode(sc, &mode);
1295 	DPRINTFN(20, ("%s: opmode: %d\n", sc->atu_dev.dv_xname, mode));
1296 	if (err || (mode != MODE_NETCARD && mode != MODE_NOFLASHNETCARD)) {
1297 		DPRINTF(("%s: starting internal firmware download\n",
1298 		    sc->atu_dev.dv_xname));
1299 
1300 		config_mountroot(self, atu_internal_firmware);
1301 		/*
1302 		 * atu_internal_firmware will cause a reset of the device
1303 		 * so we don't want to do any more configuration after this
1304 		 * point.
1305 		 */
1306 		return;
1307 	}
1308 
1309 	uaa->iface = sc->atu_iface;
1310 
1311 	if (mode != MODE_NETCARD) {
1312 		DPRINTFN(15, ("%s: device needs external firmware\n",
1313 		    sc->atu_dev.dv_xname));
1314 
1315 		if (mode != MODE_NOFLASHNETCARD) {
1316 			DPRINTF(("%s: EEK! unexpected opmode=%d\n",
1317 			    sc->atu_dev.dv_xname, mode));
1318 		}
1319 
1320 		/*
1321 		 * There is no difference in opmode before and after external
1322 		 * firmware upload with the SMC2662 V.4 . So instead we'll try
1323 		 * to read the channel number. If we succeed, external
1324 		 * firmwaremust have been already uploaded...
1325 		 */
1326 		if (sc->atu_radio != RadioIntersil) {
1327 			err = atu_get_mib(sc, MIB_PHY__CHANNEL, &channel);
1328 			if (!err) {
1329 				DPRINTF(("%s: external firmware has already"
1330 				    " been downloaded\n",
1331 				    sc->atu_dev.dv_xname));
1332 				atu_complete_attach(sc);
1333 				return;
1334 			}
1335 		}
1336 
1337 		config_mountroot(self, atu_external_firmware);
1338 
1339 		/*
1340 		 * atu_external_firmware will call atu_complete_attach after
1341 		 * it's finished so we can just return.
1342 		 */
1343 	} else {
1344 		/* all the firmwares are in place, so complete the attach */
1345 		atu_complete_attach(sc);
1346 	}
1347 fail:
1348 	usbd_deactivate(sc->atu_udev);
1349 }
1350 
1351 void
1352 atu_complete_attach(struct atu_softc *sc)
1353 {
1354 	struct ieee80211com		*ic = &sc->sc_ic;
1355 	struct ifnet			*ifp = &ic->ic_if;
1356 	usb_interface_descriptor_t	*id;
1357 	usb_endpoint_descriptor_t	*ed;
1358 	usbd_status			err;
1359 	int				i;
1360 #ifdef ATU_DEBUG
1361 	struct atu_fw			fw;
1362 #endif
1363 
1364 	id = usbd_get_interface_descriptor(sc->atu_iface);
1365 
1366 	/* Find endpoints. */
1367 	for (i = 0; i < id->bNumEndpoints; i++) {
1368 		ed = usbd_interface2endpoint_descriptor(sc->atu_iface, i);
1369 		if (!ed) {
1370 			DPRINTF(("%s: num_endp:%d\n", sc->atu_dev.dv_xname,
1371 			    sc->atu_iface->idesc->bNumEndpoints));
1372 			DPRINTF(("%s: couldn't get ep %d\n",
1373 			    sc->atu_dev.dv_xname, i));
1374 			goto fail;
1375 		}
1376 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1377 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1378 			sc->atu_ed[ATU_ENDPT_RX] = ed->bEndpointAddress;
1379 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1380 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1381 			sc->atu_ed[ATU_ENDPT_TX] = ed->bEndpointAddress;
1382 		}
1383 	}
1384 
1385 	/* read device config & get MAC address */
1386 	err = atu_get_card_config(sc);
1387 	if (err) {
1388 		printf("%s: could not get card cfg!\n",
1389 		    sc->atu_dev.dv_xname);
1390 		goto fail;
1391 	}
1392 
1393 #ifdef ATU_DEBUG
1394 	/* DEBUG : try to get firmware version */
1395 	err = atu_get_mib(sc, MIB_FW_VERSION, sizeof(fw), 0,
1396 	    (u_int8_t *)&fw);
1397 	if (!err) {
1398 		DPRINTFN(15, ("%s: firmware: maj:%d min:%d patch:%d "
1399 		    "build:%d\n", sc->atu_dev.dv_xname, fw.major, fw.minor,
1400 		    fw.patch, fw.build));
1401 	} else {
1402 		DPRINTF(("%s: get firmware version failed\n",
1403 		    sc->atu_dev.dv_xname));
1404 	}
1405 #endif /* ATU_DEBUG */
1406 
1407 	/* Show the world our MAC address */
1408 	printf("%s: address %s\n", sc->atu_dev.dv_xname,
1409 	    ether_sprintf(ic->ic_myaddr));
1410 
1411 	sc->atu_cdata.atu_tx_inuse = 0;
1412 
1413 	bzero(sc->atu_bssid, ETHER_ADDR_LEN);
1414 	sc->atu_channel = ATU_DEFAULT_CHANNEL;
1415 	sc->atu_desired_channel = IEEE80211_CHAN_ANY;
1416 	sc->atu_mode = INFRASTRUCTURE_MODE;
1417 
1418 	ic->ic_softc = sc;
1419 	ic->ic_phytype = IEEE80211_T_DS;
1420 	ic->ic_opmode = IEEE80211_M_STA;
1421 	ic->ic_state = IEEE80211_S_INIT;
1422 	ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP | IEEE80211_C_SCANALL;
1423 	ic->ic_max_rssi = atu_radfirm[sc->atu_radio].max_rssi;
1424 
1425 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
1426 
1427 	for (i = 1; i <= 14; i++) {
1428 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B |
1429 		    IEEE80211_CHAN_PASSIVE;
1430 		ic->ic_channels[i].ic_freq = ieee80211_ieee2mhz(i,
1431 		    ic->ic_channels[i].ic_flags);
1432 	}
1433 
1434 	ic->ic_ibss_chan = &ic->ic_channels[0];
1435 
1436 	ifp->if_softc = sc;
1437 	memcpy(ifp->if_xname, sc->atu_dev.dv_xname, IFNAMSIZ);
1438 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1439 	ifp->if_start = atu_start;
1440 	ifp->if_ioctl = atu_ioctl;
1441 	ifp->if_watchdog = atu_watchdog;
1442 	ifp->if_mtu = ATU_DEFAULT_MTU;
1443 
1444 	/* Call MI attach routine. */
1445 	if_attach(ifp);
1446 	ieee80211_ifattach(ifp);
1447 
1448 	sc->sc_newstate = ic->ic_newstate;
1449 	ic->ic_newstate = atu_newstate;
1450 
1451 	/* setup ifmedia interface */
1452 	ieee80211_media_init(ifp, atu_media_change, atu_media_status);
1453 
1454 	usb_init_task(&sc->sc_task, atu_task, sc, USB_TASK_TYPE_GENERIC);
1455 
1456 #if NBPFILTER > 0
1457 	bpfattach(&sc->sc_radiobpf, &sc->sc_ic.ic_if, DLT_IEEE802_11_RADIO,
1458 	    sizeof(struct ieee80211_frame) + 64);
1459 
1460 	bzero(&sc->sc_rxtapu, sizeof(sc->sc_rxtapu));
1461 	sc->sc_rxtap.rr_ihdr.it_len = sizeof(sc->sc_rxtapu);
1462 	sc->sc_rxtap.rr_ihdr.it_present = htole32(ATU_RX_RADIOTAP_PRESENT);
1463 
1464 	bzero(&sc->sc_txtapu, sizeof(sc->sc_txtapu));
1465 	sc->sc_txtap.rt_ihdr.it_len = sizeof(sc->sc_txtapu);
1466 	sc->sc_txtap.rt_ihdr.it_present = htole32(ATU_TX_RADIOTAP_PRESENT);
1467 #endif
1468 
1469 fail:
1470 	usbd_deactivate(sc->atu_udev);
1471 }
1472 
1473 int
1474 atu_detach(struct device *self, int flags)
1475 {
1476 	struct atu_softc	*sc = (struct atu_softc *)self;
1477 	struct ifnet		*ifp = &sc->sc_ic.ic_if;
1478 
1479 	DPRINTFN(10, ("%s: atu_detach\n", sc->atu_dev.dv_xname));
1480 
1481 	if (ifp->if_flags & IFF_RUNNING)
1482 		atu_stop(ifp, 1);
1483 
1484 	usb_rem_task(sc->atu_udev, &sc->sc_task);
1485 
1486 	if (sc->atu_ep[ATU_ENDPT_TX] != NULL)
1487 		usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]);
1488 	if (sc->atu_ep[ATU_ENDPT_RX] != NULL)
1489 		usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]);
1490 
1491 	if (ifp->if_softc != NULL) {
1492 		ieee80211_ifdetach(ifp);
1493 		if_detach(ifp);
1494 	}
1495 
1496 	return(0);
1497 }
1498 
1499 /*
1500  * Initialize an RX descriptor and attach an MBUF cluster.
1501  */
1502 int
1503 atu_newbuf(struct atu_softc *sc, struct atu_chain *c, struct mbuf *m)
1504 {
1505 	struct mbuf		*m_new = NULL;
1506 
1507 	if (m == NULL) {
1508 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1509 		if (m_new == NULL) {
1510 			DPRINTF(("%s: no memory for rx list\n",
1511 			    sc->atu_dev.dv_xname));
1512 			return(ENOBUFS);
1513 		}
1514 
1515 		MCLGET(m_new, M_DONTWAIT);
1516 		if (!(m_new->m_flags & M_EXT)) {
1517 			DPRINTF(("%s: no memory for rx list\n",
1518 			    sc->atu_dev.dv_xname));
1519 			m_freem(m_new);
1520 			return(ENOBUFS);
1521 		}
1522 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1523 	} else {
1524 		m_new = m;
1525 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1526 		m_new->m_data = m_new->m_ext.ext_buf;
1527 	}
1528 	c->atu_mbuf = m_new;
1529 	return(0);
1530 }
1531 
1532 int
1533 atu_rx_list_init(struct atu_softc *sc)
1534 {
1535 	struct atu_cdata	*cd = &sc->atu_cdata;
1536 	struct atu_chain	*c;
1537 	int			i;
1538 
1539 	DPRINTFN(15, ("%s: atu_rx_list_init: enter\n",
1540 	    sc->atu_dev.dv_xname));
1541 
1542 	for (i = 0; i < ATU_RX_LIST_CNT; i++) {
1543 		c = &cd->atu_rx_chain[i];
1544 		c->atu_sc = sc;
1545 		c->atu_idx = i;
1546 		if (c->atu_xfer == NULL) {
1547 			c->atu_xfer = usbd_alloc_xfer(sc->atu_udev);
1548 			if (c->atu_xfer == NULL)
1549 				return (ENOBUFS);
1550 			c->atu_buf = usbd_alloc_buffer(c->atu_xfer,
1551 			    ATU_RX_BUFSZ);
1552 			if (c->atu_buf == NULL) /* XXX free xfer */
1553 				return (ENOBUFS);
1554 			if (atu_newbuf(sc, c, NULL) == ENOBUFS) /* XXX free? */
1555 				return(ENOBUFS);
1556 		}
1557 	}
1558 	return (0);
1559 }
1560 
1561 int
1562 atu_tx_list_init(struct atu_softc *sc)
1563 {
1564 	struct atu_cdata	*cd = &sc->atu_cdata;
1565 	struct atu_chain	*c;
1566 	int			i;
1567 
1568 	DPRINTFN(15, ("%s: atu_tx_list_init\n",
1569 	    sc->atu_dev.dv_xname));
1570 
1571 	SLIST_INIT(&cd->atu_tx_free);
1572 	sc->atu_cdata.atu_tx_inuse = 0;
1573 
1574 	for (i = 0; i < ATU_TX_LIST_CNT; i++) {
1575 		c = &cd->atu_tx_chain[i];
1576 		c->atu_sc = sc;
1577 		c->atu_idx = i;
1578 		if (c->atu_xfer == NULL) {
1579 			c->atu_xfer = usbd_alloc_xfer(sc->atu_udev);
1580 			if (c->atu_xfer == NULL)
1581 				return(ENOBUFS);
1582 			c->atu_mbuf = NULL;
1583 			c->atu_buf = usbd_alloc_buffer(c->atu_xfer,
1584 			    ATU_TX_BUFSZ);
1585 			if (c->atu_buf == NULL)
1586 				return(ENOBUFS); /* XXX free xfer */
1587 			SLIST_INSERT_HEAD(&cd->atu_tx_free, c, atu_list);
1588 		}
1589 	}
1590 	return(0);
1591 }
1592 
1593 void
1594 atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch,
1595     int listlen)
1596 {
1597 	int			i;
1598 
1599 	/* Free resources. */
1600 	for (i = 0; i < listlen; i++) {
1601 		if (ch[i].atu_buf != NULL)
1602 			ch[i].atu_buf = NULL;
1603 		if (ch[i].atu_mbuf != NULL) {
1604 			m_freem(ch[i].atu_mbuf);
1605 			ch[i].atu_mbuf = NULL;
1606 		}
1607 		if (ch[i].atu_xfer != NULL) {
1608 			usbd_free_xfer(ch[i].atu_xfer);
1609 			ch[i].atu_xfer = NULL;
1610 		}
1611 	}
1612 }
1613 
1614 /*
1615  * A frame has been uploaded: pass the resulting mbuf chain up to
1616  * the higher level protocols.
1617  */
1618 void
1619 atu_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1620 {
1621 	struct atu_chain	*c = (struct atu_chain *)priv;
1622 	struct atu_softc	*sc = c->atu_sc;
1623 	struct ieee80211com	*ic = &sc->sc_ic;
1624 	struct ifnet		*ifp = &ic->ic_if;
1625 	struct atu_rx_hdr	*h;
1626 	struct ieee80211_frame	*wh;
1627 	struct ieee80211_rxinfo	rxi;
1628 	struct ieee80211_node	*ni;
1629 	struct mbuf		*m;
1630 	u_int32_t		len;
1631 	int			s;
1632 
1633 	DPRINTFN(25, ("%s: atu_rxeof\n", sc->atu_dev.dv_xname));
1634 
1635 	if (usbd_is_dying(sc->atu_udev))
1636 		return;
1637 
1638 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP))
1639 		goto done;
1640 
1641 	if (status != USBD_NORMAL_COMPLETION) {
1642 		DPRINTF(("%s: status != USBD_NORMAL_COMPLETION\n",
1643 		    sc->atu_dev.dv_xname));
1644 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1645 			return;
1646 		}
1647 #if 0
1648 		if (status == USBD_IOERROR) {
1649 			DPRINTF(("%s: rx: EEK! lost device?\n",
1650 			    sc->atu_dev.dv_xname));
1651 
1652 			/*
1653 			 * My experience with USBD_IOERROR is that trying to
1654 			 * restart the transfer will always fail and we'll
1655 			 * keep on looping restarting transfers untill someone
1656 			 * pulls the plug of the device.
1657 			 * So we don't restart the transfer, but just let it
1658 			 * die... If someone knows of a situation where we can
1659 			 * recover from USBD_IOERROR, let me know.
1660 			 */
1661 			splx(s);
1662 			return;
1663 		}
1664 #endif /* 0 */
1665 
1666 		if (usbd_ratecheck(&sc->atu_rx_notice)) {
1667 			DPRINTF(("%s: usb error on rx: %s\n",
1668 			    sc->atu_dev.dv_xname, usbd_errstr(status)));
1669 		}
1670 		if (status == USBD_STALLED)
1671 			usbd_clear_endpoint_stall_async(
1672 			    sc->atu_ep[ATU_ENDPT_RX]);
1673 		goto done;
1674 	}
1675 
1676 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1677 
1678 	if (len <= 1) {
1679 		DPRINTF(("%s: atu_rxeof: too short\n",
1680 		    sc->atu_dev.dv_xname));
1681 		goto done;
1682 	}
1683 
1684 	h = (struct atu_rx_hdr *)c->atu_buf;
1685 	len = UGETW(h->length) - 4; /* XXX magic number */
1686 
1687 	m = c->atu_mbuf;
1688 	memcpy(mtod(m, char *), c->atu_buf + ATU_RX_HDRLEN, len);
1689 	m->m_pkthdr.len = m->m_len = len;
1690 
1691 	wh = mtod(m, struct ieee80211_frame *);
1692 	ni = ieee80211_find_rxnode(ic, wh);
1693 
1694 	s = splnet();
1695 
1696 	if (atu_newbuf(sc, c, NULL) == ENOBUFS) {
1697 		ifp->if_ierrors++;
1698 		goto done1; /* XXX if we can't allocate, why restart it? */
1699 	}
1700 
1701 #if NBPFILTER > 0
1702 	if (sc->sc_radiobpf != NULL) {
1703 		struct mbuf mb;
1704 		struct atu_rx_radiotap_header *rr = &sc->sc_rxtap;
1705 
1706 		rr->rr_flags = 0;
1707 		rr->rr_chan_freq =
1708 		    htole16(ic->ic_bss->ni_chan->ic_freq);
1709 		rr->rr_chan_flags =
1710 		    htole16(ic->ic_bss->ni_chan->ic_flags);
1711 		rr->rr_rssi = h->rssi;
1712 		rr->rr_max_rssi = ic->ic_max_rssi;
1713 
1714 		mb.m_data = (caddr_t)rr;
1715 		mb.m_len = sizeof(sc->sc_txtapu);
1716 		mb.m_next = m;
1717 		mb.m_nextpkt = NULL;
1718 		mb.m_type = 0;
1719 		mb.m_flags = 0;
1720 		bpf_mtap(sc->sc_radiobpf, &mb, BPF_DIRECTION_IN);
1721 	}
1722 #endif /* NBPFILTER > 0 */
1723 
1724 	rxi.rxi_flags = 0;
1725 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1726 		/*
1727 		 * WEP is decrypted by hardware. Clear WEP bit
1728 		 * header for ieee80211_input().
1729 		 */
1730 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1731 		rxi.rxi_flags |= IEEE80211_RXI_HWDEC;
1732 	}
1733 
1734 	rxi.rxi_rssi = h->rssi;
1735 	rxi.rxi_tstamp = UGETDW(h->rx_time);
1736 	ieee80211_input(ifp, m, ni, &rxi);
1737 
1738 	ieee80211_release_node(ic, ni);
1739 done1:
1740 	splx(s);
1741 done:
1742 	/* Setup new transfer. */
1743 	usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_RX], c, c->atu_buf,
1744 	    ATU_RX_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1745 		atu_rxeof);
1746 	usbd_transfer(c->atu_xfer);
1747 }
1748 
1749 /*
1750  * A frame was downloaded to the chip. It's safe for us to clean up
1751  * the list buffers.
1752  */
1753 void
1754 atu_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1755 {
1756 	struct atu_chain	*c = (struct atu_chain *)priv;
1757 	struct atu_softc	*sc = c->atu_sc;
1758 	struct ifnet		*ifp = &sc->sc_ic.ic_if;
1759 	usbd_status		err;
1760 	int			s;
1761 
1762 	DPRINTFN(25, ("%s: atu_txeof status=%d\n", sc->atu_dev.dv_xname,
1763 	    status));
1764 
1765 	if (c->atu_mbuf != NULL) {
1766 		m_freem(c->atu_mbuf);
1767 		c->atu_mbuf = NULL;
1768 	}
1769 
1770 	if (status != USBD_NORMAL_COMPLETION) {
1771 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1772 			return;
1773 
1774 		DPRINTF(("%s: usb error on tx: %s\n", sc->atu_dev.dv_xname,
1775 		    usbd_errstr(status)));
1776 		if (status == USBD_STALLED)
1777 			usbd_clear_endpoint_stall_async(sc->atu_ep[ATU_ENDPT_TX]);
1778 		return;
1779 	}
1780 
1781 	usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL, &err);
1782 
1783 	if (err)
1784 		ifp->if_oerrors++;
1785 	else
1786 		ifp->if_opackets++;
1787 
1788 	s = splnet();
1789 	SLIST_INSERT_HEAD(&sc->atu_cdata.atu_tx_free, c, atu_list);
1790 	sc->atu_cdata.atu_tx_inuse--;
1791 	if (sc->atu_cdata.atu_tx_inuse == 0)
1792 		ifp->if_timer = 0;
1793 	ifq_clr_oactive(&ifp->if_snd);
1794 	splx(s);
1795 
1796 	atu_start(ifp);
1797 }
1798 
1799 u_int8_t
1800 atu_calculate_padding(int size)
1801 {
1802 	size %= 64;
1803 
1804 	if (size < 50)
1805 		return (50 - size);
1806 	if (size >=61)
1807 		return (64 + 50 - size);
1808 	return (0);
1809 }
1810 
1811 int
1812 atu_tx_start(struct atu_softc *sc, struct ieee80211_node *ni,
1813     struct atu_chain *c, struct mbuf *m)
1814 {
1815 	int			len;
1816 	struct atu_tx_hdr	*h;
1817 	usbd_status		err;
1818 	u_int8_t		pad;
1819 #if NBPFILTER > 0
1820 	struct ieee80211com *ic = &sc->sc_ic;
1821 #endif
1822 
1823 	DPRINTFN(25, ("%s: atu_tx_start\n", sc->atu_dev.dv_xname));
1824 
1825 	/* Don't try to send when we're shutting down the driver */
1826 	if (usbd_is_dying(sc->atu_udev)) {
1827 		m_freem(m);
1828 		return(EIO);
1829 	}
1830 
1831 #if NBPFILTER > 0
1832 	if (sc->sc_radiobpf != NULL) {
1833 		struct mbuf mb;
1834 		struct atu_tx_radiotap_header *rt = &sc->sc_txtap;
1835 
1836 		rt->rt_flags = 0;
1837 		rt->rt_chan_freq =
1838 		    htole16(ic->ic_bss->ni_chan->ic_freq);
1839 		rt->rt_chan_flags =
1840 		    htole16(ic->ic_bss->ni_chan->ic_flags);
1841 
1842 		mb.m_data = (caddr_t)rt;
1843 		mb.m_len = sizeof(sc->sc_txtapu);
1844 		mb.m_next = m;
1845 		mb.m_nextpkt = NULL;
1846 		mb.m_type = 0;
1847 		mb.m_flags = 0;
1848 		bpf_mtap(sc->sc_radiobpf, &mb, BPF_DIRECTION_OUT);
1849 	}
1850 #endif
1851 
1852 	/*
1853 	 * Copy the mbuf data into a contiguous buffer, leaving
1854 	 * enough room for the atmel headers
1855 	 */
1856 	len = m->m_pkthdr.len;
1857 
1858 	m_copydata(m, 0, m->m_pkthdr.len, c->atu_buf + ATU_TX_HDRLEN);
1859 
1860 	h = (struct atu_tx_hdr *)c->atu_buf;
1861 	memset(h, 0, ATU_TX_HDRLEN);
1862 	USETW(h->length, len);
1863 	h->tx_rate = 4; /* XXX rate = auto */
1864 	len += ATU_TX_HDRLEN;
1865 
1866 	pad = atu_calculate_padding(len);
1867 	len += pad;
1868 	h->padding = pad;
1869 
1870 	c->atu_length = len;
1871 	c->atu_mbuf = m;
1872 
1873 	usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_TX],
1874 	    c, c->atu_buf, c->atu_length, USBD_NO_COPY, ATU_TX_TIMEOUT,
1875 	    atu_txeof);
1876 
1877 	/* Let's get this thing into the air! */
1878 	c->atu_in_xfer = 1;
1879 	err = usbd_transfer(c->atu_xfer);
1880 	if (err != USBD_IN_PROGRESS) {
1881 		DPRINTFN(25, ("%s: atu_tx_start: err=%d\n",
1882 		    sc->atu_dev.dv_xname, err));
1883 		c->atu_mbuf = NULL;
1884 		m_freem(m);
1885 		return(EIO);
1886 	}
1887 
1888 	return (0);
1889 }
1890 
1891 void
1892 atu_start(struct ifnet *ifp)
1893 {
1894 	struct atu_softc	*sc = ifp->if_softc;
1895 	struct ieee80211com	*ic = &sc->sc_ic;
1896 	struct atu_cdata	*cd = &sc->atu_cdata;
1897 	struct ieee80211_node	*ni;
1898 	struct ieee80211_frame	*wh;
1899 	struct atu_chain	*c;
1900 	struct mbuf		*m = NULL;
1901 	int			s;
1902 
1903 	DPRINTFN(25, ("%s: atu_start: enter\n", sc->atu_dev.dv_xname));
1904 
1905 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
1906 		DPRINTFN(30, ("%s: atu_start: not running or up\n",
1907 		    sc->atu_dev.dv_xname));
1908 		return;
1909 	}
1910 
1911 	if (ifq_is_oactive(&ifp->if_snd)) {
1912 		DPRINTFN(30, ("%s: atu_start: oactive\n",
1913 		    sc->atu_dev.dv_xname));
1914 		return;
1915 	}
1916 
1917 	for (;;) {
1918 		/* grab a TX buffer */
1919 		s = splnet();
1920 		c = SLIST_FIRST(&cd->atu_tx_free);
1921 		if (c != NULL) {
1922 			SLIST_REMOVE_HEAD(&cd->atu_tx_free, atu_list);
1923 			cd->atu_tx_inuse++;
1924 			if (cd->atu_tx_inuse == ATU_TX_LIST_CNT)
1925 				ifq_set_oactive(&ifp->if_snd);
1926 		}
1927 		splx(s);
1928 		if (c == NULL) {
1929 			DPRINTFN(10, ("%s: out of tx xfers\n",
1930 			    sc->atu_dev.dv_xname));
1931 			ifq_set_oactive(&ifp->if_snd);
1932 			break;
1933 		}
1934 
1935 		/*
1936 		 * Poll the management queue for frames, it has priority over
1937 		 * normal data frames.
1938 		 */
1939 		m = mq_dequeue(&ic->ic_mgtq);
1940 		if (m == NULL) {
1941 			DPRINTFN(10, ("%s: atu_start: data packet\n",
1942 			    sc->atu_dev.dv_xname));
1943 			if (ic->ic_state != IEEE80211_S_RUN) {
1944 				DPRINTFN(25, ("%s: no data till running\n",
1945 				    sc->atu_dev.dv_xname));
1946 				/* put the xfer back on the list */
1947 				s = splnet();
1948 				SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1949 				    atu_list);
1950 				cd->atu_tx_inuse--;
1951 				splx(s);
1952 				break;
1953 			}
1954 
1955 			IFQ_DEQUEUE(&ifp->if_snd, m);
1956 			if (m == NULL) {
1957 				DPRINTFN(25, ("%s: nothing to send\n",
1958 				    sc->atu_dev.dv_xname));
1959 				s = splnet();
1960 				SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1961 				    atu_list);
1962 				cd->atu_tx_inuse--;
1963 				splx(s);
1964 				break;
1965 			}
1966 
1967 #if NBPFILTER > 0
1968 			if (ifp->if_bpf)
1969 				bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1970 #endif
1971 
1972 			m = ieee80211_encap(ifp, m, &ni);
1973 			if (m == NULL)
1974 				goto bad;
1975 			wh = mtod(m, struct ieee80211_frame *);
1976 
1977 #if NBPFILTER > 0
1978 			if (ic->ic_rawbpf != NULL)
1979 				bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
1980 #endif
1981 		} else {
1982 			DPRINTFN(25, ("%s: atu_start: mgmt packet\n",
1983 			    sc->atu_dev.dv_xname));
1984 
1985 			ni = m->m_pkthdr.ph_cookie;
1986 
1987 			wh = mtod(m, struct ieee80211_frame *);
1988 			/* sc->sc_stats.ast_tx_mgmt++; */
1989 		}
1990 
1991 		if (atu_tx_start(sc, ni, c, m)) {
1992 bad:
1993 			s = splnet();
1994 			SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1995 			    atu_list);
1996 			cd->atu_tx_inuse--;
1997 			splx(s);
1998 			/* ifp_if_oerrors++; */
1999 			if (ni != NULL)
2000 				ieee80211_release_node(ic, ni);
2001 			continue;
2002 		}
2003 		ifp->if_timer = 5;
2004 	}
2005 }
2006 
2007 int
2008 atu_init(struct ifnet *ifp)
2009 {
2010 	struct atu_softc	*sc = ifp->if_softc;
2011 	struct ieee80211com	*ic = &sc->sc_ic;
2012 	struct atu_chain	*c;
2013 	usbd_status		err;
2014 	int			i, s;
2015 
2016 	s = splnet();
2017 
2018 	DPRINTFN(10, ("%s: atu_init\n", sc->atu_dev.dv_xname));
2019 
2020 	if (ifp->if_flags & IFF_RUNNING) {
2021 		splx(s);
2022 		return(0);
2023 	}
2024 
2025 	/* Init TX ring */
2026 	if (atu_tx_list_init(sc))
2027 		printf("%s: tx list init failed\n", sc->atu_dev.dv_xname);
2028 
2029 	/* Init RX ring */
2030 	if (atu_rx_list_init(sc))
2031 		printf("%s: rx list init failed\n", sc->atu_dev.dv_xname);
2032 
2033 	/* Load the multicast filter. */
2034 	/*atu_setmulti(sc); */
2035 
2036 	/* Open RX and TX pipes. */
2037 	err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_RX],
2038 	    USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_RX]);
2039 	if (err) {
2040 		DPRINTF(("%s: open rx pipe failed: %s\n",
2041 		    sc->atu_dev.dv_xname, usbd_errstr(err)));
2042 		splx(s);
2043 		return(EIO);
2044 	}
2045 
2046 	err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_TX],
2047 	    USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_TX]);
2048 	if (err) {
2049 		DPRINTF(("%s: open tx pipe failed: %s\n",
2050 		    sc->atu_dev.dv_xname, usbd_errstr(err)));
2051 		splx(s);
2052 		return(EIO);
2053 	}
2054 
2055 	/* Start up the receive pipe. */
2056 	for (i = 0; i < ATU_RX_LIST_CNT; i++) {
2057 		c = &sc->atu_cdata.atu_rx_chain[i];
2058 
2059 		usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_RX], c,
2060 		    c->atu_buf, ATU_RX_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
2061 		    USBD_NO_TIMEOUT, atu_rxeof);
2062 		usbd_transfer(c->atu_xfer);
2063 	}
2064 
2065 	DPRINTFN(10, ("%s: starting up using MAC=%s\n",
2066 	    sc->atu_dev.dv_xname, ether_sprintf(ic->ic_myaddr)));
2067 
2068 	/* Do initial setup */
2069 	err = atu_initial_config(sc);
2070 	if (err) {
2071 		DPRINTF(("%s: initial config failed!\n",
2072 		    sc->atu_dev.dv_xname));
2073 		splx(s);
2074 		return(EIO);
2075 	}
2076 	DPRINTFN(10, ("%s: initialised transceiver\n",
2077 	    sc->atu_dev.dv_xname));
2078 
2079 	/* sc->atu_rxfilt = ATU_RXFILT_UNICAST|ATU_RXFILT_BROADCAST; */
2080 
2081 	/* If we want promiscuous mode, set the allframes bit. */
2082 	/*
2083 	if (ifp->if_flags & IFF_PROMISC)
2084 		sc->atu_rxfilt |= ATU_RXFILT_PROMISC;
2085 	*/
2086 
2087 	ifp->if_flags |= IFF_RUNNING;
2088 	ifq_clr_oactive(&ifp->if_snd);
2089 	splx(s);
2090 
2091 	/* XXX the following HAS to be replaced */
2092 	s = splnet();
2093 	err = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2094 	if (err)
2095 		DPRINTFN(1, ("%s: atu_init: error calling "
2096 		    "ieee80211_net_state", sc->atu_dev.dv_xname));
2097 	splx(s);
2098 
2099 	return 0;
2100 }
2101 
2102 int
2103 atu_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2104 {
2105 	struct atu_softc	*sc = ifp->if_softc;
2106 	int			err = 0, s;
2107 
2108 	s = splnet();
2109 	switch (command) {
2110 	case SIOCSIFADDR:
2111 		DPRINTFN(15, ("%s: SIOCSIFADDR\n", sc->atu_dev.dv_xname));
2112 
2113 		ifp->if_flags |= IFF_UP;
2114 		atu_init(ifp);
2115 		break;
2116 
2117 	case SIOCSIFFLAGS:
2118 		DPRINTFN(15, ("%s: SIOCSIFFLAGS\n", sc->atu_dev.dv_xname));
2119 
2120 		if (ifp->if_flags & IFF_UP) {
2121 			if (ifp->if_flags & IFF_RUNNING &&
2122 			    ifp->if_flags & IFF_PROMISC &&
2123 			    !(sc->atu_if_flags & IFF_PROMISC)) {
2124 /* enable promisc */
2125 #if 0
2126 				sc->atu_rxfilt |= ATU_RXFILT_PROMISC;
2127 				atu_setword(sc, ATU_CMD_SET_PKT_FILTER,
2128 				    sc->atu_rxfilt);
2129 #endif
2130 			} else if (ifp->if_flags & IFF_RUNNING &&
2131 			    !(ifp->if_flags & IFF_PROMISC) &&
2132 			    sc->atu_if_flags & IFF_PROMISC) {
2133 /* disable promisc */
2134 #if 0
2135 				sc->atu_rxfilt &= ~ATU_RXFILT_PROMISC;
2136 				atu_setword(sc, ATU_CMD_SET_PKT_FILTER,
2137 				    sc->atu_rxfilt);
2138 #endif
2139 			} else if (!(ifp->if_flags & IFF_RUNNING))
2140 				atu_init(ifp);
2141 
2142 			DPRINTFN(15, ("%s: ioctl calling atu_init()\n",
2143 			    sc->atu_dev.dv_xname));
2144 			atu_init(ifp);
2145 			err = atu_switch_radio(sc, 1);
2146 		} else {
2147 			if (ifp->if_flags & IFF_RUNNING)
2148 				atu_stop(ifp, 0);
2149 			err = atu_switch_radio(sc, 0);
2150 		}
2151 		sc->atu_if_flags = ifp->if_flags;
2152 		err = 0;
2153 		break;
2154 
2155 	case SIOCADDMULTI:
2156 		DPRINTFN(15, ("%s: SIOCADDMULTI\n", sc->atu_dev.dv_xname));
2157 		/* TODO: implement */
2158 		err = 0;
2159 		break;
2160 
2161 	case SIOCDELMULTI:
2162 		DPRINTFN(15, ("%s: SIOCDELMULTI\n", sc->atu_dev.dv_xname));
2163 		/* TODO: implement */
2164 		err = 0;
2165 		break;
2166 
2167 	default:
2168 		DPRINTFN(15, ("%s: ieee80211_ioctl (%lu)\n",
2169 		    sc->atu_dev.dv_xname, command));
2170 		err = ieee80211_ioctl(ifp, command, data);
2171 		break;
2172 	}
2173 
2174 	if (err == ENETRESET) {
2175 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
2176 		    (IFF_RUNNING|IFF_UP)) {
2177 			DPRINTF(("%s: atu_ioctl(): netreset\n",
2178 			    sc->atu_dev.dv_xname));
2179 			atu_init(ifp);
2180 		}
2181 		err = 0;
2182 	}
2183 
2184 	splx(s);
2185 	return (err);
2186 }
2187 
2188 void
2189 atu_watchdog(struct ifnet *ifp)
2190 {
2191 	struct atu_softc	*sc = ifp->if_softc;
2192 	struct atu_chain	*c;
2193 	usbd_status		stat;
2194 	int			cnt, s;
2195 
2196 	DPRINTF(("%s: atu_watchdog\n", sc->atu_dev.dv_xname));
2197 
2198 	ifp->if_timer = 0;
2199 
2200 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP))
2201 		return;
2202 
2203 	if (usbd_is_dying(sc->atu_udev))
2204 		return;
2205 
2206 	sc = ifp->if_softc;
2207 	s = splnet();
2208 	ifp->if_oerrors++;
2209 	DPRINTF(("%s: watchdog timeout\n", sc->atu_dev.dv_xname));
2210 
2211 	/*
2212 	 * TODO:
2213 	 * we should change this since we have multiple TX transfers...
2214 	 */
2215 	for (cnt = 0; cnt < ATU_TX_LIST_CNT; cnt++) {
2216 		c = &sc->atu_cdata.atu_tx_chain[cnt];
2217 		if (c->atu_in_xfer) {
2218 			usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL,
2219 			    &stat);
2220 			atu_txeof(c->atu_xfer, c, stat);
2221 		}
2222 	}
2223 
2224 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
2225 		atu_start(ifp);
2226 	splx(s);
2227 
2228 	ieee80211_watchdog(ifp);
2229 }
2230 
2231 /*
2232  * Stop the adapter and free any mbufs allocated to the
2233  * RX and TX lists.
2234  */
2235 void
2236 atu_stop(struct ifnet *ifp, int disable)
2237 {
2238 	struct atu_softc	*sc = ifp->if_softc;
2239 	struct atu_cdata	*cd;
2240 	usbd_status		err;
2241 	int s;
2242 
2243 	s = splnet();
2244 	ifp->if_flags &= ~IFF_RUNNING;
2245 	ifq_clr_oactive(&ifp->if_snd);
2246 	ifp->if_timer = 0;
2247 
2248 	/* Stop transfers. */
2249 	if (sc->atu_ep[ATU_ENDPT_RX] != NULL) {
2250 		usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]);
2251 		err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_RX]);
2252 		if (err) {
2253 			DPRINTF(("%s: close rx pipe failed: %s\n",
2254 			    sc->atu_dev.dv_xname, usbd_errstr(err)));
2255 		}
2256 		sc->atu_ep[ATU_ENDPT_RX] = NULL;
2257 	}
2258 
2259 	if (sc->atu_ep[ATU_ENDPT_TX] != NULL) {
2260 		usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]);
2261 		err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_TX]);
2262 		if (err) {
2263 			DPRINTF(("%s: close tx pipe failed: %s\n",
2264 			    sc->atu_dev.dv_xname, usbd_errstr(err)));
2265 		}
2266 		sc->atu_ep[ATU_ENDPT_TX] = NULL;
2267 	}
2268 
2269 	/* Free RX/TX/MGMT list resources. */
2270 	cd = &sc->atu_cdata;
2271 	atu_xfer_list_free(sc, cd->atu_rx_chain, ATU_RX_LIST_CNT);
2272 	atu_xfer_list_free(sc, cd->atu_tx_chain, ATU_TX_LIST_CNT);
2273 
2274 	/* Let's be nice and turn off the radio before we leave */
2275 	atu_switch_radio(sc, 0);
2276 
2277 	splx(s);
2278 }
2279