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