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