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