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