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