Home | History | Annotate | Line # | Download | only in usb
if_zyd.c revision 1.55
      1 /*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
      2 /*	$NetBSD: if_zyd.c,v 1.55 2020/01/15 08:21:00 skrll Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2006 by Damien Bergamini <damien.bergamini (at) free.fr>
      6  * Copyright (c) 2006 by Florian Stoehr <ich (at) florian-stoehr.de>
      7  *
      8  * Permission to use, copy, modify, and distribute this software for any
      9  * purpose with or without fee is hereby granted, provided that the above
     10  * copyright notice and this permission notice appear in all copies.
     11  *
     12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  */
     20 
     21 /*-
     22  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.55 2020/01/15 08:21:00 skrll Exp $");
     27 
     28 #ifdef _KERNEL_OPT
     29 #include "opt_usb.h"
     30 #endif
     31 
     32 #include <sys/param.h>
     33 #include <sys/sockio.h>
     34 #include <sys/proc.h>
     35 #include <sys/mbuf.h>
     36 #include <sys/kernel.h>
     37 #include <sys/kmem.h>
     38 #include <sys/socket.h>
     39 #include <sys/systm.h>
     40 #include <sys/malloc.h>
     41 #include <sys/conf.h>
     42 #include <sys/device.h>
     43 
     44 #include <sys/bus.h>
     45 #include <machine/endian.h>
     46 
     47 #include <net/bpf.h>
     48 #include <net/if.h>
     49 #include <net/if_arp.h>
     50 #include <net/if_dl.h>
     51 #include <net/if_ether.h>
     52 #include <net/if_media.h>
     53 #include <net/if_types.h>
     54 
     55 #include <netinet/in.h>
     56 #include <netinet/in_systm.h>
     57 #include <netinet/in_var.h>
     58 #include <netinet/ip.h>
     59 
     60 #include <net80211/ieee80211_netbsd.h>
     61 #include <net80211/ieee80211_var.h>
     62 #include <net80211/ieee80211_amrr.h>
     63 #include <net80211/ieee80211_radiotap.h>
     64 
     65 #include <dev/firmload.h>
     66 
     67 #include <dev/usb/usb.h>
     68 #include <dev/usb/usbdi.h>
     69 #include <dev/usb/usbdi_util.h>
     70 #include <dev/usb/usbdevs.h>
     71 
     72 #include <dev/usb/if_zydreg.h>
     73 
     74 #ifdef ZYD_DEBUG
     75 #define DPRINTF(x)	do { if (zyddebug > 0) printf x; } while (0)
     76 #define DPRINTFN(n, x)	do { if (zyddebug > (n)) printf x; } while (0)
     77 int zyddebug = 0;
     78 #else
     79 #define DPRINTF(x)
     80 #define DPRINTFN(n, x)
     81 #endif
     82 
     83 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
     84 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
     85 
     86 /* various supported device vendors/products */
     87 #define ZYD_ZD1211_DEV(v, p)	\
     88 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 }
     89 #define ZYD_ZD1211B_DEV(v, p)	\
     90 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B }
     91 static const struct zyd_type {
     92 	struct usb_devno	dev;
     93 	uint8_t			rev;
     94 #define ZYD_ZD1211	0
     95 #define ZYD_ZD1211B	1
     96 } zyd_devs[] = {
     97 	ZYD_ZD1211_DEV(3COM2,		3CRUSB10075),
     98 	ZYD_ZD1211_DEV(ABOCOM,		WL54),
     99 	ZYD_ZD1211_DEV(ASUSTEK,		WL159G),
    100 	ZYD_ZD1211_DEV(CYBERTAN,	TG54USB),
    101 	ZYD_ZD1211_DEV(DRAYTEK,		VIGOR550),
    102 	ZYD_ZD1211_DEV(PLANEX2,		GWUS54GD),
    103 	ZYD_ZD1211_DEV(PLANEX2,		GWUS54GZL),
    104 	ZYD_ZD1211_DEV(PLANEX3,		GWUS54GZ),
    105 	ZYD_ZD1211_DEV(PLANEX3,		GWUS54MINI),
    106 	ZYD_ZD1211_DEV(SAGEM,		XG760A),
    107 	ZYD_ZD1211_DEV(SENAO,		NUB8301),
    108 	ZYD_ZD1211_DEV(SITECOMEU,	WL113),
    109 	ZYD_ZD1211_DEV(SWEEX,		ZD1211),
    110 	ZYD_ZD1211_DEV(TEKRAM,		QUICKWLAN),
    111 	ZYD_ZD1211_DEV(TEKRAM,		ZD1211_1),
    112 	ZYD_ZD1211_DEV(TEKRAM,		ZD1211_2),
    113 	ZYD_ZD1211_DEV(TWINMOS,		G240),
    114 	ZYD_ZD1211_DEV(UMEDIA,		ALL0298V2),
    115 	ZYD_ZD1211_DEV(UMEDIA,		TEW429UB_A),
    116 	ZYD_ZD1211_DEV(UMEDIA,		TEW429UB),
    117 	ZYD_ZD1211_DEV(WISTRONNEWEB,	UR055G),
    118 	ZYD_ZD1211_DEV(ZCOM,		ZD1211),
    119 	ZYD_ZD1211_DEV(ZYDAS,		ZD1211),
    120 	ZYD_ZD1211_DEV(ZYXEL,		AG225H),
    121 	ZYD_ZD1211_DEV(ZYXEL,		ZYAIRG220),
    122 	ZYD_ZD1211_DEV(ZYXEL,		G200V2),
    123 
    124 	ZYD_ZD1211B_DEV(ACCTON,		SMCWUSBG),
    125 	ZYD_ZD1211B_DEV(ACCTON,		WN4501H_LF_IR),
    126 	ZYD_ZD1211B_DEV(ACCTON,		WUS201),
    127 	ZYD_ZD1211B_DEV(ACCTON,		ZD1211B),
    128 	ZYD_ZD1211B_DEV(ASUSTEK,	A9T_WIFI),
    129 	ZYD_ZD1211B_DEV(BELKIN,		F5D7050C),
    130 	ZYD_ZD1211B_DEV(BELKIN,		ZD1211B),
    131 	ZYD_ZD1211B_DEV(BEWAN,		BWIFI_USB54AR),
    132 	ZYD_ZD1211B_DEV(CISCOLINKSYS,	WUSBF54G),
    133 	ZYD_ZD1211B_DEV(CYBERTAN,	ZD1211B),
    134 	ZYD_ZD1211B_DEV(FIBERLINE,	WL430U),
    135 	ZYD_ZD1211B_DEV(MELCO,		KG54L),
    136 	ZYD_ZD1211B_DEV(PHILIPS,	SNU5600),
    137 	ZYD_ZD1211B_DEV(PHILIPS,	SNU5630NS05),
    138 	ZYD_ZD1211B_DEV(PLANEX2,	GWUS54GXS),
    139 	ZYD_ZD1211B_DEV(SAGEM,		XG76NA),
    140 	ZYD_ZD1211B_DEV(SITECOMEU,	WL603),
    141 	ZYD_ZD1211B_DEV(SITECOMEU,	ZD1211B),
    142 	ZYD_ZD1211B_DEV(SONY,		IFU_WLM2),
    143 	ZYD_ZD1211B_DEV(UMEDIA,		TEW429UBC1),
    144 	ZYD_ZD1211B_DEV(UNKNOWN1,	ZD1211B_1),
    145 	ZYD_ZD1211B_DEV(UNKNOWN1,	ZD1211B_2),
    146 	ZYD_ZD1211B_DEV(UNKNOWN2,	ZD1211B),
    147 	ZYD_ZD1211B_DEV(UNKNOWN3,	ZD1211B),
    148 	ZYD_ZD1211B_DEV(USR,		USR5423),
    149 	ZYD_ZD1211B_DEV(VTECH,		ZD1211B),
    150 	ZYD_ZD1211B_DEV(ZCOM,		ZD1211B),
    151 	ZYD_ZD1211B_DEV(ZYDAS,		ZD1211B),
    152 	ZYD_ZD1211B_DEV(ZYDAS,		ZD1211B_2),
    153 	ZYD_ZD1211B_DEV(ZYXEL,		M202),
    154 	ZYD_ZD1211B_DEV(ZYXEL,		G220V2),
    155 };
    156 #define zyd_lookup(v, p)	\
    157 	((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
    158 
    159 static int zyd_match(device_t, cfdata_t, void *);
    160 static void zyd_attach(device_t, device_t, void *);
    161 static int zyd_detach(device_t, int);
    162 static int zyd_activate(device_t, enum devact);
    163 
    164 
    165 CFATTACH_DECL_NEW(zyd, sizeof(struct zyd_softc), zyd_match,
    166     zyd_attach, zyd_detach, zyd_activate);
    167 
    168 Static void	zyd_attachhook(device_t);
    169 Static int	zyd_complete_attach(struct zyd_softc *);
    170 Static int	zyd_open_pipes(struct zyd_softc *);
    171 Static void	zyd_close_pipes(struct zyd_softc *);
    172 Static int	zyd_alloc_tx_list(struct zyd_softc *);
    173 Static void	zyd_free_tx_list(struct zyd_softc *);
    174 Static int	zyd_alloc_rx_list(struct zyd_softc *);
    175 Static void	zyd_free_rx_list(struct zyd_softc *);
    176 Static struct	ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *);
    177 Static int	zyd_media_change(struct ifnet *);
    178 Static void	zyd_next_scan(void *);
    179 Static void	zyd_task(void *);
    180 Static int	zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
    181 Static int	zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
    182 		    void *, int, u_int);
    183 Static int	zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
    184 Static int	zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
    185 Static int	zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
    186 Static int	zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
    187 Static int	zyd_rfwrite(struct zyd_softc *, uint32_t);
    188 Static void	zyd_lock_phy(struct zyd_softc *);
    189 Static void	zyd_unlock_phy(struct zyd_softc *);
    190 Static int	zyd_rfmd_init(struct zyd_rf *);
    191 Static int	zyd_rfmd_switch_radio(struct zyd_rf *, int);
    192 Static int	zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
    193 Static int	zyd_al2230_init(struct zyd_rf *);
    194 Static int	zyd_al2230_switch_radio(struct zyd_rf *, int);
    195 Static int	zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
    196 Static int	zyd_al2230_init_b(struct zyd_rf *);
    197 Static int	zyd_al7230B_init(struct zyd_rf *);
    198 Static int	zyd_al7230B_switch_radio(struct zyd_rf *, int);
    199 Static int	zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
    200 Static int	zyd_al2210_init(struct zyd_rf *);
    201 Static int	zyd_al2210_switch_radio(struct zyd_rf *, int);
    202 Static int	zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
    203 Static int	zyd_gct_init(struct zyd_rf *);
    204 Static int	zyd_gct_switch_radio(struct zyd_rf *, int);
    205 Static int	zyd_gct_set_channel(struct zyd_rf *, uint8_t);
    206 Static int	zyd_maxim_init(struct zyd_rf *);
    207 Static int	zyd_maxim_switch_radio(struct zyd_rf *, int);
    208 Static int	zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
    209 Static int	zyd_maxim2_init(struct zyd_rf *);
    210 Static int	zyd_maxim2_switch_radio(struct zyd_rf *, int);
    211 Static int	zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
    212 Static int	zyd_rf_attach(struct zyd_softc *, uint8_t);
    213 Static const char *zyd_rf_name(uint8_t);
    214 Static int	zyd_hw_init(struct zyd_softc *);
    215 Static int	zyd_read_eeprom(struct zyd_softc *);
    216 Static int	zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
    217 Static int	zyd_set_bssid(struct zyd_softc *, const uint8_t *);
    218 Static int	zyd_switch_radio(struct zyd_softc *, int);
    219 Static void	zyd_set_led(struct zyd_softc *, int, int);
    220 Static int	zyd_set_rxfilter(struct zyd_softc *);
    221 Static void	zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
    222 Static int	zyd_set_beacon_interval(struct zyd_softc *, int);
    223 Static uint8_t	zyd_plcp_signal(int);
    224 Static void	zyd_intr(struct usbd_xfer *, void *, usbd_status);
    225 Static void	zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
    226 Static void	zyd_rxeof(struct usbd_xfer *, void *, usbd_status);
    227 Static void	zyd_txeof(struct usbd_xfer *, void *, usbd_status);
    228 Static int	zyd_tx_mgt(struct zyd_softc *, struct mbuf *,
    229 		    struct ieee80211_node *);
    230 Static int	zyd_tx_data(struct zyd_softc *, struct mbuf *,
    231 		    struct ieee80211_node *);
    232 Static void	zyd_start(struct ifnet *);
    233 Static void	zyd_watchdog(struct ifnet *);
    234 Static int	zyd_ioctl(struct ifnet *, u_long, void *);
    235 Static int	zyd_init(struct ifnet *);
    236 Static void	zyd_stop(struct ifnet *, int);
    237 Static int	zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
    238 Static void	zyd_iter_func(void *, struct ieee80211_node *);
    239 Static void	zyd_amrr_timeout(void *);
    240 Static void	zyd_newassoc(struct ieee80211_node *, int);
    241 
    242 static int
    243 zyd_match(device_t parent, cfdata_t match, void *aux)
    244 {
    245 	struct usb_attach_arg *uaa = aux;
    246 
    247 	return (zyd_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
    248 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    249 }
    250 
    251 Static void
    252 zyd_attachhook(device_t self)
    253 {
    254 	struct zyd_softc *sc = device_private(self);
    255 	firmware_handle_t fwh;
    256 	const char *fwname;
    257 	u_char *fw;
    258 	size_t size;
    259 	int error;
    260 
    261 	fwname = (sc->mac_rev == ZYD_ZD1211) ? "zyd-zd1211" : "zyd-zd1211b";
    262 	if ((error = firmware_open("zyd", fwname, &fwh)) != 0) {
    263 		aprint_error_dev(sc->sc_dev,
    264 		    "failed to open firmware %s (error=%d)\n", fwname, error);
    265 		return;
    266 	}
    267 	size = firmware_get_size(fwh);
    268 	fw = firmware_malloc(size);
    269 	if (fw == NULL) {
    270 		aprint_error_dev(sc->sc_dev,
    271 		    "failed to allocate firmware memory\n");
    272 		firmware_close(fwh);
    273 		return;
    274 	}
    275 	error = firmware_read(fwh, 0, fw, size);
    276 	firmware_close(fwh);
    277 	if (error != 0) {
    278 		aprint_error_dev(sc->sc_dev,
    279 		    "failed to read firmware (error %d)\n", error);
    280 		firmware_free(fw, size);
    281 		return;
    282 	}
    283 
    284 	error = zyd_loadfirmware(sc, fw, size);
    285 	if (error != 0) {
    286 		aprint_error_dev(sc->sc_dev,
    287 		    "could not load firmware (error=%d)\n", error);
    288 		firmware_free(fw, size);
    289 		return;
    290 	}
    291 
    292 	firmware_free(fw, size);
    293 
    294 	/* complete the attach process */
    295 	if ((error = zyd_complete_attach(sc)) == 0)
    296 		sc->attached = 1;
    297 	return;
    298 }
    299 
    300 static void
    301 zyd_attach(device_t parent, device_t self, void *aux)
    302 {
    303 	struct zyd_softc *sc = device_private(self);
    304 	struct usb_attach_arg *uaa = aux;
    305 	char *devinfop;
    306 	usb_device_descriptor_t* ddesc;
    307 	struct ifnet *ifp = &sc->sc_if;
    308 
    309 	sc->sc_dev = self;
    310 	sc->sc_udev = uaa->uaa_device;
    311 
    312 	aprint_naive("\n");
    313 	aprint_normal("\n");
    314 
    315 	devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
    316 	aprint_normal_dev(self, "%s\n", devinfop);
    317 	usbd_devinfo_free(devinfop);
    318 
    319 	sc->mac_rev = zyd_lookup(uaa->uaa_vendor, uaa->uaa_product)->rev;
    320 
    321 	ddesc = usbd_get_device_descriptor(sc->sc_udev);
    322 	if (UGETW(ddesc->bcdDevice) < 0x4330) {
    323 		aprint_error_dev(self, "device version mismatch: 0x%x "
    324 		    "(only >= 43.30 supported)\n", UGETW(ddesc->bcdDevice));
    325 		return;
    326 	}
    327 
    328 	ifp->if_softc = sc;
    329 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    330 	ifp->if_init = zyd_init;
    331 	ifp->if_ioctl = zyd_ioctl;
    332 	ifp->if_start = zyd_start;
    333 	ifp->if_watchdog = zyd_watchdog;
    334 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
    335 	IFQ_SET_READY(&ifp->if_snd);
    336 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    337 
    338 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    339 	cv_init(&sc->sc_cmdcv, "zydcmd");
    340 	SIMPLEQ_INIT(&sc->sc_rqh);
    341 
    342 	/* defer configrations after file system is ready to load firmware */
    343 	config_mountroot(self, zyd_attachhook);
    344 }
    345 
    346 Static int
    347 zyd_complete_attach(struct zyd_softc *sc)
    348 {
    349 	struct ieee80211com *ic = &sc->sc_ic;
    350 	struct ifnet *ifp = &sc->sc_if;
    351 	usbd_status error;
    352 	int i;
    353 
    354 	usb_init_task(&sc->sc_task, zyd_task, sc, 0);
    355 	callout_init(&(sc->sc_scan_ch), 0);
    356 
    357 	sc->amrr.amrr_min_success_threshold =  1;
    358 	sc->amrr.amrr_max_success_threshold = 10;
    359 	callout_init(&sc->sc_amrr_ch, 0);
    360 
    361 	error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
    362 	if (error != 0) {
    363 		aprint_error_dev(sc->sc_dev, "failed to set configuration"
    364 		    ", err=%s\n", usbd_errstr(error));
    365 		goto fail;
    366 	}
    367 
    368 	error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
    369 	    &sc->sc_iface);
    370 	if (error != 0) {
    371 		aprint_error_dev(sc->sc_dev,
    372 		    "getting interface handle failed\n");
    373 		goto fail;
    374 	}
    375 
    376 	if ((error = zyd_open_pipes(sc)) != 0) {
    377 		aprint_error_dev(sc->sc_dev, "could not open pipes\n");
    378 		goto fail;
    379 	}
    380 
    381 	if ((error = zyd_read_eeprom(sc)) != 0) {
    382 		aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
    383 		goto fail;
    384 	}
    385 
    386 	if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
    387 		aprint_error_dev(sc->sc_dev, "could not attach RF\n");
    388 		goto fail;
    389 	}
    390 
    391 	if ((error = zyd_hw_init(sc)) != 0) {
    392 		aprint_error_dev(sc->sc_dev,
    393 		    "hardware initialization failed\n");
    394 		goto fail;
    395 	}
    396 
    397 	aprint_normal_dev(sc->sc_dev,
    398 	    "HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
    399 	    (sc->mac_rev == ZYD_ZD1211) ? "": "B",
    400 	    sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
    401 	    sc->pa_rev, ether_sprintf(ic->ic_myaddr));
    402 
    403 	ic->ic_ifp = ifp;
    404 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
    405 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
    406 	ic->ic_state = IEEE80211_S_INIT;
    407 
    408 	/* set device capabilities */
    409 	ic->ic_caps =
    410 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
    411 	    IEEE80211_C_TXPMGT |	/* tx power management */
    412 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
    413 	    IEEE80211_C_WEP;		/* s/w WEP */
    414 
    415 	/* set supported .11b and .11g rates */
    416 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
    417 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
    418 
    419 	/* set supported .11b and .11g channels (1 through 14) */
    420 	for (i = 1; i <= 14; i++) {
    421 		ic->ic_channels[i].ic_freq =
    422 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    423 		ic->ic_channels[i].ic_flags =
    424 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    425 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    426 	}
    427 
    428 	if_attach(ifp);
    429 	ieee80211_ifattach(ic);
    430 	ic->ic_node_alloc = zyd_node_alloc;
    431 	ic->ic_newassoc = zyd_newassoc;
    432 
    433 	/* override state transition machine */
    434 	sc->sc_newstate = ic->ic_newstate;
    435 	ic->ic_newstate = zyd_newstate;
    436 	ieee80211_media_init(ic, zyd_media_change, ieee80211_media_status);
    437 
    438 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
    439 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
    440 	    &sc->sc_drvbpf);
    441 
    442 	sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
    443 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    444 	sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
    445 
    446 	sc->sc_txtap_len = sizeof(sc->sc_txtapu);
    447 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    448 	sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
    449 
    450 	ieee80211_announce(ic);
    451 
    452 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    453 
    454 fail:	return error;
    455 }
    456 
    457 static int
    458 zyd_detach(device_t self, int flags)
    459 {
    460 	struct zyd_softc *sc = device_private(self);
    461 	struct ieee80211com *ic = &sc->sc_ic;
    462 	struct ifnet *ifp = &sc->sc_if;
    463 
    464 	if (!sc->attached)
    465 		return 0;
    466 
    467 	mutex_enter(&sc->sc_lock);
    468 
    469 	zyd_stop(ifp, 1);
    470 	callout_halt(&sc->sc_scan_ch, NULL);
    471 	callout_halt(&sc->sc_amrr_ch, NULL);
    472 	usb_rem_task_wait(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER, NULL);
    473 
    474 	/* Abort, etc. done by zyd_stop */
    475 	zyd_close_pipes(sc);
    476 
    477 	sc->attached = 0;
    478 
    479 	bpf_detach(ifp);
    480 	ieee80211_ifdetach(ic);
    481 	if_detach(ifp);
    482 
    483 	mutex_exit(&sc->sc_lock);
    484 
    485 	mutex_destroy(&sc->sc_lock);
    486 	cv_destroy(&sc->sc_cmdcv);
    487 
    488 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    489 
    490 	return 0;
    491 }
    492 
    493 Static int
    494 zyd_open_pipes(struct zyd_softc *sc)
    495 {
    496 	usb_endpoint_descriptor_t *edesc;
    497 	usbd_status error;
    498 
    499 	/* interrupt in */
    500 	edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
    501 	if (edesc == NULL)
    502 		return EINVAL;
    503 
    504 	sc->ibuf_size = UGETW(edesc->wMaxPacketSize);
    505 	if (sc->ibuf_size == 0)	/* should not happen */
    506 		return EINVAL;
    507 
    508 	sc->ibuf = kmem_alloc(sc->ibuf_size, KM_SLEEP);
    509 
    510 	error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
    511 	    &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, sc->ibuf_size, zyd_intr,
    512 	    USBD_DEFAULT_INTERVAL);
    513 	if (error != 0) {
    514 		printf("%s: open rx intr pipe failed: %s\n",
    515 		    device_xname(sc->sc_dev), usbd_errstr(error));
    516 		goto fail;
    517 	}
    518 
    519 	/* interrupt out (not necessarily an interrupt pipe) */
    520 	error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
    521 	    &sc->zyd_ep[ZYD_ENDPT_IOUT]);
    522 	if (error != 0) {
    523 		printf("%s: open tx intr pipe failed: %s\n",
    524 		    device_xname(sc->sc_dev), usbd_errstr(error));
    525 		goto fail;
    526 	}
    527 
    528 	/* bulk in */
    529 	error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
    530 	    &sc->zyd_ep[ZYD_ENDPT_BIN]);
    531 	if (error != 0) {
    532 		printf("%s: open rx pipe failed: %s\n",
    533 		    device_xname(sc->sc_dev), usbd_errstr(error));
    534 		goto fail;
    535 	}
    536 
    537 	/* bulk out */
    538 	error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
    539 	    &sc->zyd_ep[ZYD_ENDPT_BOUT]);
    540 	if (error != 0) {
    541 		printf("%s: open tx pipe failed: %s\n",
    542 		    device_xname(sc->sc_dev), usbd_errstr(error));
    543 		goto fail;
    544 	}
    545 
    546 	return 0;
    547 
    548 fail:	zyd_close_pipes(sc);
    549 	return error;
    550 }
    551 
    552 Static void
    553 zyd_close_pipes(struct zyd_softc *sc)
    554 {
    555 	int i;
    556 
    557 	for (i = 0; i < ZYD_ENDPT_CNT; i++) {
    558 		if (sc->zyd_ep[i] != NULL) {
    559 			usbd_close_pipe(sc->zyd_ep[i]);
    560 			sc->zyd_ep[i] = NULL;
    561 		}
    562 	}
    563 	if (sc->ibuf != NULL) {
    564 		kmem_free(sc->ibuf, sc->ibuf_size);
    565 		sc->ibuf = NULL;
    566 	}
    567 }
    568 
    569 Static int
    570 zyd_alloc_tx_list(struct zyd_softc *sc)
    571 {
    572 	int i, error;
    573 
    574 	sc->tx_queued = 0;
    575 
    576 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
    577 		struct zyd_tx_data *data = &sc->tx_data[i];
    578 
    579 		data->sc = sc;	/* backpointer for callbacks */
    580 
    581 		error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_BOUT],
    582 		    ZYD_MAX_TXBUFSZ, USBD_FORCE_SHORT_XFER, 0, &data->xfer);
    583 		if (error) {
    584 			printf("%s: could not allocate tx xfer\n",
    585 			    device_xname(sc->sc_dev));
    586 			goto fail;
    587 		}
    588 		data->buf = usbd_get_buffer(data->xfer);
    589 
    590 		/* clear Tx descriptor */
    591 		memset(data->buf, 0, sizeof(struct zyd_tx_desc));
    592 	}
    593 	return 0;
    594 
    595 fail:	zyd_free_tx_list(sc);
    596 	return error;
    597 }
    598 
    599 Static void
    600 zyd_free_tx_list(struct zyd_softc *sc)
    601 {
    602 	int i;
    603 
    604 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
    605 		struct zyd_tx_data *data = &sc->tx_data[i];
    606 
    607 		if (data->xfer != NULL) {
    608 			usbd_destroy_xfer(data->xfer);
    609 			data->xfer = NULL;
    610 		}
    611 		if (data->ni != NULL) {
    612 			ieee80211_free_node(data->ni);
    613 			data->ni = NULL;
    614 		}
    615 	}
    616 }
    617 
    618 Static int
    619 zyd_alloc_rx_list(struct zyd_softc *sc)
    620 {
    621 	int i, error;
    622 
    623 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
    624 		struct zyd_rx_data *data = &sc->rx_data[i];
    625 
    626 		data->sc = sc;	/* backpointer for callbacks */
    627 
    628 		error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_BIN],
    629 		    ZYX_MAX_RXBUFSZ, 0, 0, &data->xfer);
    630 		if (error) {
    631 			printf("%s: could not allocate rx xfer\n",
    632 			    device_xname(sc->sc_dev));
    633 			goto fail;
    634 		}
    635 		data->buf = usbd_get_buffer(data->xfer);
    636 	}
    637 	return 0;
    638 
    639 fail:	zyd_free_rx_list(sc);
    640 	return error;
    641 }
    642 
    643 Static void
    644 zyd_free_rx_list(struct zyd_softc *sc)
    645 {
    646 	int i;
    647 
    648 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
    649 		struct zyd_rx_data *data = &sc->rx_data[i];
    650 
    651 		if (data->xfer != NULL) {
    652 			usbd_destroy_xfer(data->xfer);
    653 			data->xfer = NULL;
    654 		}
    655 	}
    656 }
    657 
    658 /* ARGUSED */
    659 Static struct ieee80211_node *
    660 zyd_node_alloc(struct ieee80211_node_table *nt __unused)
    661 {
    662 	struct zyd_node *zn;
    663 
    664 	zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
    665 	return zn ? &zn->ni : NULL;
    666 }
    667 
    668 Static int
    669 zyd_media_change(struct ifnet *ifp)
    670 {
    671 	int error;
    672 
    673 	error = ieee80211_media_change(ifp);
    674 	if (error != ENETRESET)
    675 		return error;
    676 
    677 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    678 		zyd_init(ifp);
    679 
    680 	return 0;
    681 }
    682 
    683 /*
    684  * This function is called periodically (every 200ms) during scanning to
    685  * switch from one channel to another.
    686  */
    687 Static void
    688 zyd_next_scan(void *arg)
    689 {
    690 	struct zyd_softc *sc = arg;
    691 	struct ieee80211com *ic = &sc->sc_ic;
    692 
    693 	if (ic->ic_state == IEEE80211_S_SCAN)
    694 		ieee80211_next_scan(ic);
    695 }
    696 
    697 Static void
    698 zyd_task(void *arg)
    699 {
    700 	struct zyd_softc *sc = arg;
    701 	struct ieee80211com *ic = &sc->sc_ic;
    702 	enum ieee80211_state ostate;
    703 
    704 	ostate = ic->ic_state;
    705 
    706 	switch (sc->sc_state) {
    707 	case IEEE80211_S_INIT:
    708 		if (ostate == IEEE80211_S_RUN) {
    709 			/* turn link LED off */
    710 			zyd_set_led(sc, ZYD_LED1, 0);
    711 
    712 			/* stop data LED from blinking */
    713 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
    714 		}
    715 		break;
    716 
    717 	case IEEE80211_S_SCAN:
    718 		zyd_set_chan(sc, ic->ic_curchan);
    719 		callout_reset(&sc->sc_scan_ch, hz / 5, zyd_next_scan, sc);
    720 		break;
    721 
    722 	case IEEE80211_S_AUTH:
    723 	case IEEE80211_S_ASSOC:
    724 		zyd_set_chan(sc, ic->ic_curchan);
    725 		break;
    726 
    727 	case IEEE80211_S_RUN:
    728 	{
    729 		struct ieee80211_node *ni = ic->ic_bss;
    730 
    731 		zyd_set_chan(sc, ic->ic_curchan);
    732 
    733 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
    734 			/* turn link LED on */
    735 			zyd_set_led(sc, ZYD_LED1, 1);
    736 
    737 			/* make data LED blink upon Tx */
    738 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
    739 
    740 			zyd_set_bssid(sc, ni->ni_bssid);
    741 		}
    742 
    743 		if (ic->ic_opmode == IEEE80211_M_STA) {
    744 			/* fake a join to init the tx rate */
    745 			zyd_newassoc(ni, 1);
    746 		}
    747 
    748 		/* start automatic rate control timer */
    749 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
    750 			callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
    751 
    752 		break;
    753 	}
    754 	}
    755 
    756 	sc->sc_newstate(ic, sc->sc_state, -1);
    757 }
    758 
    759 Static int
    760 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    761 {
    762 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
    763 
    764 	if (!sc->attached)
    765 		return ENXIO;
    766 
    767 	/*
    768 	 * XXXSMP: This does not wait for the task, if it is in flight,
    769 	 * to complete.  If this code works at all, it must rely on the
    770 	 * kernel lock to serialize with the USB task thread.
    771 	 */
    772 	usb_rem_task(sc->sc_udev, &sc->sc_task);
    773 	callout_stop(&sc->sc_scan_ch);
    774 	callout_stop(&sc->sc_amrr_ch);
    775 
    776 	/* do it in a process context */
    777 	sc->sc_state = nstate;
    778 	usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
    779 
    780 	return 0;
    781 }
    782 
    783 Static int
    784 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
    785     void *odata, int olen, u_int flags)
    786 {
    787 	struct usbd_xfer *xfer;
    788 	struct zyd_cmd cmd;
    789 	struct rq rq;
    790 	uint16_t xferflags;
    791 	int error;
    792 	usbd_status uerror;
    793 
    794 	error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_IOUT],
    795 	    sizeof(uint16_t) + ilen, USBD_FORCE_SHORT_XFER, 0, &xfer);
    796 	if (error)
    797 		return error;
    798 
    799 	cmd.code = htole16(code);
    800 	memcpy(cmd.data, idata, ilen);
    801 
    802 	xferflags = USBD_FORCE_SHORT_XFER;
    803 	if (!(flags & ZYD_CMD_FLAG_READ))
    804 		xferflags |= USBD_SYNCHRONOUS;
    805 	else {
    806 		rq.idata = idata;
    807 		rq.odata = odata;
    808 		rq.len = olen / sizeof(struct zyd_pair);
    809 		mutex_enter(&sc->sc_lock);
    810 		SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
    811 		mutex_exit(&sc->sc_lock);
    812 	}
    813 
    814 	usbd_setup_xfer(xfer, 0, &cmd, sizeof(uint16_t) + ilen, xferflags,
    815 	    ZYD_INTR_TIMEOUT, NULL);
    816 	uerror = usbd_transfer(xfer);
    817 	if (uerror != USBD_IN_PROGRESS && uerror != 0) {
    818 		printf("%s: could not send command (error=%s)\n",
    819 		    device_xname(sc->sc_dev), usbd_errstr(uerror));
    820 		(void)usbd_destroy_xfer(xfer);
    821 		return EIO;
    822 	}
    823 	if (!(flags & ZYD_CMD_FLAG_READ)) {
    824 		(void)usbd_destroy_xfer(xfer);
    825 		return 0;	/* write: don't wait for reply */
    826 	}
    827 	/* wait at most one second for command reply */
    828 	mutex_enter(&sc->sc_lock);
    829 	error = cv_timedwait_sig(&sc->sc_cmdcv, &sc->sc_lock, hz);
    830 	if (error == EWOULDBLOCK)
    831 		printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev));
    832 	SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
    833 	mutex_exit(&sc->sc_lock);
    834 
    835 	(void)usbd_destroy_xfer(xfer);
    836 	return error;
    837 }
    838 
    839 Static int
    840 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
    841 {
    842 	struct zyd_pair tmp;
    843 	int error;
    844 
    845 	reg = htole16(reg);
    846 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
    847 	    ZYD_CMD_FLAG_READ);
    848 	if (error == 0)
    849 		*val = le16toh(tmp.val);
    850 	else
    851 		*val = 0;
    852 	return error;
    853 }
    854 
    855 Static int
    856 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
    857 {
    858 	struct zyd_pair tmp[2];
    859 	uint16_t regs[2];
    860 	int error;
    861 
    862 	regs[0] = htole16(ZYD_REG32_HI(reg));
    863 	regs[1] = htole16(ZYD_REG32_LO(reg));
    864 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
    865 	    ZYD_CMD_FLAG_READ);
    866 	if (error == 0)
    867 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
    868 	else
    869 		*val = 0;
    870 	return error;
    871 }
    872 
    873 Static int
    874 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
    875 {
    876 	struct zyd_pair pair;
    877 
    878 	pair.reg = htole16(reg);
    879 	pair.val = htole16(val);
    880 
    881 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
    882 }
    883 
    884 Static int
    885 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
    886 {
    887 	struct zyd_pair pair[2];
    888 
    889 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
    890 	pair[0].val = htole16(val >> 16);
    891 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
    892 	pair[1].val = htole16(val & 0xffff);
    893 
    894 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
    895 }
    896 
    897 Static int
    898 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
    899 {
    900 	struct zyd_rf *rf = &sc->sc_rf;
    901 	struct zyd_rfwrite req;
    902 	uint16_t cr203;
    903 	int i;
    904 
    905 	(void)zyd_read16(sc, ZYD_CR203, &cr203);
    906 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
    907 
    908 	req.code  = htole16(2);
    909 	req.width = htole16(rf->width);
    910 	for (i = 0; i < rf->width; i++) {
    911 		req.bit[i] = htole16(cr203);
    912 		if (val & (1 << (rf->width - 1 - i)))
    913 			req.bit[i] |= htole16(ZYD_RF_DATA);
    914 	}
    915 	return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
    916 }
    917 
    918 Static void
    919 zyd_lock_phy(struct zyd_softc *sc)
    920 {
    921 	uint32_t tmp;
    922 
    923 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
    924 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
    925 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
    926 }
    927 
    928 Static void
    929 zyd_unlock_phy(struct zyd_softc *sc)
    930 {
    931 	uint32_t tmp;
    932 
    933 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
    934 	tmp |= ZYD_UNLOCK_PHY_REGS;
    935 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
    936 }
    937 
    938 /*
    939  * RFMD RF methods.
    940  */
    941 Static int
    942 zyd_rfmd_init(struct zyd_rf *rf)
    943 {
    944 	struct zyd_softc *sc = rf->rf_sc;
    945 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
    946 	static const uint32_t rfini[] = ZYD_RFMD_RF;
    947 	int error;
    948 	size_t i;
    949 
    950 	/* init RF-dependent PHY registers */
    951 	for (i = 0; i < __arraycount(phyini); i++) {
    952 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
    953 		if (error != 0)
    954 			return error;
    955 	}
    956 
    957 	/* init RFMD radio */
    958 	for (i = 0; i < __arraycount(rfini); i++) {
    959 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
    960 			return error;
    961 	}
    962 	return 0;
    963 }
    964 
    965 Static int
    966 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
    967 {
    968 	struct zyd_softc *sc = rf->rf_sc;
    969 
    970 	(void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
    971 	(void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
    972 
    973 	return 0;
    974 }
    975 
    976 Static int
    977 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
    978 {
    979 	struct zyd_softc *sc = rf->rf_sc;
    980 	static const struct {
    981 		uint32_t	r1, r2;
    982 	} rfprog[] = ZYD_RFMD_CHANTABLE;
    983 
    984 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
    985 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
    986 
    987 	return 0;
    988 }
    989 
    990 /*
    991  * AL2230 RF methods.
    992  */
    993 Static int
    994 zyd_al2230_init(struct zyd_rf *rf)
    995 {
    996 	struct zyd_softc *sc = rf->rf_sc;
    997 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
    998 	static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
    999 	static const uint32_t rfini[] = ZYD_AL2230_RF;
   1000 	int error;
   1001 	size_t i;
   1002 
   1003 	/* init RF-dependent PHY registers */
   1004 	for (i = 0; i < __arraycount(phyini); i++) {
   1005 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1006 		if (error != 0)
   1007 			return error;
   1008 	}
   1009 
   1010 	if (sc->rf_rev == ZYD_RF_AL2230S) {
   1011 		for (i = 0; i < __arraycount(phy2230s); i++) {
   1012 			error = zyd_write16(sc, phy2230s[i].reg,
   1013 			    phy2230s[i].val);
   1014 			if (error != 0)
   1015 				return error;
   1016 		}
   1017 	}
   1018 
   1019 	/* init AL2230 radio */
   1020 	for (i = 0; i < __arraycount(rfini); i++) {
   1021 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1022 			return error;
   1023 	}
   1024 	return 0;
   1025 }
   1026 
   1027 Static int
   1028 zyd_al2230_init_b(struct zyd_rf *rf)
   1029 {
   1030 	struct zyd_softc *sc = rf->rf_sc;
   1031 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
   1032 	static const uint32_t rfini[] = ZYD_AL2230_RF_B;
   1033 	int error;
   1034 	size_t i;
   1035 
   1036 	/* init RF-dependent PHY registers */
   1037 	for (i = 0; i < __arraycount(phyini); i++) {
   1038 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1039 		if (error != 0)
   1040 			return error;
   1041 	}
   1042 
   1043 	/* init AL2230 radio */
   1044 	for (i = 0; i < __arraycount(rfini); i++) {
   1045 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1046 			return error;
   1047 	}
   1048 	return 0;
   1049 }
   1050 
   1051 Static int
   1052 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
   1053 {
   1054 	struct zyd_softc *sc = rf->rf_sc;
   1055 	int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
   1056 
   1057 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
   1058 	(void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
   1059 
   1060 	return 0;
   1061 }
   1062 
   1063 Static int
   1064 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
   1065 {
   1066 	struct zyd_softc *sc = rf->rf_sc;
   1067 	static const struct {
   1068 		uint32_t	r1, r2, r3;
   1069 	} rfprog[] = ZYD_AL2230_CHANTABLE;
   1070 
   1071 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1072 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1073 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
   1074 
   1075 	(void)zyd_write16(sc, ZYD_CR138, 0x28);
   1076 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
   1077 
   1078 	return 0;
   1079 }
   1080 
   1081 /*
   1082  * AL7230B RF methods.
   1083  */
   1084 Static int
   1085 zyd_al7230B_init(struct zyd_rf *rf)
   1086 {
   1087 	struct zyd_softc *sc = rf->rf_sc;
   1088 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
   1089 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
   1090 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
   1091 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
   1092 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
   1093 	int error;
   1094 	size_t i;
   1095 
   1096 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
   1097 
   1098 	/* init RF-dependent PHY registers, part one */
   1099 	for (i = 0; i < __arraycount(phyini_1); i++) {
   1100 		error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
   1101 		if (error != 0)
   1102 			return error;
   1103 	}
   1104 	/* init AL7230B radio, part one */
   1105 	for (i = 0; i < __arraycount(rfini_1); i++) {
   1106 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
   1107 			return error;
   1108 	}
   1109 	/* init RF-dependent PHY registers, part two */
   1110 	for (i = 0; i < __arraycount(phyini_2); i++) {
   1111 		error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
   1112 		if (error != 0)
   1113 			return error;
   1114 	}
   1115 	/* init AL7230B radio, part two */
   1116 	for (i = 0; i < __arraycount(rfini_2); i++) {
   1117 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
   1118 			return error;
   1119 	}
   1120 	/* init RF-dependent PHY registers, part three */
   1121 	for (i = 0; i < __arraycount(phyini_3); i++) {
   1122 		error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
   1123 		if (error != 0)
   1124 			return error;
   1125 	}
   1126 
   1127 	return 0;
   1128 }
   1129 
   1130 Static int
   1131 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
   1132 {
   1133 	struct zyd_softc *sc = rf->rf_sc;
   1134 
   1135 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
   1136 	(void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
   1137 
   1138 	return 0;
   1139 }
   1140 
   1141 Static int
   1142 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
   1143 {
   1144 	struct zyd_softc *sc = rf->rf_sc;
   1145 	static const struct {
   1146 		uint32_t	r1, r2;
   1147 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
   1148 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
   1149 	int error;
   1150 	size_t i;
   1151 
   1152 	(void)zyd_write16(sc, ZYD_CR240, 0x57);
   1153 	(void)zyd_write16(sc, ZYD_CR251, 0x2f);
   1154 
   1155 	for (i = 0; i < __arraycount(rfsc); i++) {
   1156 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
   1157 			return error;
   1158 	}
   1159 
   1160 	(void)zyd_write16(sc, ZYD_CR128, 0x14);
   1161 	(void)zyd_write16(sc, ZYD_CR129, 0x12);
   1162 	(void)zyd_write16(sc, ZYD_CR130, 0x10);
   1163 	(void)zyd_write16(sc, ZYD_CR38,  0x38);
   1164 	(void)zyd_write16(sc, ZYD_CR136, 0xdf);
   1165 
   1166 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1167 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1168 	(void)zyd_rfwrite(sc, 0x3c9000);
   1169 
   1170 	(void)zyd_write16(sc, ZYD_CR251, 0x3f);
   1171 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
   1172 	(void)zyd_write16(sc, ZYD_CR240, 0x08);
   1173 
   1174 	return 0;
   1175 }
   1176 
   1177 /*
   1178  * AL2210 RF methods.
   1179  */
   1180 Static int
   1181 zyd_al2210_init(struct zyd_rf *rf)
   1182 {
   1183 	struct zyd_softc *sc = rf->rf_sc;
   1184 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
   1185 	static const uint32_t rfini[] = ZYD_AL2210_RF;
   1186 	uint32_t tmp;
   1187 	int error;
   1188 	size_t i;
   1189 
   1190 	(void)zyd_write32(sc, ZYD_CR18, 2);
   1191 
   1192 	/* init RF-dependent PHY registers */
   1193 	for (i = 0; i < __arraycount(phyini); i++) {
   1194 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1195 		if (error != 0)
   1196 			return error;
   1197 	}
   1198 	/* init AL2210 radio */
   1199 	for (i = 0; i < __arraycount(rfini); i++) {
   1200 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1201 			return error;
   1202 	}
   1203 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1204 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
   1205 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
   1206 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
   1207 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
   1208 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
   1209 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1210 	(void)zyd_write32(sc, ZYD_CR18, 3);
   1211 
   1212 	return 0;
   1213 }
   1214 
   1215 Static int
   1216 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
   1217 {
   1218 	/* vendor driver does nothing for this RF chip */
   1219 
   1220 	return 0;
   1221 }
   1222 
   1223 Static int
   1224 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
   1225 {
   1226 	struct zyd_softc *sc = rf->rf_sc;
   1227 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
   1228 	uint32_t tmp;
   1229 
   1230 	(void)zyd_write32(sc, ZYD_CR18, 2);
   1231 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1232 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
   1233 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
   1234 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
   1235 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
   1236 
   1237 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
   1238 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1239 
   1240 	/* actually set the channel */
   1241 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
   1242 
   1243 	(void)zyd_write32(sc, ZYD_CR18, 3);
   1244 
   1245 	return 0;
   1246 }
   1247 
   1248 /*
   1249  * GCT RF methods.
   1250  */
   1251 Static int
   1252 zyd_gct_init(struct zyd_rf *rf)
   1253 {
   1254 	struct zyd_softc *sc = rf->rf_sc;
   1255 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
   1256 	static const uint32_t rfini[] = ZYD_GCT_RF;
   1257 	int error;
   1258 	size_t i;
   1259 
   1260 	/* init RF-dependent PHY registers */
   1261 	for (i = 0; i < __arraycount(phyini); i++) {
   1262 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1263 		if (error != 0)
   1264 			return error;
   1265 	}
   1266 	/* init cgt radio */
   1267 	for (i = 0; i < __arraycount(rfini); i++) {
   1268 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1269 			return error;
   1270 	}
   1271 	return 0;
   1272 }
   1273 
   1274 Static int
   1275 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
   1276 {
   1277 	/* vendor driver does nothing for this RF chip */
   1278 
   1279 	return 0;
   1280 }
   1281 
   1282 Static int
   1283 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
   1284 {
   1285 	struct zyd_softc *sc = rf->rf_sc;
   1286 	static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
   1287 
   1288 	(void)zyd_rfwrite(sc, 0x1c0000);
   1289 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
   1290 	(void)zyd_rfwrite(sc, 0x1c0008);
   1291 
   1292 	return 0;
   1293 }
   1294 
   1295 /*
   1296  * Maxim RF methods.
   1297  */
   1298 Static int
   1299 zyd_maxim_init(struct zyd_rf *rf)
   1300 {
   1301 	struct zyd_softc *sc = rf->rf_sc;
   1302 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
   1303 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
   1304 	uint16_t tmp;
   1305 	int error;
   1306 	size_t i;
   1307 
   1308 	/* init RF-dependent PHY registers */
   1309 	for (i = 0; i < __arraycount(phyini); i++) {
   1310 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1311 		if (error != 0)
   1312 			return error;
   1313 	}
   1314 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1315 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1316 
   1317 	/* init maxim radio */
   1318 	for (i = 0; i < __arraycount(rfini); i++) {
   1319 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1320 			return error;
   1321 	}
   1322 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1323 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1324 
   1325 	return 0;
   1326 }
   1327 
   1328 Static int
   1329 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
   1330 {
   1331 	/* vendor driver does nothing for this RF chip */
   1332 
   1333 	return 0;
   1334 }
   1335 
   1336 Static int
   1337 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
   1338 {
   1339 	struct zyd_softc *sc = rf->rf_sc;
   1340 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
   1341 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
   1342 	static const struct {
   1343 		uint32_t	r1, r2;
   1344 	} rfprog[] = ZYD_MAXIM_CHANTABLE;
   1345 	uint16_t tmp;
   1346 	int error;
   1347 	size_t i;
   1348 
   1349 	/*
   1350 	 * Do the same as we do when initializing it, except for the channel
   1351 	 * values coming from the two channel tables.
   1352 	 */
   1353 
   1354 	/* init RF-dependent PHY registers */
   1355 	for (i = 0; i < __arraycount(phyini); i++) {
   1356 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1357 		if (error != 0)
   1358 			return error;
   1359 	}
   1360 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1361 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1362 
   1363 	/* first two values taken from the chantables */
   1364 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1365 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1366 
   1367 	/* init maxim radio - skipping the two first values */
   1368 	for (i = 2; i < __arraycount(rfini); i++) {
   1369 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1370 			return error;
   1371 	}
   1372 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1373 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1374 
   1375 	return 0;
   1376 }
   1377 
   1378 /*
   1379  * Maxim2 RF methods.
   1380  */
   1381 Static int
   1382 zyd_maxim2_init(struct zyd_rf *rf)
   1383 {
   1384 	struct zyd_softc *sc = rf->rf_sc;
   1385 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
   1386 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
   1387 	uint16_t tmp;
   1388 	int error;
   1389 	size_t i;
   1390 
   1391 	/* init RF-dependent PHY registers */
   1392 	for (i = 0; i < __arraycount(phyini); i++) {
   1393 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1394 		if (error != 0)
   1395 			return error;
   1396 	}
   1397 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1398 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1399 
   1400 	/* init maxim2 radio */
   1401 	for (i = 0; i < __arraycount(rfini); i++) {
   1402 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1403 			return error;
   1404 	}
   1405 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1406 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1407 
   1408 	return 0;
   1409 }
   1410 
   1411 Static int
   1412 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
   1413 {
   1414 	/* vendor driver does nothing for this RF chip */
   1415 
   1416 	return 0;
   1417 }
   1418 
   1419 Static int
   1420 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
   1421 {
   1422 	struct zyd_softc *sc = rf->rf_sc;
   1423 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
   1424 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
   1425 	static const struct {
   1426 		uint32_t	r1, r2;
   1427 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
   1428 	uint16_t tmp;
   1429 	int error;
   1430 	size_t i;
   1431 
   1432 	/*
   1433 	 * Do the same as we do when initializing it, except for the channel
   1434 	 * values coming from the two channel tables.
   1435 	 */
   1436 
   1437 	/* init RF-dependent PHY registers */
   1438 	for (i = 0; i < __arraycount(phyini); i++) {
   1439 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1440 		if (error != 0)
   1441 			return error;
   1442 	}
   1443 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1444 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1445 
   1446 	/* first two values taken from the chantables */
   1447 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1448 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1449 
   1450 	/* init maxim2 radio - skipping the two first values */
   1451 	for (i = 2; i < __arraycount(rfini); i++) {
   1452 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1453 			return error;
   1454 	}
   1455 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1456 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1457 
   1458 	return 0;
   1459 }
   1460 
   1461 Static int
   1462 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
   1463 {
   1464 	struct zyd_rf *rf = &sc->sc_rf;
   1465 
   1466 	rf->rf_sc = sc;
   1467 
   1468 	switch (type) {
   1469 	case ZYD_RF_RFMD:
   1470 		rf->init         = zyd_rfmd_init;
   1471 		rf->switch_radio = zyd_rfmd_switch_radio;
   1472 		rf->set_channel  = zyd_rfmd_set_channel;
   1473 		rf->width        = 24;	/* 24-bit RF values */
   1474 		break;
   1475 	case ZYD_RF_AL2230:
   1476 	case ZYD_RF_AL2230S:
   1477 		if (sc->mac_rev == ZYD_ZD1211B)
   1478 			rf->init = zyd_al2230_init_b;
   1479 		else
   1480 			rf->init = zyd_al2230_init;
   1481 		rf->switch_radio = zyd_al2230_switch_radio;
   1482 		rf->set_channel  = zyd_al2230_set_channel;
   1483 		rf->width        = 24;	/* 24-bit RF values */
   1484 		break;
   1485 	case ZYD_RF_AL7230B:
   1486 		rf->init         = zyd_al7230B_init;
   1487 		rf->switch_radio = zyd_al7230B_switch_radio;
   1488 		rf->set_channel  = zyd_al7230B_set_channel;
   1489 		rf->width        = 24;	/* 24-bit RF values */
   1490 		break;
   1491 	case ZYD_RF_AL2210:
   1492 		rf->init         = zyd_al2210_init;
   1493 		rf->switch_radio = zyd_al2210_switch_radio;
   1494 		rf->set_channel  = zyd_al2210_set_channel;
   1495 		rf->width        = 24;	/* 24-bit RF values */
   1496 		break;
   1497 	case ZYD_RF_GCT:
   1498 		rf->init         = zyd_gct_init;
   1499 		rf->switch_radio = zyd_gct_switch_radio;
   1500 		rf->set_channel  = zyd_gct_set_channel;
   1501 		rf->width        = 21;	/* 21-bit RF values */
   1502 		break;
   1503 	case ZYD_RF_MAXIM_NEW:
   1504 		rf->init         = zyd_maxim_init;
   1505 		rf->switch_radio = zyd_maxim_switch_radio;
   1506 		rf->set_channel  = zyd_maxim_set_channel;
   1507 		rf->width        = 18;	/* 18-bit RF values */
   1508 		break;
   1509 	case ZYD_RF_MAXIM_NEW2:
   1510 		rf->init         = zyd_maxim2_init;
   1511 		rf->switch_radio = zyd_maxim2_switch_radio;
   1512 		rf->set_channel  = zyd_maxim2_set_channel;
   1513 		rf->width        = 18;	/* 18-bit RF values */
   1514 		break;
   1515 	default:
   1516 		printf("%s: sorry, radio \"%s\" is not supported yet\n",
   1517 		    device_xname(sc->sc_dev), zyd_rf_name(type));
   1518 		return EINVAL;
   1519 	}
   1520 	return 0;
   1521 }
   1522 
   1523 Static const char *
   1524 zyd_rf_name(uint8_t type)
   1525 {
   1526 	static const char * const zyd_rfs[] = {
   1527 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
   1528 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
   1529 		"AL2230S", "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
   1530 		"PHILIPS"
   1531 	};
   1532 
   1533 	return zyd_rfs[(type > 15) ? 0 : type];
   1534 }
   1535 
   1536 Static int
   1537 zyd_hw_init(struct zyd_softc *sc)
   1538 {
   1539 	struct zyd_rf *rf = &sc->sc_rf;
   1540 	const struct zyd_phy_pair *phyp;
   1541 	int error;
   1542 
   1543 	/* specify that the plug and play is finished */
   1544 	(void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
   1545 
   1546 	(void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
   1547 	DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
   1548 
   1549 	/* retrieve firmware revision number */
   1550 	(void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
   1551 
   1552 	(void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
   1553 	(void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
   1554 
   1555 	/* disable interrupts */
   1556 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
   1557 
   1558 	/* PHY init */
   1559 	zyd_lock_phy(sc);
   1560 	phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
   1561 	for (; phyp->reg != 0; phyp++) {
   1562 		if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
   1563 			goto fail;
   1564 	}
   1565 	zyd_unlock_phy(sc);
   1566 
   1567 	/* HMAC init */
   1568 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
   1569 	zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
   1570 
   1571 	if (sc->mac_rev == ZYD_ZD1211) {
   1572 		zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
   1573 	} else {
   1574 		zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202);
   1575 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
   1576 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
   1577 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
   1578 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
   1579 		zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
   1580 		zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
   1581 		zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
   1582 	}
   1583 
   1584 	zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
   1585 	zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
   1586 	zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
   1587 	zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
   1588 	zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
   1589 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
   1590 	zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
   1591 	zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
   1592 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
   1593 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
   1594 	zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
   1595 	zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
   1596 	zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
   1597 	zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
   1598 	zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
   1599 	zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
   1600 	zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
   1601 
   1602 	/* RF chip init */
   1603 	zyd_lock_phy(sc);
   1604 	error = (*rf->init)(rf);
   1605 	zyd_unlock_phy(sc);
   1606 	if (error != 0) {
   1607 		printf("%s: radio initialization failed\n",
   1608 		    device_xname(sc->sc_dev));
   1609 		goto fail;
   1610 	}
   1611 
   1612 	/* init beacon interval to 100ms */
   1613 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
   1614 		goto fail;
   1615 
   1616 fail:	return error;
   1617 }
   1618 
   1619 Static int
   1620 zyd_read_eeprom(struct zyd_softc *sc)
   1621 {
   1622 	struct ieee80211com *ic = &sc->sc_ic;
   1623 	uint32_t tmp;
   1624 	uint16_t val;
   1625 	int i;
   1626 
   1627 	/* read MAC address */
   1628 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
   1629 	ic->ic_myaddr[0] = tmp & 0xff;
   1630 	ic->ic_myaddr[1] = tmp >>  8;
   1631 	ic->ic_myaddr[2] = tmp >> 16;
   1632 	ic->ic_myaddr[3] = tmp >> 24;
   1633 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
   1634 	ic->ic_myaddr[4] = tmp & 0xff;
   1635 	ic->ic_myaddr[5] = tmp >>  8;
   1636 
   1637 	(void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
   1638 	sc->rf_rev = tmp & 0x0f;
   1639 	sc->pa_rev = (tmp >> 16) & 0x0f;
   1640 
   1641 	/* read regulatory domain (currently unused) */
   1642 	(void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
   1643 	sc->regdomain = tmp >> 16;
   1644 	DPRINTF(("regulatory domain %x\n", sc->regdomain));
   1645 
   1646 	/* read Tx power calibration tables */
   1647 	for (i = 0; i < 7; i++) {
   1648 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
   1649 		sc->pwr_cal[i * 2] = val >> 8;
   1650 		sc->pwr_cal[i * 2 + 1] = val & 0xff;
   1651 
   1652 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
   1653 		sc->pwr_int[i * 2] = val >> 8;
   1654 		sc->pwr_int[i * 2 + 1] = val & 0xff;
   1655 
   1656 		(void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
   1657 		sc->ofdm36_cal[i * 2] = val >> 8;
   1658 		sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
   1659 
   1660 		(void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
   1661 		sc->ofdm48_cal[i * 2] = val >> 8;
   1662 		sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
   1663 
   1664 		(void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
   1665 		sc->ofdm54_cal[i * 2] = val >> 8;
   1666 		sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
   1667 	}
   1668 	return 0;
   1669 }
   1670 
   1671 Static int
   1672 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
   1673 {
   1674 	uint32_t tmp;
   1675 
   1676 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
   1677 	(void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
   1678 
   1679 	tmp = addr[5] << 8 | addr[4];
   1680 	(void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
   1681 
   1682 	return 0;
   1683 }
   1684 
   1685 Static int
   1686 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
   1687 {
   1688 	uint32_t tmp;
   1689 
   1690 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
   1691 	(void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
   1692 
   1693 	tmp = addr[5] << 8 | addr[4];
   1694 	(void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
   1695 
   1696 	return 0;
   1697 }
   1698 
   1699 Static int
   1700 zyd_switch_radio(struct zyd_softc *sc, int on)
   1701 {
   1702 	struct zyd_rf *rf = &sc->sc_rf;
   1703 	int error;
   1704 
   1705 	zyd_lock_phy(sc);
   1706 	error = (*rf->switch_radio)(rf, on);
   1707 	zyd_unlock_phy(sc);
   1708 
   1709 	return error;
   1710 }
   1711 
   1712 Static void
   1713 zyd_set_led(struct zyd_softc *sc, int which, int on)
   1714 {
   1715 	uint32_t tmp;
   1716 
   1717 	(void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
   1718 	tmp &= ~which;
   1719 	if (on)
   1720 		tmp |= which;
   1721 	(void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
   1722 }
   1723 
   1724 Static int
   1725 zyd_set_rxfilter(struct zyd_softc *sc)
   1726 {
   1727 	uint32_t rxfilter;
   1728 
   1729 	switch (sc->sc_ic.ic_opmode) {
   1730 	case IEEE80211_M_STA:
   1731 		rxfilter = ZYD_FILTER_BSS;
   1732 		break;
   1733 	case IEEE80211_M_IBSS:
   1734 	case IEEE80211_M_HOSTAP:
   1735 		rxfilter = ZYD_FILTER_HOSTAP;
   1736 		break;
   1737 	case IEEE80211_M_MONITOR:
   1738 		rxfilter = ZYD_FILTER_MONITOR;
   1739 		break;
   1740 	default:
   1741 		/* should not get there */
   1742 		return EINVAL;
   1743 	}
   1744 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
   1745 }
   1746 
   1747 Static void
   1748 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
   1749 {
   1750 	struct ieee80211com *ic = &sc->sc_ic;
   1751 	struct zyd_rf *rf = &sc->sc_rf;
   1752 	u_int chan;
   1753 
   1754 	chan = ieee80211_chan2ieee(ic, c);
   1755 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
   1756 		return;
   1757 
   1758 	zyd_lock_phy(sc);
   1759 
   1760 	(*rf->set_channel)(rf, chan);
   1761 
   1762 	/* update Tx power */
   1763 	(void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
   1764 	(void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
   1765 
   1766 	if (sc->mac_rev == ZYD_ZD1211B) {
   1767 		(void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
   1768 		(void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
   1769 		(void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
   1770 
   1771 		(void)zyd_write32(sc, ZYD_CR69, 0x28);
   1772 		(void)zyd_write32(sc, ZYD_CR69, 0x2a);
   1773 	}
   1774 
   1775 	zyd_unlock_phy(sc);
   1776 }
   1777 
   1778 Static int
   1779 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
   1780 {
   1781 	/* XXX this is probably broken.. */
   1782 	(void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
   1783 	(void)zyd_write32(sc, ZYD_CR_PRE_TBTT,        bintval - 1);
   1784 	(void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL,    bintval);
   1785 
   1786 	return 0;
   1787 }
   1788 
   1789 Static uint8_t
   1790 zyd_plcp_signal(int rate)
   1791 {
   1792 	switch (rate) {
   1793 	/* CCK rates (returned values are device-dependent) */
   1794 	case 2:		return 0x0;
   1795 	case 4:		return 0x1;
   1796 	case 11:	return 0x2;
   1797 	case 22:	return 0x3;
   1798 
   1799 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
   1800 	case 12:	return 0xb;
   1801 	case 18:	return 0xf;
   1802 	case 24:	return 0xa;
   1803 	case 36:	return 0xe;
   1804 	case 48:	return 0x9;
   1805 	case 72:	return 0xd;
   1806 	case 96:	return 0x8;
   1807 	case 108:	return 0xc;
   1808 
   1809 	/* unsupported rates (should not get there) */
   1810 	default:	return 0xff;
   1811 	}
   1812 }
   1813 
   1814 Static void
   1815 zyd_intr(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1816 {
   1817 	struct zyd_softc *sc = (struct zyd_softc *)priv;
   1818 	struct zyd_cmd *cmd;
   1819 	uint32_t datalen;
   1820 
   1821 	if (status != USBD_NORMAL_COMPLETION) {
   1822 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1823 			return;
   1824 
   1825 		if (status == USBD_STALLED) {
   1826 			usbd_clear_endpoint_stall_async(
   1827 			    sc->zyd_ep[ZYD_ENDPT_IIN]);
   1828 		}
   1829 		return;
   1830 	}
   1831 
   1832 	cmd = (struct zyd_cmd *)sc->ibuf;
   1833 
   1834 	if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
   1835 		struct zyd_notif_retry *retry =
   1836 		    (struct zyd_notif_retry *)cmd->data;
   1837 		struct ieee80211com *ic = &sc->sc_ic;
   1838 		struct ifnet *ifp = &sc->sc_if;
   1839 		struct ieee80211_node *ni;
   1840 
   1841 		DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
   1842 		    le16toh(retry->rate), ether_sprintf(retry->macaddr),
   1843 		    le16toh(retry->count) & 0xff, le16toh(retry->count)));
   1844 
   1845 		/*
   1846 		 * Find the node to which the packet was sent and update its
   1847 		 * retry statistics.  In BSS mode, this node is the AP we're
   1848 		 * associated to so no lookup is actually needed.
   1849 		 */
   1850 		if (ic->ic_opmode != IEEE80211_M_STA) {
   1851 			ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr);
   1852 			if (ni == NULL)
   1853 				return;	/* just ignore */
   1854 		} else
   1855 			ni = ic->ic_bss;
   1856 
   1857 		((struct zyd_node *)ni)->amn.amn_retrycnt++;
   1858 
   1859 		if (le16toh(retry->count) & 0x100)
   1860 			ifp->if_oerrors++;	/* too many retries */
   1861 
   1862 	} else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
   1863 		struct rq *rqp;
   1864 
   1865 		if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
   1866 			return;	/* HMAC interrupt */
   1867 
   1868 		usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
   1869 		datalen -= sizeof(cmd->code);
   1870 		datalen -= 2;	/* XXX: padding? */
   1871 
   1872 		mutex_enter(&sc->sc_lock);
   1873 		SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) {
   1874 			int i;
   1875 
   1876 			if (sizeof(struct zyd_pair) * rqp->len != datalen)
   1877 				continue;
   1878 			for (i = 0; i < rqp->len; i++) {
   1879 				if (*(((const uint16_t *)rqp->idata) + i) !=
   1880 				    (((struct zyd_pair *)cmd->data) + i)->reg)
   1881 					break;
   1882 			}
   1883 			if (i != rqp->len)
   1884 				continue;
   1885 
   1886 			/* copy answer into caller-supplied buffer */
   1887 			memcpy(rqp->odata, cmd->data,
   1888 			    sizeof(struct zyd_pair) * rqp->len);
   1889 			cv_signal(&sc->sc_cmdcv);
   1890 			mutex_exit(&sc->sc_lock);
   1891 			return;
   1892 		}
   1893 		mutex_exit(&sc->sc_lock);
   1894 		return;	/* unexpected IORD notification */
   1895 	} else {
   1896 		printf("%s: unknown notification %x\n", device_xname(sc->sc_dev),
   1897 		    le16toh(cmd->code));
   1898 	}
   1899 }
   1900 
   1901 Static void
   1902 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
   1903 {
   1904 	struct ieee80211com *ic = &sc->sc_ic;
   1905 	struct ifnet *ifp = &sc->sc_if;
   1906 	struct ieee80211_node *ni;
   1907 	struct ieee80211_frame *wh;
   1908 	const struct zyd_plcphdr *plcp;
   1909 	const struct zyd_rx_stat *stat;
   1910 	struct mbuf *m;
   1911 	int rlen, s;
   1912 
   1913 	if (len < ZYD_MIN_FRAGSZ) {
   1914 		printf("%s: frame too short (length=%d)\n",
   1915 		    device_xname(sc->sc_dev), len);
   1916 		ifp->if_ierrors++;
   1917 		return;
   1918 	}
   1919 
   1920 	plcp = (const struct zyd_plcphdr *)buf;
   1921 	stat = (const struct zyd_rx_stat *)
   1922 	    (buf + len - sizeof(struct zyd_rx_stat));
   1923 
   1924 	if (stat->flags & ZYD_RX_ERROR) {
   1925 		DPRINTF(("%s: RX status indicated error (%x)\n",
   1926 		    device_xname(sc->sc_dev), stat->flags));
   1927 		ifp->if_ierrors++;
   1928 		return;
   1929 	}
   1930 
   1931 	/* compute actual frame length */
   1932 	rlen = len - sizeof(struct zyd_plcphdr) -
   1933 	    sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
   1934 
   1935 	/* allocate a mbuf to store the frame */
   1936 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1937 	if (m == NULL) {
   1938 		printf("%s: could not allocate rx mbuf\n",
   1939 		    device_xname(sc->sc_dev));
   1940 		ifp->if_ierrors++;
   1941 		return;
   1942 	}
   1943 	if (rlen > MHLEN) {
   1944 		MCLGET(m, M_DONTWAIT);
   1945 		if (!(m->m_flags & M_EXT)) {
   1946 			printf("%s: could not allocate rx mbuf cluster\n",
   1947 			    device_xname(sc->sc_dev));
   1948 			m_freem(m);
   1949 			ifp->if_ierrors++;
   1950 			return;
   1951 		}
   1952 	}
   1953 	m_set_rcvif(m, ifp);
   1954 	m->m_pkthdr.len = m->m_len = rlen;
   1955 	memcpy(mtod(m, uint8_t *), (const uint8_t *)(plcp + 1), rlen);
   1956 
   1957 	s = splnet();
   1958 
   1959 	if (sc->sc_drvbpf != NULL) {
   1960 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
   1961 		static const uint8_t rates[] = {
   1962 			/* reverse function of zyd_plcp_signal() */
   1963 			2, 4, 11, 22, 0, 0, 0, 0,
   1964 			96, 48, 24, 12, 108, 72, 36, 18
   1965 		};
   1966 
   1967 		tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
   1968 		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
   1969 		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
   1970 		tap->wr_rssi = stat->rssi;
   1971 		tap->wr_rate = rates[plcp->signal & 0xf];
   1972 
   1973 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
   1974 	}
   1975 
   1976 	wh = mtod(m, struct ieee80211_frame *);
   1977 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1978 	ieee80211_input(ic, m, ni, stat->rssi, 0);
   1979 
   1980 	/* node is no longer needed */
   1981 	ieee80211_free_node(ni);
   1982 
   1983 	splx(s);
   1984 }
   1985 
   1986 Static void
   1987 zyd_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   1988 {
   1989 	struct zyd_rx_data *data = priv;
   1990 	struct zyd_softc *sc = data->sc;
   1991 	struct ifnet *ifp = &sc->sc_if;
   1992 	const struct zyd_rx_desc *desc;
   1993 	int len;
   1994 
   1995 	if (status != USBD_NORMAL_COMPLETION) {
   1996 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1997 			return;
   1998 
   1999 		if (status == USBD_STALLED)
   2000 			usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
   2001 
   2002 		goto skip;
   2003 	}
   2004 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   2005 
   2006 	if (len < ZYD_MIN_RXBUFSZ) {
   2007 		printf("%s: xfer too short (length=%d)\n",
   2008 		    device_xname(sc->sc_dev), len);
   2009 		ifp->if_ierrors++;
   2010 		goto skip;
   2011 	}
   2012 
   2013 	desc = (const struct zyd_rx_desc *)
   2014 	    (data->buf + len - sizeof(struct zyd_rx_desc));
   2015 
   2016 	if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
   2017 		const uint8_t *p = data->buf, *end = p + len;
   2018 		int i;
   2019 
   2020 		DPRINTFN(3, ("received multi-frame transfer\n"));
   2021 
   2022 		for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
   2023 			const uint16_t len16 = UGETW(desc->len[i]);
   2024 
   2025 			if (len16 == 0 || p + len16 > end)
   2026 				break;
   2027 
   2028 			zyd_rx_data(sc, p, len16);
   2029 			/* next frame is aligned on a 32-bit boundary */
   2030 			p += (len16 + 3) & ~3;
   2031 		}
   2032 	} else {
   2033 		DPRINTFN(3, ("received single-frame transfer\n"));
   2034 
   2035 		zyd_rx_data(sc, data->buf, len);
   2036 	}
   2037 
   2038 skip:	/* setup a new transfer */
   2039 
   2040 	usbd_setup_xfer(xfer, data, NULL, ZYX_MAX_RXBUFSZ, USBD_SHORT_XFER_OK,
   2041 	    USBD_NO_TIMEOUT, zyd_rxeof);
   2042 	(void)usbd_transfer(xfer);
   2043 }
   2044 
   2045 Static int
   2046 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   2047 {
   2048 	struct ieee80211com *ic = &sc->sc_ic;
   2049 	struct ifnet *ifp = &sc->sc_if;
   2050 	struct zyd_tx_desc *desc;
   2051 	struct zyd_tx_data *data;
   2052 	struct ieee80211_frame *wh;
   2053 	struct ieee80211_key *k;
   2054 	int xferlen, totlen, rate;
   2055 	uint16_t pktlen;
   2056 	usbd_status error;
   2057 
   2058 	data = &sc->tx_data[0];
   2059 	desc = (struct zyd_tx_desc *)data->buf;
   2060 
   2061 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
   2062 
   2063 	wh = mtod(m0, struct ieee80211_frame *);
   2064 
   2065 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2066 		k = ieee80211_crypto_encap(ic, ni, m0);
   2067 		if (k == NULL) {
   2068 			m_freem(m0);
   2069 			return ENOBUFS;
   2070 		}
   2071 	}
   2072 
   2073 	data->ni = ni;
   2074 
   2075 	wh = mtod(m0, struct ieee80211_frame *);
   2076 
   2077 	xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
   2078 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
   2079 
   2080 	/* fill Tx descriptor */
   2081 	desc->len = htole16(totlen);
   2082 
   2083 	desc->flags = ZYD_TX_FLAG_BACKOFF;
   2084 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2085 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
   2086 		if (totlen > ic->ic_rtsthreshold) {
   2087 			desc->flags |= ZYD_TX_FLAG_RTS;
   2088 		} else if (ZYD_RATE_IS_OFDM(rate) &&
   2089 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
   2090 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   2091 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
   2092 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   2093 				desc->flags |= ZYD_TX_FLAG_RTS;
   2094 		}
   2095 	} else
   2096 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
   2097 
   2098 	if ((wh->i_fc[0] &
   2099 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   2100 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
   2101 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
   2102 
   2103 	desc->phy = zyd_plcp_signal(rate);
   2104 	if (ZYD_RATE_IS_OFDM(rate)) {
   2105 		desc->phy |= ZYD_TX_PHY_OFDM;
   2106 		if (ic->ic_curmode == IEEE80211_MODE_11A)
   2107 			desc->phy |= ZYD_TX_PHY_5GHZ;
   2108 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   2109 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
   2110 
   2111 	/* actual transmit length (XXX why +10?) */
   2112 	pktlen = sizeof(struct zyd_tx_desc) + 10;
   2113 	if (sc->mac_rev == ZYD_ZD1211)
   2114 		pktlen += totlen;
   2115 	desc->pktlen = htole16(pktlen);
   2116 
   2117 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
   2118 	desc->plcp_service = 0;
   2119 	if (rate == 22) {
   2120 		const int remainder = (16 * totlen) % 22;
   2121 		if (remainder != 0 && remainder < 7)
   2122 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
   2123 	}
   2124 
   2125 	if (sc->sc_drvbpf != NULL) {
   2126 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
   2127 
   2128 		tap->wt_flags = 0;
   2129 		tap->wt_rate = rate;
   2130 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2131 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2132 
   2133 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
   2134 	}
   2135 
   2136 	m_copydata(m0, 0, m0->m_pkthdr.len,
   2137 	    data->buf + sizeof(struct zyd_tx_desc));
   2138 
   2139 	DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n",
   2140 	    device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
   2141 
   2142 	m_freem(m0);	/* mbuf no longer needed */
   2143 
   2144 	usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
   2145 	    USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof);
   2146 	error = usbd_transfer(data->xfer);
   2147 	if (error != USBD_IN_PROGRESS && error != 0) {
   2148 		ifp->if_oerrors++;
   2149 		return EIO;
   2150 	}
   2151 	sc->tx_queued++;
   2152 
   2153 	return 0;
   2154 }
   2155 
   2156 Static void
   2157 zyd_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
   2158 {
   2159 	struct zyd_tx_data *data = priv;
   2160 	struct zyd_softc *sc = data->sc;
   2161 	struct ifnet *ifp = &sc->sc_if;
   2162 	int s;
   2163 
   2164 	if (status != USBD_NORMAL_COMPLETION) {
   2165 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   2166 			return;
   2167 
   2168 		printf("%s: could not transmit buffer: %s\n",
   2169 		    device_xname(sc->sc_dev), usbd_errstr(status));
   2170 
   2171 		if (status == USBD_STALLED) {
   2172 			usbd_clear_endpoint_stall_async(
   2173 			    sc->zyd_ep[ZYD_ENDPT_BOUT]);
   2174 		}
   2175 		ifp->if_oerrors++;
   2176 		return;
   2177 	}
   2178 
   2179 	s = splnet();
   2180 
   2181 	/* update rate control statistics */
   2182 	((struct zyd_node *)data->ni)->amn.amn_txcnt++;
   2183 
   2184 	ieee80211_free_node(data->ni);
   2185 	data->ni = NULL;
   2186 
   2187 	sc->tx_queued--;
   2188 	ifp->if_opackets++;
   2189 
   2190 	sc->tx_timer = 0;
   2191 	ifp->if_flags &= ~IFF_OACTIVE;
   2192 	zyd_start(ifp);
   2193 
   2194 	splx(s);
   2195 }
   2196 
   2197 Static int
   2198 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   2199 {
   2200 	struct ieee80211com *ic = &sc->sc_ic;
   2201 	struct ifnet *ifp = &sc->sc_if;
   2202 	struct zyd_tx_desc *desc;
   2203 	struct zyd_tx_data *data;
   2204 	struct ieee80211_frame *wh;
   2205 	struct ieee80211_key *k;
   2206 	int xferlen, totlen, rate;
   2207 	uint16_t pktlen;
   2208 	usbd_status error;
   2209 
   2210 	wh = mtod(m0, struct ieee80211_frame *);
   2211 
   2212 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
   2213 		rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
   2214 	else
   2215 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   2216 	rate &= IEEE80211_RATE_VAL;
   2217 
   2218 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2219 		k = ieee80211_crypto_encap(ic, ni, m0);
   2220 		if (k == NULL) {
   2221 			m_freem(m0);
   2222 			return ENOBUFS;
   2223 		}
   2224 
   2225 		/* packet header may have moved, reset our local pointer */
   2226 		wh = mtod(m0, struct ieee80211_frame *);
   2227 	}
   2228 
   2229 	data = &sc->tx_data[0];
   2230 	desc = (struct zyd_tx_desc *)data->buf;
   2231 
   2232 	data->ni = ni;
   2233 
   2234 	xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
   2235 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
   2236 
   2237 	/* fill Tx descriptor */
   2238 	desc->len = htole16(totlen);
   2239 
   2240 	desc->flags = ZYD_TX_FLAG_BACKOFF;
   2241 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2242 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
   2243 		if (totlen > ic->ic_rtsthreshold) {
   2244 			desc->flags |= ZYD_TX_FLAG_RTS;
   2245 		} else if (ZYD_RATE_IS_OFDM(rate) &&
   2246 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
   2247 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   2248 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
   2249 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   2250 				desc->flags |= ZYD_TX_FLAG_RTS;
   2251 		}
   2252 	} else
   2253 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
   2254 
   2255 	if ((wh->i_fc[0] &
   2256 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   2257 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
   2258 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
   2259 
   2260 	desc->phy = zyd_plcp_signal(rate);
   2261 	if (ZYD_RATE_IS_OFDM(rate)) {
   2262 		desc->phy |= ZYD_TX_PHY_OFDM;
   2263 		if (ic->ic_curmode == IEEE80211_MODE_11A)
   2264 			desc->phy |= ZYD_TX_PHY_5GHZ;
   2265 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   2266 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
   2267 
   2268 	/* actual transmit length (XXX why +10?) */
   2269 	pktlen = sizeof(struct zyd_tx_desc) + 10;
   2270 	if (sc->mac_rev == ZYD_ZD1211)
   2271 		pktlen += totlen;
   2272 	desc->pktlen = htole16(pktlen);
   2273 
   2274 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
   2275 	desc->plcp_service = 0;
   2276 	if (rate == 22) {
   2277 		const int remainder = (16 * totlen) % 22;
   2278 		if (remainder != 0 && remainder < 7)
   2279 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
   2280 	}
   2281 
   2282 	if (sc->sc_drvbpf != NULL) {
   2283 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
   2284 
   2285 		tap->wt_flags = 0;
   2286 		tap->wt_rate = rate;
   2287 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2288 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2289 
   2290 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
   2291 	}
   2292 
   2293 	m_copydata(m0, 0, m0->m_pkthdr.len,
   2294 	    data->buf + sizeof(struct zyd_tx_desc));
   2295 
   2296 	DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n",
   2297 	    device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
   2298 
   2299 	m_freem(m0);	/* mbuf no longer needed */
   2300 
   2301 	usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
   2302 	    USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof);
   2303 	error = usbd_transfer(data->xfer);
   2304 	if (error != USBD_IN_PROGRESS && error != 0) {
   2305 		ifp->if_oerrors++;
   2306 		return EIO;
   2307 	}
   2308 	sc->tx_queued++;
   2309 
   2310 	return 0;
   2311 }
   2312 
   2313 Static void
   2314 zyd_start(struct ifnet *ifp)
   2315 {
   2316 	struct zyd_softc *sc = ifp->if_softc;
   2317 	struct ieee80211com *ic = &sc->sc_ic;
   2318 	struct ether_header *eh;
   2319 	struct ieee80211_node *ni;
   2320 	struct mbuf *m0;
   2321 
   2322 	for (;;) {
   2323 		IF_POLL(&ic->ic_mgtq, m0);
   2324 		if (m0 != NULL) {
   2325 			if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
   2326 				ifp->if_flags |= IFF_OACTIVE;
   2327 				break;
   2328 			}
   2329 			IF_DEQUEUE(&ic->ic_mgtq, m0);
   2330 
   2331 			ni = M_GETCTX(m0, struct ieee80211_node *);
   2332 			M_CLEARCTX(m0);
   2333 			bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
   2334 			if (zyd_tx_mgt(sc, m0, ni) != 0)
   2335 				break;
   2336 		} else {
   2337 			if (ic->ic_state != IEEE80211_S_RUN)
   2338 				break;
   2339 			IFQ_POLL(&ifp->if_snd, m0);
   2340 			if (m0 == NULL)
   2341 				break;
   2342 			if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
   2343 				ifp->if_flags |= IFF_OACTIVE;
   2344 				break;
   2345 			}
   2346 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   2347 
   2348 			if (m0->m_len < sizeof(struct ether_header) &&
   2349 			    !(m0 = m_pullup(m0, sizeof(struct ether_header))))
   2350 				continue;
   2351 
   2352 			eh = mtod(m0, struct ether_header *);
   2353 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   2354 			if (ni == NULL) {
   2355 				m_freem(m0);
   2356 				continue;
   2357 			}
   2358 			bpf_mtap(ifp, m0, BPF_D_OUT);
   2359 			if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
   2360 				ieee80211_free_node(ni);
   2361 				ifp->if_oerrors++;
   2362 				continue;
   2363 			}
   2364 			bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
   2365 			if (zyd_tx_data(sc, m0, ni) != 0) {
   2366 				ieee80211_free_node(ni);
   2367 				ifp->if_oerrors++;
   2368 				break;
   2369 			}
   2370 		}
   2371 
   2372 		sc->tx_timer = 5;
   2373 		ifp->if_timer = 1;
   2374 	}
   2375 }
   2376 
   2377 Static void
   2378 zyd_watchdog(struct ifnet *ifp)
   2379 {
   2380 	struct zyd_softc *sc = ifp->if_softc;
   2381 	struct ieee80211com *ic = &sc->sc_ic;
   2382 
   2383 	ifp->if_timer = 0;
   2384 
   2385 	if (sc->tx_timer > 0) {
   2386 		if (--sc->tx_timer == 0) {
   2387 			printf("%s: device timeout\n", device_xname(sc->sc_dev));
   2388 			/* zyd_init(ifp); XXX needs a process context ? */
   2389 			ifp->if_oerrors++;
   2390 			return;
   2391 		}
   2392 		ifp->if_timer = 1;
   2393 	}
   2394 
   2395 	ieee80211_watchdog(ic);
   2396 }
   2397 
   2398 Static int
   2399 zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2400 {
   2401 	struct zyd_softc *sc = ifp->if_softc;
   2402 	struct ieee80211com *ic = &sc->sc_ic;
   2403 	int s, error = 0;
   2404 
   2405 	s = splnet();
   2406 
   2407 	switch (cmd) {
   2408 	case SIOCSIFFLAGS:
   2409 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   2410 			break;
   2411 		/* XXX re-use ether_ioctl() */
   2412 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   2413 		case IFF_UP:
   2414 			zyd_init(ifp);
   2415 			break;
   2416 		case IFF_RUNNING:
   2417 			zyd_stop(ifp, 1);
   2418 			break;
   2419 		default:
   2420 			break;
   2421 		}
   2422 		break;
   2423 
   2424 	default:
   2425 		error = ieee80211_ioctl(ic, cmd, data);
   2426 	}
   2427 
   2428 	if (error == ENETRESET) {
   2429 		if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
   2430 		    (IFF_RUNNING | IFF_UP))
   2431 			zyd_init(ifp);
   2432 		error = 0;
   2433 	}
   2434 
   2435 	splx(s);
   2436 
   2437 	return error;
   2438 }
   2439 
   2440 Static int
   2441 zyd_init(struct ifnet *ifp)
   2442 {
   2443 	struct zyd_softc *sc = ifp->if_softc;
   2444 	struct ieee80211com *ic = &sc->sc_ic;
   2445 	int i, error;
   2446 
   2447 	zyd_stop(ifp, 0);
   2448 
   2449 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   2450 	DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
   2451 	error = zyd_set_macaddr(sc, ic->ic_myaddr);
   2452 	if (error != 0)
   2453 		return error;
   2454 
   2455 	/* we'll do software WEP decryption for now */
   2456 	DPRINTF(("setting encryption type\n"));
   2457 	error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
   2458 	if (error != 0)
   2459 		return error;
   2460 
   2461 	/* promiscuous mode */
   2462 	(void)zyd_write32(sc, ZYD_MAC_SNIFFER,
   2463 	    (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
   2464 
   2465 	(void)zyd_set_rxfilter(sc);
   2466 
   2467 	/* switch radio transmitter ON */
   2468 	(void)zyd_switch_radio(sc, 1);
   2469 
   2470 	/* set basic rates */
   2471 	if (ic->ic_curmode == IEEE80211_MODE_11B)
   2472 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
   2473 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
   2474 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
   2475 	else	/* assumes 802.11b/g */
   2476 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
   2477 
   2478 	/* set mandatory rates */
   2479 	if (ic->ic_curmode == IEEE80211_MODE_11B)
   2480 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
   2481 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
   2482 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
   2483 	else	/* assumes 802.11b/g */
   2484 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
   2485 
   2486 	/* set default BSS channel */
   2487 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   2488 	zyd_set_chan(sc, ic->ic_bss->ni_chan);
   2489 
   2490 	/* enable interrupts */
   2491 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
   2492 
   2493 	/*
   2494 	 * Allocate Tx and Rx xfer queues.
   2495 	 */
   2496 	if ((error = zyd_alloc_tx_list(sc)) != 0) {
   2497 		printf("%s: could not allocate Tx list\n",
   2498 		    device_xname(sc->sc_dev));
   2499 		goto fail;
   2500 	}
   2501 	if ((error = zyd_alloc_rx_list(sc)) != 0) {
   2502 		printf("%s: could not allocate Rx list\n",
   2503 		    device_xname(sc->sc_dev));
   2504 		goto fail;
   2505 	}
   2506 
   2507 	/*
   2508 	 * Start up the receive pipe.
   2509 	 */
   2510 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
   2511 		struct zyd_rx_data *data = &sc->rx_data[i];
   2512 
   2513 		usbd_setup_xfer(data->xfer, data, NULL, ZYX_MAX_RXBUFSZ,
   2514 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, zyd_rxeof);
   2515 		error = usbd_transfer(data->xfer);
   2516 		if (error != USBD_IN_PROGRESS && error != 0) {
   2517 			printf("%s: could not queue Rx transfer\n",
   2518 			    device_xname(sc->sc_dev));
   2519 			goto fail;
   2520 		}
   2521 	}
   2522 
   2523 	ifp->if_flags &= ~IFF_OACTIVE;
   2524 	ifp->if_flags |= IFF_RUNNING;
   2525 
   2526 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2527 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2528 	else
   2529 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2530 
   2531 	return 0;
   2532 
   2533 fail:	zyd_stop(ifp, 1);
   2534 	return error;
   2535 }
   2536 
   2537 Static void
   2538 zyd_stop(struct ifnet *ifp, int disable)
   2539 {
   2540 	struct zyd_softc *sc = ifp->if_softc;
   2541 	struct ieee80211com *ic = &sc->sc_ic;
   2542 
   2543 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
   2544 
   2545 	sc->tx_timer = 0;
   2546 	ifp->if_timer = 0;
   2547 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2548 
   2549 	/* switch radio transmitter OFF */
   2550 	(void)zyd_switch_radio(sc, 0);
   2551 
   2552 	/* disable Rx */
   2553 	(void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
   2554 
   2555 	/* disable interrupts */
   2556 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
   2557 
   2558 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
   2559 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
   2560 
   2561 	zyd_free_rx_list(sc);
   2562 	zyd_free_tx_list(sc);
   2563 }
   2564 
   2565 Static int
   2566 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
   2567 {
   2568 	usb_device_request_t req;
   2569 	uint16_t addr;
   2570 	uint8_t stat;
   2571 
   2572 	DPRINTF(("firmware size=%zu\n", size));
   2573 
   2574 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
   2575 	req.bRequest = ZYD_DOWNLOADREQ;
   2576 	USETW(req.wIndex, 0);
   2577 
   2578 	addr = ZYD_FIRMWARE_START_ADDR;
   2579 	while (size > 0) {
   2580 #if 0
   2581 		const int mlen = uimin(size, 4096);
   2582 #else
   2583 		/*
   2584 		 * XXXX: When the transfer size is 4096 bytes, it is not
   2585 		 * likely to be able to transfer it.
   2586 		 * The cause is port or machine or chip?
   2587 		 */
   2588 		const int mlen = uimin(size, 64);
   2589 #endif
   2590 
   2591 		DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
   2592 		    addr));
   2593 
   2594 		USETW(req.wValue, addr);
   2595 		USETW(req.wLength, mlen);
   2596 		if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
   2597 			return EIO;
   2598 
   2599 		addr += mlen / 2;
   2600 		fw   += mlen;
   2601 		size -= mlen;
   2602 	}
   2603 
   2604 	/* check whether the upload succeeded */
   2605 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   2606 	req.bRequest = ZYD_DOWNLOADSTS;
   2607 	USETW(req.wValue, 0);
   2608 	USETW(req.wIndex, 0);
   2609 	USETW(req.wLength, sizeof(stat));
   2610 	if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
   2611 		return EIO;
   2612 
   2613 	return (stat & 0x80) ? EIO : 0;
   2614 }
   2615 
   2616 Static void
   2617 zyd_iter_func(void *arg, struct ieee80211_node *ni)
   2618 {
   2619 	struct zyd_softc *sc = arg;
   2620 	struct zyd_node *zn = (struct zyd_node *)ni;
   2621 
   2622 	ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
   2623 }
   2624 
   2625 Static void
   2626 zyd_amrr_timeout(void *arg)
   2627 {
   2628 	struct zyd_softc *sc = arg;
   2629 	struct ieee80211com *ic = &sc->sc_ic;
   2630 	int s;
   2631 
   2632 	s = splnet();
   2633 	if (ic->ic_opmode == IEEE80211_M_STA)
   2634 		zyd_iter_func(sc, ic->ic_bss);
   2635 	else
   2636 		ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc);
   2637 	splx(s);
   2638 
   2639 	callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
   2640 }
   2641 
   2642 Static void
   2643 zyd_newassoc(struct ieee80211_node *ni, int isnew)
   2644 {
   2645 	struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc;
   2646 	int i;
   2647 
   2648 	ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
   2649 
   2650 	/* set rate to some reasonable initial value */
   2651 	for (i = ni->ni_rates.rs_nrates - 1;
   2652 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
   2653 	     i--);
   2654 	ni->ni_txrate = i;
   2655 }
   2656 
   2657 static int
   2658 zyd_activate(device_t self, enum devact act)
   2659 {
   2660 	struct zyd_softc *sc = device_private(self);
   2661 
   2662 	switch (act) {
   2663 	case DVACT_DEACTIVATE:
   2664 		if_deactivate(&sc->sc_if);
   2665 		return 0;
   2666 	default:
   2667 		return EOPNOTSUPP;
   2668 	}
   2669 }
   2670