Home | History | Annotate | Line # | Download | only in usb
if_atu.c revision 1.13
      1 /*	$NetBSD: if_atu.c,v 1.13 2005/11/28 13:31:09 augustss 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.13 2005/11/28 13:31:09 augustss 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 	int				err;
    405 	struct atu_cmd_start_ibss	Request;
    406 
    407 	Request.Cmd = CMD_START_IBSS;
    408 	Request.Reserved = 0;
    409 	Request.Size = sizeof(Request) - 4;
    410 
    411 	memset(Request.BSSID, 0x00, sizeof(Request.BSSID));
    412 	memset(Request.SSID, 0x00, sizeof(Request.SSID));
    413 	memcpy(Request.SSID, sc->atu_ssid, sc->atu_ssidlen);
    414 	Request.SSIDSize = sc->atu_ssidlen;
    415 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
    416 		Request.Channel = (u_int8_t)sc->atu_desired_channel;
    417 	else
    418 		Request.Channel = ATU_DEFAULT_CHANNEL;
    419 	Request.BSSType = AD_HOC_MODE;
    420 	memset(Request.Res, 0x00, sizeof(Request.Res));
    421 
    422 	/* Write config to adapter */
    423 	err = atu_send_command(sc, (u_int8_t *)&Request, sizeof(Request));
    424 	if (err) {
    425 		DPRINTF(("%s: start ibss failed!\n",
    426 		    USBDEVNAME(sc->atu_dev)));
    427 		return err;
    428 	}
    429 
    430 	/* Wait for the adapter to do it's thing */
    431 	err = atu_wait_completion(sc, CMD_START_IBSS, NULL);
    432 	if (err) {
    433 		DPRINTF(("%s: error waiting for start_ibss\n",
    434 		    USBDEVNAME(sc->atu_dev)));
    435 		return err;
    436 	}
    437 
    438 	/* Get the current BSSID */
    439 	err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, sc->atu_bssid);
    440 	if (err) {
    441 		DPRINTF(("%s: could not get BSSID!\n",
    442 		    USBDEVNAME(sc->atu_dev)));
    443 		return err;
    444 	}
    445 
    446 	DPRINTF(("%s: started a new IBSS (BSSID=%s)\n",
    447 	    USBDEVNAME(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
    448 	return 0;
    449 }
    450 #endif
    451 
    452 int
    453 atu_start_scan(struct atu_softc *sc)
    454 {
    455 	struct atu_cmd_do_scan		Scan;
    456 	usbd_status			err;
    457 	int				Cnt;
    458 
    459 	memset(&Scan, 0, sizeof(Scan));
    460 
    461 	Scan.Cmd = CMD_START_SCAN;
    462 	Scan.Reserved = 0;
    463 	USETW(Scan.Size, sizeof(Scan) - 4);
    464 
    465 	/* use the broadcast BSSID (in active scan) */
    466 	for (Cnt=0; Cnt<6; Cnt++)
    467 		Scan.BSSID[Cnt] = 0xff;
    468 
    469 	memset(Scan.SSID, 0x00, sizeof(Scan.SSID));
    470 	memcpy(Scan.SSID, sc->atu_ssid, sc->atu_ssidlen);
    471 	Scan.SSID_Len = sc->atu_ssidlen;
    472 
    473 	/* default values for scan */
    474 	Scan.ScanType = ATU_SCAN_ACTIVE;
    475 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
    476 		Scan.Channel = (u_int8_t)sc->atu_desired_channel;
    477 	else
    478 		Scan.Channel = sc->atu_channel;
    479 
    480 	/* we like scans to be quick :) */
    481 	/* the time we wait before sending probe's */
    482 	USETW(Scan.ProbeDelay, 0);
    483 	/* the time we stay on one channel */
    484 	USETW(Scan.MinChannelTime, 100);
    485 	USETW(Scan.MaxChannelTime, 200);
    486 	/* wether or not we scan all channels */
    487 	Scan.InternationalScan = 0xc1;
    488 
    489 #ifdef ATU_DEBUG
    490 	if (atudebug) {
    491 		DPRINTFN(20, ("%s: scan cmd len=%02lx\n",
    492 		    USBDEVNAME(sc->atu_dev), (unsigned long)sizeof(Scan)));
    493 	}
    494 #endif /* ATU_DEBUG */
    495 
    496 	/* Write config to adapter */
    497 	err = atu_send_command(sc, (u_int8_t *)&Scan, sizeof(Scan));
    498 	if (err)
    499 		return err;
    500 
    501 	/*
    502 	 * We don't wait for the command to finish... the mgmt-thread will do
    503 	 * that for us
    504 	 */
    505 	/*
    506 	err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
    507 	if (err)
    508 		return err;
    509 	*/
    510 	return 0;
    511 }
    512 
    513 int
    514 atu_switch_radio(struct atu_softc *sc, int state)
    515 {
    516 	usbd_status		err;
    517 	struct atu_cmd		CmdRadio;
    518 
    519 	if (sc->atu_radio == RadioIntersil) {
    520 		/*
    521 		 * Intersil doesn't seem to need/support switching the radio
    522 		 * on/off
    523 		 */
    524 		return 0;
    525 	}
    526 
    527 	memset(&CmdRadio, 0, sizeof(CmdRadio));
    528 	CmdRadio.Cmd = CMD_RADIO_ON;
    529 
    530 	if (sc->atu_radio_on != state) {
    531 		if (state == 0)
    532 			CmdRadio.Cmd = CMD_RADIO_OFF;
    533 
    534 		err = atu_send_command(sc, (u_int8_t *)&CmdRadio,
    535 		    sizeof(CmdRadio));
    536 		if (err)
    537 			return err;
    538 
    539 		err = atu_wait_completion(sc, CmdRadio.Cmd, NULL);
    540 		if (err)
    541 			return err;
    542 
    543 		DPRINTFN(10, ("%s: radio turned %s\n",
    544 		    USBDEVNAME(sc->atu_dev), state ? "on" : "off"));
    545 		sc->atu_radio_on = state;
    546 	}
    547 	return 0;
    548 }
    549 
    550 int
    551 atu_initial_config(struct atu_softc *sc)
    552 {
    553 	struct ieee80211com		*ic = &sc->sc_ic;
    554 	u_int32_t			i;
    555 	usbd_status			err;
    556 /*	u_int8_t			rates[4] = {0x82, 0x84, 0x8B, 0x96};*/
    557 	u_int8_t			rates[4] = {0x82, 0x04, 0x0B, 0x16};
    558 	struct atu_cmd_card_config	cmd;
    559 	u_int8_t			reg_domain;
    560 
    561 	DPRINTFN(10, ("%s: sending mac-addr\n", USBDEVNAME(sc->atu_dev)));
    562 	err = atu_send_mib(sc, MIB_MAC_ADDR__ADDR, ic->ic_myaddr);
    563 	if (err) {
    564 		DPRINTF(("%s: error setting mac-addr\n",
    565 		    USBDEVNAME(sc->atu_dev)));
    566 		return err;
    567 	}
    568 
    569 	/*
    570 	DPRINTF(("%s: sending reg-domain\n", USBDEVNAME(sc->atu_dev)));
    571 	err = atu_send_mib(sc, MIB_PHY__REG_DOMAIN, NR(0x30));
    572 	if (err) {
    573 		DPRINTF(("%s: error setting mac-addr\n",
    574 		    USBDEVNAME(sc->atu_dev)));
    575 		return err;
    576 	}
    577 	*/
    578 
    579 	memset(&cmd, 0, sizeof(cmd));
    580 	cmd.Cmd = CMD_STARTUP;
    581 	cmd.Reserved = 0;
    582 	USETW(cmd.Size, sizeof(cmd) - 4);
    583 
    584 	if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
    585 		cmd.Channel = (u_int8_t)sc->atu_desired_channel;
    586 	else
    587 		cmd.Channel = sc->atu_channel;
    588 	cmd.AutoRateFallback = 1;
    589 	memcpy(cmd.BasicRateSet, rates, 4);
    590 
    591 	/* ShortRetryLimit should be 7 according to 802.11 spec */
    592 	cmd.ShortRetryLimit = 7;
    593 	USETW(cmd.RTS_Threshold, 2347);
    594 	USETW(cmd.FragThreshold, 2346);
    595 
    596 	/* Doesn't seem to work, but we'll set it to 1 anyway */
    597 	cmd.PromiscuousMode = 1;
    598 
    599 	/* this goes into the beacon we transmit */
    600 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
    601 		cmd.PrivacyInvoked = 1;
    602 	else
    603 		cmd.PrivacyInvoked = 0;
    604 
    605 	cmd.ExcludeUnencrypted = 0;
    606 
    607 	switch (ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen) {
    608 	case 5:
    609 		cmd.EncryptionType = ATU_WEP_40BITS;
    610 		break;
    611 	case 13:
    612 		cmd.EncryptionType = ATU_WEP_104BITS;
    613 		break;
    614 	default:
    615 		cmd.EncryptionType = ATU_WEP_OFF;
    616 		break;
    617 	}
    618 
    619 
    620 	cmd.WEP_DefaultKeyID = ic->ic_def_txkey;
    621 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    622 		memcpy(cmd.WEP_DefaultKey[i], ic->ic_nw_keys[i].wk_key,
    623 		    ic->ic_nw_keys[i].wk_keylen);
    624 	}
    625 
    626 	/* Setting the SSID here doesn't seem to do anything */
    627 	memset(cmd.SSID, 0, sizeof(cmd.SSID));
    628 	memcpy(cmd.SSID, sc->atu_ssid, sc->atu_ssidlen);
    629 	cmd.SSID_Len = sc->atu_ssidlen;
    630 
    631 	cmd.ShortPreamble = 0;
    632 	USETW(cmd.BeaconPeriod, 100);
    633 	/* cmd.BeaconPeriod = 65535; */
    634 
    635 	/*
    636 	 * TODO:
    637 	 * read reg domain MIB_PHY @ 0x17 (1 byte), (reply = 0x30)
    638 	 * we should do something usefull with this info. right now it's just
    639 	 * ignored
    640 	 */
    641 	err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, &reg_domain);
    642 	if (err) {
    643 		DPRINTF(("%s: could not get regdomain!\n",
    644 		    USBDEVNAME(sc->atu_dev)));
    645 	} else {
    646 		DPRINTF(("%s: in reg domain 0x%x according to the "
    647 		    "adapter\n", USBDEVNAME(sc->atu_dev), reg_domain));
    648 	}
    649 
    650 #ifdef ATU_DEBUG
    651 	if (atudebug) {
    652 		DPRINTFN(20, ("%s: configlen=%02lx\n", USBDEVNAME(sc->atu_dev),
    653 		    (unsigned long)sizeof(cmd)));
    654 	}
    655 #endif /* ATU_DEBUG */
    656 
    657 	/* Windoze : driver says exclude-unencrypted=1 & encr-type=1 */
    658 
    659 	err = atu_send_command(sc, (u_int8_t *)&cmd, sizeof(cmd));
    660 	if (err)
    661 		return err;
    662 	err = atu_wait_completion(sc, CMD_STARTUP, NULL);
    663 	if (err)
    664 		return err;
    665 
    666 	/* Turn on radio now */
    667 	err = atu_switch_radio(sc, 1);
    668 	if (err)
    669 		return err;
    670 
    671 	/* preamble type = short */
    672 	err = atu_send_mib(sc, MIB_LOCAL__PREAMBLE, NR(PREAMBLE_SHORT));
    673 	if (err)
    674 		return err;
    675 
    676 	/* frag = 1536 */
    677 	err = atu_send_mib(sc, MIB_MAC__FRAG, NR(2346));
    678 	if (err)
    679 		return err;
    680 
    681 	/* rts = 1536 */
    682 	err = atu_send_mib(sc, MIB_MAC__RTS, NR(2347));
    683 	if (err)
    684 		return err;
    685 
    686 	/* auto rate fallback = 1 */
    687 	err = atu_send_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, NR(1));
    688 	if (err)
    689 		return err;
    690 
    691 	/* power mode = full on, no power saving */
    692 	err = atu_send_mib(sc, MIB_MAC_MGMT__POWER_MODE,
    693 	    NR(POWER_MODE_ACTIVE));
    694 	if (err)
    695 		return err;
    696 
    697 	DPRINTFN(10, ("%s: completed initial config\n",
    698 	   USBDEVNAME(sc->atu_dev)));
    699 	return 0;
    700 }
    701 
    702 int
    703 atu_join(struct atu_softc *sc, struct ieee80211_node *node)
    704 {
    705 	struct atu_cmd_join		join;
    706 	u_int8_t			status;
    707 	usbd_status			err;
    708 
    709 	memset(&join, 0, sizeof(join));
    710 
    711 	join.Cmd = CMD_JOIN;
    712 	join.Reserved = 0x00;
    713 	USETW(join.Size, sizeof(join) - 4);
    714 
    715 	DPRINTFN(15, ("%s: pre-join sc->atu_bssid=%s\n",
    716 	    USBDEVNAME(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
    717 	DPRINTFN(15, ("%s: mode=%d\n", USBDEVNAME(sc->atu_dev),
    718 	    sc->atu_mode));
    719 	memcpy(join.bssid, node->ni_bssid, IEEE80211_ADDR_LEN);
    720 	memset(join.essid, 0x00, 32);
    721 	memcpy(join.essid, node->ni_essid, node->ni_esslen);
    722 	join.essid_size = node->ni_esslen;
    723 	if (node->ni_capinfo & IEEE80211_CAPINFO_IBSS)
    724 		join.bss_type = AD_HOC_MODE;
    725 	else
    726 		join.bss_type = INFRASTRUCTURE_MODE;
    727 	join.channel = ieee80211_chan2ieee(&sc->sc_ic, node->ni_chan);
    728 
    729 	USETW(join.timeout, ATU_JOIN_TIMEOUT);
    730 	join.reserved = 0x00;
    731 
    732 	DPRINTFN(10, ("%s: trying to join BSSID=%s\n",
    733 	    USBDEVNAME(sc->atu_dev), ether_sprintf(join.bssid)));
    734 	err = atu_send_command(sc, (u_int8_t *)&join, sizeof(join));
    735 	if (err) {
    736 		DPRINTF(("%s: ERROR trying to join IBSS\n",
    737 		    USBDEVNAME(sc->atu_dev)));
    738 		return err;
    739 	}
    740 	err = atu_wait_completion(sc, CMD_JOIN, &status);
    741 	if (err) {
    742 		DPRINTF(("%s: error joining BSS!\n",
    743 		    USBDEVNAME(sc->atu_dev)));
    744 		return err;
    745 	}
    746 	if (status != STATUS_COMPLETE) {
    747 		DPRINTF(("%s: error joining... [status=%02x]\n",
    748 		    USBDEVNAME(sc->atu_dev), status));
    749 		return status;
    750 	} else {
    751 		DPRINTFN(10, ("%s: joined BSS\n", USBDEVNAME(sc->atu_dev)));
    752 	}
    753 	return err;
    754 }
    755 
    756 /*
    757  * Get the state of the DFU unit
    758  */
    759 int8_t
    760 atu_get_dfu_state(struct atu_softc *sc)
    761 {
    762 	u_int8_t	state;
    763 
    764 	if (atu_usb_request(sc, DFU_GETSTATE, 0, 0, 1, &state))
    765 		return -1;
    766 	return state;
    767 }
    768 
    769 /*
    770  * Get MAC opmode
    771  */
    772 u_int8_t
    773 atu_get_opmode(struct atu_softc *sc, u_int8_t *mode)
    774 {
    775 
    776 	return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 0x0001,
    777 	    0x0000, 1, mode);
    778 }
    779 
    780 /*
    781  * Upload the internal firmware into the device
    782  */
    783 void
    784 atu_internal_firmware(struct device *arg)
    785 {
    786 	struct atu_softc *sc = (struct atu_softc *)arg;
    787 	u_char	state, *ptr = NULL, *firm = NULL, status[6];
    788 	int block_size, block = 0, err, i;
    789 	size_t	bytes_left = 0;
    790 
    791 	/*
    792 	 * Uploading firmware is done with the DFU (Device Firmware Upgrade)
    793 	 * interface. See "Universal Serial Bus - Device Class Specification
    794 	 * for Device Firmware Upgrade" pdf for details of the protocol.
    795 	 * Maybe this could be moved to a seperate 'firmware driver' once more
    796 	 * device drivers need it... For now we'll just do it here.
    797 	 *
    798 	 * Just for your information, the Atmel's DFU descriptor looks like
    799 	 * this:
    800 	 *
    801 	 * 07		size
    802 	 * 21		type
    803 	 * 01		capabilities : only firmware download, need reset
    804 	 *		  after download
    805 	 * 13 05	detach timeout : max 1299ms between DFU_DETACH and
    806 	 *		  reset
    807 	 * 00 04	max bytes of firmware per transaction : 1024
    808 	 */
    809 
    810 	/* Choose the right firmware for the device */
    811 	for (i = 0; i < sizeof(atu_radfirm)/sizeof(atu_radfirm[0]); i++)
    812 		if (sc->atu_radio == atu_radfirm[i].atur_type) {
    813 			firm = atu_radfirm[i].atur_internal;
    814 			bytes_left = atu_radfirm[i].atur_internal_sz;
    815 		}
    816 
    817 	if (firm == NULL) {
    818 		printf("%s: no firmware found\n", USBDEVNAME(sc->atu_dev));
    819 		return;
    820 	}
    821 
    822 	ptr = firm;
    823 	state = atu_get_dfu_state(sc);
    824 
    825 	while (block >= 0 && state > 0) {
    826 		switch (state) {
    827 		case DFUState_DnLoadSync:
    828 			/* get DFU status */
    829 			err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0 , 6,
    830 			    status);
    831 			if (err) {
    832 				DPRINTF(("%s: dfu_getstatus failed!\n",
    833 				    USBDEVNAME(sc->atu_dev)));
    834 				return;
    835 			}
    836 			/* success means state => DnLoadIdle */
    837 			state = DFUState_DnLoadIdle;
    838 			continue;
    839 			break;
    840 
    841 		case DFUState_DFUIdle:
    842 		case DFUState_DnLoadIdle:
    843 			if (bytes_left>=DFU_MaxBlockSize)
    844 				block_size = DFU_MaxBlockSize;
    845 			else
    846 				block_size = bytes_left;
    847 			DPRINTFN(15, ("%s: firmware block %d\n",
    848 			    USBDEVNAME(sc->atu_dev), block));
    849 
    850 			err = atu_usb_request(sc, DFU_DNLOAD, block++, 0,
    851 			    block_size, ptr);
    852 			if (err) {
    853 				DPRINTF(("%s: dfu_dnload failed\n",
    854 				    USBDEVNAME(sc->atu_dev)));
    855 				return;
    856 			}
    857 
    858 			ptr += block_size;
    859 			bytes_left -= block_size;
    860 			if (block_size == 0)
    861 				block = -1;
    862 			break;
    863 
    864 		default:
    865 			usbd_delay_ms(sc->atu_udev, 100);
    866 			DPRINTFN(20, ("%s: sleeping for a while\n",
    867 			    USBDEVNAME(sc->atu_dev)));
    868 			break;
    869 		}
    870 
    871 		state = atu_get_dfu_state(sc);
    872 	}
    873 
    874 	if (state != DFUState_ManifestSync) {
    875 		DPRINTF(("%s: state != manifestsync... eek!\n",
    876 		    USBDEVNAME(sc->atu_dev)));
    877 	}
    878 
    879 	err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0, 6, status);
    880 	if (err) {
    881 		DPRINTF(("%s: dfu_getstatus failed!\n",
    882 		    USBDEVNAME(sc->atu_dev)));
    883 		return;
    884 	}
    885 
    886 	DPRINTFN(15, ("%s: sending remap\n", USBDEVNAME(sc->atu_dev)));
    887 	err = atu_usb_request(sc, DFU_REMAP, 0, 0, 0, NULL);
    888 	if ((err) && (! sc->atu_quirk & ATU_QUIRK_NO_REMAP)) {
    889 		DPRINTF(("%s: remap failed!\n", USBDEVNAME(sc->atu_dev)));
    890 		return;
    891 	}
    892 
    893 	/* after a lot of trying and measuring I found out the device needs
    894 	 * about 56 miliseconds after sending the remap command before
    895 	 * it's ready to communicate again. So we'll wait just a little bit
    896 	 * longer than that to be sure...
    897 	 */
    898 	usbd_delay_ms(sc->atu_udev, 56+100);
    899 
    900 	printf("%s: reattaching after firmware upload\n",
    901 	    USBDEVNAME(sc->atu_dev));
    902 	usb_needs_reattach(sc->atu_udev);
    903 }
    904 
    905 void
    906 atu_external_firmware(struct device *arg)
    907 {
    908 	struct atu_softc *sc = (struct atu_softc *)arg;
    909 	u_char	*ptr = NULL, *firm = NULL;
    910 	int	block_size, block = 0, err, i;
    911 	size_t	bytes_left = 0;
    912 
    913 	for (i = 0; i < sizeof(atu_radfirm)/sizeof(atu_radfirm[0]); i++)
    914 		if (sc->atu_radio == atu_radfirm[i].atur_type) {
    915 			firm = atu_radfirm[i].atur_external;
    916 			bytes_left = atu_radfirm[i].atur_external_sz;
    917 		}
    918 
    919 	if (firm == NULL) {
    920 		printf("%s: no firmware found\n", USBDEVNAME(sc->atu_dev));
    921 		return;
    922 	}
    923 	ptr = firm;
    924 
    925 	while (bytes_left) {
    926 		if (bytes_left > 1024)
    927 			block_size = 1024;
    928 		else
    929 			block_size = bytes_left;
    930 
    931 		DPRINTFN(15, ("%s: block:%d size:%d\n",
    932 		    USBDEVNAME(sc->atu_dev), block, block_size));
    933 		err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e,
    934 		    0x0802, block, block_size, ptr);
    935 		if (err) {
    936 			DPRINTF(("%s: could not load external firmware "
    937 			    "block\n", USBDEVNAME(sc->atu_dev)));
    938 			return;
    939 		}
    940 
    941 		ptr += block_size;
    942 		block++;
    943 		bytes_left -= block_size;
    944 	}
    945 
    946 	err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0802,
    947 	    block, 0, NULL);
    948 	if (err) {
    949 		DPRINTF(("%s: could not load last zero-length firmware "
    950 		    "block\n", USBDEVNAME(sc->atu_dev)));
    951 		return;
    952 	}
    953 
    954 	/*
    955 	 * The SMC2662w V.4 seems to require some time to do it's thing with
    956 	 * the external firmware... 20 ms isn't enough, but 21 ms works 100
    957 	 * times out of 100 tries. We'll wait a bit longer just to be sure
    958 	 */
    959 	if (sc->atu_quirk & ATU_QUIRK_FW_DELAY)
    960 		usbd_delay_ms(sc->atu_udev, 21 + 100);
    961 
    962 	DPRINTFN(10, ("%s: external firmware upload done\n",
    963 	    USBDEVNAME(sc->atu_dev)));
    964 	/* complete configuration after the firmwares have been uploaded */
    965 	atu_complete_attach(sc);
    966 }
    967 
    968 int
    969 atu_get_card_config(struct atu_softc *sc)
    970 {
    971 	struct ieee80211com		*ic = &sc->sc_ic;
    972 	struct atu_rfmd_conf		rfmd_conf;
    973 	struct atu_intersil_conf	intersil_conf;
    974 	int				err;
    975 
    976 	switch (sc->atu_radio) {
    977 
    978 	case RadioRFMD:
    979 	case RadioRFMD2958:
    980 	case RadioRFMD2958_SMC:
    981 		err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
    982 		    0x0a02, 0x0000, sizeof(rfmd_conf),
    983 		    (u_int8_t *)&rfmd_conf);
    984 		if (err) {
    985 			DPRINTF(("%s: could not get rfmd config!\n",
    986 			    USBDEVNAME(sc->atu_dev)));
    987 			return err;
    988 		}
    989 		memcpy(ic->ic_myaddr, rfmd_conf.MACAddr, IEEE80211_ADDR_LEN);
    990 		break;
    991 
    992 	case RadioIntersil:
    993 		err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
    994 		    0x0902, 0x0000, sizeof(intersil_conf),
    995 		    (u_int8_t *)&intersil_conf);
    996 		if (err) {
    997 			DPRINTF(("%s: could not get intersil config!\n",
    998 			    USBDEVNAME(sc->atu_dev)));
    999 			return err;
   1000 		}
   1001 		memcpy(ic->ic_myaddr, intersil_conf.MACAddr,
   1002 		    IEEE80211_ADDR_LEN);
   1003 		break;
   1004 	}
   1005 	return 0;
   1006 }
   1007 
   1008 /*
   1009  * Probe for an AT76c503 chip.
   1010  */
   1011 USB_MATCH(atu)
   1012 {
   1013 	USB_MATCH_START(atu, uaa);
   1014 	int			i;
   1015 
   1016 	if (!uaa->iface)
   1017 		return(UMATCH_NONE);
   1018 
   1019 	for (i = 0; i < sizeof(atu_devs)/sizeof(atu_devs[0]); i++) {
   1020 		struct atu_type *t = &atu_devs[i];
   1021 
   1022 		if (uaa->vendor == t->atu_vid &&
   1023 		    uaa->product == t->atu_pid) {
   1024 			return(UMATCH_VENDOR_PRODUCT);
   1025 		}
   1026 	}
   1027 	return(UMATCH_NONE);
   1028 }
   1029 
   1030 int
   1031 atu_media_change(struct ifnet *ifp)
   1032 {
   1033 	struct atu_softc	*sc = ifp->if_softc;
   1034 	struct ieee80211com	*ic = &sc->sc_ic;
   1035 	int			err, s;
   1036 
   1037 	DPRINTFN(10, ("%s: atu_media_change\n", USBDEVNAME(sc->atu_dev)));
   1038 
   1039 	err = ieee80211_media_change(ifp);
   1040 	if (err == ENETRESET) {
   1041 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   1042 		    (IFF_RUNNING|IFF_UP)) {
   1043 			s = splnet();
   1044 			ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1045 			atu_initial_config(sc);
   1046 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   1047 			splx(s);
   1048 		}
   1049 		err = 0;
   1050 	}
   1051 
   1052 	return (err);
   1053 }
   1054 
   1055 void
   1056 atu_media_status(struct ifnet *ifp, struct ifmediareq *req)
   1057 {
   1058 #ifdef ATU_DEBUG
   1059 	struct atu_softc	*sc = ifp->if_softc;
   1060 #endif /* ATU_DEBUG */
   1061 
   1062 	DPRINTFN(10, ("%s: atu_media_status\n", USBDEVNAME(sc->atu_dev)));
   1063 
   1064 	ieee80211_media_status(ifp, req);
   1065 }
   1066 
   1067 void
   1068 atu_task(void *arg)
   1069 {
   1070 	struct atu_softc	*sc = (struct atu_softc *)arg;
   1071 	struct ieee80211com	*ic = &sc->sc_ic;
   1072 	usbd_status		err;
   1073 	int			s;
   1074 
   1075 	DPRINTFN(10, ("%s: atu_task\n", USBDEVNAME(sc->atu_dev)));
   1076 
   1077 	if (sc->sc_state != ATU_S_OK)
   1078 		return;
   1079 
   1080 	switch (sc->sc_cmd) {
   1081 	case ATU_C_SCAN:
   1082 
   1083 		err = atu_start_scan(sc);
   1084 		if (err) {
   1085 			DPRINTFN(1, ("%s: atu_task: couldn't start scan!\n",
   1086 			    USBDEVNAME(sc->atu_dev)));
   1087 			return;
   1088 		}
   1089 
   1090 		err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
   1091 		if (err) {
   1092 			DPRINTF(("%s: atu_task: error waiting for scan\n",
   1093 			    USBDEVNAME(sc->atu_dev)));
   1094 			return;
   1095 		}
   1096 
   1097 		DPRINTF(("%s: ==========================> END OF SCAN!\n",
   1098 		    USBDEVNAME(sc->atu_dev)));
   1099 
   1100 		s = splnet();
   1101 		/* ieee80211_next_scan(ifp); */
   1102 		ieee80211_end_scan(ic);
   1103 		splx(s);
   1104 
   1105 		DPRINTF(("%s: ----------------------======> END OF SCAN2!\n",
   1106 		    USBDEVNAME(sc->atu_dev)));
   1107 		break;
   1108 
   1109 	case ATU_C_JOIN:
   1110 		atu_join(sc, ic->ic_bss);
   1111 	}
   1112 }
   1113 
   1114 int
   1115 atu_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   1116 {
   1117 	struct ifnet		*ifp = ic->ic_ifp;
   1118 	struct atu_softc	*sc = ifp->if_softc;
   1119 	enum ieee80211_state	ostate = ic->ic_state;
   1120 
   1121 	DPRINTFN(10, ("%s: atu_newstate: %s -> %s\n", USBDEVNAME(sc->atu_dev),
   1122 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
   1123 
   1124 	switch (nstate) {
   1125 	case IEEE80211_S_SCAN:
   1126 		memcpy(ic->ic_chan_scan, ic->ic_chan_active,
   1127 		    sizeof(ic->ic_chan_active));
   1128 		ieee80211_node_table_reset(&ic->ic_scan);
   1129 
   1130 		/* tell the event thread that we want a scan */
   1131 		sc->sc_cmd = ATU_C_SCAN;
   1132 		usb_add_task(sc->atu_udev, &sc->sc_task);
   1133 
   1134 		/* handle this ourselves */
   1135 		ic->ic_state = nstate;
   1136 		return (0);
   1137 
   1138 	case IEEE80211_S_AUTH:
   1139 	case IEEE80211_S_RUN:
   1140 		if (ostate == IEEE80211_S_SCAN) {
   1141 			sc->sc_cmd = ATU_C_JOIN;
   1142 			usb_add_task(sc->atu_udev, &sc->sc_task);
   1143 		}
   1144 		break;
   1145 	default:
   1146 		/* nothing to do */
   1147 		break;
   1148 	}
   1149 
   1150 	return (*sc->sc_newstate)(ic, nstate, arg);
   1151 }
   1152 
   1153 /*
   1154  * Attach the interface. Allocate softc structures, do
   1155  * setup and ethernet/BPF attach.
   1156  */
   1157 USB_ATTACH(atu)
   1158 {
   1159 	USB_ATTACH_START(atu, sc, uaa);
   1160 	char				*devinfop;
   1161 	usbd_status			err;
   1162 	usbd_device_handle		dev = uaa->device;
   1163 	u_int8_t			mode, channel;
   1164 	int i;
   1165 
   1166 	sc->sc_state = ATU_S_UNCONFIG;
   1167 
   1168 	devinfop = usbd_devinfo_alloc(dev, 0);
   1169 	USB_ATTACH_SETUP;
   1170 	printf("%s: %s\n", USBDEVNAME(sc->atu_dev), devinfop);
   1171 	usbd_devinfo_free(devinfop);
   1172 
   1173 	err = usbd_set_config_no(dev, ATU_CONFIG_NO, 1);
   1174 	if (err) {
   1175 		printf("%s: setting config no failed\n",
   1176 		    USBDEVNAME(sc->atu_dev));
   1177 		USB_ATTACH_ERROR_RETURN;
   1178 	}
   1179 
   1180 	err = usbd_device2interface_handle(dev, ATU_IFACE_IDX, &sc->atu_iface);
   1181 	if (err) {
   1182 		printf("%s: getting interface handle failed\n",
   1183 		    USBDEVNAME(sc->atu_dev));
   1184 		USB_ATTACH_ERROR_RETURN;
   1185 	}
   1186 
   1187 	sc->atu_unit = self->dv_unit;
   1188 	sc->atu_udev = dev;
   1189 
   1190 	/*
   1191 	 * look up the radio_type for the device
   1192 	 * basically does the same as USB_MATCH
   1193 	 */
   1194 	for (i = 0; i < sizeof(atu_devs)/sizeof(atu_devs[0]); i++) {
   1195 		struct atu_type *t = &atu_devs[i];
   1196 
   1197 		if (uaa->vendor == t->atu_vid &&
   1198 		    uaa->product == t->atu_pid) {
   1199 			sc->atu_radio = t->atu_radio;
   1200 			sc->atu_quirk = t->atu_quirk;
   1201 		}
   1202 	}
   1203 
   1204 	/*
   1205 	 * Check in the interface descriptor if we're in DFU mode
   1206 	 * If we're in DFU mode, we upload the external firmware
   1207 	 * If we're not, the PC must have rebooted without power-cycling
   1208 	 * the device.. I've tried this out, a reboot only requeres the
   1209 	 * external firmware to be reloaded :)
   1210 	 *
   1211 	 * Hmm. The at76c505a doesn't report a DFU descriptor when it's
   1212 	 * in DFU mode... Let's just try to get the opmode
   1213 	 */
   1214 	err = atu_get_opmode(sc, &mode);
   1215 	DPRINTFN(20, ("%s: opmode: %d\n", USBDEVNAME(sc->atu_dev), mode));
   1216 	if (err || (mode != MODE_NETCARD && mode != MODE_NOFLASHNETCARD)) {
   1217 		DPRINTF(("%s: starting internal firmware download\n",
   1218 		    USBDEVNAME(sc->atu_dev)));
   1219 
   1220 		atu_internal_firmware((struct device *)sc);
   1221 		/*
   1222 		 * atu_internal_firmware will cause a reset of the device
   1223 		 * so we don't want to do any more configuration after this
   1224 		 * point.
   1225 		 */
   1226 		USB_ATTACH_SUCCESS_RETURN;
   1227 	}
   1228 
   1229 	uaa->iface = sc->atu_iface;
   1230 
   1231 	if (mode != MODE_NETCARD) {
   1232 		DPRINTFN(15, ("%s: device needs external firmware\n",
   1233 		    USBDEVNAME(sc->atu_dev)));
   1234 
   1235 		if (mode != MODE_NOFLASHNETCARD) {
   1236 			DPRINTF(("%s: unexpected opmode=%d\n",
   1237 			    USBDEVNAME(sc->atu_dev), mode));
   1238 		}
   1239 
   1240 		/*
   1241 		 * There is no difference in opmode before and after external
   1242 		 * firmware upload with the SMC2662 V.4 . So instead we'll try
   1243 		 * to read the channel number. If we succeed, external
   1244 		 * firmwaremust have been already uploaded...
   1245 		 */
   1246 		if (sc->atu_radio != RadioIntersil) {
   1247 			err = atu_get_mib(sc, MIB_PHY__CHANNEL, &channel);
   1248 			if (!err) {
   1249 				DPRINTF(("%s: external firmware has already"
   1250 				    " been downloaded\n",
   1251 				    USBDEVNAME(sc->atu_dev)));
   1252 				atu_complete_attach(sc);
   1253 				USB_ATTACH_SUCCESS_RETURN;
   1254 			}
   1255 		}
   1256 
   1257 		atu_external_firmware((struct device *)sc);
   1258 
   1259 		/*
   1260 		 * atu_external_firmware will call atu_complete_attach after
   1261 		 * it's finished so we can just return.
   1262 		 */
   1263 	} else {
   1264 		/* all the firmwares are in place, so complete the attach */
   1265 		atu_complete_attach(sc);
   1266 	}
   1267 
   1268 	USB_ATTACH_SUCCESS_RETURN;
   1269 }
   1270 
   1271 void
   1272 atu_complete_attach(struct atu_softc *sc)
   1273 {
   1274 	struct ieee80211com		*ic = &sc->sc_ic;
   1275 	struct ifnet			*ifp = &sc->sc_if;
   1276 	usb_interface_descriptor_t	*id;
   1277 	usb_endpoint_descriptor_t	*ed;
   1278 	usbd_status			err;
   1279 	int				i;
   1280 #ifdef ATU_DEBUG
   1281 	struct atu_fw			fw;
   1282 #endif
   1283 
   1284 	id = usbd_get_interface_descriptor(sc->atu_iface);
   1285 
   1286 	/* Find endpoints. */
   1287 	for (i = 0; i < id->bNumEndpoints; i++) {
   1288 		ed = usbd_interface2endpoint_descriptor(sc->atu_iface, i);
   1289 		if (!ed) {
   1290 			DPRINTF(("%s: num_endp:%d\n", USBDEVNAME(sc->atu_dev),
   1291 			    sc->atu_iface->idesc->bNumEndpoints));
   1292 			DPRINTF(("%s: couldn't get ep %d\n",
   1293 			    USBDEVNAME(sc->atu_dev), i));
   1294 			return;
   1295 		}
   1296 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
   1297 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1298 			sc->atu_ed[ATU_ENDPT_RX] = ed->bEndpointAddress;
   1299 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
   1300 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
   1301 			sc->atu_ed[ATU_ENDPT_TX] = ed->bEndpointAddress;
   1302 		}
   1303 	}
   1304 
   1305 	/* read device config & get MAC address */
   1306 	err = atu_get_card_config(sc);
   1307 	if (err) {
   1308 		printf("\n%s: could not get card cfg!\n",
   1309 		    USBDEVNAME(sc->atu_dev));
   1310 		return;
   1311 	}
   1312 
   1313 #ifdef ATU_DEBUG
   1314 	/* DEBUG : try to get firmware version */
   1315 	err = atu_get_mib(sc, MIB_FW_VERSION, sizeof(fw), 0,
   1316 	    (u_int8_t *)&fw);
   1317 	if (!err) {
   1318 		DPRINTFN(15, ("%s: firmware: maj:%d min:%d patch:%d "
   1319 		    "build:%d\n", USBDEVNAME(sc->atu_dev), fw.major, fw.minor,
   1320 		    fw.patch, fw.build));
   1321 	} else {
   1322 		DPRINTF(("%s: get firmware version failed\n",
   1323 		    USBDEVNAME(sc->atu_dev)));
   1324 	}
   1325 #endif /* ATU_DEBUG */
   1326 
   1327 	/* Show the world our MAC address */
   1328 	printf("%s: MAC address %s\n", USBDEVNAME(sc->atu_dev),
   1329 	    ether_sprintf(ic->ic_myaddr));
   1330 
   1331 	sc->atu_cdata.atu_tx_inuse = 0;
   1332 	sc->atu_encrypt = ATU_WEP_OFF;
   1333 	sc->atu_wepkeylen = ATU_WEP_104BITS;
   1334 	sc->atu_wepkey = 0;
   1335 
   1336 	bzero(sc->atu_bssid, ETHER_ADDR_LEN);
   1337 	sc->atu_ssidlen = strlen(ATU_DEFAULT_SSID);
   1338 	memcpy(sc->atu_ssid, ATU_DEFAULT_SSID, sc->atu_ssidlen);
   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