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