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