Home | History | Annotate | Line # | Download | only in usb
if_zyd.c revision 1.8
      1 /*	$OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $	*/
      2 /*	$NetBSD: if_zyd.c,v 1.8 2007/07/16 06:56:23 nisimura 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.8 2007/07/16 06:56:23 nisimura 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 	zyd_free_rx_list(sc);
    473 	zyd_free_tx_list(sc);
    474 
    475 	sc->attached = 0;
    476 
    477 #if NBPFILTER > 0
    478 	bpfdetach(ifp);
    479 #endif
    480 	ieee80211_ifdetach(ic);
    481 	if_detach(ifp);
    482 
    483 	splx(s);
    484 
    485 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    486 	    USBDEV(sc->sc_dev));
    487 
    488 	return 0;
    489 }
    490 
    491 Static int
    492 zyd_open_pipes(struct zyd_softc *sc)
    493 {
    494 	usb_endpoint_descriptor_t *edesc;
    495 	int isize;
    496 	usbd_status error;
    497 
    498 	/* interrupt in */
    499 	edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
    500 	if (edesc == NULL)
    501 		return EINVAL;
    502 
    503 	isize = UGETW(edesc->wMaxPacketSize);
    504 	if (isize == 0)	/* should not happen */
    505 		return EINVAL;
    506 
    507 	sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
    508 	if (sc->ibuf == NULL)
    509 		return ENOMEM;
    510 
    511 	error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
    512 	    &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr,
    513 	    USBD_DEFAULT_INTERVAL);
    514 	if (error != 0) {
    515 		printf("%s: open rx intr pipe failed: %s\n",
    516 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
    517 		goto fail;
    518 	}
    519 
    520 	/* interrupt out (not necessarily an interrupt pipe) */
    521 	error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
    522 	    &sc->zyd_ep[ZYD_ENDPT_IOUT]);
    523 	if (error != 0) {
    524 		printf("%s: open tx intr pipe failed: %s\n",
    525 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
    526 		goto fail;
    527 	}
    528 
    529 	/* bulk in */
    530 	error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
    531 	    &sc->zyd_ep[ZYD_ENDPT_BIN]);
    532 	if (error != 0) {
    533 		printf("%s: open rx pipe failed: %s\n",
    534 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
    535 		goto fail;
    536 	}
    537 
    538 	/* bulk out */
    539 	error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
    540 	    &sc->zyd_ep[ZYD_ENDPT_BOUT]);
    541 	if (error != 0) {
    542 		printf("%s: open tx pipe failed: %s\n",
    543 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
    544 		goto fail;
    545 	}
    546 
    547 	return 0;
    548 
    549 fail:	zyd_close_pipes(sc);
    550 	return error;
    551 }
    552 
    553 Static void
    554 zyd_close_pipes(struct zyd_softc *sc)
    555 {
    556 	int i;
    557 
    558 	for (i = 0; i < ZYD_ENDPT_CNT; i++) {
    559 		if (sc->zyd_ep[i] != NULL) {
    560 			usbd_abort_pipe(sc->zyd_ep[i]);
    561 			usbd_close_pipe(sc->zyd_ep[i]);
    562 			sc->zyd_ep[i] = NULL;
    563 		}
    564 	}
    565 	if (sc->ibuf != NULL) {
    566 		free(sc->ibuf, M_USBDEV);
    567 		sc->ibuf = NULL;
    568 	}
    569 }
    570 
    571 Static int
    572 zyd_alloc_tx_list(struct zyd_softc *sc)
    573 {
    574 	int i, error;
    575 
    576 	sc->tx_queued = 0;
    577 
    578 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
    579 		struct zyd_tx_data *data = &sc->tx_data[i];
    580 
    581 		data->sc = sc;	/* backpointer for callbacks */
    582 
    583 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
    584 		if (data->xfer == NULL) {
    585 			printf("%s: could not allocate tx xfer\n",
    586 			    USBDEVNAME(sc->sc_dev));
    587 			error = ENOMEM;
    588 			goto fail;
    589 		}
    590 		data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ);
    591 		if (data->buf == NULL) {
    592 			printf("%s: could not allocate tx buffer\n",
    593 			    USBDEVNAME(sc->sc_dev));
    594 			error = ENOMEM;
    595 			goto fail;
    596 		}
    597 
    598 		/* clear Tx descriptor */
    599 		bzero(data->buf, sizeof (struct zyd_tx_desc));
    600 	}
    601 	return 0;
    602 
    603 fail:	zyd_free_tx_list(sc);
    604 	return error;
    605 }
    606 
    607 Static void
    608 zyd_free_tx_list(struct zyd_softc *sc)
    609 {
    610 	int i;
    611 
    612 	for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
    613 		struct zyd_tx_data *data = &sc->tx_data[i];
    614 
    615 		if (data->xfer != NULL) {
    616 			usbd_free_xfer(data->xfer);
    617 			data->xfer = NULL;
    618 		}
    619 		if (data->ni != NULL) {
    620 			ieee80211_free_node(data->ni);
    621 			data->ni = NULL;
    622 		}
    623 	}
    624 }
    625 
    626 Static int
    627 zyd_alloc_rx_list(struct zyd_softc *sc)
    628 {
    629 	int i, error;
    630 
    631 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
    632 		struct zyd_rx_data *data = &sc->rx_data[i];
    633 
    634 		data->sc = sc;	/* backpointer for callbacks */
    635 
    636 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
    637 		if (data->xfer == NULL) {
    638 			printf("%s: could not allocate rx xfer\n",
    639 			    USBDEVNAME(sc->sc_dev));
    640 			error = ENOMEM;
    641 			goto fail;
    642 		}
    643 		data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ);
    644 		if (data->buf == NULL) {
    645 			printf("%s: could not allocate rx buffer\n",
    646 			    USBDEVNAME(sc->sc_dev));
    647 			error = ENOMEM;
    648 			goto fail;
    649 		}
    650 	}
    651 	return 0;
    652 
    653 fail:	zyd_free_rx_list(sc);
    654 	return error;
    655 }
    656 
    657 Static void
    658 zyd_free_rx_list(struct zyd_softc *sc)
    659 {
    660 	int i;
    661 
    662 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
    663 		struct zyd_rx_data *data = &sc->rx_data[i];
    664 
    665 		if (data->xfer != NULL) {
    666 			usbd_free_xfer(data->xfer);
    667 			data->xfer = NULL;
    668 		}
    669 	}
    670 }
    671 
    672 /* ARGUSED */
    673 Static struct ieee80211_node *
    674 zyd_node_alloc(struct ieee80211_node_table *nt __unused)
    675 {
    676 	struct zyd_node *zn;
    677 
    678 	zn = malloc(sizeof (struct zyd_node), M_DEVBUF, M_NOWAIT);
    679 	if (zn != NULL)
    680 		bzero(zn, sizeof (struct zyd_node));
    681 	return (struct ieee80211_node *)zn;
    682 }
    683 
    684 Static int
    685 zyd_media_change(struct ifnet *ifp)
    686 {
    687 	int error;
    688 
    689 	error = ieee80211_media_change(ifp);
    690 	if (error != ENETRESET)
    691 		return error;
    692 
    693 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    694 		zyd_init(ifp);
    695 
    696 	return 0;
    697 }
    698 
    699 /*
    700  * This function is called periodically (every 200ms) during scanning to
    701  * switch from one channel to another.
    702  */
    703 Static void
    704 zyd_next_scan(void *arg)
    705 {
    706 	struct zyd_softc *sc = arg;
    707 	struct ieee80211com *ic = &sc->sc_ic;
    708 
    709 	if (ic->ic_state == IEEE80211_S_SCAN)
    710 		ieee80211_next_scan(ic);
    711 }
    712 
    713 Static void
    714 zyd_task(void *arg)
    715 {
    716 	struct zyd_softc *sc = arg;
    717 	struct ieee80211com *ic = &sc->sc_ic;
    718 	enum ieee80211_state ostate;
    719 
    720 	ostate = ic->ic_state;
    721 
    722 	switch (sc->sc_state) {
    723 	case IEEE80211_S_INIT:
    724 		if (ostate == IEEE80211_S_RUN) {
    725 			/* turn link LED off */
    726 			zyd_set_led(sc, ZYD_LED1, 0);
    727 
    728 			/* stop data LED from blinking */
    729 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
    730 		}
    731 		break;
    732 
    733 	case IEEE80211_S_SCAN:
    734 		zyd_set_chan(sc, ic->ic_curchan);
    735 		usb_callout(sc->sc_scan_ch, hz / 5, zyd_next_scan, sc);
    736 		break;
    737 
    738 	case IEEE80211_S_AUTH:
    739 	case IEEE80211_S_ASSOC:
    740 		zyd_set_chan(sc, ic->ic_curchan);
    741 		break;
    742 
    743 	case IEEE80211_S_RUN:
    744 	{
    745 		struct ieee80211_node *ni = ic->ic_bss;
    746 
    747 		zyd_set_chan(sc, ic->ic_curchan);
    748 
    749 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
    750 			/* turn link LED on */
    751 			zyd_set_led(sc, ZYD_LED1, 1);
    752 
    753 			/* make data LED blink upon Tx */
    754 			zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
    755 
    756 			zyd_set_bssid(sc, ni->ni_bssid);
    757 		}
    758 
    759 		if (ic->ic_opmode == IEEE80211_M_STA) {
    760 			/* fake a join to init the tx rate */
    761 			zyd_newassoc(ni, 1);
    762 		}
    763 
    764 		/* start automatic rate control timer */
    765 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
    766 			usb_callout(sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
    767 
    768 		break;
    769 	}
    770 	}
    771 
    772 	sc->sc_newstate(ic, sc->sc_state, -1);
    773 }
    774 
    775 Static int
    776 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    777 {
    778 	struct zyd_softc *sc = ic->ic_ifp->if_softc;
    779 
    780 	usb_rem_task(sc->sc_udev, &sc->sc_task);
    781 	usb_uncallout(sc->sc_scan_ch, zyd_next_scan, sc);
    782 	usb_uncallout(sc->sc_amrr_ch, zyd_amrr_timeout, sc);
    783 
    784 	/* do it in a process context */
    785 	sc->sc_state = nstate;
    786 	usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
    787 
    788 	return 0;
    789 }
    790 
    791 Static int
    792 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
    793     void *odata, int olen, u_int flags)
    794 {
    795 	usbd_xfer_handle xfer;
    796 	struct zyd_cmd cmd;
    797 	struct rq rq;
    798 	uint16_t xferflags;
    799 	usbd_status error;
    800 	int s = 0;
    801 
    802 	if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
    803 		return ENOMEM;
    804 
    805 	cmd.code = htole16(code);
    806 	bcopy(idata, cmd.data, ilen);
    807 
    808 	xferflags = USBD_FORCE_SHORT_XFER;
    809 	if (!(flags & ZYD_CMD_FLAG_READ))
    810 		xferflags |= USBD_SYNCHRONOUS;
    811 	else {
    812 		s = splusb();
    813 		rq.idata = idata;
    814 		rq.odata = odata;
    815 		rq.len = olen / sizeof (struct zyd_pair);
    816 		SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
    817 	}
    818 
    819 	usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
    820 	    sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
    821 	error = usbd_transfer(xfer);
    822 	if (error != USBD_IN_PROGRESS && error != 0) {
    823 		if (flags & ZYD_CMD_FLAG_READ)
    824 			splx(s);
    825 		printf("%s: could not send command (error=%s)\n",
    826 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
    827 		(void)usbd_free_xfer(xfer);
    828 		return EIO;
    829 	}
    830 	if (!(flags & ZYD_CMD_FLAG_READ)) {
    831 		(void)usbd_free_xfer(xfer);
    832 		return 0;	/* write: don't wait for reply */
    833 	}
    834 	/* wait at most one second for command reply */
    835 	error = tsleep(odata, PCATCH, "zydcmd", hz);
    836 	if (error == EWOULDBLOCK)
    837 		printf("%s: zyd_read sleep timeout\n", USBDEVNAME(sc->sc_dev));
    838 	SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
    839 	splx(s);
    840 
    841 	(void)usbd_free_xfer(xfer);
    842 	return error;
    843 }
    844 
    845 Static int
    846 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
    847 {
    848 	struct zyd_pair tmp;
    849 	int error;
    850 
    851 	reg = htole16(reg);
    852 	error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof reg, &tmp, sizeof tmp,
    853 	    ZYD_CMD_FLAG_READ);
    854 	if (error == 0)
    855 		*val = le16toh(tmp.val);
    856 	return error;
    857 }
    858 
    859 Static int
    860 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
    861 {
    862 	struct zyd_pair tmp[2];
    863 	uint16_t regs[2];
    864 	int error;
    865 
    866 	regs[0] = htole16(ZYD_REG32_HI(reg));
    867 	regs[1] = htole16(ZYD_REG32_LO(reg));
    868 	error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp,
    869 	    ZYD_CMD_FLAG_READ);
    870 	if (error == 0)
    871 		*val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
    872 	return error;
    873 }
    874 
    875 Static int
    876 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
    877 {
    878 	struct zyd_pair pair;
    879 
    880 	pair.reg = htole16(reg);
    881 	pair.val = htole16(val);
    882 
    883 	return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0);
    884 }
    885 
    886 Static int
    887 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
    888 {
    889 	struct zyd_pair pair[2];
    890 
    891 	pair[0].reg = htole16(ZYD_REG32_HI(reg));
    892 	pair[0].val = htole16(val >> 16);
    893 	pair[1].reg = htole16(ZYD_REG32_LO(reg));
    894 	pair[1].val = htole16(val & 0xffff);
    895 
    896 	return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0);
    897 }
    898 
    899 Static int
    900 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
    901 {
    902 	struct zyd_rf *rf = &sc->sc_rf;
    903 	struct zyd_rfwrite req;
    904 	uint16_t cr203;
    905 	int i;
    906 
    907 	(void)zyd_read16(sc, ZYD_CR203, &cr203);
    908 	cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
    909 
    910 	req.code  = htole16(2);
    911 	req.width = htole16(rf->width);
    912 	for (i = 0; i < rf->width; i++) {
    913 		req.bit[i] = htole16(cr203);
    914 		if (val & (1 << (rf->width - 1 - i)))
    915 			req.bit[i] |= htole16(ZYD_RF_DATA);
    916 	}
    917 	return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
    918 }
    919 
    920 Static void
    921 zyd_lock_phy(struct zyd_softc *sc)
    922 {
    923 	uint32_t tmp;
    924 
    925 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
    926 	tmp &= ~ZYD_UNLOCK_PHY_REGS;
    927 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
    928 }
    929 
    930 Static void
    931 zyd_unlock_phy(struct zyd_softc *sc)
    932 {
    933 	uint32_t tmp;
    934 
    935 	(void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
    936 	tmp |= ZYD_UNLOCK_PHY_REGS;
    937 	(void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
    938 }
    939 
    940 /*
    941  * RFMD RF methods.
    942  */
    943 Static int
    944 zyd_rfmd_init(struct zyd_rf *rf)
    945 {
    946 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
    947 	struct zyd_softc *sc = rf->rf_sc;
    948 	static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
    949 	static const uint32_t rfini[] = ZYD_RFMD_RF;
    950 	int i, error;
    951 
    952 	/* init RF-dependent PHY registers */
    953 	for (i = 0; i < N(phyini); i++) {
    954 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
    955 		if (error != 0)
    956 			return error;
    957 	}
    958 
    959 	/* init RFMD radio */
    960 	for (i = 0; i < N(rfini); i++) {
    961 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
    962 			return error;
    963 	}
    964 	return 0;
    965 #undef N
    966 }
    967 
    968 Static int
    969 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
    970 {
    971 	struct zyd_softc *sc = rf->rf_sc;
    972 
    973 	(void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
    974 	(void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
    975 
    976 	return 0;
    977 }
    978 
    979 Static int
    980 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
    981 {
    982 	struct zyd_softc *sc = rf->rf_sc;
    983 	static const struct {
    984 		uint32_t	r1, r2;
    985 	} rfprog[] = ZYD_RFMD_CHANTABLE;
    986 
    987 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
    988 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
    989 
    990 	return 0;
    991 }
    992 
    993 /*
    994  * AL2230 RF methods.
    995  */
    996 Static int
    997 zyd_al2230_init(struct zyd_rf *rf)
    998 {
    999 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1000 	struct zyd_softc *sc = rf->rf_sc;
   1001 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
   1002 	static const uint32_t rfini[] = ZYD_AL2230_RF;
   1003 	int i, error;
   1004 
   1005 	/* init RF-dependent PHY registers */
   1006 	for (i = 0; i < N(phyini); i++) {
   1007 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1008 		if (error != 0)
   1009 			return error;
   1010 	}
   1011 
   1012 	/* init AL2230 radio */
   1013 	for (i = 0; i < N(rfini); i++) {
   1014 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1015 			return error;
   1016 	}
   1017 	return 0;
   1018 #undef N
   1019 }
   1020 
   1021 Static int
   1022 zyd_al2230_init_b(struct zyd_rf *rf)
   1023 {
   1024 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1025 	struct zyd_softc *sc = rf->rf_sc;
   1026 	static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
   1027 	static const uint32_t rfini[] = ZYD_AL2230_RF_B;
   1028 	int i, error;
   1029 
   1030 	/* init RF-dependent PHY registers */
   1031 	for (i = 0; i < N(phyini); i++) {
   1032 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1033 		if (error != 0)
   1034 			return error;
   1035 	}
   1036 
   1037 	/* init AL2230 radio */
   1038 	for (i = 0; i < N(rfini); i++) {
   1039 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1040 			return error;
   1041 	}
   1042 	return 0;
   1043 #undef N
   1044 }
   1045 
   1046 Static int
   1047 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
   1048 {
   1049 	struct zyd_softc *sc = rf->rf_sc;
   1050 	int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
   1051 
   1052 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
   1053 	(void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
   1054 
   1055 	return 0;
   1056 }
   1057 
   1058 Static int
   1059 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
   1060 {
   1061 	struct zyd_softc *sc = rf->rf_sc;
   1062 	static const struct {
   1063 		uint32_t	r1, r2, r3;
   1064 	} rfprog[] = ZYD_AL2230_CHANTABLE;
   1065 
   1066 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1067 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1068 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
   1069 
   1070 	(void)zyd_write16(sc, ZYD_CR138, 0x28);
   1071 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
   1072 
   1073 	return 0;
   1074 }
   1075 
   1076 /*
   1077  * AL7230B RF methods.
   1078  */
   1079 Static int
   1080 zyd_al7230B_init(struct zyd_rf *rf)
   1081 {
   1082 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1083 	struct zyd_softc *sc = rf->rf_sc;
   1084 	static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
   1085 	static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
   1086 	static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
   1087 	static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
   1088 	static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
   1089 	int i, error;
   1090 
   1091 	/* for AL7230B, PHY and RF need to be initialized in "phases" */
   1092 
   1093 	/* init RF-dependent PHY registers, part one */
   1094 	for (i = 0; i < N(phyini_1); i++) {
   1095 		error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
   1096 		if (error != 0)
   1097 			return error;
   1098 	}
   1099 	/* init AL7230B radio, part one */
   1100 	for (i = 0; i < N(rfini_1); i++) {
   1101 		if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
   1102 			return error;
   1103 	}
   1104 	/* init RF-dependent PHY registers, part two */
   1105 	for (i = 0; i < N(phyini_2); i++) {
   1106 		error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
   1107 		if (error != 0)
   1108 			return error;
   1109 	}
   1110 	/* init AL7230B radio, part two */
   1111 	for (i = 0; i < N(rfini_2); i++) {
   1112 		if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
   1113 			return error;
   1114 	}
   1115 	/* init RF-dependent PHY registers, part three */
   1116 	for (i = 0; i < N(phyini_3); i++) {
   1117 		error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
   1118 		if (error != 0)
   1119 			return error;
   1120 	}
   1121 
   1122 	return 0;
   1123 #undef N
   1124 }
   1125 
   1126 Static int
   1127 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
   1128 {
   1129 	struct zyd_softc *sc = rf->rf_sc;
   1130 
   1131 	(void)zyd_write16(sc, ZYD_CR11,  on ? 0x00 : 0x04);
   1132 	(void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
   1133 
   1134 	return 0;
   1135 }
   1136 
   1137 Static int
   1138 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
   1139 {
   1140 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1141 	struct zyd_softc *sc = rf->rf_sc;
   1142 	static const struct {
   1143 		uint32_t	r1, r2;
   1144 	} rfprog[] = ZYD_AL7230B_CHANTABLE;
   1145 	static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
   1146 	int i, error;
   1147 
   1148 	(void)zyd_write16(sc, ZYD_CR240, 0x57);
   1149 	(void)zyd_write16(sc, ZYD_CR251, 0x2f);
   1150 
   1151 	for (i = 0; i < N(rfsc); i++) {
   1152 		if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
   1153 			return error;
   1154 	}
   1155 
   1156 	(void)zyd_write16(sc, ZYD_CR128, 0x14);
   1157 	(void)zyd_write16(sc, ZYD_CR129, 0x12);
   1158 	(void)zyd_write16(sc, ZYD_CR130, 0x10);
   1159 	(void)zyd_write16(sc, ZYD_CR38,  0x38);
   1160 	(void)zyd_write16(sc, ZYD_CR136, 0xdf);
   1161 
   1162 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1163 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1164 	(void)zyd_rfwrite(sc, 0x3c9000);
   1165 
   1166 	(void)zyd_write16(sc, ZYD_CR251, 0x3f);
   1167 	(void)zyd_write16(sc, ZYD_CR203, 0x06);
   1168 	(void)zyd_write16(sc, ZYD_CR240, 0x08);
   1169 
   1170 	return 0;
   1171 #undef N
   1172 }
   1173 
   1174 /*
   1175  * AL2210 RF methods.
   1176  */
   1177 Static int
   1178 zyd_al2210_init(struct zyd_rf *rf)
   1179 {
   1180 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1181 	struct zyd_softc *sc = rf->rf_sc;
   1182 	static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
   1183 	static const uint32_t rfini[] = ZYD_AL2210_RF;
   1184 	uint32_t tmp;
   1185 	int i, error;
   1186 
   1187 	(void)zyd_write32(sc, ZYD_CR18, 2);
   1188 
   1189 	/* init RF-dependent PHY registers */
   1190 	for (i = 0; i < N(phyini); i++) {
   1191 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1192 		if (error != 0)
   1193 			return error;
   1194 	}
   1195 	/* init AL2210 radio */
   1196 	for (i = 0; i < N(rfini); i++) {
   1197 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1198 			return error;
   1199 	}
   1200 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1201 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
   1202 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
   1203 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
   1204 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
   1205 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
   1206 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1207 	(void)zyd_write32(sc, ZYD_CR18, 3);
   1208 
   1209 	return 0;
   1210 #undef N
   1211 }
   1212 
   1213 Static int
   1214 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
   1215 {
   1216 	/* vendor driver does nothing for this RF chip */
   1217 
   1218 	return 0;
   1219 }
   1220 
   1221 Static int
   1222 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
   1223 {
   1224 	struct zyd_softc *sc = rf->rf_sc;
   1225 	static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
   1226 	uint32_t tmp;
   1227 
   1228 	(void)zyd_write32(sc, ZYD_CR18, 2);
   1229 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1230 	(void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
   1231 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
   1232 	(void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
   1233 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
   1234 
   1235 	(void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
   1236 	(void)zyd_write16(sc, ZYD_CR47, 0x1e);
   1237 
   1238 	/* actually set the channel */
   1239 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
   1240 
   1241 	(void)zyd_write32(sc, ZYD_CR18, 3);
   1242 
   1243 	return 0;
   1244 }
   1245 
   1246 /*
   1247  * GCT RF methods.
   1248  */
   1249 Static int
   1250 zyd_gct_init(struct zyd_rf *rf)
   1251 {
   1252 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1253 	struct zyd_softc *sc = rf->rf_sc;
   1254 	static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
   1255 	static const uint32_t rfini[] = ZYD_GCT_RF;
   1256 	int i, error;
   1257 
   1258 	/* init RF-dependent PHY registers */
   1259 	for (i = 0; i < N(phyini); i++) {
   1260 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1261 		if (error != 0)
   1262 			return error;
   1263 	}
   1264 	/* init cgt radio */
   1265 	for (i = 0; i < N(rfini); i++) {
   1266 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1267 			return error;
   1268 	}
   1269 	return 0;
   1270 #undef N
   1271 }
   1272 
   1273 Static int
   1274 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
   1275 {
   1276 	/* vendor driver does nothing for this RF chip */
   1277 
   1278 	return 0;
   1279 }
   1280 
   1281 Static int
   1282 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
   1283 {
   1284 	struct zyd_softc *sc = rf->rf_sc;
   1285 	static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
   1286 
   1287 	(void)zyd_rfwrite(sc, 0x1c0000);
   1288 	(void)zyd_rfwrite(sc, rfprog[chan - 1]);
   1289 	(void)zyd_rfwrite(sc, 0x1c0008);
   1290 
   1291 	return 0;
   1292 }
   1293 
   1294 /*
   1295  * Maxim RF methods.
   1296  */
   1297 Static int
   1298 zyd_maxim_init(struct zyd_rf *rf)
   1299 {
   1300 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   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 i, error;
   1306 
   1307 	/* init RF-dependent PHY registers */
   1308 	for (i = 0; i < N(phyini); i++) {
   1309 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1310 		if (error != 0)
   1311 			return error;
   1312 	}
   1313 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1314 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1315 
   1316 	/* init maxim radio */
   1317 	for (i = 0; i < N(rfini); i++) {
   1318 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1319 			return error;
   1320 	}
   1321 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1322 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1323 
   1324 	return 0;
   1325 #undef N
   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 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1340 	struct zyd_softc *sc = rf->rf_sc;
   1341 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
   1342 	static const uint32_t rfini[] = ZYD_MAXIM_RF;
   1343 	static const struct {
   1344 		uint32_t	r1, r2;
   1345 	} rfprog[] = ZYD_MAXIM_CHANTABLE;
   1346 	uint16_t tmp;
   1347 	int i, error;
   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 < N(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 < N(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 #undef N
   1377 }
   1378 
   1379 /*
   1380  * Maxim2 RF methods.
   1381  */
   1382 Static int
   1383 zyd_maxim2_init(struct zyd_rf *rf)
   1384 {
   1385 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1386 	struct zyd_softc *sc = rf->rf_sc;
   1387 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
   1388 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
   1389 	uint16_t tmp;
   1390 	int i, error;
   1391 
   1392 	/* init RF-dependent PHY registers */
   1393 	for (i = 0; i < N(phyini); i++) {
   1394 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1395 		if (error != 0)
   1396 			return error;
   1397 	}
   1398 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1399 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1400 
   1401 	/* init maxim2 radio */
   1402 	for (i = 0; i < N(rfini); i++) {
   1403 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1404 			return error;
   1405 	}
   1406 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1407 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1408 
   1409 	return 0;
   1410 #undef N
   1411 }
   1412 
   1413 Static int
   1414 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
   1415 {
   1416 	/* vendor driver does nothing for this RF chip */
   1417 
   1418 	return 0;
   1419 }
   1420 
   1421 Static int
   1422 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
   1423 {
   1424 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
   1425 	struct zyd_softc *sc = rf->rf_sc;
   1426 	static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
   1427 	static const uint32_t rfini[] = ZYD_MAXIM2_RF;
   1428 	static const struct {
   1429 		uint32_t	r1, r2;
   1430 	} rfprog[] = ZYD_MAXIM2_CHANTABLE;
   1431 	uint16_t tmp;
   1432 	int i, error;
   1433 
   1434 	/*
   1435 	 * Do the same as we do when initializing it, except for the channel
   1436 	 * values coming from the two channel tables.
   1437 	 */
   1438 
   1439 	/* init RF-dependent PHY registers */
   1440 	for (i = 0; i < N(phyini); i++) {
   1441 		error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
   1442 		if (error != 0)
   1443 			return error;
   1444 	}
   1445 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1446 	(void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
   1447 
   1448 	/* first two values taken from the chantables */
   1449 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
   1450 	(void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
   1451 
   1452 	/* init maxim2 radio - skipping the two first values */
   1453 	for (i = 2; i < N(rfini); i++) {
   1454 		if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
   1455 			return error;
   1456 	}
   1457 	(void)zyd_read16(sc, ZYD_CR203, &tmp);
   1458 	(void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
   1459 
   1460 	return 0;
   1461 #undef N
   1462 }
   1463 
   1464 Static int
   1465 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
   1466 {
   1467 	struct zyd_rf *rf = &sc->sc_rf;
   1468 
   1469 	rf->rf_sc = sc;
   1470 
   1471 	switch (type) {
   1472 	case ZYD_RF_RFMD:
   1473 		rf->init         = zyd_rfmd_init;
   1474 		rf->switch_radio = zyd_rfmd_switch_radio;
   1475 		rf->set_channel  = zyd_rfmd_set_channel;
   1476 		rf->width        = 24;	/* 24-bit RF values */
   1477 		break;
   1478 	case ZYD_RF_AL2230:
   1479 		if (sc->mac_rev == ZYD_ZD1211B)
   1480 			rf->init = zyd_al2230_init_b;
   1481 		else
   1482 			rf->init = zyd_al2230_init;
   1483 		rf->switch_radio = zyd_al2230_switch_radio;
   1484 		rf->set_channel  = zyd_al2230_set_channel;
   1485 		rf->width        = 24;	/* 24-bit RF values */
   1486 		break;
   1487 	case ZYD_RF_AL7230B:
   1488 		rf->init         = zyd_al7230B_init;
   1489 		rf->switch_radio = zyd_al7230B_switch_radio;
   1490 		rf->set_channel  = zyd_al7230B_set_channel;
   1491 		rf->width        = 24;	/* 24-bit RF values */
   1492 		break;
   1493 	case ZYD_RF_AL2210:
   1494 		rf->init         = zyd_al2210_init;
   1495 		rf->switch_radio = zyd_al2210_switch_radio;
   1496 		rf->set_channel  = zyd_al2210_set_channel;
   1497 		rf->width        = 24;	/* 24-bit RF values */
   1498 		break;
   1499 	case ZYD_RF_GCT:
   1500 		rf->init         = zyd_gct_init;
   1501 		rf->switch_radio = zyd_gct_switch_radio;
   1502 		rf->set_channel  = zyd_gct_set_channel;
   1503 		rf->width        = 21;	/* 21-bit RF values */
   1504 		break;
   1505 	case ZYD_RF_MAXIM_NEW:
   1506 		rf->init         = zyd_maxim_init;
   1507 		rf->switch_radio = zyd_maxim_switch_radio;
   1508 		rf->set_channel  = zyd_maxim_set_channel;
   1509 		rf->width        = 18;	/* 18-bit RF values */
   1510 		break;
   1511 	case ZYD_RF_MAXIM_NEW2:
   1512 		rf->init         = zyd_maxim2_init;
   1513 		rf->switch_radio = zyd_maxim2_switch_radio;
   1514 		rf->set_channel  = zyd_maxim2_set_channel;
   1515 		rf->width        = 18;	/* 18-bit RF values */
   1516 		break;
   1517 	default:
   1518 		printf("%s: sorry, radio \"%s\" is not supported yet\n",
   1519 		    USBDEVNAME(sc->sc_dev), zyd_rf_name(type));
   1520 		return EINVAL;
   1521 	}
   1522 	return 0;
   1523 }
   1524 
   1525 Static const char *
   1526 zyd_rf_name(uint8_t type)
   1527 {
   1528 	static const char * const zyd_rfs[] = {
   1529 		"unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
   1530 		"AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
   1531 		"PV2000",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
   1532 		"PHILIPS"
   1533 	};
   1534 
   1535 	return zyd_rfs[(type > 15) ? 0 : type];
   1536 }
   1537 
   1538 Static int
   1539 zyd_hw_init(struct zyd_softc *sc)
   1540 {
   1541 	struct zyd_rf *rf = &sc->sc_rf;
   1542 	const struct zyd_phy_pair *phyp;
   1543 	int error;
   1544 
   1545 	/* specify that the plug and play is finished */
   1546 	(void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
   1547 
   1548 	(void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
   1549 	DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
   1550 
   1551 	/* retrieve firmware revision number */
   1552 	(void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
   1553 
   1554 	(void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
   1555 	(void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
   1556 
   1557 	/* disable interrupts */
   1558 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
   1559 
   1560 	/* PHY init */
   1561 	zyd_lock_phy(sc);
   1562 	phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
   1563 	for (; phyp->reg != 0; phyp++) {
   1564 		if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
   1565 			goto fail;
   1566 	}
   1567 	zyd_unlock_phy(sc);
   1568 
   1569 	/* HMAC init */
   1570 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
   1571 	zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
   1572 
   1573 	if (sc->mac_rev == ZYD_ZD1211) {
   1574 		zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
   1575 	} else {
   1576 		zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202);
   1577 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
   1578 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
   1579 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
   1580 		zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
   1581 		zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
   1582 		zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
   1583 		zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
   1584 	}
   1585 
   1586 	zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
   1587 	zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
   1588 	zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
   1589 	zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
   1590 	zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
   1591 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
   1592 	zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
   1593 	zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
   1594 	zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
   1595 	zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
   1596 	zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
   1597 	zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
   1598 	zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
   1599 	zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
   1600 	zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
   1601 	zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
   1602 	zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
   1603 
   1604 	/* RF chip init */
   1605 	zyd_lock_phy(sc);
   1606 	error = (*rf->init)(rf);
   1607 	zyd_unlock_phy(sc);
   1608 	if (error != 0) {
   1609 		printf("%s: radio initialization failed\n",
   1610 		    USBDEVNAME(sc->sc_dev));
   1611 		goto fail;
   1612 	}
   1613 
   1614 	/* init beacon interval to 100ms */
   1615 	if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
   1616 		goto fail;
   1617 
   1618 fail:	return error;
   1619 }
   1620 
   1621 Static int
   1622 zyd_read_eeprom(struct zyd_softc *sc)
   1623 {
   1624 	struct ieee80211com *ic = &sc->sc_ic;
   1625 	uint32_t tmp;
   1626 	uint16_t val;
   1627 	int i;
   1628 
   1629 	/* read MAC address */
   1630 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
   1631 	ic->ic_myaddr[0] = tmp & 0xff;
   1632 	ic->ic_myaddr[1] = tmp >>  8;
   1633 	ic->ic_myaddr[2] = tmp >> 16;
   1634 	ic->ic_myaddr[3] = tmp >> 24;
   1635 	(void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
   1636 	ic->ic_myaddr[4] = tmp & 0xff;
   1637 	ic->ic_myaddr[5] = tmp >>  8;
   1638 
   1639 	(void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
   1640 	sc->rf_rev = tmp & 0x0f;
   1641 	sc->pa_rev = (tmp >> 16) & 0x0f;
   1642 
   1643 	/* read regulatory domain (currently unused) */
   1644 	(void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
   1645 	sc->regdomain = tmp >> 16;
   1646 	DPRINTF(("regulatory domain %x\n", sc->regdomain));
   1647 
   1648 	/* read Tx power calibration tables */
   1649 	for (i = 0; i < 7; i++) {
   1650 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
   1651 		sc->pwr_cal[i * 2] = val >> 8;
   1652 		sc->pwr_cal[i * 2 + 1] = val & 0xff;
   1653 
   1654 		(void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
   1655 		sc->pwr_int[i * 2] = val >> 8;
   1656 		sc->pwr_int[i * 2 + 1] = val & 0xff;
   1657 
   1658 		(void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
   1659 		sc->ofdm36_cal[i * 2] = val >> 8;
   1660 		sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
   1661 
   1662 		(void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
   1663 		sc->ofdm48_cal[i * 2] = val >> 8;
   1664 		sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
   1665 
   1666 		(void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
   1667 		sc->ofdm54_cal[i * 2] = val >> 8;
   1668 		sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
   1669 	}
   1670 	return 0;
   1671 }
   1672 
   1673 Static int
   1674 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
   1675 {
   1676 	uint32_t tmp;
   1677 
   1678 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
   1679 	(void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
   1680 
   1681 	tmp = addr[5] << 8 | addr[4];
   1682 	(void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
   1683 
   1684 	return 0;
   1685 }
   1686 
   1687 Static int
   1688 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
   1689 {
   1690 	uint32_t tmp;
   1691 
   1692 	tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
   1693 	(void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
   1694 
   1695 	tmp = addr[5] << 8 | addr[4];
   1696 	(void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
   1697 
   1698 	return 0;
   1699 }
   1700 
   1701 Static int
   1702 zyd_switch_radio(struct zyd_softc *sc, int on)
   1703 {
   1704 	struct zyd_rf *rf = &sc->sc_rf;
   1705 	int error;
   1706 
   1707 	zyd_lock_phy(sc);
   1708 	error = (*rf->switch_radio)(rf, on);
   1709 	zyd_unlock_phy(sc);
   1710 
   1711 	return error;
   1712 }
   1713 
   1714 Static void
   1715 zyd_set_led(struct zyd_softc *sc, int which, int on)
   1716 {
   1717 	uint32_t tmp;
   1718 
   1719 	(void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
   1720 	tmp &= ~which;
   1721 	if (on)
   1722 		tmp |= which;
   1723 	(void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
   1724 }
   1725 
   1726 Static int
   1727 zyd_set_rxfilter(struct zyd_softc *sc)
   1728 {
   1729 	uint32_t rxfilter;
   1730 
   1731 	switch (sc->sc_ic.ic_opmode) {
   1732 	case IEEE80211_M_STA:
   1733 		rxfilter = ZYD_FILTER_BSS;
   1734 		break;
   1735 	case IEEE80211_M_IBSS:
   1736 	case IEEE80211_M_HOSTAP:
   1737 		rxfilter = ZYD_FILTER_HOSTAP;
   1738 		break;
   1739 	case IEEE80211_M_MONITOR:
   1740 		rxfilter = ZYD_FILTER_MONITOR;
   1741 		break;
   1742 	default:
   1743 		/* should not get there */
   1744 		return EINVAL;
   1745 	}
   1746 	return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
   1747 }
   1748 
   1749 Static void
   1750 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
   1751 {
   1752 	struct ieee80211com *ic = &sc->sc_ic;
   1753 	struct zyd_rf *rf = &sc->sc_rf;
   1754 	u_int chan;
   1755 
   1756 	chan = ieee80211_chan2ieee(ic, c);
   1757 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
   1758 		return;
   1759 
   1760 	zyd_lock_phy(sc);
   1761 
   1762 	(*rf->set_channel)(rf, chan);
   1763 
   1764 	/* update Tx power */
   1765 	(void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
   1766 	(void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
   1767 
   1768 	if (sc->mac_rev == ZYD_ZD1211B) {
   1769 		(void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
   1770 		(void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
   1771 		(void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
   1772 
   1773 		(void)zyd_write32(sc, ZYD_CR69, 0x28);
   1774 		(void)zyd_write32(sc, ZYD_CR69, 0x2a);
   1775 	}
   1776 
   1777 	zyd_unlock_phy(sc);
   1778 }
   1779 
   1780 Static int
   1781 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
   1782 {
   1783 	/* XXX this is probably broken.. */
   1784 	(void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
   1785 	(void)zyd_write32(sc, ZYD_CR_PRE_TBTT,        bintval - 1);
   1786 	(void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL,    bintval);
   1787 
   1788 	return 0;
   1789 }
   1790 
   1791 Static uint8_t
   1792 zyd_plcp_signal(int rate)
   1793 {
   1794 	switch (rate) {
   1795 	/* CCK rates (returned values are device-dependent) */
   1796 	case 2:		return 0x0;
   1797 	case 4:		return 0x1;
   1798 	case 11:	return 0x2;
   1799 	case 22:	return 0x3;
   1800 
   1801 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
   1802 	case 12:	return 0xb;
   1803 	case 18:	return 0xf;
   1804 	case 24:	return 0xa;
   1805 	case 36:	return 0xe;
   1806 	case 48:	return 0x9;
   1807 	case 72:	return 0xd;
   1808 	case 96:	return 0x8;
   1809 	case 108:	return 0xc;
   1810 
   1811 	/* unsupported rates (should not get there) */
   1812 	default:	return 0xff;
   1813 	}
   1814 }
   1815 
   1816 Static void
   1817 zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1818 {
   1819 	struct zyd_softc *sc = (struct zyd_softc *)priv;
   1820 	struct zyd_cmd *cmd;
   1821 	uint32_t datalen;
   1822 
   1823 	if (status != USBD_NORMAL_COMPLETION) {
   1824 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1825 			return;
   1826 
   1827 		if (status == USBD_STALLED) {
   1828 			usbd_clear_endpoint_stall_async(
   1829 			    sc->zyd_ep[ZYD_ENDPT_IIN]);
   1830 		}
   1831 		return;
   1832 	}
   1833 
   1834 	cmd = (struct zyd_cmd *)sc->ibuf;
   1835 
   1836 	if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
   1837 		struct zyd_notif_retry *retry =
   1838 		    (struct zyd_notif_retry *)cmd->data;
   1839 		struct ieee80211com *ic = &sc->sc_ic;
   1840 		struct ifnet *ifp = &sc->sc_if;
   1841 		struct ieee80211_node *ni;
   1842 
   1843 		DPRINTF(("retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
   1844 		    le16toh(retry->rate), ether_sprintf(retry->macaddr),
   1845 		    le16toh(retry->count) & 0xff, le16toh(retry->count)));
   1846 
   1847 		/*
   1848 		 * Find the node to which the packet was sent and update its
   1849 		 * retry statistics.  In BSS mode, this node is the AP we're
   1850 		 * associated to so no lookup is actually needed.
   1851 		 */
   1852 		if (ic->ic_opmode != IEEE80211_M_STA) {
   1853 			ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr);
   1854 			if (ni == NULL)
   1855 				return;	/* just ignore */
   1856 		} else
   1857 			ni = ic->ic_bss;
   1858 
   1859 		((struct zyd_node *)ni)->amn.amn_retrycnt++;
   1860 
   1861 		if (le16toh(retry->count) & 0x100)
   1862 			ifp->if_oerrors++;	/* too many retries */
   1863 
   1864 	} else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
   1865 		struct rq *rqp;
   1866 
   1867 		if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
   1868 			return;	/* HMAC interrupt */
   1869 
   1870 		usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
   1871 		datalen -= sizeof(cmd->code);
   1872 		datalen -= 2;	/* XXX: padding? */
   1873 
   1874 		SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) {
   1875 			int i;
   1876 
   1877 			if (sizeof(struct zyd_pair) * rqp->len != datalen)
   1878 				continue;
   1879 			for (i = 0; i < rqp->len; i++) {
   1880 				if (*(((const uint16_t *)rqp->idata) + i) !=
   1881 				    (((struct zyd_pair *)cmd->data) + i)->reg)
   1882 					break;
   1883 			}
   1884 			if (i != rqp->len)
   1885 				continue;
   1886 
   1887 			/* copy answer into caller-supplied buffer */
   1888 			bcopy(cmd->data, rqp->odata,
   1889 			    sizeof(struct zyd_pair) * rqp->len);
   1890 			wakeup(rqp->odata);	/* wakeup caller */
   1891 
   1892 			return;
   1893 		}
   1894 		return;	/* unexpected IORD notification */
   1895 	} else {
   1896 		printf("%s: unknown notification %x\n", USBDEVNAME(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 		    USBDEVNAME(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 		    USBDEVNAME(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 		    USBDEVNAME(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 			    USBDEVNAME(sc->sc_dev));
   1948 			m_freem(m);
   1949 			ifp->if_ierrors++;
   1950 			return;
   1951 		}
   1952 	}
   1953 	m->m_pkthdr.rcvif = ifp;
   1954 	m->m_pkthdr.len = m->m_len = rlen;
   1955 	bcopy((const uint8_t *)(plcp + 1), mtod(m, uint8_t *), rlen);
   1956 
   1957 	s = splnet();
   1958 
   1959 #if NBPFILTER > 0
   1960 	if (sc->sc_drvbpf != NULL) {
   1961 		struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
   1962 		static const uint8_t rates[] = {
   1963 			/* reverse function of zyd_plcp_signal() */
   1964 			2, 4, 11, 22, 0, 0, 0, 0,
   1965 			96, 48, 24, 12, 108, 72, 36, 18
   1966 		};
   1967 
   1968 		tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
   1969 		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
   1970 		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
   1971 		tap->wr_rssi = stat->rssi;
   1972 		tap->wr_rate = rates[plcp->signal & 0xf];
   1973 
   1974 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   1975 	}
   1976 #endif
   1977 
   1978 	wh = mtod(m, struct ieee80211_frame *);
   1979 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1980 	ieee80211_input(ic, m, ni, stat->rssi, 0);
   1981 
   1982 	/* node is no longer needed */
   1983 	ieee80211_free_node(ni);
   1984 
   1985 	splx(s);
   1986 }
   1987 
   1988 Static void
   1989 zyd_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1990 {
   1991 	struct zyd_rx_data *data = priv;
   1992 	struct zyd_softc *sc = data->sc;
   1993 	struct ifnet *ifp = &sc->sc_if;
   1994 	const struct zyd_rx_desc *desc;
   1995 	int len;
   1996 
   1997 	if (status != USBD_NORMAL_COMPLETION) {
   1998 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1999 			return;
   2000 
   2001 		if (status == USBD_STALLED)
   2002 			usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
   2003 
   2004 		goto skip;
   2005 	}
   2006 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   2007 
   2008 	if (len < ZYD_MIN_RXBUFSZ) {
   2009 		printf("%s: xfer too short (length=%d)\n",
   2010 		    USBDEVNAME(sc->sc_dev), len);
   2011 		ifp->if_ierrors++;
   2012 		goto skip;
   2013 	}
   2014 
   2015 	desc = (const struct zyd_rx_desc *)
   2016 	    (data->buf + len - sizeof (struct zyd_rx_desc));
   2017 
   2018 	if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
   2019 		const uint8_t *p = data->buf, *end = p + len;
   2020 		int i;
   2021 
   2022 		DPRINTFN(3, ("received multi-frame transfer\n"));
   2023 
   2024 		for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
   2025 			const uint16_t len16 = UGETW(desc->len[i]);
   2026 
   2027 			if (len16 == 0 || p + len16 > end)
   2028 				break;
   2029 
   2030 			zyd_rx_data(sc, p, len16);
   2031 			/* next frame is aligned on a 32-bit boundary */
   2032 			p += (len16 + 3) & ~3;
   2033 		}
   2034 	} else {
   2035 		DPRINTFN(3, ("received single-frame transfer\n"));
   2036 
   2037 		zyd_rx_data(sc, data->buf, len);
   2038 	}
   2039 
   2040 skip:	/* setup a new transfer */
   2041 	usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data, NULL,
   2042 	    ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
   2043 	    USBD_NO_TIMEOUT, zyd_rxeof);
   2044 	(void)usbd_transfer(xfer);
   2045 }
   2046 
   2047 Static int
   2048 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   2049 {
   2050 	struct ieee80211com *ic = &sc->sc_ic;
   2051 	struct ifnet *ifp = &sc->sc_if;
   2052 	struct zyd_tx_desc *desc;
   2053 	struct zyd_tx_data *data;
   2054 	struct ieee80211_frame *wh;
   2055 	int xferlen, totlen, rate;
   2056 	uint16_t pktlen;
   2057 	usbd_status error;
   2058 
   2059 	data = &sc->tx_data[0];
   2060 	desc = (struct zyd_tx_desc *)data->buf;
   2061 
   2062 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
   2063 
   2064 	data->ni = ni;
   2065 
   2066 	wh = mtod(m0, struct ieee80211_frame *);
   2067 
   2068 	xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
   2069 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
   2070 
   2071 	/* fill Tx descriptor */
   2072 	desc->len = htole16(totlen);
   2073 
   2074 	desc->flags = ZYD_TX_FLAG_BACKOFF;
   2075 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2076 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
   2077 		if (totlen > ic->ic_rtsthreshold) {
   2078 			desc->flags |= ZYD_TX_FLAG_RTS;
   2079 		} else if (ZYD_RATE_IS_OFDM(rate) &&
   2080 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
   2081 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   2082 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
   2083 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   2084 				desc->flags |= ZYD_TX_FLAG_RTS;
   2085 		}
   2086 	} else
   2087 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
   2088 
   2089 	if ((wh->i_fc[0] &
   2090 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   2091 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
   2092 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
   2093 
   2094 	desc->phy = zyd_plcp_signal(rate);
   2095 	if (ZYD_RATE_IS_OFDM(rate)) {
   2096 		desc->phy |= ZYD_TX_PHY_OFDM;
   2097 		if (ic->ic_curmode == IEEE80211_MODE_11A)
   2098 			desc->phy |= ZYD_TX_PHY_5GHZ;
   2099 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   2100 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
   2101 
   2102 	/* actual transmit length (XXX why +10?) */
   2103 	pktlen = sizeof (struct zyd_tx_desc) + 10;
   2104 	if (sc->mac_rev == ZYD_ZD1211)
   2105 		pktlen += totlen;
   2106 	desc->pktlen = htole16(pktlen);
   2107 
   2108 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
   2109 	desc->plcp_service = 0;
   2110 	if (rate == 22) {
   2111 		const int remainder = (16 * totlen) % 22;
   2112 		if (remainder != 0 && remainder < 7)
   2113 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
   2114 	}
   2115 
   2116 #if NBPFILTER > 0
   2117 	if (sc->sc_drvbpf != NULL) {
   2118 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
   2119 
   2120 		tap->wt_flags = 0;
   2121 		tap->wt_rate = rate;
   2122 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2123 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2124 
   2125 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   2126 	}
   2127 #endif
   2128 
   2129 	m_copydata(m0, 0, m0->m_pkthdr.len,
   2130 	    data->buf + sizeof (struct zyd_tx_desc));
   2131 
   2132 	DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n",
   2133 	    USBDEVNAME(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
   2134 
   2135 	m_freem(m0);	/* mbuf no longer needed */
   2136 
   2137 	usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
   2138 	    data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   2139 	    ZYD_TX_TIMEOUT, zyd_txeof);
   2140 	error = usbd_transfer(data->xfer);
   2141 	if (error != USBD_IN_PROGRESS && error != 0) {
   2142 		ifp->if_oerrors++;
   2143 		return EIO;
   2144 	}
   2145 	sc->tx_queued++;
   2146 
   2147 	return 0;
   2148 }
   2149 
   2150 Static void
   2151 zyd_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   2152 {
   2153 	struct zyd_tx_data *data = priv;
   2154 	struct zyd_softc *sc = data->sc;
   2155 	struct ifnet *ifp = &sc->sc_if;
   2156 	int s;
   2157 
   2158 	if (status != USBD_NORMAL_COMPLETION) {
   2159 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   2160 			return;
   2161 
   2162 		printf("%s: could not transmit buffer: %s\n",
   2163 		    USBDEVNAME(sc->sc_dev), usbd_errstr(status));
   2164 
   2165 		if (status == USBD_STALLED) {
   2166 			usbd_clear_endpoint_stall_async(
   2167 			    sc->zyd_ep[ZYD_ENDPT_BOUT]);
   2168 		}
   2169 		ifp->if_oerrors++;
   2170 		return;
   2171 	}
   2172 
   2173 	s = splnet();
   2174 
   2175 	/* update rate control statistics */
   2176 	((struct zyd_node *)data->ni)->amn.amn_txcnt++;
   2177 
   2178 	ieee80211_free_node(data->ni);
   2179 	data->ni = NULL;
   2180 
   2181 	sc->tx_queued--;
   2182 	ifp->if_opackets++;
   2183 
   2184 	sc->tx_timer = 0;
   2185 	ifp->if_flags &= ~IFF_OACTIVE;
   2186 	zyd_start(ifp);
   2187 
   2188 	splx(s);
   2189 }
   2190 
   2191 Static int
   2192 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
   2193 {
   2194 	struct ieee80211com *ic = &sc->sc_ic;
   2195 	struct ifnet *ifp = &sc->sc_if;
   2196 	struct zyd_tx_desc *desc;
   2197 	struct zyd_tx_data *data;
   2198 	struct ieee80211_frame *wh;
   2199 	struct ieee80211_key *k;
   2200 	int xferlen, totlen, rate;
   2201 	uint16_t pktlen;
   2202 	usbd_status error;
   2203 
   2204 	wh = mtod(m0, struct ieee80211_frame *);
   2205 
   2206 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
   2207 		rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
   2208 	else
   2209 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   2210 	rate &= IEEE80211_RATE_VAL;
   2211 
   2212 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2213 		k = ieee80211_crypto_encap(ic, ni, m0);
   2214 		if (k == NULL) {
   2215 			m_freem(m0);
   2216 			return ENOBUFS;
   2217 		}
   2218 
   2219 		/* packet header may have moved, reset our local pointer */
   2220 		wh = mtod(m0, struct ieee80211_frame *);
   2221 	}
   2222 
   2223 	data = &sc->tx_data[0];
   2224 	desc = (struct zyd_tx_desc *)data->buf;
   2225 
   2226 	data->ni = ni;
   2227 
   2228 	xferlen = sizeof (struct zyd_tx_desc) + m0->m_pkthdr.len;
   2229 	totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
   2230 
   2231 	/* fill Tx descriptor */
   2232 	desc->len = htole16(totlen);
   2233 
   2234 	desc->flags = ZYD_TX_FLAG_BACKOFF;
   2235 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2236 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
   2237 		if (totlen > ic->ic_rtsthreshold) {
   2238 			desc->flags |= ZYD_TX_FLAG_RTS;
   2239 		} else if (ZYD_RATE_IS_OFDM(rate) &&
   2240 		    (ic->ic_flags & IEEE80211_F_USEPROT)) {
   2241 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   2242 				desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
   2243 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   2244 				desc->flags |= ZYD_TX_FLAG_RTS;
   2245 		}
   2246 	} else
   2247 		desc->flags |= ZYD_TX_FLAG_MULTICAST;
   2248 
   2249 	if ((wh->i_fc[0] &
   2250 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   2251 	    (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
   2252 		desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
   2253 
   2254 	desc->phy = zyd_plcp_signal(rate);
   2255 	if (ZYD_RATE_IS_OFDM(rate)) {
   2256 		desc->phy |= ZYD_TX_PHY_OFDM;
   2257 		if (ic->ic_curmode == IEEE80211_MODE_11A)
   2258 			desc->phy |= ZYD_TX_PHY_5GHZ;
   2259 	} else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   2260 		desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
   2261 
   2262 	/* actual transmit length (XXX why +10?) */
   2263 	pktlen = sizeof (struct zyd_tx_desc) + 10;
   2264 	if (sc->mac_rev == ZYD_ZD1211)
   2265 		pktlen += totlen;
   2266 	desc->pktlen = htole16(pktlen);
   2267 
   2268 	desc->plcp_length = (16 * totlen + rate - 1) / rate;
   2269 	desc->plcp_service = 0;
   2270 	if (rate == 22) {
   2271 		const int remainder = (16 * totlen) % 22;
   2272 		if (remainder != 0 && remainder < 7)
   2273 			desc->plcp_service |= ZYD_PLCP_LENGEXT;
   2274 	}
   2275 
   2276 #if NBPFILTER > 0
   2277 	if (sc->sc_drvbpf != NULL) {
   2278 		struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
   2279 
   2280 		tap->wt_flags = 0;
   2281 		tap->wt_rate = rate;
   2282 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2283 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2284 
   2285 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   2286 	}
   2287 #endif
   2288 
   2289 	m_copydata(m0, 0, m0->m_pkthdr.len,
   2290 	    data->buf + sizeof (struct zyd_tx_desc));
   2291 
   2292 	DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n",
   2293 	    USBDEVNAME(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
   2294 
   2295 	m_freem(m0);	/* mbuf no longer needed */
   2296 
   2297 	usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BOUT], data,
   2298 	    data->buf, xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   2299 	    ZYD_TX_TIMEOUT, zyd_txeof);
   2300 	error = usbd_transfer(data->xfer);
   2301 	if (error != USBD_IN_PROGRESS && error != 0) {
   2302 		ifp->if_oerrors++;
   2303 		return EIO;
   2304 	}
   2305 	sc->tx_queued++;
   2306 
   2307 	return 0;
   2308 }
   2309 
   2310 Static void
   2311 zyd_start(struct ifnet *ifp)
   2312 {
   2313 	struct zyd_softc *sc = ifp->if_softc;
   2314 	struct ieee80211com *ic = &sc->sc_ic;
   2315 	struct ether_header *eh;
   2316 	struct ieee80211_node *ni;
   2317 	struct mbuf *m0;
   2318 
   2319 	for (;;) {
   2320 		IF_POLL(&ic->ic_mgtq, m0);
   2321 		if (m0 != NULL) {
   2322 			if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
   2323 				ifp->if_flags |= IFF_OACTIVE;
   2324 				break;
   2325 			}
   2326 			IF_DEQUEUE(&ic->ic_mgtq, m0);
   2327 
   2328 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   2329 			m0->m_pkthdr.rcvif = NULL;
   2330 #if NBPFILTER > 0
   2331 			if (ic->ic_rawbpf != NULL)
   2332 				bpf_mtap(ic->ic_rawbpf, m0);
   2333 #endif
   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 #if NBPFILTER > 0
   2359 			if (ifp->if_bpf != NULL)
   2360 				bpf_mtap(ifp->if_bpf, m0);
   2361 #endif
   2362 			if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
   2363 				ieee80211_free_node(ni);
   2364 				ifp->if_oerrors++;
   2365 				continue;
   2366 			}
   2367 #if NBPFILTER > 0
   2368 			if (ic->ic_rawbpf != NULL)
   2369 				bpf_mtap(ic->ic_rawbpf, m0);
   2370 #endif
   2371 			if (zyd_tx_data(sc, m0, ni) != 0) {
   2372 				ieee80211_free_node(ni);
   2373 				ifp->if_oerrors++;
   2374 				break;
   2375 			}
   2376 		}
   2377 
   2378 		sc->tx_timer = 5;
   2379 		ifp->if_timer = 1;
   2380 	}
   2381 }
   2382 
   2383 Static void
   2384 zyd_watchdog(struct ifnet *ifp)
   2385 {
   2386 	struct zyd_softc *sc = ifp->if_softc;
   2387 	struct ieee80211com *ic = &sc->sc_ic;
   2388 
   2389 	ifp->if_timer = 0;
   2390 
   2391 	if (sc->tx_timer > 0) {
   2392 		if (--sc->tx_timer == 0) {
   2393 			printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
   2394 			/* zyd_init(ifp); XXX needs a process context ? */
   2395 			ifp->if_oerrors++;
   2396 			return;
   2397 		}
   2398 		ifp->if_timer = 1;
   2399 	}
   2400 
   2401 	ieee80211_watchdog(ic);
   2402 }
   2403 
   2404 Static int
   2405 zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2406 {
   2407 	struct zyd_softc *sc = ifp->if_softc;
   2408 	struct ieee80211com *ic = &sc->sc_ic;
   2409 	int s, error = 0;
   2410 
   2411 	s = splnet();
   2412 
   2413 	switch (cmd) {
   2414 	case SIOCSIFFLAGS:
   2415 		if (ifp->if_flags & IFF_UP) {
   2416 			if (!(ifp->if_flags & IFF_RUNNING))
   2417 				zyd_init(ifp);
   2418 		} else {
   2419 			if (ifp->if_flags & IFF_RUNNING)
   2420 				zyd_stop(ifp, 1);
   2421 		}
   2422 		break;
   2423 
   2424 	default:
   2425 		if (!sc->attached)
   2426 			error = ENOTTY;
   2427 		else
   2428 			error = ieee80211_ioctl(ic, cmd, data);
   2429 	}
   2430 
   2431 	if (error == ENETRESET) {
   2432 		if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
   2433 		    (IFF_RUNNING | IFF_UP))
   2434 			zyd_init(ifp);
   2435 		error = 0;
   2436 	}
   2437 
   2438 	splx(s);
   2439 
   2440 	return error;
   2441 }
   2442 
   2443 Static int
   2444 zyd_init(struct ifnet *ifp)
   2445 {
   2446 	struct zyd_softc *sc = ifp->if_softc;
   2447 	struct ieee80211com *ic = &sc->sc_ic;
   2448 	int i, error;
   2449 
   2450 	if ((sc->sc_flags & ZD1211_FWLOADED) == 0)
   2451 		if ((error = zyd_attachhook(sc)) != 0)
   2452 			return error;
   2453 
   2454 	zyd_stop(ifp, 0);
   2455 
   2456 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
   2457 	DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
   2458 	error = zyd_set_macaddr(sc, ic->ic_myaddr);
   2459 	if (error != 0)
   2460 		return error;
   2461 
   2462 	/* we'll do software WEP decryption for now */
   2463 	DPRINTF(("setting encryption type\n"));
   2464 	error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
   2465 	if (error != 0)
   2466 		return error;
   2467 
   2468 	/* promiscuous mode */
   2469 	(void)zyd_write32(sc, ZYD_MAC_SNIFFER,
   2470 	    (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
   2471 
   2472 	(void)zyd_set_rxfilter(sc);
   2473 
   2474 	/* switch radio transmitter ON */
   2475 	(void)zyd_switch_radio(sc, 1);
   2476 
   2477 	/* set basic rates */
   2478 	if (ic->ic_curmode == IEEE80211_MODE_11B)
   2479 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
   2480 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
   2481 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
   2482 	else	/* assumes 802.11b/g */
   2483 		(void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
   2484 
   2485 	/* set mandatory rates */
   2486 	if (ic->ic_curmode == IEEE80211_MODE_11B)
   2487 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
   2488 	else if (ic->ic_curmode == IEEE80211_MODE_11A)
   2489 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
   2490 	else	/* assumes 802.11b/g */
   2491 		(void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
   2492 
   2493 	/* set default BSS channel */
   2494 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   2495 	zyd_set_chan(sc, ic->ic_bss->ni_chan);
   2496 
   2497 	/* enable interrupts */
   2498 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
   2499 
   2500 	/*
   2501 	 * Allocate Tx and Rx xfer queues.
   2502 	 */
   2503 	if ((error = zyd_alloc_tx_list(sc)) != 0) {
   2504 		printf("%s: could not allocate Tx list\n",
   2505 		    USBDEVNAME(sc->sc_dev));
   2506 		goto fail;
   2507 	}
   2508 	if ((error = zyd_alloc_rx_list(sc)) != 0) {
   2509 		printf("%s: could not allocate Rx list\n",
   2510 		    USBDEVNAME(sc->sc_dev));
   2511 		goto fail;
   2512 	}
   2513 
   2514 	/*
   2515 	 * Start up the receive pipe.
   2516 	 */
   2517 	for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
   2518 		struct zyd_rx_data *data = &sc->rx_data[i];
   2519 
   2520 		usbd_setup_xfer(data->xfer, sc->zyd_ep[ZYD_ENDPT_BIN], data,
   2521 		    NULL, ZYX_MAX_RXBUFSZ, USBD_NO_COPY | USBD_SHORT_XFER_OK,
   2522 		    USBD_NO_TIMEOUT, zyd_rxeof);
   2523 		error = usbd_transfer(data->xfer);
   2524 		if (error != USBD_IN_PROGRESS && error != 0) {
   2525 			printf("%s: could not queue Rx transfer\n",
   2526 			    USBDEVNAME(sc->sc_dev));
   2527 			goto fail;
   2528 		}
   2529 	}
   2530 
   2531 	ifp->if_flags &= ~IFF_OACTIVE;
   2532 	ifp->if_flags |= IFF_RUNNING;
   2533 
   2534 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2535 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2536 	else
   2537 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2538 
   2539 	return 0;
   2540 
   2541 fail:	zyd_stop(ifp, 1);
   2542 	return error;
   2543 }
   2544 
   2545 Static void
   2546 zyd_stop(struct ifnet *ifp, int disable)
   2547 {
   2548 	struct zyd_softc *sc = ifp->if_softc;
   2549 	struct ieee80211com *ic = &sc->sc_ic;
   2550 
   2551 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
   2552 
   2553 	sc->tx_timer = 0;
   2554 	ifp->if_timer = 0;
   2555 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2556 
   2557 	/* switch radio transmitter OFF */
   2558 	(void)zyd_switch_radio(sc, 0);
   2559 
   2560 	/* disable Rx */
   2561 	(void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
   2562 
   2563 	/* disable interrupts */
   2564 	(void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
   2565 
   2566 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
   2567 	usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
   2568 
   2569 	zyd_free_rx_list(sc);
   2570 	zyd_free_tx_list(sc);
   2571 }
   2572 
   2573 Static int
   2574 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
   2575 {
   2576 	usb_device_request_t req;
   2577 	uint16_t addr;
   2578 	uint8_t stat;
   2579 
   2580 	DPRINTF(("firmware size=%zu\n", size));
   2581 
   2582 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
   2583 	req.bRequest = ZYD_DOWNLOADREQ;
   2584 	USETW(req.wIndex, 0);
   2585 
   2586 	addr = ZYD_FIRMWARE_START_ADDR;
   2587 	while (size > 0) {
   2588 #if 0
   2589 		const int mlen = min(size, 4096);
   2590 #else
   2591 		/*
   2592 		 * XXXX: When the transfer size is 4096 bytes, it is not
   2593 		 * likely to be able to transfer it.
   2594 		 * The cause is port or machine or chip?
   2595 		 */
   2596 		const int mlen = min(size, 64);
   2597 #endif
   2598 
   2599 		DPRINTF(("loading firmware block: len=%d, addr=0x%x\n", mlen,
   2600 		    addr));
   2601 
   2602 		USETW(req.wValue, addr);
   2603 		USETW(req.wLength, mlen);
   2604 		if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
   2605 			return EIO;
   2606 
   2607 		addr += mlen / 2;
   2608 		fw   += mlen;
   2609 		size -= mlen;
   2610 	}
   2611 
   2612 	/* check whether the upload succeeded */
   2613 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   2614 	req.bRequest = ZYD_DOWNLOADSTS;
   2615 	USETW(req.wValue, 0);
   2616 	USETW(req.wIndex, 0);
   2617 	USETW(req.wLength, sizeof stat);
   2618 	if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
   2619 		return EIO;
   2620 
   2621 	return (stat & 0x80) ? EIO : 0;
   2622 }
   2623 
   2624 Static void
   2625 zyd_iter_func(void *arg, struct ieee80211_node *ni)
   2626 {
   2627 	struct zyd_softc *sc = arg;
   2628 	struct zyd_node *zn = (struct zyd_node *)ni;
   2629 
   2630 	ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
   2631 }
   2632 
   2633 Static void
   2634 zyd_amrr_timeout(void *arg)
   2635 {
   2636 	struct zyd_softc *sc = arg;
   2637 	struct ieee80211com *ic = &sc->sc_ic;
   2638 	int s;
   2639 
   2640 	s = splnet();
   2641 	if (ic->ic_opmode == IEEE80211_M_STA)
   2642 		zyd_iter_func(sc, ic->ic_bss);
   2643 	else
   2644 		ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc);
   2645 	splx(s);
   2646 
   2647 	usb_callout(sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
   2648 }
   2649 
   2650 Static void
   2651 zyd_newassoc(struct ieee80211_node *ni, int isnew)
   2652 {
   2653 	struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc;
   2654 	int i;
   2655 
   2656 	ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
   2657 
   2658 	/* set rate to some reasonable initial value */
   2659 	for (i = ni->ni_rates.rs_nrates - 1;
   2660 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
   2661 	     i--);
   2662 	ni->ni_txrate = i;
   2663 }
   2664 
   2665 int
   2666 zyd_activate(device_ptr_t self, enum devact act)
   2667 {
   2668 	struct zyd_softc *sc = (struct zyd_softc *)self;
   2669 
   2670 	switch (act) {
   2671 	case DVACT_ACTIVATE:
   2672 		break;
   2673 
   2674 	case DVACT_DEACTIVATE:
   2675 		if_deactivate(&sc->sc_if);
   2676 		break;
   2677 	}
   2678 	return 0;
   2679 }
   2680