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