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