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