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