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