Home | History | Annotate | Line # | Download | only in usb
if_atu.c revision 1.16
      1 /*	$NetBSD: if_atu.c,v 1.16 2006/05/14 05:42:43 christos 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.16 2006/05/14 05:42:43 christos 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 	{ USB_VENDOR_OQO,	USB_PRODUCT_OQO_WIFI01,
    145 	  RadioRFMD2958_SMC,	ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
    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, caddr_t);
    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(struct device *);
    199 void	atu_external_firmware(struct device *);
    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 USB_DECLARE_DRIVER(atu);
    220 
    221 usbd_status
    222 atu_usb_request(struct atu_softc *sc, u_int8_t type,
    223     u_int8_t request, u_int16_t value, u_int16_t index, u_int16_t length,
    224     u_int8_t *data)
    225 {
    226 	usb_device_request_t	req;
    227 	usbd_xfer_handle	xfer;
    228 	usbd_status		err;
    229 	int			total_len = 0, s;
    230 
    231 	req.bmRequestType = type;
    232 	req.bRequest = request;
    233 	USETW(req.wValue, value);
    234 	USETW(req.wIndex, index);
    235 	USETW(req.wLength, length);
    236 
    237 #ifdef ATU_DEBUG
    238 	if (atudebug) {
    239 		DPRINTFN(20, ("%s: req=%02x val=%02x ind=%02x "
    240 		    "len=%02x\n", USBDEVNAME(sc->atu_dev), request,
    241 		    value, index, length));
    242 	}
    243 #endif /* ATU_DEBUG */
    244 
    245 	s = splnet();
    246 
    247 	xfer = usbd_alloc_xfer(sc->atu_udev);
    248 	usbd_setup_default_xfer(xfer, sc->atu_udev, 0, 500000, &req, data,
    249 	    length, USBD_SHORT_XFER_OK, 0);
    250 
    251 	err = usbd_sync_transfer(xfer);
    252 
    253 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    254 
    255 #ifdef ATU_DEBUG
    256 	if (atudebug) {
    257 		if (type & UT_READ) {
    258 			DPRINTFN(20, ("%s: transfered 0x%x bytes in\n",
    259 			    USBDEVNAME(sc->atu_dev), total_len));
    260 		} else {
    261 			if (total_len != length)
    262 				DPRINTF(("%s: wrote only %x bytes\n",
    263 				    USBDEVNAME(sc->atu_dev), total_len));
    264 		}
    265 	}
    266 #endif /* ATU_DEBUG */
    267 
    268 	usbd_free_xfer(xfer);
    269 
    270 	splx(s);
    271 	return(err);
    272 }
    273 
    274 int
    275 atu_send_command(struct atu_softc *sc, u_int8_t *command, int size)
    276 {
    277 	return atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
    278 	    0x0000, size, command);
    279 }
    280 
    281 int
    282 atu_get_cmd_status(struct atu_softc *sc, u_int8_t cmd, u_int8_t *status)
    283 {
    284 	/*
    285 	 * all other drivers (including Windoze) request 40 bytes of status
    286 	 * and get a short-xfer of just 6 bytes. we can save 34 bytes of
    287 	 * buffer if we just request those 6 bytes in the first place :)
    288 	 */
    289 	/*
    290 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
    291 	    0x0000, 40, status);
    292 	*/
    293 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
    294 	    0x0000, 6, status);
    295 }
    296 
    297 int
    298 atu_wait_completion(struct atu_softc *sc, u_int8_t cmd, u_int8_t *status)
    299 {
    300 	int			idle_count = 0, err;
    301 	u_int8_t		statusreq[6];
    302 
    303 	DPRINTFN(15, ("%s: wait-completion: cmd=%02x\n",
    304 	    USBDEVNAME(sc->atu_dev), cmd));
    305 
    306 	while (1) {
    307 		err = atu_get_cmd_status(sc, cmd, statusreq);
    308 		if (err)
    309 			return err;
    310 
    311 #ifdef ATU_DEBUG
    312 		if (atudebug) {
    313 			DPRINTFN(20, ("%s: status=%s cmd=%02x\n",
    314 			    USBDEVNAME(sc->atu_dev),
    315 			ether_sprintf(statusreq), cmd));
    316 		}
    317 #endif /* ATU_DEBUG */
    318 
    319 		/*
    320 		 * during normal operations waiting on STATUS_IDLE
    321 		 * will never happen more than once
    322 		 */
    323 		if ((statusreq[5] == STATUS_IDLE) && (idle_count++ > 20)) {
    324 			DPRINTF(("%s: idle_count > 20!\n",
    325 			    USBDEVNAME(sc->atu_dev)));
    326 			return 0;
    327 		}
    328 
    329 		if ((statusreq[5] != STATUS_IN_PROGRESS) &&
    330 		    (statusreq[5] != STATUS_IDLE)) {
    331 			if (status != NULL)
    332 				*status = statusreq[5];
    333 			return 0;
    334 		}
    335 		usbd_delay_ms(sc->atu_udev, 25);
    336 	}
    337 }
    338 
    339 int
    340 atu_send_mib(struct atu_softc *sc, u_int8_t type, u_int8_t size,
    341     u_int8_t index, void *data)
    342 {
    343 	int				err;
    344 	struct atu_cmd_set_mib		request;
    345 
    346 	/*
    347 	 * We don't construct a MIB packet first and then memcpy it into an
    348 	 * Atmel-command-packet, we just construct it the right way at once :)
    349 	 */
    350 
    351 	memset(&request, 0, sizeof(request));
    352 
    353 	request.AtCmd = CMD_SET_MIB;
    354 	USETW(request.AtSize, size + 4);
    355 
    356 	request.MIBType = type;
    357 	request.MIBSize = size;
    358 	request.MIBIndex = index;
    359 	request.MIBReserved = 0;
    360 
    361 	/*
    362 	 * For 1 and 2 byte requests we assume a direct value,
    363 	 * everything bigger than 2 bytes we assume a pointer to the data
    364 	 */
    365 	switch (size) {
    366 	case 0:
    367 		break;
    368 	case 1:
    369 		request.data[0]=(long)data & 0x000000ff;
    370 		break;
    371 	case 2:
    372 		request.data[0]=(long)data & 0x000000ff;
    373 		request.data[1]=(long)data >> 8;
    374 		break;
    375 	default:
    376 		memcpy(request.data, data, size);
    377 		break;
    378 	}
    379 
    380 	err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
    381 	    0x0000, size+8, (uByte *)&request);
    382 	if (err)
    383 		return (err);
    384 
    385 	DPRINTFN(15, ("%s: sendmib : waitcompletion...\n",
    386 	    USBDEVNAME(sc->atu_dev)));
    387 	return atu_wait_completion(sc, CMD_SET_MIB, NULL);
    388 }
    389 
    390 int
    391 atu_get_mib(struct atu_softc *sc, u_int8_t type, u_int8_t size,
    392     u_int8_t index, u_int8_t *buf)
    393 {
    394 
    395 	/* linux/at76c503.c - 478 */
    396 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x033,
    397 	    type << 8, index, size, buf);
    398 }
    399 
    400 #if 0
    401 int
    402 atu_start_ibss(struct atu_softc *sc)
    403 {
    404 	struct ieee80211com		*ic = &sc->sc_ic;
    405 	int				err;
    406 	struct atu_cmd_start_ibss	Request;
    407 
    408 	Request.Cmd = CMD_START_IBSS;
    409 	Request.Reserved = 0;
    410 	Request.Size = sizeof(Request) - 4;
    411 
    412 	memset(Request.BSSID, 0x00, sizeof(Request.BSSID));
    413 	memset(Request.SSID, 0x00, sizeof(Request.SSID));
    414 	memcpy(Request.SSID, ic->ic_des_ssid, ic->ic_des_ssidlen);
    415 	Request.SSIDSize = ic->ic_des_ssidlen;
    416 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
    417 		Request.Channel = (u_int8_t)sc->atu_desired_channel;
    418 	else
    419 		Request.Channel = ATU_DEFAULT_CHANNEL;
    420 	Request.BSSType = AD_HOC_MODE;
    421 	memset(Request.Res, 0x00, sizeof(Request.Res));
    422 
    423 	/* Write config to adapter */
    424 	err = atu_send_command(sc, (u_int8_t *)&Request, sizeof(Request));
    425 	if (err) {
    426 		DPRINTF(("%s: start ibss failed!\n",
    427 		    USBDEVNAME(sc->atu_dev)));
    428 		return err;
    429 	}
    430 
    431 	/* Wait for the adapter to do it's thing */
    432 	err = atu_wait_completion(sc, CMD_START_IBSS, NULL);
    433 	if (err) {
    434 		DPRINTF(("%s: error waiting for start_ibss\n",
    435 		    USBDEVNAME(sc->atu_dev)));
    436 		return err;
    437 	}
    438 
    439 	/* Get the current BSSID */
    440 	err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, sc->atu_bssid);
    441 	if (err) {
    442 		DPRINTF(("%s: could not get BSSID!\n",
    443 		    USBDEVNAME(sc->atu_dev)));
    444 		return err;
    445 	}
    446 
    447 	DPRINTF(("%s: started a new IBSS (BSSID=%s)\n",
    448 	    USBDEVNAME(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
    449 	return 0;
    450 }
    451 #endif
    452 
    453 int
    454 atu_start_scan(struct atu_softc *sc)
    455 {
    456 	struct ieee80211com		*ic = &sc->sc_ic;
    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, ic->ic_des_essid, ic->ic_des_esslen);
    473 	Scan.SSID_Len = ic->ic_des_esslen;
    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_def_txkey].wk_keylen) {
    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_def_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_keylen);
    626 	}
    627 
    628 	/* Setting the SSID here doesn't seem to do anything */
    629 	memset(cmd.SSID, 0x00, sizeof(cmd.SSID));
    630 	memcpy(cmd.SSID, ic->ic_des_essid, ic->ic_des_esslen);
    631 	cmd.SSID_Len = ic->ic_des_esslen;
    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 = 0;	/* XXX: GCC */
    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_ifp;
   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_node_table_reset(&ic->ic_scan);
   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				*devinfop;
   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 	devinfop = usbd_devinfo_alloc(dev, 0);
   1171 	USB_ATTACH_SETUP;
   1172 	printf("%s: %s\n", USBDEVNAME(sc->atu_dev), devinfop);
   1173 	usbd_devinfo_free(devinfop);
   1174 
   1175 	err = usbd_set_config_no(dev, ATU_CONFIG_NO, 1);
   1176 	if (err) {
   1177 		printf("%s: setting config no failed\n",
   1178 		    USBDEVNAME(sc->atu_dev));
   1179 		USB_ATTACH_ERROR_RETURN;
   1180 	}
   1181 
   1182 	err = usbd_device2interface_handle(dev, ATU_IFACE_IDX, &sc->atu_iface);
   1183 	if (err) {
   1184 		printf("%s: getting interface handle failed\n",
   1185 		    USBDEVNAME(sc->atu_dev));
   1186 		USB_ATTACH_ERROR_RETURN;
   1187 	}
   1188 
   1189 	sc->atu_unit = device_unit(self);
   1190 	sc->atu_udev = dev;
   1191 
   1192 	/*
   1193 	 * look up the radio_type for the device
   1194 	 * basically does the same as USB_MATCH
   1195 	 */
   1196 	for (i = 0; i < sizeof(atu_devs)/sizeof(atu_devs[0]); i++) {
   1197 		struct atu_type *t = &atu_devs[i];
   1198 
   1199 		if (uaa->vendor == t->atu_vid &&
   1200 		    uaa->product == t->atu_pid) {
   1201 			sc->atu_radio = t->atu_radio;
   1202 			sc->atu_quirk = t->atu_quirk;
   1203 		}
   1204 	}
   1205 
   1206 	/*
   1207 	 * Check in the interface descriptor if we're in DFU mode
   1208 	 * If we're in DFU mode, we upload the external firmware
   1209 	 * If we're not, the PC must have rebooted without power-cycling
   1210 	 * the device.. I've tried this out, a reboot only requeres the
   1211 	 * external firmware to be reloaded :)
   1212 	 *
   1213 	 * Hmm. The at76c505a doesn't report a DFU descriptor when it's
   1214 	 * in DFU mode... Let's just try to get the opmode
   1215 	 */
   1216 	err = atu_get_opmode(sc, &mode);
   1217 	DPRINTFN(20, ("%s: opmode: %d\n", USBDEVNAME(sc->atu_dev), mode));
   1218 	if (err || (mode != MODE_NETCARD && mode != MODE_NOFLASHNETCARD)) {
   1219 		DPRINTF(("%s: starting internal firmware download\n",
   1220 		    USBDEVNAME(sc->atu_dev)));
   1221 
   1222 		atu_internal_firmware((struct device *)sc);
   1223 		/*
   1224 		 * atu_internal_firmware will cause a reset of the device
   1225 		 * so we don't want to do any more configuration after this
   1226 		 * point.
   1227 		 */
   1228 		USB_ATTACH_SUCCESS_RETURN;
   1229 	}
   1230 
   1231 	uaa->iface = sc->atu_iface;
   1232 
   1233 	if (mode != MODE_NETCARD) {
   1234 		DPRINTFN(15, ("%s: device needs external firmware\n",
   1235 		    USBDEVNAME(sc->atu_dev)));
   1236 
   1237 		if (mode != MODE_NOFLASHNETCARD) {
   1238 			DPRINTF(("%s: unexpected opmode=%d\n",
   1239 			    USBDEVNAME(sc->atu_dev), mode));
   1240 		}
   1241 
   1242 		/*
   1243 		 * There is no difference in opmode before and after external
   1244 		 * firmware upload with the SMC2662 V.4 . So instead we'll try
   1245 		 * to read the channel number. If we succeed, external
   1246 		 * firmwaremust have been already uploaded...
   1247 		 */
   1248 		if (sc->atu_radio != RadioIntersil) {
   1249 			err = atu_get_mib(sc, MIB_PHY__CHANNEL, &channel);
   1250 			if (!err) {
   1251 				DPRINTF(("%s: external firmware has already"
   1252 				    " been downloaded\n",
   1253 				    USBDEVNAME(sc->atu_dev)));
   1254 				atu_complete_attach(sc);
   1255 				USB_ATTACH_SUCCESS_RETURN;
   1256 			}
   1257 		}
   1258 
   1259 		atu_external_firmware((struct device *)sc);
   1260 
   1261 		/*
   1262 		 * atu_external_firmware will call atu_complete_attach after
   1263 		 * it's finished so we can just return.
   1264 		 */
   1265 	} else {
   1266 		/* all the firmwares are in place, so complete the attach */
   1267 		atu_complete_attach(sc);
   1268 	}
   1269 
   1270 	USB_ATTACH_SUCCESS_RETURN;
   1271 }
   1272 
   1273 void
   1274 atu_complete_attach(struct atu_softc *sc)
   1275 {
   1276 	struct ieee80211com		*ic = &sc->sc_ic;
   1277 	struct ifnet			*ifp = &sc->sc_if;
   1278 	usb_interface_descriptor_t	*id;
   1279 	usb_endpoint_descriptor_t	*ed;
   1280 	usbd_status			err;
   1281 	int				i;
   1282 #ifdef ATU_DEBUG
   1283 	struct atu_fw			fw;
   1284 #endif
   1285 
   1286 	id = usbd_get_interface_descriptor(sc->atu_iface);
   1287 
   1288 	/* Find endpoints. */
   1289 	for (i = 0; i < id->bNumEndpoints; i++) {
   1290 		ed = usbd_interface2endpoint_descriptor(sc->atu_iface, i);
   1291 		if (!ed) {
   1292 			DPRINTF(("%s: num_endp:%d\n", USBDEVNAME(sc->atu_dev),
   1293 			    sc->atu_iface->idesc->bNumEndpoints));
   1294 			DPRINTF(("%s: couldn't get ep %d\n",
   1295 			    USBDEVNAME(sc->atu_dev), i));
   1296 			return;
   1297 		}
   1298 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1299 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1300 			sc->atu_ed[ATU_ENDPT_RX] = ed->bEndpointAddress;
   1301 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1302 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1303 			sc->atu_ed[ATU_ENDPT_TX] = ed->bEndpointAddress;
   1304 		}
   1305 	}
   1306 
   1307 	/* read device config & get MAC address */
   1308 	err = atu_get_card_config(sc);
   1309 	if (err) {
   1310 		printf("\n%s: could not get card cfg!\n",
   1311 		    USBDEVNAME(sc->atu_dev));
   1312 		return;
   1313 	}
   1314 
   1315 #ifdef ATU_DEBUG
   1316 	/* DEBUG : try to get firmware version */
   1317 	err = atu_get_mib(sc, MIB_FW_VERSION, sizeof(fw), 0,
   1318 	    (u_int8_t *)&fw);
   1319 	if (!err) {
   1320 		DPRINTFN(15, ("%s: firmware: maj:%d min:%d patch:%d "
   1321 		    "build:%d\n", USBDEVNAME(sc->atu_dev), fw.major, fw.minor,
   1322 		    fw.patch, fw.build));
   1323 	} else {
   1324 		DPRINTF(("%s: get firmware version failed\n",
   1325 		    USBDEVNAME(sc->atu_dev)));
   1326 	}
   1327 #endif /* ATU_DEBUG */
   1328 
   1329 	/* Show the world our MAC address */
   1330 	printf("%s: MAC address %s\n", USBDEVNAME(sc->atu_dev),
   1331 	    ether_sprintf(ic->ic_myaddr));
   1332 
   1333 	sc->atu_cdata.atu_tx_inuse = 0;
   1334 	sc->atu_encrypt = ATU_WEP_OFF;
   1335 	sc->atu_wepkeylen = ATU_WEP_104BITS;
   1336 	sc->atu_wepkey = 0;
   1337 
   1338 	bzero(sc->atu_bssid, ETHER_ADDR_LEN);
   1339 	sc->atu_channel = ATU_DEFAULT_CHANNEL;
   1340 	sc->atu_desired_channel = IEEE80211_CHAN_ANY;
   1341 	sc->atu_mode = INFRASTRUCTURE_MODE;
   1342 
   1343 	ic->ic_ifp = ifp;
   1344 	ic->ic_phytype = IEEE80211_T_DS;
   1345 	ic->ic_opmode = IEEE80211_M_STA;
   1346 	ic->ic_state = IEEE80211_S_INIT;
   1347 #ifdef FIXME
   1348 	ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP | IEEE80211_C_SCANALL;
   1349 #else
   1350 	ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP;
   1351 #endif
   1352 
   1353 	i = 0;
   1354 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 2;
   1355 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 4;
   1356 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 11;
   1357 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[i++] = 22;
   1358 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = i;
   1359 
   1360 	for (i = 1; i <= 14; i++) {
   1361 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B |
   1362 		    IEEE80211_CHAN_PASSIVE;
   1363 		ic->ic_channels[i].ic_freq = ieee80211_ieee2mhz(i,
   1364 		    ic->ic_channels[i].ic_flags);
   1365 	}
   1366 
   1367 	ic->ic_ibss_chan = &ic->ic_channels[0];
   1368 
   1369 	ifp->if_softc = sc;
   1370 	memcpy(ifp->if_xname, USBDEVNAME(sc->atu_dev), IFNAMSIZ);
   1371 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1372 	ifp->if_init = atu_init;
   1373 	ifp->if_stop = atu_stop;
   1374 	ifp->if_start = atu_start;
   1375 	ifp->if_ioctl = atu_ioctl;
   1376 	ifp->if_watchdog = atu_watchdog;
   1377 	ifp->if_mtu = ATU_DEFAULT_MTU;
   1378 	IFQ_SET_READY(&ifp->if_snd);
   1379 
   1380 	/* Call MI attach routine. */
   1381 	if_attach(ifp);
   1382 	ieee80211_ifattach(ic);
   1383 
   1384 	sc->sc_newstate = ic->ic_newstate;
   1385 	ic->ic_newstate = atu_newstate;
   1386 
   1387 	/* setup ifmedia interface */
   1388 	ieee80211_media_init(ic, atu_media_change, atu_media_status);
   1389 
   1390 	usb_init_task(&sc->sc_task, atu_task, sc);
   1391 
   1392 	sc->sc_state = ATU_S_OK;
   1393 }
   1394 
   1395 USB_DETACH(atu)
   1396 {
   1397 	USB_DETACH_START(atu, sc);
   1398 	struct ifnet		*ifp = &sc->sc_if;
   1399 
   1400 	DPRINTFN(10, ("%s: atu_detach state=%d\n", USBDEVNAME(sc->atu_dev),
   1401 	    sc->sc_state));
   1402 
   1403 	if (sc->sc_state != ATU_S_UNCONFIG) {
   1404 		atu_stop(ifp, 1);
   1405 
   1406 		ieee80211_ifdetach(&sc->sc_ic);
   1407 		if_detach(ifp);
   1408 	}
   1409 
   1410 	return(0);
   1411 }
   1412 
   1413 int
   1414 atu_activate(device_ptr_t self, enum devact act)
   1415 {
   1416 	struct atu_softc *sc = (struct atu_softc *)self;
   1417 
   1418 	switch (act) {
   1419 	case DVACT_ACTIVATE:
   1420 		return (EOPNOTSUPP);
   1421 		break;
   1422 	case DVACT_DEACTIVATE:
   1423 		if (sc->sc_state != ATU_S_UNCONFIG) {
   1424 			if_deactivate(&sc->atu_ec.ec_if);
   1425 			sc->sc_state = ATU_S_DEAD;
   1426 		}
   1427 		break;
   1428 	}
   1429 	return (0);
   1430 }
   1431 
   1432 /*
   1433  * Initialize an RX descriptor and attach an MBUF cluster.
   1434  */
   1435 int
   1436 atu_newbuf(struct atu_softc *sc, struct atu_chain *c, struct mbuf *m)
   1437 {
   1438 	struct mbuf		*m_new = NULL;
   1439 
   1440 	if (m == NULL) {
   1441 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1442 		if (m_new == NULL) {
   1443 			DPRINTF(("%s: no memory for rx list\n",
   1444 			    USBDEVNAME(sc->atu_dev)));
   1445 			return(ENOBUFS);
   1446 		}
   1447 
   1448 		MCLGET(m_new, M_DONTWAIT);
   1449 		if (!(m_new->m_flags & M_EXT)) {
   1450 			DPRINTF(("%s: no memory for rx list\n",
   1451 			    USBDEVNAME(sc->atu_dev)));
   1452 			m_freem(m_new);
   1453 			return(ENOBUFS);
   1454 		}
   1455 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
   1456 	} else {
   1457 		m_new = m;
   1458 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
   1459 		m_new->m_data = m_new->m_ext.ext_buf;
   1460 	}
   1461 	c->atu_mbuf = m_new;
   1462 	return(0);
   1463 }
   1464 
   1465 int
   1466 atu_rx_list_init(struct atu_softc *sc)
   1467 {
   1468 	struct atu_cdata	*cd = &sc->atu_cdata;
   1469 	struct atu_chain	*c;
   1470 	int			i;
   1471 
   1472 	DPRINTFN(15, ("%s: atu_rx_list_init: enter\n",
   1473 	    USBDEVNAME(sc->atu_dev)));
   1474 
   1475 	for (i = 0; i < ATU_RX_LIST_CNT; i++) {
   1476 		c = &cd->atu_rx_chain[i];
   1477 		c->atu_sc = sc;
   1478 		c->atu_idx = i;
   1479 		if (c->atu_xfer == NULL) {
   1480 			c->atu_xfer = usbd_alloc_xfer(sc->atu_udev);
   1481 			if (c->atu_xfer == NULL)
   1482 				return (ENOBUFS);
   1483 			c->atu_buf = usbd_alloc_buffer(c->atu_xfer,
   1484 			    ATU_RX_BUFSZ);
   1485 			if (c->atu_buf == NULL) /* XXX free xfer */
   1486 				return (ENOBUFS);
   1487 			if (atu_newbuf(sc, c, NULL) == ENOBUFS) /* XXX free? */
   1488 				return(ENOBUFS);
   1489 		}
   1490 	}
   1491 	return (0);
   1492 }
   1493 
   1494 int
   1495 atu_tx_list_init(struct atu_softc *sc)
   1496 {
   1497 	struct atu_cdata	*cd = &sc->atu_cdata;
   1498 	struct atu_chain	*c;
   1499 	int			i;
   1500 
   1501 	DPRINTFN(15, ("%s: atu_tx_list_init\n",
   1502 	    USBDEVNAME(sc->atu_dev)));
   1503 
   1504 	SLIST_INIT(&cd->atu_tx_free);
   1505 	sc->atu_cdata.atu_tx_inuse = 0;
   1506 
   1507 	for (i = 0; i < ATU_TX_LIST_CNT; i++) {
   1508 		c = &cd->atu_tx_chain[i];
   1509 		c->atu_sc = sc;
   1510 		c->atu_idx = i;
   1511 		if (c->atu_xfer == NULL) {
   1512 			c->atu_xfer = usbd_alloc_xfer(sc->atu_udev);
   1513 			if (c->atu_xfer == NULL)
   1514 				return(ENOBUFS);
   1515 			c->atu_mbuf = NULL;
   1516 			c->atu_buf = usbd_alloc_buffer(c->atu_xfer,
   1517 			    ATU_TX_BUFSZ);
   1518 			if (c->atu_buf == NULL)
   1519 				return(ENOBUFS); /* XXX free xfer */
   1520 			SLIST_INSERT_HEAD(&cd->atu_tx_free, c, atu_list);
   1521 		}
   1522 	}
   1523 	return(0);
   1524 }
   1525 
   1526 void
   1527 atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch,
   1528     int listlen)
   1529 {
   1530 	int			i;
   1531 
   1532 	/* Free resources. */
   1533 	for (i = 0; i < listlen; i++) {
   1534 		if (ch[i].atu_buf != NULL)
   1535 			ch[i].atu_buf = NULL;
   1536 		if (ch[i].atu_mbuf != NULL) {
   1537 			m_freem(ch[i].atu_mbuf);
   1538 			ch[i].atu_mbuf = NULL;
   1539 		}
   1540 		if (ch[i].atu_xfer != NULL) {
   1541 			usbd_free_xfer(ch[i].atu_xfer);
   1542 			ch[i].atu_xfer = NULL;
   1543 		}
   1544 	}
   1545 }
   1546 
   1547 /*
   1548  * A frame has been uploaded: pass the resulting mbuf chain up to
   1549  * the higher level protocols.
   1550  */
   1551 void
   1552 atu_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1553 {
   1554 	struct atu_chain	*c = (struct atu_chain *)priv;
   1555 	struct atu_softc	*sc = c->atu_sc;
   1556 	struct ieee80211com	*ic = &sc->sc_ic;
   1557 	struct ifnet		*ifp = &sc->sc_if;
   1558 	struct atu_rx_hdr	*h;
   1559 	struct ieee80211_frame_min	*wh;
   1560 	struct ieee80211_node	*ni;
   1561 	struct mbuf		*m;
   1562 	u_int32_t		len;
   1563 	int			s;
   1564 
   1565 	DPRINTFN(25, ("%s: atu_rxeof\n", USBDEVNAME(sc->atu_dev)));
   1566 
   1567 	if (sc->sc_state != ATU_S_OK)
   1568 		return;
   1569 
   1570 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP))
   1571 		goto done;
   1572 
   1573 	if (status != USBD_NORMAL_COMPLETION) {
   1574 		DPRINTF(("%s: status != USBD_NORMAL_COMPLETION\n",
   1575 		    USBDEVNAME(sc->atu_dev)));
   1576 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1577 			return;
   1578 		}
   1579 #if 0
   1580 		if (status == USBD_IOERROR) {
   1581 			DPRINTF(("%s: rx: EEK! lost device?\n",
   1582 			    USBDEVNAME(sc->atu_dev)));
   1583 
   1584 			/*
   1585 			 * My experience with USBD_IOERROR is that trying to
   1586 			 * restart the transfer will always fail and we'll
   1587 			 * keep on looping restarting transfers untill someone
   1588 			 * pulls the plug of the device.
   1589 			 * So we don't restart the transfer, but just let it
   1590 			 * die... If someone knows of a situation where we can
   1591 			 * recover from USBD_IOERROR, let me know.
   1592 			 */
   1593 			splx(s);
   1594 			return;
   1595 		}
   1596 #endif /* 0 */
   1597 
   1598 		if (usbd_ratecheck(&sc->atu_rx_notice)) {
   1599 			DPRINTF(("%s: usb error on rx: %s\n",
   1600 			    USBDEVNAME(sc->atu_dev), usbd_errstr(status)));
   1601 		}
   1602 		if (status == USBD_STALLED)
   1603 			usbd_clear_endpoint_stall_async(
   1604 			    sc->atu_ep[ATU_ENDPT_RX]);
   1605 		goto done;
   1606 	}
   1607 
   1608 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   1609 
   1610 	if (len <= 1) {
   1611 		DPRINTF(("%s: atu_rxeof: too short\n",
   1612 		    USBDEVNAME(sc->atu_dev)));
   1613 		goto done;
   1614 	}
   1615 
   1616 	h = (struct atu_rx_hdr *)c->atu_buf;
   1617 	len = UGETW(h->length) - 4; /* XXX magic number */
   1618 
   1619 	m = c->atu_mbuf;
   1620 	memcpy(mtod(m, char *), c->atu_buf + ATU_RX_HDRLEN, len);
   1621 	m->m_pkthdr.rcvif = ifp;
   1622 	m->m_pkthdr.len = m->m_len = len;
   1623 
   1624 	wh = mtod(m, struct ieee80211_frame_min *);
   1625 	ni = ieee80211_find_rxnode(ic, wh);
   1626 
   1627 	ifp->if_ipackets++;
   1628 
   1629 	s = splnet();
   1630 
   1631 	if (atu_newbuf(sc, c, NULL) == ENOBUFS) {
   1632 		ifp->if_ierrors++;
   1633 		goto done1; /* XXX if we can't allocate, why restart it? */
   1634 	}
   1635 
   1636 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1637 		/*
   1638 		 * WEP is decrypted by hardware. Clear WEP bit
   1639 		 * header for ieee80211_input().
   1640 		 */
   1641 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1642 	}
   1643 
   1644 	ieee80211_input(ic, m, ni, h->rssi, UGETDW(h->rx_time));
   1645 
   1646 	ieee80211_free_node(ni);
   1647 done1:
   1648 	splx(s);
   1649 done:
   1650 	/* Setup new transfer. */
   1651 	usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_RX], c, c->atu_buf,
   1652 	    ATU_RX_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1653 		atu_rxeof);
   1654 	usbd_transfer(c->atu_xfer);
   1655 }
   1656 
   1657 /*
   1658  * A frame was downloaded to the chip. It's safe for us to clean up
   1659  * the list buffers.
   1660  */
   1661 void
   1662 atu_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1663 {
   1664 	struct atu_chain	*c = (struct atu_chain *)priv;
   1665 	struct atu_softc	*sc = c->atu_sc;
   1666 	struct ifnet		*ifp = &sc->sc_if;
   1667 	usbd_status		err;
   1668 	int			s;
   1669 
   1670 	DPRINTFN(25, ("%s: atu_txeof status=%d\n", USBDEVNAME(sc->atu_dev),
   1671 	    status));
   1672 
   1673 	if (c->atu_mbuf) {
   1674 		m_freem(c->atu_mbuf);
   1675 		c->atu_mbuf = NULL;
   1676 	}
   1677 
   1678 	if (status != USBD_NORMAL_COMPLETION) {
   1679 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1680 			return;
   1681 
   1682 		DPRINTF(("%s: usb error on tx: %s\n", USBDEVNAME(sc->atu_dev),
   1683 		    usbd_errstr(status)));
   1684 		if (status == USBD_STALLED)
   1685 			usbd_clear_endpoint_stall_async(sc->atu_ep[ATU_ENDPT_TX]);
   1686 		return;
   1687 	}
   1688 
   1689 	usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL, &err);
   1690 
   1691 	if (err)
   1692 		ifp->if_oerrors++;
   1693 	else
   1694 		ifp->if_opackets++;
   1695 
   1696 	s = splnet();
   1697 	SLIST_INSERT_HEAD(&sc->atu_cdata.atu_tx_free, c, atu_list);
   1698 	sc->atu_cdata.atu_tx_inuse--;
   1699 	if (sc->atu_cdata.atu_tx_inuse == 0)
   1700 		ifp->if_timer = 0;
   1701 	ifp->if_flags &= ~IFF_OACTIVE;
   1702 	splx(s);
   1703 
   1704 	atu_start(ifp);
   1705 }
   1706 
   1707 u_int8_t
   1708 atu_calculate_padding(int size)
   1709 {
   1710 	size %= 64;
   1711 
   1712 	if (size < 50)
   1713 		return (50 - size);
   1714 	if (size >=61)
   1715 		return (64 + 50 - size);
   1716 	return (0);
   1717 }
   1718 
   1719 int
   1720 atu_tx_start(struct atu_softc *sc, struct ieee80211_node *ni,
   1721     struct atu_chain *c, struct mbuf *m)
   1722 {
   1723 	int			len;
   1724 	struct atu_tx_hdr	*h;
   1725 	usbd_status		err;
   1726 	u_int8_t		pad;
   1727 
   1728 	DPRINTFN(25, ("%s: atu_tx_start\n", USBDEVNAME(sc->atu_dev)));
   1729 
   1730 	/* Don't try to send when we're shutting down the driver */
   1731 	if (sc->sc_state != ATU_S_OK) {
   1732 		m_freem(m);
   1733 		return(EIO);
   1734 	}
   1735 
   1736 	/*
   1737 	 * Copy the mbuf data into a contiguous buffer, leaving
   1738 	 * enough room for the atmel headers
   1739 	 */
   1740 	len = m->m_pkthdr.len;
   1741 
   1742 	m_copydata(m, 0, m->m_pkthdr.len, c->atu_buf + ATU_TX_HDRLEN);
   1743 
   1744 	h = (struct atu_tx_hdr *)c->atu_buf;
   1745 	memset(h, 0, ATU_TX_HDRLEN);
   1746 	USETW(h->length, len);
   1747 	h->tx_rate = 4; /* XXX rate = auto */
   1748 	len += ATU_TX_HDRLEN;
   1749 
   1750 	pad = atu_calculate_padding(len);
   1751 	len += pad;
   1752 	h->padding = pad;
   1753 
   1754 	c->atu_length = len;
   1755 	c->atu_mbuf = m;
   1756 
   1757 	usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_TX],
   1758 	    c, c->atu_buf, c->atu_length, USBD_NO_COPY, ATU_TX_TIMEOUT,
   1759 	    atu_txeof);
   1760 
   1761 	/* Let's get this thing into the air! */
   1762 	c->atu_in_xfer = 1;
   1763 	err = usbd_transfer(c->atu_xfer);
   1764 	if (err != USBD_IN_PROGRESS) {
   1765 		DPRINTFN(25, ("%s: atu_tx_start, err=%d",
   1766 		    USBDEVNAME(sc->atu_dev), err));
   1767 		c->atu_mbuf = NULL;
   1768 		m_freem(m);
   1769 		return(EIO);
   1770 	}
   1771 
   1772 	return (0);
   1773 }
   1774 
   1775 void
   1776 atu_start(struct ifnet *ifp)
   1777 {
   1778 	struct atu_softc	*sc = ifp->if_softc;
   1779 	struct ieee80211com	*ic = &sc->sc_ic;
   1780 	struct atu_cdata	*cd = &sc->atu_cdata;
   1781 	struct ieee80211_node	*ni;
   1782 	struct atu_chain	*c;
   1783 	struct mbuf		*m = NULL;
   1784 	int			s;
   1785 
   1786 	DPRINTFN(25, ("%s: atu_start: enter\n", USBDEVNAME(sc->atu_dev)));
   1787 
   1788 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1789 		return;
   1790 	}
   1791 	if (ifp->if_flags & IFF_OACTIVE) {
   1792 		DPRINTFN(30, ("%s: atu_start: IFF_OACTIVE\n",
   1793 		    USBDEVNAME(sc->atu_dev)));
   1794 		return;
   1795 	}
   1796 
   1797 	for (;;) {
   1798 		/* grab a TX buffer */
   1799 		s = splnet();
   1800 		c = SLIST_FIRST(&cd->atu_tx_free);
   1801 		if (c != NULL) {
   1802 			SLIST_REMOVE_HEAD(&cd->atu_tx_free, atu_list);
   1803 			cd->atu_tx_inuse++;
   1804 			if (cd->atu_tx_inuse == ATU_TX_LIST_CNT)
   1805 				ifp->if_flags |= IFF_OACTIVE;
   1806 		}
   1807 		splx(s);
   1808 		if (c == NULL) {
   1809 			DPRINTFN(10, ("%s: out of tx xfers\n",
   1810 			    USBDEVNAME(sc->atu_dev)));
   1811 			ifp->if_flags |= IFF_OACTIVE;
   1812 			break;
   1813 		}
   1814 
   1815 		/*
   1816 		 * Poll the management queue for frames, it has priority over
   1817 		 * normal data frames.
   1818 		 */
   1819 		IF_DEQUEUE(&ic->ic_mgtq, m);
   1820 		if (m == NULL) {
   1821 			DPRINTFN(10, ("%s: atu_start: data packet\n",
   1822 			    USBDEVNAME(sc->atu_dev)));
   1823 			if (ic->ic_state != IEEE80211_S_RUN) {
   1824 				DPRINTFN(25, ("%s: no data till running\n",
   1825 				    USBDEVNAME(sc->atu_dev)));
   1826 				/* put the xfer back on the list */
   1827 				s = splnet();
   1828 				SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
   1829 				    atu_list);
   1830 				cd->atu_tx_inuse--;
   1831 				splx(s);
   1832 				break;
   1833 			}
   1834 
   1835 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1836 			if (m == NULL) {
   1837 				DPRINTFN(25, ("%s: nothing to send\n",
   1838 				    USBDEVNAME(sc->atu_dev)));
   1839 				s = splnet();
   1840 				SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
   1841 				    atu_list);
   1842 				cd->atu_tx_inuse--;
   1843 				splx(s);
   1844 				break;
   1845 			}
   1846 #if NBPFILTER > 0
   1847 			if (ifp->if_bpf)
   1848 				bpf_mtap(ifp->if_bpf, m);
   1849 #endif
   1850 			ni = ieee80211_find_txnode(ic,
   1851 			    mtod(m, struct ether_header *)->ether_dhost);
   1852 			if (ni == NULL) {
   1853 				m_freem(m);
   1854 				goto bad;
   1855 			}
   1856 			m = ieee80211_encap(ic, m, ni);
   1857 			if (m == NULL)
   1858 				goto bad;
   1859 		} else {
   1860 			DPRINTFN(25, ("%s: atu_start: mgmt packet\n",
   1861 			    USBDEVNAME(sc->atu_dev)));
   1862 
   1863 			/*
   1864 			 * Hack!  The referenced node pointer is in the
   1865 			 * rcvif field of the packet header.  This is
   1866 			 * placed there by ieee80211_mgmt_output because
   1867 			 * we need to hold the reference with the frame
   1868 			 * and there's no other way (other than packet
   1869 			 * tags which we consider too expensive to use)
   1870 			 * to pass it along.
   1871 			 */
   1872 			ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
   1873 			m->m_pkthdr.rcvif = NULL;
   1874 
   1875 			/* sc->sc_stats.ast_tx_mgmt++; */
   1876 		}
   1877 
   1878 #if NBPFILTER > 0
   1879 		if (ic->ic_rawbpf)
   1880 			bpf_mtap(ic->ic_rawbpf, m);
   1881 #endif
   1882 
   1883 		if (atu_tx_start(sc, ni, c, m)) {
   1884 bad:
   1885 			s = splnet();
   1886 			SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
   1887 			    atu_list);
   1888 			cd->atu_tx_inuse--;
   1889 			splx(s);
   1890 			/* ifp_if_oerrors++; */
   1891 			if (ni != NULL)
   1892 				ieee80211_free_node(ni);
   1893 			continue;
   1894 		}
   1895 		ifp->if_timer = 5;
   1896 	}
   1897 }
   1898 
   1899 int
   1900 atu_init(struct ifnet *ifp)
   1901 {
   1902 	struct atu_softc	*sc = ifp->if_softc;
   1903 	struct ieee80211com	*ic = &sc->sc_ic;
   1904 	struct atu_chain	*c;
   1905 	usbd_status		err;
   1906 	int			i, s;
   1907 
   1908 	s = splnet();
   1909 
   1910 	DPRINTFN(10, ("%s: atu_init\n", USBDEVNAME(sc->atu_dev)));
   1911 
   1912 	if (ifp->if_flags & IFF_RUNNING) {
   1913 		splx(s);
   1914 		return(0);
   1915 	}
   1916 
   1917 	/* Init TX ring */
   1918 	if (atu_tx_list_init(sc))
   1919 		printf("%s: tx list init failed\n", USBDEVNAME(sc->atu_dev));
   1920 
   1921 	/* Init RX ring */
   1922 	if (atu_rx_list_init(sc))
   1923 		printf("%s: rx list init failed\n", USBDEVNAME(sc->atu_dev));
   1924 
   1925 	/* Load the multicast filter. */
   1926 	/*atu_setmulti(sc); */
   1927 
   1928 	/* Open RX and TX pipes. */
   1929 	err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_RX],
   1930 	    USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_RX]);
   1931 	if (err) {
   1932 		DPRINTF(("%s: open rx pipe failed: %s\n",
   1933 		    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
   1934 		splx(s);
   1935 		return(EIO);
   1936 	}
   1937 
   1938 	err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_TX],
   1939 	    USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_TX]);
   1940 	if (err) {
   1941 		DPRINTF(("%s: open tx pipe failed: %s\n",
   1942 		    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
   1943 		splx(s);
   1944 		return(EIO);
   1945 	}
   1946 
   1947 	/* Start up the receive pipe. */
   1948 	for (i = 0; i < ATU_RX_LIST_CNT; i++) {
   1949 		c = &sc->atu_cdata.atu_rx_chain[i];
   1950 
   1951 		usbd_setup_xfer(c->atu_xfer, sc->atu_ep[ATU_ENDPT_RX], c,
   1952 		    c->atu_buf, ATU_RX_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1953 		    USBD_NO_TIMEOUT, atu_rxeof);
   1954 		usbd_transfer(c->atu_xfer);
   1955 	}
   1956 
   1957 	DPRINTFN(10, ("%s: starting up using MAC=%s\n",
   1958 	    USBDEVNAME(sc->atu_dev), ether_sprintf(ic->ic_myaddr)));
   1959 
   1960 	/* Do initial setup */
   1961 	err = atu_initial_config(sc);
   1962 	if (err) {
   1963 		DPRINTF(("%s: initial config failed!\n",
   1964 		    USBDEVNAME(sc->atu_dev)));
   1965 		splx(s);
   1966 		return(EIO);
   1967 	}
   1968 	DPRINTFN(10, ("%s: initialised transceiver\n",
   1969 	    USBDEVNAME(sc->atu_dev)));
   1970 
   1971 	/* sc->atu_rxfilt = ATU_RXFILT_UNICAST|ATU_RXFILT_BROADCAST; */
   1972 
   1973 	/* If we want promiscuous mode, set the allframes bit. */
   1974 	/*
   1975 	if (ifp->if_flags & IFF_PROMISC)
   1976 		sc->atu_rxfilt |= ATU_RXFILT_PROMISC;
   1977 	*/
   1978 
   1979 	ifp->if_flags |= IFF_RUNNING;
   1980 	ifp->if_flags &= ~IFF_OACTIVE;
   1981 	splx(s);
   1982 
   1983 	/* XXX the following HAS to be replaced */
   1984 	s = splnet();
   1985 	err = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   1986 	if (err)
   1987 		DPRINTFN(1, ("%s: atu_init: error calling "
   1988 		    "ieee80211_net_state", USBDEVNAME(sc->atu_dev)));
   1989 	splx(s);
   1990 
   1991 	return 0;
   1992 }
   1993 
   1994 #ifdef ATU_DEBUG
   1995 void
   1996 atu_debug_print(struct atu_softc *sc)
   1997 {
   1998 	usbd_status		err;
   1999 	u_int8_t		tmp[32];
   2000 
   2001 	/* DEBUG */
   2002 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, tmp)))
   2003 		return;
   2004 	DPRINTF(("%s: DEBUG: current BSSID=%s\n", USBDEVNAME(sc->atu_dev),
   2005 	    ether_sprintf(tmp)));
   2006 
   2007 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__BEACON_PERIOD, tmp)))
   2008 		return;
   2009 	DPRINTF(("%s: DEBUG: beacon period=%d\n", USBDEVNAME(sc->atu_dev),
   2010 	    tmp[0]));
   2011 
   2012 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__PRIVACY_INVOKED, tmp)))
   2013 		return;
   2014 	DPRINTF(("%s: DEBUG: privacy invoked=%d\n", USBDEVNAME(sc->atu_dev),
   2015 	    tmp[0]));
   2016 
   2017 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__ENCR_LEVEL, tmp)))
   2018 		return;
   2019 	DPRINTF(("%s: DEBUG: encr_level=%d\n", USBDEVNAME(sc->atu_dev),
   2020 	    tmp[0]));
   2021 
   2022 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__ICV_ERROR_COUNT, tmp)))
   2023 		return;
   2024 	DPRINTF(("%s: DEBUG: icv error count=%d\n", USBDEVNAME(sc->atu_dev),
   2025 	    *(short *)tmp));
   2026 
   2027 	if ((err = atu_get_mib(sc, MIB_MAC_WEP__EXCLUDED_COUNT, tmp)))
   2028 		return;
   2029 	DPRINTF(("%s: DEBUG: wep excluded count=%d\n",
   2030 	    USBDEVNAME(sc->atu_dev), *(short *)tmp));
   2031 
   2032 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__POWER_MODE, tmp)))
   2033 		return;
   2034 	DPRINTF(("%s: DEBUG: power mode=%d\n", USBDEVNAME(sc->atu_dev),
   2035 	    tmp[0]));
   2036 
   2037 	if ((err = atu_get_mib(sc, MIB_PHY__CHANNEL, tmp)))
   2038 		return;
   2039 	DPRINTF(("%s: DEBUG: channel=%d\n", USBDEVNAME(sc->atu_dev), tmp[0]));
   2040 
   2041 	if ((err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, tmp)))
   2042 		return;
   2043 	DPRINTF(("%s: DEBUG: reg domain=%d\n", USBDEVNAME(sc->atu_dev),
   2044 	    tmp[0]));
   2045 
   2046 	if ((err = atu_get_mib(sc, MIB_LOCAL__SSID_SIZE, tmp)))
   2047 		return;
   2048 	DPRINTF(("%s: DEBUG: ssid size=%d\n", USBDEVNAME(sc->atu_dev),
   2049 	    tmp[0]));
   2050 
   2051 	if ((err = atu_get_mib(sc, MIB_LOCAL__BEACON_ENABLE, tmp)))
   2052 		return;
   2053 	DPRINTF(("%s: DEBUG: beacon enable=%d\n", USBDEVNAME(sc->atu_dev),
   2054 	    tmp[0]));
   2055 
   2056 	if ((err = atu_get_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, tmp)))
   2057 		return;
   2058 	DPRINTF(("%s: DEBUG: auto rate fallback=%d\n",
   2059 	    USBDEVNAME(sc->atu_dev), tmp[0]));
   2060 
   2061 	if ((err = atu_get_mib(sc, MIB_MAC_ADDR__ADDR, tmp)))
   2062 		return;
   2063 	DPRINTF(("%s: DEBUG: mac addr=%s\n", USBDEVNAME(sc->atu_dev),
   2064 	    ether_sprintf(tmp)));
   2065 
   2066 	if ((err = atu_get_mib(sc, MIB_MAC__DESIRED_SSID, tmp)))
   2067 		return;
   2068 	DPRINTF(("%s: DEBUG: desired ssid=%s\n", USBDEVNAME(sc->atu_dev),
   2069 	    tmp));
   2070 
   2071 	if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_ESSID, tmp)))
   2072 		return;
   2073 	DPRINTF(("%s: DEBUG: current ESSID=%s\n", USBDEVNAME(sc->atu_dev),
   2074 	    tmp));
   2075 }
   2076 #endif /* ATU_DEBUG */
   2077 
   2078 int
   2079 atu_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   2080 {
   2081 	struct atu_softc	*sc = ifp->if_softc;
   2082 	struct ifreq		*ifr = (struct ifreq *)data;
   2083 	struct ieee80211com	*ic = &sc->sc_ic;
   2084 	int			err = 0, s;
   2085 
   2086 	s = splnet();
   2087 	switch (command) {
   2088 	case SIOCSIFMEDIA:
   2089 	case SIOCGIFMEDIA:
   2090 		err = ifmedia_ioctl(ifp, ifr, &ic->ic_media, command);
   2091 		break;
   2092 
   2093 	default:
   2094 		DPRINTFN(15, ("%s: ieee80211_ioctl (%lu)\n",
   2095 		    USBDEVNAME(sc->atu_dev), command));
   2096 		err = ieee80211_ioctl(ic, command, data);
   2097 		break;
   2098 	}
   2099 
   2100 	if (err == ENETRESET) {
   2101 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   2102 		    (IFF_RUNNING|IFF_UP)) {
   2103 			DPRINTF(("%s: atu_ioctl(): netreset %lu\n",
   2104 			    USBDEVNAME(sc->atu_dev), command));
   2105 			ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2106 			atu_initial_config(sc);
   2107 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2108 		}
   2109 		err = 0;
   2110 	}
   2111 
   2112 	splx(s);
   2113 	return (err);
   2114 }
   2115 
   2116 void
   2117 atu_watchdog(struct ifnet *ifp)
   2118 {
   2119 	struct atu_softc	*sc = ifp->if_softc;
   2120 	struct atu_chain	*c;
   2121 	usbd_status		stat;
   2122 	int			cnt, s;
   2123 
   2124 	DPRINTF(("%s: atu_watchdog\n", USBDEVNAME(sc->atu_dev)));
   2125 
   2126 	ifp->if_timer = 0;
   2127 
   2128 	if (sc->sc_state != ATU_S_OK || (ifp->if_flags & IFF_RUNNING) == 0)
   2129 		return;
   2130 
   2131 	sc = ifp->if_softc;
   2132 	s = splnet();
   2133 	ifp->if_oerrors++;
   2134 	DPRINTF(("%s: watchdog timeout\n", USBDEVNAME(sc->atu_dev)));
   2135 
   2136 	/*
   2137 	 * TODO:
   2138 	 * we should change this since we have multiple TX tranfers...
   2139 	 */
   2140 	for (cnt = 0; cnt < ATU_TX_LIST_CNT; cnt++) {
   2141 		c = &sc->atu_cdata.atu_tx_chain[cnt];
   2142 		if (c->atu_in_xfer) {
   2143 			usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL,
   2144 			    &stat);
   2145 			atu_txeof(c->atu_xfer, c, stat);
   2146 		}
   2147 	}
   2148 
   2149 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   2150 		atu_start(ifp);
   2151 	splx(s);
   2152 
   2153 	ieee80211_watchdog(&sc->sc_ic);
   2154 }
   2155 
   2156 /*
   2157  * Stop the adapter and free any mbufs allocated to the
   2158  * RX and TX lists.
   2159  */
   2160 void
   2161 atu_stop(struct ifnet *ifp, int disable)
   2162 {
   2163 	struct atu_softc	*sc = ifp->if_softc;
   2164 	struct ieee80211com	*ic = &sc->sc_ic;
   2165 	struct atu_cdata	*cd;
   2166 	usbd_status		err;
   2167 	int s;
   2168 
   2169 	s = splnet();
   2170 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2171 	ifp->if_timer = 0;
   2172 
   2173 	usb_rem_task(sc->atu_udev, &sc->sc_task);
   2174 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2175 
   2176 	/* Stop transfers. */
   2177 	if (sc->atu_ep[ATU_ENDPT_RX] != NULL) {
   2178 		err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]);
   2179 		if (err) {
   2180 			DPRINTF(("%s: abort rx pipe failed: %s\n",
   2181 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
   2182 		}
   2183 		err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_RX]);
   2184 		if (err) {
   2185 			DPRINTF(("%s: close rx pipe failed: %s\n",
   2186 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
   2187 		}
   2188 		sc->atu_ep[ATU_ENDPT_RX] = NULL;
   2189 	}
   2190 
   2191 	if (sc->atu_ep[ATU_ENDPT_TX] != NULL) {
   2192 		err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]);
   2193 		if (err) {
   2194 			DPRINTF(("%s: abort tx pipe failed: %s\n",
   2195 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
   2196 		}
   2197 		err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_TX]);
   2198 		if (err) {
   2199 			DPRINTF(("%s: close tx pipe failed: %s\n",
   2200 			    USBDEVNAME(sc->atu_dev), usbd_errstr(err)));
   2201 		}
   2202 		sc->atu_ep[ATU_ENDPT_TX] = NULL;
   2203 	}
   2204 
   2205 	/* Free RX/TX/MGMT list resources. */
   2206 	cd = &sc->atu_cdata;
   2207 	atu_xfer_list_free(sc, cd->atu_rx_chain, ATU_RX_LIST_CNT);
   2208 	atu_xfer_list_free(sc, cd->atu_tx_chain, ATU_TX_LIST_CNT);
   2209 
   2210 	/* Let's be nice and turn off the radio before we leave */
   2211 	atu_switch_radio(sc, 0);
   2212 
   2213 	splx(s);
   2214 }
   2215